Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
192a81608216b919edbe9e9eb18015650405c700
[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 /**
101  * \brief Initialize some SIMIX internal data.
102  *
103  * \param argc Argc
104  * \param argv Argv
105  */
106 void SIMIX_global_init(int *argc, char **argv)
107 {
108   s_smx_process_t proc;
109
110   if (!simix_global) {
111     surf_init(argc, argv);      /* Initialize some common structures. Warning, it sets simix_global=NULL */
112     simix_cfg_init(argc, argv);
113
114     simix_global = xbt_new0(s_SIMIX_Global_t, 1);
115
116     simix_global->host = xbt_fifo_new();
117     simix_global->process_to_run =
118         xbt_swag_new(xbt_swag_offset(proc, synchro_hookup));
119     simix_global->process_list =
120         xbt_swag_new(xbt_swag_offset(proc, process_hookup));
121     simix_global->current_process = NULL;
122     simix_global->registered_functions = xbt_dict_new();
123
124     simix_global->create_process_function = NULL;
125     simix_global->kill_process_function = NULL;
126     simix_global->cleanup_process_function = SIMIX_process_cleanup;
127   }
128 }
129
130 /* Debug purpose, incomplete */
131 void SIMIX_display_process_status(void)
132 {
133   smx_process_t process = NULL;
134   xbt_fifo_item_t item = NULL;
135   smx_action_t act;
136   int nbprocess = xbt_swag_size(simix_global->process_list);
137
138   INFO1("%d processes are still running, waiting for something.",
139         nbprocess);
140   /*  List the process and their state */
141   INFO0
142       ("Legend of the following listing: \"<process> on <host>: <status>.\"");
143   xbt_swag_foreach(process, simix_global->process_list) {
144     smx_simdata_process_t p_simdata =
145         (smx_simdata_process_t) process->simdata;
146     // simdata_host_t h_simdata=(simdata_host_t)p_simdata->host->simdata;
147     char *who, *who2;
148
149     asprintf(&who, "%s on %s: %s",
150              process->name,
151              p_simdata->smx_host->name,
152              (process->simdata->blocked) ? "[BLOCKED] "
153              : ((process->simdata->suspended) ? "[SUSPENDED] " : ""));
154
155     if (p_simdata->mutex) {
156       who2 = bprintf("%s Blocked on mutex %p", who, p_simdata->mutex);
157       free(who);
158       who = who2;
159     } else if (p_simdata->cond) {
160       who2 =
161           bprintf
162           ("%s Blocked on condition %p; Waiting for the following actions:",
163            who, p_simdata->cond);
164       free(who);
165       who = who2;
166       xbt_fifo_foreach(p_simdata->cond->actions, item, act, smx_action_t) {
167         who2 = bprintf("%s '%s'(%p)", who, act->name,act);
168         free(who);
169         who = who2;
170       }
171     } else {
172       who2 =
173           bprintf
174           ("%s Blocked in an unknown status (please report this bug)",
175            who);
176       free(who);
177       who = who2;
178     }
179     INFO1("%s.", who);
180     free(who);
181   }
182 }
183
184 /* FIXME: Yeah, I'll do it in a portable maner one day [Mt] */
185 #include <signal.h>
186
187 static void _XBT_CALL inthandler(int ignored)
188 {
189   INFO0("CTRL-C pressed. Displaying status and bailing out");
190   SIMIX_display_process_status();
191   exit(1);
192 }
193
194 /**
195  * \brief Launch the SIMIX simulation, debug purpose
196  */
197 void __SIMIX_main(void)
198 {
199   smx_process_t process = NULL;
200   smx_cond_t cond = NULL;
201   smx_action_t smx_action;
202   xbt_fifo_t actions_done = xbt_fifo_new();
203   xbt_fifo_t actions_failed = xbt_fifo_new();
204
205   /* Prepare to display some more info when dying on Ctrl-C pressing */
206   signal(SIGINT, inthandler);
207
208   /* Clean IO before the run */
209   fflush(stdout);
210   fflush(stderr);
211
212   //surf_solve(); /* Takes traces into account. Returns 0.0 */
213   /* xbt_fifo_size(msg_global->process_to_run) */
214
215   while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
216
217     while ((smx_action = xbt_fifo_pop(actions_failed))) {
218
219       xbt_fifo_item_t _cursor;
220
221       DEBUG1("** %s failed **", smx_action->name);
222       xbt_fifo_foreach(smx_action->cond_list, _cursor, cond, smx_cond_t) {
223         xbt_swag_foreach(process, cond->sleeping) {
224           DEBUG2("\t preparing to wake up %s on %s",
225                  process->name, process->simdata->smx_host->name);
226         }
227         SIMIX_cond_broadcast(cond);
228         /* remove conditional from action */
229         SIMIX_unregister_action_to_condition(smx_action, cond);
230       }
231     }
232
233     while ((smx_action = xbt_fifo_pop(actions_done))) {
234       xbt_fifo_item_t _cursor;
235
236       DEBUG1("** %s done **", smx_action->name);
237       xbt_fifo_foreach(smx_action->cond_list, _cursor, cond, smx_cond_t) {
238         xbt_swag_foreach(process, cond->sleeping) {
239           DEBUG2("\t preparing to wake up %s on %s",
240                  process->name, process->simdata->smx_host->name);
241         }
242         SIMIX_cond_broadcast(cond);
243         /* remove conditional from action */
244         SIMIX_unregister_action_to_condition(smx_action, cond);
245       }
246     }
247   }
248   return;
249 }
250
251 /**
252  * \brief Kill all running process
253  *
254  */
255 void SIMIX_process_killall()
256 {
257   smx_process_t p = NULL;
258   smx_process_t self = SIMIX_process_self();
259
260   while ((p = xbt_swag_extract(simix_global->process_list))) {
261     if (p != self)
262       SIMIX_process_kill(p);
263   }
264
265   xbt_context_empty_trash();
266
267   if (self) {
268     xbt_context_yield();
269   }
270
271   return;
272 }
273
274 /** 
275  * \brief Clean the SIMIX simulation
276  *
277  * This functions remove all memories needed to the SIMIX execution
278  */
279 void SIMIX_clean(void)
280 {
281   xbt_fifo_item_t i = NULL;
282   smx_host_t h = NULL;
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_fifo_foreach(simix_global->host, i, h, smx_host_t) {
290     __SIMIX_host_destroy(h);
291   }
292   xbt_fifo_free(simix_global->host);
293   xbt_swag_free(simix_global->process_to_run);
294   xbt_swag_free(simix_global->process_list);
295   xbt_dict_free(&(simix_global->registered_functions));
296   simix_config_finalize();
297   free(simix_global);
298   simix_global = NULL;
299   surf_exit();
300
301   return;
302 }
303
304
305 /**
306  * \brief A clock (in second).
307  *
308  * \return Return the clock.
309  */
310 double SIMIX_get_clock(void)
311 {
312   return surf_get_clock();
313 }
314
315 /**
316  *      \brief Does a turn of the simulation
317  *
318  *      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.         
319  *      \param actions_done List of actions done
320  *      \param actions_failed List of actions failed
321  *      \return The time spent to execute the simulation or -1 if the simulation ended
322  */
323 double SIMIX_solve(xbt_fifo_t actions_done, xbt_fifo_t actions_failed)
324 {
325
326   smx_process_t process = NULL;
327   unsigned int iter;
328   double elapsed_time = 0.0;
329   static int state_modifications = 1;
330   static int first = 1;
331
332   xbt_context_empty_trash();
333   if (xbt_swag_size(simix_global->process_to_run) && (elapsed_time > 0)) {
334     DEBUG0("**************************************************");
335   }
336   if (first) {
337     surf_solve();               /* Takes traces into account. Returns 0.0 */
338     first = 0;
339   }
340   while ((process = xbt_swag_extract(simix_global->process_to_run))) {
341     DEBUG2("Scheduling %s on %s",
342            process->name, process->simdata->smx_host->name);
343     simix_global->current_process = process;
344     xbt_context_schedule(process->simdata->context);
345     /*       fflush(NULL); */
346     simix_global->current_process = NULL;
347   }
348
349   {
350     surf_action_t action = NULL;
351     surf_model_t model = NULL;
352     smx_action_t smx_action = NULL;
353
354     void *fun = NULL;
355     void *arg = NULL;
356
357     xbt_dynar_foreach(model_list, iter, model) {
358       if (xbt_swag_size(model->common_public->states.failed_action_set)
359           || xbt_swag_size(model->common_public->states.
360                            done_action_set)) {
361         state_modifications = 1;
362          break;
363       }
364     }
365
366     if (!state_modifications) {
367       DEBUG1("%f : Calling surf_solve", SIMIX_get_clock());
368       elapsed_time = surf_solve();
369       DEBUG1("Elapsed_time %f", elapsed_time);
370     }
371
372     while (surf_timer_model->extension_public->get(&fun, (void *) &arg)) {
373       DEBUG2("got %p %p", fun, arg);
374       if (fun == SIMIX_process_create) {
375         smx_process_arg_t args = arg;
376         DEBUG2("Launching %s on %s", args->name, args->hostname);
377         process = SIMIX_process_create(args->name, args->code,
378                                        args->data, args->hostname,
379                                        args->argc, args->argv, args->properties);
380         if (args->kill_time > SIMIX_get_clock()) {
381           surf_timer_model->extension_public->set(args->kill_time,
382                                                      (void *)
383                                                      &SIMIX_process_kill,
384                                                      (void *) process);
385         }
386         xbt_free(args);
387       }
388       if (fun == SIMIX_process_kill) {
389         process = arg;
390         DEBUG2("Killing %s on %s", process->name,
391                process->simdata->smx_host->name);
392         SIMIX_process_kill(process);
393       }
394     }
395
396     /* Wake up all process waiting for the action finish */
397     xbt_dynar_foreach(model_list, iter, model) {
398       while ((action =
399               xbt_swag_extract(model->common_public->states.
400                                failed_action_set))) {
401         smx_action = action->data;
402         if (smx_action) {
403           xbt_fifo_unshift(actions_failed, smx_action);
404         }
405       }
406       while ((action =
407               xbt_swag_extract(model->common_public->states.
408                                done_action_set))) {
409         smx_action = action->data;
410         if (smx_action) {
411           xbt_fifo_unshift(actions_done, smx_action);
412         }
413       }
414     }
415   }
416   state_modifications = 0;
417
418   if (elapsed_time == -1) {
419     if (xbt_swag_size(simix_global->process_list) == 0) {
420 /*                      INFO0("Congratulations ! Simulation terminated : all processes are over"); */
421     } else {
422       INFO0("Oops ! Deadlock or code not perfectly clean.");
423       SIMIX_display_process_status();
424       if (XBT_LOG_ISENABLED(simix, xbt_log_priority_debug) ||
425           XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_debug)) {
426         DEBUG0("Aborting!");
427         xbt_abort();
428       }
429       INFO0("Return a Warning.");
430     }
431   }
432   return elapsed_time;
433 }
434
435 /**
436  *      \brief Set the date to execute a function
437  *
438  * Set the date to execute the function on the surf.
439  *      \param date Date to execute function
440  *      \param function Function to be executed
441  *      \param arg Parameters of the function
442  *
443  */
444 void SIMIX_timer_set(double date, void *function, void *arg)
445 {
446   surf_timer_model->extension_public->set(date, function, arg);
447 }
448
449 int SIMIX_timer_get(void **function, void **arg)
450 {
451   return surf_timer_model->extension_public->get(function, arg);
452 }
453
454 /**
455  *      \brief Registers a function to create a process.
456  *
457  *      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.
458  *      \param function Create process function
459  *
460  */
461 void SIMIX_function_register_process_create(smx_creation_func_t function)
462 {
463   xbt_assert0((simix_global->create_process_function == NULL),
464               "Data already set");
465
466   simix_global->create_process_function = function;
467 }
468
469 /**
470  *      \brief Registers a function to kill a process.
471  *
472  *      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.
473  *      \param function Kill process function
474  *
475  */
476 void SIMIX_function_register_process_kill(void_f_pvoid_t function)
477 {
478   xbt_assert0((simix_global->kill_process_function == NULL),
479               "Data already set");
480
481   simix_global->kill_process_function = function;
482 }
483
484 /**
485  *      \brief Registers a function to cleanup a process.
486  *
487  *      This function registers an user function to be called when a new process ends properly.
488  *      \param function cleanup process function
489  *
490  */
491 void SIMIX_function_register_process_cleanup(void_f_pvoid_t function)
492 {
493   simix_global->cleanup_process_function = function;
494 }