Logo AND Algorithmique Numérique Distribuée

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