Logo AND Algorithmique Numérique Distribuée

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