Logo AND Algorithmique Numérique Distribuée

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