Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Now, any surf-based program can take --surf-path arguments to help locating platform...
[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       break;
146     }
147
148     {
149       surf_action_t action = NULL;
150       surf_resource_t resource = NULL;
151       m_task_t task = NULL;
152       
153       xbt_dynar_foreach(resource_list, i, resource) {
154         while ((action =
155                xbt_swag_extract(resource->common_public->states.
156                                 failed_action_set))) {
157           task = action->data;
158           if(task) {
159             int _cursor;
160 /*          fprintf(stderr,"** %s **\n",task->name); */
161             xbt_dynar_foreach(task->simdata->sleeping,_cursor,process) {
162               xbt_fifo_unshift(msg_global->process_to_run, process);
163             }
164             process=NULL;
165           }
166         }
167         while ((action =
168                xbt_swag_extract(resource->common_public->states.
169                                 done_action_set))) {
170           task = action->data;
171           if(task) {
172             int _cursor;
173 /*          fprintf(stderr,"** %s **\n",task->name); */
174             xbt_dynar_foreach(task->simdata->sleeping,_cursor,process) {
175               xbt_fifo_unshift(msg_global->process_to_run, process);
176             }
177             process=NULL;
178           }
179         }
180       }
181     }
182   }
183
184   if ((nbprocess=xbt_fifo_size(msg_global->process_list)) == 0) {
185     fprintf(stderr,
186             "MSG: Congratulations ! Simulation terminated : all process are over\n");
187     return MSG_OK;
188   } else {
189     fprintf(stderr,"MSG: Oops ! Deadlock or code not perfectly clean.\n");
190     fprintf(stderr,"MSG: %d processes are still running, waiting for something.\n",
191             nbprocess);
192     /*  List the process and their state */
193     fprintf(stderr,"MSG: <process>(<pid>) on <host>: <status>.\n");
194     while ((process=xbt_fifo_pop(msg_global->process_list))) {
195       simdata_process_t p_simdata = (simdata_process_t) process->simdata;
196       simdata_host_t h_simdata=(simdata_host_t)p_simdata->host->simdata;
197       
198       fprintf(stderr,"MSG:  %s(%d) on %s: ",
199              process->name,p_simdata->PID,
200              p_simdata->host->name);
201       for (i=0; i<msg_global->max_channel; i++) {
202         if (h_simdata->sleeping[i] == process) {
203           fprintf(stderr,"Listening on channel %d.\n",i);
204           break;
205         }
206       }
207       if (i==msg_global->max_channel) {
208         if(p_simdata->waiting_task) {
209           if(p_simdata->waiting_task->simdata->compute)
210             fprintf(stderr,"Waiting for %s to finish.\n",p_simdata->waiting_task->name);
211           else if (p_simdata->waiting_task->simdata->comm)
212             fprintf(stderr,"Waiting for %s to be finished transfered.\n",
213                     p_simdata->waiting_task->name);
214           else
215             fprintf(stderr,"UNKNOWN STATUS. Please report this bug.\n");
216         }
217         else { /* Must be trying to put a task somewhere */
218           if(p_simdata->put_host) {
219             fprintf(stderr,"Trying to send a task on Host %s, channel %d.\n",
220                     p_simdata->put_host->name, p_simdata->put_channel);
221           } else {
222             fprintf(stderr,"UNKNOWN STATUS. Please report this bug.\n");
223           }
224         }       
225       } 
226     }
227     return MSG_WARNING;
228   }
229 }
230
231 /* static void MarkAsFailed(m_task_t t, TBX_HashTable_t failedProcessList)  */
232 /* { */
233 /*   simdata_task_t simdata = NULL; */
234 /*   xbt_fifo_item_t i = NULL; */
235 /*   m_process_t p = NULL; */
236   
237 /*   xbt_assert0((t!=NULL),"Invalid task"); */
238 /*   simdata = t->simdata; */
239
240 /* #define KILL(task) if(task) SG_failTask(task) */
241 /*   KILL(simdata->compute); */
242 /*   KILL(simdata->TCP_comm); */
243 /*   KILL(simdata->s[0]); */
244 /*   KILL(simdata->s[1]); */
245 /*   KILL(simdata->s[2]); */
246 /*   KILL(simdata->s[3]); */
247 /*   KILL(simdata->sleep); */
248 /* #undef KILL   */
249 /* /\*   if(simdata->comm) SG_failEndToEndTransfer(simdata->comm); *\/ */
250   
251 /*   xbt_fifo_foreach(simdata->sleeping,i,p,m_process_t) { */
252 /*     if(!TBX_HashTable_isInList(failedProcessList,p,TBX_basicHash))  */
253 /*       TBX_HashTable_insert(failedProcessList,p,TBX_basicHash); */
254 /*   } */
255   
256 /* } */
257
258 /* static xbt_fifo_t MSG_buildFailedHostList(long double begin, long double end) */
259 /* { */
260 /*   xbt_fifo_t failedHostList = xbt_fifo_new(); */
261 /*   m_host_t host = NULL; */
262 /*   xbt_fifo_item_t i; */
263
264 /*   xbt_fifo_foreach(msg_global->host,i,host,m_host_t) { */
265 /*     SG_Resource r= ((simdata_host_t) (host->simdata))->host; */
266
267 /*     if(SG_evaluateFailureTrace(r->failure_trace,begin,end)!=-1.0) */
268 /*       xbt_fifo_insert(failedHostList,host); */
269 /*   } */
270
271 /*   return failedHostList; */
272 /* } */
273
274 /** \ingroup msg_simulation
275  * \brief Clean the MSG simulation
276  */
277 MSG_error_t MSG_clean(void)
278 {
279   xbt_fifo_item_t i = NULL;
280   m_host_t h = NULL;
281
282   xbt_context_exit();
283
284   xbt_fifo_foreach(msg_global->host,i,h,m_host_t) {
285     __MSG_host_destroy(h);
286   }
287   xbt_fifo_free(msg_global->host);
288   xbt_fifo_free(msg_global->process_to_run);
289   xbt_fifo_free(msg_global->process_list);
290   xbt_dict_free(&(msg_global->registered_functions));
291
292   xbt_free(msg_global);
293   surf_finalize();
294
295   return MSG_OK;
296 }
297
298
299 /** \ingroup msg_easier_life
300  * \brief A clock (in second).
301  */
302 long double MSG_getClock(void) {
303   return surf_get_clock();
304 }
305