Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Do not request status if not requested by caller.
[simgrid.git] / src / simix / smx_global.c
1 /* Copyright (c) 2007, 2008, 2009, 2010. 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 "private.h"
8 #include "xbt/sysdep.h"
9 #include "xbt/log.h"
10 #include "xbt/str.h"
11 #include "xbt/ex.h"             /* ex_backtrace_display */
12 XBT_LOG_EXTERNAL_CATEGORY(simix);
13 XBT_LOG_EXTERNAL_CATEGORY(simix_action);
14 XBT_LOG_EXTERNAL_CATEGORY(simix_deployment);
15 XBT_LOG_EXTERNAL_CATEGORY(simix_environment);
16 XBT_LOG_EXTERNAL_CATEGORY(simix_host);
17 XBT_LOG_EXTERNAL_CATEGORY(simix_process);
18 XBT_LOG_EXTERNAL_CATEGORY(simix_synchro);
19 XBT_LOG_EXTERNAL_CATEGORY(simix_context);
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_kernel, simix,
21                                 "Logging specific to SIMIX (kernel)");
22
23 SIMIX_Global_t simix_global = NULL;
24
25
26 /* FIXME: Yeah, I'll do it in a portable maner one day [Mt] */
27 #include <signal.h>
28
29 static void _XBT_CALL inthandler(int ignored)
30 {
31   INFO0("CTRL-C pressed. Displaying status and bailing out");
32   SIMIX_display_process_status();
33   exit(1);
34 }
35
36 /********************************* SIMIX **************************************/
37
38 /**
39  * \brief Initialize SIMIX internal data.
40  *
41  * \param argc Argc
42  * \param argv Argv
43  */
44 void SIMIX_global_init(int *argc, char **argv)
45 {
46   s_smx_process_t proc;
47
48   if (!simix_global) {
49     /* Connect our log channels: that must be done manually under windows */
50     XBT_LOG_CONNECT(simix_action, simix);
51     XBT_LOG_CONNECT(simix_deployment, simix);
52     XBT_LOG_CONNECT(simix_environment, simix);
53     XBT_LOG_CONNECT(simix_host, simix);
54     XBT_LOG_CONNECT(simix_kernel, simix);
55     XBT_LOG_CONNECT(simix_process, simix);
56     XBT_LOG_CONNECT(simix_synchro, simix);
57     XBT_LOG_CONNECT(simix_context, simix);
58
59     simix_global = xbt_new0(s_SIMIX_Global_t, 1);
60
61     simix_global->host = xbt_dict_new();
62     simix_global->process_to_run =
63         xbt_swag_new(xbt_swag_offset(proc, synchro_hookup));
64     simix_global->process_list =
65         xbt_swag_new(xbt_swag_offset(proc, process_hookup));
66     simix_global->process_to_destroy =
67         xbt_swag_new(xbt_swag_offset(proc, destroy_hookup));
68
69     simix_global->current_process = NULL;
70     simix_global->maestro_process = NULL;
71     simix_global->registered_functions = xbt_dict_new();
72
73     simix_global->create_process_function = NULL;
74     simix_global->kill_process_function = NULL;
75     simix_global->cleanup_process_function = SIMIX_process_cleanup;
76
77     simix_global->msg_sizes = xbt_dict_new();
78 #ifdef HAVE_LATENCY_BOUND_TRACKING
79     simix_global->latency_limited_dict = xbt_dict_new();
80 #endif
81
82     SIMIX_context_mod_init();
83     SIMIX_create_maestro_process();
84
85     /* context exception handlers */
86     __xbt_ex_ctx = SIMIX_process_get_exception;
87     __xbt_ex_terminate = SIMIX_process_exception_terminate;
88
89
90     /* Prepare to display some more info when dying on Ctrl-C pressing */
91     signal(SIGINT, inthandler);
92     surf_init(argc, argv);      /* Initialize SURF structures */
93   }
94 }
95
96 /* Debug purpose, incomplete */
97 void SIMIX_display_process_status(void)
98 {
99   smx_process_t process = NULL;
100   xbt_fifo_item_t item = NULL;
101   smx_action_t act;
102   int nbprocess = xbt_swag_size(simix_global->process_list);
103
104   INFO1("%d processes are still running, waiting for something.",
105         nbprocess);
106   /*  List the process and their state */
107   INFO0
108       ("Legend of the following listing: \"<process> on <host>: <status>.\"");
109   xbt_swag_foreach(process, simix_global->process_list) {
110     char *who, *who2;
111
112     who = bprintf("%s on %s: %s",
113                   process->name,
114                   process->smx_host->name,
115                   (process->blocked) ? "[BLOCKED] "
116                   : ((process->suspended) ? "[SUSPENDED] " : ""));
117
118     if (process->mutex) {
119       who2 =
120           bprintf("%s Blocked on mutex %p", who,
121                   (XBT_LOG_ISENABLED
122                    (simix_kernel,
123                     xbt_log_priority_verbose)) ? process->mutex : (void *)
124                   0xdead);
125       free(who);
126       who = who2;
127     } else if (process->cond) {
128       who2 =
129           bprintf
130           ("%s Blocked on condition %p; Waiting for the following actions:",
131            who,
132            (XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_verbose)) ?
133            process->cond : (void *) 0xdead);
134       free(who);
135       who = who2;
136       xbt_fifo_foreach(process->cond->actions, item, act, smx_action_t) {
137         who2 =
138             bprintf("%s '%s'(%p)", who, act->name,
139                     (XBT_LOG_ISENABLED
140                      (simix_kernel, xbt_log_priority_verbose))
141                     ? act : (void *) 0xdead);
142         free(who);
143         who = who2;
144       }
145     } else if (process->sem) {
146       who2 =
147           bprintf
148           ("%s Blocked on semaphore %p; Waiting for the following actions:",
149            who,
150            (XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_verbose)) ?
151            process->sem : (void *) 0xdead);
152       free(who);
153       who = who2;
154       xbt_fifo_foreach(process->sem->actions, item, act, smx_action_t) {
155         who2 =
156             bprintf("%s '%s'(%p)", who, act->name,
157                     (XBT_LOG_ISENABLED
158                      (simix_kernel, xbt_log_priority_verbose))
159                     ? act : (void *) 0xdead);
160         free(who);
161         who = who2;
162       }
163
164     } else {
165       who2 =
166           bprintf
167           ("%s Blocked in an unknown status (please report this bug)",
168            who);
169       free(who);
170       who = who2;
171     }
172     INFO1("%s.", who);
173     free(who);
174   }
175 }
176
177
178 /**
179  * \brief Launch the SIMIX simulation, debug purpose
180  */
181 void __SIMIX_main(void)
182 {
183   smx_process_t process = NULL;
184   smx_cond_t cond = NULL;
185   smx_action_t smx_action;
186   xbt_fifo_t actions_done = xbt_fifo_new();
187   xbt_fifo_t actions_failed = xbt_fifo_new();
188
189   /* Clean IO before the run */
190   fflush(stdout);
191   fflush(stderr);
192
193   //surf_solve(); /* Takes traces into account. Returns 0.0 */
194   /* xbt_fifo_size(msg_global->process_to_run) */
195
196   while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
197
198     while ((smx_action = xbt_fifo_pop(actions_failed))) {
199
200       xbt_fifo_item_t _cursor;
201
202       DEBUG1("** %s failed **", smx_action->name);
203       xbt_fifo_foreach(smx_action->cond_list, _cursor, cond, smx_cond_t) {
204         xbt_swag_foreach(process, cond->sleeping) {
205           DEBUG2("\t preparing to wake up %s on %s",
206                  process->name, process->smx_host->name);
207         }
208         SIMIX_cond_broadcast(cond);
209         /* remove conditional from action */
210         SIMIX_unregister_action_to_condition(smx_action, cond);
211       }
212     }
213
214     while ((smx_action = xbt_fifo_pop(actions_done))) {
215       xbt_fifo_item_t _cursor;
216
217       DEBUG1("** %s done **", smx_action->name);
218       xbt_fifo_foreach(smx_action->cond_list, _cursor, cond, smx_cond_t) {
219         xbt_swag_foreach(process, cond->sleeping) {
220           DEBUG2("\t preparing to wake up %s on %s",
221                  process->name, process->smx_host->name);
222         }
223         SIMIX_cond_broadcast(cond);
224         /* remove conditional from action */
225         SIMIX_unregister_action_to_condition(smx_action, cond);
226       }
227     }
228   }
229   return;
230 }
231
232 /**
233  * \brief Kill all running process
234  *  Only maestro can kill everyone :)
235  */
236 void SIMIX_process_killall()
237 {
238   smx_process_t p = NULL;
239   xbt_assert0((simix_global->current_process ==
240                simix_global->maestro_process),
241               "You are not supposed to run this function here!");
242
243   while ((p = xbt_swag_extract(simix_global->process_list)))
244     SIMIX_process_kill(p);
245
246   SIMIX_process_empty_trash();
247
248   return;
249 }
250
251 /**
252  * \brief Clean the SIMIX simulation
253  *
254  * This functions remove the memory used by SIMIX
255  */
256 void SIMIX_clean(void)
257 {
258   /* Kill everyone (except maestro) */
259   SIMIX_process_killall();
260
261   /* Free the remaining data structures */
262   xbt_swag_free(simix_global->process_to_run);
263   xbt_swag_free(simix_global->process_to_destroy);
264   xbt_swag_free(simix_global->process_list);
265   simix_global->process_list = NULL;
266   simix_global->process_to_destroy = NULL;
267   xbt_dict_free(&(simix_global->registered_functions));
268   xbt_dict_free(&(simix_global->host));
269
270   xbt_dict_free(&(simix_global->msg_sizes));
271 #ifdef HAVE_LATENCY_BOUND_TRACKING
272   xbt_dict_free(&(simix_global->latency_limited_dict));
273 #endif
274
275   /* Let's free maestro now */
276   SIMIX_context_free(simix_global->maestro_process->context);
277   xbt_free(simix_global->maestro_process->exception);
278   xbt_free(simix_global->maestro_process);
279   simix_global->maestro_process = NULL;
280
281   /* Restore the default exception setup */
282   __xbt_ex_ctx = &__xbt_ex_ctx_default;
283   __xbt_ex_terminate = &__xbt_ex_terminate_default;
284
285   /* Finish context module and SURF */
286   SIMIX_context_mod_exit();
287
288   surf_exit();
289
290   xbt_free(simix_global);
291   simix_global = NULL;
292
293   return;
294 }
295
296
297 /**
298  * \brief A clock (in second).
299  *
300  * \return Return the clock.
301  */
302 XBT_INLINE double SIMIX_get_clock(void)
303 {
304   return surf_get_clock();
305 }
306
307 /**
308  *      \brief Finish the simulation initialization
309  *
310  *      Must be called before the first call to SIMIX_solve()
311  */
312 XBT_INLINE void SIMIX_init(void)
313 {
314   surf_presolve();
315 }
316
317 /**
318  *      \brief Does a turn of the simulation
319  *
320  *      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.
321  *      \param actions_done List of actions done
322  *      \param actions_failed List of actions failed
323  *      \return The time spent to execute the simulation or -1 if the simulation ended
324  */
325 double SIMIX_solve(xbt_fifo_t actions_done, xbt_fifo_t actions_failed)
326 {
327
328   smx_process_t process = NULL;
329   unsigned int iter;
330   double elapsed_time = 0.0;
331   static int state_modifications = 1;
332   int actions_on_system = 0;
333
334   SIMIX_process_empty_trash();
335   if (XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_debug) &&
336       xbt_swag_size(simix_global->process_to_run) && (elapsed_time > 0)) {
337     DEBUG0("**************************************************");
338   }
339
340   while ((process = xbt_swag_extract(simix_global->process_to_run))) {
341     DEBUG2("Scheduling %s on %s", process->name, process->smx_host->name);
342     SIMIX_process_schedule(process);
343   }
344
345   {
346     surf_action_t action = NULL;
347     surf_model_t model = NULL;
348     smx_action_t smx_action = NULL;
349
350     void *fun = NULL;
351     void *arg = NULL;
352
353     xbt_dynar_foreach(model_list, iter, model) {
354       if (xbt_swag_size(model->states.failed_action_set)
355           || xbt_swag_size(model->states.done_action_set)) {
356         state_modifications = 1;
357         break;
358       }
359       if (xbt_swag_size(model->states.running_action_set)
360           || xbt_swag_size(model->states.ready_action_set)) {
361         actions_on_system = 1;
362       }
363     }
364     /* only calls surf_solve if there are actions to run */
365     if (!state_modifications && actions_on_system) {
366       DEBUG1("%f : Calling surf_solve", SIMIX_get_clock());
367       elapsed_time = surf_solve();
368       DEBUG1("Elapsed_time %f", elapsed_time);
369     }
370
371     actions_on_system = 0;
372     while (surf_timer_model->extension.timer.get(&fun, (void *) &arg)) {
373       /* change in process, don't quit */
374       actions_on_system = 1;
375       DEBUG2("got %p %p", fun, arg);
376       if (fun == SIMIX_process_create) {
377         smx_process_arg_t args = arg;
378         DEBUG2("Launching %s on %s", args->name, args->hostname);
379         process = SIMIX_process_create(args->name, args->code,
380                                        args->data, args->hostname,
381                                        args->argc, args->argv,
382                                        args->properties);
383         /* verify if process has been created */
384         if (!process) {
385           xbt_free(args);
386           continue;
387         }
388
389         if (args->kill_time > SIMIX_get_clock()) {
390           surf_timer_model->extension.timer.set(args->kill_time, (void *)
391                                                 &SIMIX_process_kill,
392                                                 (void *) process);
393         }
394         xbt_free(args);
395       } else if (fun == simix_global->create_process_function) {
396         smx_process_arg_t args = arg;
397         DEBUG2("Launching %s on %s", args->name, args->hostname);
398         process =
399             (*simix_global->create_process_function) (args->name,
400                                                       args->code,
401                                                       args->data,
402                                                       args->hostname,
403                                                       args->argc,
404                                                       args->argv,
405                                                       args->properties);
406         /* verify if process has been created */
407         if (!process) {
408           xbt_free(args);
409           continue;
410         }
411         if (args->kill_time > SIMIX_get_clock()) {
412           if (simix_global->kill_process_function)
413             surf_timer_model->extension.timer.set(args->kill_time, (void *)
414                                                   simix_global->
415                                                   kill_process_function,
416                                                   process);
417           else
418             surf_timer_model->extension.timer.set(args->kill_time, (void *)
419                                                   &SIMIX_process_kill,
420                                                   (void *) process);
421         }
422         xbt_free(args);
423       } else if (fun == SIMIX_process_kill) {
424         process = arg;
425         DEBUG2("Killing %s on %s", process->name, process->smx_host->name);
426         SIMIX_process_kill(process);
427       } else if (fun == simix_global->kill_process_function) {
428         process = arg;
429         (*simix_global->kill_process_function) (process);
430       } else {
431         THROW_IMPOSSIBLE;
432       }
433     }
434
435     /* Wake up all process waiting for the action finish */
436     xbt_dynar_foreach(model_list, iter, model) {
437       /* stop simulation case there are no actions to run */
438       if ((xbt_swag_size(model->states.running_action_set)) ||
439           (xbt_swag_size(model->states.ready_action_set)) ||
440           (xbt_swag_size(model->states.done_action_set)) ||
441           (xbt_swag_size(model->states.failed_action_set)))
442         actions_on_system = 1;
443
444       while ((action = xbt_swag_extract(model->states.failed_action_set))) {
445         smx_action = action->data;
446         if (smx_action) {
447           SIMIX_action_signal_all(smx_action);
448         }
449       }
450       while ((action = xbt_swag_extract(model->states.done_action_set))) {
451         smx_action = action->data;
452         if (smx_action) {
453           SIMIX_action_signal_all(smx_action);
454         }
455       }
456     }
457   }
458   state_modifications = 0;
459   if (!actions_on_system)
460     elapsed_time = -1;
461
462   if (elapsed_time == -1) {
463     if (xbt_swag_size(simix_global->process_list) == 0) {
464 /*                      INFO0("Congratulations ! Simulation terminated : all processes are over"); */
465     } else {
466       INFO0("Oops ! Deadlock or code not perfectly clean.");
467       SIMIX_display_process_status();
468       if (XBT_LOG_ISENABLED(simix, xbt_log_priority_debug) ||
469           XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_debug)) {
470         DEBUG0("Aborting!");
471         xbt_abort();
472       }
473       INFO0("Return a Warning.");
474     }
475   }
476   return elapsed_time;
477 }
478
479 /**
480  *      \brief Set the date to execute a function
481  *
482  * Set the date to execute the function on the surf.
483  *      \param date Date to execute function
484  *      \param function Function to be executed
485  *      \param arg Parameters of the function
486  *
487  */
488 XBT_INLINE void SIMIX_timer_set(double date, void *function, void *arg)
489 {
490   surf_timer_model->extension.timer.set(date, function, arg);
491 }
492
493 XBT_INLINE int SIMIX_timer_get(void **function, void **arg)
494 {
495   return surf_timer_model->extension.timer.get(function, arg);
496 }
497
498 /**
499  *      \brief Registers a function to create a process.
500  *
501  *      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.
502  *      \param function Create process function
503  *
504  */
505 XBT_INLINE void SIMIX_function_register_process_create(smx_creation_func_t
506                                                        function)
507 {
508   xbt_assert0((simix_global->create_process_function == NULL),
509               "Data already set");
510
511   simix_global->create_process_function = function;
512 }
513
514 /**
515  *      \brief Registers a function to kill a process.
516  *
517  *      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.
518  *      \param function Kill process function
519  *
520  */
521 XBT_INLINE void SIMIX_function_register_process_kill(void_f_pvoid_t
522                                                      function)
523 {
524   xbt_assert0((simix_global->kill_process_function == NULL),
525               "Data already set");
526
527   simix_global->kill_process_function = function;
528 }
529
530 /**
531  *      \brief Registers a function to cleanup a process.
532  *
533  *      This function registers an user function to be called when a new process ends properly.
534  *      \param function cleanup process function
535  *
536  */
537 XBT_INLINE void SIMIX_function_register_process_cleanup(void_f_pvoid_t
538                                                         function)
539 {
540   simix_global->cleanup_process_function = function;
541 }