Logo AND Algorithmique Numérique Distribuée

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