Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://github.com/mpoquet/simgrid
[simgrid.git] / src / plugins / vm / VirtualMachineImpl.cpp
1 /* Copyright (c) 2013-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "src/plugins/vm/VirtualMachineImpl.hpp"
8 #include "simgrid/s4u/VirtualMachine.hpp"
9
10 #include <xbt/signal.hpp>
11
12 #include "src/surf/cpu_cas01.hpp"
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_vm, surf, "Logging specific to the SURF VM module");
15
16 simgrid::vm::VMModel* surf_vm_model = nullptr;
17
18 void surf_vm_model_init_HL13()
19 {
20   if (surf_cpu_model_vm) {
21     surf_vm_model = new simgrid::vm::VMModel();
22     all_existing_models->push_back(surf_vm_model);
23   }
24 }
25
26 namespace simgrid {
27 namespace vm {
28
29 /*************
30  * Callbacks *
31  *************/
32
33 simgrid::xbt::signal<void(simgrid::vm::VirtualMachineImpl*)> onVmCreation;
34 simgrid::xbt::signal<void(simgrid::vm::VirtualMachineImpl*)> onVmDestruction;
35 simgrid::xbt::signal<void(simgrid::vm::VirtualMachineImpl*)> onVmStateChange;
36
37 /*********
38  * Model *
39  *********/
40
41 std::deque<s4u::VirtualMachine*> VirtualMachineImpl::allVms_;
42
43 /* In the real world, processes on the guest operating system will be somewhat degraded due to virtualization overhead.
44  * The total CPU share these processes get is smaller than that of the VM process gets on a host operating system. */
45 // const double virt_overhead = 0.95;
46 const double virt_overhead = 1;
47
48 double VMModel::nextOccuringEvent(double now)
49 {
50   /* TODO: update action's cost with the total cost of processes on the VM. */
51
52   /* 1. Now we know how many resource should be assigned to each virtual
53    * machine. We update constraints of the virtual machine layer.
54    *
55    * If we have two virtual machine (VM1 and VM2) on a physical machine (PM1).
56    *     X1 + X2 = C       (Equation 1)
57    * where
58    *    the resource share of VM1: X1
59    *    the resource share of VM2: X2
60    *    the capacity of PM1: C
61    *
62    * Then, if we have two process (P1 and P2) on VM1.
63    *     X1_1 + X1_2 = X1  (Equation 2)
64    * where
65    *    the resource share of P1: X1_1
66    *    the resource share of P2: X1_2
67    *    the capacity of VM1: X1
68    *
69    * Equation 1 was solved in the physical machine layer.
70    * Equation 2 is solved in the virtual machine layer (here).
71    * X1 must be passed to the virtual machine laye as a constraint value.
72    **/
73
74   /* iterate for all virtual machines */
75   for (s4u::VirtualMachine* ws_vm : VirtualMachineImpl::allVms_) {
76     surf::Cpu* cpu = ws_vm->pimpl_cpu;
77     xbt_assert(cpu, "cpu-less host");
78
79     double solved_value = ws_vm->pimpl_vm_->action_->getVariable()->value;
80     XBT_DEBUG("assign %f to vm %s @ pm %s", solved_value, ws_vm->name().c_str(),
81               ws_vm->pimpl_vm_->getPm()->name().c_str());
82
83     // TODO: check lmm_update_constraint_bound() works fine instead of the below manual substitution.
84     // cpu_cas01->constraint->bound = solved_value;
85     xbt_assert(cpu->getModel() == surf_cpu_model_vm);
86     lmm_system_t vcpu_system = cpu->getModel()->getMaxminSystem();
87     lmm_update_constraint_bound(vcpu_system, cpu->getConstraint(), virt_overhead * solved_value);
88   }
89
90   /* 2. Calculate resource share at the virtual machine layer. */
91   adjustWeightOfDummyCpuActions();
92
93   /* 3. Ready. Get the next occuring event */
94   return surf_cpu_model_vm->nextOccuringEvent(now);
95 }
96
97 /************
98  * Resource *
99  ************/
100
101 VirtualMachineImpl::VirtualMachineImpl(simgrid::s4u::VirtualMachine* piface, simgrid::s4u::Host* host_PM)
102     : HostImpl(piface, nullptr /*storage*/), hostPM_(host_PM)
103 {
104   /* Register this VM to the list of all VMs */
105   allVms_.push_back(piface);
106
107   /* We create cpu_action corresponding to a VM process on the host operating system. */
108   /* FIXME: TODO: we have to periodically input GUESTOS_NOISE to the system? how ? */
109   action_ = host_PM->pimpl_cpu->execution_start(0);
110
111   /* Initialize the VM parameters */
112   params_.ramsize = 0;
113
114   XBT_VERB("Create VM(%s)@PM(%s)", piface->name().c_str(), hostPM_->name().c_str());
115 }
116
117 extern "C" int
118     xbt_log_no_loc; /* ugly pimpl to ensure that the debug info in the known issue below don't break the test */
119 /** @brief A physical host does not disappear in the current SimGrid code, but a VM may disappear during a simulation */
120 VirtualMachineImpl::~VirtualMachineImpl()
121 {
122   onVmDestruction(this);
123   allVms_.erase(find(allVms_.begin(), allVms_.end(), piface_));
124
125   /* dirty page tracking */
126   unsigned int size          = xbt_dict_size(dp_objs);
127   static bool already_warned = false;
128   if (size > 0 && !already_warned) {
129     xbt_dict_cursor_t cursor = nullptr;
130     xbt_dict_cursor_first(dp_objs, &cursor);
131     XBT_WARN("Dirty page tracking: %u pending task(s) on a destroyed VM (first one is %s).\n"
132              "If you don't understand why your task was not properly removed, please report that bug.\n"
133              "This is a known bug if you turned the host off during the VM execution.\n"
134              "Please remind us of that problem at some point: our code base is not ready to fix this harmless issue in "
135              "2016, sorry.",
136              size, (xbt_log_no_loc ? "(name hidden)" : xbt_dict_cursor_get_key(cursor)));
137     xbt_dict_cursor_free(&cursor);
138     already_warned = true;
139   }
140   xbt_dict_free(&dp_objs);
141
142   /* Free the cpu_action of the VM. */
143   XBT_ATTRIB_UNUSED int ret = action_->unref();
144   xbt_assert(ret == 1, "Bug: some resource still remains");
145 }
146
147 e_surf_vm_state_t VirtualMachineImpl::getState()
148 {
149   return vmState_;
150 }
151
152 void VirtualMachineImpl::setState(e_surf_vm_state_t state)
153 {
154   vmState_ = state;
155 }
156 void VirtualMachineImpl::suspend()
157 {
158   action_->suspend();
159   vmState_ = SURF_VM_STATE_SUSPENDED;
160 }
161
162 void VirtualMachineImpl::resume()
163 {
164   action_->resume();
165   vmState_ = SURF_VM_STATE_RUNNING;
166 }
167
168 void VirtualMachineImpl::save()
169 {
170   vmState_ = SURF_VM_STATE_SAVING;
171   action_->suspend();
172   vmState_ = SURF_VM_STATE_SAVED;
173 }
174
175 void VirtualMachineImpl::restore()
176 {
177   vmState_ = SURF_VM_STATE_RESTORING;
178   action_->resume();
179   vmState_ = SURF_VM_STATE_RUNNING;
180 }
181
182 /** @brief returns the physical machine on which the VM is running **/
183 s4u::Host* VirtualMachineImpl::getPm()
184 {
185   return hostPM_;
186 }
187
188 /* Update the physical host of the given VM */
189 void VirtualMachineImpl::migrate(s4u::Host* host_dest)
190 {
191   const char* vm_name     = piface_->name().c_str();
192   const char* pm_name_src = hostPM_->name().c_str();
193   const char* pm_name_dst = host_dest->name().c_str();
194
195   /* update net_elm with that of the destination physical host */
196   piface_->pimpl_netcard = host_dest->pimpl_netcard;
197
198   hostPM_ = host_dest;
199
200   /* Update vcpu's action for the new pm */
201   /* create a cpu action bound to the pm model at the destination. */
202   surf::CpuAction* new_cpu_action = static_cast<surf::CpuAction*>(host_dest->pimpl_cpu->execution_start(0));
203
204   surf::Action::State state = action_->getState();
205   if (state != surf::Action::State::done)
206     XBT_CRITICAL("FIXME: may need a proper handling, %d", static_cast<int>(state));
207   if (action_->getRemainsNoUpdate() > 0)
208     XBT_CRITICAL("FIXME: need copy the state(?), %f", action_->getRemainsNoUpdate());
209
210   /* keep the bound value of the cpu action of the VM. */
211   double old_bound = action_->getBound();
212   if (old_bound != 0) {
213     XBT_DEBUG("migrate VM(%s): set bound (%f) at %s", vm_name, old_bound, pm_name_dst);
214     new_cpu_action->setBound(old_bound);
215   }
216
217   XBT_ATTRIB_UNUSED int ret = action_->unref();
218   xbt_assert(ret == 1, "Bug: some resource still remains");
219
220   action_ = new_cpu_action;
221
222   XBT_DEBUG("migrate VM(%s): change PM (%s to %s)", vm_name, pm_name_src, pm_name_dst);
223 }
224
225 sg_size_t VirtualMachineImpl::getRamsize()
226 {
227   return params_.ramsize;
228 }
229
230 void VirtualMachineImpl::setBound(double bound)
231 {
232   action_->setBound(bound);
233 }
234
235 void VirtualMachineImpl::getParams(vm_params_t params)
236 {
237   *params = params_;
238 }
239
240 void VirtualMachineImpl::setParams(vm_params_t params)
241 {
242   /* may check something here. */
243   params_ = *params;
244 }
245 }
246 }