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_SMPI_COMPUTING "tracing/smpi/computing"
20 #define OPT_TRACING_CATEGORIZED "tracing/categorized"
21 #define OPT_TRACING_UNCATEGORIZED "tracing/uncategorized"
22 #define OPT_TRACING_MSG_PROCESS "tracing/msg/process"
23 #define OPT_TRACING_FILENAME "tracing/filename"
24 #define OPT_TRACING_BUFFER "tracing/buffer"
25 #define OPT_TRACING_ONELINK_ONLY "tracing/onelink_only"
26 #define OPT_TRACING_DISABLE_DESTROY "tracing/disable_destroy"
27 #define OPT_TRACING_BASIC "tracing/basic"
28 #define OPT_TRIVA_UNCAT_CONF "triva/uncategorized"
29 #define OPT_TRIVA_CAT_CONF "triva/categorized"
30 #define OPT_VIVA_UNCAT_CONF "viva/uncategorized"
31 #define OPT_VIVA_CAT_CONF "viva/categorized"
33 static int trace_enabled;
34 static int trace_platform;
35 static int trace_smpi_enabled;
36 static int trace_smpi_grouped;
37 static int trace_smpi_computing;
38 static int trace_categorized;
39 static int trace_uncategorized;
40 static int trace_msg_process_enabled;
41 static int trace_buffer;
42 static int trace_onelink_only;
43 static int trace_disable_destroy;
44 static int trace_basic;
46 static int trace_configured = 0;
47 static int trace_active = 0;
49 static void TRACE_getopts(void)
51 trace_enabled = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING);
52 trace_platform = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_PLATFORM);
53 trace_smpi_enabled = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_SMPI);
54 trace_smpi_grouped = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_SMPI_GROUP);
55 trace_smpi_computing = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_SMPI_COMPUTING);
56 trace_categorized = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_CATEGORIZED);
57 trace_uncategorized = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_UNCATEGORIZED);
58 trace_msg_process_enabled = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_MSG_PROCESS);
59 trace_buffer = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_BUFFER);
60 trace_onelink_only = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_ONELINK_ONLY);
61 trace_disable_destroy = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_DISABLE_DESTROY);
62 trace_basic = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_BASIC);
69 // tracing system must be:
70 // - enabled (with --cfg=tracing:1)
71 // - already configured (TRACE_global_init already called)
72 if (!(TRACE_is_enabled() && TRACE_is_configured())){
76 XBT_DEBUG("Tracing starts");
78 /* open the trace file */
82 if (trace_active == 1){
83 THROWF (tracing_error, 0, "Tracing is already active");
86 XBT_DEBUG ("Tracing is on");
88 /* other trace initialization */
89 created_categories = xbt_dict_new_homogeneous(xbt_free);
90 declared_marks = xbt_dict_new_homogeneous (xbt_free);
91 user_host_variables = xbt_dict_new_homogeneous (xbt_free);
92 user_link_variables = xbt_dict_new_homogeneous (xbt_free);
103 TRACE_generate_triva_uncat_conf();
104 TRACE_generate_triva_cat_conf();
105 TRACE_generate_viva_uncat_conf();
106 TRACE_generate_viva_cat_conf();
108 /* dump trace buffer */
109 TRACE_last_timestamp_to_dump = surf_get_clock();
110 TRACE_paje_dump_buffer(1);
112 /* destroy all data structures of tracing (and free) */
113 PJ_container_free_all();
115 PJ_container_release();
117 TRACE_smpi_release();
118 TRACE_surf_release();
119 xbt_dict_free(&user_link_variables);
120 xbt_dict_free(&user_host_variables);
121 xbt_dict_free(&declared_marks);
122 xbt_dict_free(&created_categories);
124 /* close the trace file */
127 /* de-activate trace */
129 XBT_DEBUG ("Tracing is off");
130 XBT_DEBUG("Tracing system is shutdown");
134 int TRACE_needs_platform (void)
136 return TRACE_msg_process_is_enabled() ||
137 TRACE_categorized() ||
138 TRACE_uncategorized() ||
140 (TRACE_smpi_is_enabled() && TRACE_smpi_is_grouped());
143 int TRACE_is_enabled(void)
145 return trace_enabled;
148 int TRACE_platform(void)
150 return trace_platform;
153 int TRACE_is_configured(void)
155 return trace_configured;
158 int TRACE_smpi_is_enabled(void)
160 return (trace_smpi_enabled || TRACE_smpi_is_grouped())
161 && TRACE_is_enabled();
164 int TRACE_smpi_is_grouped(void)
166 return trace_smpi_grouped;
169 int TRACE_smpi_is_computing(void)
171 return trace_smpi_computing;
175 int TRACE_categorized (void)
177 return trace_categorized;
180 int TRACE_uncategorized (void)
182 return trace_uncategorized;
185 int TRACE_msg_process_is_enabled(void)
187 return trace_msg_process_enabled && TRACE_is_enabled();
190 int TRACE_buffer (void)
192 return trace_buffer && TRACE_is_enabled();
195 int TRACE_onelink_only (void)
197 return trace_onelink_only && TRACE_is_enabled();
200 int TRACE_disable_destroy (void)
202 return trace_disable_destroy && TRACE_is_enabled();
205 int TRACE_basic (void)
207 return trace_basic && TRACE_is_enabled();
211 char *TRACE_get_filename(void)
213 return xbt_cfg_get_string(_surf_cfg_set, OPT_TRACING_FILENAME);
216 char *TRACE_get_triva_uncat_conf (void)
218 return xbt_cfg_get_string(_surf_cfg_set, OPT_TRIVA_UNCAT_CONF);
221 char *TRACE_get_triva_cat_conf (void)
223 return xbt_cfg_get_string(_surf_cfg_set, OPT_TRIVA_CAT_CONF);
226 char *TRACE_get_viva_uncat_conf (void)
228 return xbt_cfg_get_string(_surf_cfg_set, OPT_VIVA_UNCAT_CONF);
231 char *TRACE_get_viva_cat_conf (void)
233 return xbt_cfg_get_string(_surf_cfg_set, OPT_VIVA_CAT_CONF);
236 void TRACE_global_init(int *argc, char **argv)
238 /* name of the tracefile */
239 char *default_tracing_filename = xbt_strdup("simgrid.trace");
240 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_FILENAME,
241 "Trace file created by the instrumented SimGrid.",
242 xbt_cfgelm_string, &default_tracing_filename, 1, 1,
246 int default_tracing = 0;
247 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING,
249 xbt_cfgelm_int, &default_tracing, 0, 1,
252 /* register platform in the trace */
253 int default_tracing_platform = 0;
254 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_PLATFORM,
255 "Register the platform in the trace as a graph.",
256 xbt_cfgelm_int, &default_tracing_platform, 0, 1,
260 int default_tracing_smpi = 0;
261 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_SMPI,
262 "Tracing of the SMPI interface.",
263 xbt_cfgelm_int, &default_tracing_smpi, 0, 1,
267 int default_tracing_smpi_grouped = 0;
268 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_SMPI_GROUP,
269 "Group MPI processes by host.",
270 xbt_cfgelm_int, &default_tracing_smpi_grouped, 0, 1,
274 int default_tracing_smpi_computing = 0;
275 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_SMPI_COMPUTING,
276 "Generate states for timing out of SMPI parts of the application",
277 xbt_cfgelm_int, &default_tracing_smpi_computing, 0, 1,
280 /* tracing categorized resource utilization traces */
281 int default_tracing_categorized = 0;
282 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_CATEGORIZED,
283 "Tracing categorized resource utilization of hosts and links.",
284 xbt_cfgelm_int, &default_tracing_categorized, 0, 1,
287 /* tracing uncategorized resource utilization */
288 int default_tracing_uncategorized = 0;
289 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_UNCATEGORIZED,
290 "Tracing uncategorized resource utilization of hosts and links.",
291 xbt_cfgelm_int, &default_tracing_uncategorized, 0, 1,
295 int default_tracing_msg_process = 0;
296 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_MSG_PROCESS,
297 "Tracing of MSG process behavior.",
298 xbt_cfgelm_int, &default_tracing_msg_process, 0, 1,
302 int default_buffer = 1;
303 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_BUFFER,
304 "Buffer trace events to put them in temporal order.",
305 xbt_cfgelm_int, &default_buffer, 0, 1,
308 /* tracing one link only */
309 int default_onelink_only = 0;
310 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_ONELINK_ONLY,
311 "Use only routes with one link to trace platform.",
312 xbt_cfgelm_int, &default_onelink_only, 0, 1,
315 /* disable destroy */
316 int default_disable_destroy = 0;
317 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_DISABLE_DESTROY,
318 "Disable platform containers destruction.",
319 xbt_cfgelm_int, &default_disable_destroy, 0, 1,
322 /* basic -- Avoid extended events (impoverished trace file) */
323 int default_basic = 0;
324 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_BASIC,
325 "Avoid extended events (impoverished trace file).",
326 xbt_cfgelm_int, &default_basic, 0, 1,
329 /* Triva graph configuration for uncategorized tracing */
330 char *default_triva_uncat_conf_file = xbt_strdup ("");
331 xbt_cfg_register(&_surf_cfg_set, OPT_TRIVA_UNCAT_CONF,
332 "Triva Graph configuration file for uncategorized resource utilization traces.",
333 xbt_cfgelm_string, &default_triva_uncat_conf_file, 1, 1,
336 /* Triva graph configuration for uncategorized tracing */
337 char *default_triva_cat_conf_file = xbt_strdup ("");
338 xbt_cfg_register(&_surf_cfg_set, OPT_TRIVA_CAT_CONF,
339 "Triva Graph configuration file for categorized resource utilization traces.",
340 xbt_cfgelm_string, &default_triva_cat_conf_file, 1, 1,
343 /* Viva graph configuration for uncategorized tracing */
344 char *default_viva_uncat_conf_file = xbt_strdup ("");
345 xbt_cfg_register(&_surf_cfg_set, OPT_VIVA_UNCAT_CONF,
346 "Viva Graph configuration file for uncategorized resource utilization traces.",
347 xbt_cfgelm_string, &default_viva_uncat_conf_file, 1, 1,
350 /* Viva graph configuration for uncategorized tracing */
351 char *default_viva_cat_conf_file = xbt_strdup ("");
352 xbt_cfg_register(&_surf_cfg_set, OPT_VIVA_CAT_CONF,
353 "Viva Graph configuration file for categorized resource utilization traces.",
354 xbt_cfgelm_string, &default_viva_cat_conf_file, 1, 1,
357 /* instrumentation can be considered configured now */
358 trace_configured = 1;
361 static void print_line (const char *option, const char *desc, const char *longdesc, int detailed)
363 char str[INSTR_DEFAULT_STR_SIZE];
364 snprintf (str, INSTR_DEFAULT_STR_SIZE, "--cfg=%s ", option);
366 int len = strlen (str);
367 printf ("%s%*.*s %s\n", str, 30-len, 30-len, "", desc);
368 if (!!longdesc && detailed){
369 printf ("%s\n\n", longdesc);
373 void TRACE_help (int detailed)
376 "Description of the tracing options accepted by this simulator:\n\n");
377 print_line (OPT_TRACING, "Enable the tracing system",
378 " It activates the tracing system and register the simulation platform\n"
379 " in the trace file. You have to enable this option to others take effect.",
381 print_line (OPT_TRACING_CATEGORIZED, "Trace categorized resource utilization",
382 " It activates the categorized resource utilization tracing. It should\n"
383 " be enabled if tracing categories are used by this simulator.",
385 print_line (OPT_TRACING_UNCATEGORIZED, "Trace uncategorized resource utilization",
386 " It activates the uncategorized resource utilization tracing. Use it if\n"
387 " this simulator do not use tracing categories and resource use have to be\n"
390 print_line (OPT_TRACING_FILENAME, "Filename to register traces",
391 " A file with this name will be created to register the simulation. The file\n"
392 " is in the Paje format and can be analyzed using Triva or Paje visualization\n"
393 " tools. More information can be found in these webpages:\n"
394 " http://triva.gforge.inria.fr/\n"
395 " http://paje.sourceforge.net/",
397 print_line (OPT_TRACING_SMPI, "Trace the MPI Interface (SMPI)",
398 " This option only has effect if this simulator is SMPI-based. Traces the MPI\n"
399 " interface and generates a trace that can be analyzed using Gantt-like\n"
400 " visualizations. Every MPI function (implemented by SMPI) is transformed in a\n"
401 " state, and point-to-point communications can be analyzed with arrows.",
403 print_line (OPT_TRACING_SMPI_GROUP, "Group MPI processes by host (SMPI)",
404 " This option only has effect if this simulator is SMPI-based. The processes\n"
405 " are grouped by the hosts where they were executed.",
407 print_line (OPT_TRACING_SMPI_COMPUTING, "Generates a \" Computing \" State",
408 " This option aims at tracing computations in the application, outside SMPI\n"
409 " to allow further study of simulated or real computation time",
411 print_line (OPT_TRACING_MSG_PROCESS, "Trace processes behavior (MSG)",
412 " This option only has effect if this simulator is MSG-based. It traces the\n"
413 " behavior of all categorized MSG processes, grouping them by hosts. This option\n"
414 " can be used to track process location if this simulator has process migration.",
416 print_line (OPT_TRACING_BUFFER, "Buffer events to put them in temporal order",
417 " This option put some events in a time-ordered buffer using the insertion\n"
418 " sort algorithm. The process of acquiring and releasing locks to access this\n"
419 " buffer and the cost of the sorting algorithm make this process slow. The\n"
420 " simulator performance can be severely impacted if this option is activated,\n"
421 " but you are sure to get a trace file with events sorted.",
423 print_line (OPT_TRACING_ONELINK_ONLY, "Consider only one link routes to trace platform",
424 " This option changes the way SimGrid register its platform on the trace file.\n"
425 " Normally, the tracing considers all routes (no matter their size) on the\n"
426 " platform file to re-create the resource topology. If this option is activated,\n"
427 " only the routes with one link are used to register the topology within an AS.\n"
428 " Routes among AS continue to be traced as usual.",
430 print_line (OPT_TRACING_DISABLE_DESTROY, "Disable platform containers destruction",
431 " Disable the destruction of containers at the end of simulation. This can be\n"
432 " used with simulators that have a different notion of time (different from\n"
433 " the simulated time).",
435 print_line (OPT_TRACING_BASIC, "Avoid extended events (impoverished trace file).",
436 " Some visualization tools are not able to parse correctly the Paje file format.\n"
437 " Use this option if you are using one of these tools to visualize the simulation\n"
438 " trace. Keep in mind that the trace might be incomplete, without all the\n"
439 " information that would be registered otherwise.",
441 print_line (OPT_TRIVA_UNCAT_CONF, "Generate graph configuration for Triva",
442 " This option can be used in all types of simulators build with SimGrid\n"
443 " to generate a uncategorized resource utilization graph to be used as\n"
444 " configuration for the Triva visualization analysis. This option\n"
445 " can be used with tracing/categorized:1 and tracing:1 options to\n"
446 " analyze an unmodified simulator before changing it to contain\n"
449 print_line (OPT_TRIVA_CAT_CONF, "generate uncategorized graph configuration for Triva",
450 " This option can be used if this simulator uses tracing categories\n"
451 " in its code. The file specified by this option holds a graph configuration\n"
452 " file for the Triva visualization tool that can be used to analyze a categorized\n"
453 " resource utilization.",
455 print_line (OPT_VIVA_UNCAT_CONF, "Generate a graph configuration for Viva",
456 " This option can be used in all types of simulators build with SimGrid\n"
457 " to generate a uncategorized resource utilization graph to be used as\n"
458 " configuration for the Viva visualization tool. This option\n"
459 " can be used with tracing/categorized:1 and tracing:1 options to\n"
460 " analyze an unmodified simulator before changing it to contain\n"
463 print_line (OPT_VIVA_CAT_CONF, "Generate an uncategorized graph configuration for Viva",
464 " This option can be used if this simulator uses tracing categories\n"
465 " in its code. The file specified by this option holds a graph configuration\n"
466 " file for the Viva visualization tool that can be used to analyze a categorized\n"
467 " resource utilization.",
471 static void output_types (const char *name, xbt_dynar_t types, FILE *file)
474 fprintf (file, " %s = (", name);
475 for (i = xbt_dynar_length(types); i > 0; i--) {
476 char *type = *(char**)xbt_dynar_get_ptr(types, i - 1);
477 fprintf (file, "\"%s\"", type);
481 fprintf (file, ");\n");
484 xbt_dynar_free (&types);
487 static void output_categories (const char *name, xbt_dynar_t cats, FILE *file)
490 fprintf (file, " values = (");
491 for (i = xbt_dynar_length(cats); i > 0; i--) {
492 char *cat = *(char**)xbt_dynar_get_ptr(cats, i - 1);
493 fprintf (file, "\"%s%s\"", name, cat);
497 fprintf (file, ");\n");
500 xbt_dynar_free (&cats);
503 static void uncat_configuration (FILE *file)
505 //register NODE and EDGE types
506 output_types ("node", TRACE_get_node_types(), file);
507 output_types ("edge", TRACE_get_edge_types(), file);
508 fprintf (file, "\n");
510 //configuration for all nodes
513 " type = \"square\";\n"
514 " size = \"power\";\n"
515 " values = (\"power_used\");\n"
518 " type = \"rhombus\";\n"
519 " size = \"bandwidth\";\n"
520 " values = (\"bandwidth_used\");\n"
525 static void cat_configuration (FILE *file)
527 //register NODE and EDGE types
528 output_types ("node", TRACE_get_node_types(), file);
529 output_types ("edge", TRACE_get_edge_types(), file);
530 fprintf (file, "\n");
532 //configuration for all nodes
535 " type = \"square\";\n"
536 " size = \"power\";\n");
537 output_categories ("p", TRACE_get_categories(), file);
541 " type = \"rhombus\";\n"
542 " size = \"bandwidth\";\n");
543 output_categories ("b", TRACE_get_categories(), file);
544 fprintf (file, " };\n");
548 static void generate_uncat_configuration (const char *output, const char *name, int brackets)
550 if (output && strlen(output) > 0){
551 FILE *file = fopen (output, "w");
553 THROWF (system_error, 1, "Unable to open file (%s) for writing %s graph "
554 "configuration (uncategorized).", output, name);
557 if (brackets) fprintf (file, "{\n");
558 uncat_configuration (file);
559 if (brackets) fprintf (file, "}\n");
564 static void generate_cat_configuration (const char *output, const char *name, int brackets)
566 if (output && strlen(output) > 0){
567 //check if we do have categories declared
568 if (xbt_dict_is_empty(created_categories)){
569 XBT_INFO("No categories declared, ignoring generation of %s graph configuration", name);
573 FILE *file = fopen (output, "w");
575 THROWF (system_error, 1, "Unable to open file (%s) for writing %s graph "
576 "configuration (categorized).", output, name);
579 if (brackets) fprintf (file, "{\n");
580 cat_configuration (file);
581 if (brackets) fprintf (file, "}\n");
586 void TRACE_generate_triva_uncat_conf (void)
588 generate_uncat_configuration (TRACE_get_triva_uncat_conf (), "triva", 1);
591 void TRACE_generate_triva_cat_conf (void)
593 generate_cat_configuration (TRACE_get_triva_cat_conf(), "triva", 1);
596 void TRACE_generate_viva_uncat_conf (void)
598 generate_uncat_configuration (TRACE_get_viva_uncat_conf (), "viva", 0);
601 void TRACE_generate_viva_cat_conf (void)
603 generate_cat_configuration (TRACE_get_viva_cat_conf(), "viva", 0);
607 #undef OPT_TRACING_PLATFORM
608 #undef OPT_TRACING_SMPI
609 #undef OPT_TRACING_SMPI_GROUP
610 #undef OPT_TRACING_CATEGORIZED
611 #undef OPT_TRACING_UNCATEGORIZED
612 #undef OPT_TRACING_MSG_PROCESS
613 #undef OPT_TRACING_FILENAME
614 #undef OPT_TRACING_BUFFER
615 #undef OPT_TRACING_ONELINK_ONLY
616 #undef OPT_TRACING_DISABLE_DESTROY
617 #undef OPT_TRACING_BASIC
618 #undef OPT_TRIVA_UNCAT_CONF
619 #undef OPT_TRIVA_CAT_CONF
620 #undef OPT_VIVA_UNCAT_CONF
621 #undef OPT_VIVA_CAT_CONF
623 #endif /* HAVE_TRACING */