Logo AND Algorithmique Numérique Distribuée

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