Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'hypervisor' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid into hypervisor
[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 "workstation_private.h"
14 #include "surf/cpu_cas01_private.h"
15 #include "surf/maxmin_private.h"
16
17
18 /* NOTE:
19  * The workstation_VM2013 struct includes the workstation_CLM03 struct in
20  * its first member. The workstation_VM2013_t struct inherites all
21  * characteristics of the workstation_CLM03 struct. So, we can treat a
22  * workstation_VM2013 object as a workstation_CLM03 if necessary.
23  **/
24 typedef struct workstation_VM2013 {
25   s_workstation_CLM03_t ws;    /* a VM is a ''v''host */
26
27   /* The workstation object of the lower layer */
28   workstation_CLM03_t sub_ws;  // Pointer to the ''host'' OS
29
30   e_surf_vm_state_t current_state;
31
32
33   surf_action_t cpu_action;
34
35 } s_workstation_VM2013_t, *workstation_VM2013_t;
36
37 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_vm_workstation, surf,
38                                 "Logging specific to the SURF VM workstation module");
39
40
41
42
43 surf_model_t surf_vm_workstation_model = NULL;
44
45 static void vm_ws_create(const char *name, void *ind_phys_workstation)
46 {
47   workstation_VM2013_t vm_ws = xbt_new0(s_workstation_VM2013_t, 1);
48
49   vm_ws->sub_ws = surf_workstation_resource_priv(ind_phys_workstation);
50   vm_ws->current_state = SURF_VM_STATE_CREATED;
51
52
53   // //// WORKSTATION  RELATED STUFF ////
54   /* Create a workstation_CLM03 resource and register it to the system
55      Please note that the new ws is added into the host_lib. Then,
56      if you want to get a workstation_VM2013 object from host_lib, see
57      ws->generic_resouce.model->type first. If it is  SURF_MODEL_TYPE_VM_WORKSTATION,
58      you can cast ws to vm_ws. */
59
60   __init_workstation_CLM03(&vm_ws->ws, name);
61
62   // Override the model with the current VM one.
63   vm_ws->ws.generic_resource.model = surf_vm_workstation_model;
64
65
66   // //// CPU  RELATED STUFF ////
67   // Roughly, create a vcpu resource by using the values of  the sub_cpu one.
68   cpu_Cas01_t sub_cpu = surf_cpu_resource_priv(ind_phys_workstation);
69
70   /* We can assume one core and cas01 cpu for the first step.
71    * Do xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, cpu) if you get the resource.
72    * */
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   vm_ws->cpu_action = surf_cpu_model_pm->extension.cpu.execute(ind_phys_workstation, 100); // cost 0 is okay?
84
85   //// NET RELATED STUFF ////
86   // Bind virtual net_elm to the host
87   // TODO rebind each time you migrate a VM
88   // TODO check how network requests are scheduled between distinct processes competing for the same card.
89   // Please note that the __init_workstation_CLM03 invocation assigned NULL to ws.net_elm since no network elements
90   // were previously created for this hostname. Indeed all network elements are created during the SimGrid initialization phase by considering
91   // the platform file.
92   vm_ws->ws.net_elm = xbt_lib_get_or_null(host_lib, vm_ws->sub_ws->generic_resource.name, ROUTING_HOST_LEVEL);
93   xbt_lib_set(host_lib, name, ROUTING_HOST_LEVEL, vm_ws->ws.net_elm);
94
95   // //// STORAGE RELATED STUFF ////
96  // ind means ''indirect'' that this is a reference on the whole dict_elm structure (i.e not on the surf_resource_private infos)
97
98
99 }
100
101 /*
102  * Update the physical host of the given VM
103  */
104 static void vm_ws_migrate(void *ind_vm_workstation, void *ind_dest_phys_workstation)
105
106    /* ind_phys_workstation equals to smx_host_t */
107    workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_workstation);
108    xbt_assert(vm_ws);
109
110    /* do something */
111
112    vm_ws->sub_ws = surf_workstation_resource_priv(ind_dest_phys_workstation);
113 }
114
115 /*
116  * A physical host does not disapper in the current SimGrid code, but a VM may
117  * disapper during a simulation.
118  */
119 static void vm_ws_destroy(void *ind_vm_workstation)
120
121         /* ind_phys_workstation equals to smx_host_t */
122         workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_workstation);
123         xbt_assert(vm_ws);
124         xbt_assert(vm_ws->ws.generic_resource.model == surf_vm_workstation_model);
125
126   {
127     int ret = surf_cpu_model_pm->action_unref(vm_ws->cpu_action);
128     xbt_assert(ret == 1, "Bug: some resource still remains");
129   }
130
131         const char *name = vm_ws->ws.generic_resource.name;
132         /* this will call surf_resource_free() */
133         xbt_lib_unset(host_lib, name, SURF_WKS_LEVEL);
134
135   /* NOTE: surf_resource_free() frees vm_ws->ws.generic_resource.name and
136    * vm_ws->ws. Do not free them here. */
137 }
138
139 static int vm_ws_get_state(void *ind_vm_ws){
140         return ((workstation_VM2013_t) surf_workstation_resource_priv(ind_vm_ws))->current_state;
141 }
142
143 static void vm_ws_set_state(void *ind_vm_ws, int state){
144          ((workstation_VM2013_t) surf_workstation_resource_priv(ind_vm_ws))->current_state=state;
145 }
146
147
148 static double get_solved_value(surf_action_t cpu_action)
149 {
150   int found = 0;
151   /* NOTE: Do not use surf_workstation_model's maxmin_system. It is not used. */
152   lmm_system_t pm_system = surf_cpu_model_pm->model_private->maxmin_system;
153   lmm_variable_t var = NULL;
154
155   xbt_swag_foreach(var, &pm_system->variable_set) {
156     XBT_DEBUG("var id %p id_int %d double %f", var->id, var->id_int, var->value);
157     if (var->id == cpu_action) {
158       found = 1;
159       break;
160     }
161   }
162
163   if (found)
164     return var->value;
165
166   XBT_CRITICAL("bug: cannot found the solved variable of the action %p", cpu_action);
167   DIE_IMPOSSIBLE;
168   return -1; /* NOT REACHED */
169 }
170
171
172 static double vm_ws_share_resources(surf_model_t workstation_model, double now)
173 {
174   /* 0. Make sure that we already calculated the resource share at the physical
175    * machine layer. */
176   {
177     unsigned int index_of_pm_ws_model = xbt_dynar_search(model_list_invoke, &surf_workstation_model);
178     unsigned int index_of_vm_ws_model = xbt_dynar_search(model_list_invoke, &surf_vm_workstation_model);
179     xbt_assert((index_of_pm_ws_model < index_of_vm_ws_model), "Cannot assume surf_workstation_model comes before");
180  
181     /* Another option is that we call sub_ws->share_resource() here. The
182      * share_resource() function has no side-effect. We can call it here to
183      * ensure that. */
184   }
185
186
187   /* 1. Now we know how many resource should be assigned to each virtual
188    * machine. We update constraints of the virtual machine layer.
189    *
190    *
191    * If we have two virtual machine (VM1 and VM2) on a physical machine (PM1).
192    *     X1 + X2 = C       (Equation 1)
193    * where
194    *    the resource share of VM1: X1
195    *    the resource share of VM2: X2
196    *    the capacity of PM1: C
197    *
198    * Then, if we have two process (P1 and P2) on VM1.
199    *     X1_1 + X1_2 = X1  (Equation 2)
200    * where
201    *    the resource share of P1: X1_1
202    *    the resource share of P2: X1_2
203    *    the capacity of VM1: X1
204    *
205    * Equation 1 was solved in the physical machine layer.
206    * Equation 2 is solved in the virtual machine layer (here).
207    * X1 must be passed to the virtual machine laye as a constraint value.
208    *
209    **/
210
211   /* iterate for all hosts including virtual machines */
212   xbt_lib_cursor_t cursor;
213   char *key;
214   void **ind_host;
215   xbt_lib_foreach(host_lib, cursor, key, ind_host) {
216     workstation_CLM03_t ws_clm03 = ind_host[SURF_WKS_LEVEL];
217     cpu_Cas01_t cpu_cas01 = ind_host[SURF_CPU_LEVEL];
218
219     /* skip if it is not a virtual machine */
220     if (!ws_clm03)
221       continue;
222     if (ws_clm03->generic_resource.model != surf_vm_workstation_model)
223       continue;
224     xbt_assert(cpu_cas01, "cpu-less workstation");
225
226     /* It is a virtual machine, so we can cast it to workstation_VM2013_t */
227     workstation_VM2013_t ws_vm2013 = (workstation_VM2013_t) ws_clm03;
228
229     double solved_value = get_solved_value(ws_vm2013->cpu_action);
230     XBT_DEBUG("assign %f to vm %s @ pm %s", solved_value,
231         ws_clm03->generic_resource.name, ws_vm2013->sub_ws->generic_resource.name);
232
233     cpu_cas01->constraint->bound = solved_value;
234   }
235
236
237   /* 2. Calculate resource share at the virtual machine layer. */
238   double ret = ws_share_resources(workstation_model, now);
239
240
241   /* FIXME: 3. do we have to re-initialize our cpu_action object? */
242 #if 1
243   /* iterate for all hosts including virtual machines */
244   xbt_lib_foreach(host_lib, cursor, key, ind_host) {
245     workstation_CLM03_t ws_clm03 = ind_host[SURF_WKS_LEVEL];
246
247     /* skip if it is not a virtual machine */
248     if (!ws_clm03)
249       continue;
250     if (ws_clm03->generic_resource.model != surf_vm_workstation_model)
251       continue;
252
253     /* It is a virtual machine, so we can cast it to workstation_VM2013_t */
254     workstation_VM2013_t ws_vm2013 = (workstation_VM2013_t) ws_clm03;
255     {
256       void *ind_sub_host = xbt_lib_get_elm_or_null(host_lib, ws_vm2013->sub_ws->generic_resource.name);
257       surf_cpu_model_pm->action_unref(ws_vm2013->cpu_action);
258       ws_vm2013->cpu_action = surf_cpu_model_pm->extension.cpu.execute(ind_sub_host, 100); // cost 0 is okay?
259     }
260   }
261 #endif
262
263
264   return ret;
265 }
266
267
268 /*
269  * A surf level object will be useless in the upper layer. Returing the name
270  * will be simple and suffcient.
271  **/
272 static const char *vm_ws_get_phys_host(void *ind_vm_ws)
273 {
274         workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_ws);
275         return vm_ws->sub_ws->generic_resource.name;
276 }
277
278 static void surf_vm_workstation_model_init_internal(void)
279 {
280   surf_model_t model = surf_model_init();
281
282   model->name = "Virtual Workstation";
283   model->type = SURF_MODEL_TYPE_VM_WORKSTATION;
284   // model->action_unref     = ws_action_unref;
285   // model->action_cancel    = ws_action_cancel;
286   // model->action_state_set = ws_action_state_set;
287
288
289   model->model_private->share_resources       = vm_ws_share_resources;
290   model->model_private->resource_used         = ws_resource_used;
291   model->model_private->update_actions_state  = ws_update_actions_state;
292   model->model_private->update_resource_state = ws_update_resource_state;
293   model->model_private->finalize              = ws_finalize;
294
295
296 //   model->suspend          = ws_action_suspend;
297 //   model->resume           = ws_action_resume;
298 //   model->is_suspended     = ws_action_is_suspended;
299 //   model->set_max_duration = ws_action_set_max_duration;
300   model->set_priority     = ws_action_set_priority;
301 // #ifdef HAVE_TRACING
302 //   model->set_category     = ws_action_set_category;
303 // #endif
304 //   model->get_remains      = ws_action_get_remains;
305 // #ifdef HAVE_LATENCY_BOUND_TRACKING
306 //   model->get_latency_limited = ws_get_latency_limited;
307 // #endif
308
309
310
311
312
313
314
315   xbt_assert(surf_cpu_model_vm);
316   model->extension.workstation.cpu_model = surf_cpu_model_vm;
317
318   model->extension.workstation.execute   = ws_execute;
319   // model->extension.workstation.sleep     = ws_action_sleep;
320   model->extension.workstation.get_state = ws_get_state;
321   // model->extension.workstation.get_speed = ws_get_speed;
322   // model->extension.workstation.get_available_speed = ws_get_available_speed;
323
324   // model->extension.workstation.communicate           = ws_communicate;
325   // model->extension.workstation.get_route             = ws_get_route;
326   // model->extension.workstation.execute_parallel_task = ws_execute_parallel_task;
327   // model->extension.workstation.get_link_bandwidth    = ws_get_link_bandwidth;
328   // model->extension.workstation.get_link_latency      = ws_get_link_latency;
329   // model->extension.workstation.link_shared           = ws_link_shared;
330   // model->extension.workstation.get_properties        = ws_get_properties;
331
332   // model->extension.workstation.open   = ws_action_open;
333   // model->extension.workstation.close  = ws_action_close;
334   // model->extension.workstation.read   = ws_action_read;
335   // model->extension.workstation.write  = ws_action_write;
336   // model->extension.workstation.stat   = ws_action_stat;
337   // model->extension.workstation.unlink = ws_action_unlink;
338   // model->extension.workstation.ls     = ws_action_ls;
339
340
341   model->extension.vm_workstation.create        = vm_ws_create;
342   model->extension.vm_workstation.set_state     = vm_ws_set_state;
343   model->extension.vm_workstation.get_state     = vm_ws_get_state;
344   model->extension.vm_workstation.migrate       = vm_ws_migrate;
345   model->extension.vm_workstation.get_phys_host = vm_ws_get_phys_host;
346   model->extension.vm_workstation.destroy       = vm_ws_destroy;
347
348   surf_vm_workstation_model = model;
349 }
350
351 void surf_vm_workstation_model_init()
352 {
353   surf_vm_workstation_model_init_internal();
354   xbt_dynar_push(model_list, &surf_vm_workstation_model);
355   xbt_dynar_push(model_list_invoke, &surf_vm_workstation_model);
356 }