Logo AND Algorithmique Numérique Distribuée

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