Logo AND Algorithmique Numérique Distribuée

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