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;
320 int state_modifications = 1;
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;
347 surf_action_t action = NULL;
348 surf_resource_t resource = NULL;
349 m_task_t task = NULL;
354 xbt_dynar_foreach(resource_list, i, resource) {
355 if(xbt_swag_size(resource->common_public->states.failed_action_set) ||
356 xbt_swag_size(resource->common_public->states.done_action_set))
357 state_modifications = 1;
360 if(!state_modifications) {
361 DEBUG1("%g : Calling surf_solve",MSG_getClock());
362 elapsed_time = surf_solve();
363 DEBUG1("Elapsed_time %g",elapsed_time);
365 if (elapsed_time<0.0) {
366 /* fprintf(stderr, "We're done %g\n",elapsed_time); */
371 while (surf_timer_resource->extension_public->get(&fun,(void*)&arg)) {
372 DEBUG2("got %p %p", fun, arg);
373 if(fun==MSG_process_create_with_arguments) {
374 process_arg_t args = arg;
375 DEBUG2("Launching %s on %s", args->name, args->host->name);
376 process = MSG_process_create_with_arguments(args->name, args->code,
377 args->data, args->host,
378 args->argc,args->argv);
379 if(args->kill_time > MSG_getClock()) {
380 surf_timer_resource->extension_public->set(args->kill_time,
381 (void*) &MSG_process_kill,
386 if(fun==MSG_process_kill) {
388 DEBUG3("Killing %s(%d) on %s", process->name, process->simdata->PID,
389 process->simdata->host->name);
390 MSG_process_kill(process);
394 xbt_dynar_foreach(resource_list, i, resource) {
396 xbt_swag_extract(resource->common_public->states.
397 failed_action_set))) {
401 DEBUG1("** %s failed **",task->name);
402 xbt_dynar_foreach(task->simdata->sleeping,_cursor,process) {
403 DEBUG3("\t preparing to wake up %s(%d) on %s",
404 process->name,process->simdata->PID,
405 process->simdata->host->name);
406 xbt_fifo_unshift(msg_global->process_to_run, process);
412 xbt_swag_extract(resource->common_public->states.
417 DEBUG1("** %s done **",task->name);
418 xbt_dynar_foreach(task->simdata->sleeping,_cursor,process) {
419 DEBUG3("\t preparing to wake up %s(%d) on %s",
420 process->name,process->simdata->PID,
421 process->simdata->host->name);
422 xbt_fifo_unshift(msg_global->process_to_run, process);
429 state_modifications = 0;
432 if ((nbprocess=xbt_fifo_size(msg_global->process_list)) == 0) {
434 "MSG: Congratulations ! Simulation terminated : all process are over\n");
437 xbt_fifo_item_t item = NULL;
438 fprintf(stderr,"MSG: Oops ! Deadlock or code not perfectly clean.\n");
439 fprintf(stderr,"MSG: %d processes are still running, waiting for something.\n",
441 /* List the process and their state */
442 fprintf(stderr,"MSG: <process>(<pid>) on <host>: <status>.\n");
443 xbt_fifo_foreach(msg_global->process_list,item,process,m_process_t) {
444 simdata_process_t p_simdata = (simdata_process_t) process->simdata;
445 simdata_host_t h_simdata=(simdata_host_t)p_simdata->host->simdata;
448 fprintf(stderr,"MSG: %s(%d) on %s: ",
449 process->name,p_simdata->PID,
450 p_simdata->host->name);
452 if (process->simdata->blocked)
453 fprintf(stderr,"[blocked] ");
454 if (process->simdata->suspended)
455 fprintf(stderr,"[suspended] ");
457 for (i=0; i<msg_global->max_channel; i++) {
458 if (h_simdata->sleeping[i] == process) {
459 fprintf(stderr,"Listening on channel %d.\n",i);
463 if (i==msg_global->max_channel) {
464 if(p_simdata->waiting_task) {
465 if(p_simdata->waiting_task->simdata->compute) {
466 if(p_simdata->put_host)
467 fprintf(stderr,"Trying to send a task to Host %s, channel %d.\n",
468 p_simdata->put_host->name, p_simdata->put_channel);
470 fprintf(stderr,"Waiting for %s to finish.\n",p_simdata->waiting_task->name);
471 } else if (p_simdata->waiting_task->simdata->comm)
472 fprintf(stderr,"Waiting for %s to be finished transfered.\n",
473 p_simdata->waiting_task->name);
475 fprintf(stderr,"UNKNOWN STATUS. Please report this bug.\n");
477 else { /* Must be trying to put a task somewhere */
478 fprintf(stderr,"UNKNOWN STATUS. Please report this bug.\n");
482 if(XBT_LOG_ISENABLED(msg, xbt_log_priority_debug) ||
483 XBT_LOG_ISENABLED(global, xbt_log_priority_debug)) {
492 /** \ingroup msg_simulation
493 * \brief Kill all running process
495 * \param reset_PIDs should we reset the PID numbers. A negative
496 * number means no reset and a positive number will be used to set the PID
497 * of the next newly created process.
499 int MSG_process_killall(int reset_PIDs)
501 m_process_t p = NULL;
502 m_process_t self = MSG_process_self();
504 while((p=xbt_fifo_shift(msg_global->process_list))) {
505 if(p!=self) MSG_process_kill(p);
509 msg_global->PID = reset_PIDs;
510 msg_global->session++;
513 xbt_context_empty_trash();
519 return msg_global->PID;
522 /** \ingroup msg_simulation
523 * \brief Clean the MSG simulation
525 MSG_error_t MSG_clean(void)
527 xbt_fifo_item_t i = NULL;
529 m_process_t p = NULL;
532 while((p=xbt_fifo_shift(msg_global->process_list))) {
537 xbt_fifo_foreach(msg_global->host,i,h,m_host_t) {
538 __MSG_host_destroy(h);
540 xbt_fifo_free(msg_global->host);
541 xbt_fifo_free(msg_global->process_to_run);
542 xbt_fifo_free(msg_global->process_list);
543 xbt_dict_free(&(msg_global->registered_functions));
545 if(msg_global->paje_output) {
546 fclose(msg_global->paje_output);
547 msg_global->paje_output = NULL;
556 /** \ingroup msg_easier_life
557 * \brief A clock (in second).
558 * \deprecated Use MSG_get_clock
560 double MSG_getClock(void) {
561 return surf_get_clock();
564 /** \ingroup msg_easier_life
565 * \brief A clock (in second).
567 double MSG_get_clock(void) {
568 return surf_get_clock();