Logo AND Algorithmique Numérique Distribuée

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