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. */
6 #include "msg_private.h"
7 #include "xbt/sysdep.h"
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_vm, msg,
11 "Cloud-oriented parts of the MSG API");
14 /* **** ******** GENERAL ********* **** */
16 /** \ingroup m_vm_management
17 * \brief Returns the value of a given vm property
20 * \param name a property name
21 * \return value of a property (or NULL if property not set)
24 const char *MSG_vm_get_property_value(msg_vm_t vm, const char *name)
26 return MSG_host_get_property_value(vm, name);
29 /** \ingroup m_vm_management
30 * \brief Returns a xbt_dict_t consisting of the list of properties assigned to this host
33 * \return a dict containing the properties
35 xbt_dict_t MSG_vm_get_properties(msg_vm_t vm)
37 xbt_assert((vm != NULL), "Invalid parameters (vm is NULL)");
39 return (simcall_host_get_properties(vm));
42 /** \ingroup m_host_management
43 * \brief Change the value of a given host property
46 * \param name a property name
47 * \param value what to change the property to
48 * \param free_ctn the freeing function to use to kill the value on need
50 void MSG_vm_set_property_value(msg_vm_t vm, const char *name, void *value,void_f_pvoid_t free_ctn) {
52 xbt_dict_set(MSG_host_get_properties(vm), name, value,free_ctn);
55 /** \ingroup msg_vm_management
56 * \brief Finds a msg_vm_t using its name.
58 * This is a name directory service
59 * \param name the name of a vm.
60 * \return the corresponding vm
62 * Please note that a VM is a specific host. Hence, you should give a different name
66 msg_vm_t MSG_vm_get_by_name(const char *name){
67 return MSG_get_host_by_name(name);
70 /** \ingroup m_vm_management
72 * \brief Return the name of the #msg_host_t.
74 * This functions checks whether \a host is a valid pointer or not and return
77 const char *MSG_vm_get_name(msg_vm_t vm) {
78 return MSG_host_get_name(vm);
81 /** @brief Returns a newly constructed dynar containing all existing VMs in the system.
84 * Don't forget to free the dynar after use.
86 xbt_dynar_t MSG_vms_as_dynar(void) {
87 xbt_dynar_t res = xbt_dynar_new(sizeof(msg_vm_t),NULL);
89 xbt_swag_foreach(vm,msg_global->vms) {
90 xbt_dynar_push(res,&vm);
95 /* **** ******** MSG vm actions ********* **** */
97 /** @brief Create a new VM (the VM is just attached to the location but it is not started yet).
100 * Please note that a VM is a specific host. Hence, you should give a different name
103 msg_vm_t MSG_vm_create(msg_host_t location, const char *name,
104 int core_nb, int mem_cap, int net_cap){
106 // Note new and vm_workstation refer to the same area (due to the lib/dict appraoch)
108 void *vm_workstation = NULL;
109 // Ask simix to create the surf vm resource
110 vm_workstation = simcall_vm_create(name,location);
111 new = (msg_vm_t) __MSG_host_create(vm_workstation);
113 MSG_vm_set_property_value(new, "CORE_NB", bprintf("%d", core_nb), free);
114 MSG_vm_set_property_value(new, "MEM_CAP", bprintf("%d", core_nb), free);
115 MSG_vm_set_property_value(new, "NET_CAP", bprintf("%d", core_nb), free);
117 XBT_DEBUG("A new VM has been created");
118 // TODO check whether the vm (i.e the virtual host) has been correctly added into the list of all hosts.
121 TRACE_msg_vm_create(name, location);
127 /** @brief Start a vm (ie. boot)
130 * If the VM cannot be started, an exception is generated.
133 void MSG_vm_start(msg_vm_t vm) {
135 //Please note that vm start can raise an exception if the VM cannot be started.
136 simcall_vm_start(vm);
139 TRACE_msg_vm_start(vm);
143 /* **** Check state of a VM **** */
144 int __MSG_vm_is_state(msg_vm_t vm, e_msg_vm_state_t state) {
145 return simcall_get_vm_state(vm) == state ;
148 /** @brief Returns whether the given VM is currently suspended
151 int MSG_vm_is_suspended(msg_vm_t vm) {
152 return __MSG_vm_is_state(msg_vm_state_suspended);
154 /** @brief Returns whether the given VM is currently running
157 int MSG_vm_is_running(msg_vm_t vm) {
158 return __MSG_vm_is_state(msg_vm_state_running);
161 // TODO Implement the functions for the different state
163 void MSG_vm_shutdown(msg_vm_t vm)
165 /* msg_vm_t equals to msg_host_t */
166 simcall_vm_shutdown(vm);
168 /* taka: not yet confirmed */
170 TRACE_msg_vm_kill(vm);
176 ///** @brief Add the given process into the VM.
177 // * @ingroup msg_VMs
179 // * Afterward, when the VM is migrated or suspended or whatever, the process will have the corresponding handling, too.
182 //void MSG_vm_bind(msg_vm_t vm, msg_process_t process) {
183 // /* check if the process is already in a VM */
184 // simdata_process_t simdata = simcall_process_get_data(process);
185 // if (simdata->vm) {
186 // msg_vm_t old_vm = simdata->vm;
187 // int pos = xbt_dynar_search(old_vm->processes,&process);
188 // xbt_dynar_remove_at(old_vm->processes,pos, NULL);
190 // /* check if the host is in the right host */
191 // if (simdata->m_host != vm->location) {
192 // MSG_process_migrate(process,vm->location);
196 // XBT_DEBUG("binding Process %s to %p",MSG_process_get_name(process),vm);
198 // xbt_dynar_push_as(vm->processes,msg_process_t,process);
200 ///** @brief Removes the given process from the given VM, and kill it
201 // * @ingroup msg_VMs
203 // * Will raise a not_found exception if the process were not binded to that VM
205 //void MSG_vm_unbind(msg_vm_t vm, msg_process_t process) {
206 // int pos = xbt_dynar_search(vm->processes,process);
207 // xbt_dynar_remove_at(vm->processes,pos, NULL);
208 // MSG_process_kill(process);
211 ///** @brief Immediately change the host on which all processes are running.
212 // * @ingroup msg_VMs
214 // * No migration cost occurs. If you want to simulate this too, you want to use a
215 // * MSG_task_send() before or after, depending on whether you want to do cold or hot
218 //void MSG_vm_migrate(msg_vm_t vm, msg_host_t destination) {
220 // msg_process_t process;
221 // xbt_dynar_foreach(vm->processes,cpt,process) {
222 // MSG_process_migrate(process,destination);
224 // xbt_swag_remove(vm, MSG_host_priv(vm->location)->vms);
225 // xbt_swag_insert_at_tail(vm, MSG_host_priv(destination)->vms);
227 // #ifdef HAVE_TRACING
228 // TRACE_msg_vm_change_host(vm,vm->location,destination);
231 // vm->location = destination;
234 ///** @brief Immediately suspend the execution of all processes within the given VM.
235 // * @ingroup msg_VMs
237 // * No suspension cost occurs. If you want to simulate this too, you want to
238 // * use a \ref MSG_file_write() before or after, depending on the exact semantic
239 // * of VM suspend to you.
241 //void MSG_vm_suspend(msg_vm_t vm) {
243 // msg_process_t process;
244 // xbt_dynar_foreach(vm->processes,cpt,process) {
245 // XBT_DEBUG("suspend process %s of host %s",MSG_process_get_name(process),MSG_host_get_name(MSG_process_get_host(process)));
246 // MSG_process_suspend(process);
249 // #ifdef HAVE_TRACING
250 // TRACE_msg_vm_suspend(vm);
255 ///** @brief Immediately resumes the execution of all processes within the given VM.
256 // * @ingroup msg_VMs
258 // * No resume cost occurs. If you want to simulate this too, you want to
259 // * use a \ref MSG_file_read() before or after, depending on the exact semantic
260 // * of VM resume to you.
262 //void MSG_vm_resume(msg_vm_t vm) {
264 // msg_process_t process;
265 // xbt_dynar_foreach(vm->processes,cpt,process) {
266 // XBT_DEBUG("resume process %s of host %s",MSG_process_get_name(process),MSG_host_get_name(MSG_process_get_host(process)));
267 // MSG_process_resume(process);
270 // #ifdef HAVE_TRACING
271 // TRACE_msg_vm_resume(vm);
277 // * \ingroup msg_VMs
278 // * \brief Reboot the VM, restarting all the processes in it.
280 //void MSG_vm_reboot(msg_vm_t vm)
282 // xbt_dynar_t new_processes = xbt_dynar_new(sizeof(msg_process_t),NULL);
284 // msg_process_t process;
287 // xbt_dynar_foreach(vm->processes,cpt,process) {
288 // msg_process_t new_process = MSG_process_restart(process);
289 // xbt_dynar_push_as(new_processes,msg_process_t,new_process);
293 // xbt_dynar_foreach(new_processes, cpt, process) {
294 // MSG_vm_bind(vm,process);
297 // xbt_dynar_free(&new_processes);
300 ///** @brief Destroy a msg_vm_t.
301 // * @ingroup msg_VMs
303 //void MSG_vm_destroy(msg_vm_t vm)
305 // simcall_vm_destroy(vm);
309 // msg_process_t process;
310 // xbt_dynar_foreach(vm->processes,cpt,process) {
312 // simdata_process_t simdata = simcall_process_get_data(process);
313 // simdata->vm = NULL;
316 // #ifdef HAVE_TRACING
317 // TRACE_msg_vm_end(vm);
321 // xbt_dynar_free(&vm->processes);