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.",
101 /* List the process and their state */
103 ("Legend of the following listing: \"<process> on <host>: <status>.\"");
104 xbt_swag_foreach(process, simix_global->process_list) {
107 if (asprintf(&who, "%s on %s: %s",
109 process->smx_host->name,
110 (process->blocked) ? "[BLOCKED] "
111 : ((process->suspended) ? "[SUSPENDED] " : "")) == -1)
112 xbt_die("asprintf failed");
114 if (process->mutex) {
116 bprintf("%s Blocked on mutex %p", who,
119 xbt_log_priority_verbose)) ? process->mutex : (void *)
123 } else if (process->cond) {
126 ("%s Blocked on condition %p; Waiting for the following actions:",
128 (XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_verbose)) ?
129 process->cond : (void *) 0xdead);
132 xbt_fifo_foreach(process->cond->actions, item, act, smx_action_t) {
134 bprintf("%s '%s'(%p)", who, act->name,
136 (simix_kernel, xbt_log_priority_verbose))
137 ? act : (void *) 0xdead);
141 } else if (process->sem) {
144 ("%s Blocked on semaphore %p; Waiting for the following actions:",
146 (XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_verbose)) ?
147 process->sem : (void *) 0xdead);
150 xbt_fifo_foreach(process->sem->actions, item, act, smx_action_t) {
152 bprintf("%s '%s'(%p)", who, act->name,
154 (simix_kernel, xbt_log_priority_verbose))
155 ? act : (void *) 0xdead);
163 ("%s Blocked in an unknown status (please report this bug)",
175 * \brief Launch the SIMIX simulation, debug purpose
177 void __SIMIX_main(void)
179 smx_process_t process = NULL;
180 smx_cond_t cond = NULL;
181 smx_action_t smx_action;
182 xbt_fifo_t actions_done = xbt_fifo_new();
183 xbt_fifo_t actions_failed = xbt_fifo_new();
185 /* Clean IO before the run */
189 //surf_solve(); /* Takes traces into account. Returns 0.0 */
190 /* xbt_fifo_size(msg_global->process_to_run) */
192 while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
194 while ((smx_action = xbt_fifo_pop(actions_failed))) {
196 xbt_fifo_item_t _cursor;
198 DEBUG1("** %s failed **", smx_action->name);
199 xbt_fifo_foreach(smx_action->cond_list, _cursor, cond, smx_cond_t) {
200 xbt_swag_foreach(process, cond->sleeping) {
201 DEBUG2("\t preparing to wake up %s on %s",
202 process->name, process->smx_host->name);
204 SIMIX_cond_broadcast(cond);
205 /* remove conditional from action */
206 SIMIX_unregister_action_to_condition(smx_action, cond);
210 while ((smx_action = xbt_fifo_pop(actions_done))) {
211 xbt_fifo_item_t _cursor;
213 DEBUG1("** %s done **", smx_action->name);
214 xbt_fifo_foreach(smx_action->cond_list, _cursor, cond, smx_cond_t) {
215 xbt_swag_foreach(process, cond->sleeping) {
216 DEBUG2("\t preparing to wake up %s on %s",
217 process->name, process->smx_host->name);
219 SIMIX_cond_broadcast(cond);
220 /* remove conditional from action */
221 SIMIX_unregister_action_to_condition(smx_action, cond);
229 * \brief Kill all running process
230 * Only maestro can kill everyone :)
232 void SIMIX_process_killall()
234 smx_process_t p = NULL;
235 xbt_assert0((simix_global->current_process ==
236 simix_global->maestro_process),
237 "You are not supposed to run this function here!");
239 while ((p = xbt_swag_extract(simix_global->process_list)))
240 SIMIX_process_kill(p);
242 SIMIX_process_empty_trash();
248 * \brief Clean the SIMIX simulation
250 * This functions remove the memory used by SIMIX
252 void SIMIX_clean(void)
254 /* Kill everyone (except maestro) */
255 SIMIX_process_killall();
257 /* Free the remaining data structures */
258 xbt_swag_free(simix_global->process_to_run);
259 xbt_swag_free(simix_global->process_to_destroy);
260 xbt_swag_free(simix_global->process_list);
261 simix_global->process_list = NULL;
262 simix_global->process_to_destroy = NULL;
263 xbt_dict_free(&(simix_global->registered_functions));
264 xbt_dict_free(&(simix_global->host));
266 /* Let's free maestro now */
267 SIMIX_context_free(simix_global->maestro_process->context);
268 xbt_free(simix_global->maestro_process->exception);
269 xbt_free(simix_global->maestro_process);
270 simix_global->maestro_process = NULL;
272 /* Restore the default exception setup */
273 __xbt_ex_ctx = &__xbt_ex_ctx_default;
274 __xbt_ex_terminate = &__xbt_ex_terminate_default;
276 /* Finish context module and SURF */
277 SIMIX_context_mod_exit();
281 xbt_free(simix_global);
289 * \brief A clock (in second).
291 * \return Return the clock.
293 XBT_INLINE double SIMIX_get_clock(void)
295 return surf_get_clock();
299 * \brief Finish the simulation initialization
301 * Must be called before the first call to SIMIX_solve()
303 XBT_INLINE void SIMIX_init(void)
309 * \brief Does a turn of the simulation
311 * 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.
312 * \param actions_done List of actions done
313 * \param actions_failed List of actions failed
314 * \return The time spent to execute the simulation or -1 if the simulation ended
316 double SIMIX_solve(xbt_fifo_t actions_done, xbt_fifo_t actions_failed)
319 smx_process_t process = NULL;
321 double elapsed_time = 0.0;
322 static int state_modifications = 1;
323 int actions_on_system = 0;
325 SIMIX_process_empty_trash();
326 if (XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_debug) &&
327 xbt_swag_size(simix_global->process_to_run) && (elapsed_time > 0)) {
328 DEBUG0("**************************************************");
331 while ((process = xbt_swag_extract(simix_global->process_to_run))) {
332 DEBUG2("Scheduling %s on %s", process->name, process->smx_host->name);
333 SIMIX_process_schedule(process);
337 surf_action_t action = NULL;
338 surf_model_t model = NULL;
339 smx_action_t smx_action = NULL;
344 xbt_dynar_foreach(model_list, iter, model) {
345 if (xbt_swag_size(model->states.failed_action_set)
346 || xbt_swag_size(model->states.done_action_set)) {
347 state_modifications = 1;
350 if (xbt_swag_size(model->states.running_action_set)
351 || xbt_swag_size(model->states.ready_action_set)) {
352 actions_on_system = 1;
355 /* only calls surf_solve if there are actions to run */
356 if (!state_modifications && actions_on_system) {
357 DEBUG1("%f : Calling surf_solve", SIMIX_get_clock());
358 elapsed_time = surf_solve();
359 DEBUG1("Elapsed_time %f", elapsed_time);
362 actions_on_system = 0;
363 while (surf_timer_model->extension.timer.get(&fun, (void *) &arg)) {
364 /* change in process, don't quit */
365 actions_on_system = 1;
366 DEBUG2("got %p %p", fun, arg);
367 if (fun == SIMIX_process_create) {
368 smx_process_arg_t args = arg;
369 DEBUG2("Launching %s on %s", args->name, args->hostname);
370 process = SIMIX_process_create(args->name, args->code,
371 args->data, args->hostname,
372 args->argc, args->argv,
374 /* verify if process has been created */
380 if (args->kill_time > SIMIX_get_clock()) {
381 surf_timer_model->extension.timer.set(args->kill_time, (void *)
386 } else if (fun == simix_global->create_process_function) {
387 smx_process_arg_t args = arg;
388 DEBUG2("Launching %s on %s", args->name, args->hostname);
390 (*simix_global->create_process_function) (args->name,
397 /* verify if process has been created */
402 if (args->kill_time > SIMIX_get_clock()) {
403 if (simix_global->kill_process_function)
404 surf_timer_model->extension.timer.set(args->kill_time, (void *)
406 kill_process_function,
409 surf_timer_model->extension.timer.set(args->kill_time, (void *)
414 } else if (fun == SIMIX_process_kill) {
416 DEBUG2("Killing %s on %s", process->name, process->smx_host->name);
417 SIMIX_process_kill(process);
418 } else if (fun == simix_global->kill_process_function) {
420 (*simix_global->kill_process_function) (process);
426 /* Wake up all process waiting for the action finish */
427 xbt_dynar_foreach(model_list, iter, model) {
428 /* stop simulation case there are no actions to run */
429 if ((xbt_swag_size(model->states.running_action_set)) ||
430 (xbt_swag_size(model->states.ready_action_set)) ||
431 (xbt_swag_size(model->states.done_action_set)) ||
432 (xbt_swag_size(model->states.failed_action_set)))
433 actions_on_system = 1;
435 while ((action = xbt_swag_extract(model->states.failed_action_set))) {
436 smx_action = action->data;
438 SIMIX_action_signal_all(smx_action);
441 while ((action = xbt_swag_extract(model->states.done_action_set))) {
442 smx_action = action->data;
444 SIMIX_action_signal_all(smx_action);
449 state_modifications = 0;
450 if (!actions_on_system)
453 if (elapsed_time == -1) {
454 if (xbt_swag_size(simix_global->process_list) == 0) {
455 /* INFO0("Congratulations ! Simulation terminated : all processes are over"); */
457 INFO0("Oops ! Deadlock or code not perfectly clean.");
458 SIMIX_display_process_status();
459 if (XBT_LOG_ISENABLED(simix, xbt_log_priority_debug) ||
460 XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_debug)) {
464 INFO0("Return a Warning.");
471 * \brief Set the date to execute a function
473 * Set the date to execute the function on the surf.
474 * \param date Date to execute function
475 * \param function Function to be executed
476 * \param arg Parameters of the function
479 XBT_INLINE void SIMIX_timer_set(double date, void *function, void *arg)
481 surf_timer_model->extension.timer.set(date, function, arg);
484 XBT_INLINE int SIMIX_timer_get(void **function, void **arg)
486 return surf_timer_model->extension.timer.get(function, arg);
490 * \brief Registers a function to create a process.
492 * 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.
493 * \param function Create process function
496 XBT_INLINE void SIMIX_function_register_process_create(smx_creation_func_t
499 xbt_assert0((simix_global->create_process_function == NULL),
502 simix_global->create_process_function = function;
506 * \brief Registers a function to kill a process.
508 * 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.
509 * \param function Kill process function
512 XBT_INLINE void SIMIX_function_register_process_kill(void_f_pvoid_t
515 xbt_assert0((simix_global->kill_process_function == NULL),
518 simix_global->kill_process_function = function;
522 * \brief Registers a function to cleanup a process.
524 * This function registers an user function to be called when a new process ends properly.
525 * \param function cleanup process function
528 XBT_INLINE void SIMIX_function_register_process_cleanup(void_f_pvoid_t
531 simix_global->cleanup_process_function = function;