Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add the definition of VM state to the surf layer
[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
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  * Update the physical host of the given VM
102  */
103 static void vm_ws_migrate(void *ind_vm_workstation, void *ind_dest_phys_workstation)
104
105    /* ind_phys_workstation equals to smx_host_t */
106    workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_workstation);
107    xbt_assert(vm_ws);
108
109    /* do something */
110
111    vm_ws->sub_ws = surf_workstation_resource_priv(ind_dest_phys_workstation);
112 }
113
114 /*
115  * A physical host does not disapper in the current SimGrid code, but a VM may
116  * disapper during a simulation.
117  */
118 static void vm_ws_destroy(void *ind_vm_workstation)
119
120         /* ind_phys_workstation equals to smx_host_t */
121         workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_workstation);
122         xbt_assert(vm_ws);
123         xbt_assert(vm_ws->ws.generic_resource.model == surf_vm_workstation_model);
124
125         const char *name = vm_ws->ws.generic_resource.name;
126         /* this will call surf_resource_free() */
127         xbt_lib_unset(host_lib, name, SURF_WKS_LEVEL);
128
129         xbt_free(vm_ws->ws.generic_resource.name);
130         xbt_free(vm_ws);
131 }
132
133 static int vm_ws_get_state(void *ind_vm_ws){
134         return ((workstation_VM2013_t) surf_workstation_resource_priv(ind_vm_ws))->current_state;
135 }
136
137 static void vm_ws_set_state(void *ind_vm_ws, int state){
138          ((workstation_VM2013_t) surf_workstation_resource_priv(ind_vm_ws))->current_state=state;
139 }
140
141
142 static double vm_ws_share_resources(surf_model_t workstation_model, double now)
143 {
144   // Can be obsolete if you right can ensure that ws_model has been code previously
145   // invoke ws_share_resources on the physical_lyer: sub_ws->ws_share_resources()
146   {
147     unsigned int index_of_pm_ws_model = xbt_dynar_search(model_list_invoke, surf_workstation_model);
148     unsigned int index_of_vm_ws_model = xbt_dynar_search(model_list_invoke, surf_vm_workstation_model);
149     xbt_assert((index_of_pm_ws_model < index_of_vm_ws_model), "Cannot assume surf_workstation_model comes before");
150     /* we have to make sure that the share_resource() callback of the model of the lower layer must be called in advance. */
151   }
152
153         // assign the corresponding value to X1, X2, ....
154
155    // invoke cpu and net share resources on layer (1)
156         // return min;
157   return -1.0;
158 }
159
160
161 /*
162  * A surf level object will be useless in the upper layer. Returing the name
163  * will be simple and suffcient.
164  **/
165 static const char *vm_ws_get_phys_host(void *ind_vm_ws)
166 {
167         workstation_VM2013_t vm_ws = surf_workstation_resource_priv(ind_vm_ws);
168         return vm_ws->sub_ws->generic_resource.name;
169 }
170
171 static void surf_vm_workstation_model_init_internal(void)
172 {
173   surf_vm_workstation_model = surf_model_init();
174
175   surf_vm_workstation_model->name = "Virtual Workstation";
176   surf_vm_workstation_model->type = SURF_MODEL_TYPE_VM_WORKSTATION;
177
178   surf_vm_workstation_model->extension.vm_workstation.basic.cpu_model = surf_cpu_model_vm;
179
180   surf_vm_workstation_model->extension.vm_workstation.create = vm_ws_create;
181   surf_vm_workstation_model->extension.vm_workstation.set_state = vm_ws_set_state;
182   surf_vm_workstation_model->extension.vm_workstation.get_state = vm_ws_get_state;
183   surf_vm_workstation_model->extension.vm_workstation.migrate = vm_ws_migrate;
184   surf_vm_workstation_model->extension.vm_workstation.get_phys_host = vm_ws_get_phys_host;
185   surf_vm_workstation_model->extension.vm_workstation.destroy = vm_ws_destroy;
186
187   surf_vm_workstation_model->model_private->share_resources = vm_ws_share_resources;
188 }
189
190 void surf_vm_workstation_model_init()
191 {
192   surf_vm_workstation_model_init_internal();
193   xbt_dynar_push(model_list, &surf_vm_workstation_model);
194   xbt_dynar_push(model_list_invoke, &surf_vm_workstation_model);
195 }