Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics in simix VM (much more to do)
[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 "smx_private.h"
7 #include "mc/mc.h"
8 #include "src/surf/virtual_machine.hpp"
9 #include "src/surf/HostImpl.hpp"
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_vm, simix, "Logging specific to SIMIX (vms)");
12
13 /* **** create a VM **** */
14
15 /**
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)
19  */
20 sg_host_t SIMIX_vm_create(const char *name, sg_host_t ind_phys_host)
21 {
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);
26
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. */
29
30   return host;
31 }
32
33
34 /* works for VMs and PMs */
35 static long host_get_ramsize(sg_host_t vm, int *overcommit)
36 {
37   s_vm_params_t params;
38   vm->extension<simgrid::surf::HostImpl>()->getParams(&params);
39
40   if (overcommit)
41     *overcommit = params.overcommit;
42
43   return params.ramsize;
44 }
45
46 /* **** start a VM **** */
47 static int __can_be_started(sg_host_t vm)
48 {
49   sg_host_t pm = surf_vm_get_pm(vm);
50
51   int pm_overcommit = 0;
52   long pm_ramsize = host_get_ramsize(pm, &pm_overcommit);
53   long vm_ramsize = host_get_ramsize(vm, nullptr);
54
55   if (!pm_ramsize) {
56     /* We assume users do not want to care about ramsize. */
57     return 1;
58   }
59
60   if (pm_overcommit) {
61     XBT_VERB("%s allows memory overcommit.", sg_host_get_name(pm));
62     return 1;
63   }
64
65   long total_ramsize_of_vms = 0;
66   xbt_dynar_t dyn_vms = pm->extension<simgrid::surf::HostImpl>()->getVms();
67   {
68     unsigned int cursor = 0;
69     sg_host_t another_vm;
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;
73     }
74   }
75
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);
81     return 0;
82   }
83
84   return 1;
85 }
86
87 void SIMIX_vm_start(sg_host_t ind_vm)
88 {
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);
93   else
94     THROWF(vm_error, 0, "The VM %s cannot be started", sg_host_get_name(ind_vm));
95 }
96
97
98 e_surf_vm_state_t SIMIX_vm_get_state(sg_host_t ind_vm)
99 {
100   return static_cast<simgrid::surf::VirtualMachine*>(
101     ind_vm->extension<simgrid::surf::HostImpl>()
102   )->getState();
103 }
104
105 /**
106  * @brief Function to migrate a SIMIX VM host.
107  *
108  * @param host the vm host to migrate (a sg_host_t)
109  */
110 void SIMIX_vm_migrate(sg_host_t ind_vm, sg_host_t ind_dst_pm)
111 {
112   /* precopy migration makes the VM temporally paused */
113   xbt_assert(SIMIX_vm_get_state(ind_vm) == SURF_VM_STATE_SUSPENDED);
114
115   /* jump to vm_ws_xigrate(). this will update the vm location. */
116   surf_vm_migrate(ind_vm, ind_dst_pm);
117 }
118
119 /**
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_migrate(vm, dst_pm); 
123  *     simcall_vm_resume(vm);
124  *
125  * It is called at the end of the migration_rx_fun function from msg/msg_vm.c
126  *
127  * @param vm VM to migrate
128  * @param src_pm  Source physical host
129  * @param dst_pmt Destination physical host
130  */
131 void SIMIX_vm_migratefrom_resumeto(sg_host_t vm, sg_host_t src_pm, sg_host_t dst_pm)
132 {
133   /* Update the vm location */
134   SIMIX_vm_migrate(vm, dst_pm);
135  
136   /* Resume the VM */
137   smx_actor_t self = SIMIX_process_self(); 
138   SIMIX_vm_resume(vm, self->simcall.issuer);
139
140
141 /**
142  * @brief Function to get the physical host of the given SIMIX VM host.
143  *
144  * @param host the vm host to get_phys_host (a sg_host_t)
145  */
146 void *SIMIX_vm_get_pm(sg_host_t host)
147 {
148   return surf_vm_get_pm(host);
149 }
150
151 /**
152  * @brief Function to set the CPU bound of the given SIMIX VM host.
153  *
154  * @param host the vm host (a sg_host_t)
155  * @param bound bound (a double)
156  */
157 void SIMIX_vm_set_bound(sg_host_t vm, double bound)
158 {
159   surf_vm_set_bound(vm, bound);
160 }
161
162 /**
163  * @brief Function to suspend a SIMIX VM host. This function stops the execution of the
164  * VM. All the processes on this VM will pause. The state of the VM is
165  * preserved on memory. We can later resume it again.
166  *
167  * @param vm the vm host to suspend (a sg_host_t)
168  */
169 void SIMIX_vm_suspend(sg_host_t vm, smx_actor_t issuer)
170 {
171   if (SIMIX_vm_get_state(vm) != SURF_VM_STATE_RUNNING)
172     THROWF(vm_error, 0, "VM(%s) is not running", vm->name().c_str());
173
174   XBT_DEBUG("suspend VM(%s), where %d processes exist", vm->name().c_str(), xbt_swag_size(sg_host_simix(vm)->process_list));
175
176   /* jump to vm_ws_suspend. The state will be set. */
177   surf_vm_suspend(vm);
178
179   smx_actor_t smx_process, smx_process_safe;
180   xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(vm)->process_list) {
181     XBT_DEBUG("suspend %s", smx_process->name.c_str());
182     SIMIX_process_suspend(smx_process, issuer);
183   }
184
185   XBT_DEBUG("suspend all processes on the VM done done");
186 }
187
188 void simcall_HANDLER_vm_suspend(smx_simcall_t simcall, sg_host_t vm)
189 {
190   xbt_assert(simcall->issuer->host != vm, "cannot suspend the VM where I run");
191
192   SIMIX_vm_suspend(vm, simcall->issuer);
193
194   XBT_DEBUG("simcall_HANDLER_vm_suspend done");
195 }
196
197
198 /**
199  * @brief Function to resume a SIMIX VM host. This function restart the execution of the
200  * VM. All the processes on this VM will run again.
201  *
202  * @param vm the vm host to resume (a sg_host_t)
203  */
204 void SIMIX_vm_resume(sg_host_t vm, smx_actor_t issuer)
205 {
206   if (SIMIX_vm_get_state(vm) != SURF_VM_STATE_SUSPENDED)
207     THROWF(vm_error, 0, "VM(%s) was not suspended", vm->name().c_str());
208
209   XBT_DEBUG("resume VM(%s), where %d processes exist",
210       vm->name().c_str(), xbt_swag_size(sg_host_simix(vm)->process_list));
211
212   /* jump to vm_ws_resume() */
213   surf_vm_resume(vm);
214
215   smx_actor_t smx_process, smx_process_safe;
216   xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(vm)->process_list) {
217     XBT_DEBUG("resume %s", smx_process->name.c_str());
218     SIMIX_process_resume(smx_process, issuer);
219   }
220 }
221
222 void simcall_HANDLER_vm_resume(smx_simcall_t simcall, sg_host_t vm)
223 {
224   SIMIX_vm_resume(vm, simcall->issuer);
225 }
226
227
228 /**
229  * @brief Function to save a SIMIX VM host.
230  * This function is the same as vm_suspend, but the state of the VM is saved to the disk, and not preserved on memory.
231  * We can later restore it again.
232  *
233  * @param vm the vm host to save (a sg_host_t)
234  */
235 void SIMIX_vm_save(sg_host_t vm, smx_actor_t issuer)
236 {
237   const char *name = sg_host_get_name(vm);
238
239   if (SIMIX_vm_get_state(vm) != SURF_VM_STATE_RUNNING)
240     THROWF(vm_error, 0, "VM(%s) is not running", name);
241
242   XBT_DEBUG("save VM(%s), where %d processes exist", name, xbt_swag_size(sg_host_simix(vm)->process_list));
243
244   /* jump to vm_ws_save() */
245   surf_vm_save(vm);
246
247   smx_actor_t smx_process, smx_process_safe;
248   xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(vm)->process_list) {
249     XBT_DEBUG("suspend %s", smx_process->name.c_str());
250     SIMIX_process_suspend(smx_process, issuer);
251   }
252 }
253
254 void simcall_HANDLER_vm_save(smx_simcall_t simcall, sg_host_t vm)
255 {
256   SIMIX_vm_save(vm, simcall->issuer);
257 }
258
259
260 /**
261  * @brief Function to restore a SIMIX VM host. This function restart the execution of the
262  * VM. All the processes on this VM will run again.
263  *
264  * @param vm the vm host to restore (a sg_host_t)
265  */
266 void SIMIX_vm_restore(sg_host_t vm, smx_actor_t issuer)
267 {
268   if (SIMIX_vm_get_state(vm) != SURF_VM_STATE_SAVED)
269     THROWF(vm_error, 0, "VM(%s) was not saved", vm->name().c_str());
270
271   XBT_DEBUG("restore VM(%s), where %d processes exist",
272       vm->name().c_str(), xbt_swag_size(sg_host_simix(vm)->process_list));
273
274   /* jump to vm_ws_restore() */
275   surf_vm_resume(vm);
276
277   smx_actor_t smx_process, smx_process_safe;
278   xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(vm)->process_list) {
279     XBT_DEBUG("resume %s", smx_process->name.c_str());
280     SIMIX_process_resume(smx_process, issuer);
281   }
282 }
283
284 void simcall_HANDLER_vm_restore(smx_simcall_t simcall, sg_host_t vm)
285 {
286   SIMIX_vm_restore(vm, simcall->issuer);
287 }
288
289
290 /**
291  * @brief Function to shutdown a SIMIX VM host. This function powers off the
292  * VM. All the processes on this VM will be killed. But, the state of the VM is
293  * preserved on memory. We can later start it again.
294  *
295  * @param vm the VM to shutdown (a sg_host_t)
296  */
297 void SIMIX_vm_shutdown(sg_host_t vm, smx_actor_t issuer)
298 {
299   if (SIMIX_vm_get_state(vm) != SURF_VM_STATE_RUNNING)
300     THROWF(vm_error, 0, "VM(%s) is not running", vm->name().c_str());
301
302   XBT_DEBUG("shutdown VM %s, that contains %d processes",
303       vm->name().c_str(),xbt_swag_size(sg_host_simix(vm)->process_list));
304
305   smx_actor_t smx_process, smx_process_safe;
306   xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(vm)->process_list) {
307     XBT_DEBUG("kill %s", smx_process->name.c_str());
308     SIMIX_process_kill(smx_process, issuer);
309   }
310
311   /* FIXME: we may have to do something at the surf layer, e.g., vcpu action */
312   static_cast<simgrid::surf::VirtualMachine*>(
313     vm->extension<simgrid::surf::HostImpl>()
314   )->setState(SURF_VM_STATE_CREATED);
315 }
316
317 void simcall_HANDLER_vm_shutdown(smx_simcall_t simcall, sg_host_t vm)
318 {
319   SIMIX_vm_shutdown(vm, simcall->issuer);
320 }
321
322 /**
323  * @brief Function to destroy a SIMIX VM host.
324  *
325  * @param vm the vm host to destroy (a sg_host_t)
326  */
327 void SIMIX_vm_destroy(sg_host_t vm)
328 {
329   /* this code basically performs a similar thing like SIMIX_host_destroy() */
330   XBT_DEBUG("destroy %s", vm->name().c_str());
331
332   /* FIXME: this is really strange that everything fails if the next line is removed.
333    * This is as if we shared these data with the PM, which definitely should not be the case...
334    *
335    * We need to test that suspending a VM does not suspends the processes running on its PM, for example.
336    * Or we need to simplify this code enough to make it actually readable (but this sounds harder than testing)
337    */
338   vm->extension_set<simgrid::simix::Host>(nullptr);
339
340   /* Don't free these things twice: they are the ones of my physical host */
341   vm->pimpl_cpu = nullptr;
342   vm->pimpl_netcard = nullptr;
343 }