Logo AND Algorithmique Numérique Distribuée

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