Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix conflict
[simgrid.git] / src / simix / smx_vm.c
1 /* Copyright (c) 2007-2012. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "smx_private.h"
8 #include "xbt/sysdep.h"
9 #include "xbt/log.h"
10 #include "xbt/dict.h"
11 #include "mc/mc.h"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_vm, simix,
14                                 "Logging specific to SIMIX (vms)");
15 /* **** create a VM **** */
16
17 /**
18  * \brief Internal function to create a SIMIX host.
19  * \param name name of the host to create
20  * \param data some user data (may be NULL)
21  */
22 smx_host_t SIMIX_vm_create(const char *name, smx_host_t phys_host)
23 {
24
25   smx_host_priv_t smx_host = xbt_new0(s_smx_host_priv_t, 1);
26   s_smx_process_t proc;
27
28   // TODO check why we do not have any VM here and why we have the host_proc_hookup  ?
29
30   /* Host structure */
31   smx_host->data = NULL;
32   smx_host->process_list =
33       xbt_swag_new(xbt_swag_offset(proc, host_proc_hookup));
34
35   /* Update global variables */
36   xbt_lib_set(host_lib,name,SIMIX_HOST_LEVEL,smx_host);
37
38   /* Create surf associated resource */
39   // TODO change phys_host into the right workstation surf model
40   surf_vm_workstation_model->extension.vm_workstation.create(name, phys_host);
41
42   return xbt_lib_get_elm_or_null(host_lib, name);
43 }
44
45
46 smx_host_t SIMIX_pre_vm_create(smx_simcall_t simcall, const char *name, smx_host_t phys_host){
47    return SIMIX_vm_create(name, phys_host);
48 }
49
50
51 /* **** start a VM **** */
52 int __can_be_started(smx_host_t vm){
53         // TODO add checking code related to overcommitment or not.
54         return 1;
55 }
56 void SIMIX_vm_start(smx_host_t vm){
57
58   //TODO only start the VM if you can
59   if (can_be_started(vm))
60           SIMIX_set_vm_state(vm, msg_vm_state_running);
61   else
62           THROWF(vm_error, 0, "The VM %s cannot be started", SIMIX_host_get_name(vm));
63 }
64
65 void SIMIX_pre_vm_start(smx_simcall_t simcall, smx_host_t vm){
66    SIMIX_vm_start(vm);
67 }
68
69 /* ***** set/get state of a VM ***** */
70 void SIMIX_set_vm_state(smx_host_t vm, int state){
71
72 }
73 void SIMIX_prev_set_vm_state(smx_host_t vm, int state){
74         SIMIX_set_vm_state(vm, state);
75 }
76
77 int SIMIX_get_vm_state(smx_host_t vm){
78  return surf_vm_workstation_model->extension.vm_workstation.get_state(vm);
79 }
80 int SIMIX_pre_vm_state(smx_host_t vm){
81         return SIMIX_get_vm_state(vm);
82 }
83
84 /**
85  * \brief Function to destroy a SIMIX VM host.
86  *
87  * \param host the vm host to destroy (a smx_host_t)
88  */
89 void SIMIX_vm_destroy(smx_host_t host)
90 {
91   /* this code basically performs a similar thing like SIMIX_host_destroy() */
92
93   xbt_assert((host != NULL), "Invalid parameters");
94   char *hostname = host->key;
95
96   smx_host_priv_t host_priv = SIMIX_host_priv(host);
97
98   /* this will call the registered callback function, i.e., SIMIX_host_destroy().  */
99   xbt_lib_unset(host_lib, name, SIMIX_HOST_LEVEL)
100
101   /* jump to vm_ws_destroy(). The surf level resource will be freed. */
102   surf_vm_workstation_model->extension.vm_workstation.destroy(hostname);
103 }
104
105 void SIMIX_pre_vm_destroy(smx_simcall_t simcall, smx_host_t vm){
106    SIMIX_vm_start(vm);
107 }
108
109 smx_host_t SIMIX_host_get_by_name(const char *name){
110   xbt_assert(((simix_global != NULL)
111                && (host_lib != NULL)),
112               "Environment not set yet");
113
114   return xbt_lib_get_elm_or_null(host_lib, name);
115 }
116
117 smx_host_t SIMIX_host_self(void)
118 {
119   smx_process_t process = SIMIX_process_self();
120   return (process == NULL) ? NULL : SIMIX_process_get_host(process);
121 }
122
123 const char* SIMIX_pre_host_self_get_name(smx_simcall_t simcall){
124    return SIMIX_host_self_get_name();
125 }
126 /* needs to be public and without simcall because it is called
127    by exceptions and logging events */
128 const char* SIMIX_host_self_get_name(void)
129 {
130   smx_host_t host = SIMIX_host_self();
131   if (host == NULL || SIMIX_process_self() == simix_global->maestro_process)
132     return "";
133
134   return SIMIX_host_get_name(host);
135 }
136
137 const char* SIMIX_pre_host_get_name(smx_simcall_t simcall, smx_host_t host){
138    return SIMIX_host_get_name(host);
139 }
140 const char* SIMIX_host_get_name(smx_host_t host){
141   xbt_assert((host != NULL), "Invalid parameters");
142
143   return sg_host_name(host);
144 }
145
146 xbt_dict_t SIMIX_pre_host_get_properties(smx_simcall_t simcall, smx_host_t host){
147   return SIMIX_host_get_properties(host);
148 }
149 xbt_dict_t SIMIX_host_get_properties(smx_host_t host){
150   xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
151
152   return surf_workstation_model->extension.workstation.get_properties(host);
153 }
154
155 double SIMIX_pre_host_get_speed(smx_simcall_t simcall, smx_host_t host){
156   return SIMIX_host_get_speed(host);
157 }
158 double SIMIX_host_get_speed(smx_host_t host){
159   xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
160
161   return surf_workstation_model->extension.workstation.
162       get_speed(host, 1.0);
163 }
164
165 double SIMIX_pre_host_get_available_speed(smx_simcall_t simcall, smx_host_t host){
166   return SIMIX_host_get_available_speed(host);
167 }
168 double SIMIX_host_get_available_speed(smx_host_t host){
169   xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
170
171   return surf_workstation_model->extension.workstation.
172       get_available_speed(host);
173 }
174
175 int SIMIX_pre_host_get_state(smx_simcall_t simcall, smx_host_t host){
176   return SIMIX_host_get_state(host);
177 }
178 int SIMIX_host_get_state(smx_host_t host){
179   xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
180
181   return surf_workstation_model->extension.workstation.
182       get_state(host);
183 }
184
185 void* SIMIX_pre_host_self_get_data(smx_simcall_t simcall){
186   return SIMIX_host_self_get_data();
187 }
188 void* SIMIX_host_self_get_data(void)
189 {
190   smx_host_t self = SIMIX_host_self();
191   return SIMIX_host_get_data(self);
192 }
193
194 void SIMIX_host_self_set_data(void *data)
195 {
196   smx_host_t self = SIMIX_host_self();
197   SIMIX_host_set_data(self, data);
198 }
199
200 void* SIMIX_pre_host_get_data(smx_simcall_t simcall,smx_host_t host){
201   return SIMIX_host_get_data(host);
202 }
203 void* SIMIX_host_get_data(smx_host_t host){
204   xbt_assert((host != NULL), "Invalid parameters (simix host is NULL)");
205
206   return SIMIX_host_priv(host)->data;
207 }
208 void _SIMIX_host_free_process_arg(void *);
209 void _SIMIX_host_free_process_arg(void *data)
210 {
211   smx_process_arg_t arg = *(void**)data;
212   xbt_free(arg->name);
213   xbt_free(arg);
214 }
215 /**
216  * \brief Add a process to the list of the processes that the host will restart when it comes back
217  * This function add a process to the list of the processes that will be restarted when the host comes
218  * back. It is expected that this function is called when the host is down.
219  * The processes will only be restarted once, meaning that you will have to register the process
220  * again to restart the process again.
221  */
222 void SIMIX_host_add_auto_restart_process(smx_host_t host,
223                                          const char *name,
224                                          xbt_main_func_t code,
225                                          void *data,
226                                          const char *hostname,
227                                          double kill_time,
228                                          int argc, char **argv,
229                                          xbt_dict_t properties,
230                                          int auto_restart)
231 {
232   if (!SIMIX_host_priv(host)->auto_restart_processes) {
233     SIMIX_host_priv(host)->auto_restart_processes = xbt_dynar_new(sizeof(smx_process_arg_t),_SIMIX_host_free_process_arg);
234   }
235   smx_process_arg_t arg = xbt_new(s_smx_process_arg_t,1);
236   arg->name = xbt_strdup(name);
237   arg->code = code;
238   arg->data = data;
239   arg->hostname = hostname;
240   arg->kill_time = kill_time;
241   arg->argc = argc;
242
243   arg->argv = xbt_new(char*,argc + 1);
244
245   int i;
246   for (i = 0; i < argc; i++) {
247     arg->argv[i] = xbt_strdup(argv[i]);
248   }
249   arg->argv[argc] = NULL;
250
251   arg->properties = properties;
252   arg->auto_restart = auto_restart;
253
254   if( SIMIX_host_get_state(host) == SURF_RESOURCE_OFF
255       && !xbt_dict_get_or_null(watched_hosts_lib,sg_host_name(host))){
256     xbt_dict_set(watched_hosts_lib,sg_host_name(host),host,NULL);
257     XBT_DEBUG("Have push host %s to watched_hosts_lib because state == SURF_RESOURCE_OFF",sg_host_name(host));
258   }
259   xbt_dynar_push_as(SIMIX_host_priv(host)->auto_restart_processes,smx_process_arg_t,arg);
260 }
261 /**
262  * \brief Restart the list of processes that have been registered to the host
263  */
264 void SIMIX_host_restart_processes(smx_host_t host)
265 {
266   unsigned int cpt;
267   smx_process_arg_t arg;
268   xbt_dynar_foreach(SIMIX_host_priv(host)->auto_restart_processes,cpt,arg) {
269
270     smx_process_t process;
271
272     XBT_DEBUG("Restarting Process %s(%s) right now", arg->argv[0], arg->hostname);
273     if (simix_global->create_process_function) {
274       simix_global->create_process_function(&process,
275                                             arg->argv[0],
276                                             arg->code,
277                                             NULL,
278                                             arg->hostname,
279                                             arg->kill_time,
280                                             arg->argc,
281                                             arg->argv,
282                                             arg->properties,
283                                             arg->auto_restart);
284     }
285     else {
286       simcall_process_create(&process,
287                                             arg->argv[0],
288                                             arg->code,
289                                             NULL,
290                                             arg->hostname,
291                                             arg->kill_time,
292                                             arg->argc,
293                                             arg->argv,
294                                             arg->properties,
295                                             arg->auto_restart);
296
297     }
298   }
299   xbt_dynar_reset(SIMIX_host_priv(host)->auto_restart_processes);
300 }
301
302 void SIMIX_host_autorestart(smx_host_t host)
303 {
304   if(simix_global->autorestart)
305     simix_global->autorestart(host);
306   else
307     xbt_die("No function for simix_global->autorestart");
308 }
309
310 void SIMIX_pre_host_set_data(smx_simcall_t simcall, smx_host_t host, void *data) {
311   SIMIX_host_set_data(host, data);
312 }
313 void SIMIX_host_set_data(smx_host_t host, void *data){
314   xbt_assert((host != NULL), "Invalid parameters");
315   xbt_assert((SIMIX_host_priv(host)->data == NULL), "Data already set");
316
317   SIMIX_host_priv(host)->data = data;
318 }
319
320 smx_action_t SIMIX_pre_host_execute(smx_simcall_t simcall,const char *name,
321     smx_host_t host, double computation_amount, double priority){
322   return SIMIX_host_execute(name, host, computation_amount, priority);
323 }
324 smx_action_t SIMIX_host_execute(const char *name,
325     smx_host_t host, double computation_amount, double priority){
326
327   /* alloc structures and initialize */
328   smx_action_t action = xbt_mallocator_get(simix_global->action_mallocator);
329   action->type = SIMIX_ACTION_EXECUTE;
330   action->name = xbt_strdup(name);
331   action->state = SIMIX_RUNNING;
332   action->execution.host = host;
333
334 #ifdef HAVE_TRACING
335   action->category = NULL;
336 #endif
337
338   /* set surf's action */
339   if (!MC_is_active()) {
340     action->execution.surf_exec =
341       surf_workstation_model->extension.workstation.execute(host,
342     computation_amount);
343     surf_workstation_model->action_data_set(action->execution.surf_exec, action);
344     surf_workstation_model->set_priority(action->execution.surf_exec, priority);
345   }
346
347   XBT_DEBUG("Create execute action %p", action);
348
349   return action;
350 }
351
352 smx_action_t SIMIX_pre_host_parallel_execute(smx_simcall_t simcall, const char *name,
353     int host_nb, smx_host_t *host_list,
354     double *computation_amount, double *communication_amount,
355     double amount, double rate){
356   return SIMIX_host_parallel_execute(name, host_nb, host_list, computation_amount,
357                                      communication_amount, amount, rate);
358 }
359 smx_action_t SIMIX_host_parallel_execute(const char *name,
360     int host_nb, smx_host_t *host_list,
361     double *computation_amount, double *communication_amount,
362     double amount, double rate){
363
364   void **workstation_list = NULL;
365   int i;
366
367   /* alloc structures and initialize */
368   smx_action_t action = xbt_mallocator_get(simix_global->action_mallocator);
369   action->type = SIMIX_ACTION_PARALLEL_EXECUTE;
370   action->name = xbt_strdup(name);
371   action->state = SIMIX_RUNNING;
372   action->execution.host = NULL; /* FIXME: do we need the list of hosts? */
373
374 #ifdef HAVE_TRACING
375   action->category = NULL;
376 #endif
377
378   /* set surf's action */
379   workstation_list = xbt_new0(void *, host_nb);
380   for (i = 0; i < host_nb; i++)
381     workstation_list[i] = host_list[i];
382
383   /* set surf's action */
384   if (!MC_is_active()) {
385     action->execution.surf_exec =
386       surf_workstation_model->extension.workstation.
387       execute_parallel_task(host_nb, workstation_list, computation_amount,
388                       communication_amount, rate);
389
390     surf_workstation_model->action_data_set(action->execution.surf_exec, action);
391   }
392   XBT_DEBUG("Create parallel execute action %p", action);
393
394   return action;
395 }
396
397 void SIMIX_pre_host_execution_destroy(smx_simcall_t simcall, smx_action_t action){
398   SIMIX_host_execution_destroy(action);
399 }
400 void SIMIX_host_execution_destroy(smx_action_t action){
401   XBT_DEBUG("Destroy action %p", action);
402
403   if (action->execution.surf_exec) {
404     surf_workstation_model->action_unref(action->execution.surf_exec);
405     action->execution.surf_exec = NULL;
406   }
407   xbt_free(action->name);
408   xbt_mallocator_release(simix_global->action_mallocator, action);
409 }
410
411 void SIMIX_pre_host_execution_cancel(smx_simcall_t simcall, smx_action_t action){
412   SIMIX_host_execution_cancel(action);
413 }
414 void SIMIX_host_execution_cancel(smx_action_t action){
415   XBT_DEBUG("Cancel action %p", action);
416
417   if (action->execution.surf_exec)
418     surf_workstation_model->action_cancel(action->execution.surf_exec);
419 }
420
421 double SIMIX_pre_host_execution_get_remains(smx_simcall_t simcall, smx_action_t action){
422   return SIMIX_host_execution_get_remains(action);
423 }
424 double SIMIX_host_execution_get_remains(smx_action_t action){
425   double result = 0.0;
426
427   if (action->state == SIMIX_RUNNING)
428     result = surf_workstation_model->get_remains(action->execution.surf_exec);
429
430   return result;
431 }
432
433 e_smx_state_t SIMIX_pre_host_execution_get_state(smx_simcall_t simcall, smx_action_t action){
434   return SIMIX_host_execution_get_state(action);
435 }
436 e_smx_state_t SIMIX_host_execution_get_state(smx_action_t action){
437   return action->state;
438 }
439
440 void SIMIX_pre_host_execution_set_priority(smx_simcall_t simcall, smx_action_t action,
441                                         double priority){
442   return SIMIX_host_execution_set_priority(action, priority);
443 }
444 void SIMIX_host_execution_set_priority(smx_action_t action, double priority){
445   if(action->execution.surf_exec)
446     surf_workstation_model->set_priority(action->execution.surf_exec, priority);
447 }
448
449 void SIMIX_pre_host_execution_wait(smx_simcall_t simcall, smx_action_t action){
450
451   XBT_DEBUG("Wait for execution of action %p, state %d", action, (int)action->state);
452
453   /* Associate this simcall to the action */
454   xbt_fifo_push(action->simcalls, simcall);
455   simcall->issuer->waiting_action = action;
456
457   /* set surf's action */
458   if (MC_is_active()) {
459     action->state = SIMIX_DONE;
460     SIMIX_execution_finish(action);
461     return;
462   }
463
464   /* If the action is already finished then perform the error handling */
465   if (action->state != SIMIX_RUNNING)
466     SIMIX_execution_finish(action);
467 }
468
469 void SIMIX_host_execution_suspend(smx_action_t action)
470 {
471   if(action->execution.surf_exec)
472     surf_workstation_model->suspend(action->execution.surf_exec);
473 }
474
475 void SIMIX_host_execution_resume(smx_action_t action)
476 {
477   if(action->execution.surf_exec)
478     surf_workstation_model->resume(action->execution.surf_exec);
479 }
480
481 void SIMIX_execution_finish(smx_action_t action)
482 {
483   xbt_fifo_item_t item;
484   smx_simcall_t simcall;
485
486   xbt_fifo_foreach(action->simcalls, item, simcall, smx_simcall_t) {
487
488     switch (action->state) {
489
490       case SIMIX_DONE:
491         /* do nothing, action done */
492   XBT_DEBUG("SIMIX_execution_finished: execution successful");
493         break;
494
495       case SIMIX_FAILED:
496         XBT_DEBUG("SIMIX_execution_finished: host '%s' failed", sg_host_name(simcall->issuer->smx_host));
497         simcall->issuer->context->iwannadie = 1;
498         //SMX_EXCEPTION(simcall->issuer, host_error, 0, "Host failed");
499         break;
500
501       case SIMIX_CANCELED:
502         XBT_DEBUG("SIMIX_execution_finished: execution canceled");
503         SMX_EXCEPTION(simcall->issuer, cancel_error, 0, "Canceled");
504         break;
505
506       default:
507         xbt_die("Internal error in SIMIX_execution_finish: unexpected action state %d",
508             (int)action->state);
509     }
510     /* check if the host is down */
511     if (surf_workstation_model->extension.
512         workstation.get_state(simcall->issuer->smx_host) != SURF_RESOURCE_ON) {
513       simcall->issuer->context->iwannadie = 1;
514     }
515
516     simcall->issuer->waiting_action =    NULL;
517     simcall_host_execution_wait__set__result(simcall, action->state);
518     SIMIX_simcall_answer(simcall);
519   }
520
521   /* We no longer need it */
522   SIMIX_host_execution_destroy(action);
523 }
524
525 void SIMIX_post_host_execute(smx_action_t action)
526 {
527   if (action->type == SIMIX_ACTION_EXECUTE && /* FIMXE: handle resource failure
528                                                * for parallel tasks too */
529       surf_workstation_model->extension.workstation.get_state(action->execution.host) == SURF_RESOURCE_OFF) {
530     /* If the host running the action failed, notice it so that the asking
531      * process can be killed if it runs on that host itself */
532     action->state = SIMIX_FAILED;
533   } else if (surf_workstation_model->action_state_get(action->execution.surf_exec) == SURF_ACTION_FAILED) {
534     /* If the host running the action didn't fail, then the action was
535      * canceled */
536     action->state = SIMIX_CANCELED;
537   } else {
538     action->state = SIMIX_DONE;
539   }
540
541   if (action->execution.surf_exec) {
542     surf_workstation_model->action_unref(action->execution.surf_exec);
543     action->execution.surf_exec = NULL;
544   }
545
546   /* If there are simcalls associated with the action, then answer them */
547   if (xbt_fifo_size(action->simcalls)) {
548     SIMIX_execution_finish(action);
549   }
550 }
551
552
553 #ifdef HAVE_TRACING
554 void SIMIX_pre_set_category(smx_simcall_t simcall, smx_action_t action,
555                             const char *category){
556   SIMIX_set_category(action, category);
557 }
558 void SIMIX_set_category(smx_action_t action, const char *category)
559 {
560   if (action->state != SIMIX_RUNNING) return;
561   if (action->type == SIMIX_ACTION_EXECUTE){
562     surf_workstation_model->set_category(action->execution.surf_exec, category);
563   }else if (action->type == SIMIX_ACTION_COMMUNICATE){
564     surf_workstation_model->set_category(action->comm.surf_comm, category);
565   }
566 }
567 #endif
568