Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
avoid using C exceptions as valgrind don't like them too much
[simgrid.git] / src / simix / smx_vm.cpp
1 /* Copyright (c) 2007-2015. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "mc/mc.h"
7 #include "simgrid/s4u/VirtualMachine.hpp"
8 #include "smx_private.h"
9 #include "src/plugins/vm/VirtualMachineImpl.hpp"
10 #include "src/surf/HostImpl.hpp"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_vm, simix, "Logging specific to SIMIX Virtual Machines");
13
14 /* works for VMs and PMs */
15 static long host_get_ramsize(sg_host_t vm, int *overcommit)
16 {
17   s_vm_params_t params;
18   static_cast<simgrid::s4u::VirtualMachine*>(vm)->parameters(&params);
19
20   if (overcommit)
21     *overcommit = params.overcommit;
22
23   return params.ramsize;
24 }
25
26 /* **** start a VM **** */
27 static int __can_be_started(sg_host_t vm)
28 {
29   sg_host_t pm = static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->getPm();
30
31   int pm_overcommit = 0;
32   long pm_ramsize = host_get_ramsize(pm, &pm_overcommit);
33   long vm_ramsize = host_get_ramsize(vm, nullptr);
34
35   if (!pm_ramsize) {
36     /* We assume users do not want to care about ramsize. */
37     return 1;
38   }
39
40   if (pm_overcommit) {
41     XBT_VERB("%s allows memory overcommit.", sg_host_get_name(pm));
42     return 1;
43   }
44
45   long total_ramsize_of_vms = 0;
46   xbt_dynar_t dyn_vms       = pm->pimpl_->getVms();
47   {
48     unsigned int cursor = 0;
49     sg_host_t another_vm;
50     xbt_dynar_foreach(dyn_vms, cursor, another_vm) {
51       long another_vm_ramsize = host_get_ramsize(vm, nullptr);
52       total_ramsize_of_vms += another_vm_ramsize;
53     }
54   }
55
56   if (vm_ramsize > pm_ramsize - total_ramsize_of_vms) {
57     XBT_WARN("cannnot start %s@%s due to memory shortage: vm_ramsize %ld, free %ld, pm_ramsize %ld (bytes).",
58         sg_host_get_name(vm), sg_host_get_name(pm),
59         vm_ramsize, pm_ramsize - total_ramsize_of_vms, pm_ramsize);
60     xbt_dynar_free(&dyn_vms);
61     return 0;
62   }
63
64   return 1;
65 }
66
67 void SIMIX_vm_start(sg_host_t vm)
68 {
69   if (__can_be_started(vm))
70     static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->setState(SURF_VM_STATE_RUNNING);
71   else
72     THROWF(vm_error, 0, "The VM %s cannot be started", vm->name().c_str());
73 }
74
75 /**
76  * @brief Function to migrate a SIMIX VM host.
77  *
78  * @param host the vm host to migrate (a sg_host_t)
79  */
80 void SIMIX_vm_migrate(sg_host_t vm, sg_host_t dst_pm)
81 {
82   /* precopy migration makes the VM temporally paused */
83   xbt_assert(static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->getState() == SURF_VM_STATE_SUSPENDED);
84
85   /* jump to vm_ws_xigrate(). this will update the vm location. */
86   static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->migrate(dst_pm);
87 }
88
89 /**
90  * @brief Function to suspend a SIMIX VM host. This function stops the execution of the
91  * VM. All the processes on this VM will pause. The state of the VM is
92  * preserved on memory. We can later resume it again.
93  *
94  * @param vm the vm host to suspend (a sg_host_t)
95  */
96 void SIMIX_vm_suspend(sg_host_t vm, smx_actor_t issuer)
97 {
98   if (static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->getState() != SURF_VM_STATE_RUNNING)
99     THROWF(vm_error, 0, "VM(%s) is not running", vm->name().c_str());
100
101   XBT_DEBUG("suspend VM(%s), where %d processes exist", vm->name().c_str(), xbt_swag_size(sg_host_simix(vm)->process_list));
102
103   /* jump to vm_ws_suspend. The state will be set. */
104   static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->suspend();
105
106   smx_actor_t smx_process, smx_process_safe;
107   xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(vm)->process_list) {
108     XBT_DEBUG("suspend %s", smx_process->name.c_str());
109     SIMIX_process_suspend(smx_process, issuer);
110   }
111
112   XBT_DEBUG("suspend all processes on the VM done done");
113 }
114
115 void simcall_HANDLER_vm_suspend(smx_simcall_t simcall, sg_host_t vm)
116 {
117   xbt_assert(simcall->issuer->host != vm, "cannot suspend the VM where I run");
118
119   SIMIX_vm_suspend(vm, simcall->issuer);
120
121   XBT_DEBUG("simcall_HANDLER_vm_suspend done");
122 }
123
124
125 /**
126  * @brief Function to resume a SIMIX VM host. This function restart the execution of the
127  * VM. All the processes on this VM will run again.
128  *
129  * @param vm the vm host to resume (a sg_host_t)
130  */
131 void SIMIX_vm_resume(sg_host_t vm)
132 {
133   if (static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->getState() != SURF_VM_STATE_SUSPENDED)
134     THROWF(vm_error, 0, "VM(%s) was not suspended", vm->name().c_str());
135
136   XBT_DEBUG("resume VM(%s), where %d processes exist",
137       vm->name().c_str(), xbt_swag_size(sg_host_simix(vm)->process_list));
138
139   /* jump to vm_ws_resume() */
140   static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->resume();
141
142   smx_actor_t smx_process, smx_process_safe;
143   xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(vm)->process_list) {
144     XBT_DEBUG("resume %s", smx_process->name.c_str());
145     SIMIX_process_resume(smx_process);
146   }
147 }
148
149 /**
150  * @brief Function to save a SIMIX VM host.
151  * This function is the same as vm_suspend, but the state of the VM is saved to the disk, and not preserved on memory.
152  * We can later restore it again.
153  *
154  * @param vm the vm host to save (a sg_host_t)
155  */
156 void SIMIX_vm_save(sg_host_t vm, smx_actor_t issuer)
157 {
158   const char *name = sg_host_get_name(vm);
159
160   if (static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->getState() != SURF_VM_STATE_RUNNING)
161     THROWF(vm_error, 0, "VM(%s) is not running", name);
162
163   XBT_DEBUG("save VM(%s), where %d processes exist", name, xbt_swag_size(sg_host_simix(vm)->process_list));
164
165   /* jump to vm_ws_save() */
166   static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->save();
167
168   smx_actor_t smx_process, smx_process_safe;
169   xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(vm)->process_list) {
170     XBT_DEBUG("suspend %s", smx_process->name.c_str());
171     SIMIX_process_suspend(smx_process, issuer);
172   }
173 }
174
175 void simcall_HANDLER_vm_save(smx_simcall_t simcall, sg_host_t vm)
176 {
177   SIMIX_vm_save(vm, simcall->issuer);
178 }
179
180 /**
181  * @brief Function to shutdown a SIMIX VM host. This function powers off the
182  * VM. All the processes on this VM will be killed. But, the state of the VM is
183  * preserved on memory. We can later start it again.
184  *
185  * @param vm the VM to shutdown (a sg_host_t)
186  */
187 void SIMIX_vm_shutdown(sg_host_t vm, smx_actor_t issuer)
188 {
189   if (static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->getState() != SURF_VM_STATE_RUNNING)
190     THROWF(vm_error, 0, "VM(%s) is not running", vm->name().c_str());
191
192   XBT_DEBUG("shutdown VM %s, that contains %d processes",
193       vm->name().c_str(),xbt_swag_size(sg_host_simix(vm)->process_list));
194
195   smx_actor_t smx_process, smx_process_safe;
196   xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(vm)->process_list) {
197     XBT_DEBUG("kill %s", smx_process->name.c_str());
198     SIMIX_process_kill(smx_process, issuer);
199   }
200
201   /* FIXME: we may have to do something at the surf layer, e.g., vcpu action */
202   static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->setState(SURF_VM_STATE_CREATED);
203 }
204
205 void simcall_HANDLER_vm_shutdown(smx_simcall_t simcall, sg_host_t vm)
206 {
207   SIMIX_vm_shutdown(vm, simcall->issuer);
208 }