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 // QUESTIONS:
7 // 1./ check how and where a new VM is added to the list of the hosts
8 // 2./ Diff between SIMIX_Actions and SURF_Actions
9 // => SIMIX_actions : point synchro entre processus de niveau (theoretically speaking I do not have to create such SIMIX_ACTION
10 // =>  Surf_Actions
11
12 // TODO
13 //      MSG_TRACE can be revisited in order to use  the host
14 //      To implement a mixed model between workstation and vm_workstation,
15 //     please give a look at surf_model_private_t model_private at SURF Level and to the share resource functions
16 //     double (*share_resources) (double now);
17 //      For the action into the vm workstation model, we should be able to leverage the usual one (and if needed, look at
18 //              the workstation model.
19
20 #include "msg_private.h"
21 #include "xbt/sysdep.h"
22 #include "xbt/log.h"
23
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_vm, msg,
25                                 "Cloud-oriented parts of the MSG API");
26
27
28 /* **** ******** GENERAL ********* **** */
29
30 /** \ingroup m_vm_management
31  * \brief Returns the value of a given vm property
32  *
33  * \param vm a vm
34  * \param name a property name
35  * \return value of a property (or NULL if property not set)
36  */
37
38 const char *MSG_vm_get_property_value(msg_vm_t vm, const char *name)
39 {
40   return MSG_host_get_property_value(vm, name);
41 }
42
43 /** \ingroup m_vm_management
44  * \brief Returns a xbt_dict_t consisting of the list of properties assigned to this host
45  *
46  * \param vm a vm
47  * \return a dict containing the properties
48  */
49 xbt_dict_t MSG_vm_get_properties(msg_vm_t vm)
50 {
51   xbt_assert((vm != NULL), "Invalid parameters (vm is NULL)");
52
53   return (simcall_host_get_properties(vm));
54 }
55
56 /** \ingroup m_host_management
57  * \brief Change the value of a given host property
58  *
59  * \param host a host
60  * \param name a property name
61  * \param value what to change the property to
62  * \param free_ctn the freeing function to use to kill the value on need
63  */
64 void MSG_vm_set_property_value(msg_vm_t vm, const char *name, void *value,void_f_pvoid_t free_ctn) {
65
66   xbt_dict_set(MSG_host_get_properties(vm), name, value,free_ctn);
67 }
68
69 /** \ingroup msg_vm_management
70  * \brief Finds a msg_vm_t using its name.
71  *
72  * This is a name directory service
73  * \param name the name of a vm.
74  * \return the corresponding vm
75  *
76  * Please note that a VM is a specific host. Hence, you should give a different name
77  * for each VM/PM.
78  */
79
80 msg_vm_t MSG_vm_get_by_name(const char *name){
81         return MSG_get_host_by_name(name);
82 }
83
84 /** \ingroup m_vm_management
85  *
86  * \brief Return the name of the #msg_host_t.
87  *
88  * This functions checks whether \a host is a valid pointer or not and return
89    its name.
90  */
91 const char *MSG_vm_get_name(msg_vm_t vm) {
92   return MSG_host_get_name(vm);
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 ind_host, 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 *ind_vm_workstation =  NULL;
109   // Ask simix to create the surf vm resource
110   ind_vm_workstation = simcall_vm_ws_create(name,ind_host);
111   new = (msg_vm_t) __MSG_host_create(ind_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, ind_host);
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(vm, 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(vm, 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   // #ifdef HAVE_TRACING
169   // TRACE_msg_vm_(vm);
170   // #endif
171 }
172
173
174 ///** @brief Add the given process into the VM.
175 // *  @ingroup msg_VMs
176 // *
177 // * Afterward, when the VM is migrated or suspended or whatever, the process will have the corresponding handling, too.
178 // *
179 // */
180 //void MSG_vm_bind(msg_vm_t vm, msg_process_t process) {
181 //  /* check if the process is already in a VM */
182 //  simdata_process_t simdata = simcall_process_get_data(process);
183 //  if (simdata->vm) {
184 //    msg_vm_t old_vm = simdata->vm;
185 //    int pos = xbt_dynar_search(old_vm->processes,&process);
186 //    xbt_dynar_remove_at(old_vm->processes,pos, NULL);
187 //  }
188 //  /* check if the host is in the right host */
189 //  if (simdata->m_host != vm->location) {
190 //    MSG_process_migrate(process,vm->location);
191 //  }
192 //  simdata->vm = vm;
193 //
194 //  XBT_DEBUG("binding Process %s to %p",MSG_process_get_name(process),vm);
195 //
196 //  xbt_dynar_push_as(vm->processes,msg_process_t,process);
197 //}
198 ///** @brief Removes the given process from the given VM, and kill it
199 // *  @ingroup msg_VMs
200 // *
201 // *  Will raise a not_found exception if the process were not binded to that VM
202 // */
203 //void MSG_vm_unbind(msg_vm_t vm, msg_process_t process) {
204 //  int pos = xbt_dynar_search(vm->processes,process);
205 //  xbt_dynar_remove_at(vm->processes,pos, NULL);
206 //  MSG_process_kill(process);
207 //}
208 //
209 ///** @brief Immediately change the host on which all processes are running.
210 // *  @ingroup msg_VMs
211 // *
212 // * No migration cost occurs. If you want to simulate this too, you want to use a
213 // * MSG_task_send() before or after, depending on whether you want to do cold or hot
214 // * migration.
215 // */
216 //void MSG_vm_migrate(msg_vm_t vm, msg_host_t destination) {
217 //  unsigned int cpt;
218 //  msg_process_t process;
219 //  xbt_dynar_foreach(vm->processes,cpt,process) {
220 //    MSG_process_migrate(process,destination);
221 //  }
222 //  xbt_swag_remove(vm, MSG_host_priv(vm->location)->vms);
223 //  xbt_swag_insert_at_tail(vm, MSG_host_priv(destination)->vms);
224 //
225 //  #ifdef HAVE_TRACING
226 //  TRACE_msg_vm_change_host(vm,vm->location,destination);
227 //  #endif
228 //
229 //  vm->location = destination;
230 //}
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 {
242   simcall_vm_suspend(vm);
243
244   #ifdef HAVE_TRACING
245   TRACE_msg_vm_suspend(vm);
246   #endif
247 #if 0
248   unsigned int cpt;
249   msg_process_t process;
250   xbt_dynar_foreach(vm->processes,cpt,process) {
251     XBT_DEBUG("suspend process %s of host %s",MSG_process_get_name(process),MSG_host_get_name(MSG_process_get_host(process)));
252     MSG_process_suspend(process);
253   }
254 #endif
255 }
256
257 //
258 //
259 ///** @brief Immediately resumes the execution of all processes within the given VM.
260 // *  @ingroup msg_VMs
261 // *
262 // * No resume cost occurs. If you want to simulate this too, you want to
263 // * use a \ref MSG_file_read() before or after, depending on the exact semantic
264 // * of VM resume to you.
265 // */
266 //void MSG_vm_resume(msg_vm_t vm) {
267 //  unsigned int cpt;
268 //  msg_process_t process;
269 //  xbt_dynar_foreach(vm->processes,cpt,process) {
270 //    XBT_DEBUG("resume process %s of host %s",MSG_process_get_name(process),MSG_host_get_name(MSG_process_get_host(process)));
271 //    MSG_process_resume(process);
272 //  }
273 //
274 //  #ifdef HAVE_TRACING
275 //  TRACE_msg_vm_resume(vm);
276 //  #endif
277 //}
278 //
279 //
280 ///**
281 // * \ingroup msg_VMs
282 // * \brief Reboot the VM, restarting all the processes in it.
283 // */
284 //void MSG_vm_reboot(msg_vm_t vm)
285 //{
286 //  xbt_dynar_t new_processes = xbt_dynar_new(sizeof(msg_process_t),NULL);
287 //
288 //  msg_process_t process;
289 //  unsigned int cpt;
290 //
291 //  xbt_dynar_foreach(vm->processes,cpt,process) {
292 //    msg_process_t new_process = MSG_process_restart(process);
293 //    xbt_dynar_push_as(new_processes,msg_process_t,new_process);
294 //
295 //  }
296 //
297 //  xbt_dynar_foreach(new_processes, cpt, process) {
298 //    MSG_vm_bind(vm,process);
299 //  }
300 //
301 //  xbt_dynar_free(&new_processes);
302 //}
303 //
304
305 /** @brief Destroy a VM. Destroy the VM object from the simulation.
306  *  @ingroup msg_VMs
307  */
308 void MSG_vm_destroy(msg_vm_t vm)
309 {
310   /* First, terminate all processes on the VM */
311   simcall_vm_shutdown(vm);
312
313   /* Then, destroy the VM object */
314   simcall_vm_destroy(vm);
315
316   #ifdef HAVE_TRACING
317   TRACE_msg_vm_end(vm);
318   #endif
319
320 #if 0
321   unsigned int cpt;
322   msg_process_t process;
323   xbt_dynar_foreach(vm->processes,cpt,process) {
324     //FIXME: Slow ?
325     simdata_process_t simdata = simcall_process_get_data(process);
326     simdata->vm = NULL;
327   }
328
329   xbt_dynar_free(&vm->processes);
330   xbt_free(vm);
331 #endif
332 }