1 /* Copyright (c) 2007-2014. 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_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 /* works for VMs and PMs */
38 static long host_get_ramsize(smx_host_t vm, int *overcommit)
41 surf_workstation_get_params(vm, ¶ms);
44 *overcommit = params.overcommit;
46 return params.ramsize;
49 /* **** start a VM **** */
50 static int __can_be_started(smx_host_t vm)
52 smx_host_t pm = surf_vm_workstation_get_pm(vm);
54 int pm_overcommit = 0;
55 long pm_ramsize = host_get_ramsize(pm, &pm_overcommit);
56 long vm_ramsize = host_get_ramsize(vm, NULL);
59 /* We assume users do not want to care about ramsize. */
64 XBT_INFO("%s allows memory overcommit.", pm->key);
68 long total_ramsize_of_vms = 0;
69 xbt_dynar_t dyn_vms = surf_workstation_get_vms(pm);
71 unsigned int cursor = 0;
72 smx_host_t another_vm;
73 xbt_dynar_foreach(dyn_vms, cursor, another_vm) {
74 long another_vm_ramsize = host_get_ramsize(vm, NULL);
75 total_ramsize_of_vms += another_vm_ramsize;
79 if (vm_ramsize > pm_ramsize - total_ramsize_of_vms) {
80 XBT_WARN("cannnot start %s@%s due to memory shortage: vm_ramsize %ld, free %ld, pm_ramsize %ld (bytes).",
81 vm->key, pm->key, vm_ramsize, pm_ramsize - total_ramsize_of_vms, pm_ramsize);
82 xbt_dynar_free(&dyn_vms);
86 xbt_dynar_free(&dyn_vms);
90 void SIMIX_vm_start(smx_host_t ind_vm)
92 if (__can_be_started(ind_vm))
93 surf_resource_set_state(surf_workstation_resource_priv(ind_vm),
94 (int)SURF_VM_STATE_RUNNING);
96 THROWF(vm_error, 0, "The VM %s cannot be started", SIMIX_host_get_name(ind_vm));
100 int SIMIX_vm_get_state(smx_host_t ind_vm)
102 return surf_resource_get_state(surf_workstation_resource_priv(ind_vm));
106 * \brief Function to migrate a SIMIX VM host.
108 * \param host the vm host to migrate (a smx_host_t)
110 void SIMIX_vm_migrate(smx_host_t ind_vm, smx_host_t ind_dst_pm)
112 /* precopy migration makes the VM temporally paused */
113 xbt_assert(SIMIX_vm_get_state(ind_vm) == SURF_VM_STATE_SUSPENDED);
115 /* jump to vm_ws_xigrate(). this will update the vm location. */
116 surf_vm_workstation_migrate(ind_vm, ind_dst_pm);
120 * \brief Encompassing simcall to prevent the removal of the src or the dst node at the end of a VM migration
121 * The simcall actually invokes the following calls:
122 * simcall_vm_set_affinity(vm, src_pm, 0);
123 * simcall_vm_migrate(vm, dst_pm);
124 * simcall_vm_resume(vm);
126 * It is called at the end of the migration_rx_fun function from msg/msg_vm.c
128 * \param vm VM to migrate
129 * \param src_pm Source physical host
130 * \param dst_pmt Destination physical host
132 void SIMIX_vm_migratefrom_resumeto(smx_host_t vm, smx_host_t src_pm, smx_host_t dst_pm)
134 /* deinstall the current affinity setting for the CPU */
135 SIMIX_vm_set_affinity(vm, src_pm, 0);
137 /* Update the vm location */
138 SIMIX_vm_migrate(vm, dst_pm);
141 smx_process_t self = SIMIX_process_self();
142 SIMIX_vm_resume(vm, self->simcall.issuer);
146 * \brief Function to get the physical host of the given SIMIX VM host.
148 * \param host the vm host to get_phys_host (a smx_host_t)
150 void *SIMIX_vm_get_pm(smx_host_t ind_vm)
152 /* jump to vm_ws_get_pm(). this will return the vm name. */
153 return surf_vm_workstation_get_pm(ind_vm);
157 * \brief Function to set the CPU bound of the given SIMIX VM host.
159 * \param host the vm host (a smx_host_t)
160 * \param bound bound (a double)
162 void SIMIX_vm_set_bound(smx_host_t ind_vm, double bound)
164 /* jump to vm_ws_set_vm_bound(). */
165 surf_vm_workstation_set_bound(ind_vm, bound);
169 * \brief Function to set the CPU affinity of the given SIMIX VM host.
171 * \param host the vm host (a smx_host_t)
172 * \param host the pm host (a smx_host_t)
173 * \param mask affinity mask (a unsigned long)
175 void SIMIX_vm_set_affinity(smx_host_t ind_vm, smx_host_t ind_pm, unsigned long mask)
177 /* make sure this at the MSG layer. */
178 xbt_assert(SIMIX_vm_get_pm(ind_vm) == ind_pm);
180 /* jump to vm_ws_set_vm_affinity(). */
181 surf_vm_workstation_set_affinity(ind_vm, ind_pm, mask);
186 * \brief Function to suspend a SIMIX VM host. This function stops the execution of the
187 * VM. All the processes on this VM will pause. The state of the VM is
188 * preserved on memory. We can later resume it again.
190 * \param host the vm host to suspend (a smx_host_t)
192 void SIMIX_vm_suspend(smx_host_t ind_vm, smx_process_t issuer)
194 const char *name = SIMIX_host_get_name(ind_vm);
196 if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_RUNNING)
197 THROWF(vm_error, 0, "VM(%s) is not running", name);
199 XBT_DEBUG("suspend VM(%s), where %d processes exist", name, xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
201 /* jump to vm_ws_suspend. The state will be set. */
202 surf_vm_workstation_suspend(ind_vm);
204 smx_process_t smx_process, smx_process_safe;
205 xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(ind_vm)->process_list) {
206 XBT_DEBUG("suspend %s", smx_process->name);
207 SIMIX_process_suspend(smx_process, issuer);
210 XBT_DEBUG("suspend all processes on the VM done done");
213 void simcall_HANDLER_vm_suspend(smx_simcall_t simcall, smx_host_t ind_vm)
215 if (simcall->issuer->smx_host == ind_vm) {
216 XBT_ERROR("cannot suspend the VM where I run");
220 SIMIX_vm_suspend(ind_vm, simcall->issuer);
222 XBT_DEBUG("simcall_HANDLER_vm_suspend done");
227 * \brief Function to resume a SIMIX VM host. This function restart the execution of the
228 * VM. All the processes on this VM will run again.
230 * \param host the vm host to resume (a smx_host_t)
232 void SIMIX_vm_resume(smx_host_t ind_vm, smx_process_t issuer)
234 const char *name = SIMIX_host_get_name(ind_vm);
236 if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_SUSPENDED)
237 THROWF(vm_error, 0, "VM(%s) was not suspended", name);
239 XBT_DEBUG("resume VM(%s), where %d processes exist", name, xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
241 /* jump to vm_ws_resume() */
242 surf_vm_workstation_resume(ind_vm);
244 smx_process_t smx_process, smx_process_safe;
245 xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(ind_vm)->process_list) {
246 XBT_DEBUG("resume %s", smx_process->name);
247 SIMIX_process_resume(smx_process, issuer);
251 void simcall_HANDLER_vm_resume(smx_simcall_t simcall, smx_host_t ind_vm)
253 SIMIX_vm_resume(ind_vm, simcall->issuer);
258 * \brief Function to save a SIMIX VM host.
259 * This function is the same as vm_suspend, but the state of the VM is saved to the disk, and not preserved on memory.
260 * We can later restore it again.
262 * \param host the vm host to save (a smx_host_t)
264 void SIMIX_vm_save(smx_host_t ind_vm, smx_process_t issuer)
266 const char *name = SIMIX_host_get_name(ind_vm);
268 if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_RUNNING)
269 THROWF(vm_error, 0, "VM(%s) is not running", name);
272 XBT_DEBUG("save VM(%s), where %d processes exist", name, xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
274 /* jump to vm_ws_save() */
275 surf_vm_workstation_save(ind_vm);
277 smx_process_t smx_process, smx_process_safe;
278 xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(ind_vm)->process_list) {
279 XBT_DEBUG("suspend %s", smx_process->name);
280 SIMIX_process_suspend(smx_process, issuer);
284 void simcall_HANDLER_vm_save(smx_simcall_t simcall, smx_host_t ind_vm)
286 SIMIX_vm_save(ind_vm, simcall->issuer);
291 * \brief Function to restore a SIMIX VM host. This function restart the execution of the
292 * VM. All the processes on this VM will run again.
294 * \param host the vm host to restore (a smx_host_t)
296 void SIMIX_vm_restore(smx_host_t ind_vm, smx_process_t issuer)
298 const char *name = SIMIX_host_get_name(ind_vm);
300 if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_SAVED)
301 THROWF(vm_error, 0, "VM(%s) was not saved", name);
303 XBT_DEBUG("restore VM(%s), where %d processes exist", name, xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
305 /* jump to vm_ws_restore() */
306 surf_vm_workstation_resume(ind_vm);
308 smx_process_t smx_process, smx_process_safe;
309 xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(ind_vm)->process_list) {
310 XBT_DEBUG("resume %s", smx_process->name);
311 SIMIX_process_resume(smx_process, issuer);
315 void simcall_HANDLER_vm_restore(smx_simcall_t simcall, smx_host_t ind_vm)
317 SIMIX_vm_restore(ind_vm, simcall->issuer);
322 * \brief Function to shutdown a SIMIX VM host. This function powers off the
323 * VM. All the processes on this VM will be killed. But, the state of the VM is
324 * preserved on memory. We can later start it again.
326 * \param host the vm host to shutdown (a smx_host_t)
328 void SIMIX_vm_shutdown(smx_host_t ind_vm, smx_process_t issuer)
330 const char *name = SIMIX_host_get_name(ind_vm);
332 if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_RUNNING)
333 THROWF(vm_error, 0, "VM(%s) is not running", name);
335 XBT_DEBUG("shutdown %s", name);
336 XBT_DEBUG("%d processes in the VM", xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
338 smx_process_t smx_process, smx_process_safe;
339 xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(ind_vm)->process_list) {
340 XBT_DEBUG("kill %s", smx_process->name);
341 SIMIX_process_kill(smx_process, issuer);
344 /* FIXME: we may have to do something at the surf layer, e.g., vcpu action */
345 surf_resource_set_state(surf_workstation_resource_priv(ind_vm),
346 (int)SURF_VM_STATE_CREATED);
349 void simcall_HANDLER_vm_shutdown(smx_simcall_t simcall, smx_host_t ind_vm)
351 SIMIX_vm_shutdown(ind_vm, simcall->issuer);
356 * \brief Function to destroy a SIMIX VM host.
358 * \param host the vm host to destroy (a smx_host_t)
360 void SIMIX_vm_destroy(smx_host_t ind_vm)
362 /* this code basically performs a similar thing like SIMIX_host_destroy() */
364 xbt_assert((ind_vm != NULL), "Invalid parameters");
365 const char *hostname = SIMIX_host_get_name(ind_vm);
367 XBT_DEBUG("destroy %s", hostname);
369 /* this will call the registered callback function, i.e., SIMIX_host_destroy(). */
370 xbt_lib_unset(host_lib, hostname, SIMIX_HOST_LEVEL, 1);
372 /* jump to vm_ws_destroy(). The surf level resource will be freed. */
373 surf_vm_workstation_destroy(ind_vm);