Logo AND Algorithmique Numérique Distribuée

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