Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
680fa68ce098101d8b40c0e98239486a142ff0db
[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_SMPI          "tracing/smpi"
17 #define OPT_TRACING_SMPI_GROUP    "tracing/smpi/group"
18 #define OPT_TRACING_CATEGORIZED   "tracing/categorized"
19 #define OPT_TRACING_UNCATEGORIZED "tracing/uncategorized"
20 #define OPT_TRACING_MSG_TASK      "tracing/msg/task"
21 #define OPT_TRACING_MSG_PROCESS   "tracing/msg/process"
22 #define OPT_TRACING_MSG_VOLUME    "tracing/msg/volume"
23 #define OPT_TRACING_FILENAME      "tracing/filename"
24 #define OPT_TRACING_BUFFER        "tracing/buffer"
25 #define OPT_TRACING_ONELINK_ONLY  "tracing/onelink_only"
26 #define OPT_TRIVA_UNCAT_CONF      "triva/uncategorized"
27 #define OPT_TRIVA_CAT_CONF        "triva/categorized"
28
29 static int trace_configured = 0;
30 static int trace_active = 0;
31
32 xbt_dict_t created_categories; //declared in instr_interface.c
33
34 int TRACE_start()
35 {
36   // tracing system must be:
37   //    - enabled (with --cfg=tracing:1)
38   //    - already configured (TRACE_global_init already called)
39   if (!(TRACE_is_enabled() && TRACE_is_configured())){
40     return 0;
41   }
42
43   XBT_DEBUG("Tracing starts");
44
45   /* open the trace file */
46   TRACE_paje_start();
47
48   /* activate trace */
49   TRACE_activate ();
50
51   /* other trace initialization */
52   created_categories = xbt_dict_new();
53   TRACE_surf_alloc();
54   TRACE_smpi_alloc();
55   return 0;
56 }
57
58 int TRACE_end()
59 {
60   if (!TRACE_is_active())
61     return 1;
62
63   /* generate uncategorized graph configuration for triva */
64   if (TRACE_get_triva_uncat_conf()){
65     TRACE_generate_triva_uncat_conf();
66   }
67
68   /* generate categorized graph configuration for triva */
69   if (TRACE_get_triva_cat_conf()){
70     TRACE_generate_triva_cat_conf();
71   }
72
73   /* dump trace buffer */
74   TRACE_last_timestamp_to_dump = surf_get_clock();
75   TRACE_paje_dump_buffer(1);
76
77   /* destroy all data structures of tracing (and free) */
78   destroyAllContainers();
79
80   /* close the trace file */
81   TRACE_paje_end();
82
83   /* activate trace */
84   TRACE_desactivate ();
85   XBT_DEBUG("Tracing system is shutdown");
86   return 0;
87 }
88
89 void TRACE_activate (void)
90 {
91   xbt_assert (trace_active==0, "Tracing is already active.");
92   trace_active = 1;
93   XBT_DEBUG ("Tracing is on");
94 }
95
96 void TRACE_desactivate (void)
97 {
98   trace_active = 0;
99   XBT_DEBUG ("Tracing is off");
100 }
101
102 int TRACE_is_active (void)
103 {
104   return trace_active;
105 }
106
107 int TRACE_is_enabled(void)
108 {
109   return xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING);
110 }
111
112 int TRACE_is_configured(void)
113 {
114   return trace_configured;
115 }
116
117 int TRACE_smpi_is_enabled(void)
118 {
119   return xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_SMPI) &&
120       TRACE_is_enabled();
121 }
122
123 int TRACE_smpi_is_grouped(void)
124 {
125   return xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_SMPI_GROUP);
126 }
127
128 int TRACE_categorized (void)
129 {
130   return xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_CATEGORIZED);
131 }
132
133 int TRACE_uncategorized (void)
134 {
135   return xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_UNCATEGORIZED);
136 }
137
138 int TRACE_msg_task_is_enabled(void)
139 {
140   return xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_MSG_TASK) &&
141       TRACE_is_enabled();
142 }
143
144 int TRACE_msg_process_is_enabled(void)
145 {
146   return xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_MSG_PROCESS) &&
147       TRACE_is_enabled();
148 }
149
150 int TRACE_msg_volume_is_enabled(void)
151 {
152   return xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_MSG_VOLUME) &&
153       TRACE_is_enabled();
154 }
155
156 int TRACE_buffer (void)
157 {
158   return xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_BUFFER) &&
159       TRACE_is_enabled();
160 }
161
162 int TRACE_onelink_only (void)
163 {
164   return xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_ONELINK_ONLY) &&
165       TRACE_is_enabled();
166 }
167
168 char *TRACE_get_filename(void)
169 {
170   return xbt_cfg_get_string(_surf_cfg_set, OPT_TRACING_FILENAME);
171 }
172
173 char *TRACE_get_triva_uncat_conf (void)
174 {
175   return xbt_cfg_get_string(_surf_cfg_set, OPT_TRIVA_UNCAT_CONF);
176 }
177
178 char *TRACE_get_triva_cat_conf (void)
179 {
180   return xbt_cfg_get_string(_surf_cfg_set, OPT_TRIVA_CAT_CONF);
181 }
182
183 void TRACE_global_init(int *argc, char **argv)
184 {
185   /* name of the tracefile */
186   char *default_tracing_filename = xbt_strdup("simgrid.trace");
187   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_FILENAME,
188                    "Trace file created by the instrumented SimGrid.",
189                    xbt_cfgelm_string, &default_tracing_filename, 1, 1,
190                    NULL, NULL);
191
192   /* tracing */
193   int default_tracing = 0;
194   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING,
195                    "Enable Tracing.",
196                    xbt_cfgelm_int, &default_tracing, 0, 1,
197                    NULL, NULL);
198
199   /* smpi */
200   int default_tracing_smpi = 0;
201   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_SMPI,
202                    "Tracing of the SMPI interface.",
203                    xbt_cfgelm_int, &default_tracing_smpi, 0, 1,
204                    NULL, NULL);
205
206   /* smpi grouped */
207   int default_tracing_smpi_grouped = 0;
208   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_SMPI_GROUP,
209                    "Group MPI processes by host.",
210                    xbt_cfgelm_int, &default_tracing_smpi_grouped, 0, 1,
211                    NULL, NULL);
212
213
214   /* platform */
215   int default_tracing_platform = 0;
216   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_CATEGORIZED,
217                    "Tracing of categorized platform (host and link) utilization.",
218                    xbt_cfgelm_int, &default_tracing_platform, 0, 1,
219                    NULL, NULL);
220
221   /* tracing uncategorized resource utilization */
222   int default_tracing_uncategorized = 0;
223   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_UNCATEGORIZED,
224                    "Tracing of uncategorized resource (host and link) utilization.",
225                    xbt_cfgelm_int, &default_tracing_uncategorized, 0, 1,
226                    NULL, NULL);
227
228   /* msg task */
229   int default_tracing_msg_task = 0;
230   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_MSG_TASK,
231                    "Tracing of MSG task behavior.",
232                    xbt_cfgelm_int, &default_tracing_msg_task, 0, 1,
233                    NULL, NULL);
234
235   /* msg process */
236   int default_tracing_msg_process = 0;
237   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_MSG_PROCESS,
238                    "Tracing of MSG process behavior.",
239                    xbt_cfgelm_int, &default_tracing_msg_process, 0, 1,
240                    NULL, NULL);
241
242   /* msg volume (experimental) */
243   int default_tracing_msg_volume = 0;
244   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_MSG_VOLUME,
245                    "Tracing of MSG communication volume (experimental).",
246                    xbt_cfgelm_int, &default_tracing_msg_volume, 0, 1,
247                    NULL, NULL);
248
249   /* msg volume (experimental) */
250   int default_buffer = 0;
251   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_BUFFER,
252                    "Buffer trace events to put them in temporal order.",
253                    xbt_cfgelm_int, &default_buffer, 0, 1,
254                    NULL, NULL);
255
256   /* msg volume (experimental) */
257   int default_onelink_only = 0;
258   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_ONELINK_ONLY,
259                    "Use only routes with one link to trace platform.",
260                    xbt_cfgelm_int, &default_onelink_only, 0, 1,
261                    NULL, NULL);
262
263   /* Triva graph configuration for uncategorized tracing */
264   char *default_triva_uncat_conf_file = xbt_strdup ("");
265   xbt_cfg_register(&_surf_cfg_set, OPT_TRIVA_UNCAT_CONF,
266                    "Triva Graph configuration file for uncategorized resource utilization traces.",
267                    xbt_cfgelm_string, &default_triva_uncat_conf_file, 1, 1,
268                    NULL, NULL);
269
270   /* Triva graph configuration for uncategorized tracing */
271   char *default_triva_cat_conf_file = xbt_strdup ("");
272   xbt_cfg_register(&_surf_cfg_set, OPT_TRIVA_CAT_CONF,
273                    "Triva Graph configuration file for categorized resource utilization traces.",
274                    xbt_cfgelm_string, &default_triva_cat_conf_file, 1, 1,
275                    NULL, NULL);
276
277   /* instrumentation can be considered configured now */
278   trace_configured = 1;
279 }
280
281 static void print_line (const char *option, const char *desc, const char *longdesc, int detailed)
282 {
283   char str[INSTR_DEFAULT_STR_SIZE];
284   snprintf (str, INSTR_DEFAULT_STR_SIZE, "--cfg=%s ", option);
285
286   int len = strlen (str);
287   printf ("%s%*.*s %s\n", str, 30-len, 30-len, "", desc);
288   if (!!longdesc && detailed){
289     printf ("%s\n\n", longdesc);
290   }
291 }
292
293 void TRACE_help (int detailed)
294 {
295   printf(
296       "Description of the tracing options accepted by this simulator:\n\n");
297   print_line (OPT_TRACING, "Enable the tracing system",
298       "  It activates the tracing system and register the simulation platform\n"
299       "  in the trace file. You have to enable this option to others take effect.",
300       detailed);
301   print_line (OPT_TRACING_CATEGORIZED, "Trace categorized resource utilization",
302       "  It activates the categorized resource utilization tracing. It should\n"
303       "  be enabled if tracing categories are used by this simulator.",
304       detailed);
305   print_line (OPT_TRACING_UNCATEGORIZED, "Trace uncategorized resource utilization",
306       "  It activates the uncategorized resource utilization tracing. Use it if\n"
307       "  this simulator do not use tracing categories and resource use have to be\n"
308       "  traced.",
309       detailed);
310   print_line (OPT_TRACING_FILENAME, "Filename to register traces",
311       "  A file with this name will be created to register the simulation. The file\n"
312       "  is in the Paje format and can be analyzed using Triva or Paje visualization\n"
313       "  tools. More information can be found in these webpages:\n"
314       "     http://triva.gforge.inria.fr/\n"
315       "     http://paje.sourceforge.net/",
316       detailed);
317   print_line (OPT_TRACING_SMPI, "Trace the MPI Interface (SMPI)",
318       "  This option only has effect if this simulator is SMPI-based. Traces the MPI\n"
319       "  interface and generates a trace that can be analyzed using Gantt-like\n"
320       "  visualizations. Every MPI function (implemented by SMPI) is transformed in a\n"
321       "  state, and point-to-point communications can be analyzed with arrows.",
322       detailed);
323   print_line (OPT_TRACING_SMPI_GROUP, "Group MPI processes by host (SMPI)",
324       "  This option only has effect if this simulator is SMPI-based. The processes\n"
325       "  are grouped by the hosts where they were executed.",
326       detailed);
327   print_line (OPT_TRACING_MSG_TASK, "Trace task behavior (MSG)",
328       "  This option only has effect if this simulator is MSG-based. It traces the\n"
329       "  behavior of all categorized MSG tasks, grouping them by hosts.",
330       detailed);
331   print_line (OPT_TRACING_MSG_PROCESS, "Trace processes behavior (MSG)",
332       "  This option only has effect if this simulator is MSG-based. It traces the\n"
333       "  behavior of all categorized MSG processes, grouping them by hosts. This option\n"
334       "  can be used to track process location if this simulator has process migration.",
335       detailed);
336   print_line (OPT_TRACING_MSG_VOLUME, "Tracing of communication volume (MSG)",
337       "  This experimental option only has effect if this simulator is MSG-based.\n"
338       "  It traces the communication volume of MSG send/receive.",
339       detailed);
340   print_line (OPT_TRACING_BUFFER, "Buffer events to put them in temporal order",
341       "  This option put some events in a time-ordered buffer using the insertion\n"
342       "  sort algorithm. The process of acquiring and releasing locks to access this\n"
343       "  buffer and the cost of the sorting algorithm make this process slow. The\n"
344       "  simulator performance can be severely impacted if this option is activated,\n"
345       "  but you are sure to get a trace file with events sorted.",
346       detailed);
347   print_line (OPT_TRACING_ONELINK_ONLY, "Consider only one link routes to trace platform",
348       "  This option changes the way SimGrid register its platform on the trace file.\n"
349       "  Normally, the tracing considers all routes (no matter their size) on the\n"
350       "  platform file to re-create the resource topology. If this option is activated,\n"
351       "  only the routes with one link are used to register the topology within an AS.\n"
352       "  Routes among AS continue to be traced as usual.",
353       detailed);
354   print_line (OPT_TRIVA_UNCAT_CONF, "Generate graph configuration for Triva",
355       "  This option can be used in all types of simulators build with SimGrid\n"
356       "  to generate a uncategorized resource utilization graph to be used as\n"
357       "  configuration for the Triva visualization analysis. This option\n"
358       "  can be used with tracing/categorized:1 and tracing:1 options to\n"
359       "  analyze an unmodified simulator before changing it to contain\n"
360       "  categories.",
361       detailed);
362   print_line (OPT_TRIVA_CAT_CONF, "generate uncategorized graph configuration for Triva",
363       "  This option can be used if this simulator uses tracing categories\n"
364       "  in its code. The file specified by this option holds a graph configuration\n"
365       "  file for the Triva visualization tool that can be used to analyze a categorized\n"
366       "  resource utilization.",
367       detailed);
368 }
369
370 void TRACE_generate_triva_uncat_conf (void)
371 {
372   char *output = TRACE_get_triva_uncat_conf ();
373   if (output && strlen(output) > 0){
374     xbt_dict_cursor_t cursor=NULL;
375     char *name, *value;
376
377     FILE *file = fopen (output, "w");
378     xbt_assert (file != NULL,
379        "Unable to open file (%s) for writing triva graph "
380        "configuration (uncategorized).", output);
381
382     //open
383     fprintf (file, "{\n");
384
385     //register NODE types
386     fprintf (file, "  node = (");
387     xbt_dict_foreach(trivaNodeTypes, cursor, name, value) {
388       fprintf (file, "%s, ", name);
389     }
390
391     //register EDGE types
392     fprintf (file,
393         ");\n"
394         "  edge = (");
395     xbt_dict_foreach(trivaEdgeTypes, cursor, name, value) {
396       fprintf (file, "%s, ", name);
397     }
398     fprintf (file,
399         ");\n"
400         "\n");
401
402     //configuration for all nodes
403     fprintf (file,
404         "  host = {\n"
405         "    type = square;\n"
406         "    size = power;\n"
407         "    values = (power_used);\n"
408         "  };\n"
409         "  link = {\n"
410         "    type = rhombus;\n"
411         "    size = bandwidth;\n"
412         "    values = (bandwidth_used);\n"
413         "  };\n");
414     //close
415     fprintf (file, "}\n");
416     fclose (file);
417   }
418 }
419
420 void TRACE_generate_triva_cat_conf (void)
421 {
422   char *output = TRACE_get_triva_cat_conf();
423   if (output && strlen(output) > 0){
424     xbt_dict_cursor_t cursor=NULL, cursor2=NULL;
425     char *name, *name2, *value, *value2;
426
427     //check if we do have categories declared
428     if (xbt_dict_length(created_categories) == 0){
429       XBT_INFO("No categories declared, ignoring generation of triva graph configuration");
430       return;
431     }
432
433     FILE *file = fopen (output, "w");
434     xbt_assert (file != NULL,
435        "Unable to open file (%s) for writing triva graph "
436        "configuration (categorized).", output);
437
438     //open
439     fprintf (file, "{\n");
440
441     //register NODE types
442     fprintf (file, "  node = (");
443     xbt_dict_foreach(trivaNodeTypes, cursor, name, value) {
444       fprintf (file, "%s, ", name);
445     }
446
447     //register EDGE types
448     fprintf (file,
449         ");\n"
450         "  edge = (");
451     xbt_dict_foreach(trivaEdgeTypes, cursor, name, value) {
452       fprintf (file, "%s, ", name);
453     }
454     fprintf (file,
455         ");\n"
456         "\n");
457
458     //configuration for all nodes
459     fprintf (file,
460         "  host = {\n"
461         "    type = square;\n"
462         "    size = power;\n"
463         "    values = (");
464     xbt_dict_foreach(created_categories,cursor2,name2,value2) {
465       fprintf (file, "p%s, ", name2);
466     }
467     fprintf (file,
468         ");\n"
469         "  };\n"
470         "  link = {\n"
471         "    type = rhombus;\n"
472         "    size = bandwidth;\n"
473         "    values = (");
474     xbt_dict_foreach(created_categories,cursor2,name2,value2) {
475       fprintf (file, "b%s, ", name2);
476     }
477     fprintf (file,
478         ");\n"
479         "  };\n");
480     //close
481     fprintf (file, "}\n");
482     fclose (file);
483   }
484 }
485
486 #undef OPT_TRACING
487 #undef OPT_TRACING_SMPI
488 #undef OPT_TRACING_SMPI_GROUP
489 #undef OPT_TRACING_CATEGORIZED
490 #undef OPT_TRACING_UNCATEGORIZED
491 #undef OPT_TRACING_MSG_TASK
492 #undef OPT_TRACING_MSG_PROCESS
493 #undef OPT_TRACING_MSG_VOLUME
494 #undef OPT_TRACING_FILENAME
495 #undef OPT_TRACING_PLATFORM_METHOD
496 #undef OPT_TRIVA_UNCAT_CONF
497 #undef OPT_TRIVA_CAT_CONF
498
499 #endif /* HAVE_TRACING */