Logo AND Algorithmique Numérique Distribuée

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