Logo AND Algorithmique Numérique Distribuée

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