Logo AND Algorithmique Numérique Distribuée

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