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, ...) {
23 SIMIX_config(name,pa);
28 static void simix_cfg_control_set(const char* control_string) {
29 /* To split the string in commands, and the cursors */
30 xbt_dynar_t set_strings;
36 DEBUG1("Parse log settings '%s'",control_string);
38 /* split the string, and remove empty entries */
39 set_strings=xbt_str_split_quoted(control_string);
41 if (xbt_dynar_length(set_strings) == 0) { /* vicious user! */
42 xbt_dynar_free(&set_strings);
45 /* Parse each entry and either use it right now (if the category was already
46 created), or store it for further use */
47 xbt_dynar_foreach(set_strings,cpt,str) {
48 char *control_string,*control_string_sav,*name,*value;
51 control_string = control_string_sav = strdup(str);
52 control_string += strspn(control_string, " ");
53 name = control_string;
54 control_string += strcspn(str, ":=");
55 value = control_string;
59 xbt_assert1(strlen(name)!=0, "Invalid name for configuration: '%s'",name);
60 xbt_assert1(strlen(value)!=0, "Invalid value for configuration: '%s'",value);
61 INFO2("setting '%s' to '%s'",name,value);
63 __simix_config_helper(name,value);
65 free(control_string_sav);
67 xbt_dynar_free(&set_strings);
70 static void simix_cfg_init(int *argc,char **argv) {
74 for (i=1; i<*argc; i++){
75 if (!strncmp(argv[i],"--cfg=",strlen("--cfg="))) {
76 opt=strchr(argv[i],'=');
79 simix_cfg_control_set(opt);
80 DEBUG1("Did apply '%s' as config setting",opt);
81 /*remove this from argv*/
83 for (j=i+1; j<*argc; j++){
89 i--; /* compensate effect of next loop incrementation */
95 * \brief Initialize some SIMIX internal data.
100 void SIMIX_global_init(int *argc, char **argv)
102 s_smx_process_t proc;
105 surf_init(argc, argv); /* Initialize some common structures. Warning, it sets simix_global=NULL */
106 simix_cfg_init(argc,argv);
108 simix_global = xbt_new0(s_SIMIX_Global_t,1);
110 simix_global->host = xbt_fifo_new();
111 simix_global->process_to_run = xbt_swag_new(xbt_swag_offset(proc,synchro_hookup));
112 simix_global->process_list = xbt_swag_new(xbt_swag_offset(proc,process_hookup));
113 simix_global->current_process = NULL;
114 simix_global->registered_functions = xbt_dict_new();
116 simix_global->create_process_function = NULL;
117 simix_global->kill_process_function = NULL;
118 simix_global->cleanup_process_function = SIMIX_process_cleanup;
122 /* Debug purpose, incomplete */
123 void SIMIX_display_process_status(void)
125 smx_process_t process = NULL;
126 xbt_fifo_item_t item = NULL;
128 int nbprocess=xbt_swag_size(simix_global->process_list);
130 INFO1("%d processes are still running, waiting for something.",
132 /* List the process and their state */
133 INFO0("Legend of the following listing: \"<process> on <host>: <status>.\"");
134 xbt_swag_foreach(process, simix_global->process_list) {
135 smx_simdata_process_t p_simdata = (smx_simdata_process_t) process->simdata;
136 // simdata_host_t h_simdata=(simdata_host_t)p_simdata->host->simdata;
139 asprintf(&who,"%s on %s: %s",
141 p_simdata->s_host->name,
142 (process->simdata->blocked)?"[BLOCKED] "
143 :((process->simdata->suspended)?"[SUSPENDED] ":""));
145 if (p_simdata->mutex) {
146 who2=bprintf("%s Blocked on mutex %p",who,p_simdata->mutex);
148 } else if (p_simdata->cond) {
149 who2=bprintf("%s Blocked on condition %p; Waiting for the following actions:",who,p_simdata->cond);
151 xbt_fifo_foreach(p_simdata->cond->actions,item, act, smx_action_t) {
152 who2=bprintf("%s '%s'",who,act->name);
156 who2=bprintf("%s Blocked in an unknown status (please report this bug)",who);
164 /* FIXME: Yeah, I'll do it in a portable maner one day [Mt] */
167 static void _XBT_CALL inthandler(int ignored)
169 INFO0("CTRL-C pressed. Displaying status and bailing out");
170 SIMIX_display_process_status();
175 * \brief Launch the SIMIX simulation, debug purpose
177 void __SIMIX_main(void)
179 smx_process_t process = NULL;
180 smx_cond_t cond = NULL;
181 smx_action_t smx_action;
182 xbt_fifo_t actions_done = xbt_fifo_new();
183 xbt_fifo_t actions_failed = xbt_fifo_new();
185 /* Prepare to display some more info when dying on Ctrl-C pressing */
186 signal(SIGINT,inthandler);
188 /* Clean IO before the run */
192 //surf_solve(); /* Takes traces into account. Returns 0.0 */
193 /* xbt_fifo_size(msg_global->process_to_run) */
195 while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
197 while ( (smx_action = xbt_fifo_pop(actions_failed)) ) {
199 xbt_fifo_item_t _cursor;
201 DEBUG1("** %s failed **",smx_action->name);
202 xbt_fifo_foreach(smx_action->cond_list,_cursor,cond,smx_cond_t) {
203 xbt_swag_foreach(process,cond->sleeping) {
204 DEBUG2("\t preparing to wake up %s on %s",
205 process->name, process->simdata->s_host->name);
207 SIMIX_cond_broadcast(cond);
208 /* remove conditional from action */
209 xbt_fifo_remove(smx_action->cond_list,cond);
213 while ( (smx_action = xbt_fifo_pop(actions_done)) ) {
214 xbt_fifo_item_t _cursor;
216 DEBUG1("** %s done **",smx_action->name);
217 xbt_fifo_foreach(smx_action->cond_list,_cursor,cond,smx_cond_t) {
218 xbt_swag_foreach(process,cond->sleeping) {
219 DEBUG2("\t preparing to wake up %s on %s",
220 process->name, process->simdata->s_host->name);
222 SIMIX_cond_broadcast(cond);
223 /* remove conditional from action */
224 xbt_fifo_remove(smx_action->cond_list,cond);
232 * \brief Kill all running process
235 void SIMIX_process_killall()
237 smx_process_t p = NULL;
238 smx_process_t self = SIMIX_process_self();
240 while((p=xbt_swag_extract(simix_global->process_list))) {
241 if(p!=self) SIMIX_process_kill(p);
244 xbt_context_empty_trash();
254 * \brief Clean the SIMIX simulation
256 * This functions remove all memories needed to the SIMIX execution
258 void SIMIX_clean(void)
260 xbt_fifo_item_t i = NULL;
262 smx_process_t p = NULL;
264 while((p=xbt_swag_extract(simix_global->process_list))) {
265 SIMIX_process_kill(p);
268 xbt_fifo_foreach(simix_global->host,i,h,smx_host_t) {
269 __SIMIX_host_destroy(h);
271 xbt_fifo_free(simix_global->host);
272 xbt_swag_free(simix_global->process_to_run);
273 xbt_swag_free(simix_global->process_list);
274 xbt_dict_free(&(simix_global->registered_functions));
275 simix_config_finalize();
284 * \brief A clock (in second).
286 * \return Return the clock.
288 double SIMIX_get_clock(void)
290 return surf_get_clock();
294 * \brief Does a turn of the simulation
296 * 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.
297 * \param actions_done List of actions done
298 * \param actions_failed List of actions failed
299 * \return The time spent to execute the simulation or -1 if the simulation ended
301 double SIMIX_solve(xbt_fifo_t actions_done, xbt_fifo_t actions_failed)
304 smx_process_t process = NULL;
306 double elapsed_time = 0.0;
307 static int state_modifications = 1;
308 static int first = 1;
310 xbt_context_empty_trash();
311 if(xbt_swag_size(simix_global->process_to_run) && (elapsed_time>0)) {
312 DEBUG0("**************************************************");
315 surf_solve();/* Takes traces into account. Returns 0.0 */
318 while ((process = xbt_swag_extract(simix_global->process_to_run))) {
319 DEBUG2("Scheduling %s on %s",
321 process->simdata->s_host->name);
322 simix_global->current_process = process;
323 xbt_context_schedule(process->simdata->context);
325 simix_global->current_process = NULL;
329 surf_action_t action = NULL;
330 surf_resource_t resource = NULL;
331 smx_action_t smx_action = NULL;
336 xbt_dynar_foreach(resource_list, i, resource) {
337 if(xbt_swag_size(resource->common_public->states.failed_action_set) ||
338 xbt_swag_size(resource->common_public->states.done_action_set)) {
339 state_modifications = 1;
343 if(!state_modifications) {
344 DEBUG1("%f : Calling surf_solve",SIMIX_get_clock());
345 elapsed_time = surf_solve();
346 DEBUG1("Elapsed_time %f",elapsed_time);
349 while (surf_timer_resource->extension_public->get(&fun,(void*)&arg)) {
350 DEBUG2("got %p %p", fun, arg);
351 if(fun==SIMIX_process_create) {
352 smx_process_arg_t args = arg;
353 DEBUG2("Launching %s on %s", args->name, args->hostname);
354 process = SIMIX_process_create(args->name, args->code,
355 args->data, args->hostname,
356 args->argc,args->argv);
357 if(args->kill_time > SIMIX_get_clock()) {
358 surf_timer_resource->extension_public->set(args->kill_time,
359 (void*) &SIMIX_process_kill,
364 if(fun==SIMIX_process_kill) {
366 DEBUG2("Killing %s on %s", process->name,
367 process->simdata->s_host->name);
368 SIMIX_process_kill(process);
372 /* Wake up all process waiting for the action finish */
373 xbt_dynar_foreach(resource_list, i, resource) {
374 while ((action = xbt_swag_extract(resource->common_public->states.failed_action_set))) {
375 smx_action = action->data;
377 xbt_fifo_unshift(actions_failed,smx_action);
380 while ((action =xbt_swag_extract(resource->common_public->states.done_action_set))) {
381 smx_action = action->data;
383 xbt_fifo_unshift(actions_done,smx_action);
388 state_modifications = 0;
390 if (elapsed_time == -1) {
391 if (xbt_swag_size(simix_global->process_list) == 0) {
392 /* INFO0("Congratulations ! Simulation terminated : all processes are over"); */
394 INFO0("Oops ! Deadlock or code not perfectly clean.");
395 SIMIX_display_process_status();
396 if(XBT_LOG_ISENABLED(simix, xbt_log_priority_debug) ||
397 XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_debug)) {
401 INFO0("Return a Warning.");
408 * \brief Set the date to execute a function
410 * Set the date to execute the function on the surf.
411 * \param date Date to execute function
412 * \param function Function to be executed
413 * \param arg Parameters of the function
416 void SIMIX_timer_set (double date, void *function, void *arg)
418 surf_timer_resource->extension_public->set(date, function, arg);
421 int SIMIX_timer_get(void **function, void **arg)
423 return surf_timer_resource->extension_public->get(function, arg);
427 * \brief Registers a function to create a process.
429 * 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.
430 * \param function Create process function
433 void SIMIX_function_register_process_create(smx_creation_func_t* function)
435 xbt_assert0((simix_global->create_process_function == NULL), "Data already set");
437 simix_global->create_process_function = function;
441 * \brief Registers a function to kill a process.
443 * 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.
444 * \param function Kill process function
447 void SIMIX_function_register_process_kill(void_f_pvoid_t* function)
449 xbt_assert0((simix_global->kill_process_function == NULL), "Data already set");
451 simix_global->kill_process_function = function;
455 * \brief Registers a function to cleanup a process.
457 * This function registers an user function to be called when a new process ends properly.
458 * \param function cleanup process function
461 void SIMIX_function_register_process_cleanup(void_f_pvoid_t* function)
463 simix_global->cleanup_process_function = function;