Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Correctly destroy the MSG_host object of a VM
[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         return MSG_get_host_by_name(name);
82 }
83
84 /** \ingroup m_vm_management
85  *
86  * \brief Return the name of the #msg_host_t.
87  *
88  * This functions checks whether \a host is a valid pointer or not and return
89    its name.
90  */
91 const char *MSG_vm_get_name(msg_vm_t vm) {
92   return MSG_host_get_name(vm);
93 }
94
95
96 /* **** Check state of a VM **** */
97 static inline int __MSG_vm_is_state(msg_vm_t vm, e_surf_vm_state_t state) {
98   return simcall_vm_get_state(vm) == state;
99 }
100
101 /** @brief Returns whether the given VM has just reated, not running.
102  *  @ingroup msg_VMs
103  */
104 int MSG_vm_is_created(msg_vm_t vm)
105 {
106   return __MSG_vm_is_state(vm, SURF_VM_STATE_CREATED);
107 }
108
109 /** @brief Returns whether the given VM is currently running
110  *  @ingroup msg_VMs
111  */
112 int MSG_vm_is_running(msg_vm_t vm)
113 {
114   return __MSG_vm_is_state(vm, SURF_VM_STATE_RUNNING);
115 }
116
117 /** @brief Returns whether the given VM is currently migrating
118  *  @ingroup msg_VMs
119  */
120 int MSG_vm_is_migrating(msg_vm_t vm)
121 {
122   return __MSG_vm_is_state(vm, SURF_VM_STATE_MIGRATING);
123 }
124
125 /** @brief Returns whether the given VM is currently suspended, not running.
126  *  @ingroup msg_VMs
127  */
128 int MSG_vm_is_suspended(msg_vm_t vm)
129 {
130   return __MSG_vm_is_state(vm, SURF_VM_STATE_SUSPENDED);
131 }
132
133 /** @brief Returns whether the given VM is being saved (FIXME: live saving or not?).
134  *  @ingroup msg_VMs
135  */
136 int MSG_vm_is_saving(msg_vm_t vm)
137 {
138   return __MSG_vm_is_state(vm, SURF_VM_STATE_SAVING);
139 }
140
141 /** @brief Returns whether the given VM has been saved, not running.
142  *  @ingroup msg_VMs
143  */
144 int MSG_vm_is_saved(msg_vm_t vm)
145 {
146   return __MSG_vm_is_state(vm, SURF_VM_STATE_SAVED);
147 }
148
149 /** @brief Returns whether the given VM is being restored, not running.
150  *  @ingroup msg_VMs
151  */
152 int MSG_vm_is_restoring(msg_vm_t vm)
153 {
154   return __MSG_vm_is_state(vm, SURF_VM_STATE_RESTORING);
155 }
156
157
158
159 /* ------------------------------------------------------------------------- */
160 /* ------------------------------------------------------------------------- */
161
162 /* **** ******** MSG vm actions ********* **** */
163
164 /** @brief Create a new VM with specified parameters.
165  *  @ingroup msg_VMs*
166  *
167  */
168 msg_vm_t MSG_vm_create(msg_host_t ind_pm, const char *name,
169                                              int core_nb, int mem_cap, int net_cap, char *disk_path, int disk_size)
170 {
171   msg_vm_t vm = MSG_vm_create_core(ind_pm, name);
172
173   MSG_vm_set_property_value(vm, "CORE_NB", bprintf("%d", core_nb), free);
174   MSG_vm_set_property_value(vm, "MEM_CAP", bprintf("%d", mem_cap), free);
175   MSG_vm_set_property_value(vm, "NET_CAP", bprintf("%d", net_cap), free);
176
177   /* TODO: We will revisit the disk support later. */
178
179   return vm;
180 }
181
182
183 /** @brief Create a new VM object. The VM is not yet started. The resource of the VM is allocated upon MSG_vm_start().
184  *  @ingroup msg_VMs*
185  *
186  * A VM is treated as a host. The name of the VM must be unique among all hosts.
187  */
188 msg_vm_t MSG_vm_create_core(msg_host_t ind_pm, const char *name)
189 {
190   /* make sure the VM of the same name does not exit */
191   {
192     void *ind_host_tmp = xbt_lib_get_elm_or_null(host_lib, name);
193     if (ind_host_tmp) {
194       XBT_ERROR("host %s already exits", name);
195       return NULL;
196     }
197   }
198
199   /* Note: ind_vm and vm_workstation point to the same elm object. */
200   msg_vm_t ind_vm = NULL;
201   void *ind_vm_workstation =  NULL;
202
203   // Ask simix to create the surf vm resource
204   ind_vm_workstation = simcall_vm_create(name, ind_pm);
205   ind_vm = (msg_vm_t) __MSG_host_create(ind_vm_workstation);
206
207   XBT_DEBUG("A new VM (%s) has been created", name);
208
209   #ifdef HAVE_TRACING
210   TRACE_msg_vm_create(name, ind_pm);
211   #endif
212
213   return ind_vm;
214 }
215
216
217 /** @brief Start a vm (ie. boot)
218  *  @ingroup msg_VMs
219  *
220  *  If the VM cannot be started, an exception is generated.
221  *
222  */
223 void MSG_vm_start(msg_vm_t vm)
224 {
225   //Please note that vm start can raise an exception if the VM cannot be started.
226   simcall_vm_start(vm);
227
228   #ifdef HAVE_TRACING
229   TRACE_msg_vm_start(vm);
230   #endif
231 }
232
233
234
235 /** @brief Immediately kills all processes within the given VM. Any memory that they allocated will be leaked.
236  *  @ingroup msg_VMs
237  *
238  * FIXME: No extra delay occurs. If you want to simulate this too, you want to
239  * use a #MSG_process_sleep() or something. I'm not quite sure.
240  */
241 void MSG_vm_shutdown(msg_vm_t vm)
242 {
243   /* msg_vm_t equals to msg_host_t */
244   simcall_vm_shutdown(vm);
245
246   // #ifdef HAVE_TRACING
247   // TRACE_msg_vm_(vm);
248   // #endif
249 }
250
251
252 /** @brief Migrate the VM to the given host.
253  *  @ingroup msg_VMs
254  *
255  * FIXME: No migration cost occurs. If you want to simulate this too, you want to use a
256  * MSG_task_send() before or after, depending on whether you want to do cold or hot
257  * migration.
258  */
259 void MSG_vm_migrate(msg_vm_t vm, msg_host_t new_pm)
260 {
261   /* some thoughts:
262    * - One approach is ...
263    *   We first create a new VM (i.e., destination VM) on the destination
264    *   physical host. The destination VM will receive the state of the source
265    *   VM over network. We will finally destroy the source VM.
266    *   - This behavior is similar to the way of migration in the real world.
267    *     Even before a migration is completed, we will see a destination VM,
268    *     consuming resources.
269    *   - We have to relocate all processes. The existing process migraion code
270    *     will work for this?
271    *   - The name of the VM is a somewhat unique ID in the code. It is tricky
272    *     for the destination VM?
273    *
274    * - Another one is ...
275    *   We update the information of the given VM to place it to the destination
276    *   physical host.
277    *
278    * The second one would be easier.
279    *   
280    */
281
282   msg_host_t old_pm = simcall_vm_get_pm(vm);
283
284   simcall_vm_migrate(vm, new_pm);
285
286   XBT_DEBUG("VM(%s) moved from PM(%s) to PM(%s)", vm->key, old_pm->key, new_pm->key);
287
288   #ifdef HAVE_TRACING
289   TRACE_msg_vm_change_host(vm, old_pm, new_pm);
290   #endif
291 }
292
293
294 /** @brief Immediately suspend the execution of all processes within the given VM.
295  *  @ingroup msg_VMs
296  *
297  * This function stops the exection of the VM. All the processes on this VM
298  * will pause. The state of the VM is perserved. We can later resume it again.
299  *
300  * No suspension cost occurs.
301  */
302 void MSG_vm_suspend(msg_vm_t vm)
303 {
304   simcall_vm_suspend(vm);
305
306   XBT_DEBUG("vm_suspend done");
307
308   #ifdef HAVE_TRACING
309   TRACE_msg_vm_suspend(vm);
310   #endif
311 }
312
313
314 /** @brief Resume the execution of the VM. All processes on the VM run again.
315  *  @ingroup msg_VMs
316  *
317  * No resume cost occurs.
318  */
319 void MSG_vm_resume(msg_vm_t vm)
320 {
321   simcall_vm_resume(vm);
322
323   #ifdef HAVE_TRACING
324   TRACE_msg_vm_resume(vm);
325   #endif
326 }
327
328
329 /** @brief Immediately save the execution of all processes within the given VM.
330  *  @ingroup msg_VMs
331  *
332  * This function stops the exection of the VM. All the processes on this VM
333  * will pause. The state of the VM is perserved. We can later resume it again.
334  *
335  * FIXME: No suspension cost occurs. If you want to simulate this too, you want to
336  * use a \ref MSG_file_write() before or after, depending on the exact semantic
337  * of VM save to you.
338  */
339 void MSG_vm_save(msg_vm_t vm)
340 {
341   simcall_vm_save(vm);
342   #ifdef HAVE_TRACING
343   TRACE_msg_vm_save(vm);
344   #endif
345 }
346
347 /** @brief Restore the execution of the VM. All processes on the VM run again.
348  *  @ingroup msg_VMs
349  *
350  * FIXME: No restore cost occurs. If you want to simulate this too, you want to
351  * use a \ref MSG_file_read() before or after, depending on the exact semantic
352  * of VM restore to you.
353  */
354 void MSG_vm_restore(msg_vm_t vm)
355 {
356   simcall_vm_restore(vm);
357
358   #ifdef HAVE_TRACING
359   TRACE_msg_vm_restore(vm);
360   #endif
361 }
362
363
364 /** @brief Destroy a VM. Destroy the VM object from the simulation.
365  *  @ingroup msg_VMs
366  */
367 void MSG_vm_destroy(msg_vm_t vm)
368 {
369   /* First, terminate all processes on the VM if necessary */
370   if (MSG_vm_is_running(vm))
371       simcall_vm_shutdown(vm);
372
373   if (!MSG_vm_is_created(vm)) {
374     XBT_CRITICAL("shutdown the given VM before destroying it");
375     DIE_IMPOSSIBLE;
376   }
377
378   /* Then, destroy the VM object */
379   simcall_vm_destroy(vm);
380
381   __MSG_host_destroy(vm);
382
383   #ifdef HAVE_TRACING
384   TRACE_msg_vm_end(vm);
385   #endif
386 }