Logo AND Algorithmique Numérique Distribuée

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