Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot//simgrid/simgrid
[simgrid.git] / src / simix / smx_vm.c
1 /* Copyright (c) 2007-2014. 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 smx_host_t SIMIX_vm_create(const char *name, smx_host_t ind_phys_host)
24 {
25   /* Create surf associated resource */
26   surf_vm_workstation_model_create(name, ind_phys_host);
27
28   smx_host_t smx_host = SIMIX_host_create(name, ind_phys_host, NULL);
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 smx_host;
34 }
35
36
37 /* works for VMs and PMs */
38 static long host_get_ramsize(smx_host_t vm, int *overcommit)
39 {
40   s_ws_params_t params;
41   surf_workstation_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(smx_host_t vm)
51 {
52   smx_host_t pm = surf_vm_workstation_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.", pm->key);
65     return 1;
66   }
67
68   long total_ramsize_of_vms = 0;
69   xbt_dynar_t dyn_vms = surf_workstation_get_vms(pm);
70   {
71     unsigned int cursor = 0;
72     smx_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         vm->key, pm->key, vm_ramsize, pm_ramsize - total_ramsize_of_vms, pm_ramsize);
82     xbt_dynar_free(&dyn_vms);
83     return 0;
84   }
85
86   xbt_dynar_free(&dyn_vms);
87         return 1;
88 }
89
90 void SIMIX_vm_start(smx_host_t ind_vm)
91 {
92   if (__can_be_started(ind_vm))
93     surf_resource_set_state(surf_workstation_resource_priv(ind_vm),
94                             (int)SURF_VM_STATE_RUNNING);
95   else
96     THROWF(vm_error, 0, "The VM %s cannot be started", SIMIX_host_get_name(ind_vm));
97 }
98
99
100 int SIMIX_vm_get_state(smx_host_t ind_vm)
101 {
102   return surf_resource_get_state(surf_workstation_resource_priv(ind_vm));
103 }
104
105 /**
106  * \brief Function to migrate a SIMIX VM host.
107  *
108  * \param host the vm host to migrate (a smx_host_t)
109  */
110 void SIMIX_vm_migrate(smx_host_t ind_vm, smx_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_workstation_migrate(ind_vm, ind_dst_pm);
117 }
118
119 /**
120  * \brief Function to get the physical host of the given SIMIX VM host.
121  *
122  * \param host the vm host to get_phys_host (a smx_host_t)
123  */
124 void *SIMIX_vm_get_pm(smx_host_t ind_vm)
125 {
126   /* jump to vm_ws_get_pm(). this will return the vm name. */
127   return surf_vm_workstation_get_pm(ind_vm);
128 }
129
130 /**
131  * \brief Function to set the CPU bound of the given SIMIX VM host.
132  *
133  * \param host the vm host (a smx_host_t)
134  * \param bound bound (a double)
135  */
136 void SIMIX_vm_set_bound(smx_host_t ind_vm, double bound)
137 {
138   /* jump to vm_ws_set_vm_bound(). */
139   surf_vm_workstation_set_bound(ind_vm, bound);
140 }
141
142 /**
143  * \brief Function to set the CPU affinity of the given SIMIX VM host.
144  *
145  * \param host the vm host (a smx_host_t)
146  * \param host the pm host (a smx_host_t)
147  * \param mask affinity mask (a unsigned long)
148  */
149 void SIMIX_vm_set_affinity(smx_host_t ind_vm, smx_host_t ind_pm, unsigned long mask)
150 {
151   /* make sure this at the MSG layer. */
152   xbt_assert(SIMIX_vm_get_pm(ind_vm) == ind_pm);
153
154   /* jump to vm_ws_set_vm_affinity(). */
155   surf_vm_workstation_set_affinity(ind_vm, ind_pm, mask);
156 }
157
158
159 /**
160  * \brief Function to suspend a SIMIX VM host. This function stops the execution of the
161  * VM. All the processes on this VM will pause. The state of the VM is
162  * preserved on memory. We can later resume it again.
163  *
164  * \param host the vm host to suspend (a smx_host_t)
165  */
166 void SIMIX_vm_suspend(smx_host_t ind_vm, smx_process_t issuer)
167 {
168   const char *name = SIMIX_host_get_name(ind_vm);
169
170   if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_RUNNING)
171     THROWF(vm_error, 0, "VM(%s) is not running", name);
172
173   XBT_DEBUG("suspend VM(%s), where %d processes exist", name, xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
174
175   /* jump to vm_ws_suspend. The state will be set. */
176   surf_vm_workstation_suspend(ind_vm);
177
178   smx_process_t smx_process, smx_process_safe;
179   xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(ind_vm)->process_list) {
180     XBT_DEBUG("suspend %s", smx_process->name);
181     SIMIX_process_suspend(smx_process, issuer);
182   }
183
184   XBT_DEBUG("suspend all processes on the VM done done");
185 }
186
187 void simcall_HANDLER_vm_suspend(smx_simcall_t simcall, smx_host_t ind_vm)
188 {
189   if (simcall->issuer->smx_host == ind_vm) {
190     XBT_ERROR("cannot suspend the VM where I run");
191     DIE_IMPOSSIBLE;
192   }
193
194   SIMIX_vm_suspend(ind_vm, simcall->issuer);
195
196   XBT_DEBUG("simcall_HANDLER_vm_suspend done");
197 }
198
199
200 /**
201  * \brief Function to resume a SIMIX VM host. This function restart the execution of the
202  * VM. All the processes on this VM will run again.
203  *
204  * \param host the vm host to resume (a smx_host_t)
205  */
206 void SIMIX_vm_resume(smx_host_t ind_vm, smx_process_t issuer)
207 {
208   const char *name = SIMIX_host_get_name(ind_vm);
209
210   if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_SUSPENDED)
211     THROWF(vm_error, 0, "VM(%s) was not suspended", name);
212
213   XBT_DEBUG("resume VM(%s), where %d processes exist", name, xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
214
215   /* jump to vm_ws_resume() */
216   surf_vm_workstation_resume(ind_vm);
217
218   smx_process_t smx_process, smx_process_safe;
219   xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(ind_vm)->process_list) {
220     XBT_DEBUG("resume %s", smx_process->name);
221     SIMIX_process_resume(smx_process, issuer);
222   }
223 }
224
225 void simcall_HANDLER_vm_resume(smx_simcall_t simcall, smx_host_t ind_vm)
226 {
227   SIMIX_vm_resume(ind_vm, simcall->issuer);
228 }
229
230
231 /**
232  * \brief Function to save a SIMIX VM host.
233  * This function is the same as vm_suspend, but the state of the VM is saved to the disk, and not preserved on memory.
234  * We can later restore it again.
235  *
236  * \param host the vm host to save (a smx_host_t)
237  */
238 void SIMIX_vm_save(smx_host_t ind_vm, smx_process_t issuer)
239 {
240   const char *name = SIMIX_host_get_name(ind_vm);
241
242   if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_RUNNING)
243     THROWF(vm_error, 0, "VM(%s) is not running", name);
244
245
246   XBT_DEBUG("save VM(%s), where %d processes exist", name, xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
247
248   /* jump to vm_ws_save() */
249   surf_vm_workstation_save(ind_vm);
250
251   smx_process_t smx_process, smx_process_safe;
252   xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(ind_vm)->process_list) {
253     XBT_DEBUG("suspend %s", smx_process->name);
254     SIMIX_process_suspend(smx_process, issuer);
255   }
256 }
257
258 void simcall_HANDLER_vm_save(smx_simcall_t simcall, smx_host_t ind_vm)
259 {
260   SIMIX_vm_save(ind_vm, simcall->issuer);
261 }
262
263
264 /**
265  * \brief Function to restore a SIMIX VM host. This function restart the execution of the
266  * VM. All the processes on this VM will run again.
267  *
268  * \param host the vm host to restore (a smx_host_t)
269  */
270 void SIMIX_vm_restore(smx_host_t ind_vm, smx_process_t issuer)
271 {
272   const char *name = SIMIX_host_get_name(ind_vm);
273
274   if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_SAVED)
275     THROWF(vm_error, 0, "VM(%s) was not saved", name);
276
277   XBT_DEBUG("restore VM(%s), where %d processes exist", name, xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
278
279   /* jump to vm_ws_restore() */
280   surf_vm_workstation_resume(ind_vm);
281
282   smx_process_t smx_process, smx_process_safe;
283   xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(ind_vm)->process_list) {
284     XBT_DEBUG("resume %s", smx_process->name);
285     SIMIX_process_resume(smx_process, issuer);
286   }
287 }
288
289 void simcall_HANDLER_vm_restore(smx_simcall_t simcall, smx_host_t ind_vm)
290 {
291   SIMIX_vm_restore(ind_vm, simcall->issuer);
292 }
293
294
295 /**
296  * \brief Function to shutdown a SIMIX VM host. This function powers off the
297  * VM. All the processes on this VM will be killed. But, the state of the VM is
298  * preserved on memory. We can later start it again.
299  *
300  * \param host the vm host to shutdown (a smx_host_t)
301  */
302 void SIMIX_vm_shutdown(smx_host_t ind_vm, smx_process_t issuer)
303 {
304   const char *name = SIMIX_host_get_name(ind_vm);
305
306   if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_RUNNING)
307     THROWF(vm_error, 0, "VM(%s) is not running", name);
308
309   XBT_DEBUG("shutdown %s", name);
310   XBT_DEBUG("%d processes in the VM", xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
311
312   smx_process_t smx_process, smx_process_safe;
313   xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(ind_vm)->process_list) {
314     XBT_DEBUG("kill %s", smx_process->name);
315     SIMIX_process_kill(smx_process, issuer);
316   }
317
318   /* FIXME: we may have to do something at the surf layer, e.g., vcpu action */
319   surf_resource_set_state(surf_workstation_resource_priv(ind_vm),
320                           (int)SURF_VM_STATE_CREATED);
321 }
322
323 void simcall_HANDLER_vm_shutdown(smx_simcall_t simcall, smx_host_t ind_vm)
324 {
325   SIMIX_vm_shutdown(ind_vm, simcall->issuer);
326 }
327
328
329 /**
330  * \brief Function to destroy a SIMIX VM host.
331  *
332  * \param host the vm host to destroy (a smx_host_t)
333  */
334 void SIMIX_vm_destroy(smx_host_t ind_vm)
335 {
336   /* this code basically performs a similar thing like SIMIX_host_destroy() */
337
338   xbt_assert((ind_vm != NULL), "Invalid parameters");
339   const char *hostname = SIMIX_host_get_name(ind_vm);
340
341   XBT_DEBUG("destroy %s", hostname);
342
343   /* this will call the registered callback function, i.e., SIMIX_host_destroy().  */
344   xbt_lib_unset(host_lib, hostname, SIMIX_HOST_LEVEL, 1);
345
346   /* jump to vm_ws_destroy(). The surf level resource will be freed. */
347   surf_vm_workstation_destroy(ind_vm);
348 }