Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
We need a default if the user didn't set any function.
[simgrid.git] / src / simix / smx_global.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2007 Arnaud Legrand, Bruno Donassolo.
4    All rights reserved.                                          */
5
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. */
8
9 #include "private.h"
10 #include "xbt/sysdep.h"
11 #include "xbt/log.h"
12 #include "xbt/ex.h" /* ex_backtrace_display */
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_kernel, simix,
14                                 "Logging specific to SIMIX (kernel)");
15
16 SIMIX_Global_t simix_global = NULL;
17
18 /********************************* SIMIX **************************************/
19
20 /**
21  * \brief Initialize some SIMIX internal data.
22  *
23  * \param argc Argc
24  * \param argv Argv
25  */
26 void SIMIX_global_init(int *argc, char **argv)
27 {
28         s_smx_process_t proc;
29
30         if (!simix_global) {
31                 surf_init(argc, argv);  /* Initialize some common structures. Warning, it sets simix_global=NULL */
32
33                 simix_global = xbt_new0(s_SIMIX_Global_t,1);
34
35                 simix_global->host = xbt_fifo_new();
36                 simix_global->process_to_run = xbt_swag_new(xbt_swag_offset(proc,synchro_hookup));
37                 simix_global->process_list = xbt_swag_new(xbt_swag_offset(proc,process_hookup));
38                 simix_global->current_process = NULL;
39                 simix_global->registered_functions = xbt_dict_new();
40
41                 simix_global->create_process_function = NULL;
42                 simix_global->kill_process_function = NULL;
43                 simix_global->cleanup_process_function = SIMIX_process_cleanup;
44         }
45 }
46
47 /* Debug purpose, incomplete */
48 void SIMIX_display_process_status(void)
49 {
50    smx_process_t process = NULL;
51    xbt_fifo_item_t item = NULL;
52          smx_action_t act;
53    int nbprocess=xbt_swag_size(simix_global->process_list);
54    
55    INFO1("%d processes are still running, waiting for something.",
56          nbprocess);
57    /*  List the process and their state */
58    INFO0("Legend of the following listing: \"<process> on <host>: <status>.\"");
59    xbt_swag_foreach(process, simix_global->process_list) {
60       smx_simdata_process_t p_simdata = (smx_simdata_process_t) process->simdata;
61      // simdata_host_t h_simdata=(simdata_host_t)p_simdata->host->simdata;
62       char *who, *who2;
63         
64       asprintf(&who,"%s on %s: %s",
65                process->name,
66                p_simdata->s_host->name,
67                (process->simdata->blocked)?"[BLOCKED] "
68                :((process->simdata->suspended)?"[SUSPENDED] ":""));
69       
70       if (p_simdata->mutex) {
71          who2=bprintf("%s Blocked on mutex %p",who,p_simdata->mutex);
72          free(who); who=who2;
73       } else if (p_simdata->cond) {
74          who2=bprintf("%s Blocked on condition %p; Waiting for the following actions:",who,p_simdata->cond);
75          free(who); who=who2;
76          xbt_fifo_foreach(p_simdata->cond->actions,item, act, smx_action_t) {
77             who2=bprintf("%s '%s'",who,act->name);
78             free(who); who=who2;
79          }
80       } else {
81          who2=bprintf("%s Blocked in an unknown status (please report this bug)",who);
82          free(who); who=who2;
83       }
84       INFO1("%s.",who);
85       free(who);
86    }
87 }
88
89 /* FIXME: Yeah, I'll do it in a portable maner one day [Mt] */
90 #include <signal.h>
91
92 static void _XBT_CALL inthandler(int ignored)
93 {
94    INFO0("CTRL-C pressed. Displaying status and bailing out");
95    SIMIX_display_process_status();
96    exit(1);
97 }
98
99 /**
100  * \brief Launch the SIMIX simulation, debug purpose
101  */
102 void __SIMIX_main(void)
103 {
104         smx_process_t process = NULL;
105         smx_cond_t cond = NULL;
106         smx_action_t smx_action;
107         xbt_fifo_t actions_done = xbt_fifo_new();
108         xbt_fifo_t actions_failed = xbt_fifo_new();
109
110         /* Prepare to display some more info when dying on Ctrl-C pressing */
111         signal(SIGINT,inthandler);
112
113         /* Clean IO before the run */
114         fflush(stdout);
115         fflush(stderr);
116
117         //surf_solve(); /* Takes traces into account. Returns 0.0 */
118         /* xbt_fifo_size(msg_global->process_to_run) */
119
120         while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
121
122                 while ( (smx_action = xbt_fifo_pop(actions_failed)) ) {
123
124                         xbt_fifo_item_t _cursor;
125
126                         DEBUG1("** %s failed **",smx_action->name);
127                         xbt_fifo_foreach(smx_action->cond_list,_cursor,cond,smx_cond_t) {
128                                 xbt_swag_foreach(process,cond->sleeping) {
129                                         DEBUG2("\t preparing to wake up %s on %s",           
130                                                         process->name,  process->simdata->s_host->name);
131                                 }
132                                 SIMIX_cond_broadcast(cond);
133                                 /* remove conditional from action */
134                                 xbt_fifo_remove(smx_action->cond_list,cond);
135                         }
136                 }
137
138                 while ( (smx_action = xbt_fifo_pop(actions_done)) ) {
139                         xbt_fifo_item_t _cursor;
140
141                         DEBUG1("** %s done **",smx_action->name);
142                         xbt_fifo_foreach(smx_action->cond_list,_cursor,cond,smx_cond_t) {
143                                 xbt_swag_foreach(process,cond->sleeping) {
144                                         DEBUG2("\t preparing to wake up %s on %s",           
145                                                         process->name,  process->simdata->s_host->name);
146                                 }
147                                 SIMIX_cond_broadcast(cond);
148                                 /* remove conditional from action */
149                                 xbt_fifo_remove(smx_action->cond_list,cond);
150                         }
151                 }
152         }
153         return;
154 }
155
156 /**
157  * \brief Kill all running process
158  *
159  */
160 void SIMIX_process_killall()
161 {
162   smx_process_t p = NULL;
163   smx_process_t self = SIMIX_process_self();
164
165   while((p=xbt_swag_extract(simix_global->process_list))) {
166     if(p!=self) SIMIX_process_kill(p);
167   }
168
169   xbt_context_empty_trash();
170
171   if(self) {
172     xbt_context_yield();
173   }
174
175   return;
176 }
177
178 /** 
179  * \brief Clean the SIMIX simulation
180  *
181  * This functions remove all memories needed to the SIMIX execution
182  */
183 void SIMIX_clean(void)
184 {
185   xbt_fifo_item_t i = NULL;
186   smx_host_t h = NULL;
187   smx_process_t p = NULL;
188
189   while((p=xbt_swag_extract(simix_global->process_list))) {
190     SIMIX_process_kill(p);
191   }
192
193   xbt_fifo_foreach(simix_global->host,i,h,smx_host_t) {
194     __SIMIX_host_destroy(h);
195   }
196   xbt_fifo_free(simix_global->host);
197   xbt_swag_free(simix_global->process_to_run);
198   xbt_swag_free(simix_global->process_list);
199   xbt_dict_free(&(simix_global->registered_functions));
200   simix_config_finalize();
201   free(simix_global);
202   surf_exit();
203
204   return ;
205 }
206
207
208 /**
209  * \brief A clock (in second).
210  *
211  * \return Return the clock.
212  */
213 double SIMIX_get_clock(void)
214 {
215   return surf_get_clock();
216 }
217
218 /**
219  *      \brief Does a turn of the simulation
220  *
221  *      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.         
222  *      \param actions_done List of actions done
223  *      \param actions_failed List of actions failed
224  *      \return The time spent to execute the simulation or -1 if the simulation ended
225  */
226 double SIMIX_solve(xbt_fifo_t actions_done, xbt_fifo_t actions_failed) 
227 {
228
229         smx_process_t process = NULL;
230         int i;
231         double elapsed_time = 0.0;
232         static int state_modifications = 1;
233         static int first = 1;
234
235         xbt_context_empty_trash();
236         if(xbt_swag_size(simix_global->process_to_run) && (elapsed_time>0)) {
237                 DEBUG0("**************************************************");
238         }
239         if (first) {
240                 surf_solve();/* Takes traces into account. Returns 0.0 */
241                 first=0;
242         }
243         while ((process = xbt_swag_extract(simix_global->process_to_run))) {
244                 DEBUG2("Scheduling %s on %s",        
245                                 process->name,
246                                 process->simdata->s_host->name);
247                 simix_global->current_process = process;
248                 xbt_context_schedule(process->simdata->context);
249                 /*       fflush(NULL); */
250                 simix_global->current_process = NULL;
251         }
252
253         {
254                 surf_action_t action = NULL;
255                 surf_resource_t resource = NULL;
256                 smx_action_t smx_action = NULL;
257
258                 void *fun = NULL;
259                 void *arg = NULL;
260
261                 xbt_dynar_foreach(resource_list, i, resource) {
262                         if(xbt_swag_size(resource->common_public->states.failed_action_set) ||
263                                         xbt_swag_size(resource->common_public->states.done_action_set)) {
264                                 state_modifications = 1;
265                                 }
266                 }
267
268                 if(!state_modifications) {
269                         DEBUG1("%f : Calling surf_solve",SIMIX_get_clock());
270                         elapsed_time = surf_solve();
271                         DEBUG1("Elapsed_time %f",elapsed_time);
272                 }
273
274                 while (surf_timer_resource->extension_public->get(&fun,(void*)&arg)) {
275                         DEBUG2("got %p %p", fun, arg);
276                         if(fun==SIMIX_process_create) {
277                                 smx_process_arg_t args = arg;
278                                 DEBUG2("Launching %s on %s", args->name, args->hostname);
279                                 process = SIMIX_process_create(args->name, args->code, 
280                                                 args->data, args->hostname,
281                                                 args->argc,args->argv);
282                                 if(args->kill_time > SIMIX_get_clock()) {
283                                         surf_timer_resource->extension_public->set(args->kill_time, 
284                                                         (void*) &SIMIX_process_kill,
285                                                         (void*) process);
286                                 }
287                                 xbt_free(args);
288                         }
289                         if(fun==SIMIX_process_kill) {
290                                 process = arg;
291                                 DEBUG2("Killing %s on %s", process->name, 
292                                                 process->simdata->s_host->name);
293                                 SIMIX_process_kill(process);
294                         }
295                 }
296
297                 /* Wake up all process waiting for the action finish */
298                 xbt_dynar_foreach(resource_list, i, resource) {
299                         while ((action = xbt_swag_extract(resource->common_public->states.failed_action_set))) {
300                                 smx_action = action->data;
301                                 if (smx_action) {
302                                         xbt_fifo_unshift(actions_failed,smx_action);
303                                 }
304                         }
305                         while ((action =xbt_swag_extract(resource->common_public->states.done_action_set))) {
306                                 smx_action = action->data;
307                                 if (smx_action) {
308                                         xbt_fifo_unshift(actions_done,smx_action);
309                                 }
310                         }
311                 }
312         }
313         state_modifications = 0;
314
315         if (elapsed_time == -1) {
316                 if (xbt_swag_size(simix_global->process_list) == 0) {
317 /*                      INFO0("Congratulations ! Simulation terminated : all processes are over"); */
318                 } else {
319                         INFO0("Oops ! Deadlock or code not perfectly clean.");
320                         SIMIX_display_process_status();
321                         if(XBT_LOG_ISENABLED(simix, xbt_log_priority_debug) ||
322                                         XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_debug)) {
323                                 DEBUG0("Aborting!");
324                                 xbt_abort();
325                         }
326                         INFO0("Return a Warning.");
327                 }
328         }
329         return elapsed_time;
330 }
331
332 /**
333  *      \brief Set the date to execute a function
334  *
335  * Set the date to execute the function on the surf.
336  *      \param date Date to execute function
337  *      \param function Function to be executed
338  *      \param arg Parameters of the function
339  *
340  */
341 void SIMIX_timer_set (double date, void *function, void *arg)
342 {
343         surf_timer_resource->extension_public->set(date, function, arg);
344 }
345
346 int SIMIX_timer_get(void **function, void **arg)
347 {
348         return surf_timer_resource->extension_public->get(function, arg);
349 }
350
351 /**
352  *      \brief Registers a function to create a process.
353  *
354  *      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.
355  *      \param function Create process function
356  *
357  */
358 void SIMIX_function_register_process_create(smx_creation_func_t* function)
359 {
360   xbt_assert0((simix_global->create_process_function == NULL), "Data already set");
361
362   simix_global->create_process_function = function;
363 }
364
365 /**
366  *      \brief Registers a function to kill a process.
367  *
368  *      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.
369  *      \param function Kill process function
370  *
371  */
372 void SIMIX_function_register_process_kill(void_f_pvoid_t* function)
373 {
374   xbt_assert0((simix_global->kill_process_function == NULL), "Data already set");
375
376   simix_global->kill_process_function = function;
377 }
378
379 /**
380  *      \brief Registers a function to cleanup a process.
381  *
382  *      This function registers an user function to be called when a new process ends properly.
383  *      \param function cleanup process function
384  *
385  */
386 void SIMIX_function_register_process_cleanup(void_f_pvoid_t* function)
387 {
388   simix_global->cleanup_process_function = function;
389 }