Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
debug debug debug
[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   if (!msg_global) {
27     int argc=0;
28     char **argv=NULL;
29
30     msg_global = xbt_new0(s_MSG_Global_t,1);
31
32     surf_init(&argc, argv);     /* Initialize some common structures */
33     msg_global->host = xbt_fifo_new();
34     msg_global->process_to_run = xbt_fifo_new();
35     msg_global->process_list = xbt_fifo_new();
36     msg_global->max_channel = 0;
37     msg_global->current_process = NULL;
38     msg_global->registered_functions = xbt_dict_new();
39   }
40 }
41
42 /** \ingroup msg_simulation
43  * \brief Defines the verbosity of the simulation.
44  */
45 void MSG_set_verbosity(MSG_outputmode_t mode)
46 {
47   CRITICAL0("MSG_set_verbosity : Not implemented yet.");
48 }
49
50 /** \ingroup m_channel_management
51  * \brief Set the number of channel in the simulation.
52  *
53  * This function has to be called to fix the number of channel in the
54    simulation before creating any host. Indeed, each channel is
55    represented by a different mailbox on each #m_host_t. This
56    function can then be called only once. This function takes only one
57    parameter.
58  * \param number the number of channel in the simulation. It has to be >0
59  */
60 MSG_error_t MSG_set_channel_number(int number)
61 {
62   MSG_global_init();
63
64   xbt_assert0((msg_global->max_channel == 0), "Channel number already set!");
65
66   msg_global->max_channel = number;
67
68   return MSG_OK;
69 }
70
71 /** \ingroup m_simulation
72  * \brief Set the sharing policy used for the links
73  *
74  * This function can be called to change the sharing policy used for the links 
75    (see \ref paper_tcp). By default the store and forward mechanism is used
76    with a parameter equal to 0.1. This function has to be called before creating 
77    any link. 
78  * \param mode the sharing policy used for the links: can be either 
79    MSG_STORE_AND_FORWARD or MSG_TCP.
80  * \param param a parameter for the sharing policy. It has to be >0. It is 
81    currently used only for the MSG_STORE_AND_FORWARD flavor and represents the
82    granularity of the communications (i.e. the packet size).
83  */
84 MSG_error_t MSG_set_sharing_policy(MSG_sharing_t mode, long double param)
85 {
86   CRITICAL0("MSG_set_sharing_policy: this function is now deprecated and useless. Store and forward does not exist anymore. Please stop using it.");
87   
88   return MSG_OK;
89 }
90
91 /** \ingroup m_channel_management
92  * \brief Return the number of channel in the simulation.
93  *
94  * This function has to be called once the number of channel is fixed. I can't 
95    figure out a reason why anyone would like to call this function but nevermind.
96  * \return the number of channel in the simulation.
97  */
98 int MSG_get_channel_number(void)
99 {
100   MSG_global_init();
101
102   xbt_assert0((msg_global->max_channel != 0), "Channel number not set yet!");
103
104   return msg_global->max_channel;
105 }
106
107 /** \ingroup msg_simulation
108  * \brief Launch the MSG simulation
109  */
110 MSG_error_t MSG_main(void)
111 {
112   m_process_t process = NULL;
113   int nbprocess,i;
114   long double Before=0.0;
115   long double Now=0.0;
116   double elapsed_time = 0.0;
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   while (xbt_fifo_size(msg_global->process_list)) {
124     while ((process = xbt_fifo_pop(msg_global->process_to_run))) {
125       msg_global->current_process = process;
126       xbt_context_yield(process->simdata->context);
127       msg_global->current_process = NULL;
128     }
129     Before = MSG_getClock();
130     elapsed_time = surf_solve();
131     Now = MSG_getClock();
132
133     if (elapsed_time==0.0)
134       break;
135     
136 /*     /\* Handle Failures *\/ */
137 /*     { */
138 /*       xbt_fifo_t failedHostList = MSG_buildFailedHostList(Before,Now); */
139 /*       m_host_t host = NULL; */
140 /*       TBX_HashTable_Bucket_t b; */
141 /*       TBX_HashTable_t failedProcessList = TBX_HashTable_newList(); */
142       
143 /*       while((host=xbt_fifo_pop(failedHostList))) { */
144 /*      simdata_host_t h_simdata= host->simdata; */
145 /*      TBX_HashTable_t process_list= h_simdata->process; */
146         
147 /*      h_simdata->state = HOST_DOWN; */
148         
149 /*      for (b=TBX_HashTable_getFirstBucket(process_list);b; */
150 /*           b=TBX_HashTable_getNextBucket(process_list,b)) { */
151 /*        m_process_t p = b->content; */
152 /*        simdata_process_t p_simdata = p->simdata; */
153           
154 /*        if(!TBX_HashTable_isInList(failedProcessList,p,TBX_basicHash))  */
155 /*          TBX_HashTable_insert(failedProcessList,p,TBX_basicHash); */
156           
157 /*        fprintf(stderr,"MSG:  %s(%d) on %s(%d): Has died while ", */
158 /*                p->name,p_simdata->PID, */
159 /*                p_simdata->host->name,h_simdata->PID); */
160 /*        for (i=0; i<msg_global->max_channel; i++) { */
161 /*          if (h_simdata->sleeping[i] == p_simdata->context) { */
162 /*            fprintf(stderr,"listening on channel %d.\n",i); */
163 /*            break; */
164 /*          } */
165 /*        } */
166 /*        if (i==msg_global->max_channel) { */
167 /*          if(p_simdata->waiting_task) {              */
168 /*            fprintf(stderr,"waiting for %s to finish.\n",p_simdata->waiting_task->name); */
169 /*            MarkAsFailed(p_simdata->waiting_task,failedProcessList); */
170 /*          } else { /\* Must be trying to put a task somewhere *\/ */
171 /*            if(p_simdata->put_host) { */
172 /*              fprintf(stderr,"trying to send a task on Host %s, channel %d.\n", */
173 /*                      p_simdata->put_host->name, p_simdata->put_channel); */
174 /*            } else { */
175 /*              fprintf(stderr,"... WTF! UNKNOWN STATUS. Please report this bug.\n"); */
176 /*            } */
177 /*          }    */
178 /*        }  */
179 /*      } */
180 /*       } */
181 /*       xbt_fifo_freeFIFO(failedHostList); */
182 /*       for (b=TBX_HashTable_getFirstBucket(failedProcessList);b; */
183 /*         b=TBX_HashTable_getNextBucket(failedProcessList,b)) { */
184 /*      m_process_t p = b->content; */
185 /*      xbt_fifo_insert(msg_global->process_to_run, p); /\* Brutal... *\/ */
186 /*       } */
187 /*       TBX_HashTable_freeList(failedProcessList,NULL); */
188 /*     } */
189
190
191     {
192       surf_action_t action = NULL;
193       surf_resource_t resource = NULL;
194       
195       xbt_dynar_foreach(resource_list, i, resource) {
196         while ((action =
197                xbt_swag_extract(resource->common_public->states.
198                                 failed_action_set))) {
199 /*        xbt_fifo_insert(msg_global->process_to_run, process); */
200         }
201         while ((action =
202                xbt_swag_extract(resource->common_public->states.
203                                 done_action_set))) {
204 /*        xbt_fifo_insert(msg_global->process_to_run, process); */
205         }
206       }
207     }
208   }
209
210   if ((nbprocess=xbt_fifo_size(msg_global->process_list)) == 0) {
211     fprintf(stderr,
212             "MSG: Congratulations ! Simulation terminated : all process are over\n");
213     return MSG_OK;
214   } else {
215     fprintf(stderr,"MSG: Oops ! Deadlock or code not perfectly clean.\n");
216     fprintf(stderr,"MSG: %d processes are still running, waiting for something.\n",
217             nbprocess);
218     /*  List the process and their state */
219     fprintf(stderr,"MSG: <process>(<pid>) on <host>(<pid>): <status>.\n");
220     while ((process=xbt_fifo_pop(msg_global->process_list))) {
221       simdata_process_t p_simdata = (simdata_process_t) process->simdata;
222       simdata_host_t h_simdata=(simdata_host_t)p_simdata->host->simdata;
223       
224       fprintf(stderr,"MSG:  %s(%d) on %s: ",
225              process->name,p_simdata->PID,
226              p_simdata->host->name);
227       for (i=0; i<msg_global->max_channel; i++) {
228         if (h_simdata->sleeping[i] == process) {
229           fprintf(stderr,"Listening on channel %d.\n",i);
230           break;
231         }
232       }
233       if (i==msg_global->max_channel) {
234         if(p_simdata->waiting_task) 
235           fprintf(stderr,"Waiting for %s to finish.\n",p_simdata->waiting_task->name);
236         else { /* Must be trying to put a task somewhere */
237           CRITICAL0("Well! I don't know. I have to fix this part of the code. ;)");
238 /*        if(p_simdata->put_host) { */
239 /*          fprintf(stderr,"Trying to send a task on Host %s, channel %d.\n", */
240 /*                  p_simdata->put_host->name, p_simdata->put_channel); */
241 /*        } else { */
242 /*          fprintf(stderr,"UNKNOWN STATUS. Please report this bug.\n"); */
243 /*        } */
244         }       
245       } 
246     }
247     return MSG_WARNING;
248   }
249 }
250
251 /* static void MarkAsFailed(m_task_t t, TBX_HashTable_t failedProcessList)  */
252 /* { */
253 /*   simdata_task_t simdata = NULL; */
254 /*   xbt_fifo_item_t i = NULL; */
255 /*   m_process_t p = NULL; */
256   
257 /*   xbt_assert0((t!=NULL),"Invalid task"); */
258 /*   simdata = t->simdata; */
259
260 /* #define KILL(task) if(task) SG_failTask(task) */
261 /*   KILL(simdata->compute); */
262 /*   KILL(simdata->TCP_comm); */
263 /*   KILL(simdata->s[0]); */
264 /*   KILL(simdata->s[1]); */
265 /*   KILL(simdata->s[2]); */
266 /*   KILL(simdata->s[3]); */
267 /*   KILL(simdata->sleep); */
268 /* #undef KILL   */
269 /* /\*   if(simdata->comm) SG_failEndToEndTransfer(simdata->comm); *\/ */
270   
271 /*   xbt_fifo_foreach(simdata->sleeping,i,p,m_process_t) { */
272 /*     if(!TBX_HashTable_isInList(failedProcessList,p,TBX_basicHash))  */
273 /*       TBX_HashTable_insert(failedProcessList,p,TBX_basicHash); */
274 /*   } */
275   
276 /* } */
277
278 /* static xbt_fifo_t MSG_buildFailedHostList(long double begin, long double end) */
279 /* { */
280 /*   xbt_fifo_t failedHostList = xbt_fifo_new(); */
281 /*   m_host_t host = NULL; */
282 /*   xbt_fifo_item_t i; */
283
284 /*   xbt_fifo_foreach(msg_global->host,i,host,m_host_t) { */
285 /*     SG_Resource r= ((simdata_host_t) (host->simdata))->host; */
286
287 /*     if(SG_evaluateFailureTrace(r->failure_trace,begin,end)!=-1.0) */
288 /*       xbt_fifo_insert(failedHostList,host); */
289 /*   } */
290
291 /*   return failedHostList; */
292 /* } */
293
294 /** \ingroup msg_simulation
295  * \brief Clean the MSG simulation
296  */
297 MSG_error_t MSG_clean(void)
298 {
299   xbt_fifo_item_t i = NULL;
300   m_host_t h = NULL;
301
302   xbt_fifo_foreach(msg_global->host,i,h,m_host_t) {
303     __MSG_host_destroy(h);
304   }
305   xbt_fifo_free(msg_global->host);
306   xbt_fifo_free(msg_global->process_to_run);
307   xbt_fifo_free(msg_global->process_list);
308   xbt_dict_free(&(msg_global->registered_functions));
309
310   xbt_free(msg_global);
311   surf_finalize();
312
313   return MSG_OK;
314 }
315
316
317 /** \ingroup msg_easier_life
318  * \brief A clock (in second).
319  */
320 long double MSG_getClock(void) {
321   return surf_get_clock();
322 }
323