Logo AND Algorithmique Numérique Distribuée

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