Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill 2 dumb functions in simix_vm
[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::surf::VirtualMachineImpl*>(vm->pimpl_)->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 e_surf_vm_state_t SIMIX_vm_get_state(sg_host_t vm)
77 {
78   return static_cast<simgrid::surf::VirtualMachineImpl*>(vm->pimpl_)->getState();
79 }
80
81 /**
82  * @brief Function to migrate a SIMIX VM host.
83  *
84  * @param host the vm host to migrate (a sg_host_t)
85  */
86 void SIMIX_vm_migrate(sg_host_t vm, sg_host_t dst_pm)
87 {
88   /* precopy migration makes the VM temporally paused */
89   xbt_assert(SIMIX_vm_get_state(vm) == SURF_VM_STATE_SUSPENDED);
90
91   /* jump to vm_ws_xigrate(). this will update the vm location. */
92   static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->migrate(dst_pm);
93 }
94
95 /**
96  * @brief Encompassing simcall to prevent the removal of the src or the dst node at the end of a VM migration
97  *  The simcall actually invokes the following calls: 
98  *     simcall_vm_migrate(vm, dst_pm); 
99  *     simcall_vm_resume(vm);
100  *
101  * It is called at the end of the migration_rx_fun function from msg/msg_vm.c
102  *
103  * @param vm VM to migrate
104  * @param src_pm  Source physical host
105  * @param dst_pmt Destination physical host
106  */
107 void SIMIX_vm_migratefrom_resumeto(sg_host_t vm, sg_host_t src_pm, sg_host_t dst_pm)
108 {
109   /* Update the vm location */
110   SIMIX_vm_migrate(vm, dst_pm);
111  
112   /* Resume the VM */
113   smx_actor_t self = SIMIX_process_self(); 
114   SIMIX_vm_resume(vm, self);
115
116
117 /**
118  * @brief Function to suspend a SIMIX VM host. This function stops the execution of the
119  * VM. All the processes on this VM will pause. The state of the VM is
120  * preserved on memory. We can later resume it again.
121  *
122  * @param vm the vm host to suspend (a sg_host_t)
123  */
124 void SIMIX_vm_suspend(sg_host_t vm, smx_actor_t issuer)
125 {
126   if (SIMIX_vm_get_state(vm) != SURF_VM_STATE_RUNNING)
127     THROWF(vm_error, 0, "VM(%s) is not running", vm->name().c_str());
128
129   XBT_DEBUG("suspend VM(%s), where %d processes exist", vm->name().c_str(), xbt_swag_size(sg_host_simix(vm)->process_list));
130
131   /* jump to vm_ws_suspend. The state will be set. */
132   static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->suspend();
133
134   smx_actor_t smx_process, smx_process_safe;
135   xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(vm)->process_list) {
136     XBT_DEBUG("suspend %s", smx_process->name.c_str());
137     SIMIX_process_suspend(smx_process, issuer);
138   }
139
140   XBT_DEBUG("suspend all processes on the VM done done");
141 }
142
143 void simcall_HANDLER_vm_suspend(smx_simcall_t simcall, sg_host_t vm)
144 {
145   xbt_assert(simcall->issuer->host != vm, "cannot suspend the VM where I run");
146
147   SIMIX_vm_suspend(vm, simcall->issuer);
148
149   XBT_DEBUG("simcall_HANDLER_vm_suspend done");
150 }
151
152
153 /**
154  * @brief Function to resume a SIMIX VM host. This function restart the execution of the
155  * VM. All the processes on this VM will run again.
156  *
157  * @param vm the vm host to resume (a sg_host_t)
158  */
159 void SIMIX_vm_resume(sg_host_t vm, smx_actor_t issuer)
160 {
161   if (SIMIX_vm_get_state(vm) != SURF_VM_STATE_SUSPENDED)
162     THROWF(vm_error, 0, "VM(%s) was not suspended", vm->name().c_str());
163
164   XBT_DEBUG("resume VM(%s), where %d processes exist",
165       vm->name().c_str(), xbt_swag_size(sg_host_simix(vm)->process_list));
166
167   /* jump to vm_ws_resume() */
168   static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->resume();
169
170   smx_actor_t smx_process, smx_process_safe;
171   xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(vm)->process_list) {
172     XBT_DEBUG("resume %s", smx_process->name.c_str());
173     SIMIX_process_resume(smx_process, issuer);
174   }
175 }
176
177 void simcall_HANDLER_vm_resume(smx_simcall_t simcall, sg_host_t vm)
178 {
179   SIMIX_vm_resume(vm, simcall->issuer);
180 }
181
182
183 /**
184  * @brief Function to save a SIMIX VM host.
185  * This function is the same as vm_suspend, but the state of the VM is saved to the disk, and not preserved on memory.
186  * We can later restore it again.
187  *
188  * @param vm the vm host to save (a sg_host_t)
189  */
190 void SIMIX_vm_save(sg_host_t vm, smx_actor_t issuer)
191 {
192   const char *name = sg_host_get_name(vm);
193
194   if (SIMIX_vm_get_state(vm) != SURF_VM_STATE_RUNNING)
195     THROWF(vm_error, 0, "VM(%s) is not running", name);
196
197   XBT_DEBUG("save VM(%s), where %d processes exist", name, xbt_swag_size(sg_host_simix(vm)->process_list));
198
199   /* jump to vm_ws_save() */
200   static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->resume();
201
202   smx_actor_t smx_process, smx_process_safe;
203   xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(vm)->process_list) {
204     XBT_DEBUG("suspend %s", smx_process->name.c_str());
205     SIMIX_process_suspend(smx_process, issuer);
206   }
207 }
208
209 void simcall_HANDLER_vm_save(smx_simcall_t simcall, sg_host_t vm)
210 {
211   SIMIX_vm_save(vm, simcall->issuer);
212 }
213
214
215 /**
216  * @brief Function to restore a SIMIX VM host. This function restart the execution of the
217  * VM. All the processes on this VM will run again.
218  *
219  * @param vm the vm host to restore (a sg_host_t)
220  */
221 void SIMIX_vm_restore(sg_host_t vm, smx_actor_t issuer)
222 {
223   if (SIMIX_vm_get_state(vm) != SURF_VM_STATE_SAVED)
224     THROWF(vm_error, 0, "VM(%s) was not saved", vm->name().c_str());
225
226   XBT_DEBUG("restore VM(%s), where %d processes exist",
227       vm->name().c_str(), xbt_swag_size(sg_host_simix(vm)->process_list));
228
229   /* jump to vm_ws_restore() */
230   static_cast<simgrid::s4u::VirtualMachine*>(vm)->pimpl_vm_->resume();
231
232   smx_actor_t smx_process, smx_process_safe;
233   xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(vm)->process_list) {
234     XBT_DEBUG("resume %s", smx_process->name.c_str());
235     SIMIX_process_resume(smx_process, issuer);
236   }
237 }
238
239 void simcall_HANDLER_vm_restore(smx_simcall_t simcall, sg_host_t vm)
240 {
241   SIMIX_vm_restore(vm, simcall->issuer);
242 }
243
244
245 /**
246  * @brief Function to shutdown a SIMIX VM host. This function powers off the
247  * VM. All the processes on this VM will be killed. But, the state of the VM is
248  * preserved on memory. We can later start it again.
249  *
250  * @param vm the VM to shutdown (a sg_host_t)
251  */
252 void SIMIX_vm_shutdown(sg_host_t vm, smx_actor_t issuer)
253 {
254   if (SIMIX_vm_get_state(vm) != SURF_VM_STATE_RUNNING)
255     THROWF(vm_error, 0, "VM(%s) is not running", vm->name().c_str());
256
257   XBT_DEBUG("shutdown VM %s, that contains %d processes",
258       vm->name().c_str(),xbt_swag_size(sg_host_simix(vm)->process_list));
259
260   smx_actor_t smx_process, smx_process_safe;
261   xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(vm)->process_list) {
262     XBT_DEBUG("kill %s", smx_process->name.c_str());
263     SIMIX_process_kill(smx_process, issuer);
264   }
265
266   /* FIXME: we may have to do something at the surf layer, e.g., vcpu action */
267   static_cast<simgrid::surf::VirtualMachineImpl*>(vm->pimpl_)->setState(SURF_VM_STATE_CREATED);
268 }
269
270 void simcall_HANDLER_vm_shutdown(smx_simcall_t simcall, sg_host_t vm)
271 {
272   SIMIX_vm_shutdown(vm, simcall->issuer);
273 }
274
275 /**
276  * @brief Function to destroy a SIMIX VM host.
277  *
278  * @param vm the vm host to destroy (a sg_host_t)
279  */
280 void SIMIX_vm_destroy(sg_host_t vm)
281 {
282   /* this code basically performs a similar thing like SIMIX_host_destroy() */
283   XBT_DEBUG("destroy %s", vm->name().c_str());
284
285   /* FIXME: this is really strange that everything fails if the next line is removed.
286    * This is as if we shared these data with the PM, which definitely should not be the case...
287    *
288    * We need to test that suspending a VM does not suspends the processes running on its PM, for example.
289    * Or we need to simplify this code enough to make it actually readable (but this sounds harder than testing)
290    */
291   vm->extension_set<simgrid::simix::Host>(nullptr);
292
293   /* Don't free these things twice: they are the ones of my physical host */
294   vm->pimpl_cpu = nullptr;
295   vm->pimpl_netcard = nullptr;
296
297   vm->destroy();
298 }