Logo AND Algorithmique Numérique Distribuée

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