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)
25 /* Create surf associated resource */
26 surf_vm_workstation_model->extension.vm_workstation.create(name, ind_phys_host);
28 smx_host_t smx_host = SIMIX_host_create(name, ind_phys_host, NULL);
30 /* We will be able to register the VM to its physical host, so that we can promptly
31 * retrieve the list VMs on the physical host. */
37 smx_host_t SIMIX_pre_vm_create(smx_simcall_t simcall, const char *name, smx_host_t ind_phys_host)
39 return SIMIX_vm_create(name, ind_phys_host);
43 /* works for VMs and PMs */
44 static long host_get_ramsize(smx_host_t vm, int *overcommit)
47 surf_workstation_model->extension.workstation.get_params(vm, ¶ms);
50 *overcommit = params.overcommit;
52 return params.ramsize;
55 /* **** start a VM **** */
56 static int __can_be_started(smx_host_t vm)
58 smx_host_t pm = surf_vm_workstation_model->extension.vm_workstation.get_pm(vm);
60 int pm_overcommit = 0;
61 long pm_ramsize = host_get_ramsize(pm, &pm_overcommit);
62 long vm_ramsize = host_get_ramsize(vm, NULL);
65 /* We assume users do not want to care about ramsize. */
70 XBT_INFO("%s allows memory overcommit.", pm->key);
74 long total_ramsize_of_vms = 0;
75 xbt_dynar_t dyn_vms = surf_workstation_model->extension.workstation.get_vms(pm);
78 smx_host_t another_vm;
79 xbt_dynar_foreach(dyn_vms, cursor, another_vm) {
80 long another_vm_ramsize = host_get_ramsize(vm, NULL);
81 total_ramsize_of_vms += another_vm_ramsize;
85 if (vm_ramsize > pm_ramsize - total_ramsize_of_vms) {
86 XBT_WARN("cannnot start %s@%s due to memory shortage: vm_ramsize %ld, free %ld, pm_ramsize %ld (bytes).",
87 vm->key, pm->key, vm_ramsize, pm_ramsize - total_ramsize_of_vms, pm_ramsize);
88 xbt_dynar_free(&dyn_vms);
92 xbt_dynar_free(&dyn_vms);
96 void SIMIX_vm_start(smx_host_t ind_vm)
98 if (__can_be_started(ind_vm))
99 SIMIX_vm_set_state(ind_vm, SURF_VM_STATE_RUNNING);
101 THROWF(vm_error, 0, "The VM %s cannot be started", SIMIX_host_get_name(ind_vm));
106 void SIMIX_pre_vm_start(smx_simcall_t simcall, smx_host_t ind_vm)
108 SIMIX_vm_start(ind_vm);
109 SIMIX_simcall_answer(simcall);
112 /* ***** set/get state of a VM ***** */
113 void SIMIX_vm_set_state(smx_host_t ind_vm, int state)
115 /* jump to vm_ws_set_state */
116 surf_vm_workstation_model->extension.vm_workstation.set_state(ind_vm, state);
119 void SIMIX_pre_vm_set_state(smx_simcall_t simcall, smx_host_t ind_vm, int state)
121 SIMIX_vm_set_state(ind_vm, state);
124 int SIMIX_vm_get_state(smx_host_t ind_vm)
126 return surf_vm_workstation_model->extension.vm_workstation.get_state(ind_vm);
129 int SIMIX_pre_vm_get_state(smx_simcall_t simcall, smx_host_t ind_vm)
131 return SIMIX_vm_get_state(ind_vm);
136 * \brief Function to migrate a SIMIX VM host.
138 * \param host the vm host to migrate (a smx_host_t)
140 void SIMIX_vm_migrate(smx_host_t ind_vm, smx_host_t ind_dst_pm)
142 const char *name = SIMIX_host_get_name(ind_vm);
144 /* precopy migration makes the VM temporally paused */
145 e_surf_vm_state_t state = SIMIX_vm_get_state(ind_vm);
146 xbt_assert(state == SURF_VM_STATE_SUSPENDED);
148 /* jump to vm_ws_migrate(). this will update the vm location. */
149 surf_vm_workstation_model->extension.vm_workstation.migrate(ind_vm, ind_dst_pm);
152 void SIMIX_pre_vm_migrate(smx_simcall_t simcall, smx_host_t ind_vm, smx_host_t ind_dst_pm)
154 SIMIX_vm_migrate(ind_vm, ind_dst_pm);
155 SIMIX_simcall_answer(simcall);
160 * \brief Function to get the physical host of the given the SIMIX VM host.
162 * \param host the vm host to get_phys_host (a smx_host_t)
164 void *SIMIX_vm_get_pm(smx_host_t ind_vm)
166 /* jump to vm_ws_get_pm(). this will return the vm name. */
167 return surf_vm_workstation_model->extension.vm_workstation.get_pm(ind_vm);
170 void *SIMIX_pre_vm_get_pm(smx_simcall_t simcall, smx_host_t ind_vm)
172 return SIMIX_vm_get_pm(ind_vm);
177 * \brief Function to suspend a SIMIX VM host. This function stops the exection of the
178 * VM. All the processes on this VM will pause. The state of the VM is
179 * preserved on memory. We can later resume it again.
181 * \param host the vm host to suspend (a smx_host_t)
183 void SIMIX_vm_suspend(smx_host_t ind_vm, smx_process_t issuer)
185 const char *name = SIMIX_host_get_name(ind_vm);
187 if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_RUNNING)
188 THROWF(vm_error, 0, "VM(%s) is not running", name);
190 XBT_DEBUG("suspend VM(%s), where %d processes exist", name, xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
192 /* jump to vm_ws_suspend. The state will be set. */
193 surf_vm_workstation_model->extension.vm_workstation.suspend(ind_vm);
195 smx_process_t smx_process, smx_process_safe;
196 xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(ind_vm)->process_list) {
197 XBT_DEBUG("suspend %s", smx_process->name);
198 SIMIX_process_suspend(smx_process, issuer);
201 XBT_DEBUG("suspend all processes on the VM done done");
204 void SIMIX_pre_vm_suspend(smx_simcall_t simcall, smx_host_t ind_vm)
206 if (simcall->issuer->smx_host == ind_vm) {
207 XBT_ERROR("cannot suspend the VM where I run");
211 SIMIX_vm_suspend(ind_vm, simcall->issuer);
213 /* without this, simcall_vm_suspend() does not return to the userland. why? */
214 SIMIX_simcall_answer(simcall);
216 XBT_DEBUG("SIMIX_pre_vm_suspend done");
221 * \brief Function to resume a SIMIX VM host. This function restart the execution of the
222 * VM. All the processes on this VM will run again.
224 * \param host the vm host to resume (a smx_host_t)
226 void SIMIX_vm_resume(smx_host_t ind_vm, smx_process_t issuer)
228 const char *name = SIMIX_host_get_name(ind_vm);
230 if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_SUSPENDED)
231 THROWF(vm_error, 0, "VM(%s) was not suspended", name);
233 XBT_DEBUG("resume VM(%s), where %d processes exist", name, xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
235 /* jump to vm_ws_resume() */
236 surf_vm_workstation_model->extension.vm_workstation.resume(ind_vm);
238 smx_process_t smx_process, smx_process_safe;
239 xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(ind_vm)->process_list) {
240 XBT_DEBUG("resume %s", smx_process->name);
241 SIMIX_process_resume(smx_process, issuer);
245 void SIMIX_pre_vm_resume(smx_simcall_t simcall, smx_host_t ind_vm)
247 SIMIX_vm_resume(ind_vm, simcall->issuer);
248 SIMIX_simcall_answer(simcall);
253 * \brief Function to save a SIMIX VM host.
254 * This function is the same as vm_suspend, but the state of the VM is saved to the disk, and not preserved on memory.
255 * We can later restore it again.
257 * \param host the vm host to save (a smx_host_t)
259 void SIMIX_vm_save(smx_host_t ind_vm, smx_process_t issuer)
261 const char *name = SIMIX_host_get_name(ind_vm);
263 if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_RUNNING)
264 THROWF(vm_error, 0, "VM(%s) is not running", name);
267 XBT_DEBUG("save VM(%s), where %d processes exist", name, xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
269 /* jump to vm_ws_save() */
270 surf_vm_workstation_model->extension.vm_workstation.save(ind_vm);
272 smx_process_t smx_process, smx_process_safe;
273 xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(ind_vm)->process_list) {
274 XBT_DEBUG("suspend %s", smx_process->name);
275 SIMIX_process_suspend(smx_process, issuer);
279 void SIMIX_pre_vm_save(smx_simcall_t simcall, smx_host_t ind_vm)
281 SIMIX_vm_save(ind_vm, simcall->issuer);
283 /* without this, simcall_vm_suspend() does not return to the userland. why? */
284 SIMIX_simcall_answer(simcall);
289 * \brief Function to restore a SIMIX VM host. This function restart the execution of the
290 * VM. All the processes on this VM will run again.
292 * \param host the vm host to restore (a smx_host_t)
294 void SIMIX_vm_restore(smx_host_t ind_vm, smx_process_t issuer)
296 const char *name = SIMIX_host_get_name(ind_vm);
298 if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_SAVED)
299 THROWF(vm_error, 0, "VM(%s) was not saved", name);
301 XBT_DEBUG("restore VM(%s), where %d processes exist", name, xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
303 /* jump to vm_ws_restore() */
304 surf_vm_workstation_model->extension.vm_workstation.resume(ind_vm);
306 smx_process_t smx_process, smx_process_safe;
307 xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(ind_vm)->process_list) {
308 XBT_DEBUG("resume %s", smx_process->name);
309 SIMIX_process_resume(smx_process, issuer);
313 void SIMIX_pre_vm_restore(smx_simcall_t simcall, smx_host_t ind_vm)
315 SIMIX_vm_restore(ind_vm, simcall->issuer);
316 SIMIX_simcall_answer(simcall);
321 * \brief Function to shutdown a SIMIX VM host. This function powers off the
322 * VM. All the processes on this VM will be killed. But, the state of the VM is
323 * preserved on memory. We can later start it again.
325 * \param host the vm host to shutdown (a smx_host_t)
327 void SIMIX_vm_shutdown(smx_host_t ind_vm, smx_process_t issuer)
329 const char *name = SIMIX_host_get_name(ind_vm);
331 if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_RUNNING)
332 THROWF(vm_error, 0, "VM(%s) is not running", name);
334 XBT_DEBUG("%d processes in the VM", xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
336 smx_process_t smx_process, smx_process_safe;
337 xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(ind_vm)->process_list) {
338 XBT_DEBUG("shutdown %s", name);
339 SIMIX_process_kill(smx_process, issuer);
342 /* FIXME: we may have to do something at the surf layer, e.g., vcpu action */
343 SIMIX_vm_set_state(ind_vm, SURF_VM_STATE_CREATED);
346 void SIMIX_pre_vm_shutdown(smx_simcall_t simcall, smx_host_t ind_vm)
348 SIMIX_vm_shutdown(ind_vm, simcall->issuer);
349 SIMIX_simcall_answer(simcall);
354 * \brief Function to destroy a SIMIX VM host.
356 * \param host the vm host to destroy (a smx_host_t)
358 void SIMIX_vm_destroy(smx_host_t ind_vm)
360 /* this code basically performs a similar thing like SIMIX_host_destroy() */
362 xbt_assert((ind_vm != NULL), "Invalid parameters");
363 const char *hostname = SIMIX_host_get_name(ind_vm);
365 smx_host_priv_t host_priv = SIMIX_host_priv(ind_vm);
367 /* this will call the registered callback function, i.e., SIMIX_host_destroy(). */
368 xbt_lib_unset(host_lib, hostname, SIMIX_HOST_LEVEL, 1);
370 /* jump to vm_ws_destroy(). The surf level resource will be freed. */
371 surf_vm_workstation_model->extension.vm_workstation.destroy(ind_vm);
374 void SIMIX_pre_vm_destroy(smx_simcall_t simcall, smx_host_t ind_vm)
376 SIMIX_vm_destroy(ind_vm);
377 SIMIX_simcall_answer(simcall);