Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add minor cleanup around model initialization
[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
16
17 /* FIXME: Where should the VM state be defined?
18  * At now, this must be synchronized with e_msg_vm_state_t */
19 typedef enum {
20   /* created, but not yet started */
21   surf_vm_state_created,
22
23   surf_vm_state_running,
24   surf_vm_state_migrating,
25
26   /* Suspend/resume does not involve disk I/O, so we assume there is no transition states. */
27   surf_vm_state_suspended,
28
29   /* Save/restore involves disk I/O, so there should be transition states. */
30   surf_vm_state_saving,
31   surf_vm_state_saved,
32   surf_vm_state_restoring,
33
34 } e_surf_vm_state_t;
35
36
37
38
39 /* NOTE:
40  * The workstation_VM2013 struct includes the workstation_CLM03 struct in
41  * its first member. The workstation_VM2013_t struct inherites all
42  * characteristics of the workstation_CLM03 struct. So, we can treat a
43  * workstation_VM2013 object as a workstation_CLM03 if necessary.
44  **/
45 typedef struct workstation_VM2013 {
46   s_workstation_CLM03_t ws;    /* a VM is a ''v''host */
47
48   /* The workstation object of the lower layer */
49   workstation_CLM03_t sub_ws;  // Pointer to the ''host'' OS
50
51   e_surf_vm_state_t current_state;
52 } s_workstation_VM2013_t, *workstation_VM2013_t;
53
54 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_vm_workstation, surf,
55                                 "Logging specific to the SURF VM workstation module");
56
57
58
59
60 surf_model_t surf_vm_workstation_model = NULL;
61
62 static void vm_ws_create(const char *name, void *ind_phys_workstation)
63 {
64   workstation_VM2013_t vm_ws = xbt_new0(s_workstation_VM2013_t, 1);
65
66   // //// WORKSTATION  RELATED STUFF ////
67   __init_workstation_CLM03(&vm_ws->ws, name, surf_vm_workstation_model);
68
69   // Override the model with the current VM one.
70   // vm_ws->ws.generic_resource.model = surf_vm_workstation_model;
71
72
73   // //// CPU  RELATED STUFF ////
74   // This can be done directly during the creation of the surf_vm_worsktation model if
75   // we consider that there will be only one CPU model for one layer of virtualization.
76   // However, if you want to provide several layers (i.e. a VM inside a VM hosted by a PM)
77   // we should add a kind of table that stores the different CPU model.
78   // vm_ws->ws->generic_resource.model.extension.cpu=cpu_model_cas01(VM level ? );
79
80  // //// NET RELATED STUFF ////
81   // Bind virtual net_elm to the host
82   // TODO rebind each time you migrate a VM
83   // TODO check how network requests are scheduled between distinct processes competing for the same card.
84   vm_ws->ws.net_elm=xbt_lib_get_or_null(host_lib, vm_ws->sub_ws->generic_resource.name, ROUTING_HOST_LEVEL);
85   xbt_lib_set(host_lib, name, ROUTING_HOST_LEVEL, vm_ws->ws.net_elm);
86
87   // //// STORAGE RELATED STUFF ////
88
89   // ind means ''indirect'' that this is a reference on the whole dict_elm structure (i.e not on the surf_resource_private infos)
90   vm_ws->sub_ws = surf_workstation_resource_priv(ind_phys_workstation);
91   vm_ws->current_state = surf_vm_state_created;
92
93
94   /* If you want to get a workstation_VM2013 object from host_lib, see
95    * ws->generic_resouce.model->type first. If it is
96    * SURF_MODEL_TYPE_VM_WORKSTATION, cast ws to vm_ws. */
97   xbt_lib_set(host_lib, name, SURF_WKS_LEVEL, &vm_ws->ws);
98
99
100
101   cpu_Cas01_t sub_cpu = surf_cpu_resource_priv(ind_phys_workstation);
102
103   /* We can assume one core and cas01 cpu for the first step.
104    * Do xbt_lib_set(host_lib, name, SURF_CPU_LEVEL, cpu) if you get the resource.
105    * */
106   cpu_cas01_create_resource(name, // name
107       sub_cpu->power_peak,        // host->power_peak,
108       1,                          // host->power_scale,
109       NULL,                       // host->power_trace,
110       1,                          // host->core_amount,
111       SURF_RESOURCE_ON,           // host->initial_state,
112       NULL,                       // host->state_trace,
113       NULL,                       // host->properties,
114       surf_cpu_model_vm);
115
116   // void *ind_host = xbt_lib_get_elm_or_null(host_lib, name);
117 }
118
119 /*
120  * Update the physical host of the given VM
121  */
122 static void vm_ws_migrate(void *ind_vm_workstation, void *ind_dest_phys_workstation)
123
124    /* ind_phys_workstation equals to smx_host_t */
125    workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_workstation);
126    xbt_assert(vm_ws);
127
128    /* do something */
129
130    vm_ws->sub_ws = surf_workstation_resource_priv(ind_dest_phys_workstation);
131 }
132
133 /*
134  * A physical host does not disapper in the current SimGrid code, but a VM may
135  * disapper during a simulation.
136  */
137 static void vm_ws_destroy(void *ind_vm_workstation)
138
139         /* ind_phys_workstation equals to smx_host_t */
140         workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_workstation);
141         xbt_assert(vm_ws);
142         xbt_assert(vm_ws->ws.generic_resource.model == surf_vm_workstation_model);
143
144         const char *name = vm_ws->ws.generic_resource.name;
145         /* this will call surf_resource_free() */
146         xbt_lib_unset(host_lib, name, SURF_WKS_LEVEL);
147
148         xbt_free(vm_ws->ws.generic_resource.name);
149         xbt_free(vm_ws);
150 }
151
152 static int vm_ws_get_state(void *ind_vm_ws){
153         return ((workstation_VM2013_t) surf_workstation_resource_priv(ind_vm_ws))->current_state;
154 }
155
156 static void vm_ws_set_state(void *ind_vm_ws, int state){
157          ((workstation_VM2013_t) surf_workstation_resource_priv(ind_vm_ws))->current_state=state;
158 }
159
160
161 static double vm_ws_share_resources(surf_model_t workstation_model, double now)
162 {
163   // Can be obsolete if you right can ensure that ws_model has been code previously
164   // invoke ws_share_resources on the physical_lyer: sub_ws->ws_share_resources()
165   {
166     unsigned int index_of_pm_ws_model = xbt_dynar_search(model_list_invoke, surf_workstation_model);
167     unsigned int index_of_vm_ws_model = xbt_dynar_search(model_list_invoke, surf_vm_workstation_model);
168     xbt_assert((index_of_pm_ws_model < index_of_vm_ws_model), "Cannot assume surf_workstation_model comes before");
169     /* we have to make sure that the share_resource() callback of the model of the lower layer must be called in advance. */
170   }
171
172         // assign the corresponding value to X1, X2, ....
173
174    // invoke cpu and net share resources on layer (1)
175         // return min;
176   return -1.0;
177 }
178
179
180 /*
181  * A surf level object will be useless in the upper layer. Returing the name
182  * will be simple and suffcient.
183  **/
184 static const char *vm_ws_get_phys_host(void *ind_vm_ws)
185 {
186         workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_ws);
187         return vm_ws->sub_ws->generic_resource.name;
188 }
189
190 static void surf_vm_workstation_model_init_internal(void)
191 {
192   surf_model_t model = surf_model_init();
193
194   model->name = "Virtual Workstation";
195   model->type = SURF_MODEL_TYPE_VM_WORKSTATION;
196
197   model->extension.vm_workstation.basic.cpu_model = surf_cpu_model_vm;
198
199   model->extension.vm_workstation.create        = vm_ws_create;
200   model->extension.vm_workstation.set_state     = vm_ws_set_state;
201   model->extension.vm_workstation.get_state     = vm_ws_get_state;
202   model->extension.vm_workstation.migrate       = vm_ws_migrate;
203   model->extension.vm_workstation.get_phys_host = vm_ws_get_phys_host;
204   model->extension.vm_workstation.destroy       = vm_ws_destroy;
205
206   model->model_private->share_resources       = vm_ws_share_resources;
207   model->model_private->resource_used         = ws_resource_used;
208   model->model_private->update_actions_state  = ws_update_actions_state;
209   model->model_private->update_resource_state = ws_update_resource_state;
210   model->model_private->finalize              = ws_finalize;
211
212   surf_vm_workstation_model = model;
213 }
214
215 void surf_vm_workstation_model_init()
216 {
217   surf_vm_workstation_model_init_internal();
218   xbt_dynar_push(model_list, &surf_vm_workstation_model);
219   xbt_dynar_push(model_list_invoke, &surf_vm_workstation_model);
220 }