Logo AND Algorithmique Numérique Distribuée

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