Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Display the status of simulated processes when receiving SIGINT
[simgrid.git] / src / simix / smx_global.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2007 Arnaud Legrand, Bruno Donassolo.
4    All rights reserved.                                          */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "private.h"
10 #include "xbt/sysdep.h"
11 #include "xbt/log.h"
12 #include "xbt/str.h"
13 #include "xbt/ex.h"             /* ex_backtrace_display */
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_kernel, simix,
15                                 "Logging specific to SIMIX (kernel)");
16
17 SIMIX_Global_t simix_global = NULL;
18
19 /********************************* SIMIX **************************************/
20 static void __simix_config_helper(const char *name, ...)
21 {
22   va_list pa;
23   va_start(pa, name);
24
25   SIMIX_config(name, pa);
26
27   va_end(pa);
28 }
29
30 static void simix_cfg_control_set(const char *control_string)
31 {
32   /* To split the string in commands, and the cursors */
33   xbt_dynar_t set_strings;
34   char *str;
35   unsigned int cpt;
36
37   if (!control_string)
38     return;
39   DEBUG1("Parse log settings '%s'", control_string);
40
41   /* split the string, and remove empty entries */
42   set_strings = xbt_str_split_quoted(control_string);
43
44   if (xbt_dynar_length(set_strings) == 0) {     /* vicious user! */
45     xbt_dynar_free(&set_strings);
46     return;
47   }
48   /* Parse each entry and either use it right now (if the category was already
49      created), or store it for further use */
50   xbt_dynar_foreach(set_strings, cpt, str) {
51     char *control_string, *control_string_sav, *name, *value;
52
53
54     control_string = control_string_sav = strdup(str);
55     control_string += strspn(control_string, " ");
56     name = control_string;
57     control_string += strcspn(str, ":=");
58     value = control_string;
59     *value = 0;
60     value++;
61
62     xbt_assert1(strlen(name) != 0, "Invalid name for configuration: '%s'",
63                 name);
64     xbt_assert1(strlen(value) != 0,
65                 "Invalid value for configuration: '%s'", value);
66     INFO2("setting '%s' to '%s'", name, value);
67
68     __simix_config_helper(name, value);
69
70     free(control_string_sav);
71   }
72   xbt_dynar_free(&set_strings);
73 }
74
75 static void simix_cfg_init(int *argc, char **argv)
76 {
77   int i, j;
78   char *opt;
79
80   for (i = 1; i < *argc; i++) {
81     if (!strncmp(argv[i], "--cfg=", strlen("--cfg="))) {
82       opt = strchr(argv[i], '=');
83       opt++;
84
85       simix_cfg_control_set(opt);
86       DEBUG1("Did apply '%s' as config setting", opt);
87       /*remove this from argv */
88
89       for (j = i + 1; j < *argc; j++) {
90         argv[j - 1] = argv[j];
91       }
92
93       argv[j - 1] = NULL;
94       (*argc)--;
95       i--;                      /* compensate effect of next loop incrementation */
96     }
97   }
98 }
99
100 /* FIXME: Yeah, I'll do it in a portable maner one day [Mt] */
101 #include <signal.h>
102
103 static void _XBT_CALL inthandler(int ignored)
104 {
105   INFO0("CTRL-C pressed. Displaying status and bailing out");
106   SIMIX_display_process_status();
107   exit(1);
108 }
109
110
111 /**
112  * \brief Initialize some SIMIX internal data.
113  *
114  * \param argc Argc
115  * \param argv Argv
116  */
117 void SIMIX_global_init(int *argc, char **argv)
118 {
119   s_smx_process_t proc;
120
121   if (!simix_global) {
122     surf_init(argc, argv);      /* Initialize some common structures. Warning, it sets simix_global=NULL */
123     simix_cfg_init(argc, argv);
124
125     simix_global = xbt_new0(s_SIMIX_Global_t, 1);
126
127     simix_global->host = xbt_dict_new();
128     simix_global->process_to_run =
129         xbt_swag_new(xbt_swag_offset(proc, synchro_hookup));
130     simix_global->process_list =
131         xbt_swag_new(xbt_swag_offset(proc, process_hookup));
132     simix_global->current_process = NULL;
133     simix_global->registered_functions = xbt_dict_new();
134
135     simix_global->create_process_function = NULL;
136     simix_global->kill_process_function = NULL;
137     simix_global->cleanup_process_function = SIMIX_process_cleanup;
138
139     /* Prepare to display some more info when dying on Ctrl-C pressing */
140     signal(SIGINT, inthandler);
141   }
142 }
143
144 /* Debug purpose, incomplete */
145 void SIMIX_display_process_status(void)
146 {
147   smx_process_t process = NULL;
148   xbt_fifo_item_t item = NULL;
149   smx_action_t act;
150   int nbprocess = xbt_swag_size(simix_global->process_list);
151
152   INFO1("%d processes are still running, waiting for something.",
153         nbprocess);
154   /*  List the process and their state */
155   INFO0
156       ("Legend of the following listing: \"<process> on <host>: <status>.\"");
157   xbt_swag_foreach(process, simix_global->process_list) {
158     smx_simdata_process_t p_simdata =
159         (smx_simdata_process_t) process->simdata;
160     // simdata_host_t h_simdata=(simdata_host_t)p_simdata->host->simdata;
161     char *who, *who2;
162
163     asprintf(&who, "%s on %s: %s",
164              process->name,
165              p_simdata->smx_host->name,
166              (process->simdata->blocked) ? "[BLOCKED] "
167              : ((process->simdata->suspended) ? "[SUSPENDED] " : ""));
168
169     if (p_simdata->mutex) {
170                 who2 = bprintf("%s Blocked on mutex %p", who, (XBT_LOG_ISENABLED(simix_kernel,xbt_log_priority_verbose))?p_simdata->mutex:(void*)0xdead);
171       free(who);
172       who = who2;
173     } else if (p_simdata->cond) {
174       who2 =
175           bprintf
176           ("%s Blocked on condition %p; Waiting for the following actions:",
177            who, (XBT_LOG_ISENABLED(simix_kernel,xbt_log_priority_verbose))?p_simdata->cond:(void*)0xdead);
178       free(who);
179       who = who2;
180       xbt_fifo_foreach(p_simdata->cond->actions, item, act, smx_action_t) {
181                   who2 = bprintf("%s '%s'(%p)", who, act->name,(XBT_LOG_ISENABLED(simix_kernel,xbt_log_priority_verbose))?act:(void*)0xdead);
182         free(who);
183         who = who2;
184       }
185     } else {
186       who2 =
187           bprintf
188           ("%s Blocked in an unknown status (please report this bug)",
189            who);
190       free(who);
191       who = who2;
192     }
193     INFO1("%s.", who);
194     free(who);
195   }
196 }
197
198
199 /**
200  * \brief Launch the SIMIX simulation, debug purpose
201  */
202 void __SIMIX_main(void)
203 {
204   smx_process_t process = NULL;
205   smx_cond_t cond = NULL;
206   smx_action_t smx_action;
207   xbt_fifo_t actions_done = xbt_fifo_new();
208   xbt_fifo_t actions_failed = xbt_fifo_new();
209
210   /* Clean IO before the run */
211   fflush(stdout);
212   fflush(stderr);
213
214   //surf_solve(); /* Takes traces into account. Returns 0.0 */
215   /* xbt_fifo_size(msg_global->process_to_run) */
216
217   while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
218
219     while ((smx_action = xbt_fifo_pop(actions_failed))) {
220
221       xbt_fifo_item_t _cursor;
222
223       DEBUG1("** %s failed **", smx_action->name);
224       xbt_fifo_foreach(smx_action->cond_list, _cursor, cond, smx_cond_t) {
225         xbt_swag_foreach(process, cond->sleeping) {
226           DEBUG2("\t preparing to wake up %s on %s",
227                  process->name, process->simdata->smx_host->name);
228         }
229         SIMIX_cond_broadcast(cond);
230         /* remove conditional from action */
231         SIMIX_unregister_action_to_condition(smx_action, cond);
232       }
233     }
234
235     while ((smx_action = xbt_fifo_pop(actions_done))) {
236       xbt_fifo_item_t _cursor;
237
238       DEBUG1("** %s done **", smx_action->name);
239       xbt_fifo_foreach(smx_action->cond_list, _cursor, cond, smx_cond_t) {
240         xbt_swag_foreach(process, cond->sleeping) {
241           DEBUG2("\t preparing to wake up %s on %s",
242                  process->name, process->simdata->smx_host->name);
243         }
244         SIMIX_cond_broadcast(cond);
245         /* remove conditional from action */
246         SIMIX_unregister_action_to_condition(smx_action, cond);
247       }
248     }
249   }
250   return;
251 }
252
253 /**
254  * \brief Kill all running process
255  *
256  */
257 void SIMIX_process_killall()
258 {
259   smx_process_t p = NULL;
260   smx_process_t self = SIMIX_process_self();
261
262   while ((p = xbt_swag_extract(simix_global->process_list))) {
263     if (p != self)
264       SIMIX_process_kill(p);
265   }
266
267   xbt_context_empty_trash();
268
269   if (self) {
270     xbt_context_yield();
271   }
272
273   return;
274 }
275
276 /**
277  * \brief Clean the SIMIX simulation
278  *
279  * This functions remove all memories needed to the SIMIX execution
280  */
281 void SIMIX_clean(void)
282 {
283   smx_process_t p = NULL;
284
285   while ((p = xbt_swag_extract(simix_global->process_list))) {
286     SIMIX_process_kill(p);
287   }
288
289   xbt_dict_free(&(simix_global->host));
290   xbt_swag_free(simix_global->process_to_run);
291   xbt_swag_free(simix_global->process_list);
292   xbt_dict_free(&(simix_global->registered_functions));
293   simix_config_finalize();
294   free(simix_global);
295   simix_global = NULL;
296
297   surf_exit();
298
299   return;
300 }
301
302
303 /**
304  * \brief A clock (in second).
305  *
306  * \return Return the clock.
307  */
308 double SIMIX_get_clock(void)
309 {
310   return surf_get_clock();
311 }
312
313 /**
314  *      \brief Finish the simulation initialization
315  *
316  *      Must be called before the first call to SIMIX_solve()
317  */
318 void SIMIX_init(void) {
319     surf_presolve();
320 }
321
322 /**
323  *      \brief Does a turn of the simulation
324  *
325  *      Executes a step in the surf simulation, adding to the two lists all the actions that finished on this turn. Schedules all processus in the process_to_run list.
326  *      \param actions_done List of actions done
327  *      \param actions_failed List of actions failed
328  *      \return The time spent to execute the simulation or -1 if the simulation ended
329  */
330 double SIMIX_solve(xbt_fifo_t actions_done, xbt_fifo_t actions_failed)
331 {
332
333   smx_process_t process = NULL;
334   unsigned int iter;
335   double elapsed_time = 0.0;
336   static int state_modifications = 1;
337
338   xbt_context_empty_trash();
339   if (xbt_swag_size(simix_global->process_to_run) && (elapsed_time > 0)) {
340     DEBUG0("**************************************************");
341   }
342
343   while ((process = xbt_swag_extract(simix_global->process_to_run))) {
344     DEBUG2("Scheduling %s on %s",
345            process->name, process->simdata->smx_host->name);
346     simix_global->current_process = process;
347     xbt_context_schedule(process->simdata->context);
348     /*       fflush(NULL); */
349     simix_global->current_process = NULL;
350   }
351
352   {
353     surf_action_t action = NULL;
354     surf_model_t model = NULL;
355     smx_action_t smx_action = NULL;
356
357     void *fun = NULL;
358     void *arg = NULL;
359
360     xbt_dynar_foreach(model_list, iter, model) {
361       if (xbt_swag_size(model->common_public->states.failed_action_set)
362           || xbt_swag_size(model->common_public->states.
363                            done_action_set)) {
364         state_modifications = 1;
365          break;
366       }
367     }
368
369     if (!state_modifications) {
370       DEBUG1("%f : Calling surf_solve", SIMIX_get_clock());
371       elapsed_time = surf_solve();
372       DEBUG1("Elapsed_time %f", elapsed_time);
373     }
374
375     while (surf_timer_model->extension_public->get(&fun, (void *) &arg)) {
376       DEBUG2("got %p %p", fun, arg);
377       if (fun == SIMIX_process_create) {
378         smx_process_arg_t args = arg;
379         DEBUG2("Launching %s on %s", args->name, args->hostname);
380         process = SIMIX_process_create(args->name, args->code,
381                                        args->data, args->hostname,
382                                        args->argc, args->argv, args->properties);
383         if (args->kill_time > SIMIX_get_clock()) {
384           surf_timer_model->extension_public->set(args->kill_time,
385                                                      (void *)
386                                                      &SIMIX_process_kill,
387                                                      (void *) process);
388         }
389         xbt_free(args);
390       }
391       if (fun == SIMIX_process_kill) {
392         process = arg;
393         DEBUG2("Killing %s on %s", process->name,
394                process->simdata->smx_host->name);
395         SIMIX_process_kill(process);
396       }
397     }
398
399     /* Wake up all process waiting for the action finish */
400     xbt_dynar_foreach(model_list, iter, model) {
401       while ((action =
402               xbt_swag_extract(model->common_public->states.
403                                failed_action_set))) {
404         smx_action = action->data;
405         if (smx_action) {
406           xbt_fifo_unshift(actions_failed, smx_action);
407         }
408       }
409       while ((action =
410               xbt_swag_extract(model->common_public->states.
411                                done_action_set))) {
412         smx_action = action->data;
413         if (smx_action) {
414           xbt_fifo_unshift(actions_done, smx_action);
415         }
416       }
417     }
418   }
419   state_modifications = 0;
420
421   if (elapsed_time == -1) {
422     if (xbt_swag_size(simix_global->process_list) == 0) {
423 /*                      INFO0("Congratulations ! Simulation terminated : all processes are over"); */
424     } else {
425       INFO0("Oops ! Deadlock or code not perfectly clean.");
426       SIMIX_display_process_status();
427       if (XBT_LOG_ISENABLED(simix, xbt_log_priority_debug) ||
428           XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_debug)) {
429         DEBUG0("Aborting!");
430         xbt_abort();
431       }
432       INFO0("Return a Warning.");
433     }
434   }
435   return elapsed_time;
436 }
437
438 /**
439  *      \brief Set the date to execute a function
440  *
441  * Set the date to execute the function on the surf.
442  *      \param date Date to execute function
443  *      \param function Function to be executed
444  *      \param arg Parameters of the function
445  *
446  */
447 void SIMIX_timer_set(double date, void *function, void *arg)
448 {
449   surf_timer_model->extension_public->set(date, function, arg);
450 }
451
452 int SIMIX_timer_get(void **function, void **arg)
453 {
454   return surf_timer_model->extension_public->get(function, arg);
455 }
456
457 /**
458  *      \brief Registers a function to create a process.
459  *
460  *      This function registers an user function to be called when a new process is created. The user function have to call the SIMIX_create_process function.
461  *      \param function Create process function
462  *
463  */
464 void SIMIX_function_register_process_create(smx_creation_func_t function)
465 {
466   xbt_assert0((simix_global->create_process_function == NULL),
467               "Data already set");
468
469   simix_global->create_process_function = function;
470 }
471
472 /**
473  *      \brief Registers a function to kill a process.
474  *
475  *      This function registers an user function to be called when a new process is killed. The user function have to call the SIMIX_kill_process function.
476  *      \param function Kill process function
477  *
478  */
479 void SIMIX_function_register_process_kill(void_f_pvoid_t function)
480 {
481   xbt_assert0((simix_global->kill_process_function == NULL),
482               "Data already set");
483
484   simix_global->kill_process_function = function;
485 }
486
487 /**
488  *      \brief Registers a function to cleanup a process.
489  *
490  *      This function registers an user function to be called when a new process ends properly.
491  *      \param function cleanup process function
492  *
493  */
494 void SIMIX_function_register_process_cleanup(void_f_pvoid_t function)
495 {
496   simix_global->cleanup_process_function = function;
497 }