Logo AND Algorithmique Numérique Distribuée

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