1 /* Copyright (c) 2007-2015. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "smx_private.h"
8 #include "src/surf/virtual_machine.hpp"
9 #include "src/surf/HostImpl.hpp"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_vm, simix, "Logging specific to SIMIX (vms)");
13 /* **** create a VM **** */
16 * @brief Internal function to create a SIMIX host.
17 * @param name name of the host to create
18 * @param data some user data (may be nullptr)
20 sg_host_t SIMIX_vm_create(const char *name, sg_host_t ind_phys_host)
22 /* Create surf associated resource */
23 surf_vm_model_create(name, ind_phys_host);
24 sg_host_t host = sg_host_by_name(name);
25 SIMIX_host_create(host);
27 /* We will be able to register the VM to its physical host, so that we can promptly
28 * retrieve the list VMs on the physical host. */
34 /* works for VMs and PMs */
35 static long host_get_ramsize(sg_host_t vm, int *overcommit)
38 vm->extension<simgrid::surf::HostImpl>()->getParams(¶ms);
41 *overcommit = params.overcommit;
43 return params.ramsize;
46 /* **** start a VM **** */
47 static int __can_be_started(sg_host_t vm)
49 sg_host_t pm = surf_vm_get_pm(vm);
51 int pm_overcommit = 0;
52 long pm_ramsize = host_get_ramsize(pm, &pm_overcommit);
53 long vm_ramsize = host_get_ramsize(vm, nullptr);
56 /* We assume users do not want to care about ramsize. */
61 XBT_VERB("%s allows memory overcommit.", sg_host_get_name(pm));
65 long total_ramsize_of_vms = 0;
66 xbt_dynar_t dyn_vms = pm->extension<simgrid::surf::HostImpl>()->getVms();
68 unsigned int cursor = 0;
70 xbt_dynar_foreach(dyn_vms, cursor, another_vm) {
71 long another_vm_ramsize = host_get_ramsize(vm, nullptr);
72 total_ramsize_of_vms += another_vm_ramsize;
76 if (vm_ramsize > pm_ramsize - total_ramsize_of_vms) {
77 XBT_WARN("cannnot start %s@%s due to memory shortage: vm_ramsize %ld, free %ld, pm_ramsize %ld (bytes).",
78 sg_host_get_name(vm), sg_host_get_name(pm),
79 vm_ramsize, pm_ramsize - total_ramsize_of_vms, pm_ramsize);
80 xbt_dynar_free(&dyn_vms);
87 void SIMIX_vm_start(sg_host_t ind_vm)
89 if (__can_be_started(ind_vm))
90 static_cast<simgrid::surf::VirtualMachine*>(
91 ind_vm->extension<simgrid::surf::HostImpl>()
92 )->setState(SURF_VM_STATE_RUNNING);
94 THROWF(vm_error, 0, "The VM %s cannot be started", sg_host_get_name(ind_vm));
98 e_surf_vm_state_t SIMIX_vm_get_state(sg_host_t ind_vm)
100 return static_cast<simgrid::surf::VirtualMachine*>(
101 ind_vm->extension<simgrid::surf::HostImpl>()
106 * @brief Function to migrate a SIMIX VM host.
108 * @param host the vm host to migrate (a sg_host_t)
110 void SIMIX_vm_migrate(sg_host_t ind_vm, sg_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_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(sg_host_t vm, sg_host_t src_pm, sg_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 sg_host_t)
150 void *SIMIX_vm_get_pm(sg_host_t host)
152 return surf_vm_get_pm(host);
156 * @brief Function to set the CPU bound of the given SIMIX VM host.
158 * @param host the vm host (a sg_host_t)
159 * @param bound bound (a double)
161 void SIMIX_vm_set_bound(sg_host_t host, double bound)
163 surf_vm_set_bound(host, bound);
167 * @brief Function to set the CPU affinity of the given SIMIX VM host.
169 * @param host the vm host (a sg_host_t)
170 * @param host the pm host (a sg_host_t)
171 * @param mask affinity mask (a unsigned long)
173 void SIMIX_vm_set_affinity(sg_host_t ind_vm, sg_host_t ind_pm, unsigned long mask)
175 /* make sure this at the MSG layer. */
176 xbt_assert(SIMIX_vm_get_pm(ind_vm) == ind_pm);
178 surf_vm_set_affinity(ind_vm, ind_pm, mask);
183 * @brief Function to suspend a SIMIX VM host. This function stops the execution of the
184 * VM. All the processes on this VM will pause. The state of the VM is
185 * preserved on memory. We can later resume it again.
187 * @param host the vm host to suspend (a sg_host_t)
189 void SIMIX_vm_suspend(sg_host_t ind_vm, smx_process_t issuer)
191 const char *name = sg_host_get_name(ind_vm);
193 if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_RUNNING)
194 THROWF(vm_error, 0, "VM(%s) is not running", name);
196 XBT_DEBUG("suspend VM(%s), where %d processes exist", name, xbt_swag_size(sg_host_simix(ind_vm)->process_list));
198 /* jump to vm_ws_suspend. The state will be set. */
199 surf_vm_suspend(ind_vm);
201 smx_process_t smx_process, smx_process_safe;
202 xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(ind_vm)->process_list) {
203 XBT_DEBUG("suspend %s", smx_process->name.c_str());
204 SIMIX_process_suspend(smx_process, issuer);
207 XBT_DEBUG("suspend all processes on the VM done done");
210 void simcall_HANDLER_vm_suspend(smx_simcall_t simcall, sg_host_t ind_vm)
212 if (simcall->issuer->host == ind_vm) {
213 XBT_ERROR("cannot suspend the VM where I run");
217 SIMIX_vm_suspend(ind_vm, simcall->issuer);
219 XBT_DEBUG("simcall_HANDLER_vm_suspend done");
224 * @brief Function to resume a SIMIX VM host. This function restart the execution of the
225 * VM. All the processes on this VM will run again.
227 * @param host the vm host to resume (a sg_host_t)
229 void SIMIX_vm_resume(sg_host_t ind_vm, smx_process_t issuer)
231 const char *name = sg_host_get_name(ind_vm);
233 if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_SUSPENDED)
234 THROWF(vm_error, 0, "VM(%s) was not suspended", name);
236 XBT_DEBUG("resume VM(%s), where %d processes exist", name, xbt_swag_size(sg_host_simix(ind_vm)->process_list));
238 /* jump to vm_ws_resume() */
239 surf_vm_resume(ind_vm);
241 smx_process_t smx_process, smx_process_safe;
242 xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(ind_vm)->process_list) {
243 XBT_DEBUG("resume %s", smx_process->name.c_str());
244 SIMIX_process_resume(smx_process, issuer);
248 void simcall_HANDLER_vm_resume(smx_simcall_t simcall, sg_host_t ind_vm)
250 SIMIX_vm_resume(ind_vm, simcall->issuer);
255 * @brief Function to save a SIMIX VM host.
256 * This function is the same as vm_suspend, but the state of the VM is saved to the disk, and not preserved on memory.
257 * We can later restore it again.
259 * @param host the vm host to save (a sg_host_t)
261 void SIMIX_vm_save(sg_host_t ind_vm, smx_process_t issuer)
263 const char *name = sg_host_get_name(ind_vm);
265 if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_RUNNING)
266 THROWF(vm_error, 0, "VM(%s) is not running", name);
269 XBT_DEBUG("save VM(%s), where %d processes exist", name, xbt_swag_size(sg_host_simix(ind_vm)->process_list));
271 /* jump to vm_ws_save() */
272 surf_vm_save(ind_vm);
274 smx_process_t smx_process, smx_process_safe;
275 xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(ind_vm)->process_list) {
276 XBT_DEBUG("suspend %s", smx_process->name.c_str());
277 SIMIX_process_suspend(smx_process, issuer);
281 void simcall_HANDLER_vm_save(smx_simcall_t simcall, sg_host_t ind_vm)
283 SIMIX_vm_save(ind_vm, simcall->issuer);
288 * @brief Function to restore a SIMIX VM host. This function restart the execution of the
289 * VM. All the processes on this VM will run again.
291 * @param host the vm host to restore (a sg_host_t)
293 void SIMIX_vm_restore(sg_host_t ind_vm, smx_process_t issuer)
295 const char *name = sg_host_get_name(ind_vm);
297 if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_SAVED)
298 THROWF(vm_error, 0, "VM(%s) was not saved", name);
300 XBT_DEBUG("restore VM(%s), where %d processes exist", name, xbt_swag_size(sg_host_simix(ind_vm)->process_list));
302 /* jump to vm_ws_restore() */
303 surf_vm_resume(ind_vm);
305 smx_process_t smx_process, smx_process_safe;
306 xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(ind_vm)->process_list) {
307 XBT_DEBUG("resume %s", smx_process->name.c_str());
308 SIMIX_process_resume(smx_process, issuer);
312 void simcall_HANDLER_vm_restore(smx_simcall_t simcall, sg_host_t ind_vm)
314 SIMIX_vm_restore(ind_vm, simcall->issuer);
319 * @brief Function to shutdown a SIMIX VM host. This function powers off the
320 * VM. All the processes on this VM will be killed. But, the state of the VM is
321 * preserved on memory. We can later start it again.
323 * @param host the vm host to shutdown (a sg_host_t)
325 void SIMIX_vm_shutdown(sg_host_t ind_vm, smx_process_t issuer)
327 const char *name = sg_host_get_name(ind_vm);
329 if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_RUNNING)
330 THROWF(vm_error, 0, "VM(%s) is not running", name);
332 XBT_DEBUG("shutdown %s", name);
333 XBT_DEBUG("%d processes in the VM", xbt_swag_size(sg_host_simix(ind_vm)->process_list));
335 smx_process_t smx_process, smx_process_safe;
336 xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(ind_vm)->process_list) {
337 XBT_DEBUG("kill %s", smx_process->name.c_str());
338 SIMIX_process_kill(smx_process, issuer);
341 /* FIXME: we may have to do something at the surf layer, e.g., vcpu action */
342 static_cast<simgrid::surf::VirtualMachine*>(
343 ind_vm->extension<simgrid::surf::HostImpl>()
344 )->setState(SURF_VM_STATE_CREATED);
347 void simcall_HANDLER_vm_shutdown(smx_simcall_t simcall, sg_host_t ind_vm)
349 SIMIX_vm_shutdown(ind_vm, simcall->issuer);
354 * @brief Function to destroy a SIMIX VM host.
356 * @param vm the vm host to destroy (a sg_host_t)
358 void SIMIX_vm_destroy(sg_host_t vm)
360 /* this code basically performs a similar thing like SIMIX_host_destroy() */
362 const char *hostname = sg_host_get_name(vm);
364 XBT_DEBUG("destroy %s", hostname);
366 /* this will call the registered callback function, i.e., SIMIX_host_destroy(). */
367 sg_host_simix_destroy(vm);
369 /* jump to vm_ws_destroy(). The surf level resource will be freed. */