Logo AND Algorithmique Numérique Distribuée

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