Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d68acb2c00f7e8a1e663ed02b0bae8eec2e4bf51
[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 }
95
96 /* ***** set/get state of a VM ***** */
97 void SIMIX_vm_set_state(smx_host_t ind_vm, int state)
98 {
99   surf_vm_workstation_model->extension.vm_workstation.set_state(ind_vm, state);
100 }
101
102 void SIMIX_pre_vm_set_state(smx_simcall_t simcall, smx_host_t ind_vm, int state)
103 {
104   SIMIX_vm_set_state(ind_vm, state);
105 }
106
107 int SIMIX_vm_get_state(smx_host_t ind_vm)
108 {
109   return surf_vm_workstation_model->extension.vm_workstation.get_state(ind_vm);
110 }
111
112 int SIMIX_pre_vm_get_state(smx_simcall_t simcall, smx_host_t ind_vm)
113 {
114   return SIMIX_vm_get_state(ind_vm);
115 }
116
117
118 /**
119  * \brief Function to migrate a SIMIX VM host. 
120  *
121  * \param host the vm host to migrate (a smx_host_t)
122  */
123 void SIMIX_vm_migrate(smx_host_t ind_vm, smx_host_t ind_dst_pm)
124 {
125   const char *name = SIMIX_host_get_name(ind_vm);
126
127   if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_RUNNING)
128     THROWF(vm_error, 0, "VM(%s) is not running", name);
129
130   /* jump to vm_ws_migrate(). this will update the vm location. */
131   surf_vm_workstation_model->extension.vm_workstation.migrate(ind_vm, ind_dst_pm);
132 }
133
134 void SIMIX_pre_vm_migrate(smx_simcall_t simcall, smx_host_t ind_vm, smx_host_t ind_dst_pm)
135 {
136   SIMIX_vm_migrate(ind_vm, ind_dst_pm);
137 }
138
139
140 /**
141  * \brief Function to get the physical host of the given the SIMIX VM host.
142  *
143  * \param host the vm host to get_phys_host (a smx_host_t)
144  */
145 void *SIMIX_vm_get_pm(smx_host_t ind_vm)
146 {
147   /* jump to vm_ws_get_pm(). this will return the vm name. */
148   return surf_vm_workstation_model->extension.vm_workstation.get_pm(ind_vm);
149 }
150
151 void *SIMIX_pre_vm_get_pm(smx_simcall_t simcall, smx_host_t ind_vm)
152 {
153   return SIMIX_vm_get_pm(ind_vm);
154 }
155
156
157 /**
158  * \brief Function to suspend a SIMIX VM host. This function stops the exection of the
159  * VM. All the processes on this VM will pause. The state of the VM is
160  * preserved on memory. We can later resume it again.
161  *
162  * \param host the vm host to suspend (a smx_host_t)
163  */
164 void SIMIX_vm_suspend(smx_host_t ind_vm, smx_process_t issuer)
165 {
166   const char *name = SIMIX_host_get_name(ind_vm);
167
168   if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_RUNNING)
169     THROWF(vm_error, 0, "VM(%s) is not running", name);
170
171   XBT_DEBUG("suspend VM(%s), where %d processes exist", name, xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
172
173   /* jump to vm_ws_suspend. The state will be set. */
174   surf_vm_workstation_model->extension.vm_workstation.suspend(ind_vm);
175
176   smx_process_t smx_process, smx_process_safe;
177   xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(ind_vm)->process_list) {
178     XBT_DEBUG("suspend %s", smx_process->name);
179     SIMIX_process_suspend(smx_process, issuer);
180   }
181
182   XBT_DEBUG("suspend all processes on the VM done done");
183 }
184
185 void SIMIX_pre_vm_suspend(smx_simcall_t simcall, smx_host_t ind_vm)
186 {
187   if (simcall->issuer->smx_host == ind_vm) {
188     XBT_ERROR("cannot suspend the VM where I run");
189     DIE_IMPOSSIBLE;
190   }
191
192   SIMIX_vm_suspend(ind_vm, simcall->issuer);
193
194   /* without this, simcall_vm_suspend() does not return to the userland. why? */
195   SIMIX_simcall_answer(simcall);
196
197   XBT_DEBUG("SIMIX_pre_vm_suspend done");
198 }
199
200
201 /**
202  * \brief Function to resume a SIMIX VM host. This function restart the execution of the
203  * VM. All the processes on this VM will run again. 
204  *
205  * \param host the vm host to resume (a smx_host_t)
206  */
207 void SIMIX_vm_resume(smx_host_t ind_vm, smx_process_t issuer)
208 {
209   const char *name = SIMIX_host_get_name(ind_vm);
210
211   if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_SUSPENDED)
212     THROWF(vm_error, 0, "VM(%s) was not suspended", name);
213
214   XBT_DEBUG("resume VM(%s), where %d processes exist", name, xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
215
216   /* jump to vm_ws_resume() */
217   surf_vm_workstation_model->extension.vm_workstation.resume(ind_vm);
218
219   smx_process_t smx_process, smx_process_safe;
220   xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(ind_vm)->process_list) {
221     XBT_DEBUG("resume %s", smx_process->name);
222     SIMIX_process_resume(smx_process, issuer);
223   }
224 }
225
226 void SIMIX_pre_vm_resume(smx_simcall_t simcall, smx_host_t ind_vm)
227 {
228   SIMIX_vm_resume(ind_vm, simcall->issuer);
229   SIMIX_simcall_answer(simcall);
230 }
231
232
233 /**
234  * \brief Function to save a SIMIX VM host.
235  * This function is the same as vm_suspend, but the state of the VM is saved to the disk, and not preserved on memory.
236  * We can later restore it again.
237  *
238  * \param host the vm host to save (a smx_host_t)
239  */
240 void SIMIX_vm_save(smx_host_t ind_vm, smx_process_t issuer)
241 {
242   const char *name = SIMIX_host_get_name(ind_vm);
243
244   if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_RUNNING)
245     THROWF(vm_error, 0, "VM(%s) is not running", name);
246
247
248   XBT_DEBUG("save VM(%s), where %d processes exist", name, xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
249
250   /* jump to vm_ws_save() */
251   surf_vm_workstation_model->extension.vm_workstation.save(ind_vm);
252
253   smx_process_t smx_process, smx_process_safe;
254   xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(ind_vm)->process_list) {
255     XBT_DEBUG("suspend %s", smx_process->name);
256     SIMIX_process_suspend(smx_process, issuer);
257   }
258 }
259
260 void SIMIX_pre_vm_save(smx_simcall_t simcall, smx_host_t ind_vm)
261 {
262   SIMIX_vm_save(ind_vm, simcall->issuer);
263
264   /* without this, simcall_vm_suspend() does not return to the userland. why? */
265   SIMIX_simcall_answer(simcall);
266 }
267
268
269 /**
270  * \brief Function to restore a SIMIX VM host. This function restart the execution of the
271  * VM. All the processes on this VM will run again. 
272  *
273  * \param host the vm host to restore (a smx_host_t)
274  */
275 void SIMIX_vm_restore(smx_host_t ind_vm, smx_process_t issuer)
276 {
277   const char *name = SIMIX_host_get_name(ind_vm);
278
279   if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_SAVED)
280     THROWF(vm_error, 0, "VM(%s) was not saved", name);
281
282   XBT_DEBUG("restore VM(%s), where %d processes exist", name, xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
283
284   /* jump to vm_ws_restore() */
285   surf_vm_workstation_model->extension.vm_workstation.resume(ind_vm);
286
287   smx_process_t smx_process, smx_process_safe;
288   xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(ind_vm)->process_list) {
289     XBT_DEBUG("resume %s", smx_process->name);
290     SIMIX_process_resume(smx_process, issuer);
291   }
292 }
293
294 void SIMIX_pre_vm_restore(smx_simcall_t simcall, smx_host_t ind_vm)
295 {
296   SIMIX_vm_restore(ind_vm, simcall->issuer);
297   SIMIX_simcall_answer(simcall);
298 }
299
300
301 /**
302  * \brief Function to shutdown a SIMIX VM host. This function powers off the
303  * VM. All the processes on this VM will be killed. But, the state of the VM is
304  * preserved on memory. We can later start it again.
305  *
306  * \param host the vm host to shutdown (a smx_host_t)
307  */
308 void SIMIX_vm_shutdown(smx_host_t ind_vm, smx_process_t issuer)
309 {
310   const char *name = SIMIX_host_get_name(ind_vm);
311
312   if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_RUNNING)
313     THROWF(vm_error, 0, "VM(%s) is not running", name);
314
315   XBT_DEBUG("%d processes in the VM", xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
316
317   smx_process_t smx_process, smx_process_safe;
318   xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(ind_vm)->process_list) {
319     XBT_DEBUG("shutdown %s", name);
320     SIMIX_process_kill(smx_process, issuer);
321   }
322
323   /* FIXME: we may have to do something at the surf layer, e.g., vcpu action */
324   SIMIX_vm_set_state(ind_vm, SURF_VM_STATE_CREATED);
325 }
326
327 void SIMIX_pre_vm_shutdown(smx_simcall_t simcall, smx_host_t ind_vm)
328 {
329   SIMIX_vm_shutdown(ind_vm, simcall->issuer);
330   SIMIX_simcall_answer(simcall);
331 }
332
333
334 /**
335  * \brief Function to destroy a SIMIX VM host.
336  *
337  * \param host the vm host to destroy (a smx_host_t)
338  */
339 void SIMIX_vm_destroy(smx_host_t ind_vm)
340 {
341   /* this code basically performs a similar thing like SIMIX_host_destroy() */
342
343   xbt_assert((ind_vm != NULL), "Invalid parameters");
344   const char *hostname = SIMIX_host_get_name(ind_vm);
345
346   smx_host_priv_t host_priv = SIMIX_host_priv(ind_vm);
347
348   /* this will call the registered callback function, i.e., SIMIX_host_destroy().  */
349   xbt_lib_unset(host_lib, hostname, SIMIX_HOST_LEVEL, 1);
350
351   /* jump to vm_ws_destroy(). The surf level resource will be freed. */
352   surf_vm_workstation_model->extension.vm_workstation.destroy(ind_vm);
353 }
354
355 void SIMIX_pre_vm_destroy(smx_simcall_t simcall, smx_host_t ind_vm){
356    SIMIX_vm_destroy(ind_vm);
357 }