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 int __stop_time = -1.0 ;
18 SIMIX_Global_t simix_global = NULL;
21 /** \defgroup simix_simulation SIMIX simulation Functions
22 * \brief This section describes the functions you need to know to
23 * set up a simulation. You should have a look at \ref SIMIX_examples
24 * to have an overview of their usage.
25 * \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Simulation functions" --> \endhtmlonly
28 /********************************* SIMIX **************************************/
30 /** \ingroup simix_simulation
31 * \brief Initialize some SIMIX internal data.
33 void SIMIX_global_init_args(int *argc, char **argv)
35 SIMIX_global_init(argc,argv);
38 /** \ingroup simix_simulation
39 * \brief Initialize some SIMIX internal data.
41 void SIMIX_global_init(int *argc, char **argv)
46 surf_init(argc, argv); /* Initialize some common structures. Warning, it sets msg_global=NULL */
48 simix_global = xbt_new0(s_SIMIX_Global_t,1);
51 simix_global->host = xbt_fifo_new();
52 simix_global->process_to_run = xbt_swag_new(xbt_swag_offset(proc,synchro_hookup));
53 simix_global->process_list = xbt_swag_new(xbt_swag_offset(proc,process_hookup));
54 simix_global->current_process = NULL;
55 simix_global->registered_functions = xbt_dict_new();
59 /* Debug purpose, incomplete */
60 void __SIMIX_display_process_status(void)
62 smx_process_t process = NULL;
63 //xbt_fifo_item_t item = NULL;
65 int nbprocess=xbt_swag_size(simix_global->process_list);
67 INFO1("SIMIX: %d processes are still running, waiting for something.",
69 /* List the process and their state */
70 INFO0("SIMIX: <process> on <host>: <status>.");
71 xbt_swag_foreach(process, simix_global->process_list) {
72 simdata_process_t p_simdata = (simdata_process_t) process->simdata;
73 // simdata_host_t h_simdata=(simdata_host_t)p_simdata->host->simdata;
76 asprintf(&who,"SIMIX: %s on %s: %s",
78 p_simdata->host->name,
79 (process->simdata->blocked)?"[blocked] "
80 :((process->simdata->suspended)?"[suspended] ":""));
86 /* FIXME: Yeah, I'll do it in a portable maner one day [Mt] */
89 static void _XBT_CALL inthandler(int ignored)
91 INFO0("CTRL-C pressed. Displaying status and bailing out");
92 __SIMIX_display_process_status();
96 /** \ingroup msg_simulation
97 * \brief Launch the SIMIX simulation
101 smx_process_t process = NULL;
102 smx_cond_t cond = NULL;
104 double elapsed_time = 0.0;
105 int state_modifications = 1;
107 /* Prepare to display some more info when dying on Ctrl-C pressing */
108 signal(SIGINT,inthandler);
110 /* Clean IO before the run */
114 surf_solve(); /* Takes traces into account. Returns 0.0 */
115 /* xbt_fifo_size(msg_global->process_to_run) */
117 xbt_context_empty_trash();
118 if(xbt_swag_size(simix_global->process_to_run) && (elapsed_time>0)) {
119 DEBUG0("**************************************************");
122 if((__stop_time>0) && (SIMIX_get_clock() >= __stop_time)) {
123 DEBUG0("Let's stop here!");
126 while ((process = xbt_swag_extract(simix_global->process_to_run))) {
127 DEBUG2("Scheduling %s on %s",
129 process->simdata->host->name);
130 simix_global->current_process = process;
131 xbt_context_schedule(process->simdata->context);
133 simix_global->current_process = NULL;
137 surf_action_t action = NULL;
138 surf_resource_t resource = NULL;
139 smx_action_t smx_action = NULL;
144 xbt_dynar_foreach(resource_list, i, resource) {
145 if(xbt_swag_size(resource->common_public->states.failed_action_set) ||
146 xbt_swag_size(resource->common_public->states.done_action_set))
147 state_modifications = 1;
150 if(!state_modifications) {
151 DEBUG1("%f : Calling surf_solve",SIMIX_get_clock());
152 elapsed_time = surf_solve();
153 DEBUG1("Elapsed_time %f",elapsed_time);
155 if (elapsed_time<0.0) {
156 /* fprintf(stderr, "We're done %g\n",elapsed_time); */
161 while (surf_timer_resource->extension_public->get(&fun,(void*)&arg)) {
162 DEBUG2("got %p %p", fun, arg);
163 if(fun==SIMIX_process_create_with_arguments) {
164 process_arg_t args = arg;
165 DEBUG2("Launching %s on %s", args->name, args->host->name);
166 process = SIMIX_process_create_with_arguments(args->name, args->code,
167 args->data, args->host,
168 args->argc,args->argv);
169 if(args->kill_time > SIMIX_get_clock()) {
170 surf_timer_resource->extension_public->set(args->kill_time,
171 (void*) &SIMIX_process_kill,
176 if(fun==SIMIX_process_kill) {
178 DEBUG2("Killing %s on %s", process->name,
179 process->simdata->host->name);
180 SIMIX_process_kill(process);
184 /* Wake up all process waiting for the action finish */
185 xbt_dynar_foreach(resource_list, i, resource) {
187 xbt_swag_extract(resource->common_public->states.
188 failed_action_set))) {
189 smx_action = action->data;
191 xbt_fifo_item_t _cursor;
193 DEBUG1("** %s failed **",smx_action->name);
194 /* put all the process that are waiting in a conditional (dummy action) on the process_to_run list */
195 if ( smx_action->simdata->action_block) {
196 xbt_swag_insert(smx_action->simdata->cond_process,simix_global->process_to_run);
198 /* else, do the dummy action finish, call the signal broadcast */
200 xbt_fifo_foreach(smx_action->simdata->cond_list,_cursor,cond,smx_cond_t) {
201 xbt_swag_foreach(process,cond->sleeping) {
202 DEBUG2("\t preparing to wake up %s on %s",
203 process->name, process->simdata->host->name);
205 SIMIX_cond_broadcast(cond);
206 /* remove conditional from action */
207 xbt_fifo_remove(smx_action->simdata->cond_list,cond);
214 xbt_swag_extract(resource->common_public->states.
216 smx_action = action->data;
218 xbt_fifo_item_t _cursor;
220 DEBUG1("** %s done **",smx_action->name);
221 /* put all the process that are waiting in a conditional (dummy action) on the process_to_run list */
222 if ( smx_action->simdata->action_block) {
223 if (smx_action->simdata->timeout_cond) {
224 xbt_swag_remove(smx_action->simdata->cond_process,smx_action->simdata->timeout_cond->sleeping);
226 process = smx_action->simdata->cond_process;
227 xbt_swag_insert(smx_action->simdata->cond_process,simix_global->process_to_run);
228 DEBUG2("\t preparing to wake up %s on %s",
229 process->name, process->simdata->host->name);
231 /* else, do the dummy action finish, call the signal broadcast */
233 xbt_fifo_foreach(smx_action->simdata->cond_list,_cursor,cond,smx_cond_t) {
234 xbt_swag_foreach(process,cond->sleeping) {
235 DEBUG2("\t preparing to wake up %s on %s",
236 process->name, process->simdata->host->name);
238 SIMIX_cond_broadcast(cond);
239 /* remove conditional from action */
240 xbt_fifo_remove(smx_action->simdata->cond_list,cond);
247 xbt_fifo_item_t _cursor;
249 DEBUG1("** %s done **",smx_action->name);
250 xbt_fifo_foreach(smx_action->simdata->cond_list,_cursor,cond,smx_cond_t) {
251 SIMIX_cond_broadcast(cond);
252 xbt_swag_foreach(process,cond->sleeping) {
253 DEBUG2("\t preparing to wake up %s on %s",
254 process->name, process->simdata->host->name);
263 state_modifications = 0;
266 if (xbt_swag_size(simix_global->process_list) == 0) {
267 INFO0("Congratulations ! Simulation terminated : all processes are over");
270 INFO0("Oops ! Deadlock or code not perfectly clean.");
271 __SIMIX_display_process_status();
272 if(XBT_LOG_ISENABLED(simix, xbt_log_priority_debug) ||
273 XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_debug)) {
278 INFO0("Return a Warning.");
283 /** \ingroup msg_simulation
284 * \brief Kill all running process
286 * \param reset_PIDs should we reset the PID numbers. A negative
287 * number means no reset and a positive number will be used to set the PID
288 * of the next newly created process.
290 void SIMIX_process_killall()
292 smx_process_t p = NULL;
293 smx_process_t self = SIMIX_process_self();
295 while((p=xbt_swag_extract(simix_global->process_list))) {
296 if(p!=self) SIMIX_process_kill(p);
299 xbt_context_empty_trash();
308 /** \ingroup msg_simulation
309 * \brief Clean the SIMIX simulation
311 void SIMIX_clean(void)
313 xbt_fifo_item_t i = NULL;
315 smx_process_t p = NULL;
318 while((p=xbt_swag_extract(simix_global->process_list))) {
319 SIMIX_process_kill(p);
323 xbt_fifo_foreach(simix_global->host,i,h,smx_host_t) {
324 __SIMIX_host_destroy(h);
326 xbt_fifo_free(simix_global->host);
327 xbt_swag_free(simix_global->process_to_run);
328 xbt_swag_free(simix_global->process_list);
329 xbt_dict_free(&(simix_global->registered_functions));
330 simix_config_finalize();
338 /** \ingroup msg_easier_life
339 * \brief A clock (in second).
341 double SIMIX_get_clock(void) {
342 return surf_get_clock();