Logo AND Algorithmique Numérique Distribuée

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