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 /* Prepare to display some more info when dying on Ctrl-C pressing */
83 signal(SIGINT, inthandler);
84 surf_init(argc, argv); /* Initialize SURF structures */
88 /* Debug purpose, incomplete */
89 void SIMIX_display_process_status(void)
91 smx_process_t process = NULL;
92 xbt_fifo_item_t item = NULL;
94 int nbprocess = xbt_swag_size(simix_global->process_list);
96 INFO1("%d processes are still running, waiting for something.", nbprocess);
97 /* List the process and their state */
99 ("Legend of the following listing: \"<process> on <host>: <status>.\"");
100 xbt_swag_foreach(process, simix_global->process_list) {
103 asprintf(&who, "%s on %s: %s",
105 process->smx_host->name,
106 (process->blocked) ? "[BLOCKED] "
107 : ((process->suspended) ? "[SUSPENDED] " : ""));
109 if (process->mutex) {
111 bprintf("%s Blocked on mutex %p", who,
112 (XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_verbose)) ?
113 process->mutex : (void *) 0xdead);
116 } else if (process->cond) {
119 ("%s Blocked on condition %p; Waiting for the following actions:",
121 (XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_verbose)) ?
122 process->cond : (void *) 0xdead);
125 xbt_fifo_foreach(process->cond->actions, item, act, smx_action_t) {
127 bprintf("%s '%s'(%p)", who, act->name,
128 (XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_verbose))
129 ? act : (void *) 0xdead);
136 ("%s Blocked in an unknown status (please report this bug)", who);
147 * \brief Launch the SIMIX simulation, debug purpose
149 void __SIMIX_main(void)
151 smx_process_t process = NULL;
152 smx_cond_t cond = NULL;
153 smx_action_t smx_action;
154 xbt_fifo_t actions_done = xbt_fifo_new();
155 xbt_fifo_t actions_failed = xbt_fifo_new();
157 /* Clean IO before the run */
161 //surf_solve(); /* Takes traces into account. Returns 0.0 */
162 /* xbt_fifo_size(msg_global->process_to_run) */
164 while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
166 while ((smx_action = xbt_fifo_pop(actions_failed))) {
168 xbt_fifo_item_t _cursor;
170 DEBUG1("** %s failed **", smx_action->name);
171 xbt_fifo_foreach(smx_action->cond_list, _cursor, cond, smx_cond_t) {
172 xbt_swag_foreach(process, cond->sleeping) {
173 DEBUG2("\t preparing to wake up %s on %s",
174 process->name, process->smx_host->name);
176 SIMIX_cond_broadcast(cond);
177 /* remove conditional from action */
178 SIMIX_unregister_action_to_condition(smx_action, cond);
182 while ((smx_action = xbt_fifo_pop(actions_done))) {
183 xbt_fifo_item_t _cursor;
185 DEBUG1("** %s done **", smx_action->name);
186 xbt_fifo_foreach(smx_action->cond_list, _cursor, cond, smx_cond_t) {
187 xbt_swag_foreach(process, cond->sleeping) {
188 DEBUG2("\t preparing to wake up %s on %s",
189 process->name, process->smx_host->name);
191 SIMIX_cond_broadcast(cond);
192 /* remove conditional from action */
193 SIMIX_unregister_action_to_condition(smx_action, cond);
201 * \brief Kill all running process
202 * Only maestro can kill everyone :)
204 void SIMIX_process_killall()
206 smx_process_t p = NULL;
207 xbt_assert0((simix_global->current_process == simix_global->maestro_process),
208 "You are not supposed to run this function here!");
210 while ((p = xbt_swag_extract(simix_global->process_list)))
211 SIMIX_process_kill(p);
213 SIMIX_process_empty_trash();
219 * \brief Clean the SIMIX simulation
221 * This functions remove the memory used by SIMIX
223 void SIMIX_clean(void)
225 /* Kill everyone (except maestro) */
226 SIMIX_process_killall();
228 /* Free the remaining data structures*/
229 xbt_swag_free(simix_global->process_to_run);
230 xbt_swag_free(simix_global->process_to_destroy);
231 xbt_swag_free(simix_global->process_list);
232 simix_global->process_list = NULL;
233 simix_global->process_to_destroy = NULL;
234 xbt_dict_free(&(simix_global->registered_functions));
235 xbt_dict_free(&(simix_global->host));
237 /* Let's free maestro now */
238 SIMIX_context_free(simix_global->maestro_process->context);
239 free(simix_global->maestro_process);
240 simix_global->maestro_process = NULL;
242 /* Finish context module and SURF */
243 SIMIX_context_mod_exit();
254 * \brief A clock (in second).
256 * \return Return the clock.
258 double SIMIX_get_clock(void)
260 return surf_get_clock();
264 * \brief Finish the simulation initialization
266 * Must be called before the first call to SIMIX_solve()
268 void SIMIX_init(void)
274 * \brief Does a turn of the simulation
276 * 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.
277 * \param actions_done List of actions done
278 * \param actions_failed List of actions failed
279 * \return The time spent to execute the simulation or -1 if the simulation ended
281 double SIMIX_solve(xbt_fifo_t actions_done, xbt_fifo_t actions_failed)
284 smx_process_t process = NULL;
286 double elapsed_time = 0.0;
287 static int state_modifications = 1;
289 SIMIX_process_empty_trash();
290 if (xbt_swag_size(simix_global->process_to_run) && (elapsed_time > 0)) {
291 DEBUG0("**************************************************");
294 while ((process = xbt_swag_extract(simix_global->process_to_run))) {
295 DEBUG2("Scheduling %s on %s", process->name, process->smx_host->name);
296 __SIMIX_process_schedule(process);
300 surf_action_t action = NULL;
301 surf_model_t model = NULL;
302 smx_action_t smx_action = NULL;
307 xbt_dynar_foreach(model_list, iter, model) {
308 if (xbt_swag_size(model->states.failed_action_set)
309 || xbt_swag_size(model->states.done_action_set)) {
310 state_modifications = 1;
315 if (!state_modifications) {
316 DEBUG1("%f : Calling surf_solve", SIMIX_get_clock());
317 elapsed_time = surf_solve();
318 DEBUG1("Elapsed_time %f", elapsed_time);
321 while (surf_timer_model->extension.timer.get(&fun, (void *) &arg)) {
322 DEBUG2("got %p %p", fun, arg);
323 if (fun == SIMIX_process_create) {
324 smx_process_arg_t args = arg;
325 DEBUG2("Launching %s on %s", args->name, args->hostname);
326 process = SIMIX_process_create(args->name, args->code,
327 args->data, args->hostname,
328 args->argc, args->argv,
330 if (process && args->kill_time > SIMIX_get_clock()) {
331 surf_timer_model->extension.timer.set(args->kill_time, (void *)
337 if (fun == SIMIX_process_kill) {
339 DEBUG2("Killing %s on %s", process->name,
340 process->smx_host->name);
341 SIMIX_process_kill(process);
345 /* Wake up all process waiting for the action finish */
346 xbt_dynar_foreach(model_list, iter, model) {
347 while ((action = xbt_swag_extract(model->states.failed_action_set))) {
348 smx_action = action->data;
350 xbt_fifo_unshift(actions_failed, smx_action);
353 while ((action = xbt_swag_extract(model->states.done_action_set))) {
354 smx_action = action->data;
356 xbt_fifo_unshift(actions_done, smx_action);
361 state_modifications = 0;
363 if (elapsed_time == -1) {
364 if (xbt_swag_size(simix_global->process_list) == 0) {
365 /* INFO0("Congratulations ! Simulation terminated : all processes are over"); */
367 INFO0("Oops ! Deadlock or code not perfectly clean.");
368 SIMIX_display_process_status();
369 if (XBT_LOG_ISENABLED(simix, xbt_log_priority_debug) ||
370 XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_debug)) {
374 INFO0("Return a Warning.");
381 * \brief Set the date to execute a function
383 * Set the date to execute the function on the surf.
384 * \param date Date to execute function
385 * \param function Function to be executed
386 * \param arg Parameters of the function
389 void SIMIX_timer_set(double date, void *function, void *arg)
391 surf_timer_model->extension.timer.set(date, function, arg);
394 int SIMIX_timer_get(void **function, void **arg)
396 return surf_timer_model->extension.timer.get(function, arg);
400 * \brief Registers a function to create a process.
402 * 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.
403 * \param function Create process function
406 void SIMIX_function_register_process_create(smx_creation_func_t function)
408 xbt_assert0((simix_global->create_process_function == NULL),
411 simix_global->create_process_function = function;
415 * \brief Registers a function to kill a process.
417 * 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.
418 * \param function Kill process function
421 void SIMIX_function_register_process_kill(void_f_pvoid_t function)
423 xbt_assert0((simix_global->kill_process_function == NULL),
426 simix_global->kill_process_function = function;
430 * \brief Registers a function to cleanup a process.
432 * This function registers an user function to be called when a new process ends properly.
433 * \param function cleanup process function
436 void SIMIX_function_register_process_cleanup(void_f_pvoid_t function)
438 simix_global->cleanup_process_function = function;