Logo AND Algorithmique Numérique Distribuée

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