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_surf_release();
112 TRACE_smpi_release();
113 xbt_dict_free(&created_categories);
115 /* close the trace file */
118 /* de-activate trace */
120 XBT_DEBUG ("Tracing is off");
121 XBT_DEBUG("Tracing system is shutdown");
125 int TRACE_needs_platform (void)
127 return TRACE_msg_process_is_enabled() ||
128 TRACE_categorized() ||
129 TRACE_uncategorized() ||
131 (TRACE_smpi_is_enabled() && TRACE_smpi_is_grouped());
134 int TRACE_is_enabled(void)
136 return trace_enabled;
139 int TRACE_platform(void)
141 return trace_platform;
144 int TRACE_is_configured(void)
146 return trace_configured;
149 int TRACE_smpi_is_enabled(void)
151 return (trace_smpi_enabled || TRACE_smpi_is_grouped())
152 && TRACE_is_enabled();
155 int TRACE_smpi_is_grouped(void)
157 return trace_smpi_grouped;
160 int TRACE_categorized (void)
162 return trace_categorized;
165 int TRACE_uncategorized (void)
167 return trace_uncategorized;
170 int TRACE_msg_process_is_enabled(void)
172 return trace_msg_process_enabled && TRACE_is_enabled();
175 int TRACE_buffer (void)
177 return trace_buffer && TRACE_is_enabled();
180 int TRACE_onelink_only (void)
182 return trace_onelink_only && TRACE_is_enabled();
185 int TRACE_disable_destroy (void)
187 return trace_disable_destroy && TRACE_is_enabled();
190 char *TRACE_get_filename(void)
192 return xbt_cfg_get_string(_surf_cfg_set, OPT_TRACING_FILENAME);
195 char *TRACE_get_triva_uncat_conf (void)
197 return xbt_cfg_get_string(_surf_cfg_set, OPT_TRIVA_UNCAT_CONF);
200 char *TRACE_get_triva_cat_conf (void)
202 return xbt_cfg_get_string(_surf_cfg_set, OPT_TRIVA_CAT_CONF);
205 char *TRACE_get_viva_uncat_conf (void)
207 return xbt_cfg_get_string(_surf_cfg_set, OPT_VIVA_UNCAT_CONF);
210 char *TRACE_get_viva_cat_conf (void)
212 return xbt_cfg_get_string(_surf_cfg_set, OPT_VIVA_CAT_CONF);
215 void TRACE_global_init(int *argc, char **argv)
217 /* name of the tracefile */
218 char *default_tracing_filename = xbt_strdup("simgrid.trace");
219 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_FILENAME,
220 "Trace file created by the instrumented SimGrid.",
221 xbt_cfgelm_string, &default_tracing_filename, 1, 1,
225 int default_tracing = 0;
226 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING,
228 xbt_cfgelm_int, &default_tracing, 0, 1,
231 /* register platform in the trace */
232 int default_tracing_platform = 0;
233 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_PLATFORM,
234 "Register the platform in the trace as a graph.",
235 xbt_cfgelm_int, &default_tracing_platform, 0, 1,
239 int default_tracing_smpi = 0;
240 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_SMPI,
241 "Tracing of the SMPI interface.",
242 xbt_cfgelm_int, &default_tracing_smpi, 0, 1,
246 int default_tracing_smpi_grouped = 0;
247 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_SMPI_GROUP,
248 "Group MPI processes by host.",
249 xbt_cfgelm_int, &default_tracing_smpi_grouped, 0, 1,
253 /* tracing categorized resource utilization traces */
254 int default_tracing_categorized = 0;
255 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_CATEGORIZED,
256 "Tracing categorized resource utilization of hosts and links.",
257 xbt_cfgelm_int, &default_tracing_categorized, 0, 1,
260 /* tracing uncategorized resource utilization */
261 int default_tracing_uncategorized = 0;
262 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_UNCATEGORIZED,
263 "Tracing uncategorized resource utilization of hosts and links.",
264 xbt_cfgelm_int, &default_tracing_uncategorized, 0, 1,
268 int default_tracing_msg_process = 0;
269 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_MSG_PROCESS,
270 "Tracing of MSG process behavior.",
271 xbt_cfgelm_int, &default_tracing_msg_process, 0, 1,
275 int default_buffer = 1;
276 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_BUFFER,
277 "Buffer trace events to put them in temporal order.",
278 xbt_cfgelm_int, &default_buffer, 0, 1,
281 /* tracing one link only */
282 int default_onelink_only = 0;
283 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_ONELINK_ONLY,
284 "Use only routes with one link to trace platform.",
285 xbt_cfgelm_int, &default_onelink_only, 0, 1,
288 /* disable destroy */
289 int default_disable_destroy = 0;
290 xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_DISABLE_DESTROY,
291 "Disable platform containers destruction.",
292 xbt_cfgelm_int, &default_disable_destroy, 0, 1,
295 /* Triva graph configuration for uncategorized tracing */
296 char *default_triva_uncat_conf_file = xbt_strdup ("");
297 xbt_cfg_register(&_surf_cfg_set, OPT_TRIVA_UNCAT_CONF,
298 "Triva Graph configuration file for uncategorized resource utilization traces.",
299 xbt_cfgelm_string, &default_triva_uncat_conf_file, 1, 1,
302 /* Triva graph configuration for uncategorized tracing */
303 char *default_triva_cat_conf_file = xbt_strdup ("");
304 xbt_cfg_register(&_surf_cfg_set, OPT_TRIVA_CAT_CONF,
305 "Triva Graph configuration file for categorized resource utilization traces.",
306 xbt_cfgelm_string, &default_triva_cat_conf_file, 1, 1,
309 /* Viva graph configuration for uncategorized tracing */
310 char *default_viva_uncat_conf_file = xbt_strdup ("");
311 xbt_cfg_register(&_surf_cfg_set, OPT_VIVA_UNCAT_CONF,
312 "Viva Graph configuration file for uncategorized resource utilization traces.",
313 xbt_cfgelm_string, &default_viva_uncat_conf_file, 1, 1,
316 /* Viva graph configuration for uncategorized tracing */
317 char *default_viva_cat_conf_file = xbt_strdup ("");
318 xbt_cfg_register(&_surf_cfg_set, OPT_VIVA_CAT_CONF,
319 "Viva Graph configuration file for categorized resource utilization traces.",
320 xbt_cfgelm_string, &default_viva_cat_conf_file, 1, 1,
323 /* instrumentation can be considered configured now */
324 trace_configured = 1;
327 static void print_line (const char *option, const char *desc, const char *longdesc, int detailed)
329 char str[INSTR_DEFAULT_STR_SIZE];
330 snprintf (str, INSTR_DEFAULT_STR_SIZE, "--cfg=%s ", option);
332 int len = strlen (str);
333 printf ("%s%*.*s %s\n", str, 30-len, 30-len, "", desc);
334 if (!!longdesc && detailed){
335 printf ("%s\n\n", longdesc);
339 void TRACE_help (int detailed)
342 "Description of the tracing options accepted by this simulator:\n\n");
343 print_line (OPT_TRACING, "Enable the tracing system",
344 " It activates the tracing system and register the simulation platform\n"
345 " in the trace file. You have to enable this option to others take effect.",
347 print_line (OPT_TRACING_CATEGORIZED, "Trace categorized resource utilization",
348 " It activates the categorized resource utilization tracing. It should\n"
349 " be enabled if tracing categories are used by this simulator.",
351 print_line (OPT_TRACING_UNCATEGORIZED, "Trace uncategorized resource utilization",
352 " It activates the uncategorized resource utilization tracing. Use it if\n"
353 " this simulator do not use tracing categories and resource use have to be\n"
356 print_line (OPT_TRACING_FILENAME, "Filename to register traces",
357 " A file with this name will be created to register the simulation. The file\n"
358 " is in the Paje format and can be analyzed using Triva or Paje visualization\n"
359 " tools. More information can be found in these webpages:\n"
360 " http://triva.gforge.inria.fr/\n"
361 " http://paje.sourceforge.net/",
363 print_line (OPT_TRACING_SMPI, "Trace the MPI Interface (SMPI)",
364 " This option only has effect if this simulator is SMPI-based. Traces the MPI\n"
365 " interface and generates a trace that can be analyzed using Gantt-like\n"
366 " visualizations. Every MPI function (implemented by SMPI) is transformed in a\n"
367 " state, and point-to-point communications can be analyzed with arrows.",
369 print_line (OPT_TRACING_SMPI_GROUP, "Group MPI processes by host (SMPI)",
370 " This option only has effect if this simulator is SMPI-based. The processes\n"
371 " are grouped by the hosts where they were executed.",
373 print_line (OPT_TRACING_MSG_PROCESS, "Trace processes behavior (MSG)",
374 " This option only has effect if this simulator is MSG-based. It traces the\n"
375 " behavior of all categorized MSG processes, grouping them by hosts. This option\n"
376 " can be used to track process location if this simulator has process migration.",
378 print_line (OPT_TRACING_BUFFER, "Buffer events to put them in temporal order",
379 " This option put some events in a time-ordered buffer using the insertion\n"
380 " sort algorithm. The process of acquiring and releasing locks to access this\n"
381 " buffer and the cost of the sorting algorithm make this process slow. The\n"
382 " simulator performance can be severely impacted if this option is activated,\n"
383 " but you are sure to get a trace file with events sorted.",
385 print_line (OPT_TRACING_ONELINK_ONLY, "Consider only one link routes to trace platform",
386 " This option changes the way SimGrid register its platform on the trace file.\n"
387 " Normally, the tracing considers all routes (no matter their size) on the\n"
388 " platform file to re-create the resource topology. If this option is activated,\n"
389 " only the routes with one link are used to register the topology within an AS.\n"
390 " Routes among AS continue to be traced as usual.",
392 print_line (OPT_TRACING_DISABLE_DESTROY, "Disable platform containers destruction",
393 " Disable the destruction of containers at the end of simulation. This can be\n"
394 " used with simulators that have a different notion of time (different from\n"
395 " the simulated time).",
397 print_line (OPT_TRIVA_UNCAT_CONF, "Generate graph configuration for Triva",
398 " This option can be used in all types of simulators build with SimGrid\n"
399 " to generate a uncategorized resource utilization graph to be used as\n"
400 " configuration for the Triva visualization analysis. This option\n"
401 " can be used with tracing/categorized:1 and tracing:1 options to\n"
402 " analyze an unmodified simulator before changing it to contain\n"
405 print_line (OPT_TRIVA_CAT_CONF, "generate uncategorized graph configuration for Triva",
406 " This option can be used if this simulator uses tracing categories\n"
407 " in its code. The file specified by this option holds a graph configuration\n"
408 " file for the Triva visualization tool that can be used to analyze a categorized\n"
409 " resource utilization.",
411 print_line (OPT_VIVA_UNCAT_CONF, "Generate a graph configuration for Viva",
412 " This option can be used in all types of simulators build with SimGrid\n"
413 " to generate a uncategorized resource utilization graph to be used as\n"
414 " configuration for the Viva visualization tool. This option\n"
415 " can be used with tracing/categorized:1 and tracing:1 options to\n"
416 " analyze an unmodified simulator before changing it to contain\n"
419 print_line (OPT_VIVA_CAT_CONF, "Generate an uncategorized graph configuration for Viva",
420 " This option can be used if this simulator uses tracing categories\n"
421 " in its code. The file specified by this option holds a graph configuration\n"
422 " file for the Viva visualization tool that can be used to analyze a categorized\n"
423 " resource utilization.",
427 static void output_types (const char *name, xbt_dynar_t types, FILE *file)
430 fprintf (file, " %s = (", name);
431 for (i = xbt_dynar_length(types); i > 0; i--) {
432 char *type = *(char**)xbt_dynar_get_ptr(types, i - 1);
433 fprintf (file, "\"%s\"", type);
437 fprintf (file, ");\n");
440 xbt_dynar_free (&types);
443 static void output_categories (const char *name, xbt_dynar_t cats, FILE *file)
446 fprintf (file, " values = (");
447 for (i = xbt_dynar_length(cats); i > 0; i--) {
448 char *cat = *(char**)xbt_dynar_get_ptr(cats, i - 1);
449 fprintf (file, "\"%s%s\"", name, cat);
453 fprintf (file, ");\n");
456 xbt_dynar_free (&cats);
459 static void uncat_configuration (FILE *file)
461 //register NODE and EDGE types
462 output_types ("node", TRACE_get_node_types(), file);
463 output_types ("edge", TRACE_get_edge_types(), file);
464 fprintf (file, "\n");
466 //configuration for all nodes
469 " type = \"square\";\n"
470 " size = \"power\";\n"
471 " values = (\"power_used\");\n"
474 " type = \"rhombus\";\n"
475 " size = \"bandwidth\";\n"
476 " values = (\"bandwidth_used\");\n"
481 static void cat_configuration (FILE *file)
483 //register NODE and EDGE types
484 output_types ("node", TRACE_get_node_types(), file);
485 output_types ("edge", TRACE_get_edge_types(), file);
486 fprintf (file, "\n");
488 //configuration for all nodes
491 " type = \"square\";\n"
492 " size = \"power\";\n");
493 output_categories ("p", TRACE_get_categories(), file);
497 " type = \"rhombus\";\n"
498 " size = \"bandwidth\";\n");
499 output_categories ("b", TRACE_get_categories(), file);
500 fprintf (file, " };\n");
504 static void generate_uncat_configuration (const char *output, const char *name, int brackets)
506 if (output && strlen(output) > 0){
507 FILE *file = fopen (output, "w");
509 THROWF (system_error, 1, "Unable to open file (%s) for writing %s graph "
510 "configuration (uncategorized).", output, name);
513 if (brackets) fprintf (file, "{\n");
514 uncat_configuration (file);
515 if (brackets) fprintf (file, "}\n");
520 static void generate_cat_configuration (const char *output, const char *name, int brackets)
522 if (output && strlen(output) > 0){
523 //check if we do have categories declared
524 if (xbt_dict_is_empty(created_categories)){
525 XBT_INFO("No categories declared, ignoring generation of %s graph configuration", name);
529 FILE *file = fopen (output, "w");
531 THROWF (system_error, 1, "Unable to open file (%s) for writing %s graph "
532 "configuration (categorized).", output, name);
535 if (brackets) fprintf (file, "{\n");
536 cat_configuration (file);
537 if (brackets) fprintf (file, "}\n");
542 void TRACE_generate_triva_uncat_conf (void)
544 generate_uncat_configuration (TRACE_get_triva_uncat_conf (), "triva", 1);
547 void TRACE_generate_triva_cat_conf (void)
549 generate_cat_configuration (TRACE_get_triva_cat_conf(), "triva", 1);
552 void TRACE_generate_viva_uncat_conf (void)
554 generate_uncat_configuration (TRACE_get_viva_uncat_conf (), "viva", 0);
557 void TRACE_generate_viva_cat_conf (void)
559 generate_cat_configuration (TRACE_get_viva_cat_conf(), "viva", 0);
563 #undef OPT_TRACING_PLATFORM
564 #undef OPT_TRACING_SMPI
565 #undef OPT_TRACING_SMPI_GROUP
566 #undef OPT_TRACING_CATEGORIZED
567 #undef OPT_TRACING_UNCATEGORIZED
568 #undef OPT_TRACING_MSG_PROCESS
569 #undef OPT_TRACING_FILENAME
570 #undef OPT_TRACING_BUFFER
571 #undef OPT_TRACING_ONELINK_ONLY
572 #undef OPT_TRACING_DISABLE_DESTROY
573 #undef OPT_TRIVA_UNCAT_CONF
574 #undef OPT_TRIVA_CAT_CONF
575 #undef OPT_VIVA_UNCAT_CONF
576 #undef OPT_VIVA_CAT_CONF
578 #endif /* HAVE_TRACING */