1 /* Copyright (c) 2007-2012. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "smx_private.h"
8 #include "xbt/sysdep.h"
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)");
16 /* **** create a VM **** */
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)
23 smx_host_t SIMIX_vm_create(const char *name, smx_host_t ind_phys_host)
26 smx_host_priv_t smx_host = xbt_new0(s_smx_host_priv_t, 1);
29 // TODO check why we do not have any VM here and why we have the host_proc_hookup ?
32 smx_host->data = NULL;
33 smx_host->process_list =
34 xbt_swag_new(xbt_swag_offset(proc, host_proc_hookup));
36 /* Update global variables */
37 xbt_lib_set(host_lib,name,SIMIX_HOST_LEVEL,smx_host);
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);
43 return xbt_lib_get_elm_or_null(host_lib, name);
47 smx_host_t SIMIX_pre_vm_create(smx_simcall_t simcall, const char *name, smx_host_t ind_phys_host)
49 return SIMIX_vm_create(name, ind_phys_host);
53 static int get_host_property_as_integer(smx_host_t host, const char *name)
55 xbt_dict_t dict = SIMIX_host_get_properties(host);
57 char *value = xbt_dict_get_or_null(dict, name);
63 /* **** start a VM **** */
64 static int __can_be_started(smx_host_t vm)
66 // TODO add checking code related to overcommitment or not.
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");
75 /* we need to get other VM objects on this physical host. */
82 void SIMIX_vm_start(smx_host_t ind_vm)
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);
88 THROWF(vm_error, 0, "The VM %s cannot be started", SIMIX_host_get_name(ind_vm));
91 void SIMIX_pre_vm_start(smx_simcall_t simcall, smx_host_t ind_vm)
93 SIMIX_vm_start(ind_vm);
96 /* ***** set/get state of a VM ***** */
97 void SIMIX_vm_set_state(smx_host_t ind_vm, int state)
99 surf_vm_workstation_model->extension.vm_workstation.set_state(ind_vm, state);
102 void SIMIX_pre_vm_set_state(smx_simcall_t simcall, smx_host_t ind_vm, int state)
104 SIMIX_vm_set_state(ind_vm, state);
107 int SIMIX_vm_get_state(smx_host_t ind_vm)
109 return surf_vm_workstation_model->extension.vm_workstation.get_state(ind_vm);
112 int SIMIX_pre_vm_get_state(smx_simcall_t simcall, smx_host_t ind_vm)
114 return SIMIX_vm_get_state(ind_vm);
119 * \brief Function to migrate a SIMIX VM host.
121 * \param host the vm host to migrate (a smx_host_t)
123 void SIMIX_vm_migrate(smx_host_t ind_vm, smx_host_t ind_dst_pm)
125 const char *name = SIMIX_host_get_name(ind_vm);
127 if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_RUNNING)
128 THROWF(vm_error, 0, "VM(%s) is not running", name);
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);
134 void SIMIX_pre_vm_migrate(smx_simcall_t simcall, smx_host_t ind_vm, smx_host_t ind_dst_pm)
136 SIMIX_vm_migrate(ind_vm, ind_dst_pm);
141 * \brief Function to get the physical host of the given the SIMIX VM host.
143 * \param host the vm host to get_phys_host (a smx_host_t)
145 void *SIMIX_vm_get_pm(smx_host_t ind_vm)
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);
151 void *SIMIX_pre_vm_get_pm(smx_simcall_t simcall, smx_host_t ind_vm)
153 return SIMIX_vm_get_pm(ind_vm);
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.
162 * \param host the vm host to suspend (a smx_host_t)
164 void SIMIX_vm_suspend(smx_host_t ind_vm, smx_process_t issuer)
166 const char *name = SIMIX_host_get_name(ind_vm);
168 if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_RUNNING)
169 THROWF(vm_error, 0, "VM(%s) is not running", name);
171 XBT_DEBUG("suspend VM(%s), where %d processes exist", name, xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
173 /* jump to vm_ws_suspend. The state will be set. */
174 surf_vm_workstation_model->extension.vm_workstation.suspend(ind_vm);
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);
182 XBT_DEBUG("suspend all processes on the VM done done");
185 void SIMIX_pre_vm_suspend(smx_simcall_t simcall, smx_host_t ind_vm)
187 if (simcall->issuer->smx_host == ind_vm) {
188 XBT_ERROR("cannot suspend the VM where I run");
192 SIMIX_vm_suspend(ind_vm, simcall->issuer);
194 /* without this, simcall_vm_suspend() does not return to the userland. why? */
195 SIMIX_simcall_answer(simcall);
197 XBT_DEBUG("SIMIX_pre_vm_suspend done");
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.
205 * \param host the vm host to resume (a smx_host_t)
207 void SIMIX_vm_resume(smx_host_t ind_vm, smx_process_t issuer)
209 const char *name = SIMIX_host_get_name(ind_vm);
211 if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_SUSPENDED)
212 THROWF(vm_error, 0, "VM(%s) was not suspended", name);
214 XBT_DEBUG("resume VM(%s), where %d processes exist", name, xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
216 /* jump to vm_ws_resume() */
217 surf_vm_workstation_model->extension.vm_workstation.resume(ind_vm);
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);
226 void SIMIX_pre_vm_resume(smx_simcall_t simcall, smx_host_t ind_vm)
228 SIMIX_vm_resume(ind_vm, simcall->issuer);
229 SIMIX_simcall_answer(simcall);
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.
238 * \param host the vm host to save (a smx_host_t)
240 void SIMIX_vm_save(smx_host_t ind_vm, smx_process_t issuer)
242 const char *name = SIMIX_host_get_name(ind_vm);
244 if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_RUNNING)
245 THROWF(vm_error, 0, "VM(%s) is not running", name);
248 XBT_DEBUG("save VM(%s), where %d processes exist", name, xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
250 /* jump to vm_ws_save() */
251 surf_vm_workstation_model->extension.vm_workstation.save(ind_vm);
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);
260 void SIMIX_pre_vm_save(smx_simcall_t simcall, smx_host_t ind_vm)
262 SIMIX_vm_save(ind_vm, simcall->issuer);
264 /* without this, simcall_vm_suspend() does not return to the userland. why? */
265 SIMIX_simcall_answer(simcall);
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.
273 * \param host the vm host to restore (a smx_host_t)
275 void SIMIX_vm_restore(smx_host_t ind_vm, smx_process_t issuer)
277 const char *name = SIMIX_host_get_name(ind_vm);
279 if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_SAVED)
280 THROWF(vm_error, 0, "VM(%s) was not saved", name);
282 XBT_DEBUG("restore VM(%s), where %d processes exist", name, xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
284 /* jump to vm_ws_restore() */
285 surf_vm_workstation_model->extension.vm_workstation.resume(ind_vm);
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);
294 void SIMIX_pre_vm_restore(smx_simcall_t simcall, smx_host_t ind_vm)
296 SIMIX_vm_restore(ind_vm, simcall->issuer);
297 SIMIX_simcall_answer(simcall);
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.
306 * \param host the vm host to shutdown (a smx_host_t)
308 void SIMIX_vm_shutdown(smx_host_t ind_vm, smx_process_t issuer)
310 const char *name = SIMIX_host_get_name(ind_vm);
312 if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_RUNNING)
313 THROWF(vm_error, 0, "VM(%s) is not running", name);
315 XBT_DEBUG("%d processes in the VM", xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
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);
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);
327 void SIMIX_pre_vm_shutdown(smx_simcall_t simcall, smx_host_t ind_vm)
329 SIMIX_vm_shutdown(ind_vm, simcall->issuer);
330 SIMIX_simcall_answer(simcall);
335 * \brief Function to destroy a SIMIX VM host.
337 * \param host the vm host to destroy (a smx_host_t)
339 void SIMIX_vm_destroy(smx_host_t ind_vm)
341 /* this code basically performs a similar thing like SIMIX_host_destroy() */
343 xbt_assert((ind_vm != NULL), "Invalid parameters");
344 const char *hostname = SIMIX_host_get_name(ind_vm);
346 smx_host_priv_t host_priv = SIMIX_host_priv(ind_vm);
348 /* this will call the registered callback function, i.e., SIMIX_host_destroy(). */
349 xbt_lib_unset(host_lib, hostname, SIMIX_HOST_LEVEL, 1);
351 /* jump to vm_ws_destroy(). The surf level resource will be freed. */
352 surf_vm_workstation_model->extension.vm_workstation.destroy(ind_vm);
355 void SIMIX_pre_vm_destroy(smx_simcall_t simcall, smx_host_t ind_vm){
356 SIMIX_vm_destroy(ind_vm);