Logo AND Algorithmique Numérique Distribuée

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