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. */
8 // 1./ check how and where a new VM is added to the list of the hosts
9 // 2./ MSG_TRACE can be revisited in order to use the host
12 #include "msg_private.h"
13 #include "xbt/sysdep.h"
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_vm, msg,
17 "Cloud-oriented parts of the MSG API");
20 /* **** ******** GENERAL ********* **** */
22 /** \ingroup m_vm_management
23 * \brief Returns the value of a given vm property
26 * \param name a property name
27 * \return value of a property (or NULL if property not set)
30 const char *MSG_vm_get_property_value(msg_vm_t vm, const char *name)
32 return MSG_host_get_property_value(vm, name);
35 /** \ingroup m_vm_management
36 * \brief Returns a xbt_dict_t consisting of the list of properties assigned to this host
39 * \return a dict containing the properties
41 xbt_dict_t MSG_vm_get_properties(msg_vm_t vm)
43 xbt_assert((vm != NULL), "Invalid parameters (vm is NULL)");
45 return (simcall_host_get_properties(vm));
48 /** \ingroup m_host_management
49 * \brief Change the value of a given host property
52 * \param name a property name
53 * \param value what to change the property to
54 * \param free_ctn the freeing function to use to kill the value on need
56 void MSG_vm_set_property_value(msg_vm_t vm, const char *name, void *value,void_f_pvoid_t free_ctn) {
58 xbt_dict_set(MSG_host_get_properties(vm), name, value,free_ctn);
61 /** \ingroup msg_vm_management
62 * \brief Finds a msg_vm_t using its name.
64 * This is a name directory service
65 * \param name the name of a vm.
66 * \return the corresponding vm
68 * Please note that a VM is a specific host. Hence, you should give a different name
72 msg_vm_t MSG_vm_get_by_name(const char *name){
73 return MSG_get_host_by_name(name);
76 /** \ingroup m_vm_management
78 * \brief Return the name of the #msg_host_t.
80 * This functions checks whether \a host is a valid pointer or not and return
83 const char *MSG_vm_get_name(msg_vm_t vm) {
84 return MSG_host_get_name(vm);
87 /* **** ******** MSG vm actions ********* **** */
89 /** @brief Create a new VM (the VM is just attached to the location but it is not started yet).
92 * Please note that a VM is a specific host. Hence, you should give a different name
95 msg_vm_t MSG_vm_create(msg_host_t ind_host, const char *name,
96 int core_nb, int mem_cap, int net_cap){
98 // Note new and vm_workstation refer to the same area (due to the lib/dict appraoch)
100 void *ind_vm_workstation = NULL;
101 // Ask simix to create the surf vm resource
102 ind_vm_workstation = simcall_vm_ws_create(name,ind_host);
103 new = (msg_vm_t) __MSG_host_create(ind_vm_workstation);
105 MSG_vm_set_property_value(new, "CORE_NB", bprintf("%d", core_nb), free);
106 MSG_vm_set_property_value(new, "MEM_CAP", bprintf("%d", core_nb), free);
107 MSG_vm_set_property_value(new, "NET_CAP", bprintf("%d", core_nb), free);
109 XBT_DEBUG("A new VM has been created");
110 // TODO check whether the vm (i.e the virtual host) has been correctly added into the list of all hosts.
113 TRACE_msg_vm_create(name, ind_host);
119 /** @brief Start a vm (ie. boot)
122 * If the VM cannot be started, an exception is generated.
125 void MSG_vm_start(msg_vm_t vm) {
127 //Please note that vm start can raise an exception if the VM cannot be started.
128 simcall_vm_start(vm);
131 TRACE_msg_vm_start(vm);
135 /* **** Check state of a VM **** */
136 int __MSG_vm_is_state(msg_vm_t vm, e_msg_vm_state_t state) {
137 return simcall_get_vm_state(vm) == state ;
140 /** @brief Returns whether the given VM is currently suspended
143 int MSG_vm_is_suspended(msg_vm_t vm) {
144 return __MSG_vm_is_state(vm, msg_vm_state_suspended);
146 /** @brief Returns whether the given VM is currently running
149 int MSG_vm_is_running(msg_vm_t vm) {
150 return __MSG_vm_is_state(vm, msg_vm_state_running);
153 // TODO Implement the functions for the different state
155 void MSG_vm_shutdown(msg_vm_t vm)
157 /* msg_vm_t equals to msg_host_t */
158 simcall_vm_shutdown(vm);
160 // #ifdef HAVE_TRACING
161 // TRACE_msg_vm_(vm);
166 ///** @brief Add the given process into the VM.
167 // * @ingroup msg_VMs
169 // * Afterward, when the VM is migrated or suspended or whatever, the process will have the corresponding handling, too.
172 //void MSG_vm_bind(msg_vm_t vm, msg_process_t process) {
173 // /* check if the process is already in a VM */
174 // simdata_process_t simdata = simcall_process_get_data(process);
175 // if (simdata->vm) {
176 // msg_vm_t old_vm = simdata->vm;
177 // int pos = xbt_dynar_search(old_vm->processes,&process);
178 // xbt_dynar_remove_at(old_vm->processes,pos, NULL);
180 // /* check if the host is in the right host */
181 // if (simdata->m_host != vm->location) {
182 // MSG_process_migrate(process,vm->location);
186 // XBT_DEBUG("binding Process %s to %p",MSG_process_get_name(process),vm);
188 // xbt_dynar_push_as(vm->processes,msg_process_t,process);
190 ///** @brief Removes the given process from the given VM, and kill it
191 // * @ingroup msg_VMs
193 // * Will raise a not_found exception if the process were not binded to that VM
195 //void MSG_vm_unbind(msg_vm_t vm, msg_process_t process) {
196 // int pos = xbt_dynar_search(vm->processes,process);
197 // xbt_dynar_remove_at(vm->processes,pos, NULL);
198 // MSG_process_kill(process);
201 ///** @brief Immediately change the host on which all processes are running.
202 // * @ingroup msg_VMs
204 // * No migration cost occurs. If you want to simulate this too, you want to use a
205 // * MSG_task_send() before or after, depending on whether you want to do cold or hot
208 //void MSG_vm_migrate(msg_vm_t vm, msg_host_t destination) {
210 // msg_process_t process;
211 // xbt_dynar_foreach(vm->processes,cpt,process) {
212 // MSG_process_migrate(process,destination);
214 // xbt_swag_remove(vm, MSG_host_priv(vm->location)->vms);
215 // xbt_swag_insert_at_tail(vm, MSG_host_priv(destination)->vms);
217 // #ifdef HAVE_TRACING
218 // TRACE_msg_vm_change_host(vm,vm->location,destination);
221 // vm->location = destination;
225 /** @brief Immediately suspend the execution of all processes within the given VM.
228 * No suspension cost occurs. If you want to simulate this too, you want to
229 * use a \ref MSG_file_write() before or after, depending on the exact semantic
230 * of VM suspend to you.
232 void MSG_vm_suspend(msg_vm_t vm)
234 simcall_vm_suspend(vm);
237 TRACE_msg_vm_suspend(vm);
241 msg_process_t process;
242 xbt_dynar_foreach(vm->processes,cpt,process) {
243 XBT_DEBUG("suspend process %s of host %s",MSG_process_get_name(process),MSG_host_get_name(MSG_process_get_host(process)));
244 MSG_process_suspend(process);
251 ///** @brief Immediately resumes the execution of all processes within the given VM.
252 // * @ingroup msg_VMs
254 // * No resume cost occurs. If you want to simulate this too, you want to
255 // * use a \ref MSG_file_read() before or after, depending on the exact semantic
256 // * of VM resume to you.
258 //void MSG_vm_resume(msg_vm_t vm) {
260 // msg_process_t process;
261 // xbt_dynar_foreach(vm->processes,cpt,process) {
262 // XBT_DEBUG("resume process %s of host %s",MSG_process_get_name(process),MSG_host_get_name(MSG_process_get_host(process)));
263 // MSG_process_resume(process);
266 // #ifdef HAVE_TRACING
267 // TRACE_msg_vm_resume(vm);
273 // * \ingroup msg_VMs
274 // * \brief Reboot the VM, restarting all the processes in it.
276 //void MSG_vm_reboot(msg_vm_t vm)
278 // xbt_dynar_t new_processes = xbt_dynar_new(sizeof(msg_process_t),NULL);
280 // msg_process_t process;
283 // xbt_dynar_foreach(vm->processes,cpt,process) {
284 // msg_process_t new_process = MSG_process_restart(process);
285 // xbt_dynar_push_as(new_processes,msg_process_t,new_process);
289 // xbt_dynar_foreach(new_processes, cpt, process) {
290 // MSG_vm_bind(vm,process);
293 // xbt_dynar_free(&new_processes);
297 /** @brief Destroy a VM. Destroy the VM object from the simulation.
300 void MSG_vm_destroy(msg_vm_t vm)
302 /* First, terminate all processes on the VM */
303 simcall_vm_shutdown(vm);
305 /* Then, destroy the VM object */
306 simcall_vm_destroy(vm);
309 TRACE_msg_vm_end(vm);
314 msg_process_t process;
315 xbt_dynar_foreach(vm->processes,cpt,process) {
317 simdata_process_t simdata = simcall_process_get_data(process);
321 xbt_dynar_free(&vm->processes);