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"
12 #include "xbt/ex.h" /* ex_backtrace_display */
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_kernel, simix,
14 "Logging specific to SIMIX (kernel)");
16 SIMIX_Global_t simix_global = NULL;
18 /********************************* SIMIX **************************************/
19 static void __simix_config_helper(const char *name, ...)
24 SIMIX_config(name, pa);
29 static void simix_cfg_control_set(const char *control_string)
31 /* To split the string in commands, and the cursors */
32 xbt_dynar_t set_strings;
38 DEBUG1("Parse log settings '%s'", control_string);
40 /* split the string, and remove empty entries */
41 set_strings = xbt_str_split_quoted(control_string);
43 if (xbt_dynar_length(set_strings) == 0) { /* vicious user! */
44 xbt_dynar_free(&set_strings);
47 /* Parse each entry and either use it right now (if the category was already
48 created), or store it for further use */
49 xbt_dynar_foreach(set_strings, cpt, str) {
50 char *control_string, *control_string_sav, *name, *value;
53 control_string = control_string_sav = strdup(str);
54 control_string += strspn(control_string, " ");
55 name = control_string;
56 control_string += strcspn(str, ":=");
57 value = control_string;
61 xbt_assert1(strlen(name) != 0, "Invalid name for configuration: '%s'",
63 xbt_assert1(strlen(value) != 0,
64 "Invalid value for configuration: '%s'", value);
65 INFO2("setting '%s' to '%s'", name, value);
67 __simix_config_helper(name, value);
69 free(control_string_sav);
71 xbt_dynar_free(&set_strings);
74 static void simix_cfg_init(int *argc, char **argv)
79 for (i = 1; i < *argc; i++) {
80 if (!strncmp(argv[i], "--cfg=", strlen("--cfg="))) {
81 opt = strchr(argv[i], '=');
84 simix_cfg_control_set(opt);
85 DEBUG1("Did apply '%s' as config setting", opt);
86 /*remove this from argv */
88 for (j = i + 1; j < *argc; j++) {
89 argv[j - 1] = argv[j];
94 i--; /* compensate effect of next loop incrementation */
100 * \brief Initialize some SIMIX internal data.
105 void SIMIX_global_init(int *argc, char **argv)
107 s_smx_process_t proc;
110 surf_init(argc, argv); /* Initialize some common structures. Warning, it sets simix_global=NULL */
111 simix_cfg_init(argc, argv);
113 simix_global = xbt_new0(s_SIMIX_Global_t, 1);
115 simix_global->host = xbt_fifo_new();
116 simix_global->process_to_run =
117 xbt_swag_new(xbt_swag_offset(proc, synchro_hookup));
118 simix_global->process_list =
119 xbt_swag_new(xbt_swag_offset(proc, process_hookup));
120 simix_global->current_process = NULL;
121 simix_global->registered_functions = xbt_dict_new();
123 simix_global->create_process_function = NULL;
124 simix_global->kill_process_function = NULL;
125 simix_global->cleanup_process_function = SIMIX_process_cleanup;
129 /* Debug purpose, incomplete */
130 void SIMIX_display_process_status(void)
132 smx_process_t process = NULL;
133 xbt_fifo_item_t item = NULL;
135 int nbprocess = xbt_swag_size(simix_global->process_list);
137 INFO1("%d processes are still running, waiting for something.",
139 /* List the process and their state */
141 ("Legend of the following listing: \"<process> on <host>: <status>.\"");
142 xbt_swag_foreach(process, simix_global->process_list) {
143 smx_simdata_process_t p_simdata =
144 (smx_simdata_process_t) process->simdata;
145 // simdata_host_t h_simdata=(simdata_host_t)p_simdata->host->simdata;
148 asprintf(&who, "%s on %s: %s",
150 p_simdata->smx_host->name,
151 (process->simdata->blocked) ? "[BLOCKED] "
152 : ((process->simdata->suspended) ? "[SUSPENDED] " : ""));
154 if (p_simdata->mutex) {
155 who2 = bprintf("%s Blocked on mutex %p", who, p_simdata->mutex);
158 } else if (p_simdata->cond) {
161 ("%s Blocked on condition %p; Waiting for the following actions:",
162 who, p_simdata->cond);
165 xbt_fifo_foreach(p_simdata->cond->actions, item, act, smx_action_t) {
166 who2 = bprintf("%s '%s'(%p)", who, act->name,act);
173 ("%s Blocked in an unknown status (please report this bug)",
183 /* FIXME: Yeah, I'll do it in a portable maner one day [Mt] */
186 static void _XBT_CALL inthandler(int ignored)
188 INFO0("CTRL-C pressed. Displaying status and bailing out");
189 SIMIX_display_process_status();
194 * \brief Launch the SIMIX simulation, debug purpose
196 void __SIMIX_main(void)
198 smx_process_t process = NULL;
199 smx_cond_t cond = NULL;
200 smx_action_t smx_action;
201 xbt_fifo_t actions_done = xbt_fifo_new();
202 xbt_fifo_t actions_failed = xbt_fifo_new();
204 /* Prepare to display some more info when dying on Ctrl-C pressing */
205 signal(SIGINT, inthandler);
207 /* Clean IO before the run */
211 //surf_solve(); /* Takes traces into account. Returns 0.0 */
212 /* xbt_fifo_size(msg_global->process_to_run) */
214 while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
216 while ((smx_action = xbt_fifo_pop(actions_failed))) {
218 xbt_fifo_item_t _cursor;
220 DEBUG1("** %s failed **", smx_action->name);
221 xbt_fifo_foreach(smx_action->cond_list, _cursor, cond, smx_cond_t) {
222 xbt_swag_foreach(process, cond->sleeping) {
223 DEBUG2("\t preparing to wake up %s on %s",
224 process->name, process->simdata->smx_host->name);
226 SIMIX_cond_broadcast(cond);
227 /* remove conditional from action */
228 SIMIX_unregister_action_to_condition(smx_action, cond);
232 while ((smx_action = xbt_fifo_pop(actions_done))) {
233 xbt_fifo_item_t _cursor;
235 DEBUG1("** %s done **", smx_action->name);
236 xbt_fifo_foreach(smx_action->cond_list, _cursor, cond, smx_cond_t) {
237 xbt_swag_foreach(process, cond->sleeping) {
238 DEBUG2("\t preparing to wake up %s on %s",
239 process->name, process->simdata->smx_host->name);
241 SIMIX_cond_broadcast(cond);
242 /* remove conditional from action */
243 SIMIX_unregister_action_to_condition(smx_action, cond);
251 * \brief Kill all running process
254 void SIMIX_process_killall()
256 smx_process_t p = NULL;
257 smx_process_t self = SIMIX_process_self();
259 while ((p = xbt_swag_extract(simix_global->process_list))) {
261 SIMIX_process_kill(p);
264 xbt_context_empty_trash();
274 * \brief Clean the SIMIX simulation
276 * This functions remove all memories needed to the SIMIX execution
278 void SIMIX_clean(void)
280 xbt_fifo_item_t i = NULL;
282 smx_process_t p = NULL;
284 while ((p = xbt_swag_extract(simix_global->process_list))) {
285 SIMIX_process_kill(p);
288 xbt_fifo_foreach(simix_global->host, i, h, smx_host_t) {
289 __SIMIX_host_destroy(h);
291 xbt_fifo_free(simix_global->host);
292 xbt_swag_free(simix_global->process_to_run);
293 xbt_swag_free(simix_global->process_list);
294 xbt_dict_free(&(simix_global->registered_functions));
295 simix_config_finalize();
305 * \brief A clock (in second).
307 * \return Return the clock.
309 double SIMIX_get_clock(void)
311 return surf_get_clock();
315 * \brief Does a turn of the simulation
317 * 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.
318 * \param actions_done List of actions done
319 * \param actions_failed List of actions failed
320 * \return The time spent to execute the simulation or -1 if the simulation ended
322 double SIMIX_solve(xbt_fifo_t actions_done, xbt_fifo_t actions_failed)
325 smx_process_t process = NULL;
327 double elapsed_time = 0.0;
328 static int state_modifications = 1;
329 static int first = 1;
331 xbt_context_empty_trash();
332 if (xbt_swag_size(simix_global->process_to_run) && (elapsed_time > 0)) {
333 DEBUG0("**************************************************");
336 surf_solve(); /* Takes traces into account. Returns 0.0 */
339 while ((process = xbt_swag_extract(simix_global->process_to_run))) {
340 DEBUG2("Scheduling %s on %s",
341 process->name, process->simdata->smx_host->name);
342 simix_global->current_process = process;
343 xbt_context_schedule(process->simdata->context);
345 simix_global->current_process = NULL;
349 surf_action_t action = NULL;
350 surf_model_t model = NULL;
351 smx_action_t smx_action = NULL;
356 xbt_dynar_foreach(model_list, i, model) {
357 if (xbt_swag_size(model->common_public->states.failed_action_set)
358 || xbt_swag_size(model->common_public->states.
360 state_modifications = 1;
364 if (!state_modifications) {
365 DEBUG1("%f : Calling surf_solve", SIMIX_get_clock());
366 elapsed_time = surf_solve();
367 DEBUG1("Elapsed_time %f", elapsed_time);
370 while (surf_timer_model->extension_public->get(&fun, (void *) &arg)) {
371 DEBUG2("got %p %p", fun, arg);
372 if (fun == SIMIX_process_create) {
373 smx_process_arg_t args = arg;
374 DEBUG2("Launching %s on %s", args->name, args->hostname);
375 process = SIMIX_process_create(args->name, args->code,
376 args->data, args->hostname,
377 args->argc, args->argv);
378 if (args->kill_time > SIMIX_get_clock()) {
379 surf_timer_model->extension_public->set(args->kill_time,
386 if (fun == SIMIX_process_kill) {
388 DEBUG2("Killing %s on %s", process->name,
389 process->simdata->smx_host->name);
390 SIMIX_process_kill(process);
394 /* Wake up all process waiting for the action finish */
395 xbt_dynar_foreach(model_list, i, model) {
397 xbt_swag_extract(model->common_public->states.
398 failed_action_set))) {
399 smx_action = action->data;
401 xbt_fifo_unshift(actions_failed, smx_action);
405 xbt_swag_extract(model->common_public->states.
407 smx_action = action->data;
409 xbt_fifo_unshift(actions_done, smx_action);
414 state_modifications = 0;
416 if (elapsed_time == -1) {
417 if (xbt_swag_size(simix_global->process_list) == 0) {
418 /* INFO0("Congratulations ! Simulation terminated : all processes are over"); */
420 INFO0("Oops ! Deadlock or code not perfectly clean.");
421 SIMIX_display_process_status();
422 if (XBT_LOG_ISENABLED(simix, xbt_log_priority_debug) ||
423 XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_debug)) {
427 INFO0("Return a Warning.");
434 * \brief Set the date to execute a function
436 * Set the date to execute the function on the surf.
437 * \param date Date to execute function
438 * \param function Function to be executed
439 * \param arg Parameters of the function
442 void SIMIX_timer_set(double date, void *function, void *arg)
444 surf_timer_model->extension_public->set(date, function, arg);
447 int SIMIX_timer_get(void **function, void **arg)
449 return surf_timer_model->extension_public->get(function, arg);
453 * \brief Registers a function to create a process.
455 * 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.
456 * \param function Create process function
459 void SIMIX_function_register_process_create(smx_creation_func_t * function)
461 xbt_assert0((simix_global->create_process_function == NULL),
464 simix_global->create_process_function = function;
468 * \brief Registers a function to kill a process.
470 * 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.
471 * \param function Kill process function
474 void SIMIX_function_register_process_kill(void_f_pvoid_t * function)
476 xbt_assert0((simix_global->kill_process_function == NULL),
479 simix_global->kill_process_function = function;
483 * \brief Registers a function to cleanup a process.
485 * This function registers an user function to be called when a new process ends properly.
486 * \param function cleanup process function
489 void SIMIX_function_register_process_cleanup(void_f_pvoid_t * function)
491 simix_global->cleanup_process_function = function;