Logo AND Algorithmique Numérique Distribuée

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