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. */
9 #include "xbt/sysdep.h"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(global, msg,
12 "Logging specific to MSG (global)");
14 int __stop_at_time = -1.0 ;
16 MSG_Global_t msg_global = NULL;
18 /* static void MarkAsFailed(m_task_t t, TBX_HashTable_t failedProcessList); */
19 /* static xbt_fifo_t MSG_buildFailedHostList(double a, double b); */
21 /** \defgroup msg_simulation MSG simulation Functions
22 * \brief This section describes the functions you need to know to
23 * set up a simulation. You should have a look at \ref MSG_examples
24 * to have an overview of their usage.
25 * \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Simulation functions" --> \endhtmlonly
28 /********************************* MSG **************************************/
30 /** \ingroup msg_simulation
31 * \brief Initialize some MSG internal data.
33 void MSG_global_init_args(int *argc, char **argv)
35 MSG_global_init(argc,argv);
38 /** \ingroup msg_simulation
39 * \brief Initialize some MSG internal data.
41 void MSG_global_init(int *argc, char **argv)
44 surf_init(argc, argv); /* Initialize some common structures. Warning, it sets msg_global=NULL */
46 msg_global = xbt_new0(s_MSG_Global_t,1);
49 msg_global->host = xbt_fifo_new();
50 msg_global->process_to_run = xbt_fifo_new();
51 msg_global->process_list = xbt_fifo_new();
52 msg_global->max_channel = 0;
53 msg_global->current_process = NULL;
54 msg_global->registered_functions = xbt_dict_new();
59 /** \ingroup msg_easier_life
60 * \brief Traces MSG events in the Paje format.
62 void MSG_paje_output(const char *filename)
65 const char *paje_preembule="%EventDef SetLimits 0\n"
69 "%EventDef PajeDefineContainerType 1\n"
71 "% ContainerType string\n"
74 "%EventDef PajeDefineEventType 2\n"
76 "% ContainerType string\n"
79 "%EventDef PajeDefineStateType 3\n"
81 "% ContainerType string\n"
84 "%EventDef PajeDefineVariableType 4\n"
86 "% ContainerType string\n"
89 "%EventDef PajeDefineLinkType 5\n"
91 "% ContainerType string\n"
92 "% SourceContainerType string\n"
93 "% DestContainerType string\n"
96 "%EventDef PajeDefineEntityValue 6\n"
98 "% EntityType string\n"
101 "%EventDef PajeCreateContainer 7\n"
103 "% NewContainer string\n"
104 "% NewContainerType string\n"
105 "% Container string\n"
108 "%EventDef PajeDestroyContainer 8\n"
113 "%EventDef PajeNewEvent 9\n"
115 "% EntityType string\n"
116 "% Container string\n"
119 "%EventDef PajeSetState 10\n"
121 "% EntityType string\n"
122 "% Container string\n"
125 "%EventDef PajeSetState 101\n"
127 "% EntityType string\n"
128 "% Container string\n"
130 "% FileName string\n"
133 "%EventDef PajePushState 111\n"
135 "% EntityType string\n"
136 "% Container string\n"
138 "% FileName string\n"
141 "%EventDef PajePushState 11\n"
143 "% EntityType string\n"
144 "% Container string\n"
147 "%EventDef PajePopState 12\n"
149 "% EntityType string\n"
150 "% Container string\n"
152 "%EventDef PajeSetVariable 13\n"
154 "% EntityType string\n"
155 "% Container string\n"
158 "%EventDef PajeAddVariable 14\n"
160 "% EntityType string\n"
161 "% Container string\n"
164 "%EventDef PajeSubVariable 15\n"
166 "% EntityType string\n"
167 "% Container string\n"
170 "%EventDef PajeStartLink 16\n"
172 "% EntityType string\n"
173 "% Container string\n"
175 "% SourceContainer string\n"
178 "%EventDef PajeEndLink 17\n"
180 "% EntityType string\n"
181 "% Container string\n"
183 "% DestContainer string\n"
187 const char *type_definitions = "1 Sim_t 0 Simulation_t\n"
188 "1 H_t Sim_t m_host_t\n"
189 "1 P_t H_t m_process_t\n"
190 "3 S_t P_t \"Process State\"\n"
191 "6 E S_t Executing\n"
193 "6 C S_t Communicating\n"
194 "5 Comm Sim_t P_t P_t Communication_t\n";
196 const char *ext = ".trace";
197 int ext_len = strlen(ext);
201 xbt_fifo_item_t item = NULL;
203 xbt_assert0(msg_global, "Initialize MSG first\n");
204 xbt_assert0(!msg_global->paje_output, "Paje output already defined\n");
205 xbt_assert0(filename, "Need a real file name\n");
207 len = strlen(filename);
208 if((len<ext_len) || (strncmp(filename+len-ext_len,ext,ext_len))) {
209 CRITICAL2("The name of the Paje trace file \"%s\" does not end by \"%s\". Paje will cause difficulties to read it.\n",
213 msg_global->paje_output=fopen(filename,"w");
214 xbt_assert1(msg_global->paje_output, "Failed to open %s \n",filename);
216 fprintf(msg_global->paje_output,"%s",paje_preembule);
217 fprintf(msg_global->paje_output,"%s",type_definitions);
220 for(i=0; i<msg_global->max_channel; i++) {
221 fprintf(msg_global->paje_output, "6 COMM_%d Comm \"Channel %d\"\n" ,i,i);
223 fprintf(msg_global->paje_output,
224 "7 0.0 CUR Sim_t 0 \"MSG simulation\"\n");
227 xbt_fifo_foreach(msg_global->host,item,host,m_host_t) {
232 xbt_fifo_foreach(msg_global->process_list,item,process,m_process_t) {
233 PAJE_PROCESS_NEW(process);
237 /** \defgroup m_channel_management Understanding channels
238 * \brief This section briefly describes the channel notion of MSG
240 * \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Channels" --> \endhtmlonly
243 * For convenience, the simulator provides the notion of channel
244 * that is close to the tag notion in MPI. A channel is not a
245 * socket. It doesn't need to be opened neither closed. It rather
246 * corresponds to the ports opened on the different machines.
250 /** \ingroup m_channel_management
251 * \brief Set the number of channel in the simulation.
253 * This function has to be called to fix the number of channel in the
254 simulation before creating any host. Indeed, each channel is
255 represented by a different mailbox on each #m_host_t. This
256 function can then be called only once. This function takes only one
258 * \param number the number of channel in the simulation. It has to be >0
260 MSG_error_t MSG_set_channel_number(int number)
262 xbt_assert0((msg_global) && (msg_global->max_channel == 0), "Channel number already set!");
264 msg_global->max_channel = number;
269 /** \ingroup m_channel_management
270 * \brief Return the number of channel in the simulation.
272 * This function has to be called once the number of channel is fixed. I can't
273 figure out a reason why anyone would like to call this function but nevermind.
274 * \return the number of channel in the simulation.
276 int MSG_get_channel_number(void)
278 xbt_assert0((msg_global)&&(msg_global->max_channel != 0), "Channel number not set yet!");
280 return msg_global->max_channel;
283 /** \ingroup msg_simulation
284 * \brief Launch the MSG simulation
286 MSG_error_t MSG_main(void)
288 m_process_t process = NULL;
290 double elapsed_time = 0.0;
291 int state_modifications = 1;
292 /* Clean IO before the run */
296 surf_solve(); /* Takes traces into account. Returns 0.0 */
297 /* xbt_fifo_size(msg_global->process_to_run) */
299 xbt_context_empty_trash();
300 if(xbt_fifo_size(msg_global->process_to_run) && (elapsed_time>0)) {
301 DEBUG0("**************************************************");
303 if((__stop_at_time>0) && (MSG_get_clock() >= __stop_at_time)) {
304 DEBUG0("Let's stop here!");
307 while ((process = xbt_fifo_pop(msg_global->process_to_run))) {
308 DEBUG3("Scheduling %s(%d) on %s",
309 process->name,process->simdata->PID,
310 process->simdata->host->name);
311 msg_global->current_process = process;
313 xbt_context_schedule(process->simdata->context);
314 msg_global->current_process = NULL;
318 surf_action_t action = NULL;
319 surf_resource_t resource = NULL;
320 m_task_t task = NULL;
325 xbt_dynar_foreach(resource_list, i, resource) {
326 if(xbt_swag_size(resource->common_public->states.failed_action_set) ||
327 xbt_swag_size(resource->common_public->states.done_action_set))
328 state_modifications = 1;
331 if(!state_modifications) {
332 DEBUG1("%g : Calling surf_solve",MSG_get_clock());
333 elapsed_time = surf_solve();
334 DEBUG1("Elapsed_time %g",elapsed_time);
336 if (elapsed_time<0.0) {
337 /* fprintf(stderr, "We're done %g\n",elapsed_time); */
342 while (surf_timer_resource->extension_public->get(&fun,(void*)&arg)) {
343 DEBUG2("got %p %p", fun, arg);
344 if(fun==MSG_process_create_with_arguments) {
345 process_arg_t args = arg;
346 DEBUG2("Launching %s on %s", args->name, args->host->name);
347 process = MSG_process_create_with_arguments(args->name, args->code,
348 args->data, args->host,
349 args->argc,args->argv);
350 if(args->kill_time > MSG_get_clock()) {
351 surf_timer_resource->extension_public->set(args->kill_time,
352 (void*) &MSG_process_kill,
357 if(fun==MSG_process_kill) {
359 DEBUG3("Killing %s(%d) on %s", process->name, process->simdata->PID,
360 process->simdata->host->name);
361 MSG_process_kill(process);
365 xbt_dynar_foreach(resource_list, i, resource) {
367 xbt_swag_extract(resource->common_public->states.
368 failed_action_set))) {
372 DEBUG1("** %s failed **",task->name);
373 xbt_dynar_foreach(task->simdata->sleeping,_cursor,process) {
374 DEBUG3("\t preparing to wake up %s(%d) on %s",
375 process->name,process->simdata->PID,
376 process->simdata->host->name);
377 xbt_fifo_unshift(msg_global->process_to_run, process);
383 xbt_swag_extract(resource->common_public->states.
388 DEBUG1("** %s done **",task->name);
389 xbt_dynar_foreach(task->simdata->sleeping,_cursor,process) {
390 DEBUG3("\t preparing to wake up %s(%d) on %s",
391 process->name,process->simdata->PID,
392 process->simdata->host->name);
393 xbt_fifo_unshift(msg_global->process_to_run, process);
400 state_modifications = 0;
403 if ((nbprocess=xbt_fifo_size(msg_global->process_list)) == 0) {
404 INFO0("Congratulations ! Simulation terminated : all process are over");
407 xbt_fifo_item_t item = NULL;
408 INFO0("Oops ! Deadlock or code not perfectly clean.");
409 INFO1("MSG: %d processes are still running, waiting for something.",
411 /* List the process and their state */
412 INFO0("MSG: <process>(<pid>) on <host>: <status>.");
413 xbt_fifo_foreach(msg_global->process_list,item,process,m_process_t) {
414 simdata_process_t p_simdata = (simdata_process_t) process->simdata;
415 simdata_host_t h_simdata=(simdata_host_t)p_simdata->host->simdata;
418 INFO4("MSG: %s(%d) on %s: %s",
419 process->name,p_simdata->PID,
420 p_simdata->host->name,
421 (process->simdata->blocked)?"[blocked] "
422 :((process->simdata->suspended)?"[suspended] ":""));
424 for (i=0; i<msg_global->max_channel; i++) {
425 if (h_simdata->sleeping[i] == process) {
426 INFO1("\tListening on channel %d.",i);
430 if (i==msg_global->max_channel) {
431 if(p_simdata->waiting_task) {
432 if(p_simdata->waiting_task->simdata->compute) {
433 if(p_simdata->put_host)
434 INFO2("\tTrying to send a task to Host %s, channel %d.",
435 p_simdata->put_host->name, p_simdata->put_channel);
437 INFO1("Waiting for %s to finish.",p_simdata->waiting_task->name);
438 } else if (p_simdata->waiting_task->simdata->comm)
439 INFO1("Waiting for %s to be finished transfered.",
440 p_simdata->waiting_task->name);
442 INFO0("UNKNOWN STATUS. Please report this bug.");
444 else { /* Must be trying to put a task somewhere */
445 INFO0("UNKNOWN STATUS. Please report this bug.");
449 if(XBT_LOG_ISENABLED(msg, xbt_log_priority_debug) ||
450 XBT_LOG_ISENABLED(global, xbt_log_priority_debug)) {
455 INFO0("Return a Warning.");
460 /** \ingroup msg_simulation
461 * \brief Kill all running process
463 * \param reset_PIDs should we reset the PID numbers. A negative
464 * number means no reset and a positive number will be used to set the PID
465 * of the next newly created process.
467 int MSG_process_killall(int reset_PIDs)
469 m_process_t p = NULL;
470 m_process_t self = MSG_process_self();
472 while((p=xbt_fifo_pop(msg_global->process_list))) {
473 if(p!=self) MSG_process_kill(p);
477 msg_global->PID = reset_PIDs;
478 msg_global->session++;
481 xbt_context_empty_trash();
487 return msg_global->PID;
490 /** \ingroup msg_simulation
491 * \brief Clean the MSG simulation
493 MSG_error_t MSG_clean(void)
495 xbt_fifo_item_t i = NULL;
497 m_process_t p = NULL;
500 while((p=xbt_fifo_pop(msg_global->process_list))) {
505 xbt_fifo_foreach(msg_global->host,i,h,m_host_t) {
506 __MSG_host_destroy(h);
508 xbt_fifo_free(msg_global->host);
509 xbt_fifo_free(msg_global->process_to_run);
510 xbt_fifo_free(msg_global->process_list);
511 xbt_dict_free(&(msg_global->registered_functions));
513 if(msg_global->paje_output) {
514 fclose(msg_global->paje_output);
515 msg_global->paje_output = NULL;
517 msg_config_finalize();
525 /** \ingroup msg_easier_life
526 * \brief A clock (in second).
528 double MSG_get_clock(void) {
529 return surf_get_clock();