Logo AND Algorithmique Numérique Distribuée

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