Logo AND Algorithmique Numérique Distribuée

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