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(double a, double b); */
19 /** \defgroup msg_simulation MSG simulation Functions
20 * \brief This section describes the functions you need to know to
21 * set up a simulation. You should have a look at \ref MSG_examples
22 * to have an overview of their usage.
25 /********************************* MSG **************************************/
27 /** \ingroup msg_simulation
28 * \brief Initialize some MSG internal data.
30 void MSG_global_init(void)
35 CRITICAL0("Function MSG_global_init() is deprecated by MSG_global_init_args().");
36 MSG_global_init_args(&argc,argv);
39 /** \ingroup msg_simulation
40 * \brief Initialize some MSG internal data.
42 void MSG_global_init_args(int *argc, char **argv)
45 surf_init(argc, argv); /* Initialize some common structures. Warning, it sets msg_global=NULL */
47 msg_global = xbt_new0(s_MSG_Global_t,1);
50 msg_global->host = xbt_fifo_new();
51 msg_global->process_to_run = xbt_fifo_new();
52 msg_global->process_list = xbt_fifo_new();
53 msg_global->max_channel = 0;
54 msg_global->current_process = NULL;
55 msg_global->registered_functions = xbt_dict_new();
60 /** \ingroup msg_easier_life
61 * \brief Traces MSG events in the Paje format.
63 void MSG_paje_output(const char *filename)
66 const char *paje_preembule="%EventDef SetLimits 0\n"
70 "%EventDef PajeDefineContainerType 1\n"
72 "% ContainerType string\n"
75 "%EventDef PajeDefineEventType 2\n"
77 "% ContainerType string\n"
80 "%EventDef PajeDefineStateType 3\n"
82 "% ContainerType string\n"
85 "%EventDef PajeDefineVariableType 4\n"
87 "% ContainerType string\n"
90 "%EventDef PajeDefineLinkType 5\n"
92 "% ContainerType string\n"
93 "% SourceContainerType string\n"
94 "% DestContainerType string\n"
97 "%EventDef PajeDefineEntityValue 6\n"
99 "% EntityType string\n"
102 "%EventDef PajeCreateContainer 7\n"
104 "% NewContainer string\n"
105 "% NewContainerType string\n"
106 "% Container string\n"
109 "%EventDef PajeDestroyContainer 8\n"
114 "%EventDef PajeNewEvent 9\n"
116 "% EntityType string\n"
117 "% Container string\n"
120 "%EventDef PajeSetState 10\n"
122 "% EntityType string\n"
123 "% Container string\n"
126 "%EventDef PajeSetState 101\n"
128 "% EntityType string\n"
129 "% Container string\n"
131 "% FileName string\n"
134 "%EventDef PajePushState 111\n"
136 "% EntityType string\n"
137 "% Container string\n"
139 "% FileName string\n"
142 "%EventDef PajePushState 11\n"
144 "% EntityType string\n"
145 "% Container 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("%s does not end by \"%s\". It may cause troubles when using Paje\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 /** \ingroup msg_simulation
239 * \brief Defines the verbosity of the simulation.
241 void MSG_set_verbosity(MSG_outputmode_t mode)
243 CRITICAL0("MSG_set_verbosity : Deprecated function. Use the XBT logging interface.");
246 /** \defgroup m_channel_management Understanding channels
247 * \brief This section briefly describes the channel notion of MSG
250 * For convenience, the simulator provides the notion of channel
251 * that is close to the tag notion in MPI. A channel is not a
252 * socket. It doesn't need to be opened neither closed. It rather
253 * corresponds to the ports opened on the different machines.
257 /** \ingroup m_channel_management
258 * \brief Set the number of channel in the simulation.
260 * This function has to be called to fix the number of channel in the
261 simulation before creating any host. Indeed, each channel is
262 represented by a different mailbox on each #m_host_t. This
263 function can then be called only once. This function takes only one
265 * \param number the number of channel in the simulation. It has to be >0
267 MSG_error_t MSG_set_channel_number(int number)
269 xbt_assert0((msg_global) && (msg_global->max_channel == 0), "Channel number already set!");
271 msg_global->max_channel = number;
276 /** \ingroup m_simulation
277 * \brief Set the sharing policy used for the links
279 * This function can be called to change the sharing policy used for the links
280 (see \ref paper_tcp). By default the store and forward mechanism is used
281 with a parameter equal to 0.1. This function has to be called before creating
283 * \param mode the sharing policy used for the links: can be either
284 MSG_STORE_AND_FORWARD or MSG_TCP.
285 * \param param a parameter for the sharing policy. It has to be >0. It is
286 currently used only for the MSG_STORE_AND_FORWARD flavor and represents the
287 granularity of the communications (i.e. the packet size).
289 MSG_error_t MSG_set_sharing_policy(MSG_sharing_t mode, double param)
291 CRITICAL0("MSG_set_sharing_policy: this function is now deprecated and useless. Store and forward does not exist anymore. Please stop using it.");
296 /** \ingroup m_channel_management
297 * \brief Return the number of channel in the simulation.
299 * This function has to be called once the number of channel is fixed. I can't
300 figure out a reason why anyone would like to call this function but nevermind.
301 * \return the number of channel in the simulation.
303 int MSG_get_channel_number(void)
305 xbt_assert0((msg_global)&&(msg_global->max_channel != 0), "Channel number not set yet!");
307 return msg_global->max_channel;
310 /** \ingroup msg_simulation
311 * \brief Launch the MSG simulation
313 MSG_error_t MSG_main(void)
315 m_process_t process = NULL;
317 double elapsed_time = 0.0;
319 /* Clean IO before the run */
323 surf_solve(); /* Takes traces into account. Returns 0.0 */
324 /* xbt_fifo_size(msg_global->process_to_run) */
326 xbt_context_empty_trash();
327 while ((process = xbt_fifo_pop(msg_global->process_to_run))) {
328 DEBUG3("Scheduling %s(%d) on %s",
329 process->name,process->simdata->PID,
330 process->simdata->host->name);
331 msg_global->current_process = process;
333 xbt_context_schedule(process->simdata->context);
334 msg_global->current_process = NULL;
336 DEBUG1("%lg : Calling surf_solve",MSG_getClock());
337 elapsed_time = surf_solve();
338 DEBUG1("Elapsed_time %lg",elapsed_time);
340 /* fprintf(stderr, "====== %lg =====\n",Now); */
341 /* if (elapsed_time==0.0) { */
342 /* fprintf(stderr, "No change in time\n"); */
344 if (elapsed_time<0.0) {
345 /* fprintf(stderr, "We're done %lg\n",elapsed_time); */
350 surf_action_t action = NULL;
351 surf_resource_t resource = NULL;
352 m_task_t task = NULL;
356 while (surf_timer_resource->extension_public->get(&fun,(void*)&arg)) {
357 DEBUG2("got %p %p", fun, arg);
358 if(fun==MSG_process_create_with_arguments) {
359 process_arg_t args = arg;
360 DEBUG2("Launching %s on %s", args->name, args->host->name);
361 process = MSG_process_create_with_arguments(args->name, args->code,
362 args->data, args->host,
363 args->argc,args->argv);
364 if(args->kill_time > MSG_getClock()) {
365 surf_timer_resource->extension_public->set(args->kill_time,
366 (void*) &MSG_process_kill,
371 if(fun==MSG_process_kill) {
373 DEBUG3("Killing %s(%d) on %s", process->name, process->simdata->PID,
374 process->simdata->host->name);
375 MSG_process_kill(process);
379 xbt_dynar_foreach(resource_list, i, resource) {
381 xbt_swag_extract(resource->common_public->states.
382 failed_action_set))) {
386 DEBUG1("** %s failed **",task->name);
387 xbt_dynar_foreach(task->simdata->sleeping,_cursor,process) {
388 DEBUG3("\t preparing to wake up %s(%d) on %s",
389 process->name,process->simdata->PID,
390 process->simdata->host->name);
391 xbt_fifo_unshift(msg_global->process_to_run, process);
397 xbt_swag_extract(resource->common_public->states.
402 DEBUG1("** %s done **",task->name);
403 xbt_dynar_foreach(task->simdata->sleeping,_cursor,process) {
404 DEBUG3("\t preparing to wake up %s(%d) on %s",
405 process->name,process->simdata->PID,
406 process->simdata->host->name);
407 xbt_fifo_unshift(msg_global->process_to_run, process);
416 if ((nbprocess=xbt_fifo_size(msg_global->process_list)) == 0) {
418 "MSG: Congratulations ! Simulation terminated : all process are over\n");
421 xbt_fifo_item_t item = NULL;
422 fprintf(stderr,"MSG: Oops ! Deadlock or code not perfectly clean.\n");
423 fprintf(stderr,"MSG: %d processes are still running, waiting for something.\n",
425 /* List the process and their state */
426 fprintf(stderr,"MSG: <process>(<pid>) on <host>: <status>.\n");
427 xbt_fifo_foreach(msg_global->process_list,item,process,m_process_t) {
428 simdata_process_t p_simdata = (simdata_process_t) process->simdata;
429 simdata_host_t h_simdata=(simdata_host_t)p_simdata->host->simdata;
432 fprintf(stderr,"MSG: %s(%d) on %s: ",
433 process->name,p_simdata->PID,
434 p_simdata->host->name);
436 if (process->simdata->blocked)
437 fprintf(stderr,"[blocked] ");
438 if (process->simdata->suspended)
439 fprintf(stderr,"[suspended] ");
441 for (i=0; i<msg_global->max_channel; i++) {
442 if (h_simdata->sleeping[i] == process) {
443 fprintf(stderr,"Listening on channel %d.\n",i);
447 if (i==msg_global->max_channel) {
448 if(p_simdata->waiting_task) {
449 if(p_simdata->waiting_task->simdata->compute) {
450 if(p_simdata->put_host)
451 fprintf(stderr,"Trying to send a task on Host %s, channel %d.\n",
452 p_simdata->put_host->name, p_simdata->put_channel);
454 fprintf(stderr,"Waiting for %s to finish.\n",p_simdata->waiting_task->name);
455 } else if (p_simdata->waiting_task->simdata->comm)
456 fprintf(stderr,"Waiting for %s to be finished transfered.\n",
457 p_simdata->waiting_task->name);
459 fprintf(stderr,"UNKNOWN STATUS. Please report this bug.\n");
461 else { /* Must be trying to put a task somewhere */
462 fprintf(stderr,"UNKNOWN STATUS. Please report this bug.\n");
466 if(XBT_LOG_ISENABLED(msg, xbt_log_priority_debug) ||
467 XBT_LOG_ISENABLED(global, xbt_log_priority_debug)) {
476 /* static void MarkAsFailed(m_task_t t, TBX_HashTable_t failedProcessList) */
478 /* simdata_task_t simdata = NULL; */
479 /* xbt_fifo_item_t i = NULL; */
480 /* m_process_t p = NULL; */
482 /* xbt_assert0((t!=NULL),"Invalid task"); */
483 /* simdata = t->simdata; */
485 /* #define KILL(task) if(task) SG_failTask(task) */
486 /* KILL(simdata->compute); */
487 /* KILL(simdata->TCP_comm); */
488 /* KILL(simdata->s[0]); */
489 /* KILL(simdata->s[1]); */
490 /* KILL(simdata->s[2]); */
491 /* KILL(simdata->s[3]); */
492 /* KILL(simdata->sleep); */
494 /* /\* if(simdata->comm) SG_failEndToEndTransfer(simdata->comm); *\/ */
496 /* xbt_fifo_foreach(simdata->sleeping,i,p,m_process_t) { */
497 /* if(!TBX_HashTable_isInList(failedProcessList,p,TBX_basicHash)) */
498 /* TBX_HashTable_insert(failedProcessList,p,TBX_basicHash); */
503 /* static xbt_fifo_t MSG_buildFailedHostList(double begin, double end) */
505 /* xbt_fifo_t failedHostList = xbt_fifo_new(); */
506 /* m_host_t host = NULL; */
507 /* xbt_fifo_item_t i; */
509 /* xbt_fifo_foreach(msg_global->host,i,host,m_host_t) { */
510 /* SG_Resource r= ((simdata_host_t) (host->simdata))->host; */
512 /* if(SG_evaluateFailureTrace(r->failure_trace,begin,end)!=-1.0) */
513 /* xbt_fifo_insert(failedHostList,host); */
516 /* return failedHostList; */
519 /** \ingroup msg_simulation
520 * \brief Kill all running process
522 * \param reset_PIDs should we reset the PID numbers. A negative
523 * number means no reset and a positive number will be used to set the PID
524 * of the next newly created process.
526 int MSG_process_killall(int reset_PIDs)
528 xbt_fifo_item_t i = NULL;
529 m_process_t p = NULL;
530 m_process_t self = MSG_process_self();
532 while((p=xbt_fifo_shift(msg_global->process_list))) {
533 if(p!=self) MSG_process_kill(p);
537 msg_global->PID = reset_PIDs;
538 msg_global->session++;
541 xbt_context_empty_trash();
547 return msg_global->PID;
550 /** \ingroup msg_simulation
551 * \brief Clean the MSG simulation
553 MSG_error_t MSG_clean(void)
555 xbt_fifo_item_t i = NULL;
557 m_process_t p = NULL;
560 while((p=xbt_fifo_shift(msg_global->process_list))) {
565 xbt_fifo_foreach(msg_global->host,i,h,m_host_t) {
566 __MSG_host_destroy(h);
568 xbt_fifo_free(msg_global->host);
569 xbt_fifo_free(msg_global->process_to_run);
570 xbt_fifo_free(msg_global->process_list);
571 xbt_dict_free(&(msg_global->registered_functions));
573 if(msg_global->paje_output) {
574 fclose(msg_global->paje_output);
575 msg_global->paje_output = NULL;
584 /** \ingroup msg_easier_life
585 * \brief A clock (in second).
586 * \deprecated Use MSG_get_clock
588 double MSG_getClock(void) {
589 return surf_get_clock();
592 /** \ingroup msg_easier_life
593 * \brief A clock (in second).
595 double MSG_get_clock(void) {
596 return surf_get_clock();