Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
089bfea2adac874ef713ad5e5c43ff2e2351b167
[simgrid.git] / src / instr / instr_config.c
1 /* Copyright (c) 2010. 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
8 #include "instr/instr_private.h"
9
10 #ifdef HAVE_TRACING
11
12 XBT_LOG_NEW_CATEGORY(instr, "Logging the behavior of the tracing system (used for Visualization/Analysis of simulations)");
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_config, instr, "Configuration");
14
15 #define OPT_TRACING               "tracing"
16 #define OPT_TRACING_PLATFORM      "tracing/platform"
17 #define OPT_TRACING_SMPI          "tracing/smpi"
18 #define OPT_TRACING_SMPI_GROUP    "tracing/smpi/group"
19 #define OPT_TRACING_CATEGORIZED   "tracing/categorized"
20 #define OPT_TRACING_UNCATEGORIZED "tracing/uncategorized"
21 #define OPT_TRACING_MSG_PROCESS   "tracing/msg/process"
22 #define OPT_TRACING_FILENAME      "tracing/filename"
23 #define OPT_TRACING_BUFFER        "tracing/buffer"
24 #define OPT_TRACING_ONELINK_ONLY  "tracing/onelink_only"
25 #define OPT_TRACING_DISABLE_DESTROY "tracing/disable_destroy"
26 #define OPT_TRIVA_UNCAT_CONF      "triva/uncategorized"
27 #define OPT_TRIVA_CAT_CONF        "triva/categorized"
28 #define OPT_VIVA_UNCAT_CONF      "viva/uncategorized"
29 #define OPT_VIVA_CAT_CONF        "viva/categorized"
30
31 static int trace_enabled;
32 static int trace_platform;
33 static int trace_smpi_enabled;
34 static int trace_smpi_grouped;
35 static int trace_categorized;
36 static int trace_uncategorized;
37 static int trace_msg_process_enabled;
38 static int trace_buffer;
39 static int trace_onelink_only;
40 static int trace_disable_destroy;
41
42 static int trace_configured = 0;
43 static int trace_active = 0;
44
45 static void TRACE_getopts(void)
46 {
47   trace_enabled = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING);
48   trace_platform = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_PLATFORM);
49   trace_smpi_enabled = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_SMPI);
50   trace_smpi_grouped = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_SMPI_GROUP);
51   trace_categorized = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_CATEGORIZED);
52   trace_uncategorized = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_UNCATEGORIZED);
53   trace_msg_process_enabled = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_MSG_PROCESS);
54   trace_buffer = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_BUFFER);
55   trace_onelink_only = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_ONELINK_ONLY);
56   trace_disable_destroy = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_DISABLE_DESTROY);
57 }
58
59 int TRACE_start()
60 {
61   TRACE_getopts();
62
63   // tracing system must be:
64   //    - enabled (with --cfg=tracing:1)
65   //    - already configured (TRACE_global_init already called)
66   if (!(TRACE_is_enabled() && TRACE_is_configured())){
67     return 0;
68   }
69
70   XBT_DEBUG("Tracing starts");
71
72   /* open the trace file */
73   TRACE_paje_start();
74
75   /* activate trace */
76   if (trace_active == 1){
77     THROWF (tracing_error, 0, "Tracing is already active");
78   }
79   trace_active = 1;
80   XBT_DEBUG ("Tracing is on");
81
82   /* other trace initialization */
83   created_categories = xbt_dict_new_homogeneous(xbt_free);
84   declared_marks = xbt_dict_new_homogeneous (xbt_free);
85   user_host_variables = xbt_dict_new_homogeneous (xbt_free);
86   user_link_variables = xbt_dict_new_homogeneous (xbt_free);
87   TRACE_surf_alloc();
88   TRACE_smpi_alloc();
89   return 0;
90 }
91
92 int TRACE_end()
93 {
94   if (!trace_active)
95     return 1;
96
97   TRACE_generate_triva_uncat_conf();
98   TRACE_generate_triva_cat_conf();
99   TRACE_generate_viva_uncat_conf();
100   TRACE_generate_viva_cat_conf();
101
102   /* dump trace buffer */
103   TRACE_last_timestamp_to_dump = surf_get_clock();
104   TRACE_paje_dump_buffer(1);
105
106   /* destroy all data structures of tracing (and free) */
107   PJ_container_free_all();
108   PJ_type_free_all();
109   PJ_container_release();
110   PJ_type_release();
111   TRACE_smpi_release();
112   TRACE_surf_release();
113   xbt_dict_free(&user_link_variables);
114   xbt_dict_free(&user_host_variables);
115   xbt_dict_free(&declared_marks);
116   xbt_dict_free(&created_categories);
117
118   /* close the trace file */
119   TRACE_paje_end();
120
121   /* de-activate trace */
122   trace_active = 0;
123   XBT_DEBUG ("Tracing is off");
124   XBT_DEBUG("Tracing system is shutdown");
125   return 0;
126 }
127
128 int TRACE_needs_platform (void)
129 {
130   return TRACE_msg_process_is_enabled() ||
131          TRACE_categorized() ||
132          TRACE_uncategorized() ||
133          TRACE_platform () ||
134          (TRACE_smpi_is_enabled() && TRACE_smpi_is_grouped());
135 }
136
137 int TRACE_is_enabled(void)
138 {
139   return trace_enabled;
140 }
141
142 int TRACE_platform(void)
143 {
144   return trace_platform;
145 }
146
147 int TRACE_is_configured(void)
148 {
149   return trace_configured;
150 }
151
152 int TRACE_smpi_is_enabled(void)
153 {
154   return (trace_smpi_enabled || TRACE_smpi_is_grouped())
155     && TRACE_is_enabled();
156 }
157
158 int TRACE_smpi_is_grouped(void)
159 {
160   return trace_smpi_grouped;
161 }
162
163 int TRACE_categorized (void)
164 {
165   return trace_categorized;
166 }
167
168 int TRACE_uncategorized (void)
169 {
170   return trace_uncategorized;
171 }
172
173 int TRACE_msg_process_is_enabled(void)
174 {
175   return trace_msg_process_enabled && TRACE_is_enabled();
176 }
177
178 int TRACE_buffer (void)
179 {
180   return trace_buffer && TRACE_is_enabled();
181 }
182
183 int TRACE_onelink_only (void)
184 {
185   return trace_onelink_only && TRACE_is_enabled();
186 }
187
188 int TRACE_disable_destroy (void)
189 {
190   return trace_disable_destroy && TRACE_is_enabled();
191 }
192
193 char *TRACE_get_filename(void)
194 {
195   return xbt_cfg_get_string(_surf_cfg_set, OPT_TRACING_FILENAME);
196 }
197
198 char *TRACE_get_triva_uncat_conf (void)
199 {
200   return xbt_cfg_get_string(_surf_cfg_set, OPT_TRIVA_UNCAT_CONF);
201 }
202
203 char *TRACE_get_triva_cat_conf (void)
204 {
205   return xbt_cfg_get_string(_surf_cfg_set, OPT_TRIVA_CAT_CONF);
206 }
207
208 char *TRACE_get_viva_uncat_conf (void)
209 {
210   return xbt_cfg_get_string(_surf_cfg_set, OPT_VIVA_UNCAT_CONF);
211 }
212
213 char *TRACE_get_viva_cat_conf (void)
214 {
215   return xbt_cfg_get_string(_surf_cfg_set, OPT_VIVA_CAT_CONF);
216 }
217
218 void TRACE_global_init(int *argc, char **argv)
219 {
220   /* name of the tracefile */
221   char *default_tracing_filename = xbt_strdup("simgrid.trace");
222   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_FILENAME,
223                    "Trace file created by the instrumented SimGrid.",
224                    xbt_cfgelm_string, &default_tracing_filename, 1, 1,
225                    NULL, NULL);
226
227   /* tracing */
228   int default_tracing = 0;
229   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING,
230                    "Enable Tracing.",
231                    xbt_cfgelm_int, &default_tracing, 0, 1,
232                    NULL, NULL);
233
234   /* register platform in the trace */
235   int default_tracing_platform = 0;
236   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_PLATFORM,
237                    "Register the platform in the trace as a graph.",
238                    xbt_cfgelm_int, &default_tracing_platform, 0, 1,
239                    NULL, NULL);
240
241   /* smpi */
242   int default_tracing_smpi = 0;
243   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_SMPI,
244                    "Tracing of the SMPI interface.",
245                    xbt_cfgelm_int, &default_tracing_smpi, 0, 1,
246                    NULL, NULL);
247
248   /* smpi grouped */
249   int default_tracing_smpi_grouped = 0;
250   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_SMPI_GROUP,
251                    "Group MPI processes by host.",
252                    xbt_cfgelm_int, &default_tracing_smpi_grouped, 0, 1,
253                    NULL, NULL);
254
255
256   /* tracing categorized resource utilization traces */
257   int default_tracing_categorized = 0;
258   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_CATEGORIZED,
259                    "Tracing categorized resource utilization of hosts and links.",
260                    xbt_cfgelm_int, &default_tracing_categorized, 0, 1,
261                    NULL, NULL);
262
263   /* tracing uncategorized resource utilization */
264   int default_tracing_uncategorized = 0;
265   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_UNCATEGORIZED,
266                    "Tracing uncategorized resource utilization of hosts and links.",
267                    xbt_cfgelm_int, &default_tracing_uncategorized, 0, 1,
268                    NULL, NULL);
269
270   /* msg process */
271   int default_tracing_msg_process = 0;
272   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_MSG_PROCESS,
273                    "Tracing of MSG process behavior.",
274                    xbt_cfgelm_int, &default_tracing_msg_process, 0, 1,
275                    NULL, NULL);
276
277   /* tracing buffer */
278   int default_buffer = 1;
279   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_BUFFER,
280                    "Buffer trace events to put them in temporal order.",
281                    xbt_cfgelm_int, &default_buffer, 0, 1,
282                    NULL, NULL);
283
284   /* tracing one link only */
285   int default_onelink_only = 0;
286   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_ONELINK_ONLY,
287                    "Use only routes with one link to trace platform.",
288                    xbt_cfgelm_int, &default_onelink_only, 0, 1,
289                    NULL, NULL);
290
291   /* disable destroy */
292   int default_disable_destroy = 0;
293   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_DISABLE_DESTROY,
294                    "Disable platform containers destruction.",
295                    xbt_cfgelm_int, &default_disable_destroy, 0, 1,
296                    NULL, NULL);
297
298   /* Triva graph configuration for uncategorized tracing */
299   char *default_triva_uncat_conf_file = xbt_strdup ("");
300   xbt_cfg_register(&_surf_cfg_set, OPT_TRIVA_UNCAT_CONF,
301                    "Triva Graph configuration file for uncategorized resource utilization traces.",
302                    xbt_cfgelm_string, &default_triva_uncat_conf_file, 1, 1,
303                    NULL, NULL);
304
305   /* Triva graph configuration for uncategorized tracing */
306   char *default_triva_cat_conf_file = xbt_strdup ("");
307   xbt_cfg_register(&_surf_cfg_set, OPT_TRIVA_CAT_CONF,
308                    "Triva Graph configuration file for categorized resource utilization traces.",
309                    xbt_cfgelm_string, &default_triva_cat_conf_file, 1, 1,
310                    NULL, NULL);
311
312   /* Viva graph configuration for uncategorized tracing */
313   char *default_viva_uncat_conf_file = xbt_strdup ("");
314   xbt_cfg_register(&_surf_cfg_set, OPT_VIVA_UNCAT_CONF,
315                    "Viva Graph configuration file for uncategorized resource utilization traces.",
316                    xbt_cfgelm_string, &default_viva_uncat_conf_file, 1, 1,
317                    NULL, NULL);
318
319   /* Viva graph configuration for uncategorized tracing */
320   char *default_viva_cat_conf_file = xbt_strdup ("");
321   xbt_cfg_register(&_surf_cfg_set, OPT_VIVA_CAT_CONF,
322                    "Viva Graph configuration file for categorized resource utilization traces.",
323                    xbt_cfgelm_string, &default_viva_cat_conf_file, 1, 1,
324                    NULL, NULL);
325
326   /* instrumentation can be considered configured now */
327   trace_configured = 1;
328 }
329
330 static void print_line (const char *option, const char *desc, const char *longdesc, int detailed)
331 {
332   char str[INSTR_DEFAULT_STR_SIZE];
333   snprintf (str, INSTR_DEFAULT_STR_SIZE, "--cfg=%s ", option);
334
335   int len = strlen (str);
336   printf ("%s%*.*s %s\n", str, 30-len, 30-len, "", desc);
337   if (!!longdesc && detailed){
338     printf ("%s\n\n", longdesc);
339   }
340 }
341
342 void TRACE_help (int detailed)
343 {
344   printf(
345       "Description of the tracing options accepted by this simulator:\n\n");
346   print_line (OPT_TRACING, "Enable the tracing system",
347       "  It activates the tracing system and register the simulation platform\n"
348       "  in the trace file. You have to enable this option to others take effect.",
349       detailed);
350   print_line (OPT_TRACING_CATEGORIZED, "Trace categorized resource utilization",
351       "  It activates the categorized resource utilization tracing. It should\n"
352       "  be enabled if tracing categories are used by this simulator.",
353       detailed);
354   print_line (OPT_TRACING_UNCATEGORIZED, "Trace uncategorized resource utilization",
355       "  It activates the uncategorized resource utilization tracing. Use it if\n"
356       "  this simulator do not use tracing categories and resource use have to be\n"
357       "  traced.",
358       detailed);
359   print_line (OPT_TRACING_FILENAME, "Filename to register traces",
360       "  A file with this name will be created to register the simulation. The file\n"
361       "  is in the Paje format and can be analyzed using Triva or Paje visualization\n"
362       "  tools. More information can be found in these webpages:\n"
363       "     http://triva.gforge.inria.fr/\n"
364       "     http://paje.sourceforge.net/",
365       detailed);
366   print_line (OPT_TRACING_SMPI, "Trace the MPI Interface (SMPI)",
367       "  This option only has effect if this simulator is SMPI-based. Traces the MPI\n"
368       "  interface and generates a trace that can be analyzed using Gantt-like\n"
369       "  visualizations. Every MPI function (implemented by SMPI) is transformed in a\n"
370       "  state, and point-to-point communications can be analyzed with arrows.",
371       detailed);
372   print_line (OPT_TRACING_SMPI_GROUP, "Group MPI processes by host (SMPI)",
373       "  This option only has effect if this simulator is SMPI-based. The processes\n"
374       "  are grouped by the hosts where they were executed.",
375       detailed);
376   print_line (OPT_TRACING_MSG_PROCESS, "Trace processes behavior (MSG)",
377       "  This option only has effect if this simulator is MSG-based. It traces the\n"
378       "  behavior of all categorized MSG processes, grouping them by hosts. This option\n"
379       "  can be used to track process location if this simulator has process migration.",
380       detailed);
381   print_line (OPT_TRACING_BUFFER, "Buffer events to put them in temporal order",
382       "  This option put some events in a time-ordered buffer using the insertion\n"
383       "  sort algorithm. The process of acquiring and releasing locks to access this\n"
384       "  buffer and the cost of the sorting algorithm make this process slow. The\n"
385       "  simulator performance can be severely impacted if this option is activated,\n"
386       "  but you are sure to get a trace file with events sorted.",
387       detailed);
388   print_line (OPT_TRACING_ONELINK_ONLY, "Consider only one link routes to trace platform",
389       "  This option changes the way SimGrid register its platform on the trace file.\n"
390       "  Normally, the tracing considers all routes (no matter their size) on the\n"
391       "  platform file to re-create the resource topology. If this option is activated,\n"
392       "  only the routes with one link are used to register the topology within an AS.\n"
393       "  Routes among AS continue to be traced as usual.",
394       detailed);
395   print_line (OPT_TRACING_DISABLE_DESTROY, "Disable platform containers destruction",
396       "  Disable the destruction of containers at the end of simulation. This can be\n"
397       "  used with simulators that have a different notion of time (different from\n"
398       "  the simulated time).",
399       detailed);
400   print_line (OPT_TRIVA_UNCAT_CONF, "Generate graph configuration for Triva",
401       "  This option can be used in all types of simulators build with SimGrid\n"
402       "  to generate a uncategorized resource utilization graph to be used as\n"
403       "  configuration for the Triva visualization analysis. This option\n"
404       "  can be used with tracing/categorized:1 and tracing:1 options to\n"
405       "  analyze an unmodified simulator before changing it to contain\n"
406       "  categories.",
407       detailed);
408   print_line (OPT_TRIVA_CAT_CONF, "generate uncategorized graph configuration for Triva",
409       "  This option can be used if this simulator uses tracing categories\n"
410       "  in its code. The file specified by this option holds a graph configuration\n"
411       "  file for the Triva visualization tool that can be used to analyze a categorized\n"
412       "  resource utilization.",
413       detailed);
414   print_line (OPT_VIVA_UNCAT_CONF, "Generate a graph configuration for Viva",
415       "  This option can be used in all types of simulators build with SimGrid\n"
416       "  to generate a uncategorized resource utilization graph to be used as\n"
417       "  configuration for the Viva visualization tool. This option\n"
418       "  can be used with tracing/categorized:1 and tracing:1 options to\n"
419       "  analyze an unmodified simulator before changing it to contain\n"
420       "  categories.",
421       detailed);
422   print_line (OPT_VIVA_CAT_CONF, "Generate an uncategorized graph configuration for Viva",
423       "  This option can be used if this simulator uses tracing categories\n"
424       "  in its code. The file specified by this option holds a graph configuration\n"
425       "  file for the Viva visualization tool that can be used to analyze a categorized\n"
426       "  resource utilization.",
427       detailed);
428 }
429
430 static void output_types (const char *name, xbt_dynar_t types, FILE *file)
431 {
432   unsigned int i;
433   fprintf (file, "  %s = (", name);
434   for (i = xbt_dynar_length(types); i > 0; i--) {
435     char *type = *(char**)xbt_dynar_get_ptr(types, i - 1);
436     fprintf (file, "\"%s\"", type);
437     if (i - 1 > 0){
438       fprintf (file, ",");
439     }else{
440       fprintf (file, ");\n");
441     }
442   }
443   xbt_dynar_free (&types);
444 }
445
446 static void output_categories (const char *name, xbt_dynar_t cats, FILE *file)
447 {
448   unsigned int i;
449   fprintf (file, "    values = (");
450   for (i = xbt_dynar_length(cats); i > 0; i--) {
451     char *cat = *(char**)xbt_dynar_get_ptr(cats, i - 1);
452     fprintf (file, "\"%s%s\"", name, cat);
453     if (i - 1 > 0){
454       fprintf (file, ",");
455     }else{
456       fprintf (file, ");\n");
457     }
458   }
459   xbt_dynar_free (&cats);
460 }
461
462 static void uncat_configuration (FILE *file)
463 {
464   //register NODE and EDGE types
465   output_types ("node", TRACE_get_node_types(), file);
466   output_types ("edge", TRACE_get_edge_types(), file);
467   fprintf (file, "\n");
468
469   //configuration for all nodes
470   fprintf (file,
471       "  host = {\n"
472       "    type = \"square\";\n"
473       "    size = \"power\";\n"
474       "    values = (\"power_used\");\n"
475       "  };\n"
476       "  link = {\n"
477       "    type = \"rhombus\";\n"
478       "    size = \"bandwidth\";\n"
479       "    values = (\"bandwidth_used\");\n"
480       "  };\n");
481   //close
482 }
483
484 static void cat_configuration (FILE *file)
485 {
486   //register NODE and EDGE types
487   output_types ("node", TRACE_get_node_types(), file);
488   output_types ("edge", TRACE_get_edge_types(), file);
489   fprintf (file, "\n");
490
491   //configuration for all nodes
492   fprintf (file,
493            "  host = {\n"
494            "    type = \"square\";\n"
495            "    size = \"power\";\n");
496   output_categories ("p", TRACE_get_categories(), file);
497   fprintf (file,
498            "  };\n"
499            "  link = {\n"
500            "    type = \"rhombus\";\n"
501            "    size = \"bandwidth\";\n");
502   output_categories ("b", TRACE_get_categories(), file);
503   fprintf (file, "  };\n");
504   //close
505 }
506
507 static void generate_uncat_configuration (const char *output, const char *name, int brackets)
508 {
509   if (output && strlen(output) > 0){
510     FILE *file = fopen (output, "w");
511     if (file == NULL){
512       THROWF (system_error, 1, "Unable to open file (%s) for writing %s graph "
513           "configuration (uncategorized).", output, name);
514     }
515
516     if (brackets) fprintf (file, "{\n");
517     uncat_configuration (file);
518     if (brackets) fprintf (file, "}\n");
519     fclose (file);
520   }
521 }
522
523 static void generate_cat_configuration (const char *output, const char *name, int brackets)
524 {
525   if (output && strlen(output) > 0){
526     //check if we do have categories declared
527     if (xbt_dict_is_empty(created_categories)){
528       XBT_INFO("No categories declared, ignoring generation of %s graph configuration", name);
529       return;
530     }
531
532     FILE *file = fopen (output, "w");
533     if (file == NULL){
534       THROWF (system_error, 1, "Unable to open file (%s) for writing %s graph "
535           "configuration (categorized).", output, name);
536     }
537
538     if (brackets) fprintf (file, "{\n");
539     cat_configuration (file);
540     if (brackets) fprintf (file, "}\n");
541     fclose (file);
542   }
543 }
544
545 void TRACE_generate_triva_uncat_conf (void)
546 {
547   generate_uncat_configuration (TRACE_get_triva_uncat_conf (), "triva", 1);
548 }
549
550 void TRACE_generate_triva_cat_conf (void)
551 {
552   generate_cat_configuration (TRACE_get_triva_cat_conf(), "triva", 1);
553 }
554
555 void TRACE_generate_viva_uncat_conf (void)
556 {
557   generate_uncat_configuration (TRACE_get_viva_uncat_conf (), "viva", 0);
558 }
559
560 void TRACE_generate_viva_cat_conf (void)
561 {
562   generate_cat_configuration (TRACE_get_viva_cat_conf(), "viva", 0);
563 }
564
565 #undef OPT_TRACING
566 #undef OPT_TRACING_PLATFORM
567 #undef OPT_TRACING_SMPI
568 #undef OPT_TRACING_SMPI_GROUP
569 #undef OPT_TRACING_CATEGORIZED
570 #undef OPT_TRACING_UNCATEGORIZED
571 #undef OPT_TRACING_MSG_PROCESS
572 #undef OPT_TRACING_FILENAME
573 #undef OPT_TRACING_BUFFER
574 #undef OPT_TRACING_ONELINK_ONLY
575 #undef OPT_TRACING_DISABLE_DESTROY
576 #undef OPT_TRIVA_UNCAT_CONF
577 #undef OPT_TRIVA_CAT_CONF
578 #undef OPT_VIVA_UNCAT_CONF
579 #undef OPT_VIVA_CAT_CONF
580
581 #endif /* HAVE_TRACING */