Logo AND Algorithmique Numérique Distribuée

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