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 // TODO check whether the vm (i.e the virtual host) has been correctly added into the list of all hosts.
120 TRACE_msg_vm_create(name, location);
126 /** @brief Start a vm (ie. boot)
129 * If the VM cannot be started, an exception is generated.
132 void MSG_vm_start(msg_vm_t vm) {
134 //Please note that vm start can raise an exception if the VM cannot be started.
135 simcall_vm_start(vm);
138 TRACE_msg_vm_start(vm);
142 /* **** Check state of a VM **** */
143 int __MSG_vm_is_state(msg_vm_t vm, e_msg_vm_state_t state) {
144 return simcall_get_vm_state(vm) == state ;
147 /** @brief Returns whether the given VM is currently suspended
150 int MSG_vm_is_suspended(msg_vm_t vm) {
151 return __MSG_vm_is_state(msg_vm_state_suspended);
153 /** @brief Returns whether the given VM is currently running
156 int MSG_vm_is_running(msg_vm_t vm) {
157 return __MSG_vm_is_state(msg_vm_state_running);
160 // TODO Implement the functions for the different state
162 void MSG_vm_shutdown(msg_vm_t vm)
164 /* msg_vm_t equals to msg_host_t */
165 simcall_vm_shutdown(vm);
167 /* taka: not yet confirmed */
169 TRACE_msg_vm_kill(vm);
175 ///** @brief Add the given process into the VM.
176 // * @ingroup msg_VMs
178 // * Afterward, when the VM is migrated or suspended or whatever, the process will have the corresponding handling, too.
181 //void MSG_vm_bind(msg_vm_t vm, msg_process_t process) {
182 // /* check if the process is already in a VM */
183 // simdata_process_t simdata = simcall_process_get_data(process);
184 // if (simdata->vm) {
185 // msg_vm_t old_vm = simdata->vm;
186 // int pos = xbt_dynar_search(old_vm->processes,&process);
187 // xbt_dynar_remove_at(old_vm->processes,pos, NULL);
189 // /* check if the host is in the right host */
190 // if (simdata->m_host != vm->location) {
191 // MSG_process_migrate(process,vm->location);
195 // XBT_DEBUG("binding Process %s to %p",MSG_process_get_name(process),vm);
197 // xbt_dynar_push_as(vm->processes,msg_process_t,process);
199 ///** @brief Removes the given process from the given VM, and kill it
200 // * @ingroup msg_VMs
202 // * Will raise a not_found exception if the process were not binded to that VM
204 //void MSG_vm_unbind(msg_vm_t vm, msg_process_t process) {
205 // int pos = xbt_dynar_search(vm->processes,process);
206 // xbt_dynar_remove_at(vm->processes,pos, NULL);
207 // MSG_process_kill(process);
210 ///** @brief Immediately change the host on which all processes are running.
211 // * @ingroup msg_VMs
213 // * No migration cost occurs. If you want to simulate this too, you want to use a
214 // * MSG_task_send() before or after, depending on whether you want to do cold or hot
217 //void MSG_vm_migrate(msg_vm_t vm, msg_host_t destination) {
219 // msg_process_t process;
220 // xbt_dynar_foreach(vm->processes,cpt,process) {
221 // MSG_process_migrate(process,destination);
223 // xbt_swag_remove(vm, MSG_host_priv(vm->location)->vms);
224 // xbt_swag_insert_at_tail(vm, MSG_host_priv(destination)->vms);
226 // #ifdef HAVE_TRACING
227 // TRACE_msg_vm_change_host(vm,vm->location,destination);
230 // vm->location = destination;
233 ///** @brief Immediately suspend the execution of all processes within the given VM.
234 // * @ingroup msg_VMs
236 // * No suspension cost occurs. If you want to simulate this too, you want to
237 // * use a \ref MSG_file_write() before or after, depending on the exact semantic
238 // * of VM suspend to you.
240 //void MSG_vm_suspend(msg_vm_t vm) {
242 // msg_process_t process;
243 // xbt_dynar_foreach(vm->processes,cpt,process) {
244 // XBT_DEBUG("suspend process %s of host %s",MSG_process_get_name(process),MSG_host_get_name(MSG_process_get_host(process)));
245 // MSG_process_suspend(process);
248 // #ifdef HAVE_TRACING
249 // TRACE_msg_vm_suspend(vm);
254 ///** @brief Immediately resumes the execution of all processes within the given VM.
255 // * @ingroup msg_VMs
257 // * No resume cost occurs. If you want to simulate this too, you want to
258 // * use a \ref MSG_file_read() before or after, depending on the exact semantic
259 // * of VM resume to you.
261 //void MSG_vm_resume(msg_vm_t vm) {
263 // msg_process_t process;
264 // xbt_dynar_foreach(vm->processes,cpt,process) {
265 // XBT_DEBUG("resume process %s of host %s",MSG_process_get_name(process),MSG_host_get_name(MSG_process_get_host(process)));
266 // MSG_process_resume(process);
269 // #ifdef HAVE_TRACING
270 // TRACE_msg_vm_resume(vm);
276 // * \ingroup msg_VMs
277 // * \brief Reboot the VM, restarting all the processes in it.
279 //void MSG_vm_reboot(msg_vm_t vm)
281 // xbt_dynar_t new_processes = xbt_dynar_new(sizeof(msg_process_t),NULL);
283 // msg_process_t process;
286 // xbt_dynar_foreach(vm->processes,cpt,process) {
287 // msg_process_t new_process = MSG_process_restart(process);
288 // xbt_dynar_push_as(new_processes,msg_process_t,new_process);
292 // xbt_dynar_foreach(new_processes, cpt, process) {
293 // MSG_vm_bind(vm,process);
296 // xbt_dynar_free(&new_processes);
299 ///** @brief Destroy a msg_vm_t.
300 // * @ingroup msg_VMs
302 //void MSG_vm_destroy(msg_vm_t vm)
304 // simcall_vm_destroy(vm);
308 // msg_process_t process;
309 // xbt_dynar_foreach(vm->processes,cpt,process) {
311 // simdata_process_t simdata = simcall_process_get_data(process);
312 // simdata->vm = NULL;
315 // #ifdef HAVE_TRACING
316 // TRACE_msg_vm_end(vm);
320 // xbt_dynar_free(&vm->processes);