Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix conflict - 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   // 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   /* taka: not yet confirmed */
168   #ifdef HAVE_TRACING
169   TRACE_msg_vm_kill(vm);
170   #endif
171
172 }
173
174
175 ///** @brief Add the given process into the VM.
176 // *  @ingroup msg_VMs
177 // *
178 // * Afterward, when the VM is migrated or suspended or whatever, the process will have the corresponding handling, too.
179 // *
180 // */
181 //void MSG_vm_bind(msg_vm_t vm, msg_process_t process) {
182 //  /* check if the process is already in a VM */
183 //  simdata_process_t simdata = simcall_process_get_data(process);
184 //  if (simdata->vm) {
185 //    msg_vm_t old_vm = simdata->vm;
186 //    int pos = xbt_dynar_search(old_vm->processes,&process);
187 //    xbt_dynar_remove_at(old_vm->processes,pos, NULL);
188 //  }
189 //  /* check if the host is in the right host */
190 //  if (simdata->m_host != vm->location) {
191 //    MSG_process_migrate(process,vm->location);
192 //  }
193 //  simdata->vm = vm;
194 //
195 //  XBT_DEBUG("binding Process %s to %p",MSG_process_get_name(process),vm);
196 //
197 //  xbt_dynar_push_as(vm->processes,msg_process_t,process);
198 //}
199 ///** @brief Removes the given process from the given VM, and kill it
200 // *  @ingroup msg_VMs
201 // *
202 // *  Will raise a not_found exception if the process were not binded to that VM
203 // */
204 //void MSG_vm_unbind(msg_vm_t vm, msg_process_t process) {
205 //  int pos = xbt_dynar_search(vm->processes,process);
206 //  xbt_dynar_remove_at(vm->processes,pos, NULL);
207 //  MSG_process_kill(process);
208 //}
209 //
210 ///** @brief Immediately change the host on which all processes are running.
211 // *  @ingroup msg_VMs
212 // *
213 // * No migration cost occurs. If you want to simulate this too, you want to use a
214 // * MSG_task_send() before or after, depending on whether you want to do cold or hot
215 // * migration.
216 // */
217 //void MSG_vm_migrate(msg_vm_t vm, msg_host_t destination) {
218 //  unsigned int cpt;
219 //  msg_process_t process;
220 //  xbt_dynar_foreach(vm->processes,cpt,process) {
221 //    MSG_process_migrate(process,destination);
222 //  }
223 //  xbt_swag_remove(vm, MSG_host_priv(vm->location)->vms);
224 //  xbt_swag_insert_at_tail(vm, MSG_host_priv(destination)->vms);
225 //
226 //  #ifdef HAVE_TRACING
227 //  TRACE_msg_vm_change_host(vm,vm->location,destination);
228 //  #endif
229 //
230 //  vm->location = destination;
231 //}
232 //
233 ///** @brief Immediately suspend the execution of all processes within the given VM.
234 // *  @ingroup msg_VMs
235 // *
236 // * No suspension cost occurs. If you want to simulate this too, you want to
237 // * use a \ref MSG_file_write() before or after, depending on the exact semantic
238 // * of VM suspend to you.
239 // */
240 //void MSG_vm_suspend(msg_vm_t vm) {
241 //  unsigned int cpt;
242 //  msg_process_t process;
243 //  xbt_dynar_foreach(vm->processes,cpt,process) {
244 //    XBT_DEBUG("suspend process %s of host %s",MSG_process_get_name(process),MSG_host_get_name(MSG_process_get_host(process)));
245 //    MSG_process_suspend(process);
246 //  }
247 //
248 //  #ifdef HAVE_TRACING
249 //  TRACE_msg_vm_suspend(vm);
250 //  #endif
251 //}
252 //
253 //
254 ///** @brief Immediately resumes the execution of all processes within the given VM.
255 // *  @ingroup msg_VMs
256 // *
257 // * No resume cost occurs. If you want to simulate this too, you want to
258 // * use a \ref MSG_file_read() before or after, depending on the exact semantic
259 // * of VM resume to you.
260 // */
261 //void MSG_vm_resume(msg_vm_t vm) {
262 //  unsigned int cpt;
263 //  msg_process_t process;
264 //  xbt_dynar_foreach(vm->processes,cpt,process) {
265 //    XBT_DEBUG("resume process %s of host %s",MSG_process_get_name(process),MSG_host_get_name(MSG_process_get_host(process)));
266 //    MSG_process_resume(process);
267 //  }
268 //
269 //  #ifdef HAVE_TRACING
270 //  TRACE_msg_vm_resume(vm);
271 //  #endif
272 //}
273 //
274 //
275 ///**
276 // * \ingroup msg_VMs
277 // * \brief Reboot the VM, restarting all the processes in it.
278 // */
279 //void MSG_vm_reboot(msg_vm_t vm)
280 //{
281 //  xbt_dynar_t new_processes = xbt_dynar_new(sizeof(msg_process_t),NULL);
282 //
283 //  msg_process_t process;
284 //  unsigned int cpt;
285 //
286 //  xbt_dynar_foreach(vm->processes,cpt,process) {
287 //    msg_process_t new_process = MSG_process_restart(process);
288 //    xbt_dynar_push_as(new_processes,msg_process_t,new_process);
289 //
290 //  }
291 //
292 //  xbt_dynar_foreach(new_processes, cpt, process) {
293 //    MSG_vm_bind(vm,process);
294 //  }
295 //
296 //  xbt_dynar_free(&new_processes);
297 //}
298 //
299 ///** @brief Destroy a msg_vm_t.
300 // *  @ingroup msg_VMs
301 // */
302 //void MSG_vm_destroy(msg_vm_t vm)
303 //{
304 //  simcall_vm_destroy(vm);
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 //}