Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b84a271be0781b7847ca55e89a4bfa22f0ae67c7
[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 /* FIXME: Where should the VM state be defined?
19  * At now, this must be synchronized with e_msg_vm_state_t */
20 typedef enum {
21   /* created, but not yet started */
22   surf_vm_state_created,
23
24   surf_vm_state_running,
25   surf_vm_state_migrating,
26
27   /* Suspend/resume does not involve disk I/O, so we assume there is no transition states. */
28   surf_vm_state_suspended,
29
30   /* Save/restore involves disk I/O, so there should be transition states. */
31   surf_vm_state_saving,
32   surf_vm_state_saved,
33   surf_vm_state_restoring,
34
35 } e_surf_vm_state_t;
36
37
38
39
40 /* NOTE:
41  * The workstation_VM2013 struct includes the workstation_CLM03 struct in
42  * its first member. The workstation_VM2013_t struct inherites all
43  * characteristics of the workstation_CLM03 struct. So, we can treat a
44  * workstation_VM2013 object as a workstation_CLM03 if necessary.
45  **/
46 typedef struct workstation_VM2013 {
47   s_workstation_CLM03_t ws;    /* a VM is a ''v''host */
48
49   /* The workstation object of the lower layer */
50   workstation_CLM03_t sub_ws;  // Pointer to the ''host'' OS
51
52   e_surf_vm_state_t current_state;
53
54
55   surf_action_t cpu_action;
56
57 } s_workstation_VM2013_t, *workstation_VM2013_t;
58
59 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_vm_workstation, surf,
60                                 "Logging specific to the SURF VM workstation module");
61
62
63
64
65 surf_model_t surf_vm_workstation_model = NULL;
66
67 static void vm_ws_create(const char *name, void *ind_phys_workstation)
68 {
69   workstation_VM2013_t vm_ws = xbt_new0(s_workstation_VM2013_t, 1);
70
71   vm_ws->sub_ws = surf_workstation_resource_priv(ind_phys_workstation);
72   vm_ws->current_state = surf_vm_state_created;
73
74
75   // //// WORKSTATION  RELATED STUFF ////
76   /* Create a workstation_CLM03 resource and register it to the system */
77   __init_workstation_CLM03(&vm_ws->ws, name);
78
79   // Override the model with the current VM one.
80   vm_ws->ws.generic_resource.model = surf_vm_workstation_model;
81
82
83   // //// CPU  RELATED STUFF ////
84   // This can be done directly during the creation of the surf_vm_worsktation model if
85   // we consider that there will be only one CPU model for one layer of virtualization.
86   // However, if you want to provide several layers (i.e. a VM inside a VM hosted by a PM)
87   // we should add a kind of table that stores the different CPU model.
88   // vm_ws->ws->generic_resource.model.extension.cpu=cpu_model_cas01(VM level ? );
89
90  // //// NET RELATED STUFF ////
91   // Bind virtual net_elm to the host
92   // TODO rebind each time you migrate a VM
93   // TODO check how network requests are scheduled between distinct processes competing for the same card.
94   vm_ws->ws.net_elm = xbt_lib_get_or_null(host_lib, vm_ws->sub_ws->generic_resource.name, ROUTING_HOST_LEVEL);
95   xbt_lib_set(host_lib, name, ROUTING_HOST_LEVEL, vm_ws->ws.net_elm);
96
97   // //// STORAGE RELATED STUFF ////
98
99   // ind means ''indirect'' that this is a reference on the whole dict_elm structure (i.e not on the surf_resource_private infos)
100
101
102   /* If you want to get a workstation_VM2013 object from host_lib, see
103    * ws->generic_resouce.model->type first. If it is
104    * SURF_MODEL_TYPE_VM_WORKSTATION, cast ws to vm_ws. */
105   // xbt_lib_set(host_lib, name, SURF_WKS_LEVEL, &vm_ws->ws);
106
107
108
109   cpu_Cas01_t sub_cpu = surf_cpu_resource_priv(ind_phys_workstation);
110
111   /* We can assume one core and cas01 cpu for the first step.
112    * Do xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, cpu) if you get the resource.
113    * */
114   cpu_cas01_create_resource(name, // name
115       sub_cpu->power_peak,        // host->power_peak,
116       1,                          // host->power_scale,
117       NULL,                       // host->power_trace,
118       1,                          // host->core_amount,
119       SURF_RESOURCE_ON,           // host->initial_state,
120       NULL,                       // host->state_trace,
121       NULL,                       // host->properties,
122       surf_cpu_model_vm);
123
124   // void *ind_host = xbt_lib_get_elm_or_null(host_lib, name);
125
126   vm_ws->cpu_action = surf_cpu_model_pm->extension.cpu.execute(ind_phys_workstation, 0); // cost 0 is okay?
127 }
128
129 /*
130  * Update the physical host of the given VM
131  */
132 static void vm_ws_migrate(void *ind_vm_workstation, void *ind_dest_phys_workstation)
133
134    /* ind_phys_workstation equals to smx_host_t */
135    workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_workstation);
136    xbt_assert(vm_ws);
137
138    /* do something */
139
140    vm_ws->sub_ws = surf_workstation_resource_priv(ind_dest_phys_workstation);
141 }
142
143 /*
144  * A physical host does not disapper in the current SimGrid code, but a VM may
145  * disapper during a simulation.
146  */
147 static void vm_ws_destroy(void *ind_vm_workstation)
148
149         /* ind_phys_workstation equals to smx_host_t */
150         workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_workstation);
151         xbt_assert(vm_ws);
152         xbt_assert(vm_ws->ws.generic_resource.model == surf_vm_workstation_model);
153
154   {
155     int ret = surf_cpu_model_pm->action_unref(vm_ws->cpu_action);
156     xbt_assert(ret == 1, "Bug: some resource still remains");
157   }
158
159         const char *name = vm_ws->ws.generic_resource.name;
160         /* this will call surf_resource_free() */
161         xbt_lib_unset(host_lib, name, SURF_WKS_LEVEL);
162
163         xbt_free(vm_ws->ws.generic_resource.name);
164         xbt_free(vm_ws);
165 }
166
167 static int vm_ws_get_state(void *ind_vm_ws){
168         return ((workstation_VM2013_t) surf_workstation_resource_priv(ind_vm_ws))->current_state;
169 }
170
171 static void vm_ws_set_state(void *ind_vm_ws, int state){
172          ((workstation_VM2013_t) surf_workstation_resource_priv(ind_vm_ws))->current_state=state;
173 }
174
175
176 static double get_solved_value(surf_action_t cpu_action)
177 {
178   int found = 0;
179   lmm_system_t pm_system = surf_workstation_model->model_private->maxmin_system;
180   lmm_variable_t var = NULL;
181
182   xbt_swag_foreach(var, &pm_system->variable_set) {
183     XBT_DEBUG("var id %p id_int %d double %f", var->id, var->id_int, var->value);
184     if (var->id == cpu_action) {
185       found = 1;
186       break;
187     }
188   }
189
190   if (found)
191     return var->value;
192
193   XBT_CRITICAL("bug: cannot found the solved variable of the action %p", cpu_action);
194 }
195
196
197 static double vm_ws_share_resources(surf_model_t workstation_model, double now)
198 {
199   /* 0. Make sure that we already calculated the resource share at the physical
200    * machine layer. */
201   {
202     unsigned int index_of_pm_ws_model = xbt_dynar_search(model_list_invoke, surf_workstation_model);
203     unsigned int index_of_vm_ws_model = xbt_dynar_search(model_list_invoke, surf_vm_workstation_model);
204     xbt_assert((index_of_pm_ws_model < index_of_vm_ws_model), "Cannot assume surf_workstation_model comes before");
205
206     /* Another option is that we call sub_ws->share_resource() here. The
207      * share_resource() function has no side-effect. We can call it here to
208      * ensure that. */
209   }
210
211
212   /* 1. Now we know how many resource should be assigned to each virtual
213    * machine. We update constraints of the virtual machine layer.
214    *
215    *
216    * If we have two virtual machine (VM1 and VM2) on a physical machine (PM1).
217    *     X1 + X2 = C       (Equation 1)
218    * where
219    *    the resource share of VM1: X1
220    *    the resource share of VM2: X2
221    *    the capacity of PM1: C
222    *
223    * Then, if we have two process (P1 and P2) on VM1.
224    *     X1_1 + X1_2 = X1  (Equation 2)
225    * where
226    *    the resource share of P1: X1_1
227    *    the resource share of P2: X1_2
228    *    the capacity of VM1: X1
229    *
230    * Equation 1 was solved in the physical machine layer.
231    * Equation 2 is solved in the virtual machine layer (here).
232    * X1 must be passed to the virtual machine laye as a constraint value.
233    *
234    **/
235
236   /* iterate for all hosts including virtual machines */
237   xbt_lib_cursor_t cursor;
238   char *key;
239   void **ind_host;
240   xbt_lib_foreach(host_lib, cursor, key, ind_host) {
241     workstation_CLM03_t ws_clm03 = ind_host[SURF_WKS_LEVEL];
242     cpu_Cas01_t cpu_cas01 = ind_host[SURF_CPU_LEVEL];
243
244     /* skip if it is not a virtual machine */
245     if (!ws_clm03)
246       continue;
247     if (ws_clm03->generic_resource.model != surf_vm_workstation_model)
248       continue;
249     xbt_assert(cpu_cas01, "cpu-less workstation");
250
251     /* It is a virtual machine, so we can cast it to workstation_VM2013_t */
252     workstation_VM2013_t ws_vm2013 = (workstation_VM2013_t) ws_clm03;
253
254     double solved_value = get_solved_value(ws_vm2013->cpu_action);
255     XBT_DEBUG("assign %f to vm %s @ pm %s", solved_value,
256         ws_clm03->generic_resource.name, ws_vm2013->sub_ws->generic_resource.name);
257
258     cpu_cas01->constraint->bound = solved_value;
259   }
260
261
262   /* 2. Calculate resource share at the virtual machine layer. */
263   return ws_share_resources(workstation_model, now);
264 }
265
266
267 /*
268  * A surf level object will be useless in the upper layer. Returing the name
269  * will be simple and suffcient.
270  **/
271 static const char *vm_ws_get_phys_host(void *ind_vm_ws)
272 {
273         workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_ws);
274         return vm_ws->sub_ws->generic_resource.name;
275 }
276
277 static void surf_vm_workstation_model_init_internal(void)
278 {
279   surf_model_t model = surf_model_init();
280
281   model->name = "Virtual Workstation";
282   model->type = SURF_MODEL_TYPE_VM_WORKSTATION;
283
284   model->extension.vm_workstation.basic.cpu_model = surf_cpu_model_vm;
285
286   model->extension.vm_workstation.create        = vm_ws_create;
287   model->extension.vm_workstation.set_state     = vm_ws_set_state;
288   model->extension.vm_workstation.get_state     = vm_ws_get_state;
289   model->extension.vm_workstation.migrate       = vm_ws_migrate;
290   model->extension.vm_workstation.get_phys_host = vm_ws_get_phys_host;
291   model->extension.vm_workstation.destroy       = vm_ws_destroy;
292
293   model->model_private->share_resources       = vm_ws_share_resources;
294   model->model_private->resource_used         = ws_resource_used;
295   model->model_private->update_actions_state  = ws_update_actions_state;
296   model->model_private->update_resource_state = ws_update_resource_state;
297   model->model_private->finalize              = ws_finalize;
298
299   surf_vm_workstation_model = model;
300 }
301
302 void surf_vm_workstation_model_init()
303 {
304   surf_vm_workstation_model_init_internal();
305   xbt_dynar_push(model_list, &surf_vm_workstation_model);
306   xbt_dynar_push(model_list_invoke, &surf_vm_workstation_model);
307 }