Logo AND Algorithmique Numérique Distribuée

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