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 who = bprintf("%s on %s: %s",
109 process->smx_host->name,
110 (process->blocked) ? "[BLOCKED] "
111 : ((process->suspended) ? "[SUSPENDED] " : ""));
113 if (process->mutex) {
115 bprintf("%s Blocked on mutex %p", who,
118 xbt_log_priority_verbose)) ? process->mutex : (void *)
122 } else if (process->cond) {
125 ("%s Blocked on condition %p; Waiting for the following actions:",
127 (XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_verbose)) ?
128 process->cond : (void *) 0xdead);
131 xbt_fifo_foreach(process->cond->actions, item, act, smx_action_t) {
133 bprintf("%s '%s'(%p)", who, act->name,
135 (simix_kernel, xbt_log_priority_verbose))
136 ? act : (void *) 0xdead);
140 } else if (process->sem) {
143 ("%s Blocked on semaphore %p; Waiting for the following actions:",
145 (XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_verbose)) ?
146 process->sem : (void *) 0xdead);
149 xbt_fifo_foreach(process->sem->actions, item, act, smx_action_t) {
151 bprintf("%s '%s'(%p)", who, act->name,
153 (simix_kernel, xbt_log_priority_verbose))
154 ? act : (void *) 0xdead);
162 ("%s Blocked in an unknown status (please report this bug)",
174 * \brief Launch the SIMIX simulation, debug purpose
176 void __SIMIX_main(void)
178 smx_process_t process = NULL;
179 smx_cond_t cond = NULL;
180 smx_action_t smx_action;
181 xbt_fifo_t actions_done = xbt_fifo_new();
182 xbt_fifo_t actions_failed = xbt_fifo_new();
184 /* Clean IO before the run */
188 //surf_solve(); /* Takes traces into account. Returns 0.0 */
189 /* xbt_fifo_size(msg_global->process_to_run) */
191 while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
193 while ((smx_action = xbt_fifo_pop(actions_failed))) {
195 xbt_fifo_item_t _cursor;
197 DEBUG1("** %s failed **", smx_action->name);
198 xbt_fifo_foreach(smx_action->cond_list, _cursor, cond, smx_cond_t) {
199 xbt_swag_foreach(process, cond->sleeping) {
200 DEBUG2("\t preparing to wake up %s on %s",
201 process->name, process->smx_host->name);
203 SIMIX_cond_broadcast(cond);
204 /* remove conditional from action */
205 SIMIX_unregister_action_to_condition(smx_action, cond);
209 while ((smx_action = xbt_fifo_pop(actions_done))) {
210 xbt_fifo_item_t _cursor;
212 DEBUG1("** %s done **", smx_action->name);
213 xbt_fifo_foreach(smx_action->cond_list, _cursor, cond, smx_cond_t) {
214 xbt_swag_foreach(process, cond->sleeping) {
215 DEBUG2("\t preparing to wake up %s on %s",
216 process->name, process->smx_host->name);
218 SIMIX_cond_broadcast(cond);
219 /* remove conditional from action */
220 SIMIX_unregister_action_to_condition(smx_action, cond);
228 * \brief Kill all running process
229 * Only maestro can kill everyone :)
231 void SIMIX_process_killall()
233 smx_process_t p = NULL;
234 xbt_assert0((simix_global->current_process ==
235 simix_global->maestro_process),
236 "You are not supposed to run this function here!");
238 while ((p = xbt_swag_extract(simix_global->process_list)))
239 SIMIX_process_kill(p);
241 SIMIX_process_empty_trash();
247 * \brief Clean the SIMIX simulation
249 * This functions remove the memory used by SIMIX
251 void SIMIX_clean(void)
253 /* Kill everyone (except maestro) */
254 SIMIX_process_killall();
256 /* Free the remaining data structures */
257 xbt_swag_free(simix_global->process_to_run);
258 xbt_swag_free(simix_global->process_to_destroy);
259 xbt_swag_free(simix_global->process_list);
260 simix_global->process_list = NULL;
261 simix_global->process_to_destroy = NULL;
262 xbt_dict_free(&(simix_global->registered_functions));
263 xbt_dict_free(&(simix_global->host));
265 /* Let's free maestro now */
266 SIMIX_context_free(simix_global->maestro_process->context);
267 xbt_free(simix_global->maestro_process->exception);
268 xbt_free(simix_global->maestro_process);
269 simix_global->maestro_process = NULL;
271 /* Restore the default exception setup */
272 __xbt_ex_ctx = &__xbt_ex_ctx_default;
273 __xbt_ex_terminate = &__xbt_ex_terminate_default;
275 /* Finish context module and SURF */
276 SIMIX_context_mod_exit();
280 xbt_free(simix_global);
288 * \brief A clock (in second).
290 * \return Return the clock.
292 XBT_INLINE double SIMIX_get_clock(void)
294 return surf_get_clock();
298 * \brief Finish the simulation initialization
300 * Must be called before the first call to SIMIX_solve()
302 XBT_INLINE void SIMIX_init(void)
308 * \brief Does a turn of the simulation
310 * 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.
311 * \param actions_done List of actions done
312 * \param actions_failed List of actions failed
313 * \return The time spent to execute the simulation or -1 if the simulation ended
315 double SIMIX_solve(xbt_fifo_t actions_done, xbt_fifo_t actions_failed)
318 smx_process_t process = NULL;
320 double elapsed_time = 0.0;
321 static int state_modifications = 1;
322 int actions_on_system = 0;
324 SIMIX_process_empty_trash();
325 if (XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_debug) &&
326 xbt_swag_size(simix_global->process_to_run) && (elapsed_time > 0)) {
327 DEBUG0("**************************************************");
330 while ((process = xbt_swag_extract(simix_global->process_to_run))) {
331 DEBUG2("Scheduling %s on %s", process->name, process->smx_host->name);
332 SIMIX_process_schedule(process);
336 surf_action_t action = NULL;
337 surf_model_t model = NULL;
338 smx_action_t smx_action = NULL;
343 xbt_dynar_foreach(model_list, iter, model) {
344 if (xbt_swag_size(model->states.failed_action_set)
345 || xbt_swag_size(model->states.done_action_set)) {
346 state_modifications = 1;
349 if (xbt_swag_size(model->states.running_action_set)
350 || xbt_swag_size(model->states.ready_action_set)) {
351 actions_on_system = 1;
354 /* only calls surf_solve if there are actions to run */
355 if (!state_modifications && actions_on_system) {
356 DEBUG1("%f : Calling surf_solve", SIMIX_get_clock());
357 elapsed_time = surf_solve();
358 DEBUG1("Elapsed_time %f", elapsed_time);
361 actions_on_system = 0;
362 while (surf_timer_model->extension.timer.get(&fun, (void *) &arg)) {
363 /* change in process, don't quit */
364 actions_on_system = 1;
365 DEBUG2("got %p %p", fun, arg);
366 if (fun == SIMIX_process_create) {
367 smx_process_arg_t args = arg;
368 DEBUG2("Launching %s on %s", args->name, args->hostname);
369 process = SIMIX_process_create(args->name, args->code,
370 args->data, args->hostname,
371 args->argc, args->argv,
373 /* verify if process has been created */
379 if (args->kill_time > SIMIX_get_clock()) {
380 surf_timer_model->extension.timer.set(args->kill_time, (void *)
385 } else if (fun == simix_global->create_process_function) {
386 smx_process_arg_t args = arg;
387 DEBUG2("Launching %s on %s", args->name, args->hostname);
389 (*simix_global->create_process_function) (args->name,
396 /* verify if process has been created */
401 if (args->kill_time > SIMIX_get_clock()) {
402 if (simix_global->kill_process_function)
403 surf_timer_model->extension.timer.set(args->kill_time, (void *)
405 kill_process_function,
408 surf_timer_model->extension.timer.set(args->kill_time, (void *)
413 } else if (fun == SIMIX_process_kill) {
415 DEBUG2("Killing %s on %s", process->name, process->smx_host->name);
416 SIMIX_process_kill(process);
417 } else if (fun == simix_global->kill_process_function) {
419 (*simix_global->kill_process_function) (process);
425 /* Wake up all process waiting for the action finish */
426 xbt_dynar_foreach(model_list, iter, model) {
427 /* stop simulation case there are no actions to run */
428 if ((xbt_swag_size(model->states.running_action_set)) ||
429 (xbt_swag_size(model->states.ready_action_set)) ||
430 (xbt_swag_size(model->states.done_action_set)) ||
431 (xbt_swag_size(model->states.failed_action_set)))
432 actions_on_system = 1;
434 while ((action = xbt_swag_extract(model->states.failed_action_set))) {
435 smx_action = action->data;
437 SIMIX_action_signal_all(smx_action);
440 while ((action = xbt_swag_extract(model->states.done_action_set))) {
441 smx_action = action->data;
443 SIMIX_action_signal_all(smx_action);
448 state_modifications = 0;
449 if (!actions_on_system)
452 if (elapsed_time == -1) {
453 if (xbt_swag_size(simix_global->process_list) == 0) {
454 /* INFO0("Congratulations ! Simulation terminated : all processes are over"); */
456 INFO0("Oops ! Deadlock or code not perfectly clean.");
457 SIMIX_display_process_status();
458 if (XBT_LOG_ISENABLED(simix, xbt_log_priority_debug) ||
459 XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_debug)) {
463 INFO0("Return a Warning.");
470 * \brief Set the date to execute a function
472 * Set the date to execute the function on the surf.
473 * \param date Date to execute function
474 * \param function Function to be executed
475 * \param arg Parameters of the function
478 XBT_INLINE void SIMIX_timer_set(double date, void *function, void *arg)
480 surf_timer_model->extension.timer.set(date, function, arg);
483 XBT_INLINE int SIMIX_timer_get(void **function, void **arg)
485 return surf_timer_model->extension.timer.get(function, arg);
489 * \brief Registers a function to create a process.
491 * 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.
492 * \param function Create process function
495 XBT_INLINE void SIMIX_function_register_process_create(smx_creation_func_t
498 xbt_assert0((simix_global->create_process_function == NULL),
501 simix_global->create_process_function = function;
505 * \brief Registers a function to kill a process.
507 * 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.
508 * \param function Kill process function
511 XBT_INLINE void SIMIX_function_register_process_kill(void_f_pvoid_t
514 xbt_assert0((simix_global->kill_process_function == NULL),
517 simix_global->kill_process_function = function;
521 * \brief Registers a function to cleanup a process.
523 * This function registers an user function to be called when a new process ends properly.
524 * \param function cleanup process function
527 XBT_INLINE void SIMIX_function_register_process_cleanup(void_f_pvoid_t
530 simix_global->cleanup_process_function = function;