Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
just a cosmetic
[simgrid.git] / src / msg / msg_vm.c
1 /* Copyright (c) 2012. The SimGrid Team. All rights reserved.               */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 // QUESTIONS:
7 // 1./ check how and where a new VM is added to the list of the hosts
8 // 2./ Diff between SIMIX_Actions and SURF_Actions
9 // => SIMIX_actions : point synchro entre processus de niveau (theoretically speaking I do not have to create such SIMIX_ACTION
10 // =>  Surf_Actions
11
12 // TODO
13 //      MSG_TRACE can be revisited in order to use  the host
14 //      To implement a mixed model between workstation and vm_workstation,
15 //     please give a look at surf_model_private_t model_private at SURF Level and to the share resource functions
16 //     double (*share_resources) (double now);
17 //      For the action into the vm workstation model, we should be able to leverage the usual one (and if needed, look at
18 //              the workstation model.
19
20 #include "msg_private.h"
21 #include "xbt/sysdep.h"
22 #include "xbt/log.h"
23
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_vm, msg,
25                                 "Cloud-oriented parts of the MSG API");
26
27
28 /* **** ******** GENERAL ********* **** */
29
30 /** \ingroup m_vm_management
31  * \brief Returns the value of a given vm property
32  *
33  * \param vm a vm
34  * \param name a property name
35  * \return value of a property (or NULL if property not set)
36  */
37
38 const char *MSG_vm_get_property_value(msg_vm_t vm, const char *name)
39 {
40   return MSG_host_get_property_value(vm, name);
41 }
42
43 /** \ingroup m_vm_management
44  * \brief Returns a xbt_dict_t consisting of the list of properties assigned to this host
45  *
46  * \param vm a vm
47  * \return a dict containing the properties
48  */
49 xbt_dict_t MSG_vm_get_properties(msg_vm_t vm)
50 {
51   xbt_assert((vm != NULL), "Invalid parameters (vm is NULL)");
52
53   return (simcall_host_get_properties(vm));
54 }
55
56 /** \ingroup m_host_management
57  * \brief Change the value of a given host property
58  *
59  * \param host a host
60  * \param name a property name
61  * \param value what to change the property to
62  * \param free_ctn the freeing function to use to kill the value on need
63  */
64 void MSG_vm_set_property_value(msg_vm_t vm, const char *name, void *value, void_f_pvoid_t free_ctn)
65 {
66   xbt_dict_set(MSG_host_get_properties(vm), name, value, free_ctn);
67 }
68
69 /** \ingroup msg_vm_management
70  * \brief Finds a msg_vm_t using its name.
71  *
72  * This is a name directory service
73  * \param name the name of a vm.
74  * \return the corresponding vm
75  *
76  * Please note that a VM is a specific host. Hence, you should give a different name
77  * for each VM/PM.
78  */
79
80 msg_vm_t MSG_vm_get_by_name(const char *name)
81 {
82         return MSG_get_host_by_name(name);
83 }
84
85 /** \ingroup m_vm_management
86  *
87  * \brief Return the name of the #msg_host_t.
88  *
89  * This functions checks whether \a host is a valid pointer or not and return
90    its name.
91  */
92 const char *MSG_vm_get_name(msg_vm_t vm)
93 {
94   return MSG_host_get_name(vm);
95 }
96
97
98 /* **** Check state of a VM **** */
99 static inline int __MSG_vm_is_state(msg_vm_t vm, e_surf_vm_state_t state)
100 {
101   return simcall_vm_get_state(vm) == state;
102 }
103
104 /** @brief Returns whether the given VM has just reated, not running.
105  *  @ingroup msg_VMs
106  */
107 int MSG_vm_is_created(msg_vm_t vm)
108 {
109   return __MSG_vm_is_state(vm, SURF_VM_STATE_CREATED);
110 }
111
112 /** @brief Returns whether the given VM is currently running
113  *  @ingroup msg_VMs
114  */
115 int MSG_vm_is_running(msg_vm_t vm)
116 {
117   return __MSG_vm_is_state(vm, SURF_VM_STATE_RUNNING);
118 }
119
120 /** @brief Returns whether the given VM is currently migrating
121  *  @ingroup msg_VMs
122  */
123 int MSG_vm_is_migrating(msg_vm_t vm)
124 {
125   return __MSG_vm_is_state(vm, SURF_VM_STATE_MIGRATING);
126 }
127
128 /** @brief Returns whether the given VM is currently suspended, not running.
129  *  @ingroup msg_VMs
130  */
131 int MSG_vm_is_suspended(msg_vm_t vm)
132 {
133   return __MSG_vm_is_state(vm, SURF_VM_STATE_SUSPENDED);
134 }
135
136 /** @brief Returns whether the given VM is being saved (FIXME: live saving or not?).
137  *  @ingroup msg_VMs
138  */
139 int MSG_vm_is_saving(msg_vm_t vm)
140 {
141   return __MSG_vm_is_state(vm, SURF_VM_STATE_SAVING);
142 }
143
144 /** @brief Returns whether the given VM has been saved, not running.
145  *  @ingroup msg_VMs
146  */
147 int MSG_vm_is_saved(msg_vm_t vm)
148 {
149   return __MSG_vm_is_state(vm, SURF_VM_STATE_SAVED);
150 }
151
152 /** @brief Returns whether the given VM is being restored, not running.
153  *  @ingroup msg_VMs
154  */
155 int MSG_vm_is_restoring(msg_vm_t vm)
156 {
157   return __MSG_vm_is_state(vm, SURF_VM_STATE_RESTORING);
158 }
159
160
161
162 /* ------------------------------------------------------------------------- */
163 /* ------------------------------------------------------------------------- */
164
165 /* **** ******** MSG vm actions ********* **** */
166
167 /** @brief Create a new VM with specified parameters.
168  *  @ingroup msg_VMs*
169  *
170  */
171 msg_vm_t MSG_vm_create(msg_host_t ind_pm, const char *name,
172                                              int ncpus, int ramsize, int net_cap, char *disk_path, int disksize)
173 {
174   msg_vm_t vm = MSG_vm_create_core(ind_pm, name);
175
176   {
177     s_ws_params_t params;
178     memset(&params, 0, sizeof(params));
179     params.ramsize = ramsize;
180     params.overcommit = 0;
181     simcall_host_set_params(vm, &params);
182   }
183
184   /* TODO: We will revisit the disk support later. */
185
186   return vm;
187 }
188
189
190 /** @brief Create a new VM object. The VM is not yet started. The resource of the VM is allocated upon MSG_vm_start().
191  *  @ingroup msg_VMs*
192  *
193  * A VM is treated as a host. The name of the VM must be unique among all hosts.
194  */
195 msg_vm_t MSG_vm_create_core(msg_host_t ind_pm, const char *name)
196 {
197   /* make sure the VM of the same name does not exit */
198   {
199     void *ind_host_tmp = xbt_lib_get_elm_or_null(host_lib, name);
200     if (ind_host_tmp) {
201       XBT_ERROR("host %s already exits", name);
202       return NULL;
203     }
204   }
205
206   /* Note: ind_vm and vm_workstation point to the same elm object. */
207   msg_vm_t ind_vm = NULL;
208   void *ind_vm_workstation =  NULL;
209
210   /* Ask the SIMIX layer to create the surf vm resource */
211   ind_vm_workstation = simcall_vm_create(name, ind_pm);
212   ind_vm = (msg_vm_t) __MSG_host_create(ind_vm_workstation);
213
214   XBT_DEBUG("A new VM (%s) has been created", name);
215
216   #ifdef HAVE_TRACING
217   TRACE_msg_vm_create(name, ind_pm);
218   #endif
219
220   return ind_vm;
221 }
222
223
224 /** @brief Start a vm (i.e., boot the guest operating system)
225  *  @ingroup msg_VMs
226  *
227  *  If the VM cannot be started, an exception is generated.
228  *
229  */
230 void MSG_vm_start(msg_vm_t vm)
231 {
232   simcall_vm_start(vm);
233
234   #ifdef HAVE_TRACING
235   TRACE_msg_vm_start(vm);
236   #endif
237 }
238
239
240
241 /** @brief Immediately kills all processes within the given VM. Any memory that they allocated will be leaked.
242  *  @ingroup msg_VMs
243  *
244  * FIXME: No extra delay occurs. If you want to simulate this too, you want to
245  * use a #MSG_process_sleep() or something. I'm not quite sure.
246  */
247 void MSG_vm_shutdown(msg_vm_t vm)
248 {
249   /* msg_vm_t equals to msg_host_t */
250   simcall_vm_shutdown(vm);
251
252   // #ifdef HAVE_TRACING
253   // TRACE_msg_vm_(vm);
254   // #endif
255 }
256
257
258 static int migration_rx_fun(int argc, char *argv[])
259 {
260   const char *pr_name = MSG_process_get_name(MSG_process_self());
261   const char *host_name = MSG_host_get_name(MSG_host_self());
262   int need_exit = 0;
263
264   xbt_assert(argc == 3);
265   const char *mbox = argv[1];
266   const char *mbox_ctl = argv[2];
267
268   for (;;) {
269     msg_task_t task = NULL;
270     MSG_task_recv(&task, mbox);
271
272     if (strcmp(task->name, "finalize") == 0)
273       need_exit = 1;
274
275     MSG_task_destroy(task);
276
277     if (need_exit)
278       break;
279   }
280
281
282   {
283     msg_task_t task = MSG_task_create("fin", 0, 0, NULL);
284     msg_error_t ret = MSG_task_send(task, mbox_ctl);
285     xbt_assert(ret == MSG_OK);
286   }
287
288   XBT_INFO("%s@%s done", pr_name, host_name);
289
290   return 0;
291 }
292
293 static int migration_tx_fun(int argc, char *argv[])
294 {
295   const char *pr_name = MSG_process_get_name(MSG_process_self());
296   const char *host_name = MSG_host_get_name(MSG_host_self());
297
298   xbt_assert(argc == 3);
299   const char *mbox = argv[1];
300   long ramsize = atol(argv[2]);
301
302   char *task_name = bprintf("task-%s", mbox);
303   msg_task_t task = MSG_task_create(task_name, 0, ramsize, NULL);
304   msg_error_t ret = MSG_task_send(task, mbox);
305   xbt_assert(ret == MSG_OK);
306
307   xbt_free(task_name);
308
309   {
310     msg_task_t task = MSG_task_create("finalize", 0, 0, NULL);
311     msg_error_t ret = MSG_task_send(task, mbox);
312     xbt_assert(ret == MSG_OK);
313   }
314
315   XBT_INFO("%s@%s done", pr_name, host_name);
316
317   return 0;
318 }
319
320 static void create_dummy_task(msg_vm_t vm, msg_host_t old_pm, msg_host_t new_pm, long ramsize)
321 {
322   if (ramsize == 0)
323     XBT_WARN("migrate a VM, but ramsize is zero");
324
325   char *suffix = bprintf("mig-%s(%s-%s)", vm->key, old_pm->key, new_pm->key);
326   char *mbox = bprintf("MBOX:%s", suffix);
327   char *mbox_ctl = bprintf("MBOX:%s:CTL", suffix);
328
329   {
330     const char *pr_name = "mig_tx";
331     int nargvs = 4;
332     char **argv = xbt_new(char *, nargvs);
333     argv[0] = xbt_strdup(pr_name);
334     argv[1] = xbt_strdup(mbox);
335     argv[2] = bprintf("%ld", ramsize);
336     argv[3] = NULL;
337
338     msg_process_t pr = MSG_process_create_with_arguments(pr_name, migration_tx_fun, NULL, old_pm, nargvs - 1, argv);
339   }
340
341   {
342     const char *pr_name = "mig_rx";
343     int nargvs = 4;
344     char **argv = xbt_new(char *, nargvs);
345     argv[0] = xbt_strdup(pr_name);
346     argv[1] = xbt_strdup(mbox);
347     argv[2] = xbt_strdup(mbox_ctl);
348     argv[3] = NULL;
349
350     msg_process_t pr = MSG_process_create_with_arguments(pr_name, migration_rx_fun, NULL, new_pm, nargvs - 1, argv);
351   }
352
353   {
354     msg_task_t task = NULL;
355     msg_error_t ret = MSG_task_recv(&task, mbox_ctl);
356     xbt_assert(ret == MSG_OK);
357   }
358
359   xbt_free(suffix);
360   xbt_free(mbox);
361   xbt_free(mbox_ctl);
362 }
363
364
365 /** @brief Migrate the VM to the given host.
366  *  @ingroup msg_VMs
367  *
368  * FIXME: No migration cost occurs. If you want to simulate this too, you want to use a
369  * MSG_task_send() before or after, depending on whether you want to do cold or hot
370  * migration.
371  */
372 void MSG_vm_migrate(msg_vm_t vm, msg_host_t new_pm)
373 {
374   /* some thoughts:
375    * - One approach is ...
376    *   We first create a new VM (i.e., destination VM) on the destination
377    *   physical host. The destination VM will receive the state of the source
378    *   VM over network. We will finally destroy the source VM.
379    *   - This behavior is similar to the way of migration in the real world.
380    *     Even before a migration is completed, we will see a destination VM,
381    *     consuming resources.
382    *   - We have to relocate all processes. The existing process migraion code
383    *     will work for this?
384    *   - The name of the VM is a somewhat unique ID in the code. It is tricky
385    *     for the destination VM?
386    *
387    * - Another one is ...
388    *   We update the information of the given VM to place it to the destination
389    *   physical host.
390    *
391    * The second one would be easier.
392    *   
393    */
394
395   msg_host_t old_pm = simcall_vm_get_pm(vm);
396
397   s_ws_params_t params;
398   simcall_host_get_params(vm, &params);
399   long ramsize = params.ramsize;
400
401   create_dummy_task(vm, old_pm, new_pm, ramsize);
402
403   simcall_vm_migrate(vm, new_pm);
404
405   XBT_DEBUG("VM(%s) moved from PM(%s) to PM(%s)", vm->key, old_pm->key, new_pm->key);
406
407   #ifdef HAVE_TRACING
408   TRACE_msg_vm_change_host(vm, old_pm, new_pm);
409   #endif
410 }
411
412
413 /** @brief Immediately suspend the execution of all processes within the given VM.
414  *  @ingroup msg_VMs
415  *
416  * This function stops the exection of the VM. All the processes on this VM
417  * will pause. The state of the VM is perserved. We can later resume it again.
418  *
419  * No suspension cost occurs.
420  */
421 void MSG_vm_suspend(msg_vm_t vm)
422 {
423   simcall_vm_suspend(vm);
424
425   XBT_DEBUG("vm_suspend done");
426
427   #ifdef HAVE_TRACING
428   TRACE_msg_vm_suspend(vm);
429   #endif
430 }
431
432
433 /** @brief Resume the execution of the VM. All processes on the VM run again.
434  *  @ingroup msg_VMs
435  *
436  * No resume cost occurs.
437  */
438 void MSG_vm_resume(msg_vm_t vm)
439 {
440   simcall_vm_resume(vm);
441
442   #ifdef HAVE_TRACING
443   TRACE_msg_vm_resume(vm);
444   #endif
445 }
446
447
448 /** @brief Immediately save the execution of all processes within the given VM.
449  *  @ingroup msg_VMs
450  *
451  * This function stops the exection of the VM. All the processes on this VM
452  * will pause. The state of the VM is perserved. We can later resume it again.
453  *
454  * FIXME: No suspension cost occurs. If you want to simulate this too, you want to
455  * use a \ref MSG_file_write() before or after, depending on the exact semantic
456  * of VM save to you.
457  */
458 void MSG_vm_save(msg_vm_t vm)
459 {
460   simcall_vm_save(vm);
461   #ifdef HAVE_TRACING
462   TRACE_msg_vm_save(vm);
463   #endif
464 }
465
466 /** @brief Restore the execution of the VM. All processes on the VM run again.
467  *  @ingroup msg_VMs
468  *
469  * FIXME: No restore cost occurs. If you want to simulate this too, you want to
470  * use a \ref MSG_file_read() before or after, depending on the exact semantic
471  * of VM restore to you.
472  */
473 void MSG_vm_restore(msg_vm_t vm)
474 {
475   simcall_vm_restore(vm);
476
477   #ifdef HAVE_TRACING
478   TRACE_msg_vm_restore(vm);
479   #endif
480 }
481
482
483 /** @brief Destroy a VM. Destroy the VM object from the simulation.
484  *  @ingroup msg_VMs
485  */
486 void MSG_vm_destroy(msg_vm_t vm)
487 {
488   /* First, terminate all processes on the VM if necessary */
489   if (MSG_vm_is_running(vm))
490       simcall_vm_shutdown(vm);
491
492   if (!MSG_vm_is_created(vm)) {
493     XBT_CRITICAL("shutdown the given VM before destroying it");
494     DIE_IMPOSSIBLE;
495   }
496
497   /* Then, destroy the VM object */
498   simcall_vm_destroy(vm);
499
500   __MSG_host_destroy(vm);
501
502   #ifdef HAVE_TRACING
503   TRACE_msg_vm_end(vm);
504   #endif
505 }
506
507
508 /** @brief Get the physical host of a givne VM.
509  *  @ingroup msg_VMs
510  */
511 msg_host_t MSG_vm_get_pm(msg_vm_t vm)
512 {
513   return simcall_vm_get_pm(vm);
514 }