Logo AND Algorithmique Numérique Distribuée

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