Logo AND Algorithmique Numérique Distribuée

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