Logo AND Algorithmique Numérique Distribuée

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