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.
27 /********************************* MSG **************************************/
29 /** \ingroup msg_simulation
30 * \brief Initialize some MSG internal data.
32 void MSG_global_init_args(int *argc, char **argv)
34 MSG_global_init(argc,argv);
37 /** \ingroup msg_simulation
38 * \brief Initialize some MSG internal data.
40 void MSG_global_init(int *argc, char **argv)
43 surf_init(argc, argv); /* Initialize some common structures. Warning, it sets msg_global=NULL */
45 msg_global = xbt_new0(s_MSG_Global_t,1);
48 msg_global->host = xbt_fifo_new();
49 msg_global->process_to_run = xbt_fifo_new();
50 msg_global->process_list = xbt_fifo_new();
51 msg_global->max_channel = 0;
52 msg_global->current_process = NULL;
53 msg_global->registered_functions = xbt_dict_new();
58 /** \ingroup msg_easier_life
59 * \brief Traces MSG events in the Paje format.
61 void MSG_paje_output(const char *filename)
64 const char *paje_preembule="%EventDef SetLimits 0\n"
68 "%EventDef PajeDefineContainerType 1\n"
70 "% ContainerType string\n"
73 "%EventDef PajeDefineEventType 2\n"
75 "% ContainerType string\n"
78 "%EventDef PajeDefineStateType 3\n"
80 "% ContainerType string\n"
83 "%EventDef PajeDefineVariableType 4\n"
85 "% ContainerType string\n"
88 "%EventDef PajeDefineLinkType 5\n"
90 "% ContainerType string\n"
91 "% SourceContainerType string\n"
92 "% DestContainerType string\n"
95 "%EventDef PajeDefineEntityValue 6\n"
97 "% EntityType string\n"
100 "%EventDef PajeCreateContainer 7\n"
102 "% NewContainer string\n"
103 "% NewContainerType string\n"
104 "% Container string\n"
107 "%EventDef PajeDestroyContainer 8\n"
112 "%EventDef PajeNewEvent 9\n"
114 "% EntityType string\n"
115 "% Container string\n"
118 "%EventDef PajeSetState 10\n"
120 "% EntityType string\n"
121 "% Container string\n"
124 "%EventDef PajeSetState 101\n"
126 "% EntityType string\n"
127 "% Container string\n"
129 "% FileName string\n"
132 "%EventDef PajePushState 111\n"
134 "% EntityType string\n"
135 "% Container string\n"
137 "% FileName string\n"
140 "%EventDef PajePushState 11\n"
142 "% EntityType string\n"
143 "% Container string\n"
146 "%EventDef PajePopState 12\n"
148 "% EntityType string\n"
149 "% Container string\n"
151 "%EventDef PajeSetVariable 13\n"
153 "% EntityType string\n"
154 "% Container string\n"
157 "%EventDef PajeAddVariable 14\n"
159 "% EntityType string\n"
160 "% Container string\n"
163 "%EventDef PajeSubVariable 15\n"
165 "% EntityType string\n"
166 "% Container string\n"
169 "%EventDef PajeStartLink 16\n"
171 "% EntityType string\n"
172 "% Container string\n"
174 "% SourceContainer string\n"
177 "%EventDef PajeEndLink 17\n"
179 "% EntityType string\n"
180 "% Container string\n"
182 "% DestContainer string\n"
186 const char *type_definitions = "1 Sim_t 0 Simulation_t\n"
187 "1 H_t Sim_t m_host_t\n"
188 "1 P_t H_t m_process_t\n"
189 "3 S_t P_t \"Process State\"\n"
190 "6 E S_t Executing\n"
192 "6 C S_t Communicating\n"
193 "5 Comm Sim_t P_t P_t Communication_t\n";
195 const char *ext = ".trace";
196 int ext_len = strlen(ext);
200 xbt_fifo_item_t item = NULL;
202 xbt_assert0(msg_global, "Initialize MSG first\n");
203 xbt_assert0(!msg_global->paje_output, "Paje output already defined\n");
204 xbt_assert0(filename, "Need a real file name\n");
206 len = strlen(filename);
207 if((len<ext_len) || (strncmp(filename+len-ext_len,ext,ext_len))) {
208 CRITICAL2("The name of the Paje trace file \"%s\" does not end by \"%s\". Paje will cause difficulties to read it.\n",
212 msg_global->paje_output=fopen(filename,"w");
213 xbt_assert1(msg_global->paje_output, "Failed to open %s \n",filename);
215 fprintf(msg_global->paje_output,"%s",paje_preembule);
216 fprintf(msg_global->paje_output,"%s",type_definitions);
219 for(i=0; i<msg_global->max_channel; i++) {
220 fprintf(msg_global->paje_output, "6 COMM_%d Comm \"Channel %d\"\n" ,i,i);
222 fprintf(msg_global->paje_output,
223 "7 0.0 CUR Sim_t 0 \"MSG simulation\"\n");
226 xbt_fifo_foreach(msg_global->host,item,host,m_host_t) {
231 xbt_fifo_foreach(msg_global->process_list,item,process,m_process_t) {
232 PAJE_PROCESS_NEW(process);
236 /** \defgroup m_channel_management Understanding channels
237 * \brief This section briefly describes the channel notion of MSG
240 * For convenience, the simulator provides the notion of channel
241 * that is close to the tag notion in MPI. A channel is not a
242 * socket. It doesn't need to be opened neither closed. It rather
243 * corresponds to the ports opened on the different machines.
247 /** \ingroup m_channel_management
248 * \brief Set the number of channel in the simulation.
250 * This function has to be called to fix the number of channel in the
251 simulation before creating any host. Indeed, each channel is
252 represented by a different mailbox on each #m_host_t. This
253 function can then be called only once. This function takes only one
255 * \param number the number of channel in the simulation. It has to be >0
257 MSG_error_t MSG_set_channel_number(int number)
259 xbt_assert0((msg_global) && (msg_global->max_channel == 0), "Channel number already set!");
261 msg_global->max_channel = number;
266 /** \ingroup m_channel_management
267 * \brief Return the number of channel in the simulation.
269 * This function has to be called once the number of channel is fixed. I can't
270 figure out a reason why anyone would like to call this function but nevermind.
271 * \return the number of channel in the simulation.
273 int MSG_get_channel_number(void)
275 xbt_assert0((msg_global)&&(msg_global->max_channel != 0), "Channel number not set yet!");
277 return msg_global->max_channel;
280 /** \ingroup msg_simulation
281 * \brief Launch the MSG simulation
283 MSG_error_t MSG_main(void)
285 m_process_t process = NULL;
287 double elapsed_time = 0.0;
288 int state_modifications = 1;
289 /* Clean IO before the run */
293 surf_solve(); /* Takes traces into account. Returns 0.0 */
294 /* xbt_fifo_size(msg_global->process_to_run) */
296 xbt_context_empty_trash();
297 if(xbt_fifo_size(msg_global->process_to_run) && (elapsed_time>0)) {
298 DEBUG0("**************************************************");
300 if((__stop_at_time>0) && (MSG_get_clock() >= __stop_at_time)) {
301 DEBUG0("Let's stop here!");
304 while ((process = xbt_fifo_pop(msg_global->process_to_run))) {
305 DEBUG3("Scheduling %s(%d) on %s",
306 process->name,process->simdata->PID,
307 process->simdata->host->name);
308 msg_global->current_process = process;
310 xbt_context_schedule(process->simdata->context);
311 msg_global->current_process = NULL;
315 surf_action_t action = NULL;
316 surf_resource_t resource = NULL;
317 m_task_t task = NULL;
322 xbt_dynar_foreach(resource_list, i, resource) {
323 if(xbt_swag_size(resource->common_public->states.failed_action_set) ||
324 xbt_swag_size(resource->common_public->states.done_action_set))
325 state_modifications = 1;
328 if(!state_modifications) {
329 DEBUG1("%g : Calling surf_solve",MSG_get_clock());
330 elapsed_time = surf_solve();
331 DEBUG1("Elapsed_time %g",elapsed_time);
333 if (elapsed_time<0.0) {
334 /* fprintf(stderr, "We're done %g\n",elapsed_time); */
339 while (surf_timer_resource->extension_public->get(&fun,(void*)&arg)) {
340 DEBUG2("got %p %p", fun, arg);
341 if(fun==MSG_process_create_with_arguments) {
342 process_arg_t args = arg;
343 DEBUG2("Launching %s on %s", args->name, args->host->name);
344 process = MSG_process_create_with_arguments(args->name, args->code,
345 args->data, args->host,
346 args->argc,args->argv);
347 if(args->kill_time > MSG_get_clock()) {
348 surf_timer_resource->extension_public->set(args->kill_time,
349 (void*) &MSG_process_kill,
354 if(fun==MSG_process_kill) {
356 DEBUG3("Killing %s(%d) on %s", process->name, process->simdata->PID,
357 process->simdata->host->name);
358 MSG_process_kill(process);
362 xbt_dynar_foreach(resource_list, i, resource) {
364 xbt_swag_extract(resource->common_public->states.
365 failed_action_set))) {
369 DEBUG1("** %s failed **",task->name);
370 xbt_dynar_foreach(task->simdata->sleeping,_cursor,process) {
371 DEBUG3("\t preparing to wake up %s(%d) on %s",
372 process->name,process->simdata->PID,
373 process->simdata->host->name);
374 xbt_fifo_unshift(msg_global->process_to_run, process);
380 xbt_swag_extract(resource->common_public->states.
385 DEBUG1("** %s done **",task->name);
386 xbt_dynar_foreach(task->simdata->sleeping,_cursor,process) {
387 DEBUG3("\t preparing to wake up %s(%d) on %s",
388 process->name,process->simdata->PID,
389 process->simdata->host->name);
390 xbt_fifo_unshift(msg_global->process_to_run, process);
397 state_modifications = 0;
400 if ((nbprocess=xbt_fifo_size(msg_global->process_list)) == 0) {
401 INFO0("Congratulations ! Simulation terminated : all process are over");
404 xbt_fifo_item_t item = NULL;
405 INFO0("Oops ! Deadlock or code not perfectly clean.");
406 INFO1("MSG: %d processes are still running, waiting for something.",
408 /* List the process and their state */
409 INFO0("MSG: <process>(<pid>) on <host>: <status>.");
410 xbt_fifo_foreach(msg_global->process_list,item,process,m_process_t) {
411 simdata_process_t p_simdata = (simdata_process_t) process->simdata;
412 simdata_host_t h_simdata=(simdata_host_t)p_simdata->host->simdata;
415 INFO4("MSG: %s(%d) on %s: %s",
416 process->name,p_simdata->PID,
417 p_simdata->host->name,
418 (process->simdata->blocked)?"[blocked] "
419 :((process->simdata->suspended)?"[suspended] ":""));
421 for (i=0; i<msg_global->max_channel; i++) {
422 if (h_simdata->sleeping[i] == process) {
423 INFO1("\tListening on channel %d.",i);
427 if (i==msg_global->max_channel) {
428 if(p_simdata->waiting_task) {
429 if(p_simdata->waiting_task->simdata->compute) {
430 if(p_simdata->put_host)
431 INFO2("\tTrying to send a task to Host %s, channel %d.",
432 p_simdata->put_host->name, p_simdata->put_channel);
434 INFO1("Waiting for %s to finish.",p_simdata->waiting_task->name);
435 } else if (p_simdata->waiting_task->simdata->comm)
436 INFO1("Waiting for %s to be finished transfered.",
437 p_simdata->waiting_task->name);
439 INFO0("UNKNOWN STATUS. Please report this bug.");
441 else { /* Must be trying to put a task somewhere */
442 INFO0("UNKNOWN STATUS. Please report this bug.");
446 if(XBT_LOG_ISENABLED(msg, xbt_log_priority_debug) ||
447 XBT_LOG_ISENABLED(global, xbt_log_priority_debug)) {
452 INFO0("Return a Warning.");
457 /** \ingroup msg_simulation
458 * \brief Kill all running process
460 * \param reset_PIDs should we reset the PID numbers. A negative
461 * number means no reset and a positive number will be used to set the PID
462 * of the next newly created process.
464 int MSG_process_killall(int reset_PIDs)
466 m_process_t p = NULL;
467 m_process_t self = MSG_process_self();
469 while((p=xbt_fifo_pop(msg_global->process_list))) {
470 if(p!=self) MSG_process_kill(p);
474 msg_global->PID = reset_PIDs;
475 msg_global->session++;
478 xbt_context_empty_trash();
484 return msg_global->PID;
487 /** \ingroup msg_simulation
488 * \brief Clean the MSG simulation
490 MSG_error_t MSG_clean(void)
492 xbt_fifo_item_t i = NULL;
494 m_process_t p = NULL;
497 while((p=xbt_fifo_pop(msg_global->process_list))) {
502 xbt_fifo_foreach(msg_global->host,i,h,m_host_t) {
503 __MSG_host_destroy(h);
505 xbt_fifo_free(msg_global->host);
506 xbt_fifo_free(msg_global->process_to_run);
507 xbt_fifo_free(msg_global->process_list);
508 xbt_dict_free(&(msg_global->registered_functions));
510 if(msg_global->paje_output) {
511 fclose(msg_global->paje_output);
512 msg_global->paje_output = NULL;
514 msg_config_finalize();
522 /** \ingroup msg_easier_life
523 * \brief A clock (in second).
525 double MSG_get_clock(void) {
526 return surf_get_clock();