3 /* Copyright (c) 2002,2003,2004 Arnaud Legrand. All rights reserved. */
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. */
10 #include "xbt/error.h"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(global, msg,
12 "Logging specific to MSG (global)");
14 MSG_Global_t msg_global = NULL;
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); */
19 /********************************* MSG **************************************/
21 /** \ingroup msg_simulation
22 * \brief Initialize some MSG internal data.
24 void MSG_global_init(void)
30 msg_global = xbt_new0(s_MSG_Global_t,1);
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();
42 /** \ingroup msg_simulation
43 * \brief Defines the verbosity of the simulation.
45 void MSG_set_verbosity(MSG_outputmode_t mode)
47 CRITICAL0("MSG_set_verbosity : Not implemented yet.");
50 /** \ingroup m_channel_management
51 * \brief Set the number of channel in the simulation.
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
58 * \param number the number of channel in the simulation. It has to be >0
60 MSG_error_t MSG_set_channel_number(int number)
64 xbt_assert0((msg_global->max_channel == 0), "Channel number already set!");
66 msg_global->max_channel = number;
71 /** \ingroup m_simulation
72 * \brief Set the sharing policy used for the links
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
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).
84 MSG_error_t MSG_set_sharing_policy(MSG_sharing_t mode, long double param)
86 CRITICAL0("MSG_set_sharing_policy: this function is now deprecated and useless. Store and forward does not exist anymore. Please stop using it.");
91 /** \ingroup m_channel_management
92 * \brief Return the number of channel in the simulation.
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.
98 int MSG_get_channel_number(void)
102 xbt_assert0((msg_global->max_channel != 0), "Channel number not set yet!");
104 return msg_global->max_channel;
107 /** \ingroup msg_simulation
108 * \brief Launch the MSG simulation
110 MSG_error_t MSG_main(void)
112 m_process_t process = NULL;
114 long double Before=0.0;
116 double elapsed_time = 0.0;
118 /* Clean IO before the run */
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;
129 Before = MSG_getClock();
130 elapsed_time = surf_solve();
131 Now = MSG_getClock();
133 if (elapsed_time==0.0)
136 /* /\* Handle Failures *\/ */
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(); */
143 /* while((host=xbt_fifo_pop(failedHostList))) { */
144 /* simdata_host_t h_simdata= host->simdata; */
145 /* TBX_HashTable_t process_list= h_simdata->process; */
147 /* h_simdata->state = HOST_DOWN; */
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; */
154 /* if(!TBX_HashTable_isInList(failedProcessList,p,TBX_basicHash)) */
155 /* TBX_HashTable_insert(failedProcessList,p,TBX_basicHash); */
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); */
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); */
175 /* fprintf(stderr,"... WTF! UNKNOWN STATUS. Please report this bug.\n"); */
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... *\/ */
187 /* TBX_HashTable_freeList(failedProcessList,NULL); */
192 surf_action_t action = NULL;
193 surf_resource_t resource = NULL;
195 xbt_dynar_foreach(resource_list, i, resource) {
197 xbt_swag_extract(resource->common_public->states.
198 failed_action_set))) {
199 /* xbt_fifo_insert(msg_global->process_to_run, process); */
202 xbt_swag_extract(resource->common_public->states.
204 /* xbt_fifo_insert(msg_global->process_to_run, process); */
210 if ((nbprocess=xbt_fifo_size(msg_global->process_list)) == 0) {
212 "MSG: Congratulations ! Simulation terminated : all process are over\n");
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",
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;
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);
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); */
242 /* fprintf(stderr,"UNKNOWN STATUS. Please report this bug.\n"); */
251 /* static void MarkAsFailed(m_task_t t, TBX_HashTable_t failedProcessList) */
253 /* simdata_task_t simdata = NULL; */
254 /* xbt_fifo_item_t i = NULL; */
255 /* m_process_t p = NULL; */
257 /* xbt_assert0((t!=NULL),"Invalid task"); */
258 /* simdata = t->simdata; */
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); */
269 /* /\* if(simdata->comm) SG_failEndToEndTransfer(simdata->comm); *\/ */
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); */
278 /* static xbt_fifo_t MSG_buildFailedHostList(long double begin, long double end) */
280 /* xbt_fifo_t failedHostList = xbt_fifo_new(); */
281 /* m_host_t host = NULL; */
282 /* xbt_fifo_item_t i; */
284 /* xbt_fifo_foreach(msg_global->host,i,host,m_host_t) { */
285 /* SG_Resource r= ((simdata_host_t) (host->simdata))->host; */
287 /* if(SG_evaluateFailureTrace(r->failure_trace,begin,end)!=-1.0) */
288 /* xbt_fifo_insert(failedHostList,host); */
291 /* return failedHostList; */
294 /** \ingroup msg_simulation
295 * \brief Clean the MSG simulation
297 MSG_error_t MSG_clean(void)
299 xbt_fifo_item_t i = NULL;
302 xbt_fifo_foreach(msg_global->host,i,h,m_host_t) {
303 __MSG_host_destroy(h);
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));
310 xbt_free(msg_global);
317 /** \ingroup msg_easier_life
318 * \brief A clock (in second).
320 long double MSG_getClock(void) {
321 return surf_get_clock();