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_start(msg_host_t location, int coreAmount) {
21 msg_vm_t res = xbt_new0(s_msg_vm_t,1);
22 res->all_vms_hookup.prev = NULL;
23 res->host_vms_hookup.prev = NULL;
24 res->state = msg_vm_state_running;
25 res->location = location;
26 res->coreAmount = coreAmount;
27 res->processes = xbt_dynar_new(sizeof(msg_process_t),NULL);
29 xbt_swag_insert(res,msg_global->vms);
30 xbt_swag_insert(res,location->vms);
34 /** @brief Returns a newly constructed dynar containing all existing VMs in the system.
37 * Don't forget to free the dynar after use.
39 xbt_dynar_t MSG_vms_as_dynar(void) {
40 xbt_dynar_t res = xbt_dynar_new(sizeof(msg_vm_t),NULL);
42 xbt_swag_foreach(vm,msg_global->vms) {
43 xbt_dynar_push(res,&vm);
48 /** @brief Returns whether the given VM is currently suspended
51 int MSG_vm_is_suspended(msg_vm_t vm) {
52 return vm->state == msg_vm_state_suspended;
54 /** @brief Returns whether the given VM is currently running
57 int MSG_vm_is_running(msg_vm_t vm) {
58 return vm->state == msg_vm_state_running;
60 /** @brief Add the given process into the VM.
63 * Afterward, when the VM is migrated or suspended or whatever, the process will have the corresponding handling, too.
66 void MSG_vm_bind(msg_vm_t vm, msg_process_t process) {
67 /* check if the process is already in a VM */
68 simdata_process_t simdata = simcall_process_get_data(process);
70 msg_vm_t old_vm = simdata->vm;
71 int pos = xbt_dynar_search(old_vm->processes,&process);
72 xbt_dynar_remove_at(old_vm->processes,pos, NULL);
74 /* check if the host is in the right host */
75 if (simdata->m_host != vm->location) {
76 MSG_process_migrate(process,vm->location);
80 XBT_DEBUG("binding Process %s to %p",MSG_process_get_name(process),vm);
82 xbt_dynar_push_as(vm->processes,msg_process_t,process);
84 /** @brief Removes the given process from the given VM, and kill it
87 * Will raise a not_found exception if the process were not binded to that VM
89 void MSG_vm_unbind(msg_vm_t vm, msg_process_t process) {
90 int pos = xbt_dynar_search(vm->processes,process);
91 xbt_dynar_remove_at(vm->processes,pos, NULL);
92 MSG_process_kill(process);
95 /** @brief Immediately change the host on which all processes are running.
98 * No migration cost occurs. If you want to simulate this too, you want to use a
99 * MSG_task_send() before or after, depending on whether you want to do cold or hot
102 void MSG_vm_migrate(msg_vm_t vm, msg_host_t destination) {
104 msg_process_t process;
105 xbt_dynar_foreach(vm->processes,cpt,process) {
106 MSG_process_migrate(process,destination);
108 xbt_swag_remove(vm,vm->location->vms);
109 xbt_swag_insert_at_tail(vm,destination->vms);
110 vm->location = destination;
113 /** @brief Immediately suspend the execution of all processes within the given VM.
116 * No suspension cost occurs. If you want to simulate this too, you want to
117 * use a \ref MSG_file_write() before or after, depending on the exact semantic
118 * of VM suspend to you.
120 void MSG_vm_suspend(msg_vm_t vm) {
122 msg_process_t process;
123 xbt_dynar_foreach(vm->processes,cpt,process) {
124 XBT_DEBUG("suspend process %s of host %s",MSG_process_get_name(process),MSG_host_get_name(MSG_process_get_host(process)));
125 MSG_process_suspend(process);
129 /** @brief Immediately resumes the execution of all processes within the given VM.
132 * No resume cost occurs. If you want to simulate this too, you want to
133 * use a \ref MSG_file_read() before or after, depending on the exact semantic
134 * of VM resume to you.
136 void MSG_vm_resume(msg_vm_t vm) {
138 msg_process_t process;
139 xbt_dynar_foreach(vm->processes,cpt,process) {
140 XBT_DEBUG("resume process %s of host %s",MSG_process_get_name(process),MSG_host_get_name(MSG_process_get_host(process)));
141 MSG_process_resume(process);
145 /** @brief Immediately kills all processes within the given VM. Any memory that they allocated will be leaked.
148 * No extra delay occurs. If you want to simulate this too, you want to
149 * use a #MSG_process_sleep() or something. I'm not quite sure.
151 void MSG_vm_shutdown(msg_vm_t vm)
153 msg_process_t process;
154 XBT_DEBUG("%lu processes in the VM", xbt_dynar_length(vm->processes));
155 while (xbt_dynar_length(vm->processes) > 0) {
156 process = xbt_dynar_get_as(vm->processes,0,msg_process_t);
157 MSG_process_kill(process);
162 * \brief Reboot the VM, restarting all the processes in it.
164 void MSG_vm_reboot(msg_vm_t vm)
166 xbt_dynar_t new_processes = xbt_dynar_new(sizeof(msg_process_t),NULL);
168 msg_process_t process;
171 xbt_dynar_foreach(vm->processes,cpt,process) {
172 msg_process_t new_process = MSG_process_restart(process);
173 xbt_dynar_push_as(new_processes,msg_process_t,new_process);
177 xbt_dynar_foreach(new_processes, cpt, process) {
178 MSG_vm_bind(vm,process);
181 xbt_dynar_free(&new_processes);
183 /** @brief Destroy a msg_vm_t.
186 void MSG_vm_destroy(msg_vm_t vm) {
188 msg_process_t process;
189 xbt_dynar_foreach(vm->processes,cpt,process) {
191 simdata_process_t simdata = simcall_process_get_data(process);
194 xbt_dynar_free(&vm->processes);