Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
568fc0002e0cd0f1d438a1cbf7eef9111b059647
[simgrid.git] / src / msg / msg_vm.c
1 /* Copyright (c) 2012. The SimGrid Team. All rights reserved.               */
2
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. */
5
6 #include "msg_private.h"
7 #include "xbt/sysdep.h"
8 #include "xbt/log.h"
9
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_vm, msg,
11                                 "Cloud-oriented parts of the MSG API");
12
13
14 /* **** ******** GENERAL ********* **** */
15
16 /** \ingroup m_vm_management
17  * \brief Returns the value of a given vm property
18  *
19  * \param vm a vm
20  * \param name a property name
21  * \return value of a property (or NULL if property not set)
22  */
23
24 const char *MSG_vm_get_property_value(msg_vm_t vm, const char *name)
25 {
26   return MSG_host_get_property_value(vm, name);
27 }
28
29 /** \ingroup m_vm_management
30  * \brief Returns a xbt_dict_t consisting of the list of properties assigned to this host
31  *
32  * \param vm a vm
33  * \return a dict containing the properties
34  */
35 xbt_dict_t MSG_vm_get_properties(msg_vm_t vm)
36 {
37   xbt_assert((vm != NULL), "Invalid parameters (vm is NULL)");
38
39   return (simcall_host_get_properties(vm));
40 }
41
42 /** \ingroup m_host_management
43  * \brief Change the value of a given host property
44  *
45  * \param host a host
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
49  */
50 void MSG_vm_set_property_value(msg_vm_t vm, const char *name, void *value,void_f_pvoid_t free_ctn) {
51
52   xbt_dict_set(MSG_host_get_properties(vm), name, value,free_ctn);
53 }
54
55 /** \ingroup msg_vm_management
56  * \brief Finds a msg_vm_t using its name.
57  *
58  * This is a name directory service
59  * \param name the name of a vm.
60  * \return the corresponding vm
61  *
62  * Please note that a VM is a specific host. Hence, you should give a different name
63  * for each VM/PM.
64  */
65
66 msg_vm_t MSG_vm_get_by_name(const char *name){
67         return MSG_get_host_by_name(name);
68 }
69
70
71 /** @brief Returns a newly constructed dynar containing all existing VMs in the system.
72  *  @ingroup msg_VMs
73  *
74  * Don't forget to free the dynar after use.
75  */
76 xbt_dynar_t MSG_vms_as_dynar(void) {
77   xbt_dynar_t res = xbt_dynar_new(sizeof(msg_vm_t),NULL);
78   msg_vm_t vm;
79   xbt_swag_foreach(vm,msg_global->vms) {
80     xbt_dynar_push(res,&vm);
81   }
82   return res;
83 }
84
85 /* **** ******** MSG vm actions ********* **** */
86
87 /** @brief Create a new VM (the VM is just attached to the location but it is not started yet).
88  *  @ingroup msg_VMs*
89  *
90  * Please note that a VM is a specific host. Hence, you should give a different name
91  * for each VM/PM.
92  */
93 msg_vm_t MSG_vm_create(msg_host_t location, const char *name,
94                                              int core_nb, int mem_cap, int net_cap){
95
96   // Note new and vm_workstation refer to the same area (due to the lib/dict appraoch)
97   msg_vm_t new = NULL;
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);
102
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);
106
107   // TODO check whether the vm (i.e the virtual host) has been correctly added into the list of all hosts.
108
109   #ifdef HAVE_TRACING
110   TRACE_msg_vm_create(name, location);
111   #endif
112
113   return new;
114 }
115
116 /** @brief Start a vm (ie. boot)
117  *  @ingroup msg_VMs
118  *
119  *  If the VM cannot be started, an exception is generated.
120  *
121  */
122 void MSG_vm_start(msg_vm_t vm) {
123
124   //Please note that vm start can raise an exception if the VM cannot be started.
125   simcall_vm_start(vm);
126
127   #ifdef HAVE_TRACING
128   TRACE_msg_vm_start(vm);
129   #endif
130 }
131
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 ;
135 }
136
137 /** @brief Returns whether the given VM is currently suspended
138  *  @ingroup msg_VMs
139  */
140 int MSG_vm_is_suspended(msg_vm_t vm) {
141         return __MSG_vm_is_state(msg_vm_state_suspended);
142 }
143 /** @brief Returns whether the given VM is currently running
144  *  @ingroup msg_VMs
145  */
146 int MSG_vm_is_running(msg_vm_t vm) {
147   return __MSG_vm_is_state(msg_vm_state_running);
148 }
149
150 // TODO complete the different state
151
152 /** @brief Add the given process into the VM.
153  *  @ingroup msg_VMs
154  *
155  * Afterward, when the VM is migrated or suspended or whatever, the process will have the corresponding handling, too.
156  *
157  */
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);
161   if (simdata->vm) {
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);
165   }
166   /* check if the host is in the right host */
167   if (simdata->m_host != vm->location) {
168     MSG_process_migrate(process,vm->location);
169   }
170   simdata->vm = vm;
171
172   XBT_DEBUG("binding Process %s to %p",MSG_process_get_name(process),vm);
173
174   xbt_dynar_push_as(vm->processes,msg_process_t,process);
175 }
176 /** @brief Removes the given process from the given VM, and kill it
177  *  @ingroup msg_VMs
178  *
179  *  Will raise a not_found exception if the process were not binded to that VM
180  */
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);
185 }
186
187 /** @brief Immediately change the host on which all processes are running.
188  *  @ingroup msg_VMs
189  *
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
192  * migration.
193  */
194 void MSG_vm_migrate(msg_vm_t vm, msg_host_t destination) {
195   unsigned int cpt;
196   msg_process_t process;
197   xbt_dynar_foreach(vm->processes,cpt,process) {
198     MSG_process_migrate(process,destination);
199   }
200   xbt_swag_remove(vm, MSG_host_priv(vm->location)->vms);
201   xbt_swag_insert_at_tail(vm, MSG_host_priv(destination)->vms);
202   
203   #ifdef HAVE_TRACING
204   TRACE_msg_vm_change_host(vm,vm->location,destination);
205   #endif
206
207   vm->location = destination;
208 }
209
210 /** @brief Immediately suspend the execution of all processes within the given VM.
211  *  @ingroup msg_VMs
212  *
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.
216  */
217 void MSG_vm_suspend(msg_vm_t vm) {
218   unsigned int cpt;
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);
223   }
224
225   #ifdef HAVE_TRACING
226   TRACE_msg_vm_suspend(vm);
227   #endif
228 }
229
230
231 /** @brief Immediately resumes the execution of all processes within the given VM.
232  *  @ingroup msg_VMs
233  *
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.
237  */
238 void MSG_vm_resume(msg_vm_t vm) {
239   unsigned int cpt;
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);
244   }
245
246   #ifdef HAVE_TRACING
247   TRACE_msg_vm_resume(vm);
248   #endif
249 }
250
251
252 void MSG_vm_shutdown(msg_vm_t vm)
253 {
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;
258
259   XBT_DEBUG("%lu processes in the VM", xbt_swag_size(SIMIX_host_priv(smx_host)->process_list));
260
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);
264   }
265
266   /* taka: not yet confirmed */
267   #ifdef HAVE_TRACING
268   TRACE_msg_vm_kill(vm);
269   #endif
270
271
272   /* TODO: update the state of vm */
273
274 #if 0
275   while (xbt_dynar_length(vm->processes) > 0) {
276     process = xbt_dynar_get_as(vm->processes,0,msg_process_t);
277   }
278 #endif
279 }
280
281
282 /**
283  * \ingroup msg_VMs
284  * \brief Reboot the VM, restarting all the processes in it.
285  */
286 void MSG_vm_reboot(msg_vm_t vm)
287 {
288   xbt_dynar_t new_processes = xbt_dynar_new(sizeof(msg_process_t),NULL);
289
290   msg_process_t process;
291   unsigned int cpt;
292
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);
296
297   }
298
299   xbt_dynar_foreach(new_processes, cpt, process) {
300     MSG_vm_bind(vm,process);
301   }
302
303   xbt_dynar_free(&new_processes);
304 }
305
306 /** @brief Destroy a msg_vm_t.
307  *  @ingroup msg_VMs
308  */
309 void MSG_vm_destroy(msg_vm_t vm) {
310   unsigned int cpt;
311   msg_process_t process;
312   xbt_dynar_foreach(vm->processes,cpt,process) {
313     //FIXME: Slow ?
314     simdata_process_t simdata = simcall_process_get_data(process);
315     simdata->vm = NULL;
316   }
317
318   #ifdef HAVE_TRACING
319   TRACE_msg_vm_end(vm);
320   #endif
321
322
323   xbt_dynar_free(&vm->processes);
324   xbt_free(vm);
325 }