Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Keeping up with the new surf model description.
[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 SIMIX_Global_t simix_global = NULL;
17
18 /********************************* SIMIX **************************************/
19 static void __simix_config_helper(const char *name, ...) {
20   va_list pa;
21   va_start(pa,name);
22   
23   SIMIX_config(name,pa);
24   
25   va_end(pa);
26 }
27
28 static void simix_cfg_control_set(const char* control_string) {
29   /* To split the string in commands, and the cursors */
30   xbt_dynar_t set_strings;
31   char *str;
32   int cpt;
33
34   if (!control_string)
35     return;
36   DEBUG1("Parse log settings '%s'",control_string);
37
38   /* split the string, and remove empty entries */
39   set_strings=xbt_str_split_quoted(control_string);
40
41   if (xbt_dynar_length(set_strings) == 0) { /* vicious user! */
42     xbt_dynar_free(&set_strings);
43     return; 
44   }
45   /* Parse each entry and either use it right now (if the category was already
46      created), or store it for further use */
47   xbt_dynar_foreach(set_strings,cpt,str) {
48     char *control_string,*control_string_sav,*name,*value;
49
50
51     control_string = control_string_sav = strdup(str);
52     control_string += strspn(control_string, " ");
53     name = control_string;
54     control_string += strcspn(str, ":=");
55     value = control_string;
56     *value=0;
57     value++;
58
59     xbt_assert1(strlen(name)!=0, "Invalid name for configuration: '%s'",name);
60     xbt_assert1(strlen(value)!=0, "Invalid value for configuration: '%s'",value);
61     INFO2("setting '%s' to '%s'",name,value);
62
63     __simix_config_helper(name,value);
64
65     free(control_string_sav);
66   }
67   xbt_dynar_free(&set_strings);
68 }
69
70 static void simix_cfg_init(int *argc,char **argv) {
71   int i,j;
72   char *opt;
73
74   for (i=1; i<*argc; i++){
75     if (!strncmp(argv[i],"--cfg=",strlen("--cfg="))) {
76       opt=strchr(argv[i],'=');
77       opt++;
78       
79       simix_cfg_control_set(opt);
80       DEBUG1("Did apply '%s' as config setting",opt);
81       /*remove this from argv*/
82       
83       for (j=i+1; j<*argc; j++){
84         argv[j-1] = argv[j];
85       } 
86       
87       argv[j-1] = NULL;
88       (*argc)--;
89       i--; /* compensate effect of next loop incrementation */
90     }
91   }
92 }
93
94 /**
95  * \brief Initialize some SIMIX internal data.
96  *
97  * \param argc Argc
98  * \param argv Argv
99  */
100 void SIMIX_global_init(int *argc, char **argv)
101 {
102         s_smx_process_t proc;
103
104         if (!simix_global) {
105                 surf_init(argc, argv);  /* Initialize some common structures. Warning, it sets simix_global=NULL */
106                 simix_cfg_init(argc,argv);
107
108                 simix_global = xbt_new0(s_SIMIX_Global_t,1);
109
110                 simix_global->host = xbt_fifo_new();
111                 simix_global->process_to_run = xbt_swag_new(xbt_swag_offset(proc,synchro_hookup));
112                 simix_global->process_list = xbt_swag_new(xbt_swag_offset(proc,process_hookup));
113                 simix_global->current_process = NULL;
114                 simix_global->registered_functions = xbt_dict_new();
115
116                 simix_global->create_process_function = NULL;
117                 simix_global->kill_process_function = NULL;
118                 simix_global->cleanup_process_function = SIMIX_process_cleanup;         
119         }
120 }
121
122 /* Debug purpose, incomplete */
123 void SIMIX_display_process_status(void)
124 {
125    smx_process_t process = NULL;
126    xbt_fifo_item_t item = NULL;
127          smx_action_t act;
128    int nbprocess=xbt_swag_size(simix_global->process_list);
129    
130    INFO1("%d processes are still running, waiting for something.",
131          nbprocess);
132    /*  List the process and their state */
133    INFO0("Legend of the following listing: \"<process> on <host>: <status>.\"");
134    xbt_swag_foreach(process, simix_global->process_list) {
135       smx_simdata_process_t p_simdata = (smx_simdata_process_t) process->simdata;
136      // simdata_host_t h_simdata=(simdata_host_t)p_simdata->host->simdata;
137       char *who, *who2;
138         
139       asprintf(&who,"%s on %s: %s",
140                process->name,
141                p_simdata->s_host->name,
142                (process->simdata->blocked)?"[BLOCKED] "
143                :((process->simdata->suspended)?"[SUSPENDED] ":""));
144       
145       if (p_simdata->mutex) {
146          who2=bprintf("%s Blocked on mutex %p",who,p_simdata->mutex);
147          free(who); who=who2;
148       } else if (p_simdata->cond) {
149          who2=bprintf("%s Blocked on condition %p; Waiting for the following actions:",who,p_simdata->cond);
150          free(who); who=who2;
151          xbt_fifo_foreach(p_simdata->cond->actions,item, act, smx_action_t) {
152             who2=bprintf("%s '%s'",who,act->name);
153             free(who); who=who2;
154          }
155       } else {
156          who2=bprintf("%s Blocked in an unknown status (please report this bug)",who);
157          free(who); who=who2;
158       }
159       INFO1("%s.",who);
160       free(who);
161    }
162 }
163
164 /* FIXME: Yeah, I'll do it in a portable maner one day [Mt] */
165 #include <signal.h>
166
167 static void _XBT_CALL inthandler(int ignored)
168 {
169    INFO0("CTRL-C pressed. Displaying status and bailing out");
170    SIMIX_display_process_status();
171    exit(1);
172 }
173
174 /**
175  * \brief Launch the SIMIX simulation, debug purpose
176  */
177 void __SIMIX_main(void)
178 {
179         smx_process_t process = NULL;
180         smx_cond_t cond = NULL;
181         smx_action_t smx_action;
182         xbt_fifo_t actions_done = xbt_fifo_new();
183         xbt_fifo_t actions_failed = xbt_fifo_new();
184
185         /* Prepare to display some more info when dying on Ctrl-C pressing */
186         signal(SIGINT,inthandler);
187
188         /* Clean IO before the run */
189         fflush(stdout);
190         fflush(stderr);
191
192         //surf_solve(); /* Takes traces into account. Returns 0.0 */
193         /* xbt_fifo_size(msg_global->process_to_run) */
194
195         while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
196
197                 while ( (smx_action = xbt_fifo_pop(actions_failed)) ) {
198
199                         xbt_fifo_item_t _cursor;
200
201                         DEBUG1("** %s failed **",smx_action->name);
202                         xbt_fifo_foreach(smx_action->cond_list,_cursor,cond,smx_cond_t) {
203                                 xbt_swag_foreach(process,cond->sleeping) {
204                                         DEBUG2("\t preparing to wake up %s on %s",           
205                                                         process->name,  process->simdata->s_host->name);
206                                 }
207                                 SIMIX_cond_broadcast(cond);
208                                 /* remove conditional from action */
209                                 xbt_fifo_remove(smx_action->cond_list,cond);
210                         }
211                 }
212
213                 while ( (smx_action = xbt_fifo_pop(actions_done)) ) {
214                         xbt_fifo_item_t _cursor;
215
216                         DEBUG1("** %s done **",smx_action->name);
217                         xbt_fifo_foreach(smx_action->cond_list,_cursor,cond,smx_cond_t) {
218                                 xbt_swag_foreach(process,cond->sleeping) {
219                                         DEBUG2("\t preparing to wake up %s on %s",           
220                                                         process->name,  process->simdata->s_host->name);
221                                 }
222                                 SIMIX_cond_broadcast(cond);
223                                 /* remove conditional from action */
224                                 xbt_fifo_remove(smx_action->cond_list,cond);
225                         }
226                 }
227         }
228         return;
229 }
230
231 /**
232  * \brief Kill all running process
233  *
234  */
235 void SIMIX_process_killall()
236 {
237   smx_process_t p = NULL;
238   smx_process_t self = SIMIX_process_self();
239
240   while((p=xbt_swag_extract(simix_global->process_list))) {
241     if(p!=self) SIMIX_process_kill(p);
242   }
243
244   xbt_context_empty_trash();
245
246   if(self) {
247     xbt_context_yield();
248   }
249
250   return;
251 }
252
253 /** 
254  * \brief Clean the SIMIX simulation
255  *
256  * This functions remove all memories needed to the SIMIX execution
257  */
258 void SIMIX_clean(void)
259 {
260   xbt_fifo_item_t i = NULL;
261   smx_host_t h = NULL;
262   smx_process_t p = NULL;
263
264   while((p=xbt_swag_extract(simix_global->process_list))) {
265     SIMIX_process_kill(p);
266   }
267
268   xbt_fifo_foreach(simix_global->host,i,h,smx_host_t) {
269     __SIMIX_host_destroy(h);
270   }
271   xbt_fifo_free(simix_global->host);
272   xbt_swag_free(simix_global->process_to_run);
273   xbt_swag_free(simix_global->process_list);
274   xbt_dict_free(&(simix_global->registered_functions));
275   simix_config_finalize();
276   free(simix_global);
277   surf_exit();
278
279   return ;
280 }
281
282
283 /**
284  * \brief A clock (in second).
285  *
286  * \return Return the clock.
287  */
288 double SIMIX_get_clock(void)
289 {
290   return surf_get_clock();
291 }
292
293 /**
294  *      \brief Does a turn of the simulation
295  *
296  *      Executes a step in the surf simulation, adding to the two lists all the actions that finished on this turn. Schedules all processus in the process_to_run list.         
297  *      \param actions_done List of actions done
298  *      \param actions_failed List of actions failed
299  *      \return The time spent to execute the simulation or -1 if the simulation ended
300  */
301 double SIMIX_solve(xbt_fifo_t actions_done, xbt_fifo_t actions_failed) 
302 {
303
304         smx_process_t process = NULL;
305         int i;
306         double elapsed_time = 0.0;
307         static int state_modifications = 1;
308         static int first = 1;
309
310         xbt_context_empty_trash();
311         if(xbt_swag_size(simix_global->process_to_run) && (elapsed_time>0)) {
312                 DEBUG0("**************************************************");
313         }
314         if (first) {
315                 surf_solve();/* Takes traces into account. Returns 0.0 */
316                 first=0;
317         }
318         while ((process = xbt_swag_extract(simix_global->process_to_run))) {
319                 DEBUG2("Scheduling %s on %s",        
320                                 process->name,
321                                 process->simdata->s_host->name);
322                 simix_global->current_process = process;
323                 xbt_context_schedule(process->simdata->context);
324                 /*       fflush(NULL); */
325                 simix_global->current_process = NULL;
326         }
327
328         {
329                 surf_action_t action = NULL;
330                 surf_resource_t resource = NULL;
331                 smx_action_t smx_action = NULL;
332
333                 void *fun = NULL;
334                 void *arg = NULL;
335
336                 xbt_dynar_foreach(resource_list, i, resource) {
337                         if(xbt_swag_size(resource->common_public->states.failed_action_set) ||
338                                         xbt_swag_size(resource->common_public->states.done_action_set)) {
339                                 state_modifications = 1;
340                                 }
341                 }
342
343                 if(!state_modifications) {
344                         DEBUG1("%f : Calling surf_solve",SIMIX_get_clock());
345                         elapsed_time = surf_solve();
346                         DEBUG1("Elapsed_time %f",elapsed_time);
347                 }
348
349                 while (surf_timer_resource->extension_public->get(&fun,(void*)&arg)) {
350                         DEBUG2("got %p %p", fun, arg);
351                         if(fun==SIMIX_process_create) {
352                                 smx_process_arg_t args = arg;
353                                 DEBUG2("Launching %s on %s", args->name, args->hostname);
354                                 process = SIMIX_process_create(args->name, args->code, 
355                                                 args->data, args->hostname,
356                                                 args->argc,args->argv);
357                                 if(args->kill_time > SIMIX_get_clock()) {
358                                         surf_timer_resource->extension_public->set(args->kill_time, 
359                                                         (void*) &SIMIX_process_kill,
360                                                         (void*) process);
361                                 }
362                                 xbt_free(args);
363                         }
364                         if(fun==SIMIX_process_kill) {
365                                 process = arg;
366                                 DEBUG2("Killing %s on %s", process->name, 
367                                                 process->simdata->s_host->name);
368                                 SIMIX_process_kill(process);
369                         }
370                 }
371
372                 /* Wake up all process waiting for the action finish */
373                 xbt_dynar_foreach(resource_list, i, resource) {
374                         while ((action = xbt_swag_extract(resource->common_public->states.failed_action_set))) {
375                                 smx_action = action->data;
376                                 if (smx_action) {
377                                         xbt_fifo_unshift(actions_failed,smx_action);
378                                 }
379                         }
380                         while ((action =xbt_swag_extract(resource->common_public->states.done_action_set))) {
381                                 smx_action = action->data;
382                                 if (smx_action) {
383                                         xbt_fifo_unshift(actions_done,smx_action);
384                                 }
385                         }
386                 }
387         }
388         state_modifications = 0;
389
390         if (elapsed_time == -1) {
391                 if (xbt_swag_size(simix_global->process_list) == 0) {
392 /*                      INFO0("Congratulations ! Simulation terminated : all processes are over"); */
393                 } else {
394                         INFO0("Oops ! Deadlock or code not perfectly clean.");
395                         SIMIX_display_process_status();
396                         if(XBT_LOG_ISENABLED(simix, xbt_log_priority_debug) ||
397                                         XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_debug)) {
398                                 DEBUG0("Aborting!");
399                                 xbt_abort();
400                         }
401                         INFO0("Return a Warning.");
402                 }
403         }
404         return elapsed_time;
405 }
406
407 /**
408  *      \brief Set the date to execute a function
409  *
410  * Set the date to execute the function on the surf.
411  *      \param date Date to execute function
412  *      \param function Function to be executed
413  *      \param arg Parameters of the function
414  *
415  */
416 void SIMIX_timer_set (double date, void *function, void *arg)
417 {
418         surf_timer_resource->extension_public->set(date, function, arg);
419 }
420
421 int SIMIX_timer_get(void **function, void **arg)
422 {
423         return surf_timer_resource->extension_public->get(function, arg);
424 }
425
426 /**
427  *      \brief Registers a function to create a process.
428  *
429  *      This function registers an user function to be called when a new process is created. The user function have to call the SIMIX_create_process function.
430  *      \param function Create process function
431  *
432  */
433 void SIMIX_function_register_process_create(smx_creation_func_t* function)
434 {
435   xbt_assert0((simix_global->create_process_function == NULL), "Data already set");
436
437   simix_global->create_process_function = function;
438 }
439
440 /**
441  *      \brief Registers a function to kill a process.
442  *
443  *      This function registers an user function to be called when a new process is killed. The user function have to call the SIMIX_kill_process function.
444  *      \param function Kill process function
445  *
446  */
447 void SIMIX_function_register_process_kill(void_f_pvoid_t* function)
448 {
449   xbt_assert0((simix_global->kill_process_function == NULL), "Data already set");
450
451   simix_global->kill_process_function = function;
452 }
453
454 /**
455  *      \brief Registers a function to cleanup a process.
456  *
457  *      This function registers an user function to be called when a new process ends properly.
458  *      \param function cleanup process function
459  *
460  */
461 void SIMIX_function_register_process_cleanup(void_f_pvoid_t* function)
462 {
463   simix_global->cleanup_process_function = function;
464 }