Logo AND Algorithmique Numérique Distribuée

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