Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b14dbc6ad9e6691c2ad39f0f238a2c2bbdd8e299
[simgrid.git] / src / surf / 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 VmDirtyPageTrackingExt {
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 startTracking();
23   void stopTracking() { dp_tracking = false; }
24   bool isTracking() { 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 getStoredRemains(kernel::activity::ExecImplPtr exec) { return dp_objs.at(exec); }
28   void updateDirtyPageCount(double delta) { dp_updated_by_deleted_tasks += delta; }
29   double computedFlopsLookup();
30   double getIntensity() { return dp_intensity; }
31   void setIntensity(double intensity) { dp_intensity = intensity; }
32   double getWorkingSetMemory() { return working_set_memory; }
33   void setWorkingSetMemory(sg_size_t size) { working_set_memory = size; }
34   void setMigrationSpeed(double speed) { mig_speed = speed; }
35   double getMigrationSpeed() { return mig_speed; }
36   double getMaxDowntime() { return max_downtime; }
37
38   static simgrid::xbt::Extension<VirtualMachineImpl, VmDirtyPageTrackingExt> EXTENSION_ID;
39   virtual ~VmDirtyPageTrackingExt() = default;
40   VmDirtyPageTrackingExt()          = default;
41 };
42
43 simgrid::xbt::Extension<VirtualMachineImpl, VmDirtyPageTrackingExt> VmDirtyPageTrackingExt::EXTENSION_ID;
44
45 void VmDirtyPageTrackingExt::startTracking()
46 {
47   dp_tracking = true;
48   for (auto const& elm : dp_objs)
49     dp_objs[elm.first] = elm.first->remains();
50 }
51
52 double VmDirtyPageTrackingExt::computedFlopsLookup()
53 {
54   double total = 0;
55
56   for (auto const& elm : dp_objs) {
57     total += elm.second - elm.first->remains();
58     dp_objs[elm.first] = elm.first->remains();
59   }
60   total += dp_updated_by_deleted_tasks;
61
62   dp_updated_by_deleted_tasks = 0;
63
64   return total;
65 }
66 }
67 }
68
69 static void onVirtualMachineCreation(simgrid::vm::VirtualMachineImpl* vm)
70 {
71   vm->extension_set<simgrid::vm::VmDirtyPageTrackingExt>(new simgrid::vm::VmDirtyPageTrackingExt());
72 }
73
74 static void onExecCreation(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->getImpl()->extension<simgrid::vm::VmDirtyPageTrackingExt>()->isTracking()) {
81     vm->getImpl()->extension<simgrid::vm::VmDirtyPageTrackingExt>()->track(exec, exec->remains());
82   } else {
83     vm->getImpl()->extension<simgrid::vm::VmDirtyPageTrackingExt>()->track(exec, 0.0);
84   }
85 }
86
87 static void onExecCompletion(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->getImpl()->extension<simgrid::vm::VmDirtyPageTrackingExt>()->isTracking()) {
96     double delta =
97         vm->getImpl()->extension<simgrid::vm::VmDirtyPageTrackingExt>()->getStoredRemains(exec) - exec->remains();
98     vm->getImpl()->extension<simgrid::vm::VmDirtyPageTrackingExt>()->updateDirtyPageCount(delta);
99   }
100   vm->getImpl()->extension<simgrid::vm::VmDirtyPageTrackingExt>()->untrack(exec);
101 }
102
103 void sg_vm_dirty_page_tracking_init()
104 {
105   if (not simgrid::vm::VmDirtyPageTrackingExt::EXTENSION_ID.valid()) {
106     simgrid::vm::VmDirtyPageTrackingExt::EXTENSION_ID =
107         simgrid::vm::VirtualMachineImpl::extension_create<simgrid::vm::VmDirtyPageTrackingExt>();
108     simgrid::vm::VirtualMachineImpl::onVmCreation.connect(&onVirtualMachineCreation);
109     simgrid::kernel::activity::ExecImpl::onCreation.connect(&onExecCreation);
110     simgrid::kernel::activity::ExecImpl::onCompletion.connect(&onExecCompletion);
111   }
112 }
113
114 void sg_vm_start_dirty_page_tracking(sg_vm_t vm)
115 {
116   vm->getImpl()->extension<simgrid::vm::VmDirtyPageTrackingExt>()->startTracking();
117 }
118
119 void sg_vm_stop_dirty_page_tracking(sg_vm_t vm)
120 {
121   vm->getImpl()->extension<simgrid::vm::VmDirtyPageTrackingExt>()->stopTracking();
122 }
123
124 double sg_vm_lookup_computed_flops(sg_vm_t vm)
125 {
126   return vm->getImpl()->extension<simgrid::vm::VmDirtyPageTrackingExt>()->computedFlopsLookup();
127 }
128
129 void sg_vm_set_dirty_page_intensity(sg_vm_t vm, double intensity)
130 {
131   vm->getImpl()->extension<simgrid::vm::VmDirtyPageTrackingExt>()->setIntensity(intensity);
132 }
133
134 double sg_vm_get_dirty_page_intensity(sg_vm_t vm)
135 {
136   return vm->getImpl()->extension<simgrid::vm::VmDirtyPageTrackingExt>()->getIntensity();
137 }
138
139 void sg_vm_set_working_set_memory(sg_vm_t vm, sg_size_t size)
140 {
141   vm->getImpl()->extension<simgrid::vm::VmDirtyPageTrackingExt>()->setWorkingSetMemory(size);
142 }
143
144 sg_size_t sg_vm_get_working_set_memory(sg_vm_t vm)
145 {
146   return vm->getImpl()->extension<simgrid::vm::VmDirtyPageTrackingExt>()->getWorkingSetMemory();
147 }
148
149 void sg_vm_set_migration_speed(sg_vm_t vm, double speed)
150 {
151   vm->getImpl()->extension<simgrid::vm::VmDirtyPageTrackingExt>()->setMigrationSpeed(speed);
152 }
153
154 double sg_vm_get_migration_speed(sg_vm_t vm)
155 {
156   return vm->getImpl()->extension<simgrid::vm::VmDirtyPageTrackingExt>()->getMigrationSpeed();
157 }
158
159 double sg_vm_get_max_downtime(sg_vm_t vm)
160 {
161   return vm->getImpl()->extension<simgrid::vm::VmDirtyPageTrackingExt>()->getMaxDowntime();
162 }