Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Copyright corrected.
[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,process_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 SIMIX_error_t 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                         /*       fflush(NULL); */
132                         xbt_context_schedule(process->simdata->context);
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                                                 xbt_fifo_foreach(smx_action->simdata->cond_list,_cursor,cond,smx_cond_t) {
195                                                         SIMIX_cond_broadcast(cond);
196                                                         xbt_swag_foreach(process,cond->sleeping) {
197                                                                 DEBUG2("\t preparing to wake up %s on %s",           
198                                                                                 process->name,  process->simdata->host->name);
199                                                         }
200                                                 }
201                                                 process=NULL;                                           
202                                         }
203                                 }
204                                 while ((action =
205                                                         xbt_swag_extract(resource->common_public->states.
206                                                                 done_action_set))) {
207                                         smx_action = action->data;
208                                         if (smx_action) {
209                                                 xbt_fifo_item_t _cursor;
210
211                                                 DEBUG1("** %s done **",smx_action->name);
212                                                 xbt_fifo_foreach(smx_action->simdata->cond_list,_cursor,cond,smx_cond_t) {
213                                                         SIMIX_cond_broadcast(cond);
214                                                         xbt_swag_foreach(process,cond->sleeping) {
215                                                                 DEBUG2("\t preparing to wake up %s on %s",           
216                                                                                 process->name,  process->simdata->host->name);
217                                                         }
218                                                 }
219                                                 process=NULL;                                           
220                                         }
221
222                                 }
223                         }
224                 }
225                 state_modifications = 0;
226         }
227
228         if (xbt_swag_size(simix_global->process_list) == 0) {
229                 INFO0("Congratulations ! Simulation terminated : all processes are over");
230                 return SIMIX_OK;
231         } else {
232                 INFO0("Oops ! Deadlock or code not perfectly clean.");
233                 __SIMIX_display_process_status();
234                 if(XBT_LOG_ISENABLED(simix, xbt_log_priority_debug) ||
235                                 XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_debug)) {
236                         DEBUG0("Aborting!");
237                         xbt_abort();
238                 }
239
240                 INFO0("Return a Warning.");
241                 return SIMIX_WARNING;
242         }
243 }
244
245 /** \ingroup msg_simulation
246  * \brief Kill all running process
247
248  * \param reset_PIDs should we reset the PID numbers. A negative
249  *   number means no reset and a positive number will be used to set the PID
250  *   of the next newly created process.
251  */
252 void SIMIX_process_killall()
253 {
254   smx_process_t p = NULL;
255   smx_process_t self = SIMIX_process_self();
256
257   while((p=xbt_swag_extract(simix_global->process_list))) {
258     if(p!=self) SIMIX_process_kill(p);
259   }
260
261   xbt_context_empty_trash();
262
263   if(self) {
264     xbt_context_yield();
265   }
266
267   return;
268 }
269
270 /** \ingroup msg_simulation
271  * \brief Clean the SIMIX simulation
272  */
273 SIMIX_error_t SIMIX_clean(void)
274 {
275   xbt_fifo_item_t i = NULL;
276   smx_host_t h = NULL;
277   smx_process_t p = NULL;
278
279
280   while((p=xbt_swag_extract(simix_global->process_list))) {
281     SIMIX_process_kill(p);
282   }
283   xbt_context_exit();
284
285   xbt_fifo_foreach(simix_global->host,i,h,smx_host_t) {
286     __SIMIX_host_destroy(h);
287   }
288   xbt_fifo_free(simix_global->host);
289   xbt_swag_free(simix_global->process_to_run);
290   xbt_swag_free(simix_global->process_list);
291   xbt_dict_free(&(simix_global->registered_functions));
292
293   simix_config_finalize();
294   free(simix_global);
295   surf_exit();
296
297   return SIMIX_OK;
298 }
299
300
301 /** \ingroup msg_easier_life
302  * \brief A clock (in second).
303  */
304 double SIMIX_get_clock(void) {
305   return surf_get_clock();
306 }