Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into hypervisor
[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
7 // TODO
8 // 1./ check how and where a new VM is added to the list of the hosts
9 // 2./ MSG_TRACE can be revisited in order to use  the host
10
11
12 #include "msg_private.h"
13 #include "xbt/sysdep.h"
14 #include "xbt/log.h"
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_vm, msg,
17                                 "Cloud-oriented parts of the MSG API");
18
19
20 /* **** ******** GENERAL ********* **** */
21
22 /** \ingroup m_vm_management
23  * \brief Returns the value of a given vm property
24  *
25  * \param vm a vm
26  * \param name a property name
27  * \return value of a property (or NULL if property not set)
28  */
29
30 const char *MSG_vm_get_property_value(msg_vm_t vm, const char *name)
31 {
32   return MSG_host_get_property_value(vm, name);
33 }
34
35 /** \ingroup m_vm_management
36  * \brief Returns a xbt_dict_t consisting of the list of properties assigned to this host
37  *
38  * \param vm a vm
39  * \return a dict containing the properties
40  */
41 xbt_dict_t MSG_vm_get_properties(msg_vm_t vm)
42 {
43   xbt_assert((vm != NULL), "Invalid parameters (vm is NULL)");
44
45   return (simcall_host_get_properties(vm));
46 }
47
48 /** \ingroup m_host_management
49  * \brief Change the value of a given host property
50  *
51  * \param host a host
52  * \param name a property name
53  * \param value what to change the property to
54  * \param free_ctn the freeing function to use to kill the value on need
55  */
56 void MSG_vm_set_property_value(msg_vm_t vm, const char *name, void *value,void_f_pvoid_t free_ctn) {
57
58   xbt_dict_set(MSG_host_get_properties(vm), name, value,free_ctn);
59 }
60
61 /** \ingroup msg_vm_management
62  * \brief Finds a msg_vm_t using its name.
63  *
64  * This is a name directory service
65  * \param name the name of a vm.
66  * \return the corresponding vm
67  *
68  * Please note that a VM is a specific host. Hence, you should give a different name
69  * for each VM/PM.
70  */
71
72 msg_vm_t MSG_vm_get_by_name(const char *name){
73         return MSG_get_host_by_name(name);
74 }
75
76 /** \ingroup m_vm_management
77  *
78  * \brief Return the name of the #msg_host_t.
79  *
80  * This functions checks whether \a host is a valid pointer or not and return
81    its name.
82  */
83 const char *MSG_vm_get_name(msg_vm_t vm) {
84   return MSG_host_get_name(vm);
85 }
86
87 /* **** ******** MSG vm actions ********* **** */
88
89 /** @brief Create a new VM (the VM is just attached to the location but it is not started yet).
90  *  @ingroup msg_VMs*
91  *
92  * Please note that a VM is a specific host. Hence, you should give a different name
93  * for each VM/PM.
94  */
95 msg_vm_t MSG_vm_create(msg_host_t ind_host, const char *name,
96                                              int core_nb, int mem_cap, int net_cap){
97
98   // Note new and vm_workstation refer to the same area (due to the lib/dict appraoch)
99   msg_vm_t new = NULL;
100   void *ind_vm_workstation =  NULL;
101   // Ask simix to create the surf vm resource
102   ind_vm_workstation = simcall_vm_ws_create(name,ind_host);
103   new = (msg_vm_t) __MSG_host_create(ind_vm_workstation);
104
105   MSG_vm_set_property_value(new, "CORE_NB", bprintf("%d", core_nb), free);
106   MSG_vm_set_property_value(new, "MEM_CAP", bprintf("%d", core_nb), free);
107   MSG_vm_set_property_value(new, "NET_CAP", bprintf("%d", core_nb), free);
108
109   XBT_DEBUG("A new VM has been created");
110   // TODO check whether the vm (i.e the virtual host) has been correctly added into the list of all hosts.
111
112   #ifdef HAVE_TRACING
113   TRACE_msg_vm_create(name, ind_host);
114   #endif
115
116   return new;
117 }
118
119 /** @brief Start a vm (ie. boot)
120  *  @ingroup msg_VMs
121  *
122  *  If the VM cannot be started, an exception is generated.
123  *
124  */
125 void MSG_vm_start(msg_vm_t vm) {
126
127   //Please note that vm start can raise an exception if the VM cannot be started.
128   simcall_vm_start(vm);
129
130   #ifdef HAVE_TRACING
131   TRACE_msg_vm_start(vm);
132   #endif
133 }
134
135 /* **** Check state of a VM **** */
136 int __MSG_vm_is_state(msg_vm_t vm, e_msg_vm_state_t state) {
137         return simcall_get_vm_state(vm) == state ;
138 }
139
140 /** @brief Returns whether the given VM is currently suspended
141  *  @ingroup msg_VMs
142  */
143 int MSG_vm_is_suspended(msg_vm_t vm) {
144         return __MSG_vm_is_state(vm, msg_vm_state_suspended);
145 }
146 /** @brief Returns whether the given VM is currently running
147  *  @ingroup msg_VMs
148  */
149 int MSG_vm_is_running(msg_vm_t vm) {
150   return __MSG_vm_is_state(vm, msg_vm_state_running);
151 }
152
153 // TODO Implement the functions for the different state
154
155 void MSG_vm_shutdown(msg_vm_t vm)
156 {
157   /* msg_vm_t equals to msg_host_t */
158   simcall_vm_shutdown(vm);
159
160   // #ifdef HAVE_TRACING
161   // TRACE_msg_vm_(vm);
162   // #endif
163 }
164
165
166 ///** @brief Add the given process into the VM.
167 // *  @ingroup msg_VMs
168 // *
169 // * Afterward, when the VM is migrated or suspended or whatever, the process will have the corresponding handling, too.
170 // *
171 // */
172 //void MSG_vm_bind(msg_vm_t vm, msg_process_t process) {
173 //  /* check if the process is already in a VM */
174 //  simdata_process_t simdata = simcall_process_get_data(process);
175 //  if (simdata->vm) {
176 //    msg_vm_t old_vm = simdata->vm;
177 //    int pos = xbt_dynar_search(old_vm->processes,&process);
178 //    xbt_dynar_remove_at(old_vm->processes,pos, NULL);
179 //  }
180 //  /* check if the host is in the right host */
181 //  if (simdata->m_host != vm->location) {
182 //    MSG_process_migrate(process,vm->location);
183 //  }
184 //  simdata->vm = vm;
185 //
186 //  XBT_DEBUG("binding Process %s to %p",MSG_process_get_name(process),vm);
187 //
188 //  xbt_dynar_push_as(vm->processes,msg_process_t,process);
189 //}
190 ///** @brief Removes the given process from the given VM, and kill it
191 // *  @ingroup msg_VMs
192 // *
193 // *  Will raise a not_found exception if the process were not binded to that VM
194 // */
195 //void MSG_vm_unbind(msg_vm_t vm, msg_process_t process) {
196 //  int pos = xbt_dynar_search(vm->processes,process);
197 //  xbt_dynar_remove_at(vm->processes,pos, NULL);
198 //  MSG_process_kill(process);
199 //}
200 //
201 ///** @brief Immediately change the host on which all processes are running.
202 // *  @ingroup msg_VMs
203 // *
204 // * No migration cost occurs. If you want to simulate this too, you want to use a
205 // * MSG_task_send() before or after, depending on whether you want to do cold or hot
206 // * migration.
207 // */
208 //void MSG_vm_migrate(msg_vm_t vm, msg_host_t destination) {
209 //  unsigned int cpt;
210 //  msg_process_t process;
211 //  xbt_dynar_foreach(vm->processes,cpt,process) {
212 //    MSG_process_migrate(process,destination);
213 //  }
214 //  xbt_swag_remove(vm, MSG_host_priv(vm->location)->vms);
215 //  xbt_swag_insert_at_tail(vm, MSG_host_priv(destination)->vms);
216 //
217 //  #ifdef HAVE_TRACING
218 //  TRACE_msg_vm_change_host(vm,vm->location,destination);
219 //  #endif
220 //
221 //  vm->location = destination;
222 //}
223 //
224
225 /** @brief Immediately suspend the execution of all processes within the given VM.
226  *  @ingroup msg_VMs
227  *
228  * No suspension cost occurs. If you want to simulate this too, you want to
229  * use a \ref MSG_file_write() before or after, depending on the exact semantic
230  * of VM suspend to you.
231  */
232 void MSG_vm_suspend(msg_vm_t vm)
233 {
234   simcall_vm_suspend(vm);
235
236   #ifdef HAVE_TRACING
237   TRACE_msg_vm_suspend(vm);
238   #endif
239 #if 0
240   unsigned int cpt;
241   msg_process_t process;
242   xbt_dynar_foreach(vm->processes,cpt,process) {
243     XBT_DEBUG("suspend process %s of host %s",MSG_process_get_name(process),MSG_host_get_name(MSG_process_get_host(process)));
244     MSG_process_suspend(process);
245   }
246 #endif
247 }
248
249 //
250 //
251 ///** @brief Immediately resumes the execution of all processes within the given VM.
252 // *  @ingroup msg_VMs
253 // *
254 // * No resume cost occurs. If you want to simulate this too, you want to
255 // * use a \ref MSG_file_read() before or after, depending on the exact semantic
256 // * of VM resume to you.
257 // */
258 //void MSG_vm_resume(msg_vm_t vm) {
259 //  unsigned int cpt;
260 //  msg_process_t process;
261 //  xbt_dynar_foreach(vm->processes,cpt,process) {
262 //    XBT_DEBUG("resume process %s of host %s",MSG_process_get_name(process),MSG_host_get_name(MSG_process_get_host(process)));
263 //    MSG_process_resume(process);
264 //  }
265 //
266 //  #ifdef HAVE_TRACING
267 //  TRACE_msg_vm_resume(vm);
268 //  #endif
269 //}
270 //
271 //
272 ///**
273 // * \ingroup msg_VMs
274 // * \brief Reboot the VM, restarting all the processes in it.
275 // */
276 //void MSG_vm_reboot(msg_vm_t vm)
277 //{
278 //  xbt_dynar_t new_processes = xbt_dynar_new(sizeof(msg_process_t),NULL);
279 //
280 //  msg_process_t process;
281 //  unsigned int cpt;
282 //
283 //  xbt_dynar_foreach(vm->processes,cpt,process) {
284 //    msg_process_t new_process = MSG_process_restart(process);
285 //    xbt_dynar_push_as(new_processes,msg_process_t,new_process);
286 //
287 //  }
288 //
289 //  xbt_dynar_foreach(new_processes, cpt, process) {
290 //    MSG_vm_bind(vm,process);
291 //  }
292 //
293 //  xbt_dynar_free(&new_processes);
294 //}
295 //
296
297 /** @brief Destroy a VM. Destroy the VM object from the simulation.
298  *  @ingroup msg_VMs
299  */
300 void MSG_vm_destroy(msg_vm_t vm)
301 {
302   /* First, terminate all processes on the VM */
303   simcall_vm_shutdown(vm);
304
305   /* Then, destroy the VM object */
306   simcall_vm_destroy(vm);
307
308   #ifdef HAVE_TRACING
309   TRACE_msg_vm_end(vm);
310   #endif
311
312 #if 0
313   unsigned int cpt;
314   msg_process_t process;
315   xbt_dynar_foreach(vm->processes,cpt,process) {
316     //FIXME: Slow ?
317     simdata_process_t simdata = simcall_process_get_data(process);
318     simdata->vm = NULL;
319   }
320
321   xbt_dynar_free(&vm->processes);
322   xbt_free(vm);
323 #endif
324 }