3 /* Copyright (c) 2007 Arnaud Legrand, Bruno Donassolo.
4 All rights reserved. */
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. */
10 #include "xbt/sysdep.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_EXTERNAL_CATEGORY(simix_context);
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_kernel, simix,
23 "Logging specific to SIMIX (kernel)");
25 SIMIX_Global_t simix_global = NULL;
28 /* FIXME: Yeah, I'll do it in a portable maner one day [Mt] */
31 static void _XBT_CALL inthandler(int ignored)
33 INFO0("CTRL-C pressed. Displaying status and bailing out");
34 SIMIX_display_process_status();
38 /********************************* SIMIX **************************************/
41 * \brief Initialize SIMIX internal data.
46 void SIMIX_global_init(int *argc, char **argv)
51 /* Connect our log channels: that must be done manually under windows */
52 XBT_LOG_CONNECT(simix_action, simix);
53 XBT_LOG_CONNECT(simix_deployment, simix);
54 XBT_LOG_CONNECT(simix_environment, simix);
55 XBT_LOG_CONNECT(simix_host, simix);
56 XBT_LOG_CONNECT(simix_kernel, simix);
57 XBT_LOG_CONNECT(simix_process, simix);
58 XBT_LOG_CONNECT(simix_synchro, simix);
59 XBT_LOG_CONNECT(simix_context, simix);
61 simix_global = xbt_new0(s_SIMIX_Global_t, 1);
63 simix_global->host = xbt_dict_new();
64 simix_global->process_to_run =
65 xbt_swag_new(xbt_swag_offset(proc, synchro_hookup));
66 simix_global->process_list =
67 xbt_swag_new(xbt_swag_offset(proc, process_hookup));
68 simix_global->process_to_destroy =
69 xbt_swag_new(xbt_swag_offset(proc, destroy_hookup));
71 simix_global->current_process = NULL;
72 simix_global->maestro_process = NULL;
73 simix_global->registered_functions = xbt_dict_new();
75 simix_global->create_process_function = NULL;
76 simix_global->kill_process_function = NULL;
77 simix_global->cleanup_process_function = SIMIX_process_cleanup;
79 SIMIX_context_mod_init();
80 SIMIX_create_maestro_process();
82 /* context exception handlers */
83 __xbt_ex_ctx = SIMIX_process_get_exception;
84 __xbt_ex_terminate = SIMIX_process_exception_terminate;
87 /* Prepare to display some more info when dying on Ctrl-C pressing */
88 signal(SIGINT, inthandler);
89 surf_init(argc, argv); /* Initialize SURF structures */
93 /* Debug purpose, incomplete */
94 void SIMIX_display_process_status(void)
96 smx_process_t process = NULL;
97 xbt_fifo_item_t item = NULL;
99 int nbprocess = xbt_swag_size(simix_global->process_list);
101 INFO1("%d processes are still running, waiting for something.", nbprocess);
102 /* List the process and their state */
104 ("Legend of the following listing: \"<process> on <host>: <status>.\"");
105 xbt_swag_foreach(process, simix_global->process_list) {
108 asprintf(&who, "%s on %s: %s",
110 process->smx_host->name,
111 (process->blocked) ? "[BLOCKED] "
112 : ((process->suspended) ? "[SUSPENDED] " : ""));
114 if (process->mutex) {
116 bprintf("%s Blocked on mutex %p", who,
117 (XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_verbose)) ?
118 process->mutex : (void *) 0xdead);
121 } else if (process->cond) {
124 ("%s Blocked on condition %p; Waiting for the following actions:",
126 (XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_verbose)) ?
127 process->cond : (void *) 0xdead);
130 xbt_fifo_foreach(process->cond->actions, item, act, smx_action_t) {
132 bprintf("%s '%s'(%p)", who, act->name,
133 (XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_verbose))
134 ? act : (void *) 0xdead);
141 ("%s Blocked in an unknown status (please report this bug)", who);
152 * \brief Launch the SIMIX simulation, debug purpose
154 void __SIMIX_main(void)
156 smx_process_t process = NULL;
157 smx_cond_t cond = NULL;
158 smx_action_t smx_action;
159 xbt_fifo_t actions_done = xbt_fifo_new();
160 xbt_fifo_t actions_failed = xbt_fifo_new();
162 /* Clean IO before the run */
166 //surf_solve(); /* Takes traces into account. Returns 0.0 */
167 /* xbt_fifo_size(msg_global->process_to_run) */
169 while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
171 while ((smx_action = xbt_fifo_pop(actions_failed))) {
173 xbt_fifo_item_t _cursor;
175 DEBUG1("** %s failed **", smx_action->name);
176 xbt_fifo_foreach(smx_action->cond_list, _cursor, cond, smx_cond_t) {
177 xbt_swag_foreach(process, cond->sleeping) {
178 DEBUG2("\t preparing to wake up %s on %s",
179 process->name, process->smx_host->name);
181 SIMIX_cond_broadcast(cond);
182 /* remove conditional from action */
183 SIMIX_unregister_action_to_condition(smx_action, cond);
187 while ((smx_action = xbt_fifo_pop(actions_done))) {
188 xbt_fifo_item_t _cursor;
190 DEBUG1("** %s done **", smx_action->name);
191 xbt_fifo_foreach(smx_action->cond_list, _cursor, cond, smx_cond_t) {
192 xbt_swag_foreach(process, cond->sleeping) {
193 DEBUG2("\t preparing to wake up %s on %s",
194 process->name, process->smx_host->name);
196 SIMIX_cond_broadcast(cond);
197 /* remove conditional from action */
198 SIMIX_unregister_action_to_condition(smx_action, cond);
206 * \brief Kill all running process
207 * Only maestro can kill everyone :)
209 void SIMIX_process_killall()
211 smx_process_t p = NULL;
212 xbt_assert0((simix_global->current_process ==
213 simix_global->maestro_process),
214 "You are not supposed to run this function here!");
216 while ((p = xbt_swag_extract(simix_global->process_list)))
217 SIMIX_process_kill(p);
219 SIMIX_process_empty_trash();
225 * \brief Clean the SIMIX simulation
227 * This functions remove the memory used by SIMIX
229 void SIMIX_clean(void)
231 /* Kill everyone (except maestro) */
232 SIMIX_process_killall();
234 /* Free the remaining data structures */
235 xbt_swag_free(simix_global->process_to_run);
236 xbt_swag_free(simix_global->process_to_destroy);
237 xbt_swag_free(simix_global->process_list);
238 simix_global->process_list = NULL;
239 simix_global->process_to_destroy = NULL;
240 xbt_dict_free(&(simix_global->registered_functions));
241 xbt_dict_free(&(simix_global->host));
243 /* Let's free maestro now */
244 SIMIX_context_free(simix_global->maestro_process->context);
245 xbt_free(simix_global->maestro_process->exception);
246 xbt_free(simix_global->maestro_process);
247 simix_global->maestro_process = NULL;
249 /* Restore the default exception setup */
250 __xbt_ex_ctx = &__xbt_ex_ctx_default;
251 __xbt_ex_terminate = &__xbt_ex_terminate_default;
253 /* Finish context module and SURF */
254 SIMIX_context_mod_exit();
258 xbt_free(simix_global);
266 * \brief A clock (in second).
268 * \return Return the clock.
270 XBT_INLINE double SIMIX_get_clock(void)
272 return surf_get_clock();
276 * \brief Finish the simulation initialization
278 * Must be called before the first call to SIMIX_solve()
280 XBT_INLINE void SIMIX_init(void)
286 * \brief Does a turn of the simulation
288 * 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.
289 * \param actions_done List of actions done
290 * \param actions_failed List of actions failed
291 * \return The time spent to execute the simulation or -1 if the simulation ended
293 double SIMIX_solve(xbt_fifo_t actions_done, xbt_fifo_t actions_failed)
296 smx_process_t process = NULL;
298 double elapsed_time = 0.0;
299 static int state_modifications = 1;
300 int actions_on_system = 0;
302 SIMIX_process_empty_trash();
303 if (xbt_swag_size(simix_global->process_to_run) && (elapsed_time > 0)) {
304 DEBUG0("**************************************************");
307 while ((process = xbt_swag_extract(simix_global->process_to_run))) {
308 DEBUG2("Scheduling %s on %s", process->name, process->smx_host->name);
309 SIMIX_process_schedule(process);
313 surf_action_t action = NULL;
314 surf_model_t model = NULL;
315 smx_action_t smx_action = NULL;
320 xbt_dynar_foreach(model_list, iter, model) {
321 if (xbt_swag_size(model->states.failed_action_set)
322 || xbt_swag_size(model->states.done_action_set)) {
323 state_modifications = 1;
326 if (xbt_swag_size(model->states.running_action_set)
327 || xbt_swag_size(model->states.ready_action_set)) {
328 actions_on_system = 1;
331 /* only calls surf_solve if there are actions to run */
332 if (!state_modifications && actions_on_system) {
333 DEBUG1("%f : Calling surf_solve", SIMIX_get_clock());
334 elapsed_time = surf_solve();
335 DEBUG1("Elapsed_time %f", elapsed_time);
338 actions_on_system = 0;
339 while (surf_timer_model->extension.timer.get(&fun, (void *) &arg)) {
340 /* change in process, don't quit */
341 actions_on_system = 1;
342 DEBUG2("got %p %p", fun, arg);
343 if (fun == SIMIX_process_create) {
344 smx_process_arg_t args = arg;
345 DEBUG2("Launching %s on %s", args->name, args->hostname);
346 process = SIMIX_process_create(args->name, args->code,
347 args->data, args->hostname,
348 args->argc, args->argv,
350 /* verify if process has been created */
355 if (args->kill_time > SIMIX_get_clock()) {
356 surf_timer_model->extension.timer.set(args->kill_time, (void *)
362 if (fun == simix_global->create_process_function) {
363 smx_process_arg_t args = arg;
364 DEBUG2("Launching %s on %s", args->name, args->hostname);
366 (*simix_global->create_process_function) (args->name, args->code,
369 args->argc, args->argv,
371 /* verify if process has been created */
376 if (args->kill_time > SIMIX_get_clock()) {
377 if (simix_global->kill_process_function)
378 surf_timer_model->extension.timer.set(args->kill_time, (void *)
380 kill_process_function,
383 surf_timer_model->extension.timer.set(args->kill_time, (void *)
389 if (fun == SIMIX_process_kill) {
391 DEBUG2("Killing %s on %s", process->name, process->smx_host->name);
392 SIMIX_process_kill(process);
394 if (fun == simix_global->kill_process_function) {
396 (*simix_global->kill_process_function) (process);
400 /* Wake up all process waiting for the action finish */
401 xbt_dynar_foreach(model_list, iter, model) {
402 /* stop simulation case there are no actions to run */
403 if ((xbt_swag_size(model->states.running_action_set)) ||
404 (xbt_swag_size(model->states.ready_action_set)) ||
405 (xbt_swag_size(model->states.done_action_set)) ||
406 (xbt_swag_size(model->states.failed_action_set)))
407 actions_on_system = 1;
409 while ((action = xbt_swag_extract(model->states.failed_action_set))) {
410 smx_action = action->data;
412 SIMIX_action_signal_all(smx_action);
415 while ((action = xbt_swag_extract(model->states.done_action_set))) {
416 smx_action = action->data;
418 /* Copy the transfered data of the completed communication actions */
419 /* FIXME: find a better way to determine if its a comm action */
420 if(smx_action->data != NULL)
421 SIMIX_network_copy_data((smx_comm_t)smx_action->data);
422 SIMIX_action_signal_all(smx_action);
427 state_modifications = 0;
428 if (!actions_on_system)
431 if (elapsed_time == -1) {
432 if (xbt_swag_size(simix_global->process_list) == 0) {
433 /* INFO0("Congratulations ! Simulation terminated : all processes are over"); */
435 INFO0("Oops ! Deadlock or code not perfectly clean.");
436 SIMIX_display_process_status();
437 if (XBT_LOG_ISENABLED(simix, xbt_log_priority_debug) ||
438 XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_debug)) {
442 INFO0("Return a Warning.");
449 * \brief Set the date to execute a function
451 * Set the date to execute the function on the surf.
452 * \param date Date to execute function
453 * \param function Function to be executed
454 * \param arg Parameters of the function
457 XBT_INLINE void SIMIX_timer_set(double date, void *function, void *arg)
459 surf_timer_model->extension.timer.set(date, function, arg);
462 XBT_INLINE int SIMIX_timer_get(void **function, void **arg)
464 return surf_timer_model->extension.timer.get(function, arg);
468 * \brief Registers a function to create a process.
470 * 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.
471 * \param function Create process function
474 XBT_INLINE void SIMIX_function_register_process_create(smx_creation_func_t function)
476 xbt_assert0((simix_global->create_process_function == NULL),
479 simix_global->create_process_function = function;
483 * \brief Registers a function to kill a process.
485 * 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.
486 * \param function Kill process function
489 XBT_INLINE void SIMIX_function_register_process_kill(void_f_pvoid_t function)
491 xbt_assert0((simix_global->kill_process_function == NULL),
494 simix_global->kill_process_function = function;
498 * \brief Registers a function to cleanup a process.
500 * This function registers an user function to be called when a new process ends properly.
501 * \param function cleanup process function
504 XBT_INLINE void SIMIX_function_register_process_cleanup(void_f_pvoid_t function)
506 simix_global->cleanup_process_function = function;