Logo AND Algorithmique Numérique Distribuée

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