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");
13 /** @brief Create a new VM (the VM is just attached to the location but it is not started yet).
17 msg_vm_t MSG_vm_create(msg_host_t location, const char *name,
18 int core_nb, int mem_cap, int net_cap){
20 // Note new and vm_workstation refer to the same area (due to the lib/dict appraoch)
22 void *vm_workstation = NULL;
23 // Ask simix to create the surf vm resource
24 vm_workstation = simcall_vm_create(name,location);
25 new = (msg_vm_t) __MSG_host_create(vm_workstation);
28 MSG_vm_set_property_value(new, "CORE_NB", bprintf("%d", core_nb), free);
29 MSG_vm_set_property_value(new, "MEM_CAP", bprintf("%d", core_nb), free);
30 MSG_vm_set_property_value(new, "NET_CAP", bprintf("%d", core_nb), free);
33 TRACE_msg_vm_create(name, location);
38 /** \ingroup m_host_management
39 * \brief Returns the value of a given host property
42 * \param name a property name
43 * \return value of a property (or NULL if property not set)
45 const char *MSG_host_get_property_value(msg_host_t host, const char *name)
47 return xbt_dict_get_or_null(MSG_host_get_properties(host), name);
50 /** \ingroup m_host_management
51 * \brief Returns a xbt_dict_t consisting of the list of properties assigned to this host
54 * \return a dict containing the properties
56 xbt_dict_t MSG_host_get_properties(msg_host_t host)
58 xbt_assert((host != NULL), "Invalid parameters (host is NULL)");
60 return (simcall_host_get_properties(host));
63 /** \ingroup m_host_management
64 * \brief Change the value of a given host property
67 * \param name a property name
68 * \param value what to change the property to
69 * \param free_ctn the freeing function to use to kill the value on need
71 void MSG_vm_set_property_value(msg_vm_t vm, const char *name, void *value,void_f_pvoid_t free_ctn) {
73 xbt_dict_set(MSG_host_get_properties(vm), name, value,free_ctn);
76 /** @brief Immediately suspend the execution of all processes within the given VM.
78 * return wheter the VM has been correctly started (0) or not (<0)
81 int MSG_vm_start(msg_vm_t vm) {
82 // TODO Please complete the code
85 TRACE_msg_vm_start(vm);
89 /** @brief Returns a newly constructed dynar containing all existing VMs in the system.
92 * Don't forget to free the dynar after use.
94 xbt_dynar_t MSG_vms_as_dynar(void) {
95 xbt_dynar_t res = xbt_dynar_new(sizeof(msg_vm_t),NULL);
97 xbt_swag_foreach(vm,msg_global->vms) {
98 xbt_dynar_push(res,&vm);
103 /** @brief Returns whether the given VM is currently suspended
106 int MSG_vm_is_suspended(msg_vm_t vm) {
107 return vm->state == msg_vm_state_suspended;
109 /** @brief Returns whether the given VM is currently running
112 int MSG_vm_is_running(msg_vm_t vm) {
113 return vm->state == msg_vm_state_running;
115 /** @brief Add the given process into the VM.
118 * Afterward, when the VM is migrated or suspended or whatever, the process will have the corresponding handling, too.
121 void MSG_vm_bind(msg_vm_t vm, msg_process_t process) {
122 /* check if the process is already in a VM */
123 simdata_process_t simdata = simcall_process_get_data(process);
125 msg_vm_t old_vm = simdata->vm;
126 int pos = xbt_dynar_search(old_vm->processes,&process);
127 xbt_dynar_remove_at(old_vm->processes,pos, NULL);
129 /* check if the host is in the right host */
130 if (simdata->m_host != vm->location) {
131 MSG_process_migrate(process,vm->location);
135 XBT_DEBUG("binding Process %s to %p",MSG_process_get_name(process),vm);
137 xbt_dynar_push_as(vm->processes,msg_process_t,process);
139 /** @brief Removes the given process from the given VM, and kill it
142 * Will raise a not_found exception if the process were not binded to that VM
144 void MSG_vm_unbind(msg_vm_t vm, msg_process_t process) {
145 int pos = xbt_dynar_search(vm->processes,process);
146 xbt_dynar_remove_at(vm->processes,pos, NULL);
147 MSG_process_kill(process);
150 /** @brief Immediately change the host on which all processes are running.
153 * No migration cost occurs. If you want to simulate this too, you want to use a
154 * MSG_task_send() before or after, depending on whether you want to do cold or hot
157 void MSG_vm_migrate(msg_vm_t vm, msg_host_t destination) {
159 msg_process_t process;
160 xbt_dynar_foreach(vm->processes,cpt,process) {
161 MSG_process_migrate(process,destination);
163 xbt_swag_remove(vm, MSG_host_priv(vm->location)->vms);
164 xbt_swag_insert_at_tail(vm, MSG_host_priv(destination)->vms);
167 TRACE_msg_vm_change_host(vm,vm->location,destination);
170 vm->location = destination;
173 /** @brief Immediately suspend the execution of all processes within the given VM.
176 * No suspension cost occurs. If you want to simulate this too, you want to
177 * use a \ref MSG_file_write() before or after, depending on the exact semantic
178 * of VM suspend to you.
180 void MSG_vm_suspend(msg_vm_t vm) {
182 msg_process_t process;
183 xbt_dynar_foreach(vm->processes,cpt,process) {
184 XBT_DEBUG("suspend process %s of host %s",MSG_process_get_name(process),MSG_host_get_name(MSG_process_get_host(process)));
185 MSG_process_suspend(process);
189 TRACE_msg_vm_suspend(vm);
194 /** @brief Immediately resumes the execution of all processes within the given VM.
197 * No resume cost occurs. If you want to simulate this too, you want to
198 * use a \ref MSG_file_read() before or after, depending on the exact semantic
199 * of VM resume to you.
201 void MSG_vm_resume(msg_vm_t vm) {
203 msg_process_t process;
204 xbt_dynar_foreach(vm->processes,cpt,process) {
205 XBT_DEBUG("resume process %s of host %s",MSG_process_get_name(process),MSG_host_get_name(MSG_process_get_host(process)));
206 MSG_process_resume(process);
210 TRACE_msg_vm_resume(vm);
215 void MSG_vm_shutdown(msg_vm_t vm)
217 /* msg_vm_t equals to msg_host_t */
218 char *name = simcall_host_get_name(vm);
219 smx_host_t smx_host = xbt_lib_get_or_null(host_lib, name, SIMIX_HOST_LEVEL);
220 smx_process_t smx_process, smx_process_next;
222 XBT_DEBUG("%lu processes in the VM", xbt_swag_size(SIMIX_host_priv(smx_host)->process_list));
224 xbt_swag_foreach_safe(smx_process, SIMIX_host_priv(smx_host)->process_list) {
225 XBT_DEBUG("kill %s", SIMIX_host_get_name(smx_host));
226 simcall_process_kill(smx_process);
229 /* taka: not yet confirmed */
231 TRACE_msg_vm_kill(vm);
235 /* TODO: update the state of vm */
238 while (xbt_dynar_length(vm->processes) > 0) {
239 process = xbt_dynar_get_as(vm->processes,0,msg_process_t);
247 * \brief Reboot the VM, restarting all the processes in it.
249 void MSG_vm_reboot(msg_vm_t vm)
251 xbt_dynar_t new_processes = xbt_dynar_new(sizeof(msg_process_t),NULL);
253 msg_process_t process;
256 xbt_dynar_foreach(vm->processes,cpt,process) {
257 msg_process_t new_process = MSG_process_restart(process);
258 xbt_dynar_push_as(new_processes,msg_process_t,new_process);
262 xbt_dynar_foreach(new_processes, cpt, process) {
263 MSG_vm_bind(vm,process);
266 xbt_dynar_free(&new_processes);
269 /** @brief Destroy a msg_vm_t.
272 void MSG_vm_destroy(msg_vm_t vm) {
274 msg_process_t process;
275 xbt_dynar_foreach(vm->processes,cpt,process) {
277 simdata_process_t simdata = simcall_process_get_data(process);
282 TRACE_msg_vm_end(vm);
286 xbt_dynar_free(&vm->processes);