Logo AND Algorithmique Numérique Distribuée

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