Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Cast enum to int.
[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 smx_host_t SIMIX_pre_vm_create(smx_simcall_t simcall, const char *name, smx_host_t ind_phys_host)
38 {
39   return SIMIX_vm_create(name, ind_phys_host);
40 }
41
42
43 /* works for VMs and PMs */
44 static long host_get_ramsize(smx_host_t vm, int *overcommit)
45 {
46   s_ws_params_t params;
47   surf_workstation_get_params(vm, &params);
48
49   if (overcommit)
50     *overcommit = params.overcommit;
51
52   return params.ramsize;
53 }
54
55 /* **** start a VM **** */
56 static int __can_be_started(smx_host_t vm)
57 {
58   smx_host_t pm = surf_vm_workstation_get_pm(vm);
59
60   int pm_overcommit = 0;
61   long pm_ramsize = host_get_ramsize(pm, &pm_overcommit);
62   long vm_ramsize = host_get_ramsize(vm, NULL);
63
64   if (!pm_ramsize) {
65     /* We assume users do not want to care about ramsize. */
66     return 1;
67   }
68
69   if (pm_overcommit) {
70     XBT_INFO("%s allows memory overcommit.", pm->key);
71     return 1;
72   }
73
74   long total_ramsize_of_vms = 0;
75   xbt_dynar_t dyn_vms = surf_workstation_get_vms(pm);
76   {
77     unsigned int cursor = 0;
78     smx_host_t another_vm;
79     xbt_dynar_foreach(dyn_vms, cursor, another_vm) {
80       long another_vm_ramsize = host_get_ramsize(vm, NULL);
81       total_ramsize_of_vms += another_vm_ramsize;
82     }
83   }
84
85   if (vm_ramsize > pm_ramsize - total_ramsize_of_vms) {
86     XBT_WARN("cannnot start %s@%s due to memory shortage: vm_ramsize %ld, free %ld, pm_ramsize %ld (bytes).",
87         vm->key, pm->key, vm_ramsize, pm_ramsize - total_ramsize_of_vms, pm_ramsize);
88     xbt_dynar_free(&dyn_vms);
89     return 0;
90   }
91
92   xbt_dynar_free(&dyn_vms);
93         return 1;
94 }
95
96 void SIMIX_vm_start(smx_host_t ind_vm)
97 {
98   if (__can_be_started(ind_vm))
99     surf_resource_set_state(surf_workstation_resource_priv(ind_vm),
100                             (int)SURF_VM_STATE_RUNNING);
101   else
102     THROWF(vm_error, 0, "The VM %s cannot be started", SIMIX_host_get_name(ind_vm));
103 }
104
105
106
107 void SIMIX_pre_vm_start(smx_simcall_t simcall, smx_host_t ind_vm)
108 {
109   SIMIX_vm_start(ind_vm);
110 }
111
112 int SIMIX_vm_get_state(smx_host_t ind_vm)
113 {
114   return surf_resource_get_state(surf_workstation_resource_priv(ind_vm));
115 }
116
117 int SIMIX_pre_vm_get_state(smx_simcall_t simcall, smx_host_t ind_vm)
118 {
119   return SIMIX_vm_get_state(ind_vm);
120 }
121
122
123 /**
124  * \brief Function to migrate a SIMIX VM host. 
125  *
126  * \param host the vm host to migrate (a smx_host_t)
127  */
128 void SIMIX_vm_migrate(smx_host_t ind_vm, smx_host_t ind_dst_pm)
129 {
130   /* precopy migration makes the VM temporally paused */
131   e_surf_vm_state_t state = SIMIX_vm_get_state(ind_vm);
132   xbt_assert(state == SURF_VM_STATE_SUSPENDED);
133
134   /* jump to vm_ws_migrate(). this will update the vm location. */
135   surf_vm_workstation_migrate(ind_vm, ind_dst_pm);
136 }
137
138 void SIMIX_pre_vm_migrate(smx_simcall_t simcall, smx_host_t ind_vm, smx_host_t ind_dst_pm)
139 {
140   SIMIX_vm_migrate(ind_vm, ind_dst_pm);
141 }
142
143
144 /**
145  * \brief Function to get the physical host of the given SIMIX VM host.
146  *
147  * \param host the vm host to get_phys_host (a smx_host_t)
148  */
149 void *SIMIX_vm_get_pm(smx_host_t ind_vm)
150 {
151   /* jump to vm_ws_get_pm(). this will return the vm name. */
152   return surf_vm_workstation_get_pm(ind_vm);
153 }
154
155 void *SIMIX_pre_vm_get_pm(smx_simcall_t simcall, smx_host_t ind_vm)
156 {
157   return SIMIX_vm_get_pm(ind_vm);
158 }
159
160
161 /**
162  * \brief Function to set the CPU bound of the given SIMIX VM host.
163  *
164  * \param host the vm host (a smx_host_t)
165  * \param bound bound (a double)
166  */
167 void SIMIX_vm_set_bound(smx_host_t ind_vm, double bound)
168 {
169   /* jump to vm_ws_set_vm_bound(). */
170   surf_vm_workstation_set_bound(ind_vm, bound);
171 }
172
173 void SIMIX_pre_vm_set_bound(smx_simcall_t simcall, smx_host_t ind_vm, double bound)
174 {
175   SIMIX_vm_set_bound(ind_vm, bound);
176 }
177
178
179 /**
180  * \brief Function to set the CPU affinity of the given SIMIX VM host.
181  *
182  * \param host the vm host (a smx_host_t)
183  * \param host the pm host (a smx_host_t)
184  * \param mask affinity mask (a unsigned long)
185  */
186 void SIMIX_vm_set_affinity(smx_host_t ind_vm, smx_host_t ind_pm, unsigned long mask)
187 {
188   /* make sure this at the MSG layer. */
189   xbt_assert(SIMIX_vm_get_pm(ind_vm) == ind_pm);
190
191   /* jump to vm_ws_set_vm_affinity(). */
192   surf_vm_workstation_set_affinity(ind_vm, ind_pm, mask);
193 }
194
195 void SIMIX_pre_vm_set_affinity(smx_simcall_t simcall, smx_host_t ind_vm, smx_host_t ind_pm, unsigned long mask)
196 {
197   SIMIX_vm_set_affinity(ind_vm, ind_pm, mask);
198 }
199
200
201 /**
202  * \brief Function to suspend a SIMIX VM host. This function stops the exection of the
203  * VM. All the processes on this VM will pause. The state of the VM is
204  * preserved on memory. We can later resume it again.
205  *
206  * \param host the vm host to suspend (a smx_host_t)
207  */
208 void SIMIX_vm_suspend(smx_host_t ind_vm, smx_process_t issuer)
209 {
210   const char *name = SIMIX_host_get_name(ind_vm);
211
212   if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_RUNNING)
213     THROWF(vm_error, 0, "VM(%s) is not running", name);
214
215   XBT_DEBUG("suspend VM(%s), where %d processes exist", name, xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
216
217   /* jump to vm_ws_suspend. The state will be set. */
218   surf_vm_workstation_suspend(ind_vm);
219
220   smx_process_t smx_process, smx_process_safe;
221   xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(ind_vm)->process_list) {
222     XBT_DEBUG("suspend %s", smx_process->name);
223     SIMIX_process_suspend(smx_process, issuer);
224   }
225
226   XBT_DEBUG("suspend all processes on the VM done done");
227 }
228
229 void SIMIX_pre_vm_suspend(smx_simcall_t simcall, smx_host_t ind_vm)
230 {
231   if (simcall->issuer->smx_host == ind_vm) {
232     XBT_ERROR("cannot suspend the VM where I run");
233     DIE_IMPOSSIBLE;
234   }
235
236   SIMIX_vm_suspend(ind_vm, simcall->issuer);
237
238   XBT_DEBUG("SIMIX_pre_vm_suspend done");
239 }
240
241
242 /**
243  * \brief Function to resume a SIMIX VM host. This function restart the execution of the
244  * VM. All the processes on this VM will run again. 
245  *
246  * \param host the vm host to resume (a smx_host_t)
247  */
248 void SIMIX_vm_resume(smx_host_t ind_vm, smx_process_t issuer)
249 {
250   const char *name = SIMIX_host_get_name(ind_vm);
251
252   if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_SUSPENDED)
253     THROWF(vm_error, 0, "VM(%s) was not suspended", name);
254
255   XBT_DEBUG("resume VM(%s), where %d processes exist", name, xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
256
257   /* jump to vm_ws_resume() */
258   surf_vm_workstation_resume(ind_vm);
259
260   smx_process_t smx_process, smx_process_safe;
261   xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(ind_vm)->process_list) {
262     XBT_DEBUG("resume %s", smx_process->name);
263     SIMIX_process_resume(smx_process, issuer);
264   }
265 }
266
267 void SIMIX_pre_vm_resume(smx_simcall_t simcall, smx_host_t ind_vm)
268 {
269   SIMIX_vm_resume(ind_vm, simcall->issuer);
270 }
271
272
273 /**
274  * \brief Function to save a SIMIX VM host.
275  * This function is the same as vm_suspend, but the state of the VM is saved to the disk, and not preserved on memory.
276  * We can later restore it again.
277  *
278  * \param host the vm host to save (a smx_host_t)
279  */
280 void SIMIX_vm_save(smx_host_t ind_vm, smx_process_t issuer)
281 {
282   const char *name = SIMIX_host_get_name(ind_vm);
283
284   if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_RUNNING)
285     THROWF(vm_error, 0, "VM(%s) is not running", name);
286
287
288   XBT_DEBUG("save VM(%s), where %d processes exist", name, xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
289
290   /* jump to vm_ws_save() */
291   surf_vm_workstation_save(ind_vm);
292
293   smx_process_t smx_process, smx_process_safe;
294   xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(ind_vm)->process_list) {
295     XBT_DEBUG("suspend %s", smx_process->name);
296     SIMIX_process_suspend(smx_process, issuer);
297   }
298 }
299
300 void SIMIX_pre_vm_save(smx_simcall_t simcall, smx_host_t ind_vm)
301 {
302   SIMIX_vm_save(ind_vm, simcall->issuer);
303 }
304
305
306 /**
307  * \brief Function to restore a SIMIX VM host. This function restart the execution of the
308  * VM. All the processes on this VM will run again. 
309  *
310  * \param host the vm host to restore (a smx_host_t)
311  */
312 void SIMIX_vm_restore(smx_host_t ind_vm, smx_process_t issuer)
313 {
314   const char *name = SIMIX_host_get_name(ind_vm);
315
316   if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_SAVED)
317     THROWF(vm_error, 0, "VM(%s) was not saved", name);
318
319   XBT_DEBUG("restore VM(%s), where %d processes exist", name, xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
320
321   /* jump to vm_ws_restore() */
322   surf_vm_workstation_resume(ind_vm);
323
324   smx_process_t smx_process, smx_process_safe;
325   xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(ind_vm)->process_list) {
326     XBT_DEBUG("resume %s", smx_process->name);
327     SIMIX_process_resume(smx_process, issuer);
328   }
329 }
330
331 void SIMIX_pre_vm_restore(smx_simcall_t simcall, smx_host_t ind_vm)
332 {
333   SIMIX_vm_restore(ind_vm, simcall->issuer);
334 }
335
336
337 /**
338  * \brief Function to shutdown a SIMIX VM host. This function powers off the
339  * VM. All the processes on this VM will be killed. But, the state of the VM is
340  * preserved on memory. We can later start it again.
341  *
342  * \param host the vm host to shutdown (a smx_host_t)
343  */
344 void SIMIX_vm_shutdown(smx_host_t ind_vm, smx_process_t issuer)
345 {
346   const char *name = SIMIX_host_get_name(ind_vm);
347
348   if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_RUNNING)
349     THROWF(vm_error, 0, "VM(%s) is not running", name);
350
351   XBT_DEBUG("shutdown %s", name);
352   XBT_DEBUG("%d processes in the VM", xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
353
354   smx_process_t smx_process, smx_process_safe;
355   xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(ind_vm)->process_list) {
356     XBT_DEBUG("kill %s", smx_process->name);
357     SIMIX_process_kill(smx_process, issuer);
358   }
359
360   /* FIXME: we may have to do something at the surf layer, e.g., vcpu action */
361   surf_resource_set_state(surf_workstation_resource_priv(ind_vm),
362                           (int)SURF_VM_STATE_CREATED);
363 }
364
365 void SIMIX_pre_vm_shutdown(smx_simcall_t simcall, smx_host_t ind_vm)
366 {
367   SIMIX_vm_shutdown(ind_vm, simcall->issuer);
368 }
369
370
371 /**
372  * \brief Function to destroy a SIMIX VM host.
373  *
374  * \param host the vm host to destroy (a smx_host_t)
375  */
376 void SIMIX_vm_destroy(smx_host_t ind_vm)
377 {
378   /* this code basically performs a similar thing like SIMIX_host_destroy() */
379
380   xbt_assert((ind_vm != NULL), "Invalid parameters");
381   const char *hostname = SIMIX_host_get_name(ind_vm);
382
383   XBT_DEBUG("destroy %s", hostname);
384
385   /* this will call the registered callback function, i.e., SIMIX_host_destroy().  */
386   xbt_lib_unset(host_lib, hostname, SIMIX_HOST_LEVEL, 1);
387
388   /* jump to vm_ws_destroy(). The surf level resource will be freed. */
389   surf_vm_workstation_destroy(ind_vm);
390 }
391
392 void SIMIX_pre_vm_destroy(smx_simcall_t simcall, smx_host_t ind_vm)
393 {
394   SIMIX_vm_destroy(ind_vm);
395 }