Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Whitespace cleanup.
[simgrid.git] / src / msg / msg_vm.c
1 /* Copyright (c) 2012-2013. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "msg_private.h"
8 #include "xbt/sysdep.h"
9 #include "xbt/log.h"
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_vm, msg,
12                                 "Cloud-oriented parts of the MSG API");
13
14 /** @brief Create a new (empty) VMs.
15  *  @ingroup msg_VMs
16  *
17  *  @bug it is expected that in the future, the coreAmount parameter will be used
18  *  to add extra constraints on the execution, but the argument is ignored for now.
19  */
20
21 msg_vm_t MSG_vm_start(msg_host_t location, const char *name, int coreAmount) {
22   msg_vm_t res = xbt_new0(s_msg_vm_t,1);
23   res->all_vms_hookup.prev = NULL;
24   res->host_vms_hookup.prev = NULL;
25   res->state = msg_vm_state_running;
26   res->location = location;
27   res->coreAmount = coreAmount;
28   res->name = xbt_strdup(name);
29   res->processes = xbt_dynar_new(sizeof(msg_process_t),NULL);
30
31   xbt_swag_insert(res,msg_global->vms);
32   xbt_swag_insert(res, MSG_host_priv(location)->vms);
33
34   #ifdef HAVE_TRACING
35   TRACE_msg_vm_create(name, location);
36   #endif
37
38
39   return res;
40 }
41 /** @brief Returns a newly constructed dynar containing all existing VMs in the system.
42  *  @ingroup msg_VMs
43  *
44  * Don't forget to free the dynar after use.
45  */
46 xbt_dynar_t MSG_vms_as_dynar(void) {
47   xbt_dynar_t res = xbt_dynar_new(sizeof(msg_vm_t),NULL);
48   msg_vm_t vm;
49   xbt_swag_foreach(vm,msg_global->vms) {
50     xbt_dynar_push(res,&vm);
51   }
52   return res;
53 }
54
55 /** @brief Returns whether the given VM is currently suspended
56  *  @ingroup msg_VMs
57  */
58 int MSG_vm_is_suspended(msg_vm_t vm) {
59   return vm->state == msg_vm_state_suspended;
60 }
61 /** @brief Returns whether the given VM is currently running
62  *  @ingroup msg_VMs
63  */
64 int MSG_vm_is_running(msg_vm_t vm) {
65   return vm->state == msg_vm_state_running;
66 }
67 /** @brief Add the given process into the VM.
68  *  @ingroup msg_VMs
69  *
70  * Afterward, when the VM is migrated or suspended or whatever, the process will have the corresponding handling, too.
71  *
72  */
73 void MSG_vm_bind(msg_vm_t vm, msg_process_t process) {
74   /* check if the process is already in a VM */
75   simdata_process_t simdata = simcall_process_get_data(process);
76   if (simdata->vm) {
77     msg_vm_t old_vm = simdata->vm;
78     int pos = xbt_dynar_search(old_vm->processes,&process);
79     xbt_dynar_remove_at(old_vm->processes,pos, NULL);
80   }
81   /* check if the host is in the right host */
82   if (simdata->m_host != vm->location) {
83     MSG_process_migrate(process,vm->location);
84   }
85   simdata->vm = vm;
86
87   XBT_DEBUG("binding Process %s to %p",MSG_process_get_name(process),vm);
88
89   xbt_dynar_push_as(vm->processes,msg_process_t,process);
90 }
91 /** @brief Removes the given process from the given VM, and kill it
92  *  @ingroup msg_VMs
93  *
94  *  Will raise a not_found exception if the process were not bound to that VM
95  */
96 void MSG_vm_unbind(msg_vm_t vm, msg_process_t process) {
97   int pos = xbt_dynar_search(vm->processes,process);
98   xbt_dynar_remove_at(vm->processes,pos, NULL);
99   MSG_process_kill(process);
100 }
101
102 /** @brief Immediately change the host on which all processes are running.
103  *  @ingroup msg_VMs
104  *
105  * No migration cost occurs. If you want to simulate this too, you want to use a
106  * MSG_task_send() before or after, depending on whether you want to do cold or hot
107  * migration.
108  */
109 void MSG_vm_migrate(msg_vm_t vm, msg_host_t destination) {
110   unsigned int cpt;
111   msg_process_t process;
112   xbt_dynar_foreach(vm->processes,cpt,process) {
113     MSG_process_migrate(process,destination);
114   }
115   xbt_swag_remove(vm, MSG_host_priv(vm->location)->vms);
116   xbt_swag_insert_at_tail(vm, MSG_host_priv(destination)->vms);
117   
118   #ifdef HAVE_TRACING
119   TRACE_msg_vm_change_host(vm,vm->location,destination);
120   #endif
121
122   vm->location = destination;
123 }
124
125 /** @brief Immediately suspend the execution of all processes within the given VM.
126  *  @ingroup msg_VMs
127  *
128  * No suspension cost occurs. If you want to simulate this too, you want to
129  * use a \ref MSG_file_write() before or after, depending on the exact semantic
130  * of VM suspend to you.
131  */
132 void MSG_vm_suspend(msg_vm_t vm) {
133   unsigned int cpt;
134   msg_process_t process;
135   xbt_dynar_foreach(vm->processes,cpt,process) {
136     XBT_DEBUG("suspend process %s of host %s",MSG_process_get_name(process),MSG_host_get_name(MSG_process_get_host(process)));
137     MSG_process_suspend(process);
138   }
139
140   #ifdef HAVE_TRACING
141   TRACE_msg_vm_suspend(vm);
142   #endif
143 }
144
145 /** @brief Immediately resumes the execution of all processes within the given VM.
146  *  @ingroup msg_VMs
147  *
148  * No resume cost occurs. If you want to simulate this too, you want to
149  * use a \ref MSG_file_read() before or after, depending on the exact semantic
150  * of VM resume to you.
151  */
152 void MSG_vm_resume(msg_vm_t vm) {
153   unsigned int cpt;
154   msg_process_t process;
155   xbt_dynar_foreach(vm->processes,cpt,process) {
156     XBT_DEBUG("resume process %s of host %s",MSG_process_get_name(process),MSG_host_get_name(MSG_process_get_host(process)));
157     MSG_process_resume(process);
158   }
159
160   #ifdef HAVE_TRACING
161   TRACE_msg_vm_resume(vm);
162   #endif
163 }
164
165 /** @brief Immediately kills all processes within the given VM. Any memory that they allocated will be leaked.
166  *  @ingroup msg_VMs
167  *
168  * No extra delay occurs. If you want to simulate this too, you want to
169  * use a #MSG_process_sleep() or something. I'm not quite sure.
170  */
171 void MSG_vm_shutdown(msg_vm_t vm)
172 {
173   msg_process_t process;
174   XBT_DEBUG("%lu processes in the VM", xbt_dynar_length(vm->processes));
175   while (!xbt_dynar_is_empty(vm->processes)) {
176     process = xbt_dynar_get_as(vm->processes,0,msg_process_t);
177     MSG_process_kill(process);
178   }
179
180   #ifdef HAVE_TRACING
181   TRACE_msg_vm_kill(vm);
182   #endif
183
184 }
185
186 /**
187  * \ingroup msg_VMs
188  * \brief Reboot the VM, restarting all the processes in it.
189  */
190 void MSG_vm_reboot(msg_vm_t vm)
191 {
192   xbt_dynar_t process_list = xbt_dynar_new(sizeof(msg_process_t), NULL);
193   msg_process_t process;
194   unsigned int cpt;
195
196   xbt_dynar_foreach(vm->processes, cpt, process) {
197     xbt_dynar_push_as(process_list, msg_process_t, process);
198   }
199
200   xbt_dynar_foreach(process_list, cpt, process) {
201     msg_process_t new_process = MSG_process_restart(process);
202     MSG_vm_bind(vm, new_process);
203   }
204
205   xbt_dynar_free(&process_list);
206 }
207
208 /** @brief Destroy a msg_vm_t.
209  *  @ingroup msg_VMs
210  */
211 void MSG_vm_destroy(msg_vm_t vm) {
212   unsigned int cpt;
213   msg_process_t process;
214   xbt_dynar_foreach(vm->processes,cpt,process) {
215     //FIXME: Slow ?
216     simdata_process_t simdata = simcall_process_get_data(process);
217     simdata->vm = NULL;
218   }
219
220   #ifdef HAVE_TRACING
221   TRACE_msg_vm_end(vm);
222   #endif
223
224   xbt_free(vm->name);
225   xbt_dynar_free(&vm->processes);
226   xbt_free(vm);
227 }