Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Now VM operations workig!
[simgrid.git] / src / surf / vm_workstation.c
1 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. 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 "xbt/ex.h"
8 #include "xbt/dict.h"
9 #include "portable.h"
10 #include "surf_private.h"
11 #include "surf/surf_resource.h"
12 #include "simgrid/sg_config.h"
13 #include "vm_workstation_private.h"
14 #include "surf/cpu_cas01_private.h"
15 #include "surf/maxmin_private.h"
16
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_vm_workstation, surf,
18                                 "Logging specific to the SURF VM workstation module");
19
20
21 surf_model_t surf_vm_workstation_model = NULL;
22
23 /* ind means ''indirect'' that this is a reference on the whole dict_elm
24  * structure (i.e not on the surf_resource_private infos) */
25
26 static void vm_ws_create(const char *name, void *ind_phys_workstation)
27 {
28   workstation_CLM03_t sub_ws = surf_workstation_resource_priv(ind_phys_workstation);
29   const char *sub_ws_name = sub_ws->generic_resource.name;
30
31   /* The workstation_VM2013 struct inherits the workstation_CLM03 struct. We
32    * create a physical workstation resource, but specifying the size of
33    * s_workstation_VM2013_t and the vm workstation model object. */
34   workstation_CLM03_t ws = (workstation_CLM03_t) surf_resource_new(sizeof(s_workstation_VM2013_t),
35       surf_vm_workstation_model, name, NULL);
36
37   /* Currently, we assume a VM has no storage. */
38   ws->storage = NULL;
39
40   /* Currently, a VM uses the network resource of its physical host. In
41    * host_lib, this network resource object is refered from two different keys.
42    * When deregistering the reference that points the network resource object
43    * from the VM name, we have to make sure that the system does not call the
44    * free callback for the network resource object. The network resource object
45    * is still used by the physical machine. */
46   ws->net_elm = xbt_lib_get_or_null(host_lib, sub_ws_name, ROUTING_HOST_LEVEL);
47   xbt_lib_set(host_lib, name, ROUTING_HOST_LEVEL, ws->net_elm);
48
49   /* The SURF_WKS_LEVEL at host_lib saves workstation_CLM03 objects. Please
50    * note workstation_VM2013 objects, inheriting the workstation_CLM03
51    * structure, are also saved there. 
52    *
53    * If you want to get a workstation_VM2013 object from host_lib, see
54    * ws->generic_resouce.model->type first. If it is
55    * SURF_MODEL_TYPE_VM_WORKSTATION, you can cast ws to vm_ws. */
56   XBT_INFO("Create VM(%s)@PM(%s) with %ld mounted disks", name, sub_ws_name, xbt_dynar_length(ws->storage));
57   xbt_lib_set(host_lib, name, SURF_WKS_LEVEL, ws);
58
59
60   /* We initialize the VM-specific members. */
61   workstation_VM2013_t vm_ws = (workstation_VM2013_t) ws;
62   vm_ws->sub_ws = sub_ws;
63   vm_ws->current_state = SURF_VM_STATE_CREATED;
64
65
66
67   // //// CPU  RELATED STUFF ////
68   // Roughly, create a vcpu resource by using the values of the sub_cpu one.
69   cpu_Cas01_t sub_cpu = surf_cpu_resource_priv(ind_phys_workstation);
70
71   /* We can assume one core and cas01 cpu for the first step.
72    * Do xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, cpu) if you get the resource. */
73   cpu_cas01_create_resource(name, // name
74       sub_cpu->power_peak,        // host->power_peak,
75       1,                          // host->power_scale,
76       NULL,                       // host->power_trace,
77       1,                          // host->core_amount,
78       SURF_RESOURCE_ON,           // host->initial_state,
79       NULL,                       // host->state_trace,
80       NULL,                       // host->properties,
81       surf_cpu_model_vm);
82
83
84
85   /* We create cpu_action corresponding to a VM process on the host operating system. */
86   /* FIXME: TODO: we have to peridocally input GUESTOS_NOISE to the system? how ? */
87   // vm_ws->cpu_action = surf_cpu_model_pm->extension.cpu.execute(ind_phys_workstation, GUESTOS_NOISE);
88   vm_ws->cpu_action = surf_cpu_model_pm->extension.cpu.execute(ind_phys_workstation, 0);
89
90
91   /* TODO:
92    * - check how network requests are scheduled between distinct processes competing for the same card.
93    */
94 }
95
96 /*
97  * Update the physical host of the given VM
98  */
99 static void vm_ws_migrate(void *ind_vm, void *ind_dst_pm)
100
101    /* ind_phys_workstation equals to smx_host_t */
102    workstation_VM2013_t ws_vm2013 = surf_workstation_resource_priv(ind_vm);
103    workstation_CLM03_t ws_clm03_dst = surf_workstation_resource_priv(ind_dst_pm);
104    const char *vm_name = ws_vm2013->ws.generic_resource.name;
105    const char *pm_name_src = ws_vm2013->sub_ws->generic_resource.name;
106    const char *pm_name_dst = ws_clm03_dst->generic_resource.name;
107
108    xbt_assert(ws_vm2013);
109    xbt_assert(ws_clm03_dst);
110
111    ws_vm2013->current_state = SURF_VM_STATE_MIGRATING;
112
113    /* do something */
114
115    /* update net_elm with that of the destination physical host */
116    void *old_net_elm = ws_vm2013->ws.net_elm;
117    void *new_net_elm = xbt_lib_get_or_null(host_lib, pm_name_dst, ROUTING_HOST_LEVEL);
118    xbt_assert(new_net_elm);
119
120    /* Unregister the current net_elm from host_lib. Do not call the free callback. */
121    xbt_lib_unset(host_lib, vm_name, ROUTING_HOST_LEVEL, 0);
122
123    /* Then, resister the new one. */
124    ws_vm2013->ws.net_elm = new_net_elm;
125    xbt_lib_set(host_lib, vm_name, ROUTING_HOST_LEVEL, ws_vm2013->ws.net_elm);
126
127    ws_vm2013->sub_ws = ws_clm03_dst;
128
129    XBT_DEBUG("migrate VM(%s): change net_elm (%p to %p)", vm_name, old_net_elm, new_net_elm);
130    XBT_DEBUG("migrate VM(%s): change PM (%s to %s)", vm_name, pm_name_src, pm_name_dst);
131
132    ws_vm2013->current_state = SURF_VM_STATE_RUNNING;
133 }
134
135 /*
136  * A physical host does not disapper in the current SimGrid code, but a VM may
137  * disapper during a simulation.
138  */
139 static void vm_ws_destroy(void *ind_vm_workstation)
140
141         /* ind_phys_workstation equals to smx_host_t */
142
143   /* Before clearing the entries in host_lib, we have to pick up resources. */
144         workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_workstation);
145   cpu_Cas01_t cpu = surf_cpu_resource_priv(ind_vm_workstation);
146         const char *name = vm_ws->ws.generic_resource.name;
147   XBT_INFO("%s", name);
148
149         xbt_assert(vm_ws);
150         xbt_assert(vm_ws->ws.generic_resource.model == surf_vm_workstation_model);
151
152
153   /* We deregister objects from host_lib, without invoking the freeing callback
154    * of each level.
155    *
156    * Do not call xbt_lib_remove() here. It deletes all levels of the key,
157    * including MSG_HOST_LEVEL and others. We should unregister only what we know.
158    */
159   xbt_lib_unset(host_lib, name, SURF_CPU_LEVEL, 0);
160   xbt_lib_unset(host_lib, name, ROUTING_HOST_LEVEL, 0);
161   xbt_lib_unset(host_lib, name, SURF_STORAGE_LEVEL, 0);
162   xbt_lib_unset(host_lib, name, SURF_WKS_LEVEL, 0);
163
164
165   /* Free the cpu_action of the VM. */
166   int ret = surf_cpu_model_pm->action_unref(vm_ws->cpu_action);
167   xbt_assert(ret == 1, "Bug: some resource still remains");
168
169   /* Free the cpu resource of the VM. If using power_trace, we will have to
170    * free other objects than lmm_constraint. */
171   surf_model_t cpu_model = cpu->generic_resource.model;
172   lmm_constraint_free(cpu_model->model_private->maxmin_system, cpu->constraint);
173   surf_resource_free(cpu);
174
175   /* Free the network resource of the VM. */
176         // Nothing has to be done, because net_elmts is just a pointer on the physical one
177
178   /* Free the storage resource of the VM. */
179   // Not relevant yet
180
181         /* Free the workstation resource of the VM. */
182   surf_resource_free(vm_ws);
183 }
184
185 static int vm_ws_get_state(void *ind_vm_ws)
186 {
187         return ((workstation_VM2013_t) surf_workstation_resource_priv(ind_vm_ws))->current_state;
188 }
189
190 static void vm_ws_set_state(void *ind_vm_ws, int state)
191 {
192          ((workstation_VM2013_t) surf_workstation_resource_priv(ind_vm_ws))->current_state = state;
193 }
194
195 static void vm_ws_suspend(void *ind_vm_ws)
196 {
197   workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_ws);
198
199   XBT_INFO("vm %p suspend", ind_vm_ws);
200   surf_action_suspend(vm_ws->cpu_action);
201
202   vm_ws->current_state = SURF_VM_STATE_SUSPENDED;
203 }
204
205 static void vm_ws_resume(void *ind_vm_ws)
206 {
207   workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_ws);
208
209   surf_action_resume(vm_ws->cpu_action);
210
211   vm_ws->current_state = SURF_VM_STATE_RUNNING;
212 }
213
214 static void vm_ws_save(void *ind_vm_ws)
215 {
216   workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_ws);
217
218   vm_ws->current_state = SURF_VM_STATE_SAVING;
219
220   /* FIXME: do something here */
221   surf_action_suspend(vm_ws->cpu_action);
222
223   vm_ws->current_state = SURF_VM_STATE_SAVED;
224 }
225
226 static void vm_ws_restore(void *ind_vm_ws)
227 {
228   workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_ws);
229
230   vm_ws->current_state = SURF_VM_STATE_RESTORING;
231
232   /* FIXME: do something here */
233   surf_action_resume(vm_ws->cpu_action);
234
235   vm_ws->current_state = SURF_VM_STATE_RUNNING;
236 }
237
238
239 static double get_solved_value(surf_action_t cpu_action)
240 {
241   int found = 0;
242   /* NOTE: Do not use surf_workstation_model's maxmin_system. It is not used. */
243   lmm_system_t pm_system = surf_cpu_model_pm->model_private->maxmin_system;
244   lmm_variable_t var = NULL;
245
246   xbt_swag_foreach(var, &pm_system->variable_set) {
247     XBT_DEBUG("var id %p id_int %d double %f", var->id, var->id_int, var->value);
248     if (var->id == cpu_action) {
249       found = 1;
250       break;
251     }
252   }
253
254   if (found)
255     return var->value;
256
257   XBT_CRITICAL("bug: cannot found the solved variable of the action %p", cpu_action);
258   DIE_IMPOSSIBLE;
259   return -1; /* NOT REACHED */
260 }
261
262
263 static double vm_ws_share_resources(surf_model_t workstation_model, double now)
264 {
265   /* TODO: udpate action's cost with the total cost of processes on the VM. */
266
267
268
269   /* 0. Make sure that we already calculated the resource share at the physical
270    * machine layer. */
271   {
272     unsigned int index_of_pm_ws_model = xbt_dynar_search(model_list_invoke, &surf_workstation_model);
273     unsigned int index_of_vm_ws_model = xbt_dynar_search(model_list_invoke, &surf_vm_workstation_model);
274     xbt_assert((index_of_pm_ws_model < index_of_vm_ws_model), "Cannot assume surf_workstation_model comes before");
275  
276     /* Another option is that we call sub_ws->share_resource() here. The
277      * share_resource() function has no side-effect. We can call it here to
278      * ensure that. */
279   }
280
281
282   /* 1. Now we know how many resource should be assigned to each virtual
283    * machine. We update constraints of the virtual machine layer.
284    *
285    *
286    * If we have two virtual machine (VM1 and VM2) on a physical machine (PM1).
287    *     X1 + X2 = C       (Equation 1)
288    * where
289    *    the resource share of VM1: X1
290    *    the resource share of VM2: X2
291    *    the capacity of PM1: C
292    *
293    * Then, if we have two process (P1 and P2) on VM1.
294    *     X1_1 + X1_2 = X1  (Equation 2)
295    * where
296    *    the resource share of P1: X1_1
297    *    the resource share of P2: X1_2
298    *    the capacity of VM1: X1
299    *
300    * Equation 1 was solved in the physical machine layer.
301    * Equation 2 is solved in the virtual machine layer (here).
302    * X1 must be passed to the virtual machine laye as a constraint value.
303    *
304    **/
305
306   /* iterate for all hosts including virtual machines */
307   xbt_lib_cursor_t cursor;
308   char *key;
309   void **ind_host;
310   xbt_lib_foreach(host_lib, cursor, key, ind_host) {
311     workstation_CLM03_t ws_clm03 = ind_host[SURF_WKS_LEVEL];
312     cpu_Cas01_t cpu_cas01 = ind_host[SURF_CPU_LEVEL];
313
314     /* skip if it is not a virtual machine */
315     if (!ws_clm03)
316       continue;
317     if (ws_clm03->generic_resource.model != surf_vm_workstation_model)
318       continue;
319     xbt_assert(cpu_cas01, "cpu-less workstation");
320
321     /* It is a virtual machine, so we can cast it to workstation_VM2013_t */
322     workstation_VM2013_t ws_vm2013 = (workstation_VM2013_t) ws_clm03;
323
324     double solved_value = get_solved_value(ws_vm2013->cpu_action);
325     XBT_DEBUG("assign %f to vm %s @ pm %s", solved_value,
326         ws_clm03->generic_resource.name, ws_vm2013->sub_ws->generic_resource.name);
327
328     cpu_cas01->constraint->bound = solved_value;
329   }
330
331
332   /* 2. Calculate resource share at the virtual machine layer. */
333   double ret = ws_share_resources(workstation_model, now);
334
335
336   /* FIXME: 3. do we have to re-initialize our cpu_action object? */
337 #if 1
338   /* iterate for all hosts including virtual machines */
339   xbt_lib_foreach(host_lib, cursor, key, ind_host) {
340     workstation_CLM03_t ws_clm03 = ind_host[SURF_WKS_LEVEL];
341
342     /* skip if it is not a virtual machine */
343     if (!ws_clm03)
344       continue;
345     if (ws_clm03->generic_resource.model != surf_vm_workstation_model)
346       continue;
347
348     /* It is a virtual machine, so we can cast it to workstation_VM2013_t */
349     workstation_VM2013_t ws_vm2013 = (workstation_VM2013_t) ws_clm03;
350     {
351       void *ind_sub_host = xbt_lib_get_elm_or_null(host_lib, ws_vm2013->sub_ws->generic_resource.name);
352       XBT_INFO("cost %f remains %f start %f finish %f", ws_vm2013->cpu_action->cost,
353           ws_vm2013->cpu_action->remains,
354           ws_vm2013->cpu_action->start,
355           ws_vm2013->cpu_action->finish
356           );
357
358 #if 0
359       surf_cpu_model_pm->action_unref(ws_vm2013->cpu_action);
360       /* FIXME: this means busy loop? */
361       // ws_vm2013->cpu_action = surf_cpu_model_pm->extension.cpu.execute(ind_sub_host, GUESTOS_NOISE);
362       ws_vm2013->cpu_action = surf_cpu_model_pm->extension.cpu.execute(ind_sub_host, 0);
363 #endif
364
365     }
366   }
367 #endif
368
369
370   return ret;
371 }
372
373
374 /*
375  * A surf level object will be useless in the upper layer. Returing the
376  * dict_elm of the host.
377  **/
378 static void *vm_ws_get_pm(void *ind_vm_ws)
379 {
380         workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_ws);
381   const char *sub_ws_name = vm_ws->sub_ws->generic_resource.name;
382
383   return xbt_lib_get_elm_or_null(host_lib, sub_ws_name);
384 }
385
386
387
388 /* Adding a task to a VM updates the VCPU task on its physical machine. */
389 surf_action_t vm_ws_execute(void *workstation, double size)
390 {
391   surf_resource_t ws = ((surf_resource_t) surf_workstation_resource_priv(workstation));
392
393   xbt_assert(ws->model->type == SURF_MODEL_TYPE_VM_WORKSTATION);
394   workstation_VM2013_t vm_ws = (workstation_VM2013_t) ws;
395
396   double old_cost = vm_ws->cpu_action->cost;
397   double new_cost = old_cost + size;
398
399   XBT_INFO("VM(%s)@PM(%s): update dummy action's cost (%f -> %f)",
400       ws->name, vm_ws->sub_ws->generic_resource.name,
401       old_cost, new_cost);
402
403   vm_ws->cpu_action->cost = new_cost;
404
405   return ws_execute(workstation, size);
406 }
407
408 static void vm_ws_action_cancel(surf_action_t action)
409 {
410   XBT_CRITICAL("FIXME: Not yet implemented. Reduce dummy action's cost by %f", action->cost);
411
412   ws_action_cancel(action);
413 }
414
415
416 static void surf_vm_workstation_model_init_internal(void)
417 {
418   surf_model_t model = surf_model_init();
419
420   model->name = "Virtual Workstation";
421   model->type = SURF_MODEL_TYPE_VM_WORKSTATION;
422
423   // model->action_unref     = ws_action_unref;
424   model->action_cancel    = vm_ws_action_cancel;
425   // model->action_state_set = ws_action_state_set;
426
427
428   model->model_private->share_resources       = vm_ws_share_resources;
429   model->model_private->resource_used         = ws_resource_used;
430   model->model_private->update_actions_state  = ws_update_actions_state;
431   model->model_private->update_resource_state = ws_update_resource_state;
432   model->model_private->finalize              = ws_finalize;
433
434
435   /* operation for an action, not for VM it self */
436   model->suspend          = ws_action_suspend;
437   model->resume           = ws_action_resume;
438 //   model->is_suspended     = ws_action_is_suspended;
439 //   model->set_max_duration = ws_action_set_max_duration;
440   model->set_priority     = ws_action_set_priority;
441 // #ifdef HAVE_TRACING
442 //   model->set_category     = ws_action_set_category;
443 // #endif
444 //   model->get_remains      = ws_action_get_remains;
445 // #ifdef HAVE_LATENCY_BOUND_TRACKING
446 //   model->get_latency_limited = ws_get_latency_limited;
447 // #endif
448
449
450
451
452
453
454
455   xbt_assert(surf_cpu_model_vm);
456   model->extension.workstation.cpu_model = surf_cpu_model_vm;
457
458   model->extension.workstation.execute   = vm_ws_execute;
459   model->extension.workstation.sleep     = ws_action_sleep;
460   model->extension.workstation.get_state = ws_get_state;
461   // model->extension.workstation.get_speed = ws_get_speed;
462   // model->extension.workstation.get_available_speed = ws_get_available_speed;
463
464   // model->extension.workstation.communicate           = ws_communicate;
465   // model->extension.workstation.get_route             = ws_get_route;
466   // model->extension.workstation.execute_parallel_task = ws_execute_parallel_task;
467   // model->extension.workstation.get_link_bandwidth    = ws_get_link_bandwidth;
468   // model->extension.workstation.get_link_latency      = ws_get_link_latency;
469   // model->extension.workstation.link_shared           = ws_link_shared;
470   // model->extension.workstation.get_properties        = ws_get_properties;
471
472   // model->extension.workstation.open   = ws_action_open;
473   // model->extension.workstation.close  = ws_action_close;
474   // model->extension.workstation.read   = ws_action_read;
475   // model->extension.workstation.write  = ws_action_write;
476   // model->extension.workstation.stat   = ws_action_stat;
477   // model->extension.workstation.unlink = ws_action_unlink;
478   // model->extension.workstation.ls     = ws_action_ls;
479
480
481   model->extension.vm_workstation.create        = vm_ws_create;
482   model->extension.vm_workstation.set_state     = vm_ws_set_state;
483   model->extension.vm_workstation.get_state     = vm_ws_get_state;
484   model->extension.vm_workstation.migrate       = vm_ws_migrate;
485   model->extension.vm_workstation.get_pm        = vm_ws_get_pm;
486   model->extension.vm_workstation.destroy       = vm_ws_destroy;
487   model->extension.vm_workstation.suspend       = vm_ws_suspend;
488   model->extension.vm_workstation.resume        = vm_ws_resume;
489   model->extension.vm_workstation.save          = vm_ws_save;
490   model->extension.vm_workstation.restore       = vm_ws_restore;
491
492   surf_vm_workstation_model = model;
493 }
494
495 void surf_vm_workstation_model_init(void)
496 {
497   surf_vm_workstation_model_init_internal();
498   xbt_dynar_push(model_list, &surf_vm_workstation_model);
499   xbt_dynar_push(model_list_invoke, &surf_vm_workstation_model);
500 }