Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix msg_vm_shutdown
[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 /** \ingroup m_vm_management
71  *
72  * \brief Return the name of the #msg_host_t.
73  *
74  * This functions checks whether \a host is a valid pointer or not and return
75    its name.
76  */
77 const char *MSG_vm_get_name(msg_vm_t vm) {
78   return MSG_host_get_name(vm);
79 }
80
81 /** @brief Returns a newly constructed dynar containing all existing VMs in the system.
82  *  @ingroup msg_VMs
83  *
84  * Don't forget to free the dynar after use.
85  */
86 xbt_dynar_t MSG_vms_as_dynar(void) {
87   xbt_dynar_t res = xbt_dynar_new(sizeof(msg_vm_t),NULL);
88   msg_vm_t vm;
89   xbt_swag_foreach(vm,msg_global->vms) {
90     xbt_dynar_push(res,&vm);
91   }
92   return res;
93 }
94
95 /* **** ******** MSG vm actions ********* **** */
96
97 /** @brief Create a new VM (the VM is just attached to the location but it is not started yet).
98  *  @ingroup msg_VMs*
99  *
100  * Please note that a VM is a specific host. Hence, you should give a different name
101  * for each VM/PM.
102  */
103 msg_vm_t MSG_vm_create(msg_host_t location, const char *name,
104                                              int core_nb, int mem_cap, int net_cap){
105
106   // Note new and vm_workstation refer to the same area (due to the lib/dict appraoch)
107   msg_vm_t new = NULL;
108   void *vm_workstation =  NULL;
109   // Ask simix to create the surf vm resource
110   vm_workstation = simcall_vm_create(name,location);
111   new = (msg_vm_t) __MSG_host_create(vm_workstation);
112
113   MSG_vm_set_property_value(new, "CORE_NB", bprintf("%d", core_nb), free);
114   MSG_vm_set_property_value(new, "MEM_CAP", bprintf("%d", core_nb), free);
115   MSG_vm_set_property_value(new, "NET_CAP", bprintf("%d", core_nb), free);
116
117   // TODO check whether the vm (i.e the virtual host) has been correctly added into the list of all hosts.
118
119   #ifdef HAVE_TRACING
120   TRACE_msg_vm_create(name, location);
121   #endif
122
123   return new;
124 }
125
126 /** @brief Start a vm (ie. boot)
127  *  @ingroup msg_VMs
128  *
129  *  If the VM cannot be started, an exception is generated.
130  *
131  */
132 void MSG_vm_start(msg_vm_t vm) {
133
134   //Please note that vm start can raise an exception if the VM cannot be started.
135   simcall_vm_start(vm);
136
137   #ifdef HAVE_TRACING
138   TRACE_msg_vm_start(vm);
139   #endif
140 }
141
142 /* **** Check state of a VM **** */
143 int __MSG_vm_is_state(msg_vm_t vm, e_msg_vm_state_t state) {
144         return simcall_get_vm_state(vm) == state ;
145 }
146
147 /** @brief Returns whether the given VM is currently suspended
148  *  @ingroup msg_VMs
149  */
150 int MSG_vm_is_suspended(msg_vm_t vm) {
151         return __MSG_vm_is_state(msg_vm_state_suspended);
152 }
153 /** @brief Returns whether the given VM is currently running
154  *  @ingroup msg_VMs
155  */
156 int MSG_vm_is_running(msg_vm_t vm) {
157   return __MSG_vm_is_state(msg_vm_state_running);
158 }
159
160 // TODO Implement the functions for the different state
161
162 void MSG_vm_shutdown(msg_vm_t vm)
163 {
164   /* msg_vm_t equals to msg_host_t */
165   simcall_vm_shutdown(vm);
166
167   #ifdef HAVE_TRACING
168   TRACE_msg_vm_kill(vm);
169   #endif
170 }
171
172
173 ///** @brief Add the given process into the VM.
174 // *  @ingroup msg_VMs
175 // *
176 // * Afterward, when the VM is migrated or suspended or whatever, the process will have the corresponding handling, too.
177 // *
178 // */
179 //void MSG_vm_bind(msg_vm_t vm, msg_process_t process) {
180 //  /* check if the process is already in a VM */
181 //  simdata_process_t simdata = simcall_process_get_data(process);
182 //  if (simdata->vm) {
183 //    msg_vm_t old_vm = simdata->vm;
184 //    int pos = xbt_dynar_search(old_vm->processes,&process);
185 //    xbt_dynar_remove_at(old_vm->processes,pos, NULL);
186 //  }
187 //  /* check if the host is in the right host */
188 //  if (simdata->m_host != vm->location) {
189 //    MSG_process_migrate(process,vm->location);
190 //  }
191 //  simdata->vm = vm;
192 //
193 //  XBT_DEBUG("binding Process %s to %p",MSG_process_get_name(process),vm);
194 //
195 //  xbt_dynar_push_as(vm->processes,msg_process_t,process);
196 //}
197 ///** @brief Removes the given process from the given VM, and kill it
198 // *  @ingroup msg_VMs
199 // *
200 // *  Will raise a not_found exception if the process were not binded to that VM
201 // */
202 //void MSG_vm_unbind(msg_vm_t vm, msg_process_t process) {
203 //  int pos = xbt_dynar_search(vm->processes,process);
204 //  xbt_dynar_remove_at(vm->processes,pos, NULL);
205 //  MSG_process_kill(process);
206 //}
207 //
208 ///** @brief Immediately change the host on which all processes are running.
209 // *  @ingroup msg_VMs
210 // *
211 // * No migration cost occurs. If you want to simulate this too, you want to use a
212 // * MSG_task_send() before or after, depending on whether you want to do cold or hot
213 // * migration.
214 // */
215 //void MSG_vm_migrate(msg_vm_t vm, msg_host_t destination) {
216 //  unsigned int cpt;
217 //  msg_process_t process;
218 //  xbt_dynar_foreach(vm->processes,cpt,process) {
219 //    MSG_process_migrate(process,destination);
220 //  }
221 //  xbt_swag_remove(vm, MSG_host_priv(vm->location)->vms);
222 //  xbt_swag_insert_at_tail(vm, MSG_host_priv(destination)->vms);
223 //
224 //  #ifdef HAVE_TRACING
225 //  TRACE_msg_vm_change_host(vm,vm->location,destination);
226 //  #endif
227 //
228 //  vm->location = destination;
229 //}
230 //
231 ///** @brief Immediately suspend the execution of all processes within the given VM.
232 // *  @ingroup msg_VMs
233 // *
234 // * No suspension cost occurs. If you want to simulate this too, you want to
235 // * use a \ref MSG_file_write() before or after, depending on the exact semantic
236 // * of VM suspend to you.
237 // */
238 //void MSG_vm_suspend(msg_vm_t vm) {
239 //  unsigned int cpt;
240 //  msg_process_t process;
241 //  xbt_dynar_foreach(vm->processes,cpt,process) {
242 //    XBT_DEBUG("suspend process %s of host %s",MSG_process_get_name(process),MSG_host_get_name(MSG_process_get_host(process)));
243 //    MSG_process_suspend(process);
244 //  }
245 //
246 //  #ifdef HAVE_TRACING
247 //  TRACE_msg_vm_suspend(vm);
248 //  #endif
249 //}
250 //
251 //
252 ///** @brief Immediately resumes the execution of all processes within the given VM.
253 // *  @ingroup msg_VMs
254 // *
255 // * No resume cost occurs. If you want to simulate this too, you want to
256 // * use a \ref MSG_file_read() before or after, depending on the exact semantic
257 // * of VM resume to you.
258 // */
259 //void MSG_vm_resume(msg_vm_t vm) {
260 //  unsigned int cpt;
261 //  msg_process_t process;
262 //  xbt_dynar_foreach(vm->processes,cpt,process) {
263 //    XBT_DEBUG("resume process %s of host %s",MSG_process_get_name(process),MSG_host_get_name(MSG_process_get_host(process)));
264 //    MSG_process_resume(process);
265 //  }
266 //
267 //  #ifdef HAVE_TRACING
268 //  TRACE_msg_vm_resume(vm);
269 //  #endif
270 //}
271 //
272 //
273 ///**
274 // * \ingroup msg_VMs
275 // * \brief Reboot the VM, restarting all the processes in it.
276 // */
277 //void MSG_vm_reboot(msg_vm_t vm)
278 //{
279 //  xbt_dynar_t new_processes = xbt_dynar_new(sizeof(msg_process_t),NULL);
280 //
281 //  msg_process_t process;
282 //  unsigned int cpt;
283 //
284 //  xbt_dynar_foreach(vm->processes,cpt,process) {
285 //    msg_process_t new_process = MSG_process_restart(process);
286 //    xbt_dynar_push_as(new_processes,msg_process_t,new_process);
287 //
288 //  }
289 //
290 //  xbt_dynar_foreach(new_processes, cpt, process) {
291 //    MSG_vm_bind(vm,process);
292 //  }
293 //
294 //  xbt_dynar_free(&new_processes);
295 //}
296 //
297
298 /** @brief Destroy a VM.
299  *  @ingroup msg_VMs
300  */
301 void MSG_vm_destroy(msg_vm_t vm)
302 {
303   simcall_vm_destroy(vm);
304   /* TOOD: do we have to do something for processes? */
305
306 #if 0
307   unsigned int cpt;
308   msg_process_t process;
309   xbt_dynar_foreach(vm->processes,cpt,process) {
310     //FIXME: Slow ?
311     simdata_process_t simdata = simcall_process_get_data(process);
312     simdata->vm = NULL;
313   }
314
315   #ifdef HAVE_TRACING
316   TRACE_msg_vm_end(vm);
317   #endif
318
319
320   xbt_dynar_free(&vm->processes);
321   xbt_free(vm);
322 #endif
323 }