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 */
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();
43 /** \ingroup msg_simulation
44 * \brief Defines the verbosity of the simulation.
46 void MSG_set_verbosity(MSG_outputmode_t mode)
48 CRITICAL0("MSG_set_verbosity : Not implemented yet.");
51 /** \ingroup m_channel_management
52 * \brief Set the number of channel in the simulation.
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
59 * \param number the number of channel in the simulation. It has to be >0
61 MSG_error_t MSG_set_channel_number(int number)
65 xbt_assert0((msg_global->max_channel == 0), "Channel number already set!");
67 msg_global->max_channel = number;
72 /** \ingroup m_simulation
73 * \brief Set the sharing policy used for the links
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
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).
85 MSG_error_t MSG_set_sharing_policy(MSG_sharing_t mode, long double param)
87 CRITICAL0("MSG_set_sharing_policy: this function is now deprecated and useless. Store and forward does not exist anymore. Please stop using it.");
92 /** \ingroup m_channel_management
93 * \brief Return the number of channel in the simulation.
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.
99 int MSG_get_channel_number(void)
103 xbt_assert0((msg_global->max_channel != 0), "Channel number not set yet!");
105 return msg_global->max_channel;
108 /** \ingroup msg_simulation
109 * \brief Launch the MSG simulation
111 MSG_error_t MSG_main(void)
113 m_process_t process = NULL;
115 long double Before=0.0;
117 double elapsed_time = 0.0;
119 /* Clean IO before the run */
123 surf_solve(); /* Takes traces into account. Returns 0.0 */
124 /* xbt_fifo_size(msg_global->process_to_run) */
126 xbt_context_empty_trash();
127 while ((process = xbt_fifo_pop(msg_global->process_to_run))) {
128 /* fprintf(stderr,"-> %s (%d)\n",process->name, process->simdata->PID); */
129 msg_global->current_process = process;
130 xbt_context_schedule(process->simdata->context);
131 msg_global->current_process = NULL;
133 Before = MSG_getClock();
134 elapsed_time = surf_solve();
135 Now = MSG_getClock();
137 /* fprintf(stderr, "====== %Lg =====\n",Now); */
138 /* if (elapsed_time==0.0) { */
139 /* fprintf(stderr, "No change in time\n"); */
141 if (elapsed_time<0.0) {
146 surf_action_t action = NULL;
147 surf_resource_t resource = NULL;
148 m_task_t task = NULL;
150 xbt_dynar_foreach(resource_list, i, resource) {
152 xbt_swag_extract(resource->common_public->states.
153 failed_action_set))) {
157 /* fprintf(stderr,"** %s **\n",task->name); */
158 xbt_dynar_foreach(task->simdata->sleeping,_cursor,process) {
159 xbt_fifo_unshift(msg_global->process_to_run, process);
165 xbt_swag_extract(resource->common_public->states.
170 /* fprintf(stderr,"** %s **\n",task->name); */
171 xbt_dynar_foreach(task->simdata->sleeping,_cursor,process) {
172 xbt_fifo_unshift(msg_global->process_to_run, process);
181 if ((nbprocess=xbt_fifo_size(msg_global->process_list)) == 0) {
183 "MSG: Congratulations ! Simulation terminated : all process are over\n");
186 fprintf(stderr,"MSG: Oops ! Deadlock or code not perfectly clean.\n");
187 fprintf(stderr,"MSG: %d processes are still running, waiting for something.\n",
189 /* List the process and their state */
190 fprintf(stderr,"MSG: <process>(<pid>) on <host>: <status>.\n");
191 while ((process=xbt_fifo_pop(msg_global->process_list))) {
192 simdata_process_t p_simdata = (simdata_process_t) process->simdata;
193 simdata_host_t h_simdata=(simdata_host_t)p_simdata->host->simdata;
195 fprintf(stderr,"MSG: %s(%d) on %s: ",
196 process->name,p_simdata->PID,
197 p_simdata->host->name);
198 for (i=0; i<msg_global->max_channel; i++) {
199 if (h_simdata->sleeping[i] == process) {
200 fprintf(stderr,"Listening on channel %d.\n",i);
204 if (i==msg_global->max_channel) {
205 if(p_simdata->waiting_task) {
206 if(p_simdata->waiting_task->simdata->compute)
207 fprintf(stderr,"Waiting for %s to finish.\n",p_simdata->waiting_task->name);
208 else if (p_simdata->waiting_task->simdata->comm)
209 fprintf(stderr,"Waiting for %s to be finished transfered.\n",
210 p_simdata->waiting_task->name);
212 fprintf(stderr,"UNKNOWN STATUS. Please report this bug.\n");
214 else { /* Must be trying to put a task somewhere */
215 if(p_simdata->put_host) {
216 fprintf(stderr,"Trying to send a task on Host %s, channel %d.\n",
217 p_simdata->put_host->name, p_simdata->put_channel);
219 fprintf(stderr,"UNKNOWN STATUS. Please report this bug.\n");
228 /* static void MarkAsFailed(m_task_t t, TBX_HashTable_t failedProcessList) */
230 /* simdata_task_t simdata = NULL; */
231 /* xbt_fifo_item_t i = NULL; */
232 /* m_process_t p = NULL; */
234 /* xbt_assert0((t!=NULL),"Invalid task"); */
235 /* simdata = t->simdata; */
237 /* #define KILL(task) if(task) SG_failTask(task) */
238 /* KILL(simdata->compute); */
239 /* KILL(simdata->TCP_comm); */
240 /* KILL(simdata->s[0]); */
241 /* KILL(simdata->s[1]); */
242 /* KILL(simdata->s[2]); */
243 /* KILL(simdata->s[3]); */
244 /* KILL(simdata->sleep); */
246 /* /\* if(simdata->comm) SG_failEndToEndTransfer(simdata->comm); *\/ */
248 /* xbt_fifo_foreach(simdata->sleeping,i,p,m_process_t) { */
249 /* if(!TBX_HashTable_isInList(failedProcessList,p,TBX_basicHash)) */
250 /* TBX_HashTable_insert(failedProcessList,p,TBX_basicHash); */
255 /* static xbt_fifo_t MSG_buildFailedHostList(long double begin, long double end) */
257 /* xbt_fifo_t failedHostList = xbt_fifo_new(); */
258 /* m_host_t host = NULL; */
259 /* xbt_fifo_item_t i; */
261 /* xbt_fifo_foreach(msg_global->host,i,host,m_host_t) { */
262 /* SG_Resource r= ((simdata_host_t) (host->simdata))->host; */
264 /* if(SG_evaluateFailureTrace(r->failure_trace,begin,end)!=-1.0) */
265 /* xbt_fifo_insert(failedHostList,host); */
268 /* return failedHostList; */
271 /** \ingroup msg_simulation
272 * \brief Clean the MSG simulation
274 MSG_error_t MSG_clean(void)
276 xbt_fifo_item_t i = NULL;
281 xbt_fifo_foreach(msg_global->host,i,h,m_host_t) {
282 __MSG_host_destroy(h);
284 xbt_fifo_free(msg_global->host);
285 xbt_fifo_free(msg_global->process_to_run);
286 xbt_fifo_free(msg_global->process_list);
287 xbt_dict_free(&(msg_global->registered_functions));
289 xbt_free(msg_global);
296 /** \ingroup msg_easier_life
297 * \brief A clock (in second).
299 long double MSG_getClock(void) {
300 return surf_get_clock();