Logo AND Algorithmique Numérique Distribuée

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