Logo AND Algorithmique Numérique Distribuée

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