Logo AND Algorithmique Numérique Distribuée

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