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"
146 "% TaskName string\n"
148 "%EventDef PajePopState 12\n"
150 "% EntityType string\n"
151 "% Container string\n"
153 "%EventDef PajeSetVariable 13\n"
155 "% EntityType string\n"
156 "% Container string\n"
159 "%EventDef PajeAddVariable 14\n"
161 "% EntityType string\n"
162 "% Container string\n"
165 "%EventDef PajeSubVariable 15\n"
167 "% EntityType string\n"
168 "% Container string\n"
171 "%EventDef PajeStartLink 16\n"
173 "% EntityType string\n"
174 "% Container string\n"
176 "% SourceContainer string\n"
179 "%EventDef PajeEndLink 17\n"
181 "% EntityType string\n"
182 "% Container string\n"
184 "% DestContainer string\n"
188 const char *type_definitions = "1 Sim_t 0 Simulation_t\n"
189 "1 H_t Sim_t m_host_t\n"
190 "1 P_t H_t m_process_t\n"
191 "3 S_t P_t \"Process State\"\n"
192 "6 E S_t Executing\n"
194 "6 C S_t Communicating\n"
195 "5 Comm Sim_t P_t P_t Communication_t\n";
197 const char *ext = ".trace";
198 int ext_len = strlen(ext);
202 xbt_fifo_item_t item = NULL;
204 xbt_assert0(msg_global, "Initialize MSG first\n");
205 xbt_assert0(!msg_global->paje_output, "Paje output already defined\n");
206 xbt_assert0(filename, "Need a real file name\n");
208 len = strlen(filename);
209 if((len<ext_len) || (strncmp(filename+len-ext_len,ext,ext_len))) {
210 CRITICAL2("The name of the Paje trace file \"%s\" does not end by \"%s\". Paje will cause difficulties to read it.\n",
214 msg_global->paje_output=fopen(filename,"w");
215 xbt_assert1(msg_global->paje_output, "Failed to open %s \n",filename);
217 fprintf(msg_global->paje_output,"%s",paje_preembule);
218 fprintf(msg_global->paje_output,"%s",type_definitions);
221 for(i=0; i<msg_global->max_channel; i++) {
222 fprintf(msg_global->paje_output, "6 COMM_%d Comm \"Channel %d\"\n" ,i,i);
224 fprintf(msg_global->paje_output,
225 "7 0.0 CUR Sim_t 0 \"MSG simulation\"\n");
228 xbt_fifo_foreach(msg_global->host,item,host,m_host_t) {
233 xbt_fifo_foreach(msg_global->process_list,item,process,m_process_t) {
234 PAJE_PROCESS_NEW(process);
238 /** \defgroup m_channel_management Understanding channels
239 * \brief This section briefly describes the channel notion of MSG
241 * \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Channels" --> \endhtmlonly
244 * For convenience, the simulator provides the notion of channel
245 * that is close to the tag notion in MPI. A channel is not a
246 * socket. It doesn't need to be opened neither closed. It rather
247 * corresponds to the ports opened on the different machines.
251 /** \ingroup m_channel_management
252 * \brief Set the number of channel in the simulation.
254 * This function has to be called to fix the number of channel in the
255 simulation before creating any host. Indeed, each channel is
256 represented by a different mailbox on each #m_host_t. This
257 function can then be called only once. This function takes only one
259 * \param number the number of channel in the simulation. It has to be >0
261 MSG_error_t MSG_set_channel_number(int number)
263 xbt_assert0((msg_global) && (msg_global->max_channel == 0), "Channel number already set!");
265 msg_global->max_channel = number;
270 /** \ingroup m_channel_management
271 * \brief Return the number of channel in the simulation.
273 * This function has to be called once the number of channel is fixed. I can't
274 figure out a reason why anyone would like to call this function but nevermind.
275 * \return the number of channel in the simulation.
277 int MSG_get_channel_number(void)
279 xbt_assert0((msg_global)&&(msg_global->max_channel != 0), "Channel number not set yet!");
281 return msg_global->max_channel;
284 /** \ingroup msg_simulation
285 * \brief Launch the MSG simulation
287 MSG_error_t MSG_main(void)
289 m_process_t process = NULL;
291 double elapsed_time = 0.0;
292 int state_modifications = 1;
293 /* Clean IO before the run */
297 surf_solve(); /* Takes traces into account. Returns 0.0 */
298 /* xbt_fifo_size(msg_global->process_to_run) */
300 xbt_context_empty_trash();
301 if(xbt_fifo_size(msg_global->process_to_run) && (elapsed_time>0)) {
302 DEBUG0("**************************************************");
304 if((__stop_at_time>0) && (MSG_get_clock() >= __stop_at_time)) {
305 DEBUG0("Let's stop here!");
308 while ((process = xbt_fifo_pop(msg_global->process_to_run))) {
309 DEBUG3("Scheduling %s(%d) on %s",
310 process->name,process->simdata->PID,
311 process->simdata->host->name);
312 msg_global->current_process = process;
314 xbt_context_schedule(process->simdata->context);
315 msg_global->current_process = NULL;
319 surf_action_t action = NULL;
320 surf_resource_t resource = NULL;
321 m_task_t task = NULL;
326 xbt_dynar_foreach(resource_list, i, resource) {
327 if(xbt_swag_size(resource->common_public->states.failed_action_set) ||
328 xbt_swag_size(resource->common_public->states.done_action_set))
329 state_modifications = 1;
332 if(!state_modifications) {
333 DEBUG1("%f : Calling surf_solve",MSG_get_clock());
334 elapsed_time = surf_solve();
335 DEBUG1("Elapsed_time %f",elapsed_time);
337 if (elapsed_time<0.0) {
338 /* fprintf(stderr, "We're done %g\n",elapsed_time); */
343 while (surf_timer_resource->extension_public->get(&fun,(void*)&arg)) {
344 DEBUG2("got %p %p", fun, arg);
345 if(fun==MSG_process_create_with_arguments) {
346 process_arg_t args = arg;
347 DEBUG2("Launching %s on %s", args->name, args->host->name);
348 process = MSG_process_create_with_arguments(args->name, args->code,
349 args->data, args->host,
350 args->argc,args->argv);
351 if(args->kill_time > MSG_get_clock()) {
352 surf_timer_resource->extension_public->set(args->kill_time,
353 (void*) &MSG_process_kill,
358 if(fun==MSG_process_kill) {
360 DEBUG3("Killing %s(%d) on %s", process->name, process->simdata->PID,
361 process->simdata->host->name);
362 MSG_process_kill(process);
366 xbt_dynar_foreach(resource_list, i, resource) {
368 xbt_swag_extract(resource->common_public->states.
369 failed_action_set))) {
373 DEBUG1("** %s failed **",task->name);
374 xbt_dynar_foreach(task->simdata->sleeping,_cursor,process) {
375 DEBUG3("\t preparing to wake up %s(%d) on %s",
376 process->name,process->simdata->PID,
377 process->simdata->host->name);
378 xbt_fifo_unshift(msg_global->process_to_run, process);
384 xbt_swag_extract(resource->common_public->states.
389 DEBUG1("** %s done **",task->name);
390 xbt_dynar_foreach(task->simdata->sleeping,_cursor,process) {
391 DEBUG3("\t preparing to wake up %s(%d) on %s",
392 process->name,process->simdata->PID,
393 process->simdata->host->name);
394 xbt_fifo_unshift(msg_global->process_to_run, process);
401 state_modifications = 0;
404 if ((nbprocess=xbt_fifo_size(msg_global->process_list)) == 0) {
405 INFO0("Congratulations ! Simulation terminated : all process are over");
408 xbt_fifo_item_t item = NULL;
409 INFO0("Oops ! Deadlock or code not perfectly clean.");
410 INFO1("MSG: %d processes are still running, waiting for something.",
412 /* List the process and their state */
413 INFO0("MSG: <process>(<pid>) on <host>: <status>.");
414 xbt_fifo_foreach(msg_global->process_list,item,process,m_process_t) {
415 simdata_process_t p_simdata = (simdata_process_t) process->simdata;
416 simdata_host_t h_simdata=(simdata_host_t)p_simdata->host->simdata;
419 INFO4("MSG: %s(%d) on %s: %s",
420 process->name,p_simdata->PID,
421 p_simdata->host->name,
422 (process->simdata->blocked)?"[blocked] "
423 :((process->simdata->suspended)?"[suspended] ":""));
425 for (i=0; i<msg_global->max_channel; i++) {
426 if (h_simdata->sleeping[i] == process) {
427 INFO1("\tListening on channel %d.",i);
431 if (i==msg_global->max_channel) {
432 if(p_simdata->waiting_task) {
433 if(p_simdata->waiting_task->simdata->compute) {
434 if(p_simdata->put_host)
435 INFO2("\tTrying to send a task to Host %s, channel %d.",
436 p_simdata->put_host->name, p_simdata->put_channel);
438 INFO1("Waiting for %s to finish.",p_simdata->waiting_task->name);
439 } else if (p_simdata->waiting_task->simdata->comm)
440 INFO1("Waiting for %s to be finished transfered.",
441 p_simdata->waiting_task->name);
443 INFO0("UNKNOWN STATUS. Please report this bug.");
445 else { /* Must be trying to put a task somewhere */
446 INFO0("UNKNOWN STATUS. Please report this bug.");
450 if(XBT_LOG_ISENABLED(msg, xbt_log_priority_debug) ||
451 XBT_LOG_ISENABLED(global, xbt_log_priority_debug)) {
456 INFO0("Return a Warning.");
461 /** \ingroup msg_simulation
462 * \brief Kill all running process
464 * \param reset_PIDs should we reset the PID numbers. A negative
465 * number means no reset and a positive number will be used to set the PID
466 * of the next newly created process.
468 int MSG_process_killall(int reset_PIDs)
470 m_process_t p = NULL;
471 m_process_t self = MSG_process_self();
473 while((p=xbt_fifo_pop(msg_global->process_list))) {
474 if(p!=self) MSG_process_kill(p);
478 msg_global->PID = reset_PIDs;
479 msg_global->session++;
482 xbt_context_empty_trash();
488 return msg_global->PID;
491 /** \ingroup msg_simulation
492 * \brief Clean the MSG simulation
494 MSG_error_t MSG_clean(void)
496 xbt_fifo_item_t i = NULL;
498 m_process_t p = NULL;
501 while((p=xbt_fifo_pop(msg_global->process_list))) {
506 xbt_fifo_foreach(msg_global->host,i,h,m_host_t) {
507 __MSG_host_destroy(h);
509 xbt_fifo_free(msg_global->host);
510 xbt_fifo_free(msg_global->process_to_run);
511 xbt_fifo_free(msg_global->process_list);
512 xbt_dict_free(&(msg_global->registered_functions));
514 if(msg_global->paje_output) {
515 fclose(msg_global->paje_output);
516 msg_global->paje_output = NULL;
518 msg_config_finalize();
526 /** \ingroup msg_easier_life
527 * \brief A clock (in second).
529 double MSG_get_clock(void) {
530 return surf_get_clock();