Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cleanups in mailbox module init/fini
[simgrid.git] / src / simix / smx_vm.cpp
1 /* Copyright (c) 2007-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "smx_private.h"
8 #include "mc/mc.h"
9 #include "src/surf/virtual_machine.hpp"
10 #include "src/surf/HostImpl.hpp"
11
12 //If you need to log some stuffs, just uncomment these two lines and uses XBT_DEBUG for instance
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_vm, simix, "Logging specific to SIMIX (vms)");
14
15 /* **** create a VM **** */
16
17 /**
18  * \brief Internal function to create a SIMIX host.
19  * \param name name of the host to create
20  * \param data some user data (may be NULL)
21  */
22 sg_host_t SIMIX_vm_create(const char *name, sg_host_t ind_phys_host)
23 {
24   /* Create surf associated resource */
25   surf_vm_model_create(name, ind_phys_host);
26   sg_host_t host = sg_host_by_name(name);
27   SIMIX_host_create(host);
28
29   /* We will be able to register the VM to its physical host, so that we can promptly
30    * retrieve the list VMs on the physical host. */
31
32   return host;
33 }
34
35
36 /* works for VMs and PMs */
37 static long host_get_ramsize(sg_host_t vm, int *overcommit)
38 {
39   s_vm_params_t params;
40   vm->extension<simgrid::surf::HostImpl>()->getParams(&params);
41
42   if (overcommit)
43     *overcommit = params.overcommit;
44
45   return params.ramsize;
46 }
47
48 /* **** start a VM **** */
49 static int __can_be_started(sg_host_t vm)
50 {
51   sg_host_t pm = surf_vm_get_pm(vm);
52
53   int pm_overcommit = 0;
54   long pm_ramsize = host_get_ramsize(pm, &pm_overcommit);
55   long vm_ramsize = host_get_ramsize(vm, NULL);
56
57   if (!pm_ramsize) {
58     /* We assume users do not want to care about ramsize. */
59     return 1;
60   }
61
62   if (pm_overcommit) {
63     XBT_VERB("%s allows memory overcommit.", sg_host_get_name(pm));
64     return 1;
65   }
66
67   long total_ramsize_of_vms = 0;
68   xbt_dynar_t dyn_vms = pm->extension<simgrid::surf::HostImpl>()->getVms();
69   {
70     unsigned int cursor = 0;
71     sg_host_t another_vm;
72     xbt_dynar_foreach(dyn_vms, cursor, another_vm) {
73       long another_vm_ramsize = host_get_ramsize(vm, NULL);
74       total_ramsize_of_vms += another_vm_ramsize;
75     }
76   }
77
78   if (vm_ramsize > pm_ramsize - total_ramsize_of_vms) {
79     XBT_WARN("cannnot start %s@%s due to memory shortage: vm_ramsize %ld, free %ld, pm_ramsize %ld (bytes).",
80         sg_host_get_name(vm), sg_host_get_name(pm),
81         vm_ramsize, pm_ramsize - total_ramsize_of_vms, pm_ramsize);
82     xbt_dynar_free(&dyn_vms);
83     return 0;
84   }
85
86   return 1;
87 }
88
89 void SIMIX_vm_start(sg_host_t ind_vm)
90 {
91   if (__can_be_started(ind_vm))
92     static_cast<simgrid::surf::VirtualMachine*>(
93       ind_vm->extension<simgrid::surf::HostImpl>()
94     )->setState(SURF_VM_STATE_RUNNING);
95   else
96     THROWF(vm_error, 0, "The VM %s cannot be started", sg_host_get_name(ind_vm));
97 }
98
99
100 e_surf_vm_state_t SIMIX_vm_get_state(sg_host_t ind_vm)
101 {
102   return static_cast<simgrid::surf::VirtualMachine*>(
103     ind_vm->extension<simgrid::surf::HostImpl>()
104   )->getState();
105 }
106
107 /**
108  * \brief Function to migrate a SIMIX VM host.
109  *
110  * \param host the vm host to migrate (a sg_host_t)
111  */
112 void SIMIX_vm_migrate(sg_host_t ind_vm, sg_host_t ind_dst_pm)
113 {
114   /* precopy migration makes the VM temporally paused */
115   xbt_assert(SIMIX_vm_get_state(ind_vm) == SURF_VM_STATE_SUSPENDED);
116
117   /* jump to vm_ws_xigrate(). this will update the vm location. */
118   surf_vm_migrate(ind_vm, ind_dst_pm);
119 }
120
121 /**
122  * \brief Encompassing simcall to prevent the removal of the src or the dst node at the end of a VM migration
123  *  The simcall actually invokes the following calls: 
124  *     simcall_vm_set_affinity(vm, src_pm, 0); 
125  *     simcall_vm_migrate(vm, dst_pm); 
126  *     simcall_vm_resume(vm);
127  *
128  * It is called at the end of the migration_rx_fun function from msg/msg_vm.c
129  *
130  * \param vm VM to migrate
131  * \param src_pm  Source physical host
132  * \param dst_pmt Destination physical host
133  */
134 void SIMIX_vm_migratefrom_resumeto(sg_host_t vm, sg_host_t src_pm, sg_host_t dst_pm)
135 {
136   /* deinstall the current affinity setting for the CPU */
137   SIMIX_vm_set_affinity(vm, src_pm, 0);
138
139   /* Update the vm location */
140   SIMIX_vm_migrate(vm, dst_pm);
141  
142   /* Resume the VM */
143   smx_process_t self = SIMIX_process_self(); 
144   SIMIX_vm_resume(vm, self->simcall.issuer);
145
146
147 /**
148  * \brief Function to get the physical host of the given SIMIX VM host.
149  *
150  * \param host the vm host to get_phys_host (a sg_host_t)
151  */
152 void *SIMIX_vm_get_pm(sg_host_t ind_vm)
153 {
154   /* jump to vm_ws_get_pm(). this will return the vm name. */
155   return surf_vm_get_pm(ind_vm);
156 }
157
158 /**
159  * \brief Function to set the CPU bound of the given SIMIX VM host.
160  *
161  * \param host the vm host (a sg_host_t)
162  * \param bound bound (a double)
163  */
164 void SIMIX_vm_set_bound(sg_host_t ind_vm, double bound)
165 {
166   /* jump to vm_ws_set_vm_bound(). */
167   surf_vm_set_bound(ind_vm, bound);
168 }
169
170 /**
171  * \brief Function to set the CPU affinity of the given SIMIX VM host.
172  *
173  * \param host the vm host (a sg_host_t)
174  * \param host the pm host (a sg_host_t)
175  * \param mask affinity mask (a unsigned long)
176  */
177 void SIMIX_vm_set_affinity(sg_host_t ind_vm, sg_host_t ind_pm, unsigned long mask)
178 {
179   /* make sure this at the MSG layer. */
180   xbt_assert(SIMIX_vm_get_pm(ind_vm) == ind_pm);
181
182   /* jump to vm_ws_set_vm_affinity(). */
183   surf_vm_set_affinity(ind_vm, ind_pm, mask);
184 }
185
186
187 /**
188  * \brief Function to suspend a SIMIX VM host. This function stops the execution of the
189  * VM. All the processes on this VM will pause. The state of the VM is
190  * preserved on memory. We can later resume it again.
191  *
192  * \param host the vm host to suspend (a sg_host_t)
193  */
194 void SIMIX_vm_suspend(sg_host_t ind_vm, smx_process_t issuer)
195 {
196   const char *name = sg_host_get_name(ind_vm);
197
198   if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_RUNNING)
199     THROWF(vm_error, 0, "VM(%s) is not running", name);
200
201   XBT_DEBUG("suspend VM(%s), where %d processes exist", name, xbt_swag_size(sg_host_simix(ind_vm)->process_list));
202
203   /* jump to vm_ws_suspend. The state will be set. */
204   surf_vm_suspend(ind_vm);
205
206   smx_process_t smx_process, smx_process_safe;
207   xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(ind_vm)->process_list) {
208     XBT_DEBUG("suspend %s", smx_process->name);
209     SIMIX_process_suspend(smx_process, issuer);
210   }
211
212   XBT_DEBUG("suspend all processes on the VM done done");
213 }
214
215 void simcall_HANDLER_vm_suspend(smx_simcall_t simcall, sg_host_t ind_vm)
216 {
217   if (simcall->issuer->host == ind_vm) {
218     XBT_ERROR("cannot suspend the VM where I run");
219     DIE_IMPOSSIBLE;
220   }
221
222   SIMIX_vm_suspend(ind_vm, simcall->issuer);
223
224   XBT_DEBUG("simcall_HANDLER_vm_suspend done");
225 }
226
227
228 /**
229  * \brief Function to resume a SIMIX VM host. This function restart the execution of the
230  * VM. All the processes on this VM will run again.
231  *
232  * \param host the vm host to resume (a sg_host_t)
233  */
234 void SIMIX_vm_resume(sg_host_t ind_vm, smx_process_t issuer)
235 {
236   const char *name = sg_host_get_name(ind_vm);
237
238   if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_SUSPENDED)
239     THROWF(vm_error, 0, "VM(%s) was not suspended", name);
240
241   XBT_DEBUG("resume VM(%s), where %d processes exist", name, xbt_swag_size(sg_host_simix(ind_vm)->process_list));
242
243   /* jump to vm_ws_resume() */
244   surf_vm_resume(ind_vm);
245
246   smx_process_t smx_process, smx_process_safe;
247   xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(ind_vm)->process_list) {
248     XBT_DEBUG("resume %s", smx_process->name);
249     SIMIX_process_resume(smx_process, issuer);
250   }
251 }
252
253 void simcall_HANDLER_vm_resume(smx_simcall_t simcall, sg_host_t ind_vm)
254 {
255   SIMIX_vm_resume(ind_vm, simcall->issuer);
256 }
257
258
259 /**
260  * \brief Function to save a SIMIX VM host.
261  * This function is the same as vm_suspend, but the state of the VM is saved to the disk, and not preserved on memory.
262  * We can later restore it again.
263  *
264  * \param host the vm host to save (a sg_host_t)
265  */
266 void SIMIX_vm_save(sg_host_t ind_vm, smx_process_t issuer)
267 {
268   const char *name = sg_host_get_name(ind_vm);
269
270   if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_RUNNING)
271     THROWF(vm_error, 0, "VM(%s) is not running", name);
272
273
274   XBT_DEBUG("save VM(%s), where %d processes exist", name, xbt_swag_size(sg_host_simix(ind_vm)->process_list));
275
276   /* jump to vm_ws_save() */
277   surf_vm_save(ind_vm);
278
279   smx_process_t smx_process, smx_process_safe;
280   xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(ind_vm)->process_list) {
281     XBT_DEBUG("suspend %s", smx_process->name);
282     SIMIX_process_suspend(smx_process, issuer);
283   }
284 }
285
286 void simcall_HANDLER_vm_save(smx_simcall_t simcall, sg_host_t ind_vm)
287 {
288   SIMIX_vm_save(ind_vm, simcall->issuer);
289 }
290
291
292 /**
293  * \brief Function to restore a SIMIX VM host. This function restart the execution of the
294  * VM. All the processes on this VM will run again.
295  *
296  * \param host the vm host to restore (a sg_host_t)
297  */
298 void SIMIX_vm_restore(sg_host_t ind_vm, smx_process_t issuer)
299 {
300   const char *name = sg_host_get_name(ind_vm);
301
302   if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_SAVED)
303     THROWF(vm_error, 0, "VM(%s) was not saved", name);
304
305   XBT_DEBUG("restore VM(%s), where %d processes exist", name, xbt_swag_size(sg_host_simix(ind_vm)->process_list));
306
307   /* jump to vm_ws_restore() */
308   surf_vm_resume(ind_vm);
309
310   smx_process_t smx_process, smx_process_safe;
311   xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(ind_vm)->process_list) {
312     XBT_DEBUG("resume %s", smx_process->name);
313     SIMIX_process_resume(smx_process, issuer);
314   }
315 }
316
317 void simcall_HANDLER_vm_restore(smx_simcall_t simcall, sg_host_t ind_vm)
318 {
319   SIMIX_vm_restore(ind_vm, simcall->issuer);
320 }
321
322
323 /**
324  * \brief Function to shutdown a SIMIX VM host. This function powers off the
325  * VM. All the processes on this VM will be killed. But, the state of the VM is
326  * preserved on memory. We can later start it again.
327  *
328  * \param host the vm host to shutdown (a sg_host_t)
329  */
330 void SIMIX_vm_shutdown(sg_host_t ind_vm, smx_process_t issuer)
331 {
332   const char *name = sg_host_get_name(ind_vm);
333
334   if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_RUNNING)
335     THROWF(vm_error, 0, "VM(%s) is not running", name);
336
337   XBT_DEBUG("shutdown %s", name);
338   XBT_DEBUG("%d processes in the VM", xbt_swag_size(sg_host_simix(ind_vm)->process_list));
339
340   smx_process_t smx_process, smx_process_safe;
341   xbt_swag_foreach_safe(smx_process, smx_process_safe, sg_host_simix(ind_vm)->process_list) {
342     XBT_DEBUG("kill %s", smx_process->name);
343     SIMIX_process_kill(smx_process, issuer);
344   }
345
346   /* FIXME: we may have to do something at the surf layer, e.g., vcpu action */
347   static_cast<simgrid::surf::VirtualMachine*>(
348     ind_vm->extension<simgrid::surf::HostImpl>()
349   )->setState(SURF_VM_STATE_CREATED);
350 }
351
352 void simcall_HANDLER_vm_shutdown(smx_simcall_t simcall, sg_host_t ind_vm)
353 {
354   SIMIX_vm_shutdown(ind_vm, simcall->issuer);
355 }
356
357
358 /**
359  * \brief Function to destroy a SIMIX VM host.
360  *
361  * \param host the vm host to destroy (a sg_host_t)
362  */
363 void SIMIX_vm_destroy(sg_host_t ind_vm)
364 {
365   /* this code basically performs a similar thing like SIMIX_host_destroy() */
366
367   const char *hostname = sg_host_get_name(ind_vm);
368
369   XBT_DEBUG("destroy %s", hostname);
370
371   /* this will call the registered callback function, i.e., SIMIX_host_destroy().  */
372   sg_host_simix_destroy(ind_vm);
373
374   /* jump to vm_ws_destroy(). The surf level resource will be freed. */
375   surf_vm_destroy(ind_vm);
376 }