Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Kill a VM-related function out of HostImpl
[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::surf::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::surf::VMModel();
22     all_existing_models->push_back(surf_vm_model);
23   }
24 }
25
26 namespace simgrid {
27 namespace surf {
28
29 /*************
30  * Callbacks *
31  *************/
32
33 simgrid::xbt::signal<void(simgrid::surf::VirtualMachineImpl*)> onVmCreation;
34 simgrid::xbt::signal<void(simgrid::surf::VirtualMachineImpl*)> onVmDestruction;
35 simgrid::xbt::signal<void(simgrid::surf::VirtualMachineImpl*)> onVmStateChange;
36
37 /*********
38  * Model *
39  *********/
40
41 std::deque<VirtualMachineImpl*> 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 (VirtualMachineImpl* ws_vm : VirtualMachineImpl::allVms_) {
76     Cpu* cpu = ws_vm->piface_->pimpl_cpu;
77     xbt_assert(cpu, "cpu-less host");
78
79     double solved_value = ws_vm->action_->getVariable()->value;
80     XBT_DEBUG("assign %f to vm %s @ pm %s", solved_value, ws_vm->piface_->name().c_str(),
81               ws_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::Host* 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(this);
106
107   /* Currently, a VM uses the network resource of its physical host. In
108    * host_lib, this network resource object is referred from two different keys.
109    * When deregistering the reference that points the network resource object
110    * from the VM name, we have to make sure that the system does not call the
111    * free callback for the network resource object. The network resource object
112    * is still used by the physical machine. */
113   piface_->pimpl_netcard = host_PM->pimpl_netcard;
114
115   // //// CPU  RELATED STUFF ////
116   // Roughly, create a vcpu resource by using the values of the sub_cpu one.
117   CpuCas01* sub_cpu = dynamic_cast<CpuCas01*>(host_PM->pimpl_cpu);
118
119   piface_->pimpl_cpu = surf_cpu_model_vm->createCpu(piface_, sub_cpu->getSpeedPeakList(), 1 /*cores*/);
120   if (sub_cpu->getPState() != 0)
121     piface_->pimpl_cpu->setPState(sub_cpu->getPState());
122
123   /* We create cpu_action corresponding to a VM process on the host operating system. */
124   /* FIXME: TODO: we have to periodically input GUESTOS_NOISE to the system? how ? */
125   action_ = sub_cpu->execution_start(0);
126
127   /* Initialize the VM parameters */
128   params_.ramsize = 0;
129
130   XBT_VERB("Create VM(%s)@PM(%s) with %ld mounted disks", piface->name().c_str(), hostPM_->name().c_str(),
131            xbt_dynar_length(storage_));
132 }
133
134 extern "C" int
135     xbt_log_no_loc; /* ugly pimpl to ensure that the debug info in the known issue below don't break the test */
136 /** @brief A physical host does not disappear in the current SimGrid code, but a VM may disappear during a simulation */
137 VirtualMachineImpl::~VirtualMachineImpl()
138 {
139   onVmDestruction(this);
140   allVms_.erase(find(allVms_.begin(), allVms_.end(), this));
141
142   /* dirty page tracking */
143   unsigned int size          = xbt_dict_size(dp_objs);
144   static bool already_warned = false;
145   if (size > 0 && !already_warned) {
146     xbt_dict_cursor_t cursor = nullptr;
147     xbt_dict_cursor_first(dp_objs, &cursor);
148     XBT_WARN("Dirty page tracking: %u pending task(s) on a destroyed VM (first one is %s).\n"
149              "If you don't understand why your task was not properly removed, please report that bug.\n"
150              "This is a known bug if you turned the host off during the VM execution.\n"
151              "Please remind us of that problem at some point: our code base is not ready to fix this harmless issue in "
152              "2016, sorry.",
153              size, (xbt_log_no_loc ? "(name hidden)" : xbt_dict_cursor_get_key(cursor)));
154     xbt_dict_cursor_free(&cursor);
155     already_warned = true;
156   }
157   xbt_dict_free(&dp_objs);
158
159   /* Free the cpu_action of the VM. */
160   XBT_ATTRIB_UNUSED int ret = action_->unref();
161   xbt_assert(ret == 1, "Bug: some resource still remains");
162
163   delete piface_->pimpl_cpu;
164 }
165
166 e_surf_vm_state_t VirtualMachineImpl::getState()
167 {
168   return vmState_;
169 }
170
171 void VirtualMachineImpl::setState(e_surf_vm_state_t state)
172 {
173   vmState_ = state;
174 }
175 void VirtualMachineImpl::suspend()
176 {
177   action_->suspend();
178   vmState_ = SURF_VM_STATE_SUSPENDED;
179 }
180
181 void VirtualMachineImpl::resume()
182 {
183   action_->resume();
184   vmState_ = SURF_VM_STATE_RUNNING;
185 }
186
187 void VirtualMachineImpl::save()
188 {
189   vmState_ = SURF_VM_STATE_SAVING;
190   action_->suspend();
191   vmState_ = SURF_VM_STATE_SAVED;
192 }
193
194 void VirtualMachineImpl::restore()
195 {
196   vmState_ = SURF_VM_STATE_RESTORING;
197   action_->resume();
198   vmState_ = SURF_VM_STATE_RUNNING;
199 }
200
201 /** @brief returns the physical machine on which the VM is running **/
202 s4u::Host* VirtualMachineImpl::getPm()
203 {
204   return hostPM_;
205 }
206
207 /* Update the physical host of the given VM */
208 void VirtualMachineImpl::migrate(s4u::Host* host_dest)
209 {
210   const char* vm_name     = piface_->name().c_str();
211   const char* pm_name_src = hostPM_->name().c_str();
212   const char* pm_name_dst = host_dest->name().c_str();
213
214   /* update net_elm with that of the destination physical host */
215   piface_->pimpl_netcard = host_dest->pimpl_netcard;
216
217   hostPM_ = host_dest;
218
219   /* Update vcpu's action for the new pm */
220   /* create a cpu action bound to the pm model at the destination. */
221   CpuAction* new_cpu_action = static_cast<CpuAction*>(host_dest->pimpl_cpu->execution_start(0));
222
223   Action::State state = action_->getState();
224   if (state != Action::State::done)
225     XBT_CRITICAL("FIXME: may need a proper handling, %d", static_cast<int>(state));
226   if (action_->getRemainsNoUpdate() > 0)
227     XBT_CRITICAL("FIXME: need copy the state(?), %f", action_->getRemainsNoUpdate());
228
229   /* keep the bound value of the cpu action of the VM. */
230   double old_bound = action_->getBound();
231   if (old_bound != 0) {
232     XBT_DEBUG("migrate VM(%s): set bound (%f) at %s", vm_name, old_bound, pm_name_dst);
233     new_cpu_action->setBound(old_bound);
234   }
235
236   XBT_ATTRIB_UNUSED int ret = action_->unref();
237   xbt_assert(ret == 1, "Bug: some resource still remains");
238
239   action_ = new_cpu_action;
240
241   XBT_DEBUG("migrate VM(%s): change PM (%s to %s)", vm_name, pm_name_src, pm_name_dst);
242 }
243
244 sg_size_t VirtualMachineImpl::getRamsize()
245 {
246   return params_.ramsize;
247 }
248
249 void VirtualMachineImpl::setBound(double bound)
250 {
251   action_->setBound(bound);
252 }
253
254 void VirtualMachineImpl::getParams(vm_params_t params)
255 {
256   *params = params_;
257 }
258
259 void VirtualMachineImpl::setParams(vm_params_t params)
260 {
261   /* may check something here. */
262   params_ = *params;
263 }
264 }
265 }