Logo AND Algorithmique Numérique Distribuée

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