1 /* Copyright (c) 2010. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "instr/instr_private.h"
8 #include "simgrid/sg_config.h"
12 XBT_LOG_NEW_CATEGORY(instr, "Logging the behavior of the tracing system (used for Visualization/Analysis of simulations)");
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_config, instr, "Configuration");
15 #define OPT_TRACING "tracing"
16 #define OPT_TRACING_PLATFORM "tracing/platform"
17 #define OPT_TRACING_TOPOLOGY "tracing/platform/topology"
18 #define OPT_TRACING_SMPI "tracing/smpi"
19 #define OPT_TRACING_SMPI_GROUP "tracing/smpi/group"
20 #define OPT_TRACING_SMPI_COMPUTING "tracing/smpi/computing"
21 #define OPT_TRACING_CATEGORIZED "tracing/categorized"
22 #define OPT_TRACING_UNCATEGORIZED "tracing/uncategorized"
23 #define OPT_TRACING_MSG_PROCESS "tracing/msg/process"
24 #define OPT_TRACING_FILENAME "tracing/filename"
25 #define OPT_TRACING_BUFFER "tracing/buffer"
26 #define OPT_TRACING_ONELINK_ONLY "tracing/onelink_only"
27 #define OPT_TRACING_DISABLE_DESTROY "tracing/disable_destroy"
28 #define OPT_TRACING_BASIC "tracing/basic"
29 #define OPT_TRACING_COMMENT "tracing/comment"
30 #define OPT_TRACING_COMMENT_FILE "tracing/comment_file"
31 #define OPT_VIVA_UNCAT_CONF "viva/uncategorized"
32 #define OPT_VIVA_CAT_CONF "viva/categorized"
34 static int trace_enabled;
35 static int trace_platform;
36 static int trace_platform_topology;
37 static int trace_smpi_enabled;
38 static int trace_smpi_grouped;
39 static int trace_smpi_computing;
40 static int trace_categorized;
41 static int trace_uncategorized;
42 static int trace_msg_process_enabled;
43 static int trace_buffer;
44 static int trace_onelink_only;
45 static int trace_disable_destroy;
46 static int trace_basic;
48 static int trace_configured = 0;
49 static int trace_active = 0;
51 static void TRACE_getopts(void)
53 trace_enabled = xbt_cfg_get_int(_sg_cfg_set, OPT_TRACING);
54 trace_platform = xbt_cfg_get_int(_sg_cfg_set, OPT_TRACING_PLATFORM);
55 trace_platform_topology = xbt_cfg_get_int(_sg_cfg_set, OPT_TRACING_TOPOLOGY);
56 trace_smpi_enabled = xbt_cfg_get_int(_sg_cfg_set, OPT_TRACING_SMPI);
57 trace_smpi_grouped = xbt_cfg_get_int(_sg_cfg_set, OPT_TRACING_SMPI_GROUP);
58 trace_smpi_computing = xbt_cfg_get_int(_sg_cfg_set, OPT_TRACING_SMPI_COMPUTING);
59 trace_categorized = xbt_cfg_get_int(_sg_cfg_set, OPT_TRACING_CATEGORIZED);
60 trace_uncategorized = xbt_cfg_get_int(_sg_cfg_set, OPT_TRACING_UNCATEGORIZED);
61 trace_msg_process_enabled = xbt_cfg_get_int(_sg_cfg_set, OPT_TRACING_MSG_PROCESS);
62 trace_buffer = xbt_cfg_get_int(_sg_cfg_set, OPT_TRACING_BUFFER);
63 trace_onelink_only = xbt_cfg_get_int(_sg_cfg_set, OPT_TRACING_ONELINK_ONLY);
64 trace_disable_destroy = xbt_cfg_get_int(_sg_cfg_set, OPT_TRACING_DISABLE_DESTROY);
65 trace_basic = xbt_cfg_get_int(_sg_cfg_set, OPT_TRACING_BASIC);
72 // tracing system must be:
73 // - enabled (with --cfg=tracing:1)
74 // - already configured (TRACE_global_init already called)
75 if (!(TRACE_is_enabled() && TRACE_is_configured())){
79 XBT_DEBUG("Tracing starts");
81 /* open the trace file */
85 if (trace_active == 1){
86 THROWF (tracing_error, 0, "Tracing is already active");
89 XBT_DEBUG ("Tracing is on");
91 /* other trace initialization */
92 created_categories = xbt_dict_new_homogeneous(xbt_free);
93 declared_marks = xbt_dict_new_homogeneous (xbt_free);
94 user_host_variables = xbt_dict_new_homogeneous (xbt_free);
95 user_link_variables = xbt_dict_new_homogeneous (xbt_free);
106 TRACE_generate_viva_uncat_conf();
107 TRACE_generate_viva_cat_conf();
109 /* dump trace buffer */
110 TRACE_last_timestamp_to_dump = surf_get_clock();
111 TRACE_paje_dump_buffer(1);
113 /* destroy all data structures of tracing (and free) */
114 PJ_container_free_all();
116 PJ_container_release();
118 TRACE_smpi_release();
119 TRACE_surf_release();
120 xbt_dict_free(&user_link_variables);
121 xbt_dict_free(&user_host_variables);
122 xbt_dict_free(&declared_marks);
123 xbt_dict_free(&created_categories);
125 /* close the trace file */
128 /* de-activate trace */
130 XBT_DEBUG ("Tracing is off");
131 XBT_DEBUG("Tracing system is shutdown");
135 int TRACE_needs_platform (void)
137 return TRACE_msg_process_is_enabled() ||
138 TRACE_categorized() ||
139 TRACE_uncategorized() ||
141 (TRACE_smpi_is_enabled() && TRACE_smpi_is_grouped());
144 int TRACE_is_enabled(void)
146 return trace_enabled;
149 int TRACE_platform(void)
151 return trace_platform;
154 int TRACE_platform_topology(void)
156 return trace_platform_topology;
159 int TRACE_is_configured(void)
161 return trace_configured;
164 int TRACE_smpi_is_enabled(void)
166 return (trace_smpi_enabled || TRACE_smpi_is_grouped())
167 && TRACE_is_enabled();
170 int TRACE_smpi_is_grouped(void)
172 return trace_smpi_grouped;
175 int TRACE_smpi_is_computing(void)
177 return trace_smpi_computing;
181 int TRACE_categorized (void)
183 return trace_categorized;
186 int TRACE_uncategorized (void)
188 return trace_uncategorized;
191 int TRACE_msg_process_is_enabled(void)
193 return trace_msg_process_enabled && TRACE_is_enabled();
196 int TRACE_buffer (void)
198 return trace_buffer && TRACE_is_enabled();
201 int TRACE_onelink_only (void)
203 return trace_onelink_only && TRACE_is_enabled();
206 int TRACE_disable_destroy (void)
208 return trace_disable_destroy && TRACE_is_enabled();
211 int TRACE_basic (void)
213 return trace_basic && TRACE_is_enabled();
216 char *TRACE_get_comment (void)
218 return xbt_cfg_get_string(_sg_cfg_set, OPT_TRACING_COMMENT);
221 char *TRACE_get_comment_file (void)
223 return xbt_cfg_get_string(_sg_cfg_set, OPT_TRACING_COMMENT_FILE);
226 char *TRACE_get_filename(void)
228 return xbt_cfg_get_string(_sg_cfg_set, OPT_TRACING_FILENAME);
231 char *TRACE_get_viva_uncat_conf (void)
233 return xbt_cfg_get_string(_sg_cfg_set, OPT_VIVA_UNCAT_CONF);
236 char *TRACE_get_viva_cat_conf (void)
238 return xbt_cfg_get_string(_sg_cfg_set, OPT_VIVA_CAT_CONF);
241 void TRACE_global_init(int *argc, char **argv)
243 /* name of the tracefile */
244 char *default_tracing_filename = xbt_strdup("simgrid.trace");
245 xbt_cfg_register(&_sg_cfg_set, OPT_TRACING_FILENAME,
246 "Trace file created by the instrumented SimGrid.",
247 xbt_cfgelm_string, &default_tracing_filename, 1, 1,
251 int default_tracing = 0;
252 xbt_cfg_register(&_sg_cfg_set, OPT_TRACING,
254 xbt_cfgelm_int, &default_tracing, 0, 1,
257 /* register platform in the trace */
258 int default_tracing_platform = 0;
259 xbt_cfg_register(&_sg_cfg_set, OPT_TRACING_PLATFORM,
260 "Register the platform in the trace as a hierarchy.",
261 xbt_cfgelm_int, &default_tracing_platform, 0, 1,
264 /* register platform in the trace */
265 int default_tracing_platform_topology = 1;
266 xbt_cfg_register(&_sg_cfg_set, OPT_TRACING_TOPOLOGY,
267 "Register the platform topology in the trace as a graph.",
268 xbt_cfgelm_int, &default_tracing_platform_topology, 0, 1,
272 int default_tracing_smpi = 0;
273 xbt_cfg_register(&_sg_cfg_set, OPT_TRACING_SMPI,
274 "Tracing of the SMPI interface.",
275 xbt_cfgelm_int, &default_tracing_smpi, 0, 1,
279 int default_tracing_smpi_grouped = 0;
280 xbt_cfg_register(&_sg_cfg_set, OPT_TRACING_SMPI_GROUP,
281 "Group MPI processes by host.",
282 xbt_cfgelm_int, &default_tracing_smpi_grouped, 0, 1,
286 int default_tracing_smpi_computing = 0;
287 xbt_cfg_register(&_sg_cfg_set, OPT_TRACING_SMPI_COMPUTING,
288 "Generate states for timing out of SMPI parts of the application",
289 xbt_cfgelm_int, &default_tracing_smpi_computing, 0, 1,
292 /* tracing categorized resource utilization traces */
293 int default_tracing_categorized = 0;
294 xbt_cfg_register(&_sg_cfg_set, OPT_TRACING_CATEGORIZED,
295 "Tracing categorized resource utilization of hosts and links.",
296 xbt_cfgelm_int, &default_tracing_categorized, 0, 1,
299 /* tracing uncategorized resource utilization */
300 int default_tracing_uncategorized = 0;
301 xbt_cfg_register(&_sg_cfg_set, OPT_TRACING_UNCATEGORIZED,
302 "Tracing uncategorized resource utilization of hosts and links.",
303 xbt_cfgelm_int, &default_tracing_uncategorized, 0, 1,
307 int default_tracing_msg_process = 0;
308 xbt_cfg_register(&_sg_cfg_set, OPT_TRACING_MSG_PROCESS,
309 "Tracing of MSG process behavior.",
310 xbt_cfgelm_int, &default_tracing_msg_process, 0, 1,
314 int default_buffer = 1;
315 xbt_cfg_register(&_sg_cfg_set, OPT_TRACING_BUFFER,
316 "Buffer trace events to put them in temporal order.",
317 xbt_cfgelm_int, &default_buffer, 0, 1,
320 /* tracing one link only */
321 int default_onelink_only = 0;
322 xbt_cfg_register(&_sg_cfg_set, OPT_TRACING_ONELINK_ONLY,
323 "Use only routes with one link to trace platform.",
324 xbt_cfgelm_int, &default_onelink_only, 0, 1,
327 /* disable destroy */
328 int default_disable_destroy = 0;
329 xbt_cfg_register(&_sg_cfg_set, OPT_TRACING_DISABLE_DESTROY,
330 "Disable platform containers destruction.",
331 xbt_cfgelm_int, &default_disable_destroy, 0, 1,
334 /* basic -- Avoid extended events (impoverished trace file) */
335 int default_basic = 0;
336 xbt_cfg_register(&_sg_cfg_set, OPT_TRACING_BASIC,
337 "Avoid extended events (impoverished trace file).",
338 xbt_cfgelm_int, &default_basic, 0, 1,
342 char *default_tracing_comment = xbt_strdup ("");
343 xbt_cfg_register(&_sg_cfg_set, OPT_TRACING_COMMENT,
344 "Comment to be added on the top of the trace file.",
345 xbt_cfgelm_string, &default_tracing_comment, 1, 1,
349 char *default_tracing_comment_file = xbt_strdup ("");
350 xbt_cfg_register(&_sg_cfg_set, OPT_TRACING_COMMENT_FILE,
351 "The contents of the file are added to the top of the trace file as comment.",
352 xbt_cfgelm_string, &default_tracing_comment_file, 1, 1,
355 /* Viva graph configuration for uncategorized tracing */
356 char *default_viva_uncat_conf_file = xbt_strdup ("");
357 xbt_cfg_register(&_sg_cfg_set, OPT_VIVA_UNCAT_CONF,
358 "Viva Graph configuration file for uncategorized resource utilization traces.",
359 xbt_cfgelm_string, &default_viva_uncat_conf_file, 1, 1,
362 /* Viva graph configuration for uncategorized tracing */
363 char *default_viva_cat_conf_file = xbt_strdup ("");
364 xbt_cfg_register(&_sg_cfg_set, OPT_VIVA_CAT_CONF,
365 "Viva Graph configuration file for categorized resource utilization traces.",
366 xbt_cfgelm_string, &default_viva_cat_conf_file, 1, 1,
369 /* instrumentation can be considered configured now */
370 trace_configured = 1;
373 static void print_line (const char *option, const char *desc, const char *longdesc, int detailed)
375 char str[INSTR_DEFAULT_STR_SIZE];
376 snprintf (str, INSTR_DEFAULT_STR_SIZE, "--cfg=%s ", option);
378 int len = strlen (str);
379 printf ("%s%*.*s %s\n", str, 30-len, 30-len, "", desc);
380 if (!!longdesc && detailed){
381 printf ("%s\n\n", longdesc);
385 void TRACE_help (int detailed)
388 "Description of the tracing options accepted by this simulator:\n\n");
389 print_line (OPT_TRACING, "Enable the tracing system",
390 " It activates the tracing system and register the simulation platform\n"
391 " in the trace file. You have to enable this option to others take effect.",
393 print_line (OPT_TRACING_CATEGORIZED, "Trace categorized resource utilization",
394 " It activates the categorized resource utilization tracing. It should\n"
395 " be enabled if tracing categories are used by this simulator.",
397 print_line (OPT_TRACING_UNCATEGORIZED, "Trace uncategorized resource utilization",
398 " It activates the uncategorized resource utilization tracing. Use it if\n"
399 " this simulator do not use tracing categories and resource use have to be\n"
402 print_line (OPT_TRACING_FILENAME, "Filename to register traces",
403 " A file with this name will be created to register the simulation. The file\n"
404 " is in the Paje format and can be analyzed using Viva, Paje, and PajeNG visualization\n"
405 " tools. More information can be found in these webpages:\n"
406 " http://github.com/schnorr/viva/\n"
407 " http://github.com/schnorr/pajeng/\n"
408 " http://paje.sourceforge.net/",
410 print_line (OPT_TRACING_SMPI, "Trace the MPI Interface (SMPI)",
411 " This option only has effect if this simulator is SMPI-based. Traces the MPI\n"
412 " interface and generates a trace that can be analyzed using Gantt-like\n"
413 " visualizations. Every MPI function (implemented by SMPI) is transformed in a\n"
414 " state, and point-to-point communications can be analyzed with arrows.",
416 print_line (OPT_TRACING_SMPI_GROUP, "Group MPI processes by host (SMPI)",
417 " This option only has effect if this simulator is SMPI-based. The processes\n"
418 " are grouped by the hosts where they were executed.",
420 print_line (OPT_TRACING_SMPI_COMPUTING, "Generates a \" Computing \" State",
421 " This option aims at tracing computations in the application, outside SMPI\n"
422 " to allow further study of simulated or real computation time",
424 print_line (OPT_TRACING_MSG_PROCESS, "Trace processes behavior (MSG)",
425 " This option only has effect if this simulator is MSG-based. It traces the\n"
426 " behavior of all categorized MSG processes, grouping them by hosts. This option\n"
427 " can be used to track process location if this simulator has process migration.",
429 print_line (OPT_TRACING_BUFFER, "Buffer events to put them in temporal order",
430 " This option put some events in a time-ordered buffer using the insertion\n"
431 " sort algorithm. The process of acquiring and releasing locks to access this\n"
432 " buffer and the cost of the sorting algorithm make this process slow. The\n"
433 " simulator performance can be severely impacted if this option is activated,\n"
434 " but you are sure to get a trace file with events sorted.",
436 print_line (OPT_TRACING_ONELINK_ONLY, "Consider only one link routes to trace platform",
437 " This option changes the way SimGrid register its platform on the trace file.\n"
438 " Normally, the tracing considers all routes (no matter their size) on the\n"
439 " platform file to re-create the resource topology. If this option is activated,\n"
440 " only the routes with one link are used to register the topology within an AS.\n"
441 " Routes among AS continue to be traced as usual.",
443 print_line (OPT_TRACING_DISABLE_DESTROY, "Disable platform containers destruction",
444 " Disable the destruction of containers at the end of simulation. This can be\n"
445 " used with simulators that have a different notion of time (different from\n"
446 " the simulated time).",
448 print_line (OPT_TRACING_BASIC, "Avoid extended events (impoverished trace file).",
449 " Some visualization tools are not able to parse correctly the Paje file format.\n"
450 " Use this option if you are using one of these tools to visualize the simulation\n"
451 " trace. Keep in mind that the trace might be incomplete, without all the\n"
452 " information that would be registered otherwise.",
454 print_line (OPT_TRACING_COMMENT, "Comment to be added on the top of the trace file.",
455 " Use this to add a comment line to the top of the trace file.",
457 print_line (OPT_TRACING_COMMENT_FILE, "File contents added to trace file as comment.",
458 " Use this to add the contents of a file to the top of the trace file as comment.",
460 print_line (OPT_VIVA_UNCAT_CONF, "Generate a graph configuration for Viva",
461 " This option can be used in all types of simulators build with SimGrid\n"
462 " to generate a uncategorized resource utilization graph to be used as\n"
463 " configuration for the Viva visualization tool. This option\n"
464 " can be used with tracing/categorized:1 and tracing:1 options to\n"
465 " analyze an unmodified simulator before changing it to contain\n"
468 print_line (OPT_VIVA_CAT_CONF, "Generate an uncategorized graph configuration for Viva",
469 " This option can be used if this simulator uses tracing categories\n"
470 " in its code. The file specified by this option holds a graph configuration\n"
471 " file for the Viva visualization tool that can be used to analyze a categorized\n"
472 " resource utilization.",
474 print_line (OPT_TRACING_TOPOLOGY, "Register the platform topology as a graph",
475 " This option (enabled by default) can be used to disable the tracing of\n"
476 " the platform topology in the trace file. Sometimes, such task is really\n"
477 " time consuming, since it must get the route from each host ot other hosts\n"
478 " within the same Autonomous System (AS).",
482 static void output_types (const char *name, xbt_dynar_t types, FILE *file)
485 fprintf (file, " %s = (", name);
486 for (i = xbt_dynar_length(types); i > 0; i--) {
487 char *type = *(char**)xbt_dynar_get_ptr(types, i - 1);
488 fprintf (file, "\"%s\"", type);
492 fprintf (file, ");\n");
495 xbt_dynar_free (&types);
498 static void output_categories (const char *name, xbt_dynar_t cats, FILE *file)
501 fprintf (file, " values = (");
502 for (i = xbt_dynar_length(cats); i > 0; i--) {
503 char *cat = *(char**)xbt_dynar_get_ptr(cats, i - 1);
504 fprintf (file, "\"%s%s\"", name, cat);
508 fprintf (file, ");\n");
511 xbt_dynar_free (&cats);
514 static void uncat_configuration (FILE *file)
516 //register NODE and EDGE types
517 output_types ("node", TRACE_get_node_types(), file);
518 output_types ("edge", TRACE_get_edge_types(), file);
519 fprintf (file, "\n");
521 //configuration for all nodes
524 " type = \"square\";\n"
525 " size = \"power\";\n"
526 " values = (\"power_used\");\n"
529 " type = \"rhombus\";\n"
530 " size = \"bandwidth\";\n"
531 " values = (\"bandwidth_used\");\n"
536 static void cat_configuration (FILE *file)
538 //register NODE and EDGE types
539 output_types ("node", TRACE_get_node_types(), file);
540 output_types ("edge", TRACE_get_edge_types(), file);
541 fprintf (file, "\n");
543 //configuration for all nodes
546 " type = \"square\";\n"
547 " size = \"power\";\n");
548 output_categories ("p", TRACE_get_categories(), file);
552 " type = \"rhombus\";\n"
553 " size = \"bandwidth\";\n");
554 output_categories ("b", TRACE_get_categories(), file);
555 fprintf (file, " };\n");
559 static void generate_uncat_configuration (const char *output, const char *name, int brackets)
561 if (output && strlen(output) > 0){
562 FILE *file = fopen (output, "w");
564 THROWF (system_error, 1, "Unable to open file (%s) for writing %s graph "
565 "configuration (uncategorized).", output, name);
568 if (brackets) fprintf (file, "{\n");
569 uncat_configuration (file);
570 if (brackets) fprintf (file, "}\n");
575 static void generate_cat_configuration (const char *output, const char *name, int brackets)
577 if (output && strlen(output) > 0){
578 //check if we do have categories declared
579 if (xbt_dict_is_empty(created_categories)){
580 XBT_INFO("No categories declared, ignoring generation of %s graph configuration", name);
584 FILE *file = fopen (output, "w");
586 THROWF (system_error, 1, "Unable to open file (%s) for writing %s graph "
587 "configuration (categorized).", output, name);
590 if (brackets) fprintf (file, "{\n");
591 cat_configuration (file);
592 if (brackets) fprintf (file, "}\n");
597 void TRACE_generate_viva_uncat_conf (void)
599 generate_uncat_configuration (TRACE_get_viva_uncat_conf (), "viva", 0);
602 void TRACE_generate_viva_cat_conf (void)
604 generate_cat_configuration (TRACE_get_viva_cat_conf(), "viva", 0);
607 static int previous_trace_state = -1;
609 void instr_pause_tracing (void)
611 previous_trace_state = trace_enabled;
612 if (!TRACE_is_enabled()){
613 XBT_DEBUG ("Tracing is already paused, therefore do nothing.");
615 XBT_DEBUG ("Tracing is being paused.");
618 XBT_DEBUG ("Tracing is paused.");
621 void instr_resume_tracing (void)
623 if (TRACE_is_enabled()){
624 XBT_DEBUG ("Tracing is already running while trying to resume, therefore do nothing.");
626 XBT_DEBUG ("Tracing is being resumed.");
629 if (previous_trace_state != -1){
630 trace_enabled = previous_trace_state;
634 XBT_DEBUG ("Tracing is resumed.");
635 previous_trace_state = -1;
639 #undef OPT_TRACING_PLATFORM
640 #undef OPT_TRACING_TOPOLOGY
641 #undef OPT_TRACING_SMPI
642 #undef OPT_TRACING_SMPI_GROUP
643 #undef OPT_TRACING_CATEGORIZED
644 #undef OPT_TRACING_UNCATEGORIZED
645 #undef OPT_TRACING_MSG_PROCESS
646 #undef OPT_TRACING_FILENAME
647 #undef OPT_TRACING_BUFFER
648 #undef OPT_TRACING_ONELINK_ONLY
649 #undef OPT_TRACING_DISABLE_DESTROY
650 #undef OPT_TRACING_BASIC
651 #undef OPT_TRACING_COMMENT
652 #undef OPT_TRACING_COMMENT_FILE
653 #undef OPT_VIVA_UNCAT_CONF
654 #undef OPT_VIVA_CAT_CONF
656 #endif /* HAVE_TRACING */