Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
No need to rebuild the parser if you're not a maintainer.
[simgrid.git] / src / msg / global.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2002,2003,2004 Arnaud Legrand. All rights reserved.        */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include"private.h"
9 #include"xbt/sysdep.h"
10 #include "xbt/error.h"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(global, msg,
12                                 "Logging specific to MSG (global)");
13
14 MSG_Global_t msg_global = NULL;
15
16 /* static void MarkAsFailed(m_task_t t, TBX_HashTable_t failedProcessList); */
17 /* static xbt_fifo_t MSG_buildFailedHostList(long double a, long double b); */
18
19 /********************************* MSG **************************************/
20
21 /** \ingroup msg_simulation
22  * \brief Initialize some MSG internal data.
23  */
24 void MSG_global_init(void)
25 {
26   int argc=0;
27   char **argv=NULL;
28
29   CRITICAL0("Please stop using this function. Use MSG_global_init_args instead.");
30   DIE_IMPOSSIBLE;
31   MSG_global_init_args(&argc,argv);
32 }
33
34 void MSG_global_init_args(int *argc, char **argv)
35 {
36   if (!msg_global) {
37     msg_global = xbt_new0(s_MSG_Global_t,1);
38
39     surf_init(argc, argv);      /* Initialize some common structures */
40     xbt_context_init();
41     msg_global->host = xbt_fifo_new();
42     msg_global->process_to_run = xbt_fifo_new();
43     msg_global->process_list = xbt_fifo_new();
44     msg_global->max_channel = 0;
45     msg_global->current_process = NULL;
46     msg_global->registered_functions = xbt_dict_new();
47   }
48 }
49
50 /** \ingroup msg_simulation
51  * \brief Defines the verbosity of the simulation.
52  */
53 void MSG_set_verbosity(MSG_outputmode_t mode)
54 {
55   CRITICAL0("MSG_set_verbosity : Not implemented yet.");
56 }
57
58 /** \ingroup m_channel_management
59  * \brief Set the number of channel in the simulation.
60  *
61  * This function has to be called to fix the number of channel in the
62    simulation before creating any host. Indeed, each channel is
63    represented by a different mailbox on each #m_host_t. This
64    function can then be called only once. This function takes only one
65    parameter.
66  * \param number the number of channel in the simulation. It has to be >0
67  */
68 MSG_error_t MSG_set_channel_number(int number)
69 {
70   xbt_assert0((msg_global) && (msg_global->max_channel == 0), "Channel number already set!");
71
72   msg_global->max_channel = number;
73
74   return MSG_OK;
75 }
76
77 /** \ingroup m_simulation
78  * \brief Set the sharing policy used for the links
79  *
80  * This function can be called to change the sharing policy used for the links 
81    (see \ref paper_tcp). By default the store and forward mechanism is used
82    with a parameter equal to 0.1. This function has to be called before creating 
83    any link. 
84  * \param mode the sharing policy used for the links: can be either 
85    MSG_STORE_AND_FORWARD or MSG_TCP.
86  * \param param a parameter for the sharing policy. It has to be >0. It is 
87    currently used only for the MSG_STORE_AND_FORWARD flavor and represents the
88    granularity of the communications (i.e. the packet size).
89  */
90 MSG_error_t MSG_set_sharing_policy(MSG_sharing_t mode, long double param)
91 {
92   CRITICAL0("MSG_set_sharing_policy: this function is now deprecated and useless. Store and forward does not exist anymore. Please stop using it.");
93   
94   return MSG_OK;
95 }
96
97 /** \ingroup m_channel_management
98  * \brief Return the number of channel in the simulation.
99  *
100  * This function has to be called once the number of channel is fixed. I can't 
101    figure out a reason why anyone would like to call this function but nevermind.
102  * \return the number of channel in the simulation.
103  */
104 int MSG_get_channel_number(void)
105 {
106   xbt_assert0((msg_global)&&(msg_global->max_channel != 0), "Channel number not set yet!");
107
108   return msg_global->max_channel;
109 }
110
111 /** \ingroup msg_simulation
112  * \brief Launch the MSG simulation
113  */
114 MSG_error_t MSG_main(void)
115 {
116   m_process_t process = NULL;
117   int nbprocess,i;
118   long double Before=0.0;
119   long double Now=0.0;
120   double elapsed_time = 0.0;
121
122   /* Clean IO before the run */
123   fflush(stdout);
124   fflush(stderr);
125
126   surf_solve(); /* Takes traces into account. Returns 0.0 */
127 /* xbt_fifo_size(msg_global->process_to_run) */
128   while (1) {
129     xbt_context_empty_trash();
130     while ((process = xbt_fifo_pop(msg_global->process_to_run))) {
131 /*       fprintf(stderr,"-> %s (%d)\n",process->name, process->simdata->PID); */
132       msg_global->current_process = process;
133       xbt_context_schedule(process->simdata->context);
134       msg_global->current_process = NULL;
135     }
136     Before = MSG_getClock();
137     elapsed_time = surf_solve();
138     Now = MSG_getClock();
139
140 /*     fprintf(stderr, "====== %Lg =====\n",Now); */
141 /*     if (elapsed_time==0.0) { */
142 /*       fprintf(stderr, "No change in time\n"); */
143 /*     } */
144     if (elapsed_time<0.0) {
145 /*       fprintf(stderr, "We're done %lg\n",elapsed_time); */
146       break;
147     }
148
149     {
150       surf_action_t action = NULL;
151       surf_resource_t resource = NULL;
152       m_task_t task = NULL;
153       
154       xbt_dynar_foreach(resource_list, i, resource) {
155         while ((action =
156                xbt_swag_extract(resource->common_public->states.
157                                 failed_action_set))) {
158           task = action->data;
159           if(task) {
160             int _cursor;
161             DEBUG1("** %s failed **",task->name);
162 /*          fprintf(stderr,"** %s **\n",task->name); */
163             xbt_dynar_foreach(task->simdata->sleeping,_cursor,process) {
164               DEBUG3("\t preparing to wake up %s(%d) on %s",         
165                      process->name,process->simdata->PID,
166                      process->simdata->host->name);
167               xbt_fifo_unshift(msg_global->process_to_run, process);
168             }
169             process=NULL;
170           }
171         }
172         while ((action =
173                xbt_swag_extract(resource->common_public->states.
174                                 done_action_set))) {
175           task = action->data;
176           if(task) {
177             int _cursor;
178             DEBUG1("** %s done **",task->name);
179 /*          fprintf(stderr,"** %s **\n",task->name); */
180             xbt_dynar_foreach(task->simdata->sleeping,_cursor,process) {
181               DEBUG3("\t preparing to wake up %s(%d) on %s",         
182                      process->name,process->simdata->PID,
183                      process->simdata->host->name);
184               xbt_fifo_unshift(msg_global->process_to_run, process);
185             }
186             process=NULL;
187           }
188         }
189       }
190     }
191   }
192
193   if ((nbprocess=xbt_fifo_size(msg_global->process_list)) == 0) {
194     fprintf(stderr,
195             "MSG: Congratulations ! Simulation terminated : all process are over\n");
196     return MSG_OK;
197   } else {
198     fprintf(stderr,"MSG: Oops ! Deadlock or code not perfectly clean.\n");
199     fprintf(stderr,"MSG: %d processes are still running, waiting for something.\n",
200             nbprocess);
201     /*  List the process and their state */
202     fprintf(stderr,"MSG: <process>(<pid>) on <host>: <status>.\n");
203     while ((process=xbt_fifo_pop(msg_global->process_list))) {
204       simdata_process_t p_simdata = (simdata_process_t) process->simdata;
205       simdata_host_t h_simdata=(simdata_host_t)p_simdata->host->simdata;
206       
207
208       fprintf(stderr,"MSG:  %s(%d) on %s: ",
209              process->name,p_simdata->PID,
210              p_simdata->host->name);
211
212       if (process->simdata->blocked)      
213         fprintf(stderr,"[blocked] ");
214       if (process->simdata->suspended)    
215         fprintf(stderr,"[suspended] ");
216
217       for (i=0; i<msg_global->max_channel; i++) {
218         if (h_simdata->sleeping[i] == process) {
219           fprintf(stderr,"Listening on channel %d.\n",i);
220           break;
221         }
222       }
223       if (i==msg_global->max_channel) {
224         if(p_simdata->waiting_task) {
225           if(p_simdata->waiting_task->simdata->compute) {
226             if(p_simdata->put_host) 
227               fprintf(stderr,"Trying to send a task on Host %s, channel %d.\n",
228                       p_simdata->put_host->name, p_simdata->put_channel);
229             else 
230               fprintf(stderr,"Waiting for %s to finish.\n",p_simdata->waiting_task->name);
231           } else if (p_simdata->waiting_task->simdata->comm)
232             fprintf(stderr,"Waiting for %s to be finished transfered.\n",
233                     p_simdata->waiting_task->name);
234           else
235             fprintf(stderr,"UNKNOWN STATUS. Please report this bug.\n");
236         }
237         else { /* Must be trying to put a task somewhere */
238           fprintf(stderr,"UNKNOWN STATUS. Please report this bug.\n");
239         }
240       } 
241     }
242     if(XBT_LOG_ISENABLED(msg, xbt_log_priority_debug) ||
243        XBT_LOG_ISENABLED(global, xbt_log_priority_debug)) {
244       DEBUG0("Aborting!");
245       xbt_abort();
246     }
247
248     return MSG_WARNING;
249   }
250 }
251
252 /* static void MarkAsFailed(m_task_t t, TBX_HashTable_t failedProcessList)  */
253 /* { */
254 /*   simdata_task_t simdata = NULL; */
255 /*   xbt_fifo_item_t i = NULL; */
256 /*   m_process_t p = NULL; */
257   
258 /*   xbt_assert0((t!=NULL),"Invalid task"); */
259 /*   simdata = t->simdata; */
260
261 /* #define KILL(task) if(task) SG_failTask(task) */
262 /*   KILL(simdata->compute); */
263 /*   KILL(simdata->TCP_comm); */
264 /*   KILL(simdata->s[0]); */
265 /*   KILL(simdata->s[1]); */
266 /*   KILL(simdata->s[2]); */
267 /*   KILL(simdata->s[3]); */
268 /*   KILL(simdata->sleep); */
269 /* #undef KILL   */
270 /* /\*   if(simdata->comm) SG_failEndToEndTransfer(simdata->comm); *\/ */
271   
272 /*   xbt_fifo_foreach(simdata->sleeping,i,p,m_process_t) { */
273 /*     if(!TBX_HashTable_isInList(failedProcessList,p,TBX_basicHash))  */
274 /*       TBX_HashTable_insert(failedProcessList,p,TBX_basicHash); */
275 /*   } */
276   
277 /* } */
278
279 /* static xbt_fifo_t MSG_buildFailedHostList(long double begin, long double end) */
280 /* { */
281 /*   xbt_fifo_t failedHostList = xbt_fifo_new(); */
282 /*   m_host_t host = NULL; */
283 /*   xbt_fifo_item_t i; */
284
285 /*   xbt_fifo_foreach(msg_global->host,i,host,m_host_t) { */
286 /*     SG_Resource r= ((simdata_host_t) (host->simdata))->host; */
287
288 /*     if(SG_evaluateFailureTrace(r->failure_trace,begin,end)!=-1.0) */
289 /*       xbt_fifo_insert(failedHostList,host); */
290 /*   } */
291
292 /*   return failedHostList; */
293 /* } */
294
295 /** \ingroup msg_simulation
296  * \brief Clean the MSG simulation
297  */
298 MSG_error_t MSG_clean(void)
299 {
300   xbt_fifo_item_t i = NULL;
301   m_host_t h = NULL;
302   m_process_t p = NULL;
303
304
305   while((p=xbt_fifo_shift(msg_global->process_list))) {
306     MSG_process_kill(p);
307   }
308   xbt_context_exit();
309
310   xbt_fifo_foreach(msg_global->host,i,h,m_host_t) {
311     __MSG_host_destroy(h);
312   }
313   xbt_fifo_free(msg_global->host);
314   xbt_fifo_free(msg_global->process_to_run);
315   xbt_fifo_free(msg_global->process_list);
316   xbt_dict_free(&(msg_global->registered_functions));
317
318   xbt_free(msg_global);
319   surf_finalize();
320
321   return MSG_OK;
322 }
323
324
325 /** \ingroup msg_easier_life
326  * \brief A clock (in second).
327  */
328 long double MSG_getClock(void) {
329   return surf_get_clock();
330 }
331