1 /* Copyright (c) 2012. The SimGrid Team. All rights reserved. */
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. */
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
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.
20 #include "msg_private.h"
21 #include "xbt/sysdep.h"
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_vm, msg,
25 "Cloud-oriented parts of the MSG API");
28 /* **** ******** GENERAL ********* **** */
30 /** \ingroup m_vm_management
31 * \brief Returns the value of a given vm property
34 * \param name a property name
35 * \return value of a property (or NULL if property not set)
38 const char *MSG_vm_get_property_value(msg_vm_t vm, const char *name)
40 return MSG_host_get_property_value(vm, name);
43 /** \ingroup m_vm_management
44 * \brief Returns a xbt_dict_t consisting of the list of properties assigned to this host
47 * \return a dict containing the properties
49 xbt_dict_t MSG_vm_get_properties(msg_vm_t vm)
51 xbt_assert((vm != NULL), "Invalid parameters (vm is NULL)");
53 return (simcall_host_get_properties(vm));
56 /** \ingroup m_host_management
57 * \brief Change the value of a given host property
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
64 void MSG_vm_set_property_value(msg_vm_t vm, const char *name, void *value,void_f_pvoid_t free_ctn) {
66 xbt_dict_set(MSG_host_get_properties(vm), name, value,free_ctn);
69 /** \ingroup msg_vm_management
70 * \brief Finds a msg_vm_t using its name.
72 * This is a name directory service
73 * \param name the name of a vm.
74 * \return the corresponding vm
76 * Please note that a VM is a specific host. Hence, you should give a different name
80 msg_vm_t MSG_vm_get_by_name(const char *name){
81 return MSG_get_host_by_name(name);
84 /** \ingroup m_vm_management
86 * \brief Return the name of the #msg_host_t.
88 * This functions checks whether \a host is a valid pointer or not and return
91 const char *MSG_vm_get_name(msg_vm_t vm) {
92 return MSG_host_get_name(vm);
96 /* **** Check state of a VM **** */
97 static inline int __MSG_vm_is_state(msg_vm_t vm, e_msg_vm_state_t state) {
98 return simcall_vm_get_state(vm) == state;
101 /** @brief Returns whether the given VM has just reated, not running.
104 int MSG_vm_is_created(msg_vm_t vm)
106 return __MSG_vm_is_state(vm, msg_vm_state_created);
109 /** @brief Returns whether the given VM is currently running
112 int MSG_vm_is_running(msg_vm_t vm)
114 return __MSG_vm_is_state(vm, msg_vm_state_running);
117 /** @brief Returns whether the given VM is currently migrating
120 int MSG_vm_is_migrating(msg_vm_t vm)
122 return __MSG_vm_is_state(vm, msg_vm_state_migrating);
125 /** @brief Returns whether the given VM is currently suspended, not running.
128 int MSG_vm_is_suspended(msg_vm_t vm)
130 return __MSG_vm_is_state(vm, msg_vm_state_suspended);
133 /** @brief Returns whether the given VM is being saved (FIXME: live saving or not?).
136 int MSG_vm_is_saving(msg_vm_t vm)
138 return __MSG_vm_is_state(vm, msg_vm_state_saving);
141 /** @brief Returns whether the given VM has been saved, not running.
144 int MSG_vm_is_saved(msg_vm_t vm)
146 return __MSG_vm_is_state(vm, msg_vm_state_saved);
149 /** @brief Returns whether the given VM is being restored, not running.
152 int MSG_vm_is_restoring(msg_vm_t vm)
154 return __MSG_vm_is_state(vm, msg_vm_state_restoring);
159 /* ------------------------------------------------------------------------- */
160 /* ------------------------------------------------------------------------- */
162 /* **** ******** MSG vm actions ********* **** */
164 /** @brief Create a new VM (the VM is just attached to the location but it is not started yet).
167 * Please note that a VM is a specific host. Hence, you should give a different name
170 msg_vm_t MSG_vm_create(msg_host_t ind_host, const char *name,
171 int core_nb, int mem_cap, int net_cap){
173 // Note new and vm_workstation refer to the same area (due to the lib/dict appraoch)
175 void *ind_vm_workstation = NULL;
176 // Ask simix to create the surf vm resource
177 ind_vm_workstation = simcall_vm_create(name,ind_host);
178 new = (msg_vm_t) __MSG_host_create(ind_vm_workstation);
180 MSG_vm_set_property_value(new, "CORE_NB", bprintf("%d", core_nb), free);
181 MSG_vm_set_property_value(new, "MEM_CAP", bprintf("%d", mem_cap), free);
182 MSG_vm_set_property_value(new, "NET_CAP", bprintf("%d", net_cap), free);
184 XBT_DEBUG("A new VM has been created");
185 // TODO check whether the vm (i.e the virtual host) has been correctly added into the list of all hosts.
188 TRACE_msg_vm_create(name, ind_host);
194 /** @brief Start a vm (ie. boot)
197 * If the VM cannot be started, an exception is generated.
200 void MSG_vm_start(msg_vm_t vm) {
202 //Please note that vm start can raise an exception if the VM cannot be started.
203 simcall_vm_start(vm);
206 TRACE_msg_vm_start(vm);
212 /** @brief Immediately kills all processes within the given VM. Any memory that they allocated will be leaked.
215 * FIXME: No extra delay occurs. If you want to simulate this too, you want to
216 * use a #MSG_process_sleep() or something. I'm not quite sure.
218 void MSG_vm_shutdown(msg_vm_t vm)
220 /* msg_vm_t equals to msg_host_t */
221 simcall_vm_shutdown(vm);
223 // #ifdef HAVE_TRACING
224 // TRACE_msg_vm_(vm);
229 /** @brief Migrate the VM to the given host.
232 * FIXME: No migration cost occurs. If you want to simulate this too, you want to use a
233 * MSG_task_send() before or after, depending on whether you want to do cold or hot
236 void MSG_vm_migrate(msg_vm_t vm, msg_host_t destination)
239 * - One approach is ...
240 * We first create a new VM (i.e., destination VM) on the destination
241 * physical host. The destination VM will receive the state of the source
242 * VM over network. We will finally destroy the source VM.
243 * - This behavior is similar to the way of migration in the real world.
244 * Even before a migration is completed, we will see a destination VM,
245 * consuming resources.
246 * - We have to relocate all processes. The existing process migraion code
247 * will work for this?
248 * - The name of the VM is a somewhat unique ID in the code. It is tricky
249 * for the destination VM?
251 * - Another one is ...
252 * We update the information of the given VM to place it to the destination
255 * The second one would be easier.
260 const char *old_pm_name = simcall_vm_get_phys_host(vm);
261 msg_host_t old_pm_ind = xbt_lib_get_elm_or_null(host_lib, old_pm_name);
264 simcall_vm_migrate(vm, destination);
268 TRACE_msg_vm_change_host(vm, old_pm_ind, destination);
273 /** @brief Immediately suspend the execution of all processes within the given VM.
276 * This function stops the exection of the VM. All the processes on this VM
277 * will pause. The state of the VM is perserved. We can later resume it again.
279 * FIXME: No suspension cost occurs. If you want to simulate this too, you want to
280 * use a \ref MSG_file_write() before or after, depending on the exact semantic
281 * of VM suspend to you.
283 void MSG_vm_suspend(msg_vm_t vm)
285 simcall_vm_suspend(vm);
288 TRACE_msg_vm_suspend(vm);
293 /** @brief Resume the execution of the VM. All processes on the VM run again.
296 * FIXME: No resume cost occurs. If you want to simulate this too, you want to
297 * use a \ref MSG_file_read() before or after, depending on the exact semantic
298 * of VM resume to you.
300 void MSG_vm_resume(msg_vm_t vm)
302 simcall_vm_resume(vm);
305 TRACE_msg_vm_resume(vm);
310 /** @brief Immediately save the execution of all processes within the given VM.
313 * This function stops the exection of the VM. All the processes on this VM
314 * will pause. The state of the VM is perserved. We can later resume it again.
316 * FIXME: No suspension cost occurs. If you want to simulate this too, you want to
317 * use a \ref MSG_file_write() before or after, depending on the exact semantic
320 void MSG_vm_save(msg_vm_t vm)
325 TRACE_msg_vm_save(vm);
330 /** @brief Restore the execution of the VM. All processes on the VM run again.
333 * FIXME: No restore cost occurs. If you want to simulate this too, you want to
334 * use a \ref MSG_file_read() before or after, depending on the exact semantic
335 * of VM restore to you.
337 void MSG_vm_restore(msg_vm_t vm)
339 simcall_vm_restore(vm);
342 TRACE_msg_vm_restore(vm);
347 /** @brief Destroy a VM. Destroy the VM object from the simulation.
350 void MSG_vm_destroy(msg_vm_t vm)
352 /* First, terminate all processes on the VM */
353 simcall_vm_shutdown(vm);
355 /* Then, destroy the VM object */
356 simcall_vm_destroy(vm);
359 TRACE_msg_vm_end(vm);