Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Removed SIMIX_global_init_args and SIMIX_process_create_with_arguments.
[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(int *argc, char **argv)
27 {
28         s_smx_process_t proc;
29
30         if (!simix_global) {
31                 surf_init(argc, argv);  /* Initialize some common structures. Warning, it sets simix_global=NULL */
32
33                 simix_global = xbt_new0(s_SIMIX_Global_t,1);
34
35                 simix_global->host = xbt_fifo_new();
36                 simix_global->process_to_run = xbt_swag_new(xbt_swag_offset(proc,synchro_hookup));
37                 simix_global->process_list = xbt_swag_new(xbt_swag_offset(proc,process_hookup));
38                 simix_global->current_process = NULL;
39                 simix_global->registered_functions = xbt_dict_new();
40
41                 simix_global->create_process_function = NULL;
42                 simix_global->kill_process_function = NULL;
43         }
44 }
45
46 /* Debug purpose, incomplete */
47 void __SIMIX_display_process_status(void)
48 {
49    smx_process_t process = NULL;
50    xbt_fifo_item_t item = NULL;
51          smx_action_t act;
52    int nbprocess=xbt_swag_size(simix_global->process_list);
53    
54    INFO1("SIMIX: %d processes are still running, waiting for something.",
55          nbprocess);
56    /*  List the process and their state */
57    INFO0("SIMIX: <process> on <host>: <status>.");
58    xbt_swag_foreach(process, simix_global->process_list) {
59       smx_simdata_process_t p_simdata = (smx_simdata_process_t) process->simdata;
60      // simdata_host_t h_simdata=(simdata_host_t)p_simdata->host->simdata;
61       char *who;
62         
63       asprintf(&who,"SIMIX:  %s on %s: %s",
64                process->name,
65                                 p_simdata->host->name,
66                (process->simdata->blocked)?"[BLOCKED] "
67                :((process->simdata->suspended)?"[SUSPENDED] ":""));
68                         if (p_simdata->mutex) {
69                                 DEBUG1("Block on a mutex: %s", who);                    
70                         }
71                         else if (p_simdata->cond) {
72                                 DEBUG1("Block on a condition: %s", who);
73                                 DEBUG0("Waiting actions:");
74                                 xbt_fifo_foreach(p_simdata->cond->actions,item, act, smx_action_t) {
75                                         DEBUG1("\t %s", act->name);
76                                 }
77                         }
78                         else DEBUG1("Unknown block status: %s", who);
79       free(who);
80    }
81 }
82
83 /* FIXME: Yeah, I'll do it in a portable maner one day [Mt] */
84 #include <signal.h>
85
86 static void _XBT_CALL inthandler(int ignored)
87 {
88    INFO0("CTRL-C pressed. Displaying status and bailing out");
89    __SIMIX_display_process_status();
90    exit(1);
91 }
92
93 /**
94  * \brief Launch the SIMIX simulation, debug purpose
95  */
96 void __SIMIX_main(void)
97 {
98         smx_process_t process = NULL;
99         smx_cond_t cond = NULL;
100         smx_action_t smx_action;
101         xbt_fifo_t actions_done = xbt_fifo_new();
102         xbt_fifo_t actions_failed = xbt_fifo_new();
103
104         /* Prepare to display some more info when dying on Ctrl-C pressing */
105         signal(SIGINT,inthandler);
106
107         /* Clean IO before the run */
108         fflush(stdout);
109         fflush(stderr);
110
111         //surf_solve(); /* Takes traces into account. Returns 0.0 */
112         /* xbt_fifo_size(msg_global->process_to_run) */
113
114         while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
115
116                 while ( (smx_action = xbt_fifo_pop(actions_failed)) ) {
117
118                         xbt_fifo_item_t _cursor;
119
120                         DEBUG1("** %s failed **",smx_action->name);
121                         xbt_fifo_foreach(smx_action->cond_list,_cursor,cond,smx_cond_t) {
122                                 xbt_swag_foreach(process,cond->sleeping) {
123                                         DEBUG2("\t preparing to wake up %s on %s",           
124                                                         process->name,  process->simdata->host->name);
125                                 }
126                                 SIMIX_cond_broadcast(cond);
127                                 /* remove conditional from action */
128                                 xbt_fifo_remove(smx_action->cond_list,cond);
129                         }
130                 }
131
132                 while ( (smx_action = xbt_fifo_pop(actions_done)) ) {
133                         xbt_fifo_item_t _cursor;
134
135                         DEBUG1("** %s done **",smx_action->name);
136                         xbt_fifo_foreach(smx_action->cond_list,_cursor,cond,smx_cond_t) {
137                                 xbt_swag_foreach(process,cond->sleeping) {
138                                         DEBUG2("\t preparing to wake up %s on %s",           
139                                                         process->name,  process->simdata->host->name);
140                                 }
141                                 SIMIX_cond_broadcast(cond);
142                                 /* remove conditional from action */
143                                 xbt_fifo_remove(smx_action->cond_list,cond);
144                         }
145                 }
146         }
147         return;
148 }
149
150 /**
151  * \brief Kill all running process
152  *
153  */
154 void SIMIX_process_killall()
155 {
156   smx_process_t p = NULL;
157   smx_process_t self = SIMIX_process_self();
158
159   while((p=xbt_swag_extract(simix_global->process_list))) {
160     if(p!=self) SIMIX_process_kill(p);
161   }
162
163   xbt_context_empty_trash();
164
165   if(self) {
166     xbt_context_yield();
167   }
168
169   return;
170 }
171
172 /** 
173  * \brief Clean the SIMIX simulation
174  *
175  * This functions remove all memories needed to the SIMIX execution
176  */
177 void SIMIX_clean(void)
178 {
179   xbt_fifo_item_t i = NULL;
180   smx_host_t h = NULL;
181   smx_process_t p = NULL;
182
183
184   while((p=xbt_swag_extract(simix_global->process_list))) {
185     SIMIX_process_kill(p);
186   }
187
188   xbt_fifo_foreach(simix_global->host,i,h,smx_host_t) {
189     __SIMIX_host_destroy(h);
190   }
191   xbt_fifo_free(simix_global->host);
192   xbt_swag_free(simix_global->process_to_run);
193   xbt_swag_free(simix_global->process_list);
194   xbt_dict_free(&(simix_global->registered_functions));
195   simix_config_finalize();
196   free(simix_global);
197   surf_exit();
198
199   return ;
200 }
201
202
203 /**
204  * \brief A clock (in second).
205  *
206  * \return Return the clock.
207  */
208 double SIMIX_get_clock(void)
209 {
210   return surf_get_clock();
211 }
212
213 /**
214  *      \brief Does a turn of the simulation
215  *
216  *      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.         
217  *      \param actions_done List of actions done
218  *      \param actions_failed List of actions failed
219  *      \return The time spent to execute the simulation or -1 if the simulation ended
220  */
221 double SIMIX_solve(xbt_fifo_t actions_done, xbt_fifo_t actions_failed) 
222 {
223
224         smx_process_t process = NULL;
225         int i;
226         double elapsed_time = 0.0;
227         static int state_modifications = 1;
228         static int first = 1;
229
230         xbt_context_empty_trash();
231         if(xbt_swag_size(simix_global->process_to_run) && (elapsed_time>0)) {
232                 DEBUG0("**************************************************");
233         }
234         if (first) {
235                 surf_solve();/* Takes traces into account. Returns 0.0 */
236                 first=0;
237         }
238         while ((process = xbt_swag_extract(simix_global->process_to_run))) {
239                 DEBUG2("Scheduling %s on %s",        
240                                 process->name,
241                                 process->simdata->host->name);
242                 simix_global->current_process = process;
243                 xbt_context_schedule(process->simdata->context);
244                 /*       fflush(NULL); */
245                 simix_global->current_process = NULL;
246         }
247
248         {
249                 surf_action_t action = NULL;
250                 surf_resource_t resource = NULL;
251                 smx_action_t smx_action = NULL;
252
253                 void *fun = NULL;
254                 void *arg = NULL;
255
256                 xbt_dynar_foreach(resource_list, i, resource) {
257                         if(xbt_swag_size(resource->common_public->states.failed_action_set) ||
258                                         xbt_swag_size(resource->common_public->states.done_action_set)) {
259                                 state_modifications = 1;
260                                 }
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) {
272                                 process_arg_t args = arg;
273                                 DEBUG2("Launching %s on %s", args->name, args->hostname);
274                                 process = SIMIX_process_create(args->name, args->code, 
275                                                 args->data, args->hostname,
276                                                 args->argc,args->argv,NULL);
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         state_modifications = 0;
309
310         if (elapsed_time == -1) {
311                 if (xbt_swag_size(simix_global->process_list) == 0) {
312                         INFO0("Congratulations ! Simulation terminated : all processes are over");
313                 } else {
314                         INFO0("Oops ! Deadlock or code not perfectly clean.");
315                         __SIMIX_display_process_status();
316                         if(XBT_LOG_ISENABLED(simix, xbt_log_priority_debug) ||
317                                         XBT_LOG_ISENABLED(simix_kernel, xbt_log_priority_debug)) {
318                                 DEBUG0("Aborting!");
319                                 xbt_abort();
320                         }
321                         INFO0("Return a Warning.");
322                 }
323         }
324         return elapsed_time;
325 }
326
327 /**
328  *      \brief Set the date to execute a function
329  *
330  * Set the date to execute the function on the surf.
331  *      \param date Date to execute function
332  *      \param function Function to be executed
333  *      \param arg Parameters of the function
334  *
335  */
336 void SIMIX_timer_set (double date, void *function, void *arg)
337 {
338         surf_timer_resource->extension_public->set(date, function, arg);
339 }
340
341 int SIMIX_timer_get(void **function, void **arg)
342 {
343         return surf_timer_resource->extension_public->get(function, arg);
344 }
345
346 /**
347  *      \brief Registers a function to create a process.
348  *
349  *      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.
350  *      \param function Create process function
351  *
352  */
353 void SIMIX_function_register_process_create(void * function)
354 {
355   xbt_assert0((simix_global->create_process_function == NULL), "Data already set");
356
357   /* Assign create process */
358   simix_global->create_process_function = function;
359
360   return ;
361 }
362
363 /**
364  *      \brief Registers a function to kill a process.
365  *
366  *      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.
367  *      \param function Kill process function
368  *
369  */
370 void SIMIX_function_register_process_kill(void * function)
371 {
372   xbt_assert0((simix_global->kill_process_function == NULL), "Data already set");
373
374   /* Assign kill process */
375   simix_global->kill_process_function = function;
376
377   return ;
378 }