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_TRACING_BASIC "tracing/basic"
27 #define OPT_TRIVA_UNCAT_CONF "triva/uncategorized"
28 #define OPT_TRIVA_CAT_CONF "triva/categorized"
29 #define OPT_VIVA_UNCAT_CONF "viva/uncategorized"
30 #define OPT_VIVA_CAT_CONF "viva/categorized"
32 static int trace_enabled;
33 static int trace_platform;
34 static int trace_smpi_enabled;
35 static int trace_smpi_grouped;
36 static int trace_categorized;
37 static int trace_uncategorized;
38 static int trace_msg_process_enabled;
39 static int trace_buffer;
40 static int trace_onelink_only;
41 static int trace_disable_destroy;
42 static int trace_basic;
44 static int trace_configured = 0;
45 static int trace_active = 0;
47 static void TRACE_getopts(void)
49 trace_enabled = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING);
50 trace_platform = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_PLATFORM);
51 trace_smpi_enabled = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_SMPI);
52 trace_smpi_grouped = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_SMPI_GROUP);
53 trace_categorized = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_CATEGORIZED);
54 trace_uncategorized = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_UNCATEGORIZED);
55 trace_msg_process_enabled = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_MSG_PROCESS);
56 trace_buffer = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_BUFFER);
57 trace_onelink_only = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_ONELINK_ONLY);
58 trace_disable_destroy = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_DISABLE_DESTROY);
59 trace_basic = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_BASIC);
66 // tracing system must be:
67 // - enabled (with --cfg=tracing:1)
68 // - already configured (TRACE_global_init already called)
69 if (!(TRACE_is_enabled() && TRACE_is_configured())){
73 XBT_DEBUG("Tracing starts");
75 /* open the trace file */
79 if (trace_active == 1){
80 THROWF (tracing_error, 0, "Tracing is already active");
83 XBT_DEBUG ("Tracing is on");
85 /* other trace initialization */
86 created_categories = xbt_dict_new_homogeneous(xbt_free);
87 declared_marks = xbt_dict_new_homogeneous (xbt_free);
88 user_host_variables = xbt_dict_new_homogeneous (xbt_free);
89 user_link_variables = xbt_dict_new_homogeneous (xbt_free);
100 TRACE_generate_triva_uncat_conf();
101 TRACE_generate_triva_cat_conf();
102 TRACE_generate_viva_uncat_conf();
103 TRACE_generate_viva_cat_conf();
105 /* dump trace buffer */
106 TRACE_last_timestamp_to_dump = surf_get_clock();
107 TRACE_paje_dump_buffer(1);
109 /* destroy all data structures of tracing (and free) */
110 PJ_container_free_all();
112 PJ_container_release();
114 TRACE_smpi_release();
115 TRACE_surf_release();
116 xbt_dict_free(&user_link_variables);
117 xbt_dict_free(&user_host_variables);
118 xbt_dict_free(&declared_marks);
119 xbt_dict_free(&created_categories);
121 /* close the trace file */
124 /* de-activate trace */
126 XBT_DEBUG ("Tracing is off");
127 XBT_DEBUG("Tracing system is shutdown");
131 int TRACE_needs_platform (void)
133 return TRACE_msg_process_is_enabled() ||
134 TRACE_categorized() ||
135 TRACE_uncategorized() ||
137 (TRACE_smpi_is_enabled() && TRACE_smpi_is_grouped());
140 int TRACE_is_enabled(void)
142 return trace_enabled;
145 int TRACE_platform(void)
147 return trace_platform;
150 int TRACE_is_configured(void)
152 return trace_configured;
155 int TRACE_smpi_is_enabled(void)
157 return (trace_smpi_enabled || TRACE_smpi_is_grouped())
158 && TRACE_is_enabled();
161 int TRACE_smpi_is_grouped(void)
163 return trace_smpi_grouped;
166 int TRACE_categorized (void)
168 return trace_categorized;
171 int TRACE_uncategorized (void)
173 return trace_uncategorized;
176 int TRACE_msg_process_is_enabled(void)
178 return trace_msg_process_enabled && TRACE_is_enabled();
181 int TRACE_buffer (void)
183 return trace_buffer && TRACE_is_enabled();
186 int TRACE_onelink_only (void)
188 return trace_onelink_only && TRACE_is_enabled();
191 int TRACE_disable_destroy (void)
193 return trace_disable_destroy && TRACE_is_enabled();
196 int TRACE_basic (void)
198 return trace_basic && TRACE_is_enabled();
202 char *TRACE_get_filename(void)
204 return xbt_cfg_get_string(_surf_cfg_set, OPT_TRACING_FILENAME);
207 char *TRACE_get_triva_uncat_conf (void)
209 return xbt_cfg_get_string(_surf_cfg_set, OPT_TRIVA_UNCAT_CONF);
212 char *TRACE_get_triva_cat_conf (void)
214 return xbt_cfg_get_string(_surf_cfg_set, OPT_TRIVA_CAT_CONF);
217 char *TRACE_get_viva_uncat_conf (void)
219 return xbt_cfg_get_string(_surf_cfg_set, OPT_VIVA_UNCAT_CONF);
222 char *TRACE_get_viva_cat_conf (void)
224 return xbt_cfg_get_string(_surf_cfg_set, OPT_VIVA_CAT_CONF);
227 void TRACE_global_init(int *argc, char **argv)
229 /* name of the tracefile */
230 char *default_tracing_filename = xbt_strdup("simgrid.trace");
231 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_FILENAME,
232 "Trace file created by the instrumented SimGrid.",
233 xbt_cfgelm_string, &default_tracing_filename, 1, 1,
237 int default_tracing = 0;
238 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING,
240 xbt_cfgelm_int, &default_tracing, 0, 1,
243 /* register platform in the trace */
244 int default_tracing_platform = 0;
245 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_PLATFORM,
246 "Register the platform in the trace as a graph.",
247 xbt_cfgelm_int, &default_tracing_platform, 0, 1,
251 int default_tracing_smpi = 0;
252 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_SMPI,
253 "Tracing of the SMPI interface.",
254 xbt_cfgelm_int, &default_tracing_smpi, 0, 1,
258 int default_tracing_smpi_grouped = 0;
259 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_SMPI_GROUP,
260 "Group MPI processes by host.",
261 xbt_cfgelm_int, &default_tracing_smpi_grouped, 0, 1,
265 /* tracing categorized resource utilization traces */
266 int default_tracing_categorized = 0;
267 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_CATEGORIZED,
268 "Tracing categorized resource utilization of hosts and links.",
269 xbt_cfgelm_int, &default_tracing_categorized, 0, 1,
272 /* tracing uncategorized resource utilization */
273 int default_tracing_uncategorized = 0;
274 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_UNCATEGORIZED,
275 "Tracing uncategorized resource utilization of hosts and links.",
276 xbt_cfgelm_int, &default_tracing_uncategorized, 0, 1,
280 int default_tracing_msg_process = 0;
281 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_MSG_PROCESS,
282 "Tracing of MSG process behavior.",
283 xbt_cfgelm_int, &default_tracing_msg_process, 0, 1,
287 int default_buffer = 1;
288 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_BUFFER,
289 "Buffer trace events to put them in temporal order.",
290 xbt_cfgelm_int, &default_buffer, 0, 1,
293 /* tracing one link only */
294 int default_onelink_only = 0;
295 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_ONELINK_ONLY,
296 "Use only routes with one link to trace platform.",
297 xbt_cfgelm_int, &default_onelink_only, 0, 1,
300 /* disable destroy */
301 int default_disable_destroy = 0;
302 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_DISABLE_DESTROY,
303 "Disable platform containers destruction.",
304 xbt_cfgelm_int, &default_disable_destroy, 0, 1,
307 /* basic -- Avoid extended events (impoverished trace file) */
308 int default_basic = 0;
309 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_BASIC,
310 "Avoid extended events (impoverished trace file).",
311 xbt_cfgelm_int, &default_basic, 0, 1,
314 /* Triva graph configuration for uncategorized tracing */
315 char *default_triva_uncat_conf_file = xbt_strdup ("");
316 xbt_cfg_register(&_surf_cfg_set, OPT_TRIVA_UNCAT_CONF,
317 "Triva Graph configuration file for uncategorized resource utilization traces.",
318 xbt_cfgelm_string, &default_triva_uncat_conf_file, 1, 1,
321 /* Triva graph configuration for uncategorized tracing */
322 char *default_triva_cat_conf_file = xbt_strdup ("");
323 xbt_cfg_register(&_surf_cfg_set, OPT_TRIVA_CAT_CONF,
324 "Triva Graph configuration file for categorized resource utilization traces.",
325 xbt_cfgelm_string, &default_triva_cat_conf_file, 1, 1,
328 /* Viva graph configuration for uncategorized tracing */
329 char *default_viva_uncat_conf_file = xbt_strdup ("");
330 xbt_cfg_register(&_surf_cfg_set, OPT_VIVA_UNCAT_CONF,
331 "Viva Graph configuration file for uncategorized resource utilization traces.",
332 xbt_cfgelm_string, &default_viva_uncat_conf_file, 1, 1,
335 /* Viva graph configuration for uncategorized tracing */
336 char *default_viva_cat_conf_file = xbt_strdup ("");
337 xbt_cfg_register(&_surf_cfg_set, OPT_VIVA_CAT_CONF,
338 "Viva Graph configuration file for categorized resource utilization traces.",
339 xbt_cfgelm_string, &default_viva_cat_conf_file, 1, 1,
342 /* instrumentation can be considered configured now */
343 trace_configured = 1;
346 static void print_line (const char *option, const char *desc, const char *longdesc, int detailed)
348 char str[INSTR_DEFAULT_STR_SIZE];
349 snprintf (str, INSTR_DEFAULT_STR_SIZE, "--cfg=%s ", option);
351 int len = strlen (str);
352 printf ("%s%*.*s %s\n", str, 30-len, 30-len, "", desc);
353 if (!!longdesc && detailed){
354 printf ("%s\n\n", longdesc);
358 void TRACE_help (int detailed)
361 "Description of the tracing options accepted by this simulator:\n\n");
362 print_line (OPT_TRACING, "Enable the tracing system",
363 " It activates the tracing system and register the simulation platform\n"
364 " in the trace file. You have to enable this option to others take effect.",
366 print_line (OPT_TRACING_CATEGORIZED, "Trace categorized resource utilization",
367 " It activates the categorized resource utilization tracing. It should\n"
368 " be enabled if tracing categories are used by this simulator.",
370 print_line (OPT_TRACING_UNCATEGORIZED, "Trace uncategorized resource utilization",
371 " It activates the uncategorized resource utilization tracing. Use it if\n"
372 " this simulator do not use tracing categories and resource use have to be\n"
375 print_line (OPT_TRACING_FILENAME, "Filename to register traces",
376 " A file with this name will be created to register the simulation. The file\n"
377 " is in the Paje format and can be analyzed using Triva or Paje visualization\n"
378 " tools. More information can be found in these webpages:\n"
379 " http://triva.gforge.inria.fr/\n"
380 " http://paje.sourceforge.net/",
382 print_line (OPT_TRACING_SMPI, "Trace the MPI Interface (SMPI)",
383 " This option only has effect if this simulator is SMPI-based. Traces the MPI\n"
384 " interface and generates a trace that can be analyzed using Gantt-like\n"
385 " visualizations. Every MPI function (implemented by SMPI) is transformed in a\n"
386 " state, and point-to-point communications can be analyzed with arrows.",
388 print_line (OPT_TRACING_SMPI_GROUP, "Group MPI processes by host (SMPI)",
389 " This option only has effect if this simulator is SMPI-based. The processes\n"
390 " are grouped by the hosts where they were executed.",
392 print_line (OPT_TRACING_MSG_PROCESS, "Trace processes behavior (MSG)",
393 " This option only has effect if this simulator is MSG-based. It traces the\n"
394 " behavior of all categorized MSG processes, grouping them by hosts. This option\n"
395 " can be used to track process location if this simulator has process migration.",
397 print_line (OPT_TRACING_BUFFER, "Buffer events to put them in temporal order",
398 " This option put some events in a time-ordered buffer using the insertion\n"
399 " sort algorithm. The process of acquiring and releasing locks to access this\n"
400 " buffer and the cost of the sorting algorithm make this process slow. The\n"
401 " simulator performance can be severely impacted if this option is activated,\n"
402 " but you are sure to get a trace file with events sorted.",
404 print_line (OPT_TRACING_ONELINK_ONLY, "Consider only one link routes to trace platform",
405 " This option changes the way SimGrid register its platform on the trace file.\n"
406 " Normally, the tracing considers all routes (no matter their size) on the\n"
407 " platform file to re-create the resource topology. If this option is activated,\n"
408 " only the routes with one link are used to register the topology within an AS.\n"
409 " Routes among AS continue to be traced as usual.",
411 print_line (OPT_TRACING_DISABLE_DESTROY, "Disable platform containers destruction",
412 " Disable the destruction of containers at the end of simulation. This can be\n"
413 " used with simulators that have a different notion of time (different from\n"
414 " the simulated time).",
416 print_line (OPT_TRACING_BASIC, "Avoid extended events (impoverished trace file).",
417 " Some visualization tools are not able to parse correctly the Paje file format.\n"
418 " Use this option if you are using one of these tools to visualize the simulation\n"
419 " trace. Keep in mind that the trace might be incomplete, without all the\n"
420 " information that would be registered otherwise.",
422 print_line (OPT_TRIVA_UNCAT_CONF, "Generate graph configuration for Triva",
423 " This option can be used in all types of simulators build with SimGrid\n"
424 " to generate a uncategorized resource utilization graph to be used as\n"
425 " configuration for the Triva visualization analysis. This option\n"
426 " can be used with tracing/categorized:1 and tracing:1 options to\n"
427 " analyze an unmodified simulator before changing it to contain\n"
430 print_line (OPT_TRIVA_CAT_CONF, "generate uncategorized graph configuration for Triva",
431 " This option can be used if this simulator uses tracing categories\n"
432 " in its code. The file specified by this option holds a graph configuration\n"
433 " file for the Triva visualization tool that can be used to analyze a categorized\n"
434 " resource utilization.",
436 print_line (OPT_VIVA_UNCAT_CONF, "Generate a graph configuration for Viva",
437 " This option can be used in all types of simulators build with SimGrid\n"
438 " to generate a uncategorized resource utilization graph to be used as\n"
439 " configuration for the Viva visualization tool. This option\n"
440 " can be used with tracing/categorized:1 and tracing:1 options to\n"
441 " analyze an unmodified simulator before changing it to contain\n"
444 print_line (OPT_VIVA_CAT_CONF, "Generate an uncategorized graph configuration for Viva",
445 " This option can be used if this simulator uses tracing categories\n"
446 " in its code. The file specified by this option holds a graph configuration\n"
447 " file for the Viva visualization tool that can be used to analyze a categorized\n"
448 " resource utilization.",
452 static void output_types (const char *name, xbt_dynar_t types, FILE *file)
455 fprintf (file, " %s = (", name);
456 for (i = xbt_dynar_length(types); i > 0; i--) {
457 char *type = *(char**)xbt_dynar_get_ptr(types, i - 1);
458 fprintf (file, "\"%s\"", type);
462 fprintf (file, ");\n");
465 xbt_dynar_free (&types);
468 static void output_categories (const char *name, xbt_dynar_t cats, FILE *file)
471 fprintf (file, " values = (");
472 for (i = xbt_dynar_length(cats); i > 0; i--) {
473 char *cat = *(char**)xbt_dynar_get_ptr(cats, i - 1);
474 fprintf (file, "\"%s%s\"", name, cat);
478 fprintf (file, ");\n");
481 xbt_dynar_free (&cats);
484 static void uncat_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 " values = (\"power_used\");\n"
499 " type = \"rhombus\";\n"
500 " size = \"bandwidth\";\n"
501 " values = (\"bandwidth_used\");\n"
506 static void cat_configuration (FILE *file)
508 //register NODE and EDGE types
509 output_types ("node", TRACE_get_node_types(), file);
510 output_types ("edge", TRACE_get_edge_types(), file);
511 fprintf (file, "\n");
513 //configuration for all nodes
516 " type = \"square\";\n"
517 " size = \"power\";\n");
518 output_categories ("p", TRACE_get_categories(), file);
522 " type = \"rhombus\";\n"
523 " size = \"bandwidth\";\n");
524 output_categories ("b", TRACE_get_categories(), file);
525 fprintf (file, " };\n");
529 static void generate_uncat_configuration (const char *output, const char *name, int brackets)
531 if (output && strlen(output) > 0){
532 FILE *file = fopen (output, "w");
534 THROWF (system_error, 1, "Unable to open file (%s) for writing %s graph "
535 "configuration (uncategorized).", output, name);
538 if (brackets) fprintf (file, "{\n");
539 uncat_configuration (file);
540 if (brackets) fprintf (file, "}\n");
545 static void generate_cat_configuration (const char *output, const char *name, int brackets)
547 if (output && strlen(output) > 0){
548 //check if we do have categories declared
549 if (xbt_dict_is_empty(created_categories)){
550 XBT_INFO("No categories declared, ignoring generation of %s graph configuration", name);
554 FILE *file = fopen (output, "w");
556 THROWF (system_error, 1, "Unable to open file (%s) for writing %s graph "
557 "configuration (categorized).", output, name);
560 if (brackets) fprintf (file, "{\n");
561 cat_configuration (file);
562 if (brackets) fprintf (file, "}\n");
567 void TRACE_generate_triva_uncat_conf (void)
569 generate_uncat_configuration (TRACE_get_triva_uncat_conf (), "triva", 1);
572 void TRACE_generate_triva_cat_conf (void)
574 generate_cat_configuration (TRACE_get_triva_cat_conf(), "triva", 1);
577 void TRACE_generate_viva_uncat_conf (void)
579 generate_uncat_configuration (TRACE_get_viva_uncat_conf (), "viva", 0);
582 void TRACE_generate_viva_cat_conf (void)
584 generate_cat_configuration (TRACE_get_viva_cat_conf(), "viva", 0);
588 #undef OPT_TRACING_PLATFORM
589 #undef OPT_TRACING_SMPI
590 #undef OPT_TRACING_SMPI_GROUP
591 #undef OPT_TRACING_CATEGORIZED
592 #undef OPT_TRACING_UNCATEGORIZED
593 #undef OPT_TRACING_MSG_PROCESS
594 #undef OPT_TRACING_FILENAME
595 #undef OPT_TRACING_BUFFER
596 #undef OPT_TRACING_ONELINK_ONLY
597 #undef OPT_TRACING_DISABLE_DESTROY
598 #undef OPT_TRIVA_UNCAT_CONF
599 #undef OPT_TRIVA_CAT_CONF
600 #undef OPT_VIVA_UNCAT_CONF
601 #undef OPT_VIVA_CAT_CONF
603 #endif /* HAVE_TRACING */