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)
27 msg_global = xbt_new0(s_MSG_Global_t,1);
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();
39 /** \ingroup msg_simulation
40 * \brief Defines the verbosity of the simulation.
42 void MSG_set_verbosity(MSG_outputmode_t mode)
44 CRITICAL0("Not implemented yet.");
47 /** \ingroup m_channel_management
48 * \brief Set the number of channel in the simulation.
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
55 * \param number the number of channel in the simulation. It has to be >0
57 MSG_error_t MSG_set_channel_number(int number)
61 xbt_assert0((msg_global->max_channel == 0), "Channel number already set!");
63 msg_global->max_channel = number;
68 /** \ingroup m_simulation
69 * \brief Set the sharing policy used for the links
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
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).
81 MSG_error_t MSG_set_sharing_policy(MSG_sharing_t mode, long double param)
83 xbt_assert0(0,"This function is now deprecated and useless. Store and forward does not exist anymore. Please stop using it.");
88 /** \ingroup m_channel_management
89 * \brief Return the number of channel in the simulation.
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.
95 int MSG_get_channel_number(void)
99 xbt_assert0((msg_global->max_channel != 0), "Channel number not set yet!");
101 return msg_global->max_channel;
104 /** \ingroup msg_simulation
105 * \brief Launch the MSG simulation
107 MSG_error_t MSG_main(void)
109 m_process_t process = NULL;
111 long double Before=0.0;
113 double elapsed_time = 0.0;
115 /* Clean IO before the run */
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;
126 Before = MSG_getClock();
127 elapsed_time = surf_solve();
128 Now = MSG_getClock();
130 if (elapsed_time==0.0)
133 /* /\* Handle Failures *\/ */
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(); */
140 /* while((host=xbt_fifo_pop(failedHostList))) { */
141 /* simdata_host_t h_simdata= host->simdata; */
142 /* TBX_HashTable_t process_list= h_simdata->process; */
144 /* h_simdata->state = HOST_DOWN; */
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; */
151 /* if(!TBX_HashTable_isInList(failedProcessList,p,TBX_basicHash)) */
152 /* TBX_HashTable_insert(failedProcessList,p,TBX_basicHash); */
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); */
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); */
172 /* fprintf(stderr,"... WTF! UNKNOWN STATUS. Please report this bug.\n"); */
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... *\/ */
184 /* TBX_HashTable_freeList(failedProcessList,NULL); */
189 surf_action_t action = NULL;
190 surf_resource_t resource = NULL;
192 xbt_dynar_foreach(resource_list, i, resource) {
194 xbt_swag_extract(resource->common_public->states.
195 failed_action_set))) {
196 /* xbt_fifo_insert(msg_global->process_to_run, process); */
199 xbt_swag_extract(resource->common_public->states.
201 /* xbt_fifo_insert(msg_global->process_to_run, process); */
207 if ((nbprocess=xbt_fifo_size(msg_global->process_list)) == 0) {
209 "MSG: Congratulations ! Simulation terminated : all process are over\n");
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",
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;
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);
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); */
239 /* fprintf(stderr,"UNKNOWN STATUS. Please report this bug.\n"); */
248 /* static void MarkAsFailed(m_task_t t, TBX_HashTable_t failedProcessList) */
250 /* simdata_task_t simdata = NULL; */
251 /* xbt_fifo_item_t i = NULL; */
252 /* m_process_t p = NULL; */
254 /* xbt_assert0((t!=NULL),"Invalid task"); */
255 /* simdata = t->simdata; */
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); */
266 /* /\* if(simdata->comm) SG_failEndToEndTransfer(simdata->comm); *\/ */
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); */
275 /* static xbt_fifo_t MSG_buildFailedHostList(long double begin, long double end) */
277 /* xbt_fifo_t failedHostList = xbt_fifo_new(); */
278 /* m_host_t host = NULL; */
279 /* xbt_fifo_item_t i; */
281 /* xbt_fifo_foreach(msg_global->host,i,host,m_host_t) { */
282 /* SG_Resource r= ((simdata_host_t) (host->simdata))->host; */
284 /* if(SG_evaluateFailureTrace(r->failure_trace,begin,end)!=-1.0) */
285 /* xbt_fifo_insert(failedHostList,host); */
288 /* return failedHostList; */
291 /** \ingroup msg_simulation
292 * \brief Clean the MSG simulation
294 MSG_error_t MSG_clean(void)
296 xbt_fifo_item_t i = NULL;
299 xbt_fifo_foreach(msg_global->host,i,h,m_host_t) {
300 __MSG_host_destroy(h);
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));
307 xbt_free(msg_global);
314 /** \ingroup msg_easier_life
315 * \brief A clock (in second).
317 long double MSG_getClock(void) {
318 return surf_get_clock();