Logo AND Algorithmique Numérique Distribuée

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