1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "xbt/sysdep.h"
11 #include "xbt/ex.h" /* ex_backtrace_display */
12 XBT_LOG_EXTERNAL_CATEGORY(simix);
13 XBT_LOG_EXTERNAL_CATEGORY(simix_action);
14 XBT_LOG_EXTERNAL_CATEGORY(simix_deployment);
15 XBT_LOG_EXTERNAL_CATEGORY(simix_environment);
16 XBT_LOG_EXTERNAL_CATEGORY(simix_host);
17 XBT_LOG_EXTERNAL_CATEGORY(simix_process);
18 XBT_LOG_EXTERNAL_CATEGORY(simix_synchro);
19 XBT_LOG_EXTERNAL_CATEGORY(simix_context);
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_kernel, simix,
21 "Logging specific to SIMIX (kernel)");
23 SIMIX_Global_t simix_global = NULL;
26 /* FIXME: Yeah, I'll do it in a portable maner one day [Mt] */
29 static void _XBT_CALL inthandler(int ignored)
31 INFO0("CTRL-C pressed. Displaying status and bailing out");
32 SIMIX_display_process_status();
36 /********************************* SIMIX **************************************/
39 * \brief Initialize SIMIX internal data.
44 void SIMIX_global_init(int *argc, char **argv)
49 /* Connect our log channels: that must be done manually under windows */
50 XBT_LOG_CONNECT(simix_action, simix);
51 XBT_LOG_CONNECT(simix_deployment, simix);
52 XBT_LOG_CONNECT(simix_environment, simix);
53 XBT_LOG_CONNECT(simix_host, simix);
54 XBT_LOG_CONNECT(simix_kernel, simix);
55 XBT_LOG_CONNECT(simix_process, simix);
56 XBT_LOG_CONNECT(simix_synchro, simix);
57 XBT_LOG_CONNECT(simix_context, simix);
59 simix_global = xbt_new0(s_SIMIX_Global_t, 1);
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));
69 simix_global->current_process = NULL;
70 simix_global->maestro_process = NULL;
71 simix_global->registered_functions = xbt_dict_new();
73 simix_global->create_process_function = NULL;
74 simix_global->kill_process_function = NULL;
75 simix_global->cleanup_process_function = SIMIX_process_cleanup;
77 SIMIX_context_mod_init();
78 SIMIX_create_maestro_process();
80 /* context exception handlers */
81 __xbt_ex_ctx = SIMIX_process_get_exception;
82 __xbt_ex_terminate = SIMIX_process_exception_terminate;
85 /* Prepare to display some more info when dying on Ctrl-C pressing */
86 signal(SIGINT, inthandler);
87 surf_init(argc, argv); /* Initialize SURF structures */
91 /* Debug purpose, incomplete */
92 void SIMIX_display_process_status(void)
94 smx_process_t process = NULL;
95 xbt_fifo_item_t item = NULL;
97 int nbprocess = xbt_swag_size(simix_global->process_list);
99 INFO1("%d processes are still running, waiting for something.", nbprocess);
100 /* List the process and their state */
102 ("Legend of the following listing: \"<process> on <host>: <status>.\"");
103 xbt_swag_foreach(process, simix_global->process_list) {
106 asprintf(&who, "%s on %s: %s",
108 process->smx_host->name,
109 (process->blocked) ? "[BLOCKED] "
110 : ((process->suspended) ? "[SUSPENDED] " : ""));
112 if (process->mutex) {
114 bprintf("%s Blocked on mutex %p", who,
115 (XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_verbose)) ?
116 process->mutex : (void *) 0xdead);
119 } else if (process->cond) {
122 ("%s Blocked on condition %p; Waiting for the following actions:",
124 (XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_verbose)) ?
125 process->cond : (void *) 0xdead);
128 xbt_fifo_foreach(process->cond->actions, item, act, smx_action_t) {
130 bprintf("%s '%s'(%p)", who, act->name,
131 (XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_verbose))
132 ? act : (void *) 0xdead);
136 } else if (process->sem) {
139 ("%s Blocked on semaphore %p; Waiting for the following actions:",
141 (XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_verbose)) ?
142 process->sem : (void *) 0xdead);
145 xbt_fifo_foreach(process->sem->actions, item, act, smx_action_t) {
147 bprintf("%s '%s'(%p)", who, act->name,
148 (XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_verbose))
149 ? act : (void *) 0xdead);
157 ("%s Blocked in an unknown status (please report this bug)", who);
168 * \brief Launch the SIMIX simulation, debug purpose
170 void __SIMIX_main(void)
172 smx_process_t process = NULL;
173 smx_cond_t cond = NULL;
174 smx_action_t smx_action;
175 xbt_fifo_t actions_done = xbt_fifo_new();
176 xbt_fifo_t actions_failed = xbt_fifo_new();
178 /* Clean IO before the run */
182 //surf_solve(); /* Takes traces into account. Returns 0.0 */
183 /* xbt_fifo_size(msg_global->process_to_run) */
185 while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
187 while ((smx_action = xbt_fifo_pop(actions_failed))) {
189 xbt_fifo_item_t _cursor;
191 DEBUG1("** %s failed **", smx_action->name);
192 xbt_fifo_foreach(smx_action->cond_list, _cursor, cond, smx_cond_t) {
193 xbt_swag_foreach(process, cond->sleeping) {
194 DEBUG2("\t preparing to wake up %s on %s",
195 process->name, process->smx_host->name);
197 SIMIX_cond_broadcast(cond);
198 /* remove conditional from action */
199 SIMIX_unregister_action_to_condition(smx_action, cond);
203 while ((smx_action = xbt_fifo_pop(actions_done))) {
204 xbt_fifo_item_t _cursor;
206 DEBUG1("** %s done **", smx_action->name);
207 xbt_fifo_foreach(smx_action->cond_list, _cursor, cond, smx_cond_t) {
208 xbt_swag_foreach(process, cond->sleeping) {
209 DEBUG2("\t preparing to wake up %s on %s",
210 process->name, process->smx_host->name);
212 SIMIX_cond_broadcast(cond);
213 /* remove conditional from action */
214 SIMIX_unregister_action_to_condition(smx_action, cond);
222 * \brief Kill all running process
223 * Only maestro can kill everyone :)
225 void SIMIX_process_killall()
227 smx_process_t p = NULL;
228 xbt_assert0((simix_global->current_process ==
229 simix_global->maestro_process),
230 "You are not supposed to run this function here!");
232 while ((p = xbt_swag_extract(simix_global->process_list)))
233 SIMIX_process_kill(p);
235 SIMIX_process_empty_trash();
241 * \brief Clean the SIMIX simulation
243 * This functions remove the memory used by SIMIX
245 void SIMIX_clean(void)
247 /* Kill everyone (except maestro) */
248 SIMIX_process_killall();
250 /* Free the remaining data structures */
251 xbt_swag_free(simix_global->process_to_run);
252 xbt_swag_free(simix_global->process_to_destroy);
253 xbt_swag_free(simix_global->process_list);
254 simix_global->process_list = NULL;
255 simix_global->process_to_destroy = NULL;
256 xbt_dict_free(&(simix_global->registered_functions));
257 xbt_dict_free(&(simix_global->host));
259 /* Let's free maestro now */
260 SIMIX_context_free(simix_global->maestro_process->context);
261 xbt_free(simix_global->maestro_process->exception);
262 xbt_free(simix_global->maestro_process);
263 simix_global->maestro_process = NULL;
265 /* Restore the default exception setup */
266 __xbt_ex_ctx = &__xbt_ex_ctx_default;
267 __xbt_ex_terminate = &__xbt_ex_terminate_default;
269 /* Finish context module and SURF */
270 SIMIX_context_mod_exit();
274 xbt_free(simix_global);
282 * \brief A clock (in second).
284 * \return Return the clock.
286 XBT_INLINE double SIMIX_get_clock(void)
288 return surf_get_clock();
292 * \brief Finish the simulation initialization
294 * Must be called before the first call to SIMIX_solve()
296 XBT_INLINE void SIMIX_init(void)
302 * \brief Does a turn of the simulation
304 * 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.
305 * \param actions_done List of actions done
306 * \param actions_failed List of actions failed
307 * \return The time spent to execute the simulation or -1 if the simulation ended
309 double SIMIX_solve(xbt_fifo_t actions_done, xbt_fifo_t actions_failed)
312 smx_process_t process = NULL;
314 double elapsed_time = 0.0;
315 static int state_modifications = 1;
316 int actions_on_system = 0;
318 SIMIX_process_empty_trash();
319 if (XBT_LOG_ISENABLED(simix_kernel,xbt_log_priority_debug) &&
320 xbt_swag_size(simix_global->process_to_run) && (elapsed_time > 0)) {
321 DEBUG0("**************************************************");
324 while ((process = xbt_swag_extract(simix_global->process_to_run))) {
325 DEBUG2("Scheduling %s on %s", process->name, process->smx_host->name);
326 SIMIX_process_schedule(process);
330 surf_action_t action = NULL;
331 surf_model_t model = NULL;
332 smx_action_t smx_action = NULL;
337 xbt_dynar_foreach(model_list, iter, model) {
338 if (xbt_swag_size(model->states.failed_action_set)
339 || xbt_swag_size(model->states.done_action_set)) {
340 state_modifications = 1;
343 if (xbt_swag_size(model->states.running_action_set)
344 || xbt_swag_size(model->states.ready_action_set)) {
345 actions_on_system = 1;
348 /* only calls surf_solve if there are actions to run */
349 if (!state_modifications && actions_on_system) {
350 DEBUG1("%f : Calling surf_solve", SIMIX_get_clock());
351 elapsed_time = surf_solve();
352 DEBUG1("Elapsed_time %f", elapsed_time);
355 actions_on_system = 0;
356 while (surf_timer_model->extension.timer.get(&fun, (void *) &arg)) {
357 /* change in process, don't quit */
358 actions_on_system = 1;
359 DEBUG2("got %p %p", fun, arg);
360 if (fun == SIMIX_process_create) {
361 smx_process_arg_t args = arg;
362 DEBUG2("Launching %s on %s", args->name, args->hostname);
363 process = SIMIX_process_create(args->name, args->code,
364 args->data, args->hostname,
365 args->argc, args->argv,
367 /* verify if process has been created */
373 if (args->kill_time > SIMIX_get_clock()) {
374 surf_timer_model->extension.timer.set(args->kill_time, (void *)
379 } else if (fun == simix_global->create_process_function) {
380 smx_process_arg_t args = arg;
381 DEBUG2("Launching %s on %s", args->name, args->hostname);
383 (*simix_global->create_process_function) (args->name, args->code,
386 args->argc, args->argv,
388 /* verify if process has been created */
393 if (args->kill_time > SIMIX_get_clock()) {
394 if (simix_global->kill_process_function)
395 surf_timer_model->extension.timer.set(args->kill_time, (void *)
397 kill_process_function,
400 surf_timer_model->extension.timer.set(args->kill_time, (void *)
405 } else if (fun == SIMIX_process_kill) {
407 DEBUG2("Killing %s on %s", process->name, process->smx_host->name);
408 SIMIX_process_kill(process);
409 } else if (fun == simix_global->kill_process_function) {
411 (*simix_global->kill_process_function) (process);
417 /* Wake up all process waiting for the action finish */
418 xbt_dynar_foreach(model_list, iter, model) {
419 /* stop simulation case there are no actions to run */
420 if ((xbt_swag_size(model->states.running_action_set)) ||
421 (xbt_swag_size(model->states.ready_action_set)) ||
422 (xbt_swag_size(model->states.done_action_set)) ||
423 (xbt_swag_size(model->states.failed_action_set)))
424 actions_on_system = 1;
426 while ((action = xbt_swag_extract(model->states.failed_action_set))) {
427 smx_action = action->data;
429 SIMIX_action_signal_all(smx_action);
432 while ((action = xbt_swag_extract(model->states.done_action_set))) {
433 smx_action = action->data;
435 SIMIX_action_signal_all(smx_action);
440 state_modifications = 0;
441 if (!actions_on_system)
444 if (elapsed_time == -1) {
445 if (xbt_swag_size(simix_global->process_list) == 0) {
446 /* INFO0("Congratulations ! Simulation terminated : all processes are over"); */
448 INFO0("Oops ! Deadlock or code not perfectly clean.");
449 SIMIX_display_process_status();
450 if (XBT_LOG_ISENABLED(simix, xbt_log_priority_debug) ||
451 XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_debug)) {
455 INFO0("Return a Warning.");
462 * \brief Set the date to execute a function
464 * Set the date to execute the function on the surf.
465 * \param date Date to execute function
466 * \param function Function to be executed
467 * \param arg Parameters of the function
470 XBT_INLINE void SIMIX_timer_set(double date, void *function, void *arg)
472 surf_timer_model->extension.timer.set(date, function, arg);
475 XBT_INLINE int SIMIX_timer_get(void **function, void **arg)
477 return surf_timer_model->extension.timer.get(function, arg);
481 * \brief Registers a function to create a process.
483 * 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.
484 * \param function Create process function
487 XBT_INLINE void SIMIX_function_register_process_create(smx_creation_func_t function)
489 xbt_assert0((simix_global->create_process_function == NULL),
492 simix_global->create_process_function = function;
496 * \brief Registers a function to kill a process.
498 * 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.
499 * \param function Kill process function
502 XBT_INLINE void SIMIX_function_register_process_kill(void_f_pvoid_t function)
504 xbt_assert0((simix_global->kill_process_function == NULL),
507 simix_global->kill_process_function = function;
511 * \brief Registers a function to cleanup a process.
513 * This function registers an user function to be called when a new process ends properly.
514 * \param function cleanup process function
517 XBT_INLINE void SIMIX_function_register_process_cleanup(void_f_pvoid_t function)
519 simix_global->cleanup_process_function = function;