Logo AND Algorithmique Numérique Distribuée

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