Logo AND Algorithmique Numérique Distribuée

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