Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
7335b59e2323c5d9262f97b964d0169a203406c5
[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
17 SIMIX_Global_t simix_global = NULL;
18
19
20 /** \defgroup simix_simulation   SIMIX simulation Functions
21  *  \brief This section describes the functions you need to know to
22  *  set up a simulation. You should have a look at \ref SIMIX_examples 
23  *  to have an overview of their usage.
24  *    \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Simulation functions" --> \endhtmlonly
25  */
26
27 /********************************* SIMIX **************************************/
28
29 /** \ingroup simix_simulation
30  * \brief Initialize some SIMIX internal data.
31  */
32 void SIMIX_global_init_args(int *argc, char **argv)
33 {
34   SIMIX_global_init(argc,argv);
35 }
36
37 /** \ingroup simix_simulation
38  * \brief Initialize some SIMIX internal data.
39  */
40 void SIMIX_global_init(int *argc, char **argv)
41 {
42         s_smx_process_t proc;
43
44         if (!simix_global) {
45                 surf_init(argc, argv);  /* Initialize some common structures. Warning, it sets simix_global=NULL */
46
47                 simix_global = xbt_new0(s_SIMIX_Global_t,1);
48
49                 xbt_context_init();
50                 simix_global->host = xbt_fifo_new();
51                 simix_global->process_to_run = xbt_swag_new(xbt_swag_offset(proc,synchro_hookup));
52                 simix_global->process_list = xbt_swag_new(xbt_swag_offset(proc,process_hookup));
53                 simix_global->current_process = NULL;
54                 simix_global->registered_functions = xbt_dict_new();
55         }
56 }
57
58 /* Debug purpose, incomplete */
59 void __SIMIX_display_process_status(void)
60 {
61    smx_process_t process = NULL;
62    xbt_fifo_item_t item = NULL;
63          smx_action_t act;
64    int nbprocess=xbt_swag_size(simix_global->process_list);
65    
66    INFO1("SIMIX: %d processes are still running, waiting for something.",
67          nbprocess);
68    /*  List the process and their state */
69    INFO0("SIMIX: <process> on <host>: <status>.");
70    xbt_swag_foreach(process, simix_global->process_list) {
71       simdata_process_t p_simdata = (simdata_process_t) process->simdata;
72      // simdata_host_t h_simdata=(simdata_host_t)p_simdata->host->simdata;
73       char *who;
74         
75       asprintf(&who,"SIMIX:  %s on %s: %s",
76                process->name,
77                                 p_simdata->host->name,
78                (process->simdata->blocked)?"[BLOCKED] "
79                :((process->simdata->suspended)?"[SUSPENDED] ":""));
80                         if (p_simdata->mutex) {
81                                 DEBUG1("Block on a mutex: %s", who);                    
82                         }
83                         else if (p_simdata->cond) {
84                                 DEBUG1("Block on a condition: %s", who);
85                                 DEBUG0("Waiting actions:");
86                                 xbt_fifo_foreach(p_simdata->cond->actions,item, act, smx_action_t) {
87                                         DEBUG1("\t %s", act->name);
88                                 }
89                         }
90                         else DEBUG1("Unknown block status: %s", who);
91       free(who);
92    }
93 }
94
95 /* FIXME: Yeah, I'll do it in a portable maner one day [Mt] */
96 #include <signal.h>
97
98 static void _XBT_CALL inthandler(int ignored)
99 {
100    INFO0("CTRL-C pressed. Displaying status and bailing out");
101    __SIMIX_display_process_status();
102    exit(1);
103 }
104
105 /** \ingroup msg_simulation
106  * \brief Launch the SIMIX simulation
107  */
108 void __SIMIX_main(void)
109 {
110         smx_process_t process = NULL;
111         smx_cond_t cond = NULL;
112         smx_action_t smx_action;
113         xbt_fifo_t actions_done = xbt_fifo_new();
114         xbt_fifo_t actions_failed = xbt_fifo_new();
115
116         /* Prepare to display some more info when dying on Ctrl-C pressing */
117         signal(SIGINT,inthandler);
118
119         /* Clean IO before the run */
120         fflush(stdout);
121         fflush(stderr);
122
123         //surf_solve(); /* Takes traces into account. Returns 0.0 */
124         /* xbt_fifo_size(msg_global->process_to_run) */
125
126         while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
127
128                 while ( (smx_action = xbt_fifo_pop(actions_failed)) ) {
129
130                         xbt_fifo_item_t _cursor;
131
132                         DEBUG1("** %s failed **",smx_action->name);
133                         xbt_fifo_foreach(smx_action->simdata->cond_list,_cursor,cond,smx_cond_t) {
134                                 xbt_swag_foreach(process,cond->sleeping) {
135                                         DEBUG2("\t preparing to wake up %s on %s",           
136                                                         process->name,  process->simdata->host->name);
137                                 }
138                                 SIMIX_cond_broadcast(cond);
139                                 /* remove conditional from action */
140                                 xbt_fifo_remove(smx_action->simdata->cond_list,cond);
141                         }
142                 }
143
144                 while ( (smx_action = xbt_fifo_pop(actions_done)) ) {
145                         xbt_fifo_item_t _cursor;
146
147                         DEBUG1("** %s done **",smx_action->name);
148                         xbt_fifo_foreach(smx_action->simdata->cond_list,_cursor,cond,smx_cond_t) {
149                                 xbt_swag_foreach(process,cond->sleeping) {
150                                         DEBUG2("\t preparing to wake up %s on %s",           
151                                                         process->name,  process->simdata->host->name);
152                                 }
153                                 SIMIX_cond_broadcast(cond);
154                                 /* remove conditional from action */
155                                 xbt_fifo_remove(smx_action->simdata->cond_list,cond);
156                         }
157                 }
158         }
159         return;
160 }
161
162 /** \ingroup msg_simulation
163  * \brief Kill all running process
164
165  * \param reset_PIDs should we reset the PID numbers. A negative
166  *   number means no reset and a positive number will be used to set the PID
167  *   of the next newly created process.
168  */
169 void SIMIX_process_killall()
170 {
171   smx_process_t p = NULL;
172   smx_process_t self = SIMIX_process_self();
173
174   while((p=xbt_swag_extract(simix_global->process_list))) {
175     if(p!=self) SIMIX_process_kill(p);
176   }
177
178   xbt_context_empty_trash();
179
180   if(self) {
181     xbt_context_yield();
182   }
183
184   return;
185 }
186
187 /** \ingroup msg_simulation
188  * \brief Clean the SIMIX simulation
189  */
190 void SIMIX_clean(void)
191 {
192   xbt_fifo_item_t i = NULL;
193   smx_host_t h = NULL;
194   smx_process_t p = NULL;
195
196
197   while((p=xbt_swag_extract(simix_global->process_list))) {
198     SIMIX_process_kill(p);
199   }
200   xbt_context_exit();
201
202   xbt_fifo_foreach(simix_global->host,i,h,smx_host_t) {
203     __SIMIX_host_destroy(h);
204   }
205   xbt_fifo_free(simix_global->host);
206   xbt_swag_free(simix_global->process_to_run);
207   xbt_swag_free(simix_global->process_list);
208   xbt_dict_free(&(simix_global->registered_functions));
209   simix_config_finalize();
210   free(simix_global);
211   surf_exit();
212
213   return ;
214 }
215
216
217 /** \ingroup msg_easier_life
218  * \brief A clock (in second).
219  */
220 double SIMIX_get_clock(void)
221 {
222   return surf_get_clock();
223 }
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         int state_modifications = 0;
232
233
234         //surf_solve(); /* Takes traces into account. Returns 0.0  I don't know what to do with this*/
235
236         xbt_context_empty_trash();
237         if(xbt_swag_size(simix_global->process_to_run) && (elapsed_time>0)) {
238                 DEBUG0("**************************************************");
239         }
240
241         while ((process = xbt_swag_extract(simix_global->process_to_run))) {
242                 DEBUG2("Scheduling %s on %s",        
243                                 process->name,
244                                 process->simdata->host->name);
245                 simix_global->current_process = process;
246                 xbt_context_schedule(process->simdata->context);
247                 /*       fflush(NULL); */
248                 simix_global->current_process = NULL;
249         }
250
251         {
252                 surf_action_t action = NULL;
253                 surf_resource_t resource = NULL;
254                 smx_action_t smx_action = NULL;
255
256                 void *fun = NULL;
257                 void *arg = NULL;
258
259                 xbt_dynar_foreach(resource_list, i, resource) {
260                         if(xbt_swag_size(resource->common_public->states.failed_action_set) ||
261                                         xbt_swag_size(resource->common_public->states.done_action_set))
262                                 state_modifications = 1;
263                 }
264
265                 if(!state_modifications) {
266                         DEBUG1("%f : Calling surf_solve",SIMIX_get_clock());
267                         elapsed_time = surf_solve();
268                         DEBUG1("Elapsed_time %f",elapsed_time);
269                 }
270
271                 while (surf_timer_resource->extension_public->get(&fun,(void*)&arg)) {
272                         DEBUG2("got %p %p", fun, arg);
273                         if(fun==SIMIX_process_create_with_arguments) {
274                                 process_arg_t args = arg;
275                                 DEBUG2("Launching %s on %s", args->name, args->host->name);
276                                 process = SIMIX_process_create_with_arguments(args->name, args->code, 
277                                                 args->data, args->host,
278                                                 args->argc,args->argv);
279                                 if(args->kill_time > SIMIX_get_clock()) {
280                                         surf_timer_resource->extension_public->set(args->kill_time, 
281                                                         (void*) &SIMIX_process_kill,
282                                                         (void*) process);
283                                 }
284                                 xbt_free(args);
285                         }
286                         if(fun==SIMIX_process_kill) {
287                                 process = arg;
288                                 DEBUG2("Killing %s on %s", process->name, 
289                                                 process->simdata->host->name);
290                                 SIMIX_process_kill(process);
291                         }
292                 }
293
294                 /* Wake up all process waiting for the action finish */
295                 xbt_dynar_foreach(resource_list, i, resource) {
296                         while ((action = xbt_swag_extract(resource->common_public->states.failed_action_set))) {
297                                 smx_action = action->data;
298                                 if (smx_action) {
299                                         xbt_fifo_unshift(actions_failed,smx_action);
300                                 }
301                         }
302                         while ((action =xbt_swag_extract(resource->common_public->states.done_action_set))) {
303                                 smx_action = action->data;
304                                 if (smx_action) {
305                                         xbt_fifo_unshift(actions_done,smx_action);
306                                 }
307                         }
308                 }
309         }
310
311         if (elapsed_time == -1) {
312                 if (xbt_swag_size(simix_global->process_list) == 0) {
313                         INFO0("Congratulations ! Simulation terminated : all processes are over");
314                 } else {
315                         INFO0("Oops ! Deadlock or code not perfectly clean.");
316                         __SIMIX_display_process_status();
317                         if(XBT_LOG_ISENABLED(simix, xbt_log_priority_debug) ||
318                                         XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_debug)) {
319                                 DEBUG0("Aborting!");
320                                 xbt_abort();
321                         }
322                         INFO0("Return a Warning.");
323                 }
324         }
325         return elapsed_time;
326 }
327
328
329 void SIMIX_timer_set (double date, void *function, void *arg)
330 {
331         surf_timer_resource->extension_public->set(date, function, arg);
332 }
333
334 int SIMIX_timer_get(void **function, void **arg)
335 {
336         return surf_timer_resource->extension_public->get(function, arg);
337 }