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 (empty) VMs.
16 * @bug it is expected that in the future, the coreAmount parameter will be used
17 * to add extra constraints on the execution, but the argument is ignored for now.
20 msg_vm_t MSG_vm_create(msg_host_t location, const char *name,
21 int core_nb, int mem_cap, int net_cap){
23 // Note new and vm_workstation refer to the same area (due to the lib/dict appraoch)
25 void *vm_workstation = NULL;
26 // Ask simix to create the surf vm resource
27 vm_workstation = simcall_vm_create(name,location);
28 new = (msg_vm_t) __MSG_host_create(vm_workstation);
31 MSG_vm_set_property_value(new, "CORE_NB", bprintf("%d", core_nb)), NULL);
32 MSG_vm_set_property_value(new, "MEM_CAP", bprintf("%d", core_nb)), NULL);
33 MSG_vm_set_property_value(new, "NET_CAP", bprintf("%d", core_nb)), NULL);
36 TRACE_msg_vm_create(name, location);
41 /** \ingroup m_host_management
42 * \brief Returns the value of a given host property
45 * \param name a property name
46 * \return value of a property (or NULL if property not set)
48 const char *MSG_host_get_property_value(msg_host_t host, const char *name)
50 return xbt_dict_get_or_null(MSG_host_get_properties(host), name);
53 /** \ingroup m_host_management
54 * \brief Returns a xbt_dict_t consisting of the list of properties assigned to this host
57 * \return a dict containing the properties
59 xbt_dict_t MSG_host_get_properties(msg_host_t host)
61 xbt_assert((host != NULL), "Invalid parameters (host is NULL)");
63 return (simcall_host_get_properties(host));
66 /** \ingroup m_host_management
67 * \brief Change the value of a given host property
70 * \param name a property name
71 * \param value what to change the property to
72 * \param free_ctn the freeing function to use to kill the value on need
74 void MSG_vm_set_property_value(msg_vm_t vm, const char *name, void *value,void_f_pvoid_t free_ctn) {
76 xbt_dict_set(MSG_host_get_properties(vm), name, value,free_ctn);
79 /** @brief Immediately suspend the execution of all processes within the given VM.
81 * return wheter the VM has been correctly started (0) or not (<0)
84 int MSG_vm_start(msg_vm_t vm) {
85 // TODO Please complete the code
88 TRACE_msg_vm_start(vm);
92 /** @brief Returns a newly constructed dynar containing all existing VMs in the system.
95 * Don't forget to free the dynar after use.
97 xbt_dynar_t MSG_vms_as_dynar(void) {
98 xbt_dynar_t res = xbt_dynar_new(sizeof(msg_vm_t),NULL);
100 xbt_swag_foreach(vm,msg_global->vms) {
101 xbt_dynar_push(res,&vm);
106 /** @brief Returns whether the given VM is currently suspended
109 int MSG_vm_is_suspended(msg_vm_t vm) {
110 return vm->state == msg_vm_state_suspended;
112 /** @brief Returns whether the given VM is currently running
115 int MSG_vm_is_running(msg_vm_t vm) {
116 return vm->state == msg_vm_state_running;
118 /** @brief Add the given process into the VM.
121 * Afterward, when the VM is migrated or suspended or whatever, the process will have the corresponding handling, too.
124 void MSG_vm_bind(msg_vm_t vm, msg_process_t process) {
125 /* check if the process is already in a VM */
126 simdata_process_t simdata = simcall_process_get_data(process);
128 msg_vm_t old_vm = simdata->vm;
129 int pos = xbt_dynar_search(old_vm->processes,&process);
130 xbt_dynar_remove_at(old_vm->processes,pos, NULL);
132 /* check if the host is in the right host */
133 if (simdata->m_host != vm->location) {
134 MSG_process_migrate(process,vm->location);
138 XBT_DEBUG("binding Process %s to %p",MSG_process_get_name(process),vm);
140 xbt_dynar_push_as(vm->processes,msg_process_t,process);
142 /** @brief Removes the given process from the given VM, and kill it
145 * Will raise a not_found exception if the process were not binded to that VM
147 void MSG_vm_unbind(msg_vm_t vm, msg_process_t process) {
148 int pos = xbt_dynar_search(vm->processes,process);
149 xbt_dynar_remove_at(vm->processes,pos, NULL);
150 MSG_process_kill(process);
153 /** @brief Immediately change the host on which all processes are running.
156 * No migration cost occurs. If you want to simulate this too, you want to use a
157 * MSG_task_send() before or after, depending on whether you want to do cold or hot
160 void MSG_vm_migrate(msg_vm_t vm, msg_host_t destination) {
162 msg_process_t process;
163 xbt_dynar_foreach(vm->processes,cpt,process) {
164 MSG_process_migrate(process,destination);
166 xbt_swag_remove(vm, MSG_host_priv(vm->location)->vms);
167 xbt_swag_insert_at_tail(vm, MSG_host_priv(destination)->vms);
170 TRACE_msg_vm_change_host(vm,vm->location,destination);
173 vm->location = destination;
176 /** @brief Immediately suspend the execution of all processes within the given VM.
179 * No suspension cost occurs. If you want to simulate this too, you want to
180 * use a \ref MSG_file_write() before or after, depending on the exact semantic
181 * of VM suspend to you.
183 void MSG_vm_suspend(msg_vm_t vm) {
185 msg_process_t process;
186 xbt_dynar_foreach(vm->processes,cpt,process) {
187 XBT_DEBUG("suspend process %s of host %s",MSG_process_get_name(process),MSG_host_get_name(MSG_process_get_host(process)));
188 MSG_process_suspend(process);
192 TRACE_msg_vm_suspend(vm);
196 /** @brief Immediately resumes the execution of all processes within the given VM.
199 * No resume cost occurs. If you want to simulate this too, you want to
200 * use a \ref MSG_file_read() before or after, depending on the exact semantic
201 * of VM resume to you.
203 void MSG_vm_resume(msg_vm_t vm) {
205 msg_process_t process;
206 xbt_dynar_foreach(vm->processes,cpt,process) {
207 XBT_DEBUG("resume process %s of host %s",MSG_process_get_name(process),MSG_host_get_name(MSG_process_get_host(process)));
208 MSG_process_resume(process);
212 TRACE_msg_vm_resume(vm);
216 /** @brief Immediately kills all processes within the given VM. Any memory that they allocated will be leaked.
219 * No extra delay occurs. If you want to simulate this too, you want to
220 * use a #MSG_process_sleep() or something. I'm not quite sure.
222 void MSG_vm_shutdown(msg_vm_t vm)
224 msg_process_t process;
225 XBT_DEBUG("%lu processes in the VM", xbt_dynar_length(vm->processes));
226 while (xbt_dynar_length(vm->processes) > 0) {
227 process = xbt_dynar_get_as(vm->processes,0,msg_process_t);
228 MSG_process_kill(process);
232 TRACE_msg_vm_kill(vm);
238 * \brief Reboot the VM, restarting all the processes in it.
240 void MSG_vm_reboot(msg_vm_t vm)
242 xbt_dynar_t new_processes = xbt_dynar_new(sizeof(msg_process_t),NULL);
244 msg_process_t process;
247 xbt_dynar_foreach(vm->processes,cpt,process) {
248 msg_process_t new_process = MSG_process_restart(process);
249 xbt_dynar_push_as(new_processes,msg_process_t,new_process);
253 xbt_dynar_foreach(new_processes, cpt, process) {
254 MSG_vm_bind(vm,process);
257 xbt_dynar_free(&new_processes);
259 /** @brief Destroy a msg_vm_t.
262 void MSG_vm_destroy(msg_vm_t vm) {
264 msg_process_t process;
265 xbt_dynar_foreach(vm->processes,cpt,process) {
267 simdata_process_t simdata = simcall_process_get_data(process);
272 TRACE_msg_vm_end(vm);
276 xbt_dynar_free(&vm->processes);