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 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.
27 /********************************* MSG **************************************/
29 /** \ingroup msg_simulation
30 * \brief Initialize some MSG internal data.
32 void MSG_global_init(void)
37 CRITICAL0("Function MSG_global_init() is deprecated by MSG_global_init_args().");
38 MSG_global_init_args(&argc,argv);
41 /** \ingroup msg_simulation
42 * \brief Initialize some MSG internal data.
44 void MSG_global_init_args(int *argc, char **argv)
47 surf_init(argc, argv); /* Initialize some common structures. Warning, it sets msg_global=NULL */
49 msg_global = xbt_new0(s_MSG_Global_t,1);
52 msg_global->host = xbt_fifo_new();
53 msg_global->process_to_run = xbt_fifo_new();
54 msg_global->process_list = xbt_fifo_new();
55 msg_global->max_channel = 0;
56 msg_global->current_process = NULL;
57 msg_global->registered_functions = xbt_dict_new();
62 /** \ingroup msg_easier_life
63 * \brief Traces MSG events in the Paje format.
65 void MSG_paje_output(const char *filename)
68 const char *paje_preembule="%EventDef SetLimits 0\n"
72 "%EventDef PajeDefineContainerType 1\n"
74 "% ContainerType string\n"
77 "%EventDef PajeDefineEventType 2\n"
79 "% ContainerType string\n"
82 "%EventDef PajeDefineStateType 3\n"
84 "% ContainerType string\n"
87 "%EventDef PajeDefineVariableType 4\n"
89 "% ContainerType string\n"
92 "%EventDef PajeDefineLinkType 5\n"
94 "% ContainerType string\n"
95 "% SourceContainerType string\n"
96 "% DestContainerType string\n"
99 "%EventDef PajeDefineEntityValue 6\n"
100 "% NewValue string\n"
101 "% EntityType string\n"
104 "%EventDef PajeCreateContainer 7\n"
106 "% NewContainer string\n"
107 "% NewContainerType string\n"
108 "% Container string\n"
111 "%EventDef PajeDestroyContainer 8\n"
116 "%EventDef PajeNewEvent 9\n"
118 "% EntityType string\n"
119 "% Container string\n"
122 "%EventDef PajeSetState 10\n"
124 "% EntityType string\n"
125 "% Container string\n"
128 "%EventDef PajeSetState 101\n"
130 "% EntityType string\n"
131 "% Container string\n"
133 "% FileName string\n"
136 "%EventDef PajePushState 111\n"
138 "% EntityType string\n"
139 "% Container string\n"
141 "% FileName string\n"
144 "%EventDef PajePushState 11\n"
146 "% EntityType string\n"
147 "% Container string\n"
150 "%EventDef PajePopState 12\n"
152 "% EntityType string\n"
153 "% Container string\n"
155 "%EventDef PajeSetVariable 13\n"
157 "% EntityType string\n"
158 "% Container string\n"
161 "%EventDef PajeAddVariable 14\n"
163 "% EntityType string\n"
164 "% Container string\n"
167 "%EventDef PajeSubVariable 15\n"
169 "% EntityType string\n"
170 "% Container string\n"
173 "%EventDef PajeStartLink 16\n"
175 "% EntityType string\n"
176 "% Container string\n"
178 "% SourceContainer string\n"
181 "%EventDef PajeEndLink 17\n"
183 "% EntityType string\n"
184 "% Container string\n"
186 "% DestContainer string\n"
190 const char *type_definitions = "1 Sim_t 0 Simulation_t\n"
191 "1 H_t Sim_t m_host_t\n"
192 "1 P_t H_t m_process_t\n"
193 "3 S_t P_t \"Process State\"\n"
194 "6 E S_t Executing\n"
196 "6 C S_t Communicating\n"
197 "5 Comm Sim_t P_t P_t Communication_t\n";
199 const char *ext = ".trace";
200 int ext_len = strlen(ext);
204 xbt_fifo_item_t item = NULL;
206 xbt_assert0(msg_global, "Initialize MSG first\n");
207 xbt_assert0(!msg_global->paje_output, "Paje output already defined\n");
208 xbt_assert0(filename, "Need a real file name\n");
210 len = strlen(filename);
211 if((len<ext_len) || (strncmp(filename+len-ext_len,ext,ext_len))) {
212 CRITICAL2("%s does not end by \"%s\". It may cause troubles when using Paje\n",
216 msg_global->paje_output=fopen(filename,"w");
217 xbt_assert1(msg_global->paje_output, "Failed to open %s \n",filename);
219 fprintf(msg_global->paje_output,"%s",paje_preembule);
220 fprintf(msg_global->paje_output,"%s",type_definitions);
223 for(i=0; i<msg_global->max_channel; i++) {
224 fprintf(msg_global->paje_output, "6 COMM_%d Comm \"Channel %d\"\n" ,i,i);
226 fprintf(msg_global->paje_output,
227 "7 0.0 CUR Sim_t 0 \"MSG simulation\"\n");
230 xbt_fifo_foreach(msg_global->host,item,host,m_host_t) {
235 xbt_fifo_foreach(msg_global->process_list,item,process,m_process_t) {
236 PAJE_PROCESS_NEW(process);
240 /** @deprecated Defines the verbosity of the simulation.
241 * @ingroup msg_simulation
243 void MSG_set_verbosity(MSG_outputmode_t mode)
245 CRITICAL0("MSG_set_verbosity : Deprecated function. Use the XBT logging interface.");
248 /** \defgroup m_channel_management Understanding channels
249 * \brief This section briefly describes the channel notion of MSG
252 * For convenience, the simulator provides the notion of channel
253 * that is close to the tag notion in MPI. A channel is not a
254 * socket. It doesn't need to be opened neither closed. It rather
255 * corresponds to the ports opened on the different machines.
259 /** \ingroup m_channel_management
260 * \brief Set the number of channel in the simulation.
262 * This function has to be called to fix the number of channel in the
263 simulation before creating any host. Indeed, each channel is
264 represented by a different mailbox on each #m_host_t. This
265 function can then be called only once. This function takes only one
267 * \param number the number of channel in the simulation. It has to be >0
269 MSG_error_t MSG_set_channel_number(int number)
271 xbt_assert0((msg_global) && (msg_global->max_channel == 0), "Channel number already set!");
273 msg_global->max_channel = number;
278 /** \ingroup m_simulation
279 * \brief Set the sharing policy used for the links
281 * This function can be called to change the sharing policy used for the links
282 (see \ref paper_tcp). By default the store and forward mechanism is used
283 with a parameter equal to 0.1. This function has to be called before creating
285 * \param mode the sharing policy used for the links: can be either
286 MSG_STORE_AND_FORWARD or MSG_TCP.
287 * \param param a parameter for the sharing policy. It has to be >0. It is
288 currently used only for the MSG_STORE_AND_FORWARD flavor and represents the
289 granularity of the communications (i.e. the packet size).
291 MSG_error_t MSG_set_sharing_policy(MSG_sharing_t mode, double param)
293 CRITICAL0("MSG_set_sharing_policy: this function is now deprecated and useless. Store and forward does not exist anymore. Please stop using it.");
298 /** \ingroup m_channel_management
299 * \brief Return the number of channel in the simulation.
301 * This function has to be called once the number of channel is fixed. I can't
302 figure out a reason why anyone would like to call this function but nevermind.
303 * \return the number of channel in the simulation.
305 int MSG_get_channel_number(void)
307 xbt_assert0((msg_global)&&(msg_global->max_channel != 0), "Channel number not set yet!");
309 return msg_global->max_channel;
312 /** \ingroup msg_simulation
313 * \brief Launch the MSG simulation
315 MSG_error_t MSG_main(void)
317 m_process_t process = NULL;
319 double elapsed_time = 0.0;
321 /* Clean IO before the run */
325 surf_solve(); /* Takes traces into account. Returns 0.0 */
326 /* xbt_fifo_size(msg_global->process_to_run) */
328 xbt_context_empty_trash();
329 if(xbt_fifo_size(msg_global->process_to_run) && (elapsed_time>0)) {
330 DEBUG0("**************************************************");
332 if((__stop_at_time>0) && (MSG_getClock() >= __stop_at_time)) {
333 DEBUG0("Let's stop here!");
336 while ((process = xbt_fifo_pop(msg_global->process_to_run))) {
337 DEBUG3("Scheduling %s(%d) on %s",
338 process->name,process->simdata->PID,
339 process->simdata->host->name);
340 msg_global->current_process = process;
342 xbt_context_schedule(process->simdata->context);
343 msg_global->current_process = NULL;
345 DEBUG1("%lg : Calling surf_solve",MSG_getClock());
346 elapsed_time = surf_solve();
347 DEBUG1("Elapsed_time %lg",elapsed_time);
349 /* fprintf(stderr, "====== %lg =====\n",Now); */
350 /* if (elapsed_time==0.0) { */
351 /* fprintf(stderr, "No change in time\n"); */
353 if (elapsed_time<0.0) {
354 /* fprintf(stderr, "We're done %lg\n",elapsed_time); */
359 surf_action_t action = NULL;
360 surf_resource_t resource = NULL;
361 m_task_t task = NULL;
365 while (surf_timer_resource->extension_public->get(&fun,(void*)&arg)) {
366 DEBUG2("got %p %p", fun, arg);
367 if(fun==MSG_process_create_with_arguments) {
368 process_arg_t args = arg;
369 DEBUG2("Launching %s on %s", args->name, args->host->name);
370 process = MSG_process_create_with_arguments(args->name, args->code,
371 args->data, args->host,
372 args->argc,args->argv);
373 if(args->kill_time > MSG_getClock()) {
374 surf_timer_resource->extension_public->set(args->kill_time,
375 (void*) &MSG_process_kill,
380 if(fun==MSG_process_kill) {
382 DEBUG3("Killing %s(%d) on %s", process->name, process->simdata->PID,
383 process->simdata->host->name);
384 MSG_process_kill(process);
388 xbt_dynar_foreach(resource_list, i, resource) {
390 xbt_swag_extract(resource->common_public->states.
391 failed_action_set))) {
395 DEBUG1("** %s failed **",task->name);
396 xbt_dynar_foreach(task->simdata->sleeping,_cursor,process) {
397 DEBUG3("\t preparing to wake up %s(%d) on %s",
398 process->name,process->simdata->PID,
399 process->simdata->host->name);
400 xbt_fifo_unshift(msg_global->process_to_run, process);
406 xbt_swag_extract(resource->common_public->states.
411 DEBUG1("** %s done **",task->name);
412 xbt_dynar_foreach(task->simdata->sleeping,_cursor,process) {
413 DEBUG3("\t preparing to wake up %s(%d) on %s",
414 process->name,process->simdata->PID,
415 process->simdata->host->name);
416 xbt_fifo_unshift(msg_global->process_to_run, process);
425 if ((nbprocess=xbt_fifo_size(msg_global->process_list)) == 0) {
427 "MSG: Congratulations ! Simulation terminated : all process are over\n");
430 xbt_fifo_item_t item = NULL;
431 fprintf(stderr,"MSG: Oops ! Deadlock or code not perfectly clean.\n");
432 fprintf(stderr,"MSG: %d processes are still running, waiting for something.\n",
434 /* List the process and their state */
435 fprintf(stderr,"MSG: <process>(<pid>) on <host>: <status>.\n");
436 xbt_fifo_foreach(msg_global->process_list,item,process,m_process_t) {
437 simdata_process_t p_simdata = (simdata_process_t) process->simdata;
438 simdata_host_t h_simdata=(simdata_host_t)p_simdata->host->simdata;
441 fprintf(stderr,"MSG: %s(%d) on %s: ",
442 process->name,p_simdata->PID,
443 p_simdata->host->name);
445 if (process->simdata->blocked)
446 fprintf(stderr,"[blocked] ");
447 if (process->simdata->suspended)
448 fprintf(stderr,"[suspended] ");
450 for (i=0; i<msg_global->max_channel; i++) {
451 if (h_simdata->sleeping[i] == process) {
452 fprintf(stderr,"Listening on channel %d.\n",i);
456 if (i==msg_global->max_channel) {
457 if(p_simdata->waiting_task) {
458 if(p_simdata->waiting_task->simdata->compute) {
459 if(p_simdata->put_host)
460 fprintf(stderr,"Trying to send a task on Host %s, channel %d.\n",
461 p_simdata->put_host->name, p_simdata->put_channel);
463 fprintf(stderr,"Waiting for %s to finish.\n",p_simdata->waiting_task->name);
464 } else if (p_simdata->waiting_task->simdata->comm)
465 fprintf(stderr,"Waiting for %s to be finished transfered.\n",
466 p_simdata->waiting_task->name);
468 fprintf(stderr,"UNKNOWN STATUS. Please report this bug.\n");
470 else { /* Must be trying to put a task somewhere */
471 fprintf(stderr,"UNKNOWN STATUS. Please report this bug.\n");
475 if(XBT_LOG_ISENABLED(msg, xbt_log_priority_debug) ||
476 XBT_LOG_ISENABLED(global, xbt_log_priority_debug)) {
485 /* static void MarkAsFailed(m_task_t t, TBX_HashTable_t failedProcessList) */
487 /* simdata_task_t simdata = NULL; */
488 /* xbt_fifo_item_t i = NULL; */
489 /* m_process_t p = NULL; */
491 /* xbt_assert0((t!=NULL),"Invalid task"); */
492 /* simdata = t->simdata; */
494 /* #define KILL(task) if(task) SG_failTask(task) */
495 /* KILL(simdata->compute); */
496 /* KILL(simdata->TCP_comm); */
497 /* KILL(simdata->s[0]); */
498 /* KILL(simdata->s[1]); */
499 /* KILL(simdata->s[2]); */
500 /* KILL(simdata->s[3]); */
501 /* KILL(simdata->sleep); */
503 /* /\* if(simdata->comm) SG_failEndToEndTransfer(simdata->comm); *\/ */
505 /* xbt_fifo_foreach(simdata->sleeping,i,p,m_process_t) { */
506 /* if(!TBX_HashTable_isInList(failedProcessList,p,TBX_basicHash)) */
507 /* TBX_HashTable_insert(failedProcessList,p,TBX_basicHash); */
512 /* static xbt_fifo_t MSG_buildFailedHostList(double begin, double end) */
514 /* xbt_fifo_t failedHostList = xbt_fifo_new(); */
515 /* m_host_t host = NULL; */
516 /* xbt_fifo_item_t i; */
518 /* xbt_fifo_foreach(msg_global->host,i,host,m_host_t) { */
519 /* SG_Resource r= ((simdata_host_t) (host->simdata))->host; */
521 /* if(SG_evaluateFailureTrace(r->failure_trace,begin,end)!=-1.0) */
522 /* xbt_fifo_insert(failedHostList,host); */
525 /* return failedHostList; */
528 /** \ingroup msg_simulation
529 * \brief Kill all running process
531 * \param reset_PIDs should we reset the PID numbers. A negative
532 * number means no reset and a positive number will be used to set the PID
533 * of the next newly created process.
535 int MSG_process_killall(int reset_PIDs)
537 xbt_fifo_item_t i = NULL;
538 m_process_t p = NULL;
539 m_process_t self = MSG_process_self();
541 while((p=xbt_fifo_shift(msg_global->process_list))) {
542 if(p!=self) MSG_process_kill(p);
546 msg_global->PID = reset_PIDs;
547 msg_global->session++;
550 xbt_context_empty_trash();
556 return msg_global->PID;
559 /** \ingroup msg_simulation
560 * \brief Clean the MSG simulation
562 MSG_error_t MSG_clean(void)
564 xbt_fifo_item_t i = NULL;
566 m_process_t p = NULL;
569 while((p=xbt_fifo_shift(msg_global->process_list))) {
574 xbt_fifo_foreach(msg_global->host,i,h,m_host_t) {
575 __MSG_host_destroy(h);
577 xbt_fifo_free(msg_global->host);
578 xbt_fifo_free(msg_global->process_to_run);
579 xbt_fifo_free(msg_global->process_list);
580 xbt_dict_free(&(msg_global->registered_functions));
582 if(msg_global->paje_output) {
583 fclose(msg_global->paje_output);
584 msg_global->paje_output = NULL;
593 /** \ingroup msg_easier_life
594 * \brief A clock (in second).
595 * \deprecated Use MSG_get_clock
597 double MSG_getClock(void) {
598 return surf_get_clock();
601 /** \ingroup msg_easier_life
602 * \brief A clock (in second).
604 double MSG_get_clock(void) {
605 return surf_get_clock();