Logo AND Algorithmique Numérique Distribuée

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