Logo AND Algorithmique Numérique Distribuée

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