Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c35edaf7c38b525b0e8ca96cd8447385016bd8f1
[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 int __stop_time = -1.0 ;
17
18 SIMIX_Global_t simix_global = NULL;
19
20
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
26  */
27
28 /********************************* SIMIX **************************************/
29
30 /** \ingroup simix_simulation
31  * \brief Initialize some SIMIX internal data.
32  */
33 void SIMIX_global_init_args(int *argc, char **argv)
34 {
35   SIMIX_global_init(argc,argv);
36 }
37
38 /** \ingroup simix_simulation
39  * \brief Initialize some SIMIX internal data.
40  */
41 void SIMIX_global_init(int *argc, char **argv)
42 {
43         s_smx_process_t proc;
44
45         if (!simix_global) {
46                 surf_init(argc, argv);  /* Initialize some common structures. Warning, it sets msg_global=NULL */
47
48                 simix_global = xbt_new0(s_SIMIX_Global_t,1);
49
50                 xbt_context_init();
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();
56         }
57 }
58
59 /* Debug purpose, incomplete */
60 void __SIMIX_display_process_status(void)
61 {
62    smx_process_t process = NULL;
63    //xbt_fifo_item_t item = NULL;
64    //int i;
65    int nbprocess=xbt_swag_size(simix_global->process_list);
66    
67    INFO1("SIMIX: %d processes are still running, waiting for something.",
68          nbprocess);
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;
74       char *who;
75         
76       asprintf(&who,"SIMIX:  %s on %s: %s",
77                process->name,
78                                 p_simdata->host->name,
79                (process->simdata->blocked)?"[blocked] "
80                :((process->simdata->suspended)?"[suspended] ":""));
81       
82       free(who);
83    }
84 }
85
86 /* FIXME: Yeah, I'll do it in a portable maner one day [Mt] */
87 #include <signal.h>
88
89 static void _XBT_CALL inthandler(int ignored)
90 {
91    INFO0("CTRL-C pressed. Displaying status and bailing out");
92    __SIMIX_display_process_status();
93    exit(1);
94 }
95
96 /** \ingroup msg_simulation
97  * \brief Launch the SIMIX simulation
98  */
99 void SIMIX_main(void)
100 {
101         smx_process_t process = NULL;
102         smx_cond_t cond = NULL;
103         int i;
104         double elapsed_time = 0.0;
105         int state_modifications = 1;
106
107         /* Prepare to display some more info when dying on Ctrl-C pressing */
108         signal(SIGINT,inthandler);
109
110         /* Clean IO before the run */
111         fflush(stdout);
112         fflush(stderr);
113
114         surf_solve(); /* Takes traces into account. Returns 0.0 */
115         /* xbt_fifo_size(msg_global->process_to_run) */
116         while (1) {
117                 xbt_context_empty_trash();
118                 if(xbt_swag_size(simix_global->process_to_run) && (elapsed_time>0)) {
119                         DEBUG0("**************************************************");
120                 }
121
122                 if((__stop_time>0) && (SIMIX_get_clock() >= __stop_time)) {
123                         DEBUG0("Let's stop here!");
124                 }
125
126                 while ((process = xbt_swag_extract(simix_global->process_to_run))) {
127                         DEBUG2("Scheduling %s on %s",        
128                                         process->name,
129                                         process->simdata->host->name);
130                         simix_global->current_process = process;
131                         xbt_context_schedule(process->simdata->context);
132                         /*       fflush(NULL); */
133                         simix_global->current_process = NULL;
134                 }
135
136                 {
137                         surf_action_t action = NULL;
138                         surf_resource_t resource = NULL;
139                         smx_action_t smx_action = NULL;
140
141                         void *fun = NULL;
142                         void *arg = NULL;
143
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;
148                         }
149
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);
154
155                                 if (elapsed_time<0.0) {
156                                         /*       fprintf(stderr, "We're done %g\n",elapsed_time); */
157                                         break;
158                                 }
159                         }
160
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,
172                                                                 (void*) process);
173                                         }
174                                         xbt_free(args);
175                                 }
176                                 if(fun==SIMIX_process_kill) {
177                                         process = arg;
178                                         DEBUG2("Killing %s on %s", process->name, 
179                                                         process->simdata->host->name);
180                                         SIMIX_process_kill(process);
181                                 }
182                         }
183
184                         /* Wake up all process waiting for the action finish */
185                         xbt_dynar_foreach(resource_list, i, resource) {
186                                 while ((action =
187                                                         xbt_swag_extract(resource->common_public->states.
188                                                                 failed_action_set))) {
189                                         smx_action = action->data;
190                                         if (smx_action) {
191                                                 xbt_fifo_item_t _cursor;
192
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);
197                                                 }
198                                                 /* else, do the dummy action finish, call the signal broadcast */
199                                                 else {
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);
204                                                                 }
205                                                                 SIMIX_cond_broadcast(cond);
206                                                                 /* remove conditional from action */
207                                                                 xbt_fifo_remove(smx_action->simdata->cond_list,cond);
208                                                         }
209                                                 }
210                                                 process=NULL;                                           
211                                         }
212                                 }
213                                 while ((action =
214                                                         xbt_swag_extract(resource->common_public->states.
215                                                                 done_action_set))) {
216                                         smx_action = action->data;
217                                         if (smx_action) {
218                                                 xbt_fifo_item_t _cursor;
219
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);
225                                                         }
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);
230                                                 }
231                                                 /* else, do the dummy action finish, call the signal broadcast */
232                                                 else {
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);
237                                                                 }
238                                                                 SIMIX_cond_broadcast(cond);
239                                                                 /* remove conditional from action */
240                                                                 xbt_fifo_remove(smx_action->simdata->cond_list,cond);
241                                                         }
242                                                 }
243                                                 process=NULL;                                           
244                                         }
245 /*
246                                         if (smx_action) {
247                                                 xbt_fifo_item_t _cursor;
248
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);
255                                                         }
256                                                 }
257                                                 process=NULL;                                           
258                                         }*/
259
260                                 }
261                         }
262                 }
263                 state_modifications = 0;
264         }
265
266         if (xbt_swag_size(simix_global->process_list) == 0) {
267                 INFO0("Congratulations ! Simulation terminated : all processes are over");
268                 return;
269         } else {
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)) {
274                         DEBUG0("Aborting!");
275                         xbt_abort();
276                 }
277
278                 INFO0("Return a Warning.");
279                 return;
280         }
281 }
282
283 /** \ingroup msg_simulation
284  * \brief Kill all running process
285
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.
289  */
290 void SIMIX_process_killall()
291 {
292   smx_process_t p = NULL;
293   smx_process_t self = SIMIX_process_self();
294
295   while((p=xbt_swag_extract(simix_global->process_list))) {
296     if(p!=self) SIMIX_process_kill(p);
297   }
298
299   xbt_context_empty_trash();
300
301   if(self) {
302     xbt_context_yield();
303   }
304
305   return;
306 }
307
308 /** \ingroup msg_simulation
309  * \brief Clean the SIMIX simulation
310  */
311 void SIMIX_clean(void)
312 {
313   xbt_fifo_item_t i = NULL;
314   smx_host_t h = NULL;
315   smx_process_t p = NULL;
316
317
318   while((p=xbt_swag_extract(simix_global->process_list))) {
319     SIMIX_process_kill(p);
320   }
321   xbt_context_exit();
322
323   xbt_fifo_foreach(simix_global->host,i,h,smx_host_t) {
324     __SIMIX_host_destroy(h);
325   }
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();
331   free(simix_global);
332   surf_exit();
333
334   return ;
335 }
336
337
338 /** \ingroup msg_easier_life
339  * \brief A clock (in second).
340  */
341 double SIMIX_get_clock(void) {
342   return surf_get_clock();
343 }