Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Reorganize VM code
[simgrid.git] / src / plugins / vm / dirty_page_tracking.cpp
1 /* Copyright (c) 2017-2021. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <simgrid/plugins/live_migration.h>
7 #include <simgrid/s4u/Exec.hpp>
8 #include <simgrid/s4u/VirtualMachine.hpp>
9
10 #include "src/kernel/activity/ExecImpl.hpp"
11 #include "src/kernel/resource/VirtualMachineImpl.hpp"
12
13 namespace simgrid {
14 namespace plugin {
15 namespace vm {
16 class DirtyPageTrackingExt {
17   bool dp_tracking_ = false;
18   std::map<kernel::activity::ExecImpl const*, double> dp_objs_;
19   double dp_updated_by_deleted_tasks_ = 0.0;
20   // Percentage of pages that get dirty compared to netspeed [0;1] bytes per 1 flop execution
21   double dp_intensity_          = 0.0;
22   sg_size_t working_set_memory_ = 0.0;
23   double max_downtime_          = 0.03;
24   double mig_speed_             = 0.0;
25
26 public:
27   void start_tracking();
28   void stop_tracking() { dp_tracking_ = false; }
29   bool is_tracking() const { return dp_tracking_; }
30   void track(kernel::activity::ExecImpl const* exec, double amount) { dp_objs_.insert({exec, amount}); }
31   void untrack(kernel::activity::ExecImpl const* exec) { dp_objs_.erase(exec); }
32   double get_stored_remains(kernel::activity::ExecImpl const* exec) { return dp_objs_.at(exec); }
33   void update_dirty_page_count(double delta) { dp_updated_by_deleted_tasks_ += delta; }
34   double computed_flops_lookup();
35   double get_intensity() const { return dp_intensity_; }
36   void set_intensity(double intensity) { dp_intensity_ = intensity; }
37   sg_size_t get_working_set_memory() const { return working_set_memory_; }
38   void set_working_set_memory(sg_size_t size) { working_set_memory_ = size; }
39   void set_migration_speed(double speed) { mig_speed_ = speed; }
40   double get_migration_speed() const { return mig_speed_; }
41   double get_max_downtime() const { return max_downtime_; }
42
43   static simgrid::xbt::Extension<kernel::resource::VirtualMachineImpl, DirtyPageTrackingExt> EXTENSION_ID;
44   DirtyPageTrackingExt() = default;
45 };
46
47 simgrid::xbt::Extension<kernel::resource::VirtualMachineImpl, DirtyPageTrackingExt> DirtyPageTrackingExt::EXTENSION_ID;
48
49 void DirtyPageTrackingExt::start_tracking()
50 {
51   dp_tracking_ = true;
52   for (auto const& elm : dp_objs_)
53     dp_objs_[elm.first] = elm.first->get_remaining();
54 }
55
56 double DirtyPageTrackingExt::computed_flops_lookup()
57 {
58   double total = 0;
59
60   for (auto const& elm : dp_objs_) {
61     total += elm.second - elm.first->get_remaining();
62     dp_objs_[elm.first] = elm.first->get_remaining();
63   }
64   total += dp_updated_by_deleted_tasks_;
65
66   dp_updated_by_deleted_tasks_ = 0;
67
68   return total;
69 }
70 } // namespace vm
71 } // namespace plugin
72 } // namespace simgrid
73
74 using simgrid::plugin::vm::DirtyPageTrackingExt;
75
76 static void on_virtual_machine_creation(const simgrid::s4u::VirtualMachine& vm)
77 {
78   vm.get_vm_impl()->extension_set<DirtyPageTrackingExt>(new DirtyPageTrackingExt());
79 }
80
81 static void on_exec_creation(simgrid::s4u::Exec const& e)
82 {
83   auto exec                              = static_cast<simgrid::kernel::activity::ExecImpl*>(e.get_impl());
84   const simgrid::s4u::VirtualMachine* vm = dynamic_cast<simgrid::s4u::VirtualMachine*>(exec->get_host());
85   if (vm == nullptr)
86     return;
87
88   if (vm->get_vm_impl()->extension<DirtyPageTrackingExt>()->is_tracking()) {
89     vm->get_vm_impl()->extension<DirtyPageTrackingExt>()->track(exec, exec->get_remaining());
90   } else {
91     vm->get_vm_impl()->extension<DirtyPageTrackingExt>()->track(exec, 0.0);
92   }
93 }
94
95 static void on_exec_completion(simgrid::s4u::Exec const& e)
96 {
97   auto exec                              = static_cast<simgrid::kernel::activity::ExecImpl*>(e.get_impl());
98   const simgrid::s4u::VirtualMachine* vm = dynamic_cast<simgrid::s4u::VirtualMachine*>(exec->get_host());
99   if (vm == nullptr)
100     return;
101
102   /* If we are in the middle of dirty page tracking, we record how much computation has been done until now, and keep
103    * the information for the lookup_() function that will called soon. */
104   if (vm->get_vm_impl()->extension<DirtyPageTrackingExt>()->is_tracking()) {
105     double delta = vm->get_vm_impl()->extension<DirtyPageTrackingExt>()->get_stored_remains(exec);
106     vm->get_vm_impl()->extension<DirtyPageTrackingExt>()->update_dirty_page_count(delta);
107   }
108   vm->get_vm_impl()->extension<DirtyPageTrackingExt>()->untrack(exec);
109 }
110
111 void sg_vm_dirty_page_tracking_init()
112 {
113   if (not DirtyPageTrackingExt::EXTENSION_ID.valid()) {
114     DirtyPageTrackingExt::EXTENSION_ID =
115         simgrid::kernel::resource::VirtualMachineImpl::extension_create<DirtyPageTrackingExt>();
116     simgrid::s4u::VirtualMachine::on_creation.connect(&on_virtual_machine_creation);
117     simgrid::s4u::Exec::on_start.connect(&on_exec_creation);
118     simgrid::s4u::Exec::on_completion.connect(&on_exec_completion);
119   }
120 }
121
122 void sg_vm_start_dirty_page_tracking(const_sg_vm_t vm)
123 {
124   vm->get_vm_impl()->extension<DirtyPageTrackingExt>()->start_tracking();
125 }
126
127 void sg_vm_stop_dirty_page_tracking(const_sg_vm_t vm)
128 {
129   vm->get_vm_impl()->extension<DirtyPageTrackingExt>()->stop_tracking();
130 }
131
132 double sg_vm_lookup_computed_flops(const_sg_vm_t vm)
133 {
134   return vm->get_vm_impl()->extension<DirtyPageTrackingExt>()->computed_flops_lookup();
135 }
136
137 void sg_vm_set_dirty_page_intensity(const_sg_vm_t vm, double intensity)
138 {
139   vm->get_vm_impl()->extension<DirtyPageTrackingExt>()->set_intensity(intensity);
140 }
141
142 double sg_vm_get_dirty_page_intensity(const_sg_vm_t vm)
143 {
144   return vm->get_vm_impl()->extension<DirtyPageTrackingExt>()->get_intensity();
145 }
146
147 void sg_vm_set_working_set_memory(const_sg_vm_t vm, sg_size_t size)
148 {
149   vm->get_vm_impl()->extension<DirtyPageTrackingExt>()->set_working_set_memory(size);
150 }
151
152 sg_size_t sg_vm_get_working_set_memory(const_sg_vm_t vm)
153 {
154   return vm->get_vm_impl()->extension<DirtyPageTrackingExt>()->get_working_set_memory();
155 }
156
157 void sg_vm_set_migration_speed(const_sg_vm_t vm, double speed)
158 {
159   vm->get_vm_impl()->extension<DirtyPageTrackingExt>()->set_migration_speed(speed);
160 }
161
162 double sg_vm_get_migration_speed(const_sg_vm_t vm)
163 {
164   return vm->get_vm_impl()->extension<DirtyPageTrackingExt>()->get_migration_speed();
165 }
166
167 double sg_vm_get_max_downtime(const_sg_vm_t vm)
168 {
169   return vm->get_vm_impl()->extension<DirtyPageTrackingExt>()->get_max_downtime();
170 }