Logo AND Algorithmique Numérique Distribuée

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