Logo AND Algorithmique Numérique Distribuée

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