Logo AND Algorithmique Numérique Distribuée

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