Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new tracing options to reduce the size of the traces
[simgrid.git] / src / instr / instr_config.c
1 /* Copyright (c) 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "instr/instr_private.h"
8 #include "simgrid/sg_config.h"
9 #include "surf/surf.h"
10
11 #ifdef HAVE_TRACING
12
13 XBT_LOG_NEW_CATEGORY(instr, "Logging the behavior of the tracing system (used for Visualization/Analysis of simulations)");
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_config, instr, "Configuration");
15
16 #define OPT_TRACING               "tracing"
17 #define OPT_TRACING_PLATFORM      "tracing/platform"
18 #define OPT_TRACING_TOPOLOGY      "tracing/platform/topology"
19 #define OPT_TRACING_SMPI          "tracing/smpi"
20 #define OPT_TRACING_SMPI_GROUP    "tracing/smpi/group"
21 #define OPT_TRACING_SMPI_COMPUTING "tracing/smpi/computing"
22 #define OPT_TRACING_CATEGORIZED   "tracing/categorized"
23 #define OPT_TRACING_UNCATEGORIZED "tracing/uncategorized"
24 #define OPT_TRACING_MSG_PROCESS   "tracing/msg/process"
25 #define OPT_TRACING_MSG_VM        "tracing/msg/vm"
26 #define OPT_TRACING_FILENAME      "tracing/filename"
27 #define OPT_TRACING_BUFFER        "tracing/buffer"
28 #define OPT_TRACING_ONELINK_ONLY  "tracing/onelink_only"
29 #define OPT_TRACING_DISABLE_DESTROY "tracing/disable_destroy"
30 #define OPT_TRACING_BASIC         "tracing/basic"
31 #define OPT_TRACING_COMMENT       "tracing/comment"
32 #define OPT_TRACING_COMMENT_FILE  "tracing/comment_file"
33 #define OPT_VIVA_UNCAT_CONF      "viva/uncategorized"
34 #define OPT_VIVA_CAT_CONF        "viva/categorized"
35 #define OPT_TRACING_DISABLE_LINK        "tracing/disable_link"
36 #define OPT_TRACING_DISABLE_POWER       "tracing/disable_power"
37
38 static int trace_enabled;
39 static int trace_platform;
40 static int trace_platform_topology;
41 static int trace_smpi_enabled;
42 static int trace_smpi_grouped;
43 static int trace_smpi_computing;
44 static int trace_categorized;
45 static int trace_uncategorized;
46 static int trace_msg_process_enabled;
47 static int trace_msg_vm_enabled;
48 static int trace_buffer;
49 static int trace_onelink_only;
50 static int trace_disable_destroy;
51 static int trace_basic;
52 static int trace_disable_link;
53 static int trace_disable_power;
54
55 static int trace_configured = 0;
56 static int trace_active = 0;
57
58
59
60 static void TRACE_getopts(void)
61 {
62   trace_enabled = xbt_cfg_get_int(_sg_cfg_set, OPT_TRACING);
63   trace_platform = xbt_cfg_get_int(_sg_cfg_set, OPT_TRACING_PLATFORM);
64   trace_platform_topology = xbt_cfg_get_int(_sg_cfg_set, OPT_TRACING_TOPOLOGY);
65   trace_smpi_enabled = xbt_cfg_get_int(_sg_cfg_set, OPT_TRACING_SMPI);
66   trace_smpi_grouped = xbt_cfg_get_int(_sg_cfg_set, OPT_TRACING_SMPI_GROUP);
67   trace_smpi_computing = xbt_cfg_get_int(_sg_cfg_set, OPT_TRACING_SMPI_COMPUTING);
68   trace_categorized = xbt_cfg_get_int(_sg_cfg_set, OPT_TRACING_CATEGORIZED);
69   trace_uncategorized = xbt_cfg_get_int(_sg_cfg_set, OPT_TRACING_UNCATEGORIZED);
70   trace_msg_process_enabled = xbt_cfg_get_int(_sg_cfg_set, OPT_TRACING_MSG_PROCESS);
71   trace_msg_vm_enabled = xbt_cfg_get_int(_sg_cfg_set, OPT_TRACING_MSG_VM);
72   trace_buffer = xbt_cfg_get_int(_sg_cfg_set, OPT_TRACING_BUFFER);
73   trace_onelink_only = xbt_cfg_get_int(_sg_cfg_set, OPT_TRACING_ONELINK_ONLY);
74   trace_disable_destroy = xbt_cfg_get_int(_sg_cfg_set, OPT_TRACING_DISABLE_DESTROY);
75   trace_basic = xbt_cfg_get_int(_sg_cfg_set, OPT_TRACING_BASIC);
76   trace_disable_link = xbt_cfg_get_int(_sg_cfg_set, OPT_TRACING_DISABLE_LINK);
77   trace_disable_power = xbt_cfg_get_int(_sg_cfg_set, OPT_TRACING_DISABLE_POWER);
78 }
79
80 static xbt_dynar_t TRACE_start_functions = NULL;
81 void TRACE_add_start_function(void (*func) ())
82 {
83   if (TRACE_start_functions == NULL)
84     TRACE_start_functions = xbt_dynar_new(sizeof(void (*)()), NULL);
85   xbt_dynar_push(TRACE_start_functions, &func);
86 }
87
88 int TRACE_start()
89 {
90   TRACE_getopts();
91
92   // tracing system must be:
93   //    - enabled (with --cfg=tracing:1)
94   //    - already configured (TRACE_global_init already called)
95   if (TRACE_is_enabled() && TRACE_is_configured()) {
96     XBT_DEBUG("Tracing starts");
97
98     /* open the trace file */
99     TRACE_paje_start();
100
101     /* activate trace */
102     if (trace_active == 1) {
103       THROWF(tracing_error, 0, "Tracing is already active");
104     }
105     trace_active = 1;
106     XBT_DEBUG("Tracing is on");
107
108     /* other trace initialization */
109     created_categories = xbt_dict_new_homogeneous(xbt_free);
110     declared_marks = xbt_dict_new_homogeneous(xbt_free);
111     user_host_variables = xbt_dict_new_homogeneous(xbt_free);
112     user_vm_variables = xbt_dict_new_homogeneous (xbt_free);
113     user_link_variables = xbt_dict_new_homogeneous(xbt_free);
114
115     if (TRACE_start_functions != NULL) {
116       void (*func) ();
117       unsigned int iter = xbt_dynar_length(TRACE_start_functions);
118       xbt_dynar_foreach(TRACE_start_functions, iter, func) {
119         func();
120       }
121     }
122   }
123   xbt_dynar_free(&TRACE_start_functions);
124   return 0;
125 }
126
127 static xbt_dynar_t TRACE_end_functions = NULL;
128 void TRACE_add_end_function(void (*func) (void))
129 {
130   if (TRACE_end_functions == NULL)
131     TRACE_end_functions = xbt_dynar_new(sizeof(void (*)(void)), NULL);
132   xbt_dynar_push(TRACE_end_functions, &func);
133 }
134
135 int TRACE_end()
136 {
137   int retval;
138   if (!trace_active) {
139     retval = 1;
140   } else {
141     retval = 0;
142
143     TRACE_generate_viva_uncat_conf();
144     TRACE_generate_viva_cat_conf();
145
146     /* dump trace buffer */
147     TRACE_last_timestamp_to_dump = surf_get_clock();
148     TRACE_paje_dump_buffer(1);
149
150     /* destroy all data structures of tracing (and free) */
151     PJ_container_free_all();
152     PJ_type_free_all();
153     PJ_container_release();
154     PJ_type_release();
155
156     if (TRACE_end_functions != NULL) {
157       void (*func) (void);
158       unsigned int iter;
159       xbt_dynar_foreach(TRACE_end_functions, iter, func) {
160         func();
161       }
162     }
163
164     xbt_dict_free(&user_link_variables);
165     xbt_dict_free(&user_host_variables);
166     xbt_dict_free(&user_vm_variables);
167     xbt_dict_free(&declared_marks);
168     xbt_dict_free(&created_categories);
169
170     /* close the trace file */
171     TRACE_paje_end();
172
173     /* de-activate trace */
174     trace_active = 0;
175     XBT_DEBUG("Tracing is off");
176     XBT_DEBUG("Tracing system is shutdown");
177   }
178   xbt_dynar_free(&TRACE_end_functions);
179   return retval;
180 }
181
182 int TRACE_needs_platform (void)
183 {
184   return TRACE_msg_process_is_enabled() ||
185          TRACE_msg_vm_is_enabled() ||
186          TRACE_categorized() ||
187          TRACE_uncategorized() ||
188          TRACE_platform () ||
189          (TRACE_smpi_is_enabled() && TRACE_smpi_is_grouped());
190 }
191
192 int TRACE_is_enabled(void)
193 {
194   return trace_enabled;
195 }
196
197 int TRACE_platform(void)
198 {
199   return trace_platform;
200 }
201
202 int TRACE_platform_topology(void)
203 {
204   return trace_platform_topology;
205 }
206
207 int TRACE_is_configured(void)
208 {
209   return trace_configured;
210 }
211
212 int TRACE_smpi_is_enabled(void)
213 {
214   return (trace_smpi_enabled || TRACE_smpi_is_grouped())
215     && TRACE_is_enabled();
216 }
217
218 int TRACE_smpi_is_grouped(void)
219 {
220   return trace_smpi_grouped;
221 }
222
223 int TRACE_smpi_is_computing(void)
224 {
225   return trace_smpi_computing;
226 }
227
228
229 int TRACE_categorized (void)
230 {
231   return trace_categorized;
232 }
233
234 int TRACE_uncategorized (void)
235 {
236   return trace_uncategorized;
237 }
238
239 int TRACE_msg_process_is_enabled(void)
240 {
241   return trace_msg_process_enabled && TRACE_is_enabled();
242 }
243
244 int TRACE_msg_vm_is_enabled(void)
245 {
246   return trace_msg_vm_enabled && TRACE_is_enabled();
247 }
248
249 int TRACE_disable_link(void)
250 {
251   return trace_disable_link && TRACE_is_enabled();
252 }
253
254 int TRACE_disable_power(void)
255 {
256   return trace_disable_power && TRACE_is_enabled();
257 }
258
259 int TRACE_buffer (void)
260 {
261   return trace_buffer && TRACE_is_enabled();
262 }
263
264 int TRACE_onelink_only (void)
265 {
266   return trace_onelink_only && TRACE_is_enabled();
267 }
268
269 int TRACE_disable_destroy (void)
270 {
271   return trace_disable_destroy && TRACE_is_enabled();
272 }
273
274 int TRACE_basic (void)
275 {
276   return trace_basic && TRACE_is_enabled();
277 }
278
279 char *TRACE_get_comment (void)
280 {
281   return xbt_cfg_get_string(_sg_cfg_set, OPT_TRACING_COMMENT);
282 }
283
284 char *TRACE_get_comment_file (void)
285 {
286   return xbt_cfg_get_string(_sg_cfg_set, OPT_TRACING_COMMENT_FILE);
287 }
288
289 char *TRACE_get_filename(void)
290 {
291   return xbt_cfg_get_string(_sg_cfg_set, OPT_TRACING_FILENAME);
292 }
293
294 char *TRACE_get_viva_uncat_conf (void)
295 {
296   return xbt_cfg_get_string(_sg_cfg_set, OPT_VIVA_UNCAT_CONF);
297 }
298
299 char *TRACE_get_viva_cat_conf (void)
300 {
301   return xbt_cfg_get_string(_sg_cfg_set, OPT_VIVA_CAT_CONF);
302 }
303
304 void TRACE_global_init(int *argc, char **argv)
305 {
306   /* name of the tracefile */
307   char *default_tracing_filename = xbt_strdup("simgrid.trace");
308   xbt_cfg_register(&_sg_cfg_set, OPT_TRACING_FILENAME,
309                    "Trace file created by the instrumented SimGrid.",
310                    xbt_cfgelm_string, &default_tracing_filename, 1, 1,
311                    NULL, NULL);
312
313   /* tracing */
314   int default_tracing = 0;
315   xbt_cfg_register(&_sg_cfg_set, OPT_TRACING,
316                    "Enable Tracing.",
317                    xbt_cfgelm_int, &default_tracing, 0, 1,
318                    NULL, NULL);
319
320   /* register platform in the trace */
321   int default_tracing_platform = 0;
322   xbt_cfg_register(&_sg_cfg_set, OPT_TRACING_PLATFORM,
323                    "Register the platform in the trace as a hierarchy.",
324                    xbt_cfgelm_int, &default_tracing_platform, 0, 1,
325                    NULL, NULL);
326
327   /* register platform in the trace */
328   int default_tracing_platform_topology = 1;
329   xbt_cfg_register(&_sg_cfg_set, OPT_TRACING_TOPOLOGY,
330                    "Register the platform topology in the trace as a graph.",
331                    xbt_cfgelm_int, &default_tracing_platform_topology, 0, 1,
332                    NULL, NULL);
333
334   /* smpi */
335   int default_tracing_smpi = 0;
336   xbt_cfg_register(&_sg_cfg_set, OPT_TRACING_SMPI,
337                    "Tracing of the SMPI interface.",
338                    xbt_cfgelm_int, &default_tracing_smpi, 0, 1,
339                    NULL, NULL);
340
341   /* smpi grouped */
342   int default_tracing_smpi_grouped = 0;
343   xbt_cfg_register(&_sg_cfg_set, OPT_TRACING_SMPI_GROUP,
344                    "Group MPI processes by host.",
345                    xbt_cfgelm_int, &default_tracing_smpi_grouped, 0, 1,
346                    NULL, NULL);
347
348   /* smpi computing */
349   int default_tracing_smpi_computing = 0;
350   xbt_cfg_register(&_sg_cfg_set, OPT_TRACING_SMPI_COMPUTING,
351                    "Generate states for timing out of SMPI parts of the application",
352                    xbt_cfgelm_int, &default_tracing_smpi_computing, 0, 1,
353                    NULL, NULL);
354
355   /* tracing categorized resource utilization traces */
356   int default_tracing_categorized = 0;
357   xbt_cfg_register(&_sg_cfg_set, OPT_TRACING_CATEGORIZED,
358                    "Tracing categorized resource utilization of hosts and links.",
359                    xbt_cfgelm_int, &default_tracing_categorized, 0, 1,
360                    NULL, NULL);
361
362   /* tracing uncategorized resource utilization */
363   int default_tracing_uncategorized = 0;
364   xbt_cfg_register(&_sg_cfg_set, OPT_TRACING_UNCATEGORIZED,
365                    "Tracing uncategorized resource utilization of hosts and links.",
366                    xbt_cfgelm_int, &default_tracing_uncategorized, 0, 1,
367                    NULL, NULL);
368
369   /* msg process */
370   int default_tracing_msg_process = 0;
371   xbt_cfg_register(&_sg_cfg_set, OPT_TRACING_MSG_PROCESS,
372                    "Tracing of MSG process behavior.",
373                    xbt_cfgelm_int, &default_tracing_msg_process, 0, 1,
374                    NULL, NULL);
375
376   /* msg process */
377   int default_tracing_msg_vm = 0;
378   xbt_cfg_register(&_sg_cfg_set, OPT_TRACING_MSG_VM,
379                    "Tracing of MSG process behavior.",
380                    xbt_cfgelm_int, &default_tracing_msg_vm, 0, 1,
381                    NULL, NULL);
382
383   /* disable tracing link */
384   int default_tracing_disable_link = 0;
385   xbt_cfg_register(&_sg_cfg_set, OPT_TRACING_DISABLE_LINK,
386                    "Do not trace link bandwidth and latency.",
387                    xbt_cfgelm_int, &default_tracing_disable_link, 0, 1,
388                    NULL, NULL);
389
390   /* disable tracing link */
391   int default_tracing_disable_power = 0;
392   xbt_cfg_register(&_sg_cfg_set, OPT_TRACING_DISABLE_POWER,
393                    "Do not trace host power.",
394                    xbt_cfgelm_int, &default_tracing_disable_power, 0, 1,
395                    NULL, NULL);
396
397
398   /* tracing buffer */
399   int default_buffer = 1;
400   xbt_cfg_register(&_sg_cfg_set, OPT_TRACING_BUFFER,
401                    "Buffer trace events to put them in temporal order.",
402                    xbt_cfgelm_int, &default_buffer, 0, 1,
403                    NULL, NULL);
404
405   /* tracing one link only */
406   int default_onelink_only = 0;
407   xbt_cfg_register(&_sg_cfg_set, OPT_TRACING_ONELINK_ONLY,
408                    "Use only routes with one link to trace platform.",
409                    xbt_cfgelm_int, &default_onelink_only, 0, 1,
410                    NULL, NULL);
411
412   /* disable destroy */
413   int default_disable_destroy = 0;
414   xbt_cfg_register(&_sg_cfg_set, OPT_TRACING_DISABLE_DESTROY,
415                    "Disable platform containers destruction.",
416                    xbt_cfgelm_int, &default_disable_destroy, 0, 1,
417                    NULL, NULL);
418
419   /* basic -- Avoid extended events (impoverished trace file) */
420   int default_basic = 0;
421   xbt_cfg_register(&_sg_cfg_set, OPT_TRACING_BASIC,
422                    "Avoid extended events (impoverished trace file).",
423                    xbt_cfgelm_int, &default_basic, 0, 1,
424                    NULL, NULL);
425
426   /* comment */
427   char *default_tracing_comment = xbt_strdup ("");
428   xbt_cfg_register(&_sg_cfg_set, OPT_TRACING_COMMENT,
429                    "Comment to be added on the top of the trace file.",
430                    xbt_cfgelm_string, &default_tracing_comment, 1, 1,
431                    NULL, NULL);
432
433   /* comment_file */
434   char *default_tracing_comment_file = xbt_strdup ("");
435   xbt_cfg_register(&_sg_cfg_set, OPT_TRACING_COMMENT_FILE,
436                    "The contents of the file are added to the top of the trace file as comment.",
437                    xbt_cfgelm_string, &default_tracing_comment_file, 1, 1,
438                    NULL, NULL);
439
440   /* Viva graph configuration for uncategorized tracing */
441   char *default_viva_uncat_conf_file = xbt_strdup ("");
442   xbt_cfg_register(&_sg_cfg_set, OPT_VIVA_UNCAT_CONF,
443                    "Viva Graph configuration file for uncategorized resource utilization traces.",
444                    xbt_cfgelm_string, &default_viva_uncat_conf_file, 1, 1,
445                    NULL, NULL);
446
447   /* Viva graph configuration for uncategorized tracing */
448   char *default_viva_cat_conf_file = xbt_strdup ("");
449   xbt_cfg_register(&_sg_cfg_set, OPT_VIVA_CAT_CONF,
450                    "Viva Graph configuration file for categorized resource utilization traces.",
451                    xbt_cfgelm_string, &default_viva_cat_conf_file, 1, 1,
452                    NULL, NULL);
453
454   /* instrumentation can be considered configured now */
455   trace_configured = 1;
456 }
457
458 static void print_line (const char *option, const char *desc, const char *longdesc, int detailed)
459 {
460   char str[INSTR_DEFAULT_STR_SIZE];
461   snprintf (str, INSTR_DEFAULT_STR_SIZE, "--cfg=%s ", option);
462
463   int len = strlen (str);
464   printf ("%s%*.*s %s\n", str, 30-len, 30-len, "", desc);
465   if (!!longdesc && detailed){
466     printf ("%s\n\n", longdesc);
467   }
468 }
469
470 void TRACE_help (int detailed)
471 {
472   printf(
473       "Description of the tracing options accepted by this simulator:\n\n");
474   print_line (OPT_TRACING, "Enable the tracing system",
475       "  It activates the tracing system and register the simulation platform\n"
476       "  in the trace file. You have to enable this option to others take effect.",
477       detailed);
478   print_line (OPT_TRACING_CATEGORIZED, "Trace categorized resource utilization",
479       "  It activates the categorized resource utilization tracing. It should\n"
480       "  be enabled if tracing categories are used by this simulator.",
481       detailed);
482   print_line (OPT_TRACING_UNCATEGORIZED, "Trace uncategorized resource utilization",
483       "  It activates the uncategorized resource utilization tracing. Use it if\n"
484       "  this simulator do not use tracing categories and resource use have to be\n"
485       "  traced.",
486       detailed);
487   print_line (OPT_TRACING_FILENAME, "Filename to register traces",
488       "  A file with this name will be created to register the simulation. The file\n"
489       "  is in the Paje format and can be analyzed using Viva, Paje, and PajeNG visualization\n"
490       "  tools. More information can be found in these webpages:\n"
491       "     http://github.com/schnorr/viva/\n"
492       "     http://github.com/schnorr/pajeng/\n"
493       "     http://paje.sourceforge.net/",
494       detailed);
495   print_line (OPT_TRACING_SMPI, "Trace the MPI Interface (SMPI)",
496       "  This option only has effect if this simulator is SMPI-based. Traces the MPI\n"
497       "  interface and generates a trace that can be analyzed using Gantt-like\n"
498       "  visualizations. Every MPI function (implemented by SMPI) is transformed in a\n"
499       "  state, and point-to-point communications can be analyzed with arrows.",
500       detailed);
501   print_line (OPT_TRACING_SMPI_GROUP, "Group MPI processes by host (SMPI)",
502       "  This option only has effect if this simulator is SMPI-based. The processes\n"
503       "  are grouped by the hosts where they were executed.",
504       detailed);
505   print_line (OPT_TRACING_SMPI_COMPUTING, "Generates a \" Computing \" State",
506       "  This option aims at tracing computations in the application, outside SMPI\n"
507       "  to allow further study of simulated or real computation time",
508       detailed);
509   print_line (OPT_TRACING_MSG_PROCESS, "Trace processes behavior (MSG)",
510       "  This option only has effect if this simulator is MSG-based. It traces the\n"
511       "  behavior of all categorized MSG processes, grouping them by hosts. This option\n"
512       "  can be used to track process location if this simulator has process migration.",
513       detailed);
514   print_line (OPT_TRACING_BUFFER, "Buffer events to put them in temporal order",
515       "  This option put some events in a time-ordered buffer using the insertion\n"
516       "  sort algorithm. The process of acquiring and releasing locks to access this\n"
517       "  buffer and the cost of the sorting algorithm make this process slow. The\n"
518       "  simulator performance can be severely impacted if this option is activated,\n"
519       "  but you are sure to get a trace file with events sorted.",
520       detailed);
521   print_line (OPT_TRACING_ONELINK_ONLY, "Consider only one link routes to trace platform",
522       "  This option changes the way SimGrid register its platform on the trace file.\n"
523       "  Normally, the tracing considers all routes (no matter their size) on the\n"
524       "  platform file to re-create the resource topology. If this option is activated,\n"
525       "  only the routes with one link are used to register the topology within an AS.\n"
526       "  Routes among AS continue to be traced as usual.",
527       detailed);
528   print_line (OPT_TRACING_DISABLE_DESTROY, "Disable platform containers destruction",
529       "  Disable the destruction of containers at the end of simulation. This can be\n"
530       "  used with simulators that have a different notion of time (different from\n"
531       "  the simulated time).",
532       detailed);
533   print_line (OPT_TRACING_BASIC, "Avoid extended events (impoverished trace file).",
534       "  Some visualization tools are not able to parse correctly the Paje file format.\n"
535       "  Use this option if you are using one of these tools to visualize the simulation\n"
536       "  trace. Keep in mind that the trace might be incomplete, without all the\n"
537       "  information that would be registered otherwise.",
538       detailed);
539   print_line (OPT_TRACING_COMMENT, "Comment to be added on the top of the trace file.",
540       "  Use this to add a comment line to the top of the trace file.",
541       detailed);
542   print_line (OPT_TRACING_COMMENT_FILE, "File contents added to trace file as comment.",
543       "  Use this to add the contents of a file to the top of the trace file as comment.",
544       detailed);
545   print_line (OPT_VIVA_UNCAT_CONF, "Generate a graph configuration for Viva",
546       "  This option can be used in all types of simulators build with SimGrid\n"
547       "  to generate a uncategorized resource utilization graph to be used as\n"
548       "  configuration for the Viva visualization tool. This option\n"
549       "  can be used with tracing/categorized:1 and tracing:1 options to\n"
550       "  analyze an unmodified simulator before changing it to contain\n"
551       "  categories.",
552       detailed);
553   print_line (OPT_VIVA_CAT_CONF, "Generate an uncategorized graph configuration for Viva",
554       "  This option can be used if this simulator uses tracing categories\n"
555       "  in its code. The file specified by this option holds a graph configuration\n"
556       "  file for the Viva visualization tool that can be used to analyze a categorized\n"
557       "  resource utilization.",
558       detailed);
559   print_line (OPT_TRACING_TOPOLOGY, "Register the platform topology as a graph",
560         "  This option (enabled by default) can be used to disable the tracing of\n"
561         "  the platform topology in the trace file. Sometimes, such task is really\n"
562         "  time consuming, since it must get the route from each host ot other hosts\n"
563         "  within the same Autonomous System (AS).",
564         detailed);
565 }
566
567 static void output_types (const char *name, xbt_dynar_t types, FILE *file)
568 {
569   unsigned int i;
570   fprintf (file, "  %s = (", name);
571   for (i = xbt_dynar_length(types); i > 0; i--) {
572     char *type = *(char**)xbt_dynar_get_ptr(types, i - 1);
573     fprintf (file, "\"%s\"", type);
574     if (i - 1 > 0){
575       fprintf (file, ",");
576     }else{
577       fprintf (file, ");\n");
578     }
579   }
580   xbt_dynar_free (&types);
581 }
582
583 static void output_categories (const char *name, xbt_dynar_t cats, FILE *file)
584 {
585   unsigned int i;
586   fprintf (file, "    values = (");
587   for (i = xbt_dynar_length(cats); i > 0; i--) {
588     char *cat = *(char**)xbt_dynar_get_ptr(cats, i - 1);
589     fprintf (file, "\"%s%s\"", name, cat);
590     if (i - 1 > 0){
591       fprintf (file, ",");
592     }else{
593       fprintf (file, ");\n");
594     }
595   }
596   xbt_dynar_free (&cats);
597 }
598
599 static void uncat_configuration (FILE *file)
600 {
601   //register NODE and EDGE types
602   output_types ("node", TRACE_get_node_types(), file);
603   output_types ("edge", TRACE_get_edge_types(), file);
604   fprintf (file, "\n");
605
606   //configuration for all nodes
607   fprintf (file,
608       "  host = {\n"
609       "    type = \"square\";\n"
610       "    size = \"power\";\n"
611       "    values = (\"power_used\");\n"
612       "  };\n"
613       "  link = {\n"
614       "    type = \"rhombus\";\n"
615       "    size = \"bandwidth\";\n"
616       "    values = (\"bandwidth_used\");\n"
617       "  };\n");
618   //close
619 }
620
621 static void cat_configuration (FILE *file)
622 {
623   //register NODE and EDGE types
624   output_types ("node", TRACE_get_node_types(), file);
625   output_types ("edge", TRACE_get_edge_types(), file);
626   fprintf (file, "\n");
627
628   //configuration for all nodes
629   fprintf (file,
630            "  host = {\n"
631            "    type = \"square\";\n"
632            "    size = \"power\";\n");
633   output_categories ("p", TRACE_get_categories(), file);
634   fprintf (file,
635            "  };\n"
636            "  link = {\n"
637            "    type = \"rhombus\";\n"
638            "    size = \"bandwidth\";\n");
639   output_categories ("b", TRACE_get_categories(), file);
640   fprintf (file, "  };\n");
641   //close
642 }
643
644 static void generate_uncat_configuration (const char *output, const char *name, int brackets)
645 {
646   if (output && strlen(output) > 0){
647     FILE *file = fopen (output, "w");
648     if (file == NULL){
649       THROWF (system_error, 1, "Unable to open file (%s) for writing %s graph "
650           "configuration (uncategorized).", output, name);
651     }
652
653     if (brackets) fprintf (file, "{\n");
654     uncat_configuration (file);
655     if (brackets) fprintf (file, "}\n");
656     fclose (file);
657   }
658 }
659
660 static void generate_cat_configuration (const char *output, const char *name, int brackets)
661 {
662   if (output && strlen(output) > 0){
663     //check if we do have categories declared
664     if (xbt_dict_is_empty(created_categories)){
665       XBT_INFO("No categories declared, ignoring generation of %s graph configuration", name);
666       return;
667     }
668
669     FILE *file = fopen (output, "w");
670     if (file == NULL){
671       THROWF (system_error, 1, "Unable to open file (%s) for writing %s graph "
672           "configuration (categorized).", output, name);
673     }
674
675     if (brackets) fprintf (file, "{\n");
676     cat_configuration (file);
677     if (brackets) fprintf (file, "}\n");
678     fclose (file);
679   }
680 }
681
682 void TRACE_generate_viva_uncat_conf (void)
683 {
684   generate_uncat_configuration (TRACE_get_viva_uncat_conf (), "viva", 0);
685 }
686
687 void TRACE_generate_viva_cat_conf (void)
688 {
689   generate_cat_configuration (TRACE_get_viva_cat_conf(), "viva", 0);
690 }
691
692 static int previous_trace_state = -1;
693
694 void instr_pause_tracing (void)
695 {
696   previous_trace_state = trace_enabled;
697   if (!TRACE_is_enabled()){
698     XBT_DEBUG ("Tracing is already paused, therefore do nothing.");
699   }else{
700     XBT_DEBUG ("Tracing is being paused.");
701   }
702   trace_enabled = 0;
703   XBT_DEBUG ("Tracing is paused.");
704 }
705
706 void instr_resume_tracing (void)
707 {
708   if (TRACE_is_enabled()){
709     XBT_DEBUG ("Tracing is already running while trying to resume, therefore do nothing.");
710   }else{
711     XBT_DEBUG ("Tracing is being resumed.");
712   }
713
714   if (previous_trace_state != -1){
715     trace_enabled = previous_trace_state;
716   }else{
717     trace_enabled = 1;
718   }
719   XBT_DEBUG ("Tracing is resumed.");
720   previous_trace_state = -1;
721 }
722
723 #undef OPT_TRACING
724 #undef OPT_TRACING_PLATFORM
725 #undef OPT_TRACING_TOPOLOGY
726 #undef OPT_TRACING_SMPI
727 #undef OPT_TRACING_SMPI_GROUP
728 #undef OPT_TRACING_CATEGORIZED
729 #undef OPT_TRACING_UNCATEGORIZED
730 #undef OPT_TRACING_MSG_PROCESS
731 #undef OPT_TRACING_FILENAME
732 #undef OPT_TRACING_BUFFER
733 #undef OPT_TRACING_ONELINK_ONLY
734 #undef OPT_TRACING_DISABLE_DESTROY
735 #undef OPT_TRACING_BASIC
736 #undef OPT_TRACING_COMMENT
737 #undef OPT_TRACING_COMMENT_FILE
738 #undef OPT_VIVA_UNCAT_CONF
739 #undef OPT_VIVA_CAT_CONF
740
741 #endif /* HAVE_TRACING */