Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Clean surf interface
[simgrid.git] / src / surf / vm_workstation_hl13.cpp
1 /*
2  * vm_workstation.cpp
3  *
4  *  Created on: Nov 12, 2013
5  *      Author: bedaride
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 xbt_dynar_t WorkstationVMHL13Model::getRoute(WorkstationPtr src, WorkstationPtr dst){
31   return WorkstationCLM03Model::getRoute(src, dst);
32 }
33
34 ActionPtr WorkstationVMHL13Model::communicate(WorkstationPtr src, WorkstationPtr dst, double size, double rate){
35   return WorkstationCLM03Model::communicate(src, dst, 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 void WorkstationVMHL13Model::createResource(const char *name, void *ind_phys_workstation)
42 {
43   WorkstationVMHL13LmmPtr ws = new WorkstationVMHL13Lmm(this, name, NULL, static_cast<surf_resource_t>(ind_phys_workstation));
44
45   xbt_lib_set(host_lib, name, SURF_WKS_LEVEL, static_cast<ResourcePtr>(ws));
46
47   /* TODO:
48    * - check how network requests are scheduled between distinct processes competing for the same card.
49    */
50 }
51
52 static inline double get_solved_value(CpuActionLmmPtr cpu_action)
53 {
54   return cpu_action->getVariable()->value;
55 }
56
57 /* In the real world, processes on the guest operating system will be somewhat
58  * degraded due to virtualization overhead. The total CPU share that these
59  * processes get is smaller than that of the VM process gets on a host
60  * operating system. */
61 // const double virt_overhead = 0.95;
62 const double virt_overhead = 1;
63
64 double WorkstationVMHL13Model::shareResources(double now)
65 {
66   /* TODO: udpate action's cost with the total cost of processes on the VM. */
67
68
69   /* 0. Make sure that we already calculated the resource share at the physical
70    * machine layer. */
71   {
72         ModelPtr ws_model = static_cast<ModelPtr>(surf_workstation_model);
73         ModelPtr vm_ws_model = static_cast<ModelPtr>(surf_vm_workstation_model);
74     unsigned int index_of_pm_ws_model = xbt_dynar_search(model_list_invoke, &ws_model);
75     unsigned int index_of_vm_ws_model = xbt_dynar_search(model_list_invoke, &vm_ws_model);
76     xbt_assert((index_of_pm_ws_model < index_of_vm_ws_model), "Cannot assume surf_workstation_model comes before");
77
78     /* Another option is that we call sub_ws->share_resource() here. The
79      * share_resource() function has no side-effect. We can call it here to
80      * ensure that. */
81   }
82
83
84   /* 1. Now we know how many resource should be assigned to each virtual
85    * machine. We update constraints of the virtual machine layer.
86    *
87    *
88    * If we have two virtual machine (VM1 and VM2) on a physical machine (PM1).
89    *     X1 + X2 = C       (Equation 1)
90    * where
91    *    the resource share of VM1: X1
92    *    the resource share of VM2: X2
93    *    the capacity of PM1: C
94    *
95    * Then, if we have two process (P1 and P2) on VM1.
96    *     X1_1 + X1_2 = X1  (Equation 2)
97    * where
98    *    the resource share of P1: X1_1
99    *    the resource share of P2: X1_2
100    *    the capacity of VM1: X1
101    *
102    * Equation 1 was solved in the physical machine layer.
103    * Equation 2 is solved in the virtual machine layer (here).
104    * X1 must be passed to the virtual machine laye as a constraint value.
105    *
106    **/
107
108   /* iterate for all hosts including virtual machines */
109   xbt_lib_cursor_t cursor;
110   char *key;
111   void **ind_host;
112   xbt_lib_foreach(host_lib, cursor, key, ind_host) {
113     WorkstationPtr ws = dynamic_cast<WorkstationPtr>(
114                                    static_cast<ResourcePtr>(ind_host[SURF_WKS_LEVEL]));
115     CpuLmmPtr cpu = dynamic_cast<CpuLmmPtr>(
116                                static_cast<ResourcePtr>(ind_host[SURF_CPU_LEVEL]));
117
118     if (!ws)
119       continue;
120     /* skip if it is not a virtual machine */
121     if (ws->getModel() != static_cast<ModelPtr>(surf_vm_workstation_model))
122       continue;
123     xbt_assert(cpu, "cpu-less workstation");
124
125     /* It is a virtual machine, so we can cast it to workstation_VM2013_t */
126     WorkstationVMPtr ws_vm = dynamic_cast<WorkstationVMPtr>(ws);
127
128     double solved_value = get_solved_value(reinterpret_cast<CpuActionLmmPtr>(ws_vm->p_action));
129     XBT_DEBUG("assign %f to vm %s @ pm %s", solved_value,
130         ws->getName(), ws_vm->p_subWs->getName());
131
132     // TODO: check lmm_update_constraint_bound() works fine instead of the below manual substitution.
133     // cpu_cas01->constraint->bound = solved_value;
134     xbt_assert(cpu->getModel() == static_cast<ModelPtr>(surf_cpu_model_vm));
135     lmm_system_t vcpu_system = cpu->getModel()->getMaxminSystem();
136     lmm_update_constraint_bound(vcpu_system, cpu->constraint(), virt_overhead * solved_value);
137   }
138
139
140   /* 2. Calculate resource share at the virtual machine layer. */
141   double ret = WorkstationCLM03Model::shareResources(now);
142
143
144   /* FIXME: 3. do we have to re-initialize our cpu_action object? */
145 #if 0
146   /* iterate for all hosts including virtual machines */
147   xbt_lib_foreach(host_lib, cursor, key, ind_host) {
148     WorkstationCLM03Ptr ws_clm03 = ind_host[SURF_WKS_LEVEL];
149
150     /* skip if it is not a virtual machine */
151     if (!ws_clm03)
152       continue;
153     if (ws_clm03->getModel() != surf_vm_workstation_model)
154       continue;
155
156     /* It is a virtual machine, so we can cast it to workstation_VM2013_t */
157     {
158 #if 0
159       WorkstationVM2013Ptr ws_vm2013 = (workstation_VM2013_t) ws_clm03;
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 /************
183  * Resource *
184  ************/
185 WorkstationVMHL13Lmm::WorkstationVMHL13Lmm(WorkstationVMModelPtr model, const char* name, xbt_dict_t props,
186                                                    surf_resource_t ind_phys_workstation)
187  : Resource(model, name, props)
188  , WorkstationVMLmm(NULL, NULL)
189  , WorkstationCLM03Lmm(model, name, props, NULL, NULL, NULL)
190 {
191   WorkstationPtr sub_ws = dynamic_cast<WorkstationPtr>(
192                                static_cast<ResourcePtr>(
193                                  surf_workstation_resource_priv(ind_phys_workstation)));
194
195   /* Currently, we assume a VM has no storage. */
196   p_storage = NULL;
197
198   /* Currently, a VM uses the network resource of its physical host. In
199    * host_lib, this network resource object is refered from two different keys.
200    * When deregistering the reference that points the network resource object
201    * from the VM name, we have to make sure that the system does not call the
202    * free callback for the network resource object. The network resource object
203    * is still used by the physical machine. */
204   p_netElm = static_cast<RoutingEdgePtr>(xbt_lib_get_or_null(host_lib, sub_ws->getName(), ROUTING_HOST_LEVEL));
205   xbt_lib_set(host_lib, name, ROUTING_HOST_LEVEL, p_netElm);
206
207   p_subWs = sub_ws;
208   p_currentState = SURF_VM_STATE_CREATED;
209
210   // //// CPU  RELATED STUFF ////
211   // Roughly, create a vcpu resource by using the values of the sub_cpu one.
212   CpuCas01LmmPtr sub_cpu = dynamic_cast<CpuCas01LmmPtr>(
213                    static_cast<ResourcePtr>(
214                          surf_cpu_resource_priv(ind_phys_workstation)));
215
216   /* We can assume one core and cas01 cpu for the first step.
217    * Do xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, cpu) if you get the resource. */
218
219   p_cpu = static_cast<CpuCas01ModelPtr>(surf_cpu_model_vm)->createResource(name, // name
220       sub_cpu->p_powerPeakList,        // host->power_peak,
221       sub_cpu->m_pstate,
222       1,                          // host->power_scale,
223       NULL,                       // host->power_trace,
224       1,                          // host->core_amount,
225       SURF_RESOURCE_ON,           // host->initial_state,
226       NULL,                       // host->state_trace,
227       NULL);                       // host->properties,
228
229   /* We create cpu_action corresponding to a VM process on the host operating system. */
230   /* FIXME: TODO: we have to peridocally input GUESTOS_NOISE to the system? how ? */
231   // vm_ws->cpu_action = surf_cpu_model_pm->extension.cpu.execute(ind_phys_workstation, GUESTOS_NOISE);
232   p_action = dynamic_cast<CpuActionLmmPtr>(sub_cpu->execute(0));
233
234   /* The SURF_WKS_LEVEL at host_lib saves workstation_CLM03 objects. Please
235    * note workstation_VM2013 objects, inheriting the workstation_CLM03
236    * structure, are also saved there.
237    *
238    * If you want to get a workstation_VM2013 object from host_lib, see
239    * ws->generic_resouce.model->type first. If it is
240    * SURF_MODEL_TYPE_VM_WORKSTATION, you can cast ws to vm_ws. */
241   XBT_INFO("Create VM(%s)@PM(%s) with %ld mounted disks", name, sub_ws->getName(), xbt_dynar_length(p_storage));
242 }
243
244 /*
245  * A physical host does not disapper in the current SimGrid code, but a VM may
246  * disapper during a simulation.
247  */
248 WorkstationVMHL13Lmm::~WorkstationVMHL13Lmm()
249 {
250   /* ind_phys_workstation equals to smx_host_t */
251   surf_resource_t ind_vm_workstation = xbt_lib_get_elm_or_null(host_lib, getName());
252
253   /* Before clearing the entries in host_lib, we have to pick up resources. */
254   CpuCas01LmmPtr cpu = dynamic_cast<CpuCas01LmmPtr>(
255                     static_cast<ResourcePtr>(
256                       surf_cpu_resource_priv(ind_vm_workstation)));
257
258   /* We deregister objects from host_lib, without invoking the freeing callback
259    * of each level.
260    *
261    * Do not call xbt_lib_remove() here. It deletes all levels of the key,
262    * including MSG_HOST_LEVEL and others. We should unregister only what we know.
263    */
264   xbt_lib_unset(host_lib, getName(), SURF_CPU_LEVEL, 0);
265   xbt_lib_unset(host_lib, getName(), ROUTING_HOST_LEVEL, 0);
266   xbt_lib_unset(host_lib, getName(), SURF_WKS_LEVEL, 0);
267
268   /* TODO: comment out when VM stroage is implemented. */
269   // xbt_lib_unset(host_lib, name, SURF_STORAGE_LEVEL, 0);
270
271
272   /* Free the cpu_action of the VM. */
273   int ret = p_action->unref();
274   xbt_assert(ret == 1, "Bug: some resource still remains");
275
276   /* Free the cpu resource of the VM. If using power_trace, we will have to */
277   delete cpu;
278
279   /* Free the network resource of the VM. */
280         // Nothing has to be done, because net_elmts is just a pointer on the physical one
281
282   /* Free the storage resource of the VM. */
283   // Not relevant yet
284
285         /* Free the workstation resource of the VM. */
286 }
287
288 e_surf_resource_state_t WorkstationVMHL13Lmm::getState()
289 {
290   return (e_surf_resource_state_t) p_currentState;
291 }
292
293 void WorkstationVMHL13Lmm::setState(e_surf_resource_state_t state)
294 {
295   p_currentState = (e_surf_vm_state_t) state;
296 }
297
298 void WorkstationVMHL13Lmm::suspend()
299 {
300   p_action->suspend();
301   p_currentState = SURF_VM_STATE_SUSPENDED;
302 }
303
304 void WorkstationVMHL13Lmm::resume()
305 {
306   p_action->resume();
307   p_currentState = SURF_VM_STATE_RUNNING;
308 }
309
310 void WorkstationVMHL13Lmm::save()
311 {
312   p_currentState = SURF_VM_STATE_SAVING;
313
314   /* FIXME: do something here */
315   p_action->suspend();
316   p_currentState = SURF_VM_STATE_SAVED;
317 }
318
319 void WorkstationVMHL13Lmm::restore()
320 {
321   p_currentState = SURF_VM_STATE_RESTORING;
322
323   /* FIXME: do something here */
324   p_action->resume();
325   p_currentState = SURF_VM_STATE_RUNNING;
326 }
327
328 /*
329  * Update the physical host of the given VM
330  */
331 void WorkstationVMHL13Lmm::migrate(surf_resource_t ind_dst_pm)
332 {
333    /* ind_phys_workstation equals to smx_host_t */
334    WorkstationPtr ws_dst = dynamic_cast<WorkstationPtr>(
335                                           static_cast<ResourcePtr>(
336                                            surf_workstation_resource_priv(ind_dst_pm)));
337    const char *vm_name = getName();
338    const char *pm_name_src = p_subWs->getName();
339    const char *pm_name_dst = ws_dst->getName();
340
341    xbt_assert(ws_dst);
342
343    /* do something */
344
345    /* update net_elm with that of the destination physical host */
346    RoutingEdgePtr old_net_elm = p_netElm;
347    RoutingEdgePtr new_net_elm = static_cast<RoutingEdgePtr>(xbt_lib_get_or_null(host_lib, pm_name_dst, ROUTING_HOST_LEVEL));
348    xbt_assert(new_net_elm);
349
350    /* Unregister the current net_elm from host_lib. Do not call the free callback. */
351    xbt_lib_unset(host_lib, vm_name, ROUTING_HOST_LEVEL, 0);
352
353    /* Then, resister the new one. */
354    p_netElm = new_net_elm;
355    xbt_lib_set(host_lib, vm_name, ROUTING_HOST_LEVEL, p_netElm);
356
357    p_subWs = ws_dst;
358
359    /* Update vcpu's action for the new pm */
360    {
361 #if 0
362      XBT_INFO("cpu_action->remains %g", p_action->remains);
363      XBT_INFO("cost %f remains %f start %f finish %f", p_action->cost,
364          p_action->remains,
365          p_action->start,
366          p_action->finish
367          );
368      XBT_INFO("cpu_action state %d", surf_action_get_state(p_action));
369 #endif
370
371      /* create a cpu action bound to the pm model at the destination. */
372      CpuActionLmmPtr new_cpu_action = dynamic_cast<CpuActionLmmPtr>(
373                                             dynamic_cast<CpuCas01LmmPtr>(
374                                         static_cast<ResourcePtr>(
375                                         surf_cpu_resource_priv(ind_dst_pm)))->execute(0));
376
377      e_surf_action_state_t state = p_action->getState();
378      if (state != SURF_ACTION_DONE)
379        XBT_CRITICAL("FIXME: may need a proper handling, %d", state);
380      if (p_action->getRemains() > 0)
381        XBT_CRITICAL("FIXME: need copy the state(?), %f", p_action->getRemains());
382
383      int ret = p_action->unref();
384      xbt_assert(ret == 1, "Bug: some resource still remains");
385
386      p_action = new_cpu_action;
387    }
388
389    XBT_DEBUG("migrate VM(%s): change net_elm (%p to %p)", vm_name, old_net_elm, new_net_elm);
390    XBT_DEBUG("migrate VM(%s): change PM (%s to %s)", vm_name, pm_name_src, pm_name_dst);
391 }
392
393 void WorkstationVMHL13Lmm::setBound(double bound){
394  p_action->setBound(bound);
395 }
396
397 void WorkstationVMHL13Lmm::setAffinity(CpuLmmPtr 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. Returing the
403  * dict_elm of the host.
404  **/
405 surf_resource_t WorkstationVMHL13Lmm::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 WorkstationVMHL13Lmm::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 WorkstationCLM03Lmm::execute(size);
423 }
424
425 /**********
426  * Action *
427  **********/
428
429 //FIME:: handle action cancel
430