Logo AND Algorithmique Numérique Distribuée

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