Logo AND Algorithmique Numérique Distribuée

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