Logo AND Algorithmique Numérique Distribuée

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