Logo AND Algorithmique Numérique Distribuée

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