Logo AND Algorithmique Numérique Distribuée

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