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_global->msg_sizes = xbt_dict_new();
78 #ifdef HAVE_LATENCY_BOUND_TRACKING
79 simix_global->latency_limited_dict = xbt_dict_new();
82 SIMIX_context_mod_init();
83 SIMIX_create_maestro_process();
85 /* context exception handlers */
86 __xbt_ex_ctx = SIMIX_process_get_exception;
87 __xbt_ex_terminate = SIMIX_process_exception_terminate;
90 /* Prepare to display some more info when dying on Ctrl-C pressing */
91 signal(SIGINT, inthandler);
92 surf_init(argc, argv); /* Initialize SURF structures */
96 /* Debug purpose, incomplete */
97 void SIMIX_display_process_status(void)
99 smx_process_t process = NULL;
100 xbt_fifo_item_t item = NULL;
102 int nbprocess = xbt_swag_size(simix_global->process_list);
104 INFO1("%d processes are still running, waiting for something.",
106 /* List the process and their state */
108 ("Legend of the following listing: \"<process> on <host>: <status>.\"");
109 xbt_swag_foreach(process, simix_global->process_list) {
112 who = bprintf("%s on %s: %s",
114 process->smx_host->name,
115 (process->blocked) ? "[BLOCKED] "
116 : ((process->suspended) ? "[SUSPENDED] " : ""));
118 if (process->mutex) {
120 bprintf("%s Blocked on mutex %p", who,
123 xbt_log_priority_verbose)) ? process->mutex : (void *)
127 } else if (process->cond) {
130 ("%s Blocked on condition %p; Waiting for the following actions:",
132 (XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_verbose)) ?
133 process->cond : (void *) 0xdead);
136 xbt_fifo_foreach(process->cond->actions, item, act, smx_action_t) {
138 bprintf("%s '%s'(%p)", who, act->name,
140 (simix_kernel, xbt_log_priority_verbose))
141 ? act : (void *) 0xdead);
145 } else if (process->sem) {
148 ("%s Blocked on semaphore %p; Waiting for the following actions:",
150 (XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_verbose)) ?
151 process->sem : (void *) 0xdead);
154 xbt_fifo_foreach(process->sem->actions, item, act, smx_action_t) {
156 bprintf("%s '%s'(%p)", who, act->name,
158 (simix_kernel, xbt_log_priority_verbose))
159 ? act : (void *) 0xdead);
167 ("%s Blocked in an unknown status (please report this bug)",
179 * \brief Launch the SIMIX simulation, debug purpose
181 void __SIMIX_main(void)
183 smx_process_t process = NULL;
184 smx_cond_t cond = NULL;
185 smx_action_t smx_action;
186 xbt_fifo_t actions_done = xbt_fifo_new();
187 xbt_fifo_t actions_failed = xbt_fifo_new();
189 /* Clean IO before the run */
193 //surf_solve(); /* Takes traces into account. Returns 0.0 */
194 /* xbt_fifo_size(msg_global->process_to_run) */
196 while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
198 while ((smx_action = xbt_fifo_pop(actions_failed))) {
200 xbt_fifo_item_t _cursor;
202 DEBUG1("** %s failed **", smx_action->name);
203 xbt_fifo_foreach(smx_action->cond_list, _cursor, cond, smx_cond_t) {
204 xbt_swag_foreach(process, cond->sleeping) {
205 DEBUG2("\t preparing to wake up %s on %s",
206 process->name, process->smx_host->name);
208 SIMIX_cond_broadcast(cond);
209 /* remove conditional from action */
210 SIMIX_unregister_action_to_condition(smx_action, cond);
214 while ((smx_action = xbt_fifo_pop(actions_done))) {
215 xbt_fifo_item_t _cursor;
217 DEBUG1("** %s done **", smx_action->name);
218 xbt_fifo_foreach(smx_action->cond_list, _cursor, cond, smx_cond_t) {
219 xbt_swag_foreach(process, cond->sleeping) {
220 DEBUG2("\t preparing to wake up %s on %s",
221 process->name, process->smx_host->name);
223 SIMIX_cond_broadcast(cond);
224 /* remove conditional from action */
225 SIMIX_unregister_action_to_condition(smx_action, cond);
233 * \brief Kill all running process
234 * Only maestro can kill everyone :)
236 void SIMIX_process_killall()
238 smx_process_t p = NULL;
239 xbt_assert0((simix_global->current_process ==
240 simix_global->maestro_process),
241 "You are not supposed to run this function here!");
243 while ((p = xbt_swag_extract(simix_global->process_list)))
244 SIMIX_process_kill(p);
246 SIMIX_process_empty_trash();
252 * \brief Clean the SIMIX simulation
254 * This functions remove the memory used by SIMIX
256 void SIMIX_clean(void)
258 /* Kill everyone (except maestro) */
259 SIMIX_process_killall();
261 /* Free the remaining data structures */
262 xbt_swag_free(simix_global->process_to_run);
263 xbt_swag_free(simix_global->process_to_destroy);
264 xbt_swag_free(simix_global->process_list);
265 simix_global->process_list = NULL;
266 simix_global->process_to_destroy = NULL;
267 xbt_dict_free(&(simix_global->registered_functions));
268 xbt_dict_free(&(simix_global->host));
270 xbt_dict_free(&(simix_global->msg_sizes));
271 #ifdef HAVE_LATENCY_BOUND_TRACKING
272 xbt_dict_free(&(simix_global->latency_limited_dict));
275 /* Let's free maestro now */
276 SIMIX_context_free(simix_global->maestro_process->context);
277 xbt_free(simix_global->maestro_process->exception);
278 xbt_free(simix_global->maestro_process);
279 simix_global->maestro_process = NULL;
281 /* Restore the default exception setup */
282 __xbt_ex_ctx = &__xbt_ex_ctx_default;
283 __xbt_ex_terminate = &__xbt_ex_terminate_default;
285 /* Finish context module and SURF */
286 SIMIX_context_mod_exit();
290 xbt_free(simix_global);
298 * \brief A clock (in second).
300 * \return Return the clock.
302 XBT_INLINE double SIMIX_get_clock(void)
304 return surf_get_clock();
308 * \brief Finish the simulation initialization
310 * Must be called before the first call to SIMIX_solve()
312 XBT_INLINE void SIMIX_init(void)
318 * \brief Does a turn of the simulation
320 * 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.
321 * \param actions_done List of actions done
322 * \param actions_failed List of actions failed
323 * \return The time spent to execute the simulation or -1 if the simulation ended
325 double SIMIX_solve(xbt_fifo_t actions_done, xbt_fifo_t actions_failed)
328 smx_process_t process = NULL;
330 double elapsed_time = 0.0;
331 static int state_modifications = 1;
332 int actions_on_system = 0;
334 SIMIX_process_empty_trash();
335 if (XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_debug) &&
336 xbt_swag_size(simix_global->process_to_run) && (elapsed_time > 0)) {
337 DEBUG0("**************************************************");
340 while ((process = xbt_swag_extract(simix_global->process_to_run))) {
341 DEBUG2("Scheduling %s on %s", process->name, process->smx_host->name);
342 SIMIX_process_schedule(process);
346 surf_action_t action = NULL;
347 surf_model_t model = NULL;
348 smx_action_t smx_action = NULL;
353 xbt_dynar_foreach(model_list, iter, model) {
354 if (xbt_swag_size(model->states.failed_action_set)
355 || xbt_swag_size(model->states.done_action_set)) {
356 state_modifications = 1;
359 if (xbt_swag_size(model->states.running_action_set)
360 || xbt_swag_size(model->states.ready_action_set)) {
361 actions_on_system = 1;
364 /* only calls surf_solve if there are actions to run */
365 if (!state_modifications && actions_on_system) {
366 DEBUG1("%f : Calling surf_solve", SIMIX_get_clock());
367 elapsed_time = surf_solve();
368 DEBUG1("Elapsed_time %f", elapsed_time);
371 actions_on_system = 0;
372 while (surf_timer_model->extension.timer.get(&fun, (void *) &arg)) {
373 /* change in process, don't quit */
374 actions_on_system = 1;
375 DEBUG2("got %p %p", fun, arg);
376 if (fun == SIMIX_process_create) {
377 smx_process_arg_t args = arg;
378 DEBUG2("Launching %s on %s", args->name, args->hostname);
379 process = SIMIX_process_create(args->name, args->code,
380 args->data, args->hostname,
381 args->argc, args->argv,
383 /* verify if process has been created */
389 if (args->kill_time > SIMIX_get_clock()) {
390 surf_timer_model->extension.timer.set(args->kill_time, (void *)
395 } else if (fun == simix_global->create_process_function) {
396 smx_process_arg_t args = arg;
397 DEBUG2("Launching %s on %s", args->name, args->hostname);
399 (*simix_global->create_process_function) (args->name,
406 /* verify if process has been created */
411 if (args->kill_time > SIMIX_get_clock()) {
412 if (simix_global->kill_process_function)
413 surf_timer_model->extension.timer.set(args->kill_time, (void *)
415 kill_process_function,
418 surf_timer_model->extension.timer.set(args->kill_time, (void *)
423 } else if (fun == SIMIX_process_kill) {
425 DEBUG2("Killing %s on %s", process->name, process->smx_host->name);
426 SIMIX_process_kill(process);
427 } else if (fun == simix_global->kill_process_function) {
429 (*simix_global->kill_process_function) (process);
435 /* Wake up all process waiting for the action finish */
436 xbt_dynar_foreach(model_list, iter, model) {
437 /* stop simulation case there are no actions to run */
438 if ((xbt_swag_size(model->states.running_action_set)) ||
439 (xbt_swag_size(model->states.ready_action_set)) ||
440 (xbt_swag_size(model->states.done_action_set)) ||
441 (xbt_swag_size(model->states.failed_action_set)))
442 actions_on_system = 1;
444 while ((action = xbt_swag_extract(model->states.failed_action_set))) {
445 smx_action = action->data;
447 SIMIX_action_signal_all(smx_action);
450 while ((action = xbt_swag_extract(model->states.done_action_set))) {
451 smx_action = action->data;
453 SIMIX_action_signal_all(smx_action);
458 state_modifications = 0;
459 if (!actions_on_system)
462 if (elapsed_time == -1) {
463 if (xbt_swag_size(simix_global->process_list) == 0) {
464 /* INFO0("Congratulations ! Simulation terminated : all processes are over"); */
466 INFO0("Oops ! Deadlock or code not perfectly clean.");
467 SIMIX_display_process_status();
468 if (XBT_LOG_ISENABLED(simix, xbt_log_priority_debug) ||
469 XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_debug)) {
473 INFO0("Return a Warning.");
480 * \brief Set the date to execute a function
482 * Set the date to execute the function on the surf.
483 * \param date Date to execute function
484 * \param function Function to be executed
485 * \param arg Parameters of the function
488 XBT_INLINE void SIMIX_timer_set(double date, void *function, void *arg)
490 surf_timer_model->extension.timer.set(date, function, arg);
493 XBT_INLINE int SIMIX_timer_get(void **function, void **arg)
495 return surf_timer_model->extension.timer.get(function, arg);
499 * \brief Registers a function to create a process.
501 * 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.
502 * \param function Create process function
505 XBT_INLINE void SIMIX_function_register_process_create(smx_creation_func_t
508 xbt_assert0((simix_global->create_process_function == NULL),
511 simix_global->create_process_function = function;
515 * \brief Registers a function to kill a process.
517 * 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.
518 * \param function Kill process function
521 XBT_INLINE void SIMIX_function_register_process_kill(void_f_pvoid_t
524 xbt_assert0((simix_global->kill_process_function == NULL),
527 simix_global->kill_process_function = function;
531 * \brief Registers a function to cleanup a process.
533 * This function registers an user function to be called when a new process ends properly.
534 * \param function cleanup process function
537 XBT_INLINE void SIMIX_function_register_process_cleanup(void_f_pvoid_t
540 simix_global->cleanup_process_function = function;