Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
First complete implementation of a new MSG call (starting from MSG down
[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 /** @brief Create a new (empty) VMs.
14  *  @ingroup msg_VMs
15  *
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.
18  */
19
20 msg_vm_t MSG_vm_create(msg_host_t location, const char *name,
21                                              int core_nb, int mem_cap, int net_cap){
22
23   // Note new and vm_workstation refer to the same area (due to the lib/dict appraoch)
24   msg_vm_t new = NULL;
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);
29
30
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);
34
35   #ifdef HAVE_TRACING
36   TRACE_msg_vm_create(name, location);
37   #endif
38   return new;
39 }
40
41 /** \ingroup m_host_management
42  * \brief Returns the value of a given host property
43  *
44  * \param host a host
45  * \param name a property name
46  * \return value of a property (or NULL if property not set)
47  */
48 const char *MSG_host_get_property_value(msg_host_t host, const char *name)
49 {
50   return xbt_dict_get_or_null(MSG_host_get_properties(host), name);
51 }
52
53 /** \ingroup m_host_management
54  * \brief Returns a xbt_dict_t consisting of the list of properties assigned to this host
55  *
56  * \param host a host
57  * \return a dict containing the properties
58  */
59 xbt_dict_t MSG_host_get_properties(msg_host_t host)
60 {
61   xbt_assert((host != NULL), "Invalid parameters (host is NULL)");
62
63   return (simcall_host_get_properties(host));
64 }
65
66 /** \ingroup m_host_management
67  * \brief Change the value of a given host property
68  *
69  * \param host a host
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
73  */
74 void MSG_vm_set_property_value(msg_vm_t vm, const char *name, void *value,void_f_pvoid_t free_ctn) {
75
76   xbt_dict_set(MSG_host_get_properties(vm), name, value,free_ctn);
77 }
78
79 /** @brief Immediately suspend the execution of all processes within the given VM.
80  *  @ingroup msg_VMs
81  *  return wheter the VM has been correctly started (0) or not (<0)
82  *
83  */
84 int MSG_vm_start(msg_vm_t vm) {
85  // TODO Please complete the code
86
87   #ifdef HAVE_TRACING
88   TRACE_msg_vm_start(vm);
89   #endif
90 }
91
92 /** @brief Returns a newly constructed dynar containing all existing VMs in the system.
93  *  @ingroup msg_VMs
94  *
95  * Don't forget to free the dynar after use.
96  */
97 xbt_dynar_t MSG_vms_as_dynar(void) {
98   xbt_dynar_t res = xbt_dynar_new(sizeof(msg_vm_t),NULL);
99   msg_vm_t vm;
100   xbt_swag_foreach(vm,msg_global->vms) {
101     xbt_dynar_push(res,&vm);
102   }
103   return res;
104 }
105
106 /** @brief Returns whether the given VM is currently suspended
107  *  @ingroup msg_VMs
108  */
109 int MSG_vm_is_suspended(msg_vm_t vm) {
110   return vm->state == msg_vm_state_suspended;
111 }
112 /** @brief Returns whether the given VM is currently running
113  *  @ingroup msg_VMs
114  */
115 int MSG_vm_is_running(msg_vm_t vm) {
116   return vm->state == msg_vm_state_running;
117 }
118 /** @brief Add the given process into the VM.
119  *  @ingroup msg_VMs
120  *
121  * Afterward, when the VM is migrated or suspended or whatever, the process will have the corresponding handling, too.
122  *
123  */
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);
127   if (simdata->vm) {
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);
131   }
132   /* check if the host is in the right host */
133   if (simdata->m_host != vm->location) {
134     MSG_process_migrate(process,vm->location);
135   }
136   simdata->vm = vm;
137
138   XBT_DEBUG("binding Process %s to %p",MSG_process_get_name(process),vm);
139
140   xbt_dynar_push_as(vm->processes,msg_process_t,process);
141 }
142 /** @brief Removes the given process from the given VM, and kill it
143  *  @ingroup msg_VMs
144  *
145  *  Will raise a not_found exception if the process were not binded to that VM
146  */
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);
151 }
152
153 /** @brief Immediately change the host on which all processes are running.
154  *  @ingroup msg_VMs
155  *
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
158  * migration.
159  */
160 void MSG_vm_migrate(msg_vm_t vm, msg_host_t destination) {
161   unsigned int cpt;
162   msg_process_t process;
163   xbt_dynar_foreach(vm->processes,cpt,process) {
164     MSG_process_migrate(process,destination);
165   }
166   xbt_swag_remove(vm, MSG_host_priv(vm->location)->vms);
167   xbt_swag_insert_at_tail(vm, MSG_host_priv(destination)->vms);
168   
169   #ifdef HAVE_TRACING
170   TRACE_msg_vm_change_host(vm,vm->location,destination);
171   #endif
172
173   vm->location = destination;
174 }
175
176 /** @brief Immediately suspend the execution of all processes within the given VM.
177  *  @ingroup msg_VMs
178  *
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.
182  */
183 void MSG_vm_suspend(msg_vm_t vm) {
184   unsigned int cpt;
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);
189   }
190
191   #ifdef HAVE_TRACING
192   TRACE_msg_vm_suspend(vm);
193   #endif
194 }
195
196 /** @brief Immediately resumes the execution of all processes within the given VM.
197  *  @ingroup msg_VMs
198  *
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.
202  */
203 void MSG_vm_resume(msg_vm_t vm) {
204   unsigned int cpt;
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);
209   }
210
211   #ifdef HAVE_TRACING
212   TRACE_msg_vm_resume(vm);
213   #endif
214 }
215
216 /** @brief Immediately kills all processes within the given VM. Any memory that they allocated will be leaked.
217  *  @ingroup msg_VMs
218  *
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.
221  */
222 void MSG_vm_shutdown(msg_vm_t vm)
223 {
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);
229   }
230
231   #ifdef HAVE_TRACING
232   TRACE_msg_vm_kill(vm);
233   #endif
234
235 }
236 /**
237  * \ingroup msg_VMs
238  * \brief Reboot the VM, restarting all the processes in it.
239  */
240 void MSG_vm_reboot(msg_vm_t vm)
241 {
242   xbt_dynar_t new_processes = xbt_dynar_new(sizeof(msg_process_t),NULL);
243
244   msg_process_t process;
245   unsigned int cpt;
246
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);
250
251   }
252
253   xbt_dynar_foreach(new_processes, cpt, process) {
254     MSG_vm_bind(vm,process);
255   }
256
257   xbt_dynar_free(&new_processes);
258 }
259 /** @brief Destroy a msg_vm_t.
260  *  @ingroup msg_VMs
261  */
262 void MSG_vm_destroy(msg_vm_t vm) {
263   unsigned int cpt;
264   msg_process_t process;
265   xbt_dynar_foreach(vm->processes,cpt,process) {
266     //FIXME: Slow ?
267     simdata_process_t simdata = simcall_process_get_data(process);
268     simdata->vm = NULL;
269   }
270
271   #ifdef HAVE_TRACING
272   TRACE_msg_vm_end(vm);
273   #endif
274
275
276   xbt_dynar_free(&vm->processes);
277   xbt_free(vm);
278 }