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);
71 /** @brief Returns a newly constructed dynar containing all existing VMs in the system.
74 * Don't forget to free the dynar after use.
76 xbt_dynar_t MSG_vms_as_dynar(void) {
77 xbt_dynar_t res = xbt_dynar_new(sizeof(msg_vm_t),NULL);
79 xbt_swag_foreach(vm,msg_global->vms) {
80 xbt_dynar_push(res,&vm);
85 /* **** ******** MSG vm actions ********* **** */
87 /** @brief Create a new VM (the VM is just attached to the location but it is not started yet).
90 * Please note that a VM is a specific host. Hence, you should give a different name
93 msg_vm_t MSG_vm_create(msg_host_t location, const char *name,
94 int core_nb, int mem_cap, int net_cap){
96 // Note new and vm_workstation refer to the same area (due to the lib/dict appraoch)
98 void *vm_workstation = NULL;
99 // Ask simix to create the surf vm resource
100 vm_workstation = simcall_vm_create(name,location);
101 new = (msg_vm_t) __MSG_host_create(vm_workstation);
103 MSG_vm_set_property_value(new, "CORE_NB", bprintf("%d", core_nb), free);
104 MSG_vm_set_property_value(new, "MEM_CAP", bprintf("%d", core_nb), free);
105 MSG_vm_set_property_value(new, "NET_CAP", bprintf("%d", core_nb), free);
107 // TODO check whether the vm (i.e the virtual host) has been correctly added into the list of all hosts.
110 TRACE_msg_vm_create(name, location);
116 /** @brief Start a vm (ie. boot)
119 * If the VM cannot be started, an exception is generated.
122 void MSG_vm_start(msg_vm_t vm) {
124 //Please note that vm start can raise an exception if the VM cannot be started.
125 simcall_vm_start(vm);
128 TRACE_msg_vm_start(vm);
132 /* **** Check state of a VM **** */
133 int __MSG_vm_is_state(msg_vm_t vm, e_msg_vm_state_t state) {
134 return simcall_get_vm_state(vm) == state ;
137 /** @brief Returns whether the given VM is currently suspended
140 int MSG_vm_is_suspended(msg_vm_t vm) {
141 return __MSG_vm_is_state(msg_vm_state_suspended);
143 /** @brief Returns whether the given VM is currently running
146 int MSG_vm_is_running(msg_vm_t vm) {
147 return __MSG_vm_is_state(msg_vm_state_running);
150 // TODO complete the different state
152 /** @brief Add the given process into the VM.
155 * Afterward, when the VM is migrated or suspended or whatever, the process will have the corresponding handling, too.
158 void MSG_vm_bind(msg_vm_t vm, msg_process_t process) {
159 /* check if the process is already in a VM */
160 simdata_process_t simdata = simcall_process_get_data(process);
162 msg_vm_t old_vm = simdata->vm;
163 int pos = xbt_dynar_search(old_vm->processes,&process);
164 xbt_dynar_remove_at(old_vm->processes,pos, NULL);
166 /* check if the host is in the right host */
167 if (simdata->m_host != vm->location) {
168 MSG_process_migrate(process,vm->location);
172 XBT_DEBUG("binding Process %s to %p",MSG_process_get_name(process),vm);
174 xbt_dynar_push_as(vm->processes,msg_process_t,process);
176 /** @brief Removes the given process from the given VM, and kill it
179 * Will raise a not_found exception if the process were not binded to that VM
181 void MSG_vm_unbind(msg_vm_t vm, msg_process_t process) {
182 int pos = xbt_dynar_search(vm->processes,process);
183 xbt_dynar_remove_at(vm->processes,pos, NULL);
184 MSG_process_kill(process);
187 /** @brief Immediately change the host on which all processes are running.
190 * No migration cost occurs. If you want to simulate this too, you want to use a
191 * MSG_task_send() before or after, depending on whether you want to do cold or hot
194 void MSG_vm_migrate(msg_vm_t vm, msg_host_t destination) {
196 msg_process_t process;
197 xbt_dynar_foreach(vm->processes,cpt,process) {
198 MSG_process_migrate(process,destination);
200 xbt_swag_remove(vm, MSG_host_priv(vm->location)->vms);
201 xbt_swag_insert_at_tail(vm, MSG_host_priv(destination)->vms);
204 TRACE_msg_vm_change_host(vm,vm->location,destination);
207 vm->location = destination;
210 /** @brief Immediately suspend the execution of all processes within the given VM.
213 * No suspension cost occurs. If you want to simulate this too, you want to
214 * use a \ref MSG_file_write() before or after, depending on the exact semantic
215 * of VM suspend to you.
217 void MSG_vm_suspend(msg_vm_t vm) {
219 msg_process_t process;
220 xbt_dynar_foreach(vm->processes,cpt,process) {
221 XBT_DEBUG("suspend process %s of host %s",MSG_process_get_name(process),MSG_host_get_name(MSG_process_get_host(process)));
222 MSG_process_suspend(process);
226 TRACE_msg_vm_suspend(vm);
231 /** @brief Immediately resumes the execution of all processes within the given VM.
234 * No resume cost occurs. If you want to simulate this too, you want to
235 * use a \ref MSG_file_read() before or after, depending on the exact semantic
236 * of VM resume to you.
238 void MSG_vm_resume(msg_vm_t vm) {
240 msg_process_t process;
241 xbt_dynar_foreach(vm->processes,cpt,process) {
242 XBT_DEBUG("resume process %s of host %s",MSG_process_get_name(process),MSG_host_get_name(MSG_process_get_host(process)));
243 MSG_process_resume(process);
247 TRACE_msg_vm_resume(vm);
252 void MSG_vm_shutdown(msg_vm_t vm)
254 /* msg_vm_t equals to msg_host_t */
255 char *name = simcall_host_get_name(vm);
256 smx_host_t smx_host = xbt_lib_get_or_null(host_lib, name, SIMIX_HOST_LEVEL);
257 smx_process_t smx_process, smx_process_next;
259 XBT_DEBUG("%lu processes in the VM", xbt_swag_size(SIMIX_host_priv(smx_host)->process_list));
261 xbt_swag_foreach_safe(smx_process, SIMIX_host_priv(smx_host)->process_list) {
262 XBT_DEBUG("kill %s", SIMIX_host_get_name(smx_host));
263 simcall_process_kill(smx_process);
266 /* taka: not yet confirmed */
268 TRACE_msg_vm_kill(vm);
272 /* TODO: update the state of vm */
275 while (xbt_dynar_length(vm->processes) > 0) {
276 process = xbt_dynar_get_as(vm->processes,0,msg_process_t);
284 * \brief Reboot the VM, restarting all the processes in it.
286 void MSG_vm_reboot(msg_vm_t vm)
288 xbt_dynar_t new_processes = xbt_dynar_new(sizeof(msg_process_t),NULL);
290 msg_process_t process;
293 xbt_dynar_foreach(vm->processes,cpt,process) {
294 msg_process_t new_process = MSG_process_restart(process);
295 xbt_dynar_push_as(new_processes,msg_process_t,new_process);
299 xbt_dynar_foreach(new_processes, cpt, process) {
300 MSG_vm_bind(vm,process);
303 xbt_dynar_free(&new_processes);
306 /** @brief Destroy a msg_vm_t.
309 void MSG_vm_destroy(msg_vm_t vm) {
311 msg_process_t process;
312 xbt_dynar_foreach(vm->processes,cpt,process) {
314 simdata_process_t simdata = simcall_process_get_data(process);
319 TRACE_msg_vm_end(vm);
323 xbt_dynar_free(&vm->processes);