Logo AND Algorithmique Numérique Distribuée

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