Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'hypervisor' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid into hypervisor
[simgrid.git] / src / simix / smx_vm.c
1 /* Copyright (c) 2007-2012. 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->extension.vm_workstation.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_model->extension.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_model->extension.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_model->extension.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     SIMIX_vm_set_state(ind_vm, SURF_VM_STATE_RUNNING);
100   else
101     THROWF(vm_error, 0, "The VM %s cannot be started", SIMIX_host_get_name(ind_vm));
102 }
103
104
105
106 void SIMIX_pre_vm_start(smx_simcall_t simcall, smx_host_t ind_vm)
107 {
108   SIMIX_vm_start(ind_vm);
109   SIMIX_simcall_answer(simcall);
110 }
111
112 /* ***** set/get state of a VM ***** */
113 void SIMIX_vm_set_state(smx_host_t ind_vm, int state)
114 {
115   /* jump to vm_ws_set_state */
116   surf_vm_workstation_model->extension.vm_workstation.set_state(ind_vm, state);
117 }
118
119 void SIMIX_pre_vm_set_state(smx_simcall_t simcall, smx_host_t ind_vm, int state)
120 {
121   SIMIX_vm_set_state(ind_vm, state);
122 }
123
124 int SIMIX_vm_get_state(smx_host_t ind_vm)
125 {
126   return surf_vm_workstation_model->extension.vm_workstation.get_state(ind_vm);
127 }
128
129 int SIMIX_pre_vm_get_state(smx_simcall_t simcall, smx_host_t ind_vm)
130 {
131   return SIMIX_vm_get_state(ind_vm);
132 }
133
134
135 /**
136  * \brief Function to migrate a SIMIX VM host. 
137  *
138  * \param host the vm host to migrate (a smx_host_t)
139  */
140 void SIMIX_vm_migrate(smx_host_t ind_vm, smx_host_t ind_dst_pm)
141 {
142   /* precopy migration makes the VM temporally paused */
143   e_surf_vm_state_t state = SIMIX_vm_get_state(ind_vm);
144   xbt_assert(state == SURF_VM_STATE_SUSPENDED);
145
146   /* jump to vm_ws_migrate(). this will update the vm location. */
147   surf_vm_workstation_model->extension.vm_workstation.migrate(ind_vm, ind_dst_pm);
148 }
149
150 void SIMIX_pre_vm_migrate(smx_simcall_t simcall, smx_host_t ind_vm, smx_host_t ind_dst_pm)
151 {
152   SIMIX_vm_migrate(ind_vm, ind_dst_pm);
153   SIMIX_simcall_answer(simcall);
154 }
155
156
157 /**
158  * \brief Function to get the physical host of the given the SIMIX VM host.
159  *
160  * \param host the vm host to get_phys_host (a smx_host_t)
161  */
162 void *SIMIX_vm_get_pm(smx_host_t ind_vm)
163 {
164   /* jump to vm_ws_get_pm(). this will return the vm name. */
165   return surf_vm_workstation_model->extension.vm_workstation.get_pm(ind_vm);
166 }
167
168 void *SIMIX_pre_vm_get_pm(smx_simcall_t simcall, smx_host_t ind_vm)
169 {
170   return SIMIX_vm_get_pm(ind_vm);
171 }
172
173
174 /**
175  * \brief Function to suspend a SIMIX VM host. This function stops the exection of the
176  * VM. All the processes on this VM will pause. The state of the VM is
177  * preserved on memory. We can later resume it again.
178  *
179  * \param host the vm host to suspend (a smx_host_t)
180  */
181 void SIMIX_vm_suspend(smx_host_t ind_vm, smx_process_t issuer)
182 {
183   const char *name = SIMIX_host_get_name(ind_vm);
184
185   if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_RUNNING)
186     THROWF(vm_error, 0, "VM(%s) is not running", name);
187
188   XBT_DEBUG("suspend VM(%s), where %d processes exist", name, xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
189
190   /* jump to vm_ws_suspend. The state will be set. */
191   surf_vm_workstation_model->extension.vm_workstation.suspend(ind_vm);
192
193   smx_process_t smx_process, smx_process_safe;
194   xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(ind_vm)->process_list) {
195     XBT_DEBUG("suspend %s", smx_process->name);
196     SIMIX_process_suspend(smx_process, issuer);
197   }
198
199   XBT_DEBUG("suspend all processes on the VM done done");
200 }
201
202 void SIMIX_pre_vm_suspend(smx_simcall_t simcall, smx_host_t ind_vm)
203 {
204   if (simcall->issuer->smx_host == ind_vm) {
205     XBT_ERROR("cannot suspend the VM where I run");
206     DIE_IMPOSSIBLE;
207   }
208
209   SIMIX_vm_suspend(ind_vm, simcall->issuer);
210
211   /* without this, simcall_vm_suspend() does not return to the userland. why? */
212   SIMIX_simcall_answer(simcall);
213
214   XBT_DEBUG("SIMIX_pre_vm_suspend done");
215 }
216
217
218 /**
219  * \brief Function to resume a SIMIX VM host. This function restart the execution of the
220  * VM. All the processes on this VM will run again. 
221  *
222  * \param host the vm host to resume (a smx_host_t)
223  */
224 void SIMIX_vm_resume(smx_host_t ind_vm, smx_process_t issuer)
225 {
226   const char *name = SIMIX_host_get_name(ind_vm);
227
228   if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_SUSPENDED)
229     THROWF(vm_error, 0, "VM(%s) was not suspended", name);
230
231   XBT_DEBUG("resume VM(%s), where %d processes exist", name, xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
232
233   /* jump to vm_ws_resume() */
234   surf_vm_workstation_model->extension.vm_workstation.resume(ind_vm);
235
236   smx_process_t smx_process, smx_process_safe;
237   xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(ind_vm)->process_list) {
238     XBT_DEBUG("resume %s", smx_process->name);
239     SIMIX_process_resume(smx_process, issuer);
240   }
241 }
242
243 void SIMIX_pre_vm_resume(smx_simcall_t simcall, smx_host_t ind_vm)
244 {
245   SIMIX_vm_resume(ind_vm, simcall->issuer);
246   SIMIX_simcall_answer(simcall);
247 }
248
249
250 /**
251  * \brief Function to save a SIMIX VM host.
252  * This function is the same as vm_suspend, but the state of the VM is saved to the disk, and not preserved on memory.
253  * We can later restore it again.
254  *
255  * \param host the vm host to save (a smx_host_t)
256  */
257 void SIMIX_vm_save(smx_host_t ind_vm, smx_process_t issuer)
258 {
259   const char *name = SIMIX_host_get_name(ind_vm);
260
261   if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_RUNNING)
262     THROWF(vm_error, 0, "VM(%s) is not running", name);
263
264
265   XBT_DEBUG("save VM(%s), where %d processes exist", name, xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
266
267   /* jump to vm_ws_save() */
268   surf_vm_workstation_model->extension.vm_workstation.save(ind_vm);
269
270   smx_process_t smx_process, smx_process_safe;
271   xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(ind_vm)->process_list) {
272     XBT_DEBUG("suspend %s", smx_process->name);
273     SIMIX_process_suspend(smx_process, issuer);
274   }
275 }
276
277 void SIMIX_pre_vm_save(smx_simcall_t simcall, smx_host_t ind_vm)
278 {
279   SIMIX_vm_save(ind_vm, simcall->issuer);
280
281   /* without this, simcall_vm_suspend() does not return to the userland. why? */
282   SIMIX_simcall_answer(simcall);
283 }
284
285
286 /**
287  * \brief Function to restore a SIMIX VM host. This function restart the execution of the
288  * VM. All the processes on this VM will run again. 
289  *
290  * \param host the vm host to restore (a smx_host_t)
291  */
292 void SIMIX_vm_restore(smx_host_t ind_vm, smx_process_t issuer)
293 {
294   const char *name = SIMIX_host_get_name(ind_vm);
295
296   if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_SAVED)
297     THROWF(vm_error, 0, "VM(%s) was not saved", name);
298
299   XBT_DEBUG("restore VM(%s), where %d processes exist", name, xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
300
301   /* jump to vm_ws_restore() */
302   surf_vm_workstation_model->extension.vm_workstation.resume(ind_vm);
303
304   smx_process_t smx_process, smx_process_safe;
305   xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(ind_vm)->process_list) {
306     XBT_DEBUG("resume %s", smx_process->name);
307     SIMIX_process_resume(smx_process, issuer);
308   }
309 }
310
311 void SIMIX_pre_vm_restore(smx_simcall_t simcall, smx_host_t ind_vm)
312 {
313   SIMIX_vm_restore(ind_vm, simcall->issuer);
314   SIMIX_simcall_answer(simcall);
315 }
316
317
318 /**
319  * \brief Function to shutdown a SIMIX VM host. This function powers off the
320  * VM. All the processes on this VM will be killed. But, the state of the VM is
321  * preserved on memory. We can later start it again.
322  *
323  * \param host the vm host to shutdown (a smx_host_t)
324  */
325 void SIMIX_vm_shutdown(smx_host_t ind_vm, smx_process_t issuer)
326 {
327   const char *name = SIMIX_host_get_name(ind_vm);
328
329   if (SIMIX_vm_get_state(ind_vm) != SURF_VM_STATE_RUNNING)
330     THROWF(vm_error, 0, "VM(%s) is not running", name);
331
332   XBT_DEBUG("%d processes in the VM", xbt_swag_size(SIMIX_host_priv(ind_vm)->process_list));
333
334   smx_process_t smx_process, smx_process_safe;
335   xbt_swag_foreach_safe(smx_process, smx_process_safe, SIMIX_host_priv(ind_vm)->process_list) {
336     XBT_DEBUG("shutdown %s", name);
337     SIMIX_process_kill(smx_process, issuer);
338   }
339
340   /* FIXME: we may have to do something at the surf layer, e.g., vcpu action */
341   SIMIX_vm_set_state(ind_vm, SURF_VM_STATE_CREATED);
342 }
343
344 void SIMIX_pre_vm_shutdown(smx_simcall_t simcall, smx_host_t ind_vm)
345 {
346   SIMIX_vm_shutdown(ind_vm, simcall->issuer);
347   SIMIX_simcall_answer(simcall);
348 }
349
350
351 /**
352  * \brief Function to destroy a SIMIX VM host.
353  *
354  * \param host the vm host to destroy (a smx_host_t)
355  */
356 void SIMIX_vm_destroy(smx_host_t ind_vm)
357 {
358   /* this code basically performs a similar thing like SIMIX_host_destroy() */
359
360   xbt_assert((ind_vm != NULL), "Invalid parameters");
361   const char *hostname = SIMIX_host_get_name(ind_vm);
362
363   /* this will call the registered callback function, i.e., SIMIX_host_destroy().  */
364   xbt_lib_unset(host_lib, hostname, SIMIX_HOST_LEVEL, 1);
365
366   /* jump to vm_ws_destroy(). The surf level resource will be freed. */
367   surf_vm_workstation_model->extension.vm_workstation.destroy(ind_vm);
368 }
369
370 void SIMIX_pre_vm_destroy(smx_simcall_t simcall, smx_host_t ind_vm)
371 {
372   SIMIX_vm_destroy(ind_vm);
373   SIMIX_simcall_answer(simcall);
374 }