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. */
8 #include "instr/instr_private.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_SMPI "tracing/smpi"
18 #define OPT_TRACING_SMPI_GROUP "tracing/smpi/group"
19 #define OPT_TRACING_CATEGORIZED "tracing/categorized"
20 #define OPT_TRACING_UNCATEGORIZED "tracing/uncategorized"
21 #define OPT_TRACING_MSG_PROCESS "tracing/msg/process"
22 #define OPT_TRACING_FILENAME "tracing/filename"
23 #define OPT_TRACING_BUFFER "tracing/buffer"
24 #define OPT_TRACING_ONELINK_ONLY "tracing/onelink_only"
25 #define OPT_TRACING_DISABLE_DESTROY "tracing/disable_destroy"
26 #define OPT_TRIVA_UNCAT_CONF "triva/uncategorized"
27 #define OPT_TRIVA_CAT_CONF "triva/categorized"
28 #define OPT_VIVA_UNCAT_CONF "viva/uncategorized"
29 #define OPT_VIVA_CAT_CONF "viva/categorized"
31 static int trace_enabled;
32 static int trace_platform;
33 static int trace_smpi_enabled;
34 static int trace_smpi_grouped;
35 static int trace_categorized;
36 static int trace_uncategorized;
37 static int trace_msg_process_enabled;
38 static int trace_buffer;
39 static int trace_onelink_only;
40 static int trace_disable_destroy;
42 static int trace_configured = 0;
43 static int trace_active = 0;
45 static void TRACE_getopts(void)
47 trace_enabled = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING);
48 trace_platform = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_PLATFORM);
49 trace_smpi_enabled = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_SMPI);
50 trace_smpi_grouped = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_SMPI_GROUP);
51 trace_categorized = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_CATEGORIZED);
52 trace_uncategorized = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_UNCATEGORIZED);
53 trace_msg_process_enabled = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_MSG_PROCESS);
54 trace_buffer = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_BUFFER);
55 trace_onelink_only = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_ONELINK_ONLY);
56 trace_disable_destroy = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_DISABLE_DESTROY);
63 // tracing system must be:
64 // - enabled (with --cfg=tracing:1)
65 // - already configured (TRACE_global_init already called)
66 if (!(TRACE_is_enabled() && TRACE_is_configured())){
70 XBT_DEBUG("Tracing starts");
72 /* open the trace file */
76 if (trace_active == 1){
77 THROWF (tracing_error, 0, "Tracing is already active");
80 XBT_DEBUG ("Tracing is on");
82 /* other trace initialization */
83 created_categories = xbt_dict_new_homogeneous(xbt_free);
84 declared_marks = xbt_dict_new_homogeneous (xbt_free);
85 user_host_variables = xbt_dict_new_homogeneous (xbt_free);
86 user_link_variables = xbt_dict_new_homogeneous (xbt_free);
97 TRACE_generate_triva_uncat_conf();
98 TRACE_generate_triva_cat_conf();
99 TRACE_generate_viva_uncat_conf();
100 TRACE_generate_viva_cat_conf();
102 /* dump trace buffer */
103 TRACE_last_timestamp_to_dump = surf_get_clock();
104 TRACE_paje_dump_buffer(1);
106 /* destroy all data structures of tracing (and free) */
107 PJ_container_free_all();
109 PJ_container_release();
111 TRACE_smpi_release();
112 TRACE_surf_release();
113 xbt_dict_free(&user_link_variables);
114 xbt_dict_free(&user_host_variables);
115 xbt_dict_free(&declared_marks);
116 xbt_dict_free(&created_categories);
118 /* close the trace file */
121 /* de-activate trace */
123 XBT_DEBUG ("Tracing is off");
124 XBT_DEBUG("Tracing system is shutdown");
128 int TRACE_needs_platform (void)
130 return TRACE_msg_process_is_enabled() ||
131 TRACE_categorized() ||
132 TRACE_uncategorized() ||
134 (TRACE_smpi_is_enabled() && TRACE_smpi_is_grouped());
137 int TRACE_is_enabled(void)
139 return trace_enabled;
142 int TRACE_platform(void)
144 return trace_platform;
147 int TRACE_is_configured(void)
149 return trace_configured;
152 int TRACE_smpi_is_enabled(void)
154 return (trace_smpi_enabled || TRACE_smpi_is_grouped())
155 && TRACE_is_enabled();
158 int TRACE_smpi_is_grouped(void)
160 return trace_smpi_grouped;
163 int TRACE_categorized (void)
165 return trace_categorized;
168 int TRACE_uncategorized (void)
170 return trace_uncategorized;
173 int TRACE_msg_process_is_enabled(void)
175 return trace_msg_process_enabled && TRACE_is_enabled();
178 int TRACE_buffer (void)
180 return trace_buffer && TRACE_is_enabled();
183 int TRACE_onelink_only (void)
185 return trace_onelink_only && TRACE_is_enabled();
188 int TRACE_disable_destroy (void)
190 return trace_disable_destroy && TRACE_is_enabled();
193 char *TRACE_get_filename(void)
195 return xbt_cfg_get_string(_surf_cfg_set, OPT_TRACING_FILENAME);
198 char *TRACE_get_triva_uncat_conf (void)
200 return xbt_cfg_get_string(_surf_cfg_set, OPT_TRIVA_UNCAT_CONF);
203 char *TRACE_get_triva_cat_conf (void)
205 return xbt_cfg_get_string(_surf_cfg_set, OPT_TRIVA_CAT_CONF);
208 char *TRACE_get_viva_uncat_conf (void)
210 return xbt_cfg_get_string(_surf_cfg_set, OPT_VIVA_UNCAT_CONF);
213 char *TRACE_get_viva_cat_conf (void)
215 return xbt_cfg_get_string(_surf_cfg_set, OPT_VIVA_CAT_CONF);
218 void TRACE_global_init(int *argc, char **argv)
220 /* name of the tracefile */
221 char *default_tracing_filename = xbt_strdup("simgrid.trace");
222 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_FILENAME,
223 "Trace file created by the instrumented SimGrid.",
224 xbt_cfgelm_string, &default_tracing_filename, 1, 1,
228 int default_tracing = 0;
229 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING,
231 xbt_cfgelm_int, &default_tracing, 0, 1,
234 /* register platform in the trace */
235 int default_tracing_platform = 0;
236 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_PLATFORM,
237 "Register the platform in the trace as a graph.",
238 xbt_cfgelm_int, &default_tracing_platform, 0, 1,
242 int default_tracing_smpi = 0;
243 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_SMPI,
244 "Tracing of the SMPI interface.",
245 xbt_cfgelm_int, &default_tracing_smpi, 0, 1,
249 int default_tracing_smpi_grouped = 0;
250 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_SMPI_GROUP,
251 "Group MPI processes by host.",
252 xbt_cfgelm_int, &default_tracing_smpi_grouped, 0, 1,
256 /* tracing categorized resource utilization traces */
257 int default_tracing_categorized = 0;
258 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_CATEGORIZED,
259 "Tracing categorized resource utilization of hosts and links.",
260 xbt_cfgelm_int, &default_tracing_categorized, 0, 1,
263 /* tracing uncategorized resource utilization */
264 int default_tracing_uncategorized = 0;
265 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_UNCATEGORIZED,
266 "Tracing uncategorized resource utilization of hosts and links.",
267 xbt_cfgelm_int, &default_tracing_uncategorized, 0, 1,
271 int default_tracing_msg_process = 0;
272 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_MSG_PROCESS,
273 "Tracing of MSG process behavior.",
274 xbt_cfgelm_int, &default_tracing_msg_process, 0, 1,
278 int default_buffer = 1;
279 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_BUFFER,
280 "Buffer trace events to put them in temporal order.",
281 xbt_cfgelm_int, &default_buffer, 0, 1,
284 /* tracing one link only */
285 int default_onelink_only = 0;
286 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_ONELINK_ONLY,
287 "Use only routes with one link to trace platform.",
288 xbt_cfgelm_int, &default_onelink_only, 0, 1,
291 /* disable destroy */
292 int default_disable_destroy = 0;
293 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_DISABLE_DESTROY,
294 "Disable platform containers destruction.",
295 xbt_cfgelm_int, &default_disable_destroy, 0, 1,
298 /* Triva graph configuration for uncategorized tracing */
299 char *default_triva_uncat_conf_file = xbt_strdup ("");
300 xbt_cfg_register(&_surf_cfg_set, OPT_TRIVA_UNCAT_CONF,
301 "Triva Graph configuration file for uncategorized resource utilization traces.",
302 xbt_cfgelm_string, &default_triva_uncat_conf_file, 1, 1,
305 /* Triva graph configuration for uncategorized tracing */
306 char *default_triva_cat_conf_file = xbt_strdup ("");
307 xbt_cfg_register(&_surf_cfg_set, OPT_TRIVA_CAT_CONF,
308 "Triva Graph configuration file for categorized resource utilization traces.",
309 xbt_cfgelm_string, &default_triva_cat_conf_file, 1, 1,
312 /* Viva graph configuration for uncategorized tracing */
313 char *default_viva_uncat_conf_file = xbt_strdup ("");
314 xbt_cfg_register(&_surf_cfg_set, OPT_VIVA_UNCAT_CONF,
315 "Viva Graph configuration file for uncategorized resource utilization traces.",
316 xbt_cfgelm_string, &default_viva_uncat_conf_file, 1, 1,
319 /* Viva graph configuration for uncategorized tracing */
320 char *default_viva_cat_conf_file = xbt_strdup ("");
321 xbt_cfg_register(&_surf_cfg_set, OPT_VIVA_CAT_CONF,
322 "Viva Graph configuration file for categorized resource utilization traces.",
323 xbt_cfgelm_string, &default_viva_cat_conf_file, 1, 1,
326 /* instrumentation can be considered configured now */
327 trace_configured = 1;
330 static void print_line (const char *option, const char *desc, const char *longdesc, int detailed)
332 char str[INSTR_DEFAULT_STR_SIZE];
333 snprintf (str, INSTR_DEFAULT_STR_SIZE, "--cfg=%s ", option);
335 int len = strlen (str);
336 printf ("%s%*.*s %s\n", str, 30-len, 30-len, "", desc);
337 if (!!longdesc && detailed){
338 printf ("%s\n\n", longdesc);
342 void TRACE_help (int detailed)
345 "Description of the tracing options accepted by this simulator:\n\n");
346 print_line (OPT_TRACING, "Enable the tracing system",
347 " It activates the tracing system and register the simulation platform\n"
348 " in the trace file. You have to enable this option to others take effect.",
350 print_line (OPT_TRACING_CATEGORIZED, "Trace categorized resource utilization",
351 " It activates the categorized resource utilization tracing. It should\n"
352 " be enabled if tracing categories are used by this simulator.",
354 print_line (OPT_TRACING_UNCATEGORIZED, "Trace uncategorized resource utilization",
355 " It activates the uncategorized resource utilization tracing. Use it if\n"
356 " this simulator do not use tracing categories and resource use have to be\n"
359 print_line (OPT_TRACING_FILENAME, "Filename to register traces",
360 " A file with this name will be created to register the simulation. The file\n"
361 " is in the Paje format and can be analyzed using Triva or Paje visualization\n"
362 " tools. More information can be found in these webpages:\n"
363 " http://triva.gforge.inria.fr/\n"
364 " http://paje.sourceforge.net/",
366 print_line (OPT_TRACING_SMPI, "Trace the MPI Interface (SMPI)",
367 " This option only has effect if this simulator is SMPI-based. Traces the MPI\n"
368 " interface and generates a trace that can be analyzed using Gantt-like\n"
369 " visualizations. Every MPI function (implemented by SMPI) is transformed in a\n"
370 " state, and point-to-point communications can be analyzed with arrows.",
372 print_line (OPT_TRACING_SMPI_GROUP, "Group MPI processes by host (SMPI)",
373 " This option only has effect if this simulator is SMPI-based. The processes\n"
374 " are grouped by the hosts where they were executed.",
376 print_line (OPT_TRACING_MSG_PROCESS, "Trace processes behavior (MSG)",
377 " This option only has effect if this simulator is MSG-based. It traces the\n"
378 " behavior of all categorized MSG processes, grouping them by hosts. This option\n"
379 " can be used to track process location if this simulator has process migration.",
381 print_line (OPT_TRACING_BUFFER, "Buffer events to put them in temporal order",
382 " This option put some events in a time-ordered buffer using the insertion\n"
383 " sort algorithm. The process of acquiring and releasing locks to access this\n"
384 " buffer and the cost of the sorting algorithm make this process slow. The\n"
385 " simulator performance can be severely impacted if this option is activated,\n"
386 " but you are sure to get a trace file with events sorted.",
388 print_line (OPT_TRACING_ONELINK_ONLY, "Consider only one link routes to trace platform",
389 " This option changes the way SimGrid register its platform on the trace file.\n"
390 " Normally, the tracing considers all routes (no matter their size) on the\n"
391 " platform file to re-create the resource topology. If this option is activated,\n"
392 " only the routes with one link are used to register the topology within an AS.\n"
393 " Routes among AS continue to be traced as usual.",
395 print_line (OPT_TRACING_DISABLE_DESTROY, "Disable platform containers destruction",
396 " Disable the destruction of containers at the end of simulation. This can be\n"
397 " used with simulators that have a different notion of time (different from\n"
398 " the simulated time).",
400 print_line (OPT_TRIVA_UNCAT_CONF, "Generate graph configuration for Triva",
401 " This option can be used in all types of simulators build with SimGrid\n"
402 " to generate a uncategorized resource utilization graph to be used as\n"
403 " configuration for the Triva visualization analysis. This option\n"
404 " can be used with tracing/categorized:1 and tracing:1 options to\n"
405 " analyze an unmodified simulator before changing it to contain\n"
408 print_line (OPT_TRIVA_CAT_CONF, "generate uncategorized graph configuration for Triva",
409 " This option can be used if this simulator uses tracing categories\n"
410 " in its code. The file specified by this option holds a graph configuration\n"
411 " file for the Triva visualization tool that can be used to analyze a categorized\n"
412 " resource utilization.",
414 print_line (OPT_VIVA_UNCAT_CONF, "Generate a graph configuration for Viva",
415 " This option can be used in all types of simulators build with SimGrid\n"
416 " to generate a uncategorized resource utilization graph to be used as\n"
417 " configuration for the Viva visualization tool. This option\n"
418 " can be used with tracing/categorized:1 and tracing:1 options to\n"
419 " analyze an unmodified simulator before changing it to contain\n"
422 print_line (OPT_VIVA_CAT_CONF, "Generate an uncategorized graph configuration for Viva",
423 " This option can be used if this simulator uses tracing categories\n"
424 " in its code. The file specified by this option holds a graph configuration\n"
425 " file for the Viva visualization tool that can be used to analyze a categorized\n"
426 " resource utilization.",
430 static void output_types (const char *name, xbt_dynar_t types, FILE *file)
433 fprintf (file, " %s = (", name);
434 for (i = xbt_dynar_length(types); i > 0; i--) {
435 char *type = *(char**)xbt_dynar_get_ptr(types, i - 1);
436 fprintf (file, "\"%s\"", type);
440 fprintf (file, ");\n");
443 xbt_dynar_free (&types);
446 static void output_categories (const char *name, xbt_dynar_t cats, FILE *file)
449 fprintf (file, " values = (");
450 for (i = xbt_dynar_length(cats); i > 0; i--) {
451 char *cat = *(char**)xbt_dynar_get_ptr(cats, i - 1);
452 fprintf (file, "\"%s%s\"", name, cat);
456 fprintf (file, ");\n");
459 xbt_dynar_free (&cats);
462 static void uncat_configuration (FILE *file)
464 //register NODE and EDGE types
465 output_types ("node", TRACE_get_node_types(), file);
466 output_types ("edge", TRACE_get_edge_types(), file);
467 fprintf (file, "\n");
469 //configuration for all nodes
472 " type = \"square\";\n"
473 " size = \"power\";\n"
474 " values = (\"power_used\");\n"
477 " type = \"rhombus\";\n"
478 " size = \"bandwidth\";\n"
479 " values = (\"bandwidth_used\");\n"
484 static void cat_configuration (FILE *file)
486 //register NODE and EDGE types
487 output_types ("node", TRACE_get_node_types(), file);
488 output_types ("edge", TRACE_get_edge_types(), file);
489 fprintf (file, "\n");
491 //configuration for all nodes
494 " type = \"square\";\n"
495 " size = \"power\";\n");
496 output_categories ("p", TRACE_get_categories(), file);
500 " type = \"rhombus\";\n"
501 " size = \"bandwidth\";\n");
502 output_categories ("b", TRACE_get_categories(), file);
503 fprintf (file, " };\n");
507 static void generate_uncat_configuration (const char *output, const char *name, int brackets)
509 if (output && strlen(output) > 0){
510 FILE *file = fopen (output, "w");
512 THROWF (system_error, 1, "Unable to open file (%s) for writing %s graph "
513 "configuration (uncategorized).", output, name);
516 if (brackets) fprintf (file, "{\n");
517 uncat_configuration (file);
518 if (brackets) fprintf (file, "}\n");
523 static void generate_cat_configuration (const char *output, const char *name, int brackets)
525 if (output && strlen(output) > 0){
526 //check if we do have categories declared
527 if (xbt_dict_is_empty(created_categories)){
528 XBT_INFO("No categories declared, ignoring generation of %s graph configuration", name);
532 FILE *file = fopen (output, "w");
534 THROWF (system_error, 1, "Unable to open file (%s) for writing %s graph "
535 "configuration (categorized).", output, name);
538 if (brackets) fprintf (file, "{\n");
539 cat_configuration (file);
540 if (brackets) fprintf (file, "}\n");
545 void TRACE_generate_triva_uncat_conf (void)
547 generate_uncat_configuration (TRACE_get_triva_uncat_conf (), "triva", 1);
550 void TRACE_generate_triva_cat_conf (void)
552 generate_cat_configuration (TRACE_get_triva_cat_conf(), "triva", 1);
555 void TRACE_generate_viva_uncat_conf (void)
557 generate_uncat_configuration (TRACE_get_viva_uncat_conf (), "viva", 0);
560 void TRACE_generate_viva_cat_conf (void)
562 generate_cat_configuration (TRACE_get_viva_cat_conf(), "viva", 0);
566 #undef OPT_TRACING_PLATFORM
567 #undef OPT_TRACING_SMPI
568 #undef OPT_TRACING_SMPI_GROUP
569 #undef OPT_TRACING_CATEGORIZED
570 #undef OPT_TRACING_UNCATEGORIZED
571 #undef OPT_TRACING_MSG_PROCESS
572 #undef OPT_TRACING_FILENAME
573 #undef OPT_TRACING_BUFFER
574 #undef OPT_TRACING_ONELINK_ONLY
575 #undef OPT_TRACING_DISABLE_DESTROY
576 #undef OPT_TRIVA_UNCAT_CONF
577 #undef OPT_TRIVA_CAT_CONF
578 #undef OPT_VIVA_UNCAT_CONF
579 #undef OPT_VIVA_CAT_CONF
581 #endif /* HAVE_TRACING */