Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix problems so that the VM example works
[simgrid.git] / src / simix / smx_vm.c
1 /* Copyright (c) 2007-2012. 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 "smx_private.h"
8 #include "xbt/sysdep.h"
9 #include "xbt/log.h"
10 #include "xbt/dict.h"
11 #include "mc/mc.h"
12
13 //If you need to log some stuffs, just uncomment these two lines and uses XBT_DEBUG for instance
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_vm, simix, "Logging specific to SIMIX (vms)");
15
16 /* **** create a VM **** */
17
18 /**
19  * \brief Internal function to create a SIMIX host.
20  * \param name name of the host to create
21  * \param data some user data (may be NULL)
22  */
23 smx_host_t SIMIX_vm_create(const char *name, smx_host_t ind_phys_host)
24 {
25
26   smx_host_priv_t smx_host = xbt_new0(s_smx_host_priv_t, 1);
27   s_smx_process_t proc;
28
29   // TODO check why we do not have any VM here and why we have the host_proc_hookup  ?
30
31   /* Host structure */
32   smx_host->data = NULL;
33   smx_host->process_list =
34       xbt_swag_new(xbt_swag_offset(proc, host_proc_hookup));
35
36   /* Update global variables */
37   xbt_lib_set(host_lib, name, SIMIX_HOST_LEVEL, smx_host);
38
39   /* Create surf associated resource */
40   // TODO change phys_host into the right workstation surf model
41   surf_vm_workstation_model->extension.vm_workstation.create(name, ind_phys_host);
42
43   return xbt_lib_get_elm_or_null(host_lib, name);
44 }
45
46
47 smx_host_t SIMIX_pre_vm_create(smx_simcall_t simcall, const char *name, smx_host_t ind_phys_host)
48 {
49   return SIMIX_vm_create(name, ind_phys_host);
50 }
51
52
53 static int get_host_property_as_integer(smx_host_t host, const char *name)
54 {
55   xbt_dict_t dict = SIMIX_host_get_properties(host);
56
57   char *value = xbt_dict_get_or_null(dict, name);
58   return atoi(value);
59 }
60
61
62
63 /* **** start a VM **** */
64 static int __can_be_started(smx_host_t vm)
65 {
66         // TODO add checking code related to overcommitment or not.
67
68 #if 0
69   int overcommit = get_host_property_as_integer(vm, "OverCommit");
70   int core_nb = get_host_property_as_integer(vm, "CORE_NB");
71   int mem_cap = get_host_property_as_integer(vm, "MEM_CAP");
72   int net_cap = get_host_property_as_integer(vm, "NET_CAP");
73 #endif
74
75   /* we need to get other VM objects on this physical host. */
76
77
78
79         return 1;
80 }
81
82 void SIMIX_vm_start(smx_host_t ind_vm)
83 {
84   //TODO only start the VM if you can
85   if (__can_be_started(ind_vm))
86     SIMIX_vm_set_state(ind_vm, SURF_VM_STATE_RUNNING);
87   else
88     THROWF(vm_error, 0, "The VM %s cannot be started", SIMIX_host_get_name(ind_vm));
89 }
90
91 void SIMIX_pre_vm_start(smx_simcall_t simcall, smx_host_t ind_vm)
92 {
93   SIMIX_vm_start(ind_vm);
94   SIMIX_simcall_answer(simcall);
95 }
96
97 /* ***** set/get state of a VM ***** */
98 void SIMIX_vm_set_state(smx_host_t ind_vm, int state)
99 {
100   /* jump to vm_ws_set_state */
101   surf_vm_workstation_model->extension.vm_workstation.set_state(ind_vm, state);
102 }
103
104 void SIMIX_pre_vm_set_state(smx_simcall_t simcall, smx_host_t ind_vm, int state)
105 {
106   SIMIX_vm_set_state(ind_vm, state);
107 }
108
109 int SIMIX_vm_get_state(smx_host_t ind_vm)
110 {
111   return surf_vm_workstation_model->extension.vm_workstation.get_state(ind_vm);
112 }
113
114 int SIMIX_pre_vm_get_state(smx_simcall_t simcall, smx_host_t ind_vm)
115 {
116   return SIMIX_vm_get_state(ind_vm);
117 }
118
119
120 /**
121  * \brief Function to migrate a SIMIX VM host. 
122  *
123  * \param host the vm host to migrate (a smx_host_t)
124  */
125 void SIMIX_vm_migrate(smx_host_t ind_vm, smx_host_t ind_dst_pm)
126 {
127   const char *name = SIMIX_host_get_name(ind_vm);
128
129   if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_RUNNING)
130     THROWF(vm_error, 0, "VM(%s) is not running", name);
131
132   /* jump to vm_ws_migrate(). this will update the vm location. */
133   surf_vm_workstation_model->extension.vm_workstation.migrate(ind_vm, ind_dst_pm);
134 }
135
136 void SIMIX_pre_vm_migrate(smx_simcall_t simcall, smx_host_t ind_vm, smx_host_t ind_dst_pm)
137 {
138   SIMIX_vm_migrate(ind_vm, ind_dst_pm);
139   SIMIX_simcall_answer(simcall);
140 }
141
142
143 /**
144  * \brief Function to get the physical host of the given the SIMIX VM host.
145  *
146  * \param host the vm host to get_phys_host (a smx_host_t)
147  */
148 void *SIMIX_vm_get_pm(smx_host_t ind_vm)
149 {
150   /* jump to vm_ws_get_pm(). this will return the vm name. */
151   return surf_vm_workstation_model->extension.vm_workstation.get_pm(ind_vm);
152 }
153
154 void *SIMIX_pre_vm_get_pm(smx_simcall_t simcall, smx_host_t ind_vm)
155 {
156   return SIMIX_vm_get_pm(ind_vm);
157 }
158
159
160 /**
161  * \brief Function to suspend a SIMIX VM host. This function stops the exection of the
162  * VM. All the processes on this VM will pause. The state of the VM is
163  * preserved on memory. We can later resume it again.
164  *
165  * \param host the vm host to suspend (a smx_host_t)
166  */
167 void SIMIX_vm_suspend(smx_host_t ind_vm, smx_process_t issuer)
168 {
169   const char *name = SIMIX_host_get_name(ind_vm);
170
171   if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_RUNNING)
172     THROWF(vm_error, 0, "VM(%s) is not running", name);
173
174   XBT_DEBUG("suspend VM(%s), where %d processes exist", name, xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
175
176   /* jump to vm_ws_suspend. The state will be set. */
177   surf_vm_workstation_model->extension.vm_workstation.suspend(ind_vm);
178
179   smx_process_t smx_process, smx_process_safe;
180   xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(ind_vm)->process_list) {
181     XBT_DEBUG("suspend %s", smx_process->name);
182     SIMIX_process_suspend(smx_process, issuer);
183   }
184
185   XBT_DEBUG("suspend all processes on the VM done done");
186 }
187
188 void SIMIX_pre_vm_suspend(smx_simcall_t simcall, smx_host_t ind_vm)
189 {
190   if (simcall->issuer->smx_host == ind_vm) {
191     XBT_ERROR("cannot suspend the VM where I run");
192     DIE_IMPOSSIBLE;
193   }
194
195   SIMIX_vm_suspend(ind_vm, simcall->issuer);
196
197   /* without this, simcall_vm_suspend() does not return to the userland. why? */
198   SIMIX_simcall_answer(simcall);
199
200   XBT_DEBUG("SIMIX_pre_vm_suspend done");
201 }
202
203
204 /**
205  * \brief Function to resume a SIMIX VM host. This function restart the execution of the
206  * VM. All the processes on this VM will run again. 
207  *
208  * \param host the vm host to resume (a smx_host_t)
209  */
210 void SIMIX_vm_resume(smx_host_t ind_vm, smx_process_t issuer)
211 {
212   const char *name = SIMIX_host_get_name(ind_vm);
213
214   if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_SUSPENDED)
215     THROWF(vm_error, 0, "VM(%s) was not suspended", name);
216
217   XBT_DEBUG("resume VM(%s), where %d processes exist", name, xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
218
219   /* jump to vm_ws_resume() */
220   surf_vm_workstation_model->extension.vm_workstation.resume(ind_vm);
221
222   smx_process_t smx_process, smx_process_safe;
223   xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(ind_vm)->process_list) {
224     XBT_DEBUG("resume %s", smx_process->name);
225     SIMIX_process_resume(smx_process, issuer);
226   }
227 }
228
229 void SIMIX_pre_vm_resume(smx_simcall_t simcall, smx_host_t ind_vm)
230 {
231   SIMIX_vm_resume(ind_vm, simcall->issuer);
232   SIMIX_simcall_answer(simcall);
233 }
234
235
236 /**
237  * \brief Function to save a SIMIX VM host.
238  * This function is the same as vm_suspend, but the state of the VM is saved to the disk, and not preserved on memory.
239  * We can later restore it again.
240  *
241  * \param host the vm host to save (a smx_host_t)
242  */
243 void SIMIX_vm_save(smx_host_t ind_vm, smx_process_t issuer)
244 {
245   const char *name = SIMIX_host_get_name(ind_vm);
246
247   if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_RUNNING)
248     THROWF(vm_error, 0, "VM(%s) is not running", name);
249
250
251   XBT_DEBUG("save VM(%s), where %d processes exist", name, xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
252
253   /* jump to vm_ws_save() */
254   surf_vm_workstation_model->extension.vm_workstation.save(ind_vm);
255
256   smx_process_t smx_process, smx_process_safe;
257   xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(ind_vm)->process_list) {
258     XBT_DEBUG("suspend %s", smx_process->name);
259     SIMIX_process_suspend(smx_process, issuer);
260   }
261 }
262
263 void SIMIX_pre_vm_save(smx_simcall_t simcall, smx_host_t ind_vm)
264 {
265   SIMIX_vm_save(ind_vm, simcall->issuer);
266
267   /* without this, simcall_vm_suspend() does not return to the userland. why? */
268   SIMIX_simcall_answer(simcall);
269 }
270
271
272 /**
273  * \brief Function to restore a SIMIX VM host. This function restart the execution of the
274  * VM. All the processes on this VM will run again. 
275  *
276  * \param host the vm host to restore (a smx_host_t)
277  */
278 void SIMIX_vm_restore(smx_host_t ind_vm, smx_process_t issuer)
279 {
280   const char *name = SIMIX_host_get_name(ind_vm);
281
282   if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_SAVED)
283     THROWF(vm_error, 0, "VM(%s) was not saved", name);
284
285   XBT_DEBUG("restore VM(%s), where %d processes exist", name, xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
286
287   /* jump to vm_ws_restore() */
288   surf_vm_workstation_model->extension.vm_workstation.resume(ind_vm);
289
290   smx_process_t smx_process, smx_process_safe;
291   xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(ind_vm)->process_list) {
292     XBT_DEBUG("resume %s", smx_process->name);
293     SIMIX_process_resume(smx_process, issuer);
294   }
295 }
296
297 void SIMIX_pre_vm_restore(smx_simcall_t simcall, smx_host_t ind_vm)
298 {
299   SIMIX_vm_restore(ind_vm, simcall->issuer);
300   SIMIX_simcall_answer(simcall);
301 }
302
303
304 /**
305  * \brief Function to shutdown a SIMIX VM host. This function powers off the
306  * VM. All the processes on this VM will be killed. But, the state of the VM is
307  * preserved on memory. We can later start it again.
308  *
309  * \param host the vm host to shutdown (a smx_host_t)
310  */
311 void SIMIX_vm_shutdown(smx_host_t ind_vm, smx_process_t issuer)
312 {
313   const char *name = SIMIX_host_get_name(ind_vm);
314
315   if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_RUNNING)
316     THROWF(vm_error, 0, "VM(%s) is not running", name);
317
318   XBT_DEBUG("%d processes in the VM", xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
319
320   smx_process_t smx_process, smx_process_safe;
321   xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(ind_vm)->process_list) {
322     XBT_DEBUG("shutdown %s", name);
323     SIMIX_process_kill(smx_process, issuer);
324   }
325
326   /* FIXME: we may have to do something at the surf layer, e.g., vcpu action */
327   SIMIX_vm_set_state(ind_vm, SURF_VM_STATE_CREATED);
328 }
329
330 void SIMIX_pre_vm_shutdown(smx_simcall_t simcall, smx_host_t ind_vm)
331 {
332   SIMIX_vm_shutdown(ind_vm, simcall->issuer);
333   SIMIX_simcall_answer(simcall);
334 }
335
336
337 /**
338  * \brief Function to destroy a SIMIX VM host.
339  *
340  * \param host the vm host to destroy (a smx_host_t)
341  */
342 void SIMIX_vm_destroy(smx_host_t ind_vm)
343 {
344   /* this code basically performs a similar thing like SIMIX_host_destroy() */
345
346   xbt_assert((ind_vm != NULL), "Invalid parameters");
347   const char *hostname = SIMIX_host_get_name(ind_vm);
348
349   smx_host_priv_t host_priv = SIMIX_host_priv(ind_vm);
350
351   /* this will call the registered callback function, i.e., SIMIX_host_destroy().  */
352   xbt_lib_unset(host_lib, hostname, SIMIX_HOST_LEVEL, 1);
353
354   /* jump to vm_ws_destroy(). The surf level resource will be freed. */
355   surf_vm_workstation_model->extension.vm_workstation.destroy(ind_vm);
356 }
357
358 void SIMIX_pre_vm_destroy(smx_simcall_t simcall, smx_host_t ind_vm)
359 {
360   SIMIX_vm_destroy(ind_vm);
361   SIMIX_simcall_answer(simcall);
362 }