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. */
7 #include "smx_private.h"
8 #include "src/surf/HostImpl.hpp"
9 #include "src/surf/VirtualMachineImpl.hpp"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_vm, simix, "Logging specific to SIMIX Virtual Machines");
13 /* works for VMs and PMs */
14 static long host_get_ramsize(sg_host_t vm, int *overcommit)
17 vm->pimpl_->getParams(¶ms);
20 *overcommit = params.overcommit;
22 return params.ramsize;
25 /* **** start a VM **** */
26 static int __can_be_started(sg_host_t vm)
28 sg_host_t pm = surf_vm_get_pm(vm);
30 int pm_overcommit = 0;
31 long pm_ramsize = host_get_ramsize(pm, &pm_overcommit);
32 long vm_ramsize = host_get_ramsize(vm, nullptr);
35 /* We assume users do not want to care about ramsize. */
40 XBT_VERB("%s allows memory overcommit.", sg_host_get_name(pm));
44 long total_ramsize_of_vms = 0;
45 xbt_dynar_t dyn_vms = pm->pimpl_->getVms();
47 unsigned int cursor = 0;
49 xbt_dynar_foreach(dyn_vms, cursor, another_vm) {
50 long another_vm_ramsize = host_get_ramsize(vm, nullptr);
51 total_ramsize_of_vms += another_vm_ramsize;
55 if (vm_ramsize > pm_ramsize - total_ramsize_of_vms) {
56 XBT_WARN("cannnot start %s@%s due to memory shortage: vm_ramsize %ld, free %ld, pm_ramsize %ld (bytes).",
57 sg_host_get_name(vm), sg_host_get_name(pm),
58 vm_ramsize, pm_ramsize - total_ramsize_of_vms, pm_ramsize);
59 xbt_dynar_free(&dyn_vms);
66 void SIMIX_vm_start(sg_host_t vm)
68 if (__can_be_started(vm))
69 static_cast<simgrid::surf::VirtualMachineImpl*>(vm->pimpl_)->setState(SURF_VM_STATE_RUNNING);
71 THROWF(vm_error, 0, "The VM %s cannot be started", vm->name().c_str());
75 e_surf_vm_state_t SIMIX_vm_get_state(sg_host_t vm)
77 return static_cast<simgrid::surf::VirtualMachineImpl*>(vm->pimpl_)->getState();
81 * @brief Function to migrate a SIMIX VM host.
83 * @param host the vm host to migrate (a sg_host_t)
85 void SIMIX_vm_migrate(sg_host_t vm, sg_host_t dst_pm)
87 /* precopy migration makes the VM temporally paused */
88 xbt_assert(SIMIX_vm_get_state(vm) == SURF_VM_STATE_SUSPENDED);
90 /* jump to vm_ws_xigrate(). this will update the vm location. */
91 surf_vm_migrate(vm, dst_pm);
95 * @brief Encompassing simcall to prevent the removal of the src or the dst node at the end of a VM migration
96 * The simcall actually invokes the following calls:
97 * simcall_vm_migrate(vm, dst_pm);
98 * simcall_vm_resume(vm);
100 * It is called at the end of the migration_rx_fun function from msg/msg_vm.c
102 * @param vm VM to migrate
103 * @param src_pm Source physical host
104 * @param dst_pmt Destination physical host
106 void SIMIX_vm_migratefrom_resumeto(sg_host_t vm, sg_host_t src_pm, sg_host_t dst_pm)
108 /* Update the vm location */
109 SIMIX_vm_migrate(vm, dst_pm);
112 smx_actor_t self = SIMIX_process_self();
113 SIMIX_vm_resume(vm, self);
117 * @brief Function to get the physical host of the given SIMIX VM host.
119 * @param host the vm host to get_phys_host (a sg_host_t)
121 void *SIMIX_vm_get_pm(sg_host_t host)
123 return surf_vm_get_pm(host);
127 * @brief Function to set the CPU bound of the given SIMIX VM host.
129 * @param host the vm host (a sg_host_t)
130 * @param bound bound (a double)
132 void SIMIX_vm_set_bound(sg_host_t vm, double bound)
134 surf_vm_set_bound(vm, bound);
138 * @brief Function to suspend a SIMIX VM host. This function stops the execution of the
139 * VM. All the processes on this VM will pause. The state of the VM is
140 * preserved on memory. We can later resume it again.
142 * @param vm the vm host to suspend (a sg_host_t)
144 void SIMIX_vm_suspend(sg_host_t vm, smx_actor_t issuer)
146 if (SIMIX_vm_get_state(vm) != SURF_VM_STATE_RUNNING)
147 THROWF(vm_error, 0, "VM(%s) is not running", vm->name().c_str());
149 XBT_DEBUG("suspend VM(%s), where %d processes exist", vm->name().c_str(), xbt_swag_size(sg_host_simix(vm)->process_list));
151 /* jump to vm_ws_suspend. The state will be set. */
154 smx_actor_t smx_process, smx_process_safe;
155 xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(vm)->process_list) {
156 XBT_DEBUG("suspend %s", smx_process->name.c_str());
157 SIMIX_process_suspend(smx_process, issuer);
160 XBT_DEBUG("suspend all processes on the VM done done");
163 void simcall_HANDLER_vm_suspend(smx_simcall_t simcall, sg_host_t vm)
165 xbt_assert(simcall->issuer->host != vm, "cannot suspend the VM where I run");
167 SIMIX_vm_suspend(vm, simcall->issuer);
169 XBT_DEBUG("simcall_HANDLER_vm_suspend done");
174 * @brief Function to resume a SIMIX VM host. This function restart the execution of the
175 * VM. All the processes on this VM will run again.
177 * @param vm the vm host to resume (a sg_host_t)
179 void SIMIX_vm_resume(sg_host_t vm, smx_actor_t issuer)
181 if (SIMIX_vm_get_state(vm) != SURF_VM_STATE_SUSPENDED)
182 THROWF(vm_error, 0, "VM(%s) was not suspended", vm->name().c_str());
184 XBT_DEBUG("resume VM(%s), where %d processes exist",
185 vm->name().c_str(), xbt_swag_size(sg_host_simix(vm)->process_list));
187 /* jump to vm_ws_resume() */
190 smx_actor_t smx_process, smx_process_safe;
191 xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(vm)->process_list) {
192 XBT_DEBUG("resume %s", smx_process->name.c_str());
193 SIMIX_process_resume(smx_process, issuer);
197 void simcall_HANDLER_vm_resume(smx_simcall_t simcall, sg_host_t vm)
199 SIMIX_vm_resume(vm, simcall->issuer);
204 * @brief Function to save a SIMIX VM host.
205 * This function is the same as vm_suspend, but the state of the VM is saved to the disk, and not preserved on memory.
206 * We can later restore it again.
208 * @param vm the vm host to save (a sg_host_t)
210 void SIMIX_vm_save(sg_host_t vm, smx_actor_t issuer)
212 const char *name = sg_host_get_name(vm);
214 if (SIMIX_vm_get_state(vm) != SURF_VM_STATE_RUNNING)
215 THROWF(vm_error, 0, "VM(%s) is not running", name);
217 XBT_DEBUG("save VM(%s), where %d processes exist", name, xbt_swag_size(sg_host_simix(vm)->process_list));
219 /* jump to vm_ws_save() */
222 smx_actor_t smx_process, smx_process_safe;
223 xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(vm)->process_list) {
224 XBT_DEBUG("suspend %s", smx_process->name.c_str());
225 SIMIX_process_suspend(smx_process, issuer);
229 void simcall_HANDLER_vm_save(smx_simcall_t simcall, sg_host_t vm)
231 SIMIX_vm_save(vm, simcall->issuer);
236 * @brief Function to restore a SIMIX VM host. This function restart the execution of the
237 * VM. All the processes on this VM will run again.
239 * @param vm the vm host to restore (a sg_host_t)
241 void SIMIX_vm_restore(sg_host_t vm, smx_actor_t issuer)
243 if (SIMIX_vm_get_state(vm) != SURF_VM_STATE_SAVED)
244 THROWF(vm_error, 0, "VM(%s) was not saved", vm->name().c_str());
246 XBT_DEBUG("restore VM(%s), where %d processes exist",
247 vm->name().c_str(), xbt_swag_size(sg_host_simix(vm)->process_list));
249 /* jump to vm_ws_restore() */
252 smx_actor_t smx_process, smx_process_safe;
253 xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(vm)->process_list) {
254 XBT_DEBUG("resume %s", smx_process->name.c_str());
255 SIMIX_process_resume(smx_process, issuer);
259 void simcall_HANDLER_vm_restore(smx_simcall_t simcall, sg_host_t vm)
261 SIMIX_vm_restore(vm, simcall->issuer);
266 * @brief Function to shutdown a SIMIX VM host. This function powers off the
267 * VM. All the processes on this VM will be killed. But, the state of the VM is
268 * preserved on memory. We can later start it again.
270 * @param vm the VM to shutdown (a sg_host_t)
272 void SIMIX_vm_shutdown(sg_host_t vm, smx_actor_t issuer)
274 if (SIMIX_vm_get_state(vm) != SURF_VM_STATE_RUNNING)
275 THROWF(vm_error, 0, "VM(%s) is not running", vm->name().c_str());
277 XBT_DEBUG("shutdown VM %s, that contains %d processes",
278 vm->name().c_str(),xbt_swag_size(sg_host_simix(vm)->process_list));
280 smx_actor_t smx_process, smx_process_safe;
281 xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(vm)->process_list) {
282 XBT_DEBUG("kill %s", smx_process->name.c_str());
283 SIMIX_process_kill(smx_process, issuer);
286 /* FIXME: we may have to do something at the surf layer, e.g., vcpu action */
287 static_cast<simgrid::surf::VirtualMachineImpl*>(vm->pimpl_)->setState(SURF_VM_STATE_CREATED);
290 void simcall_HANDLER_vm_shutdown(smx_simcall_t simcall, sg_host_t vm)
292 SIMIX_vm_shutdown(vm, simcall->issuer);
296 * @brief Function to destroy a SIMIX VM host.
298 * @param vm the vm host to destroy (a sg_host_t)
300 void SIMIX_vm_destroy(sg_host_t vm)
302 /* this code basically performs a similar thing like SIMIX_host_destroy() */
303 XBT_DEBUG("destroy %s", vm->name().c_str());
305 /* FIXME: this is really strange that everything fails if the next line is removed.
306 * This is as if we shared these data with the PM, which definitely should not be the case...
308 * We need to test that suspending a VM does not suspends the processes running on its PM, for example.
309 * Or we need to simplify this code enough to make it actually readable (but this sounds harder than testing)
311 vm->extension_set<simgrid::simix::Host>(nullptr);
313 /* Don't free these things twice: they are the ones of my physical host */
314 vm->pimpl_cpu = nullptr;
315 vm->pimpl_netcard = nullptr;