Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
27437956b37da15c857bfc287898548901b77231
[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
20 /** 
21  * \brief Initialize some SIMIX internal data.
22  *
23  * \param argc Argc
24  * \param argv Argv
25  */
26 void SIMIX_global_init_args(int *argc, char **argv)
27 {
28   SIMIX_global_init(argc,argv);
29 }
30
31 /**
32  * \brief Initialize some SIMIX internal data.
33  *
34  * \param argc Argc
35  * \param argv Argv
36  */
37 void SIMIX_global_init(int *argc, char **argv)
38 {
39         s_smx_process_t proc;
40
41         if (!simix_global) {
42                 surf_init(argc, argv);  /* Initialize some common structures. Warning, it sets simix_global=NULL */
43
44                 simix_global = xbt_new0(s_SIMIX_Global_t,1);
45
46                 simix_global->host = xbt_fifo_new();
47                 simix_global->process_to_run = xbt_swag_new(xbt_swag_offset(proc,synchro_hookup));
48                 simix_global->process_list = xbt_swag_new(xbt_swag_offset(proc,process_hookup));
49                 simix_global->current_process = NULL;
50                 simix_global->registered_functions = xbt_dict_new();
51
52                 simix_global->create_process_function = NULL;
53                 simix_global->kill_process_function = NULL;
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 /**
105  * \brief Launch the SIMIX simulation, debug purpose
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 /**
162  * \brief Kill all running process
163  *
164  */
165 void SIMIX_process_killall()
166 {
167   smx_process_t p = NULL;
168   smx_process_t self = SIMIX_process_self();
169
170   while((p=xbt_swag_extract(simix_global->process_list))) {
171     if(p!=self) SIMIX_process_kill(p);
172   }
173
174   xbt_context_empty_trash();
175
176   if(self) {
177     xbt_context_yield();
178   }
179
180   return;
181 }
182
183 /** 
184  * \brief Clean the SIMIX simulation
185  *
186  * This functions remove all memories needed to the SIMIX execution
187  */
188 void SIMIX_clean(void)
189 {
190   xbt_fifo_item_t i = NULL;
191   smx_host_t h = NULL;
192   smx_process_t p = NULL;
193
194
195   while((p=xbt_swag_extract(simix_global->process_list))) {
196     SIMIX_process_kill(p);
197   }
198
199   xbt_fifo_foreach(simix_global->host,i,h,smx_host_t) {
200     __SIMIX_host_destroy(h);
201   }
202   xbt_fifo_free(simix_global->host);
203   xbt_swag_free(simix_global->process_to_run);
204   xbt_swag_free(simix_global->process_list);
205   xbt_dict_free(&(simix_global->registered_functions));
206   simix_config_finalize();
207   free(simix_global);
208   surf_exit();
209
210   return ;
211 }
212
213
214 /**
215  * \brief A clock (in second).
216  *
217  * \return Return the clock.
218  */
219 double SIMIX_get_clock(void)
220 {
221   return surf_get_clock();
222 }
223
224 /**
225  *      \brief Does a turn of the simulation
226  *
227  *      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.         
228  *      \param actions_done List of actions done
229  *      \param actions_failed List of actions failed
230  *      \return The time spent to execute the simulation or -1 if the simulation ended
231  */
232 double SIMIX_solve(xbt_fifo_t actions_done, xbt_fifo_t actions_failed) 
233 {
234
235         smx_process_t process = NULL;
236         int i;
237         double elapsed_time = 0.0;
238         static int state_modifications = 1;
239         static int first = 1;
240
241         xbt_context_empty_trash();
242         if(xbt_swag_size(simix_global->process_to_run) && (elapsed_time>0)) {
243                 DEBUG0("**************************************************");
244         }
245         if (first) {
246                 surf_solve();/* Takes traces into account. Returns 0.0 */
247                 first=0;
248         }
249         while ((process = xbt_swag_extract(simix_global->process_to_run))) {
250                 DEBUG2("Scheduling %s on %s",        
251                                 process->name,
252                                 process->simdata->host->name);
253                 simix_global->current_process = process;
254                 xbt_context_schedule(process->simdata->context);
255                 /*       fflush(NULL); */
256                 simix_global->current_process = NULL;
257         }
258
259         {
260                 surf_action_t action = NULL;
261                 surf_resource_t resource = NULL;
262                 smx_action_t smx_action = NULL;
263
264                 void *fun = NULL;
265                 void *arg = NULL;
266
267                 xbt_dynar_foreach(resource_list, i, resource) {
268                         if(xbt_swag_size(resource->common_public->states.failed_action_set) ||
269                                         xbt_swag_size(resource->common_public->states.done_action_set)) {
270                                 state_modifications = 1;
271                                 }
272                 }
273
274                 if(!state_modifications) {
275                         DEBUG1("%f : Calling surf_solve",SIMIX_get_clock());
276                         elapsed_time = surf_solve();
277                         DEBUG1("Elapsed_time %f",elapsed_time);
278                 }
279
280                 while (surf_timer_resource->extension_public->get(&fun,(void*)&arg)) {
281                         DEBUG2("got %p %p", fun, arg);
282                         if(fun==SIMIX_process_create_with_arguments) {
283                                 process_arg_t args = arg;
284                                 DEBUG2("Launching %s on %s", args->name, args->hostname);
285                                 process = SIMIX_process_create_with_arguments(args->name, args->code, 
286                                                 args->data, args->hostname,
287                                                 args->argc,args->argv,NULL);
288                                 if(args->kill_time > SIMIX_get_clock()) {
289                                         surf_timer_resource->extension_public->set(args->kill_time, 
290                                                         (void*) &SIMIX_process_kill,
291                                                         (void*) process);
292                                 }
293                                 xbt_free(args);
294                         }
295                         if(fun==SIMIX_process_kill) {
296                                 process = arg;
297                                 DEBUG2("Killing %s on %s", process->name, 
298                                                 process->simdata->host->name);
299                                 SIMIX_process_kill(process);
300                         }
301                 }
302
303                 /* Wake up all process waiting for the action finish */
304                 xbt_dynar_foreach(resource_list, i, resource) {
305                         while ((action = xbt_swag_extract(resource->common_public->states.failed_action_set))) {
306                                 smx_action = action->data;
307                                 if (smx_action) {
308                                         xbt_fifo_unshift(actions_failed,smx_action);
309                                 }
310                         }
311                         while ((action =xbt_swag_extract(resource->common_public->states.done_action_set))) {
312                                 smx_action = action->data;
313                                 if (smx_action) {
314                                         xbt_fifo_unshift(actions_done,smx_action);
315                                 }
316                         }
317                 }
318         }
319         state_modifications = 0;
320
321         if (elapsed_time == -1) {
322                 if (xbt_swag_size(simix_global->process_list) == 0) {
323                         INFO0("Congratulations ! Simulation terminated : all processes are over");
324                 } else {
325                         INFO0("Oops ! Deadlock or code not perfectly clean.");
326                         __SIMIX_display_process_status();
327                         if(XBT_LOG_ISENABLED(simix, xbt_log_priority_debug) ||
328                                         XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_debug)) {
329                                 DEBUG0("Aborting!");
330                                 xbt_abort();
331                         }
332                         INFO0("Return a Warning.");
333                 }
334         }
335         return elapsed_time;
336 }
337
338 /**
339  *      \brief Set the date to execute a function
340  *
341  * Set the date to execute the function on the surf.
342  *      \param date Date to execute function
343  *      \param function Function to be executed
344  *      \param arg Parameters of the function
345  *
346  */
347 void SIMIX_timer_set (double date, void *function, void *arg)
348 {
349         surf_timer_resource->extension_public->set(date, function, arg);
350 }
351
352 int SIMIX_timer_get(void **function, void **arg)
353 {
354         return surf_timer_resource->extension_public->get(function, arg);
355 }
356
357 /**
358  *      \brief Registers a function to create a process.
359  *
360  *      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.
361  *      \param function Create process function
362  *
363  */
364 void SIMIX_function_register_process_create(void * function)
365 {
366   xbt_assert0((simix_global->create_process_function == NULL), "Data already set");
367
368   /* Assign create process */
369   simix_global->create_process_function = function;
370
371   return ;
372 }
373
374 /**
375  *      \brief Registers a function to kill a process.
376  *
377  *      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.
378  *      \param function Kill process function
379  *
380  */
381 void SIMIX_function_register_process_kill(void * function)
382 {
383   xbt_assert0((simix_global->kill_process_function == NULL), "Data already set");
384
385   /* Assign kill process */
386   simix_global->kill_process_function = function;
387
388   return ;
389 }