Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Still fixing bugs - Adrien
[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   XBT_DEBUG("A new VM has been created");
118   // TODO check whether the vm (i.e the virtual host) has been correctly added into the list of all hosts.
119
120   #ifdef HAVE_TRACING
121   TRACE_msg_vm_create(name, location);
122   #endif
123
124   return new;
125 }
126
127 /** @brief Start a vm (ie. boot)
128  *  @ingroup msg_VMs
129  *
130  *  If the VM cannot be started, an exception is generated.
131  *
132  */
133 void MSG_vm_start(msg_vm_t vm) {
134
135   //Please note that vm start can raise an exception if the VM cannot be started.
136   simcall_vm_start(vm);
137
138   #ifdef HAVE_TRACING
139   TRACE_msg_vm_start(vm);
140   #endif
141 }
142
143 /* **** Check state of a VM **** */
144 int __MSG_vm_is_state(msg_vm_t vm, e_msg_vm_state_t state) {
145         return simcall_get_vm_state(vm) == state ;
146 }
147
148 /** @brief Returns whether the given VM is currently suspended
149  *  @ingroup msg_VMs
150  */
151 int MSG_vm_is_suspended(msg_vm_t vm) {
152         return __MSG_vm_is_state(msg_vm_state_suspended);
153 }
154 /** @brief Returns whether the given VM is currently running
155  *  @ingroup msg_VMs
156  */
157 int MSG_vm_is_running(msg_vm_t vm) {
158   return __MSG_vm_is_state(msg_vm_state_running);
159 }
160
161 // TODO Implement the functions for the different state
162
163 void MSG_vm_shutdown(msg_vm_t vm)
164 {
165   /* msg_vm_t equals to msg_host_t */
166   simcall_vm_shutdown(vm);
167
168   /* taka: not yet confirmed */
169   #ifdef HAVE_TRACING
170   TRACE_msg_vm_kill(vm);
171   #endif
172
173 }
174
175
176 ///** @brief Add the given process into the VM.
177 // *  @ingroup msg_VMs
178 // *
179 // * Afterward, when the VM is migrated or suspended or whatever, the process will have the corresponding handling, too.
180 // *
181 // */
182 //void MSG_vm_bind(msg_vm_t vm, msg_process_t process) {
183 //  /* check if the process is already in a VM */
184 //  simdata_process_t simdata = simcall_process_get_data(process);
185 //  if (simdata->vm) {
186 //    msg_vm_t old_vm = simdata->vm;
187 //    int pos = xbt_dynar_search(old_vm->processes,&process);
188 //    xbt_dynar_remove_at(old_vm->processes,pos, NULL);
189 //  }
190 //  /* check if the host is in the right host */
191 //  if (simdata->m_host != vm->location) {
192 //    MSG_process_migrate(process,vm->location);
193 //  }
194 //  simdata->vm = vm;
195 //
196 //  XBT_DEBUG("binding Process %s to %p",MSG_process_get_name(process),vm);
197 //
198 //  xbt_dynar_push_as(vm->processes,msg_process_t,process);
199 //}
200 ///** @brief Removes the given process from the given VM, and kill it
201 // *  @ingroup msg_VMs
202 // *
203 // *  Will raise a not_found exception if the process were not binded to that VM
204 // */
205 //void MSG_vm_unbind(msg_vm_t vm, msg_process_t process) {
206 //  int pos = xbt_dynar_search(vm->processes,process);
207 //  xbt_dynar_remove_at(vm->processes,pos, NULL);
208 //  MSG_process_kill(process);
209 //}
210 //
211 ///** @brief Immediately change the host on which all processes are running.
212 // *  @ingroup msg_VMs
213 // *
214 // * No migration cost occurs. If you want to simulate this too, you want to use a
215 // * MSG_task_send() before or after, depending on whether you want to do cold or hot
216 // * migration.
217 // */
218 //void MSG_vm_migrate(msg_vm_t vm, msg_host_t destination) {
219 //  unsigned int cpt;
220 //  msg_process_t process;
221 //  xbt_dynar_foreach(vm->processes,cpt,process) {
222 //    MSG_process_migrate(process,destination);
223 //  }
224 //  xbt_swag_remove(vm, MSG_host_priv(vm->location)->vms);
225 //  xbt_swag_insert_at_tail(vm, MSG_host_priv(destination)->vms);
226 //
227 //  #ifdef HAVE_TRACING
228 //  TRACE_msg_vm_change_host(vm,vm->location,destination);
229 //  #endif
230 //
231 //  vm->location = destination;
232 //}
233 //
234 ///** @brief Immediately suspend the execution of all processes within the given VM.
235 // *  @ingroup msg_VMs
236 // *
237 // * No suspension cost occurs. If you want to simulate this too, you want to
238 // * use a \ref MSG_file_write() before or after, depending on the exact semantic
239 // * of VM suspend to you.
240 // */
241 //void MSG_vm_suspend(msg_vm_t vm) {
242 //  unsigned int cpt;
243 //  msg_process_t process;
244 //  xbt_dynar_foreach(vm->processes,cpt,process) {
245 //    XBT_DEBUG("suspend process %s of host %s",MSG_process_get_name(process),MSG_host_get_name(MSG_process_get_host(process)));
246 //    MSG_process_suspend(process);
247 //  }
248 //
249 //  #ifdef HAVE_TRACING
250 //  TRACE_msg_vm_suspend(vm);
251 //  #endif
252 //}
253 //
254 //
255 ///** @brief Immediately resumes the execution of all processes within the given VM.
256 // *  @ingroup msg_VMs
257 // *
258 // * No resume cost occurs. If you want to simulate this too, you want to
259 // * use a \ref MSG_file_read() before or after, depending on the exact semantic
260 // * of VM resume to you.
261 // */
262 //void MSG_vm_resume(msg_vm_t vm) {
263 //  unsigned int cpt;
264 //  msg_process_t process;
265 //  xbt_dynar_foreach(vm->processes,cpt,process) {
266 //    XBT_DEBUG("resume process %s of host %s",MSG_process_get_name(process),MSG_host_get_name(MSG_process_get_host(process)));
267 //    MSG_process_resume(process);
268 //  }
269 //
270 //  #ifdef HAVE_TRACING
271 //  TRACE_msg_vm_resume(vm);
272 //  #endif
273 //}
274 //
275 //
276 ///**
277 // * \ingroup msg_VMs
278 // * \brief Reboot the VM, restarting all the processes in it.
279 // */
280 //void MSG_vm_reboot(msg_vm_t vm)
281 //{
282 //  xbt_dynar_t new_processes = xbt_dynar_new(sizeof(msg_process_t),NULL);
283 //
284 //  msg_process_t process;
285 //  unsigned int cpt;
286 //
287 //  xbt_dynar_foreach(vm->processes,cpt,process) {
288 //    msg_process_t new_process = MSG_process_restart(process);
289 //    xbt_dynar_push_as(new_processes,msg_process_t,new_process);
290 //
291 //  }
292 //
293 //  xbt_dynar_foreach(new_processes, cpt, process) {
294 //    MSG_vm_bind(vm,process);
295 //  }
296 //
297 //  xbt_dynar_free(&new_processes);
298 //}
299 //
300 ///** @brief Destroy a msg_vm_t.
301 // *  @ingroup msg_VMs
302 // */
303 //void MSG_vm_destroy(msg_vm_t vm)
304 //{
305 //  simcall_vm_destroy(vm);
306 //
307 //#if 0
308 //  unsigned int cpt;
309 //  msg_process_t process;
310 //  xbt_dynar_foreach(vm->processes,cpt,process) {
311 //    //FIXME: Slow ?
312 //    simdata_process_t simdata = simcall_process_get_data(process);
313 //    simdata->vm = NULL;
314 //  }
315 //
316 //  #ifdef HAVE_TRACING
317 //  TRACE_msg_vm_end(vm);
318 //  #endif
319 //
320 //
321 //  xbt_dynar_free(&vm->processes);
322 //  xbt_free(vm);
323 //#endif
324 //}