Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add VM energy extensions - Adrien
[simgrid.git] / src / surf / vm_hl13.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 "cpu_cas01.hpp"
8 #include "vm_hl13.hpp"
9
10 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_vm);
11
12 void surf_vm_model_init_HL13(void){
13   if (surf_cpu_model_vm) {
14     surf_vm_model = new VMHL13Model();
15     Model *model = surf_vm_model;
16
17     xbt_dynar_push(model_list, &model);
18     xbt_dynar_push(model_list_invoke, &model);
19   }
20 }
21
22 /*********
23  * Model *
24  *********/
25
26 VMHL13Model::VMHL13Model() : VMModel() {}
27
28 void VMHL13Model::updateActionsState(double /*now*/, double /*delta*/) {}
29
30 /* ind means ''indirect'' that this is a reference on the whole dict_elm
31  * structure (i.e not on the surf_resource_private infos) */
32
33 VM *VMHL13Model::createVM(const char *name, surf_resource_t host_PM)
34 {
35   VMHL13 *ws = new VMHL13(this, name, NULL, host_PM);
36
37   xbt_lib_set(host_lib, name, SURF_HOST_LEVEL, ws);
38
39   /* TODO:
40    * - check how network requests are scheduled between distinct processes competing for the same card.
41    */
42   return ws;
43 }
44
45 static inline double get_solved_value(CpuAction *cpu_action)
46 {
47   return cpu_action->getVariable()->value;
48 }
49
50 /* In the real world, processes on the guest operating system will be somewhat
51  * degraded due to virtualization overhead. The total CPU share that these
52  * processes get is smaller than that of the VM process gets on a host
53  * operating system. */
54 // const double virt_overhead = 0.95;
55 const double virt_overhead = 1;
56
57 double VMHL13Model::shareResources(double now)
58 {
59   /* TODO: udpate action's cost with the total cost of processes on the VM. */
60
61
62   /* 0. Make sure that we already calculated the resource share at the physical
63    * machine layer. */
64   {
65     XBT_ATTRIB_UNUSED Model *ws_model = surf_host_model;
66     XBT_ATTRIB_UNUSED Model *vm_ws_model = surf_vm_model;
67     XBT_ATTRIB_UNUSED unsigned int index_of_pm_ws_model = xbt_dynar_search(model_list_invoke, &ws_model);
68     XBT_ATTRIB_UNUSED unsigned int index_of_vm_ws_model = xbt_dynar_search(model_list_invoke, &vm_ws_model);
69     xbt_assert((index_of_pm_ws_model < index_of_vm_ws_model), "Cannot assume surf_host_model comes before");
70
71     /* Another option is that we call sub_ws->share_resource() here. The
72      * share_resource() function has no side-effect. We can call it here to
73      * ensure that. */
74   }
75
76
77   /* 1. Now we know how many resource should be assigned to each virtual
78    * machine. We update constraints of the virtual machine layer.
79    *
80    *
81    * If we have two virtual machine (VM1 and VM2) on a physical machine (PM1).
82    *     X1 + X2 = C       (Equation 1)
83    * where
84    *    the resource share of VM1: X1
85    *    the resource share of VM2: X2
86    *    the capacity of PM1: C
87    *
88    * Then, if we have two process (P1 and P2) on VM1.
89    *     X1_1 + X1_2 = X1  (Equation 2)
90    * where
91    *    the resource share of P1: X1_1
92    *    the resource share of P2: X1_2
93    *    the capacity of VM1: X1
94    *
95    * Equation 1 was solved in the physical machine layer.
96    * Equation 2 is solved in the virtual machine layer (here).
97    * X1 must be passed to the virtual machine laye as a constraint value.
98    *
99    **/
100
101   /* iterate for all virtual machines */
102   for (VMModel::vm_list_t::iterator iter =
103          VMModel::ws_vms.begin();
104        iter !=  VMModel::ws_vms.end(); ++iter) {
105
106     VM *ws_vm = &*iter;
107     Cpu *cpu = ws_vm->p_cpu;
108     xbt_assert(cpu, "cpu-less host");
109
110     double solved_value = get_solved_value(ws_vm->p_action);
111     XBT_DEBUG("assign %f to vm %s @ pm %s", solved_value,
112         ws_vm->getName(), ws_vm->p_subWs->getName());
113
114     // TODO: check lmm_update_constraint_bound() works fine instead of the below manual substitution.
115     // cpu_cas01->constraint->bound = solved_value;
116     xbt_assert(cpu->getModel() == surf_cpu_model_vm);
117     lmm_system_t vcpu_system = cpu->getModel()->getMaxminSystem();
118     lmm_update_constraint_bound(vcpu_system, cpu->getConstraint(), virt_overhead * solved_value);
119   }
120
121
122   /* 2. Calculate resource share at the virtual machine layer. */
123   adjustWeightOfDummyCpuActions();
124
125   double min_by_cpu = surf_cpu_model_vm->shareResources(now);
126   double min_by_net = surf_network_model->shareResourcesIsIdempotent() ? surf_network_model->shareResources(now) : -1;
127   // Fixme: take storage into account once it's implemented
128   double min_by_sto = -1;
129
130   XBT_DEBUG("model %p, %s min_by_cpu %f, %s min_by_net %f, %s min_by_sto %f",
131       this, typeid(surf_cpu_model_pm ).name(), min_by_cpu,
132                 typeid(surf_network_model).name(), min_by_net,
133             typeid(surf_storage_model).name(), min_by_sto);
134
135   double ret = max(max(min_by_cpu, min_by_net), min_by_sto);
136   if (min_by_cpu >= 0.0 && min_by_cpu < ret)
137         ret = min_by_cpu;
138   if (min_by_net >= 0.0 && min_by_net < ret)
139         ret = min_by_net;
140   if (min_by_sto >= 0.0 && min_by_sto < ret)
141         ret = min_by_sto;
142
143   /* FIXME: 3. do we have to re-initialize our cpu_action object? */
144 #if 0
145   /* iterate for all virtual machines */
146   for (VMModel::vm_list_t::iterator iter =
147          VMModel::ws_vms.begin();
148        iter !=  VMModel::ws_vms.end(); ++iter) {
149
150     {
151 #if 0
152       VM2013 *ws_vm2013 = static_cast<VM2013Ptr>(&*iter);
153       XBT_INFO("cost %f remains %f start %f finish %f", ws_vm2013->cpu_action->cost,
154           ws_vm2013->cpu_action->remains,
155           ws_vm2013->cpu_action->start,
156           ws_vm2013->cpu_action->finish
157           );
158 #endif
159 #if 0
160       void *ind_sub_host = xbt_lib_get_elm_or_null(host_lib, ws_vm2013->sub_ws->generic_resource.getName);
161       surf_cpu_model_pm->action_unref(ws_vm2013->cpu_action);
162       /* FIXME: this means busy loop? */
163       // ws_vm2013->cpu_action = surf_cpu_model_pm->extension.cpu.execute(ind_sub_host, GUESTOS_NOISE);
164       ws_vm2013->cpu_action = surf_cpu_model_pm->extension.cpu.execute(ind_sub_host, 0);
165 #endif
166
167     }
168   }
169 #endif
170
171
172   return ret;
173 }
174
175 Action *VMHL13Model::executeParallelTask(int host_nb,
176                                          sg_host_t *host_list,
177                                                                                  double *flops_amount,
178                                                                                  double *bytes_amount,
179                                                                                  double rate){
180 #define cost_or_zero(array,pos) ((array)?(array)[pos]:0.0)
181   if ((host_nb == 1)
182       && (cost_or_zero(bytes_amount, 0) == 0.0))
183     return surf_host_execute(host_list[0], flops_amount[0]);
184   else if ((host_nb == 1)
185            && (cost_or_zero(flops_amount, 0) == 0.0))
186     return surf_network_model_communicate(surf_network_model, host_list[0], host_list[0],bytes_amount[0], rate);
187   else if ((host_nb == 2)
188              && (cost_or_zero(flops_amount, 0) == 0.0)
189              && (cost_or_zero(flops_amount, 1) == 0.0)) {
190     int i,nb = 0;
191     double value = 0.0;
192
193     for (i = 0; i < host_nb * host_nb; i++) {
194       if (cost_or_zero(bytes_amount, i) > 0.0) {
195         nb++;
196         value = cost_or_zero(bytes_amount, i);
197       }
198     }
199     if (nb == 1)
200       return surf_network_model_communicate(surf_network_model, host_list[0], host_list[1], value, rate);
201   }
202 #undef cost_or_zero
203
204   THROW_UNIMPLEMENTED;          /* This model does not implement parallel tasks for more than 2 hosts. */
205   return NULL;
206 }
207
208 /************
209  * Resource *
210  ************/
211
212 VMHL13::VMHL13(VMModel *model, const char* name, xbt_dict_t props,
213                                                    surf_resource_t host_PM)
214  : VM(model, name, props, NULL, NULL)
215 {
216   Host *sub_ws = static_cast<Host*>(surf_host_resource_priv(host_PM));
217
218   /* Currently, we assume a VM has no storage. */
219   p_storage = NULL;
220
221   /* Currently, a VM uses the network resource of its physical host. In
222    * host_lib, this network resource object is referred from two different keys.
223    * When deregistering the reference that points the network resource object
224    * from the VM name, we have to make sure that the system does not call the
225    * free callback for the network resource object. The network resource object
226    * is still used by the physical machine. */
227   sg_host_t sg_sub_ws = sg_host_by_name_or_create(sub_ws->getName());
228   p_netElm = new RoutingEdgeWrapper(sg_host_edge(sg_sub_ws));
229   sg_host_edge_set(sg_host_by_name_or_create(name), p_netElm);
230
231   p_subWs = sub_ws;
232   p_currentState = SURF_VM_STATE_CREATED;
233
234   // //// CPU  RELATED STUFF ////
235   // Roughly, create a vcpu resource by using the values of the sub_cpu one.
236   CpuCas01 *sub_cpu = static_cast<CpuCas01*>(sg_host_surfcpu(host_PM));
237
238   p_cpu = surf_cpu_model_vm->createCpu(name, // name
239       sub_cpu->getPowerPeakList(),        // host->power_peak,
240       sub_cpu->getPState(),
241       1,                          // host->power_scale,
242       NULL,                       // host->power_trace,
243       1,                          // host->core_amount,
244       SURF_RESOURCE_ON,           // host->initial_state,
245       NULL,                       // host->state_trace,
246       NULL);                       // host->properties,
247
248         p_cpu->setVirtual(sub_cpu);
249
250   /* We create cpu_action corresponding to a VM process on the host operating system. */
251   /* FIXME: TODO: we have to periodically input GUESTOS_NOISE to the system? how ? */
252   // vm_ws->cpu_action = surf_cpu_model_pm->extension.cpu.execute(host_PM, GUESTOS_NOISE);
253   p_action = sub_cpu->execute(0);
254
255   XBT_DEBUG("Create VM(%s)@PM(%s) with %ld mounted disks", name, sub_ws->getName(), xbt_dynar_length(p_storage));
256 }
257
258 /*
259  * A physical host does not disappear in the current SimGrid code, but a VM may
260  * disappear during a simulation.
261  */
262 VMHL13::~VMHL13()
263 {
264   /* Free the cpu_action of the VM. */
265   XBT_ATTRIB_UNUSED int ret = p_action->unref();
266   xbt_assert(ret == 1, "Bug: some resource still remains");
267 }
268
269 void VMHL13::updateState(tmgr_trace_event_t /*event_type*/, double /*value*/, double /*date*/) {
270   THROW_IMPOSSIBLE;             /* This model does not implement parallel tasks */
271 }
272
273 bool VMHL13::isUsed() {
274   THROW_IMPOSSIBLE;             /* This model does not implement parallel tasks */
275   return -1;
276 }
277
278 e_surf_resource_state_t VMHL13::getState()
279 {
280   return (e_surf_resource_state_t) p_currentState;
281 }
282
283 void VMHL13::setState(e_surf_resource_state_t state)
284 {
285   p_currentState = (e_surf_vm_state_t) state;
286 }
287
288 void VMHL13::suspend()
289 {
290   p_action->suspend();
291   p_currentState = SURF_VM_STATE_SUSPENDED;
292 }
293
294 void VMHL13::resume()
295 {
296   p_action->resume();
297   p_currentState = SURF_VM_STATE_RUNNING;
298 }
299
300 void VMHL13::save()
301 {
302   p_currentState = SURF_VM_STATE_SAVING;
303
304   /* FIXME: do something here */
305   p_action->suspend();
306   p_currentState = SURF_VM_STATE_SAVED;
307 }
308
309 void VMHL13::restore()
310 {
311   p_currentState = SURF_VM_STATE_RESTORING;
312
313   /* FIXME: do something here */
314   p_action->resume();
315   p_currentState = SURF_VM_STATE_RUNNING;
316 }
317
318 /*
319  * Update the physical host of the given VM
320  */
321 void VMHL13::migrate(surf_resource_t ind_dst_pm)
322 {
323    /* ind_dst_pm equals to smx_host_t */
324    Host *ws_dst = static_cast<Host*>(surf_host_resource_priv(ind_dst_pm));
325    const char *vm_name = getName();
326    const char *pm_name_src = p_subWs->getName();
327    const char *pm_name_dst = ws_dst->getName();
328
329    xbt_assert(ws_dst);
330
331    /* update net_elm with that of the destination physical host */
332    RoutingEdge *old_net_elm = p_netElm;
333    RoutingEdge *new_net_elm = new RoutingEdgeWrapper(sg_host_edge(sg_host_by_name(pm_name_dst)));
334    xbt_assert(new_net_elm);
335
336    /* Unregister the current net_elm from host_lib. Do not call the free callback. */
337    sg_host_edge_destroy(sg_host_by_name(vm_name), 0);
338
339    /* Then, resister the new one. */
340    p_netElm = new_net_elm;
341    sg_host_edge_set(sg_host_by_name(vm_name), p_netElm);
342
343    p_subWs = ws_dst;
344
345    /* Update vcpu's action for the new pm */
346    {
347 #if 0
348      XBT_INFO("cpu_action->remains %g", p_action->remains);
349      XBT_INFO("cost %f remains %f start %f finish %f", p_action->cost,
350          p_action->remains,
351          p_action->start,
352          p_action->finish
353          );
354      XBT_INFO("cpu_action state %d", surf_action_get_state(p_action));
355 #endif
356
357      /* create a cpu action bound to the pm model at the destination. */
358      CpuAction *new_cpu_action = static_cast<CpuAction*>(
359                                             static_cast<Cpu*>(sg_host_surfcpu(ind_dst_pm))->execute(0));
360
361      e_surf_action_state_t state = p_action->getState();
362      if (state != SURF_ACTION_DONE)
363        XBT_CRITICAL("FIXME: may need a proper handling, %d", state);
364      if (p_action->getRemainsNoUpdate() > 0)
365        XBT_CRITICAL("FIXME: need copy the state(?), %f", p_action->getRemainsNoUpdate());
366
367      /* keep the bound value of the cpu action of the VM. */
368      double old_bound = p_action->getBound();
369      if (old_bound != 0) {
370        XBT_DEBUG("migrate VM(%s): set bound (%f) at %s", vm_name, old_bound, pm_name_dst);
371        new_cpu_action->setBound(old_bound);
372      }
373
374      XBT_ATTRIB_UNUSED int ret = p_action->unref();
375      xbt_assert(ret == 1, "Bug: some resource still remains");
376
377      p_action = new_cpu_action;
378    }
379
380    XBT_DEBUG("migrate VM(%s): change net_elm (%p to %p)", vm_name, old_net_elm, new_net_elm);
381    XBT_DEBUG("migrate VM(%s): change PM (%s to %s)", vm_name, pm_name_src, pm_name_dst);
382    delete old_net_elm;
383 }
384
385 void VMHL13::setBound(double bound){
386  p_action->setBound(bound);
387 }
388
389 void VMHL13::setAffinity(Cpu *cpu, unsigned long mask){
390  p_action->setAffinity(cpu, mask);
391 }
392
393 /*
394  * A surf level object will be useless in the upper layer. Returning the
395  * dict_elm of the host.
396  **/
397 surf_resource_t VMHL13::getPm()
398 {
399   return xbt_lib_get_elm_or_null(host_lib, p_subWs->getName());
400 }
401
402 /* Adding a task to a VM updates the VCPU task on its physical machine. */
403 Action *VMHL13::execute(double size)
404 {
405   double old_cost = p_action->getCost();
406   double new_cost = old_cost + size;
407
408   XBT_DEBUG("VM(%s)@PM(%s): update dummy action's cost (%f -> %f)",
409       getName(), p_subWs->getName(),
410       old_cost, new_cost);
411
412   p_action->setCost(new_cost);
413
414   return p_cpu->execute(size);
415 }
416
417 Action *VMHL13::sleep(double duration) {
418   return p_cpu->sleep(duration);
419 }
420
421 /**********
422  * Action *
423  **********/
424
425 //FIME:: handle action cancel
426