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);
94 SIMIX_simcall_answer(simcall);
97 /* ***** set/get state of a VM ***** */
98 void SIMIX_vm_set_state(smx_host_t ind_vm, int state)
100 /* jump to vm_ws_set_state */
101 surf_vm_workstation_model->extension.vm_workstation.set_state(ind_vm, state);
104 void SIMIX_pre_vm_set_state(smx_simcall_t simcall, smx_host_t ind_vm, int state)
106 SIMIX_vm_set_state(ind_vm, state);
109 int SIMIX_vm_get_state(smx_host_t ind_vm)
111 return surf_vm_workstation_model->extension.vm_workstation.get_state(ind_vm);
114 int SIMIX_pre_vm_get_state(smx_simcall_t simcall, smx_host_t ind_vm)
116 return SIMIX_vm_get_state(ind_vm);
121 * \brief Function to migrate a SIMIX VM host.
123 * \param host the vm host to migrate (a smx_host_t)
125 void SIMIX_vm_migrate(smx_host_t ind_vm, smx_host_t ind_dst_pm)
127 const char *name = SIMIX_host_get_name(ind_vm);
129 if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_RUNNING)
130 THROWF(vm_error, 0, "VM(%s) is not running", name);
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);
136 void SIMIX_pre_vm_migrate(smx_simcall_t simcall, smx_host_t ind_vm, smx_host_t ind_dst_pm)
138 SIMIX_vm_migrate(ind_vm, ind_dst_pm);
139 SIMIX_simcall_answer(simcall);
144 * \brief Function to get the physical host of the given the SIMIX VM host.
146 * \param host the vm host to get_phys_host (a smx_host_t)
148 void *SIMIX_vm_get_pm(smx_host_t ind_vm)
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);
154 void *SIMIX_pre_vm_get_pm(smx_simcall_t simcall, smx_host_t ind_vm)
156 return SIMIX_vm_get_pm(ind_vm);
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.
165 * \param host the vm host to suspend (a smx_host_t)
167 void SIMIX_vm_suspend(smx_host_t ind_vm, smx_process_t issuer)
169 const char *name = SIMIX_host_get_name(ind_vm);
171 if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_RUNNING)
172 THROWF(vm_error, 0, "VM(%s) is not running", name);
174 XBT_DEBUG("suspend VM(%s), where %d processes exist", name, xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
176 /* jump to vm_ws_suspend. The state will be set. */
177 surf_vm_workstation_model->extension.vm_workstation.suspend(ind_vm);
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);
185 XBT_DEBUG("suspend all processes on the VM done done");
188 void SIMIX_pre_vm_suspend(smx_simcall_t simcall, smx_host_t ind_vm)
190 if (simcall->issuer->smx_host == ind_vm) {
191 XBT_ERROR("cannot suspend the VM where I run");
195 SIMIX_vm_suspend(ind_vm, simcall->issuer);
197 /* without this, simcall_vm_suspend() does not return to the userland. why? */
198 SIMIX_simcall_answer(simcall);
200 XBT_DEBUG("SIMIX_pre_vm_suspend done");
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.
208 * \param host the vm host to resume (a smx_host_t)
210 void SIMIX_vm_resume(smx_host_t ind_vm, smx_process_t issuer)
212 const char *name = SIMIX_host_get_name(ind_vm);
214 if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_SUSPENDED)
215 THROWF(vm_error, 0, "VM(%s) was not suspended", name);
217 XBT_DEBUG("resume VM(%s), where %d processes exist", name, xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
219 /* jump to vm_ws_resume() */
220 surf_vm_workstation_model->extension.vm_workstation.resume(ind_vm);
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);
229 void SIMIX_pre_vm_resume(smx_simcall_t simcall, smx_host_t ind_vm)
231 SIMIX_vm_resume(ind_vm, simcall->issuer);
232 SIMIX_simcall_answer(simcall);
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.
241 * \param host the vm host to save (a smx_host_t)
243 void SIMIX_vm_save(smx_host_t ind_vm, smx_process_t issuer)
245 const char *name = SIMIX_host_get_name(ind_vm);
247 if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_RUNNING)
248 THROWF(vm_error, 0, "VM(%s) is not running", name);
251 XBT_DEBUG("save VM(%s), where %d processes exist", name, xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
253 /* jump to vm_ws_save() */
254 surf_vm_workstation_model->extension.vm_workstation.save(ind_vm);
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);
263 void SIMIX_pre_vm_save(smx_simcall_t simcall, smx_host_t ind_vm)
265 SIMIX_vm_save(ind_vm, simcall->issuer);
267 /* without this, simcall_vm_suspend() does not return to the userland. why? */
268 SIMIX_simcall_answer(simcall);
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.
276 * \param host the vm host to restore (a smx_host_t)
278 void SIMIX_vm_restore(smx_host_t ind_vm, smx_process_t issuer)
280 const char *name = SIMIX_host_get_name(ind_vm);
282 if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_SAVED)
283 THROWF(vm_error, 0, "VM(%s) was not saved", name);
285 XBT_DEBUG("restore VM(%s), where %d processes exist", name, xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
287 /* jump to vm_ws_restore() */
288 surf_vm_workstation_model->extension.vm_workstation.resume(ind_vm);
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);
297 void SIMIX_pre_vm_restore(smx_simcall_t simcall, smx_host_t ind_vm)
299 SIMIX_vm_restore(ind_vm, simcall->issuer);
300 SIMIX_simcall_answer(simcall);
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.
309 * \param host the vm host to shutdown (a smx_host_t)
311 void SIMIX_vm_shutdown(smx_host_t ind_vm, smx_process_t issuer)
313 const char *name = SIMIX_host_get_name(ind_vm);
315 if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_RUNNING)
316 THROWF(vm_error, 0, "VM(%s) is not running", name);
318 XBT_DEBUG("%d processes in the VM", xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
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);
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);
330 void SIMIX_pre_vm_shutdown(smx_simcall_t simcall, smx_host_t ind_vm)
332 SIMIX_vm_shutdown(ind_vm, simcall->issuer);
333 SIMIX_simcall_answer(simcall);
338 * \brief Function to destroy a SIMIX VM host.
340 * \param host the vm host to destroy (a smx_host_t)
342 void SIMIX_vm_destroy(smx_host_t ind_vm)
344 /* this code basically performs a similar thing like SIMIX_host_destroy() */
346 xbt_assert((ind_vm != NULL), "Invalid parameters");
347 const char *hostname = SIMIX_host_get_name(ind_vm);
349 smx_host_priv_t host_priv = SIMIX_host_priv(ind_vm);
351 /* this will call the registered callback function, i.e., SIMIX_host_destroy(). */
352 xbt_lib_unset(host_lib, hostname, SIMIX_HOST_LEVEL, 1);
354 /* jump to vm_ws_destroy(). The surf level resource will be freed. */
355 surf_vm_workstation_model->extension.vm_workstation.destroy(ind_vm);
358 void SIMIX_pre_vm_destroy(smx_simcall_t simcall, smx_host_t ind_vm)
360 SIMIX_vm_destroy(ind_vm);
361 SIMIX_simcall_answer(simcall);