Logo AND Algorithmique Numérique Distribuée

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