Logo AND Algorithmique Numérique Distribuée

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