Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
e91a964db4b8944b4794af80b4259f3eb92e9a05
[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   PJ_container_free_all();
108   PJ_type_free_all();
109   PJ_container_release();
110   PJ_type_release();
111   TRACE_surf_release();
112   TRACE_smpi_release();
113   xbt_dict_free(&created_categories);
114
115   /* close the trace file */
116   TRACE_paje_end();
117
118   /* de-activate trace */
119   trace_active = 0;
120   XBT_DEBUG ("Tracing is off");
121   XBT_DEBUG("Tracing system is shutdown");
122   return 0;
123 }
124
125 int TRACE_needs_platform (void)
126 {
127   return TRACE_msg_process_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_process_is_enabled(void)
172 {
173   return trace_msg_process_enabled && TRACE_is_enabled();
174 }
175
176 int TRACE_buffer (void)
177 {
178   return trace_buffer && TRACE_is_enabled();
179 }
180
181 int TRACE_onelink_only (void)
182 {
183   return trace_onelink_only && TRACE_is_enabled();
184 }
185
186 int TRACE_disable_destroy (void)
187 {
188   return trace_disable_destroy && TRACE_is_enabled();
189 }
190
191 char *TRACE_get_filename(void)
192 {
193   return xbt_cfg_get_string(_surf_cfg_set, OPT_TRACING_FILENAME);
194 }
195
196 char *TRACE_get_triva_uncat_conf (void)
197 {
198   return xbt_cfg_get_string(_surf_cfg_set, OPT_TRIVA_UNCAT_CONF);
199 }
200
201 char *TRACE_get_triva_cat_conf (void)
202 {
203   return xbt_cfg_get_string(_surf_cfg_set, OPT_TRIVA_CAT_CONF);
204 }
205
206 void TRACE_global_init(int *argc, char **argv)
207 {
208   /* name of the tracefile */
209   char *default_tracing_filename = xbt_strdup("simgrid.trace");
210   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_FILENAME,
211                    "Trace file created by the instrumented SimGrid.",
212                    xbt_cfgelm_string, &default_tracing_filename, 1, 1,
213                    NULL, NULL);
214
215   /* tracing */
216   int default_tracing = 0;
217   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING,
218                    "Enable Tracing.",
219                    xbt_cfgelm_int, &default_tracing, 0, 1,
220                    NULL, NULL);
221
222   /* register platform in the trace */
223   int default_tracing_platform = 0;
224   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_PLATFORM,
225                    "Register the platform in the trace as a graph.",
226                    xbt_cfgelm_int, &default_tracing_platform, 0, 1,
227                    NULL, NULL);
228
229   /* smpi */
230   int default_tracing_smpi = 0;
231   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_SMPI,
232                    "Tracing of the SMPI interface.",
233                    xbt_cfgelm_int, &default_tracing_smpi, 0, 1,
234                    NULL, NULL);
235
236   /* smpi grouped */
237   int default_tracing_smpi_grouped = 0;
238   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_SMPI_GROUP,
239                    "Group MPI processes by host.",
240                    xbt_cfgelm_int, &default_tracing_smpi_grouped, 0, 1,
241                    NULL, NULL);
242
243
244   /* tracing categorized resource utilization traces */
245   int default_tracing_categorized = 0;
246   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_CATEGORIZED,
247                    "Tracing categorized resource utilization of hosts and links.",
248                    xbt_cfgelm_int, &default_tracing_categorized, 0, 1,
249                    NULL, NULL);
250
251   /* tracing uncategorized resource utilization */
252   int default_tracing_uncategorized = 0;
253   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_UNCATEGORIZED,
254                    "Tracing uncategorized resource utilization of hosts and links.",
255                    xbt_cfgelm_int, &default_tracing_uncategorized, 0, 1,
256                    NULL, NULL);
257
258   /* msg process */
259   int default_tracing_msg_process = 0;
260   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_MSG_PROCESS,
261                    "Tracing of MSG process behavior.",
262                    xbt_cfgelm_int, &default_tracing_msg_process, 0, 1,
263                    NULL, NULL);
264
265   /* tracing buffer */
266   int default_buffer = 0;
267   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_BUFFER,
268                    "Buffer trace events to put them in temporal order.",
269                    xbt_cfgelm_int, &default_buffer, 0, 1,
270                    NULL, NULL);
271
272   /* tracing one link only */
273   int default_onelink_only = 0;
274   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_ONELINK_ONLY,
275                    "Use only routes with one link to trace platform.",
276                    xbt_cfgelm_int, &default_onelink_only, 0, 1,
277                    NULL, NULL);
278
279   /* disable destroy */
280   int default_disable_destroy = 0;
281   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_DISABLE_DESTROY,
282                    "Disable platform containers destruction.",
283                    xbt_cfgelm_int, &default_disable_destroy, 0, 1,
284                    NULL, NULL);
285
286   /* Triva graph configuration for uncategorized tracing */
287   char *default_triva_uncat_conf_file = xbt_strdup ("");
288   xbt_cfg_register(&_surf_cfg_set, OPT_TRIVA_UNCAT_CONF,
289                    "Triva Graph configuration file for uncategorized resource utilization traces.",
290                    xbt_cfgelm_string, &default_triva_uncat_conf_file, 1, 1,
291                    NULL, NULL);
292
293   /* Triva graph configuration for uncategorized tracing */
294   char *default_triva_cat_conf_file = xbt_strdup ("");
295   xbt_cfg_register(&_surf_cfg_set, OPT_TRIVA_CAT_CONF,
296                    "Triva Graph configuration file for categorized resource utilization traces.",
297                    xbt_cfgelm_string, &default_triva_cat_conf_file, 1, 1,
298                    NULL, NULL);
299
300   /* instrumentation can be considered configured now */
301   trace_configured = 1;
302 }
303
304 static void print_line (const char *option, const char *desc, const char *longdesc, int detailed)
305 {
306   char str[INSTR_DEFAULT_STR_SIZE];
307   snprintf (str, INSTR_DEFAULT_STR_SIZE, "--cfg=%s ", option);
308
309   int len = strlen (str);
310   printf ("%s%*.*s %s\n", str, 30-len, 30-len, "", desc);
311   if (!!longdesc && detailed){
312     printf ("%s\n\n", longdesc);
313   }
314 }
315
316 void TRACE_help (int detailed)
317 {
318   printf(
319       "Description of the tracing options accepted by this simulator:\n\n");
320   print_line (OPT_TRACING, "Enable the tracing system",
321       "  It activates the tracing system and register the simulation platform\n"
322       "  in the trace file. You have to enable this option to others take effect.",
323       detailed);
324   print_line (OPT_TRACING_CATEGORIZED, "Trace categorized resource utilization",
325       "  It activates the categorized resource utilization tracing. It should\n"
326       "  be enabled if tracing categories are used by this simulator.",
327       detailed);
328   print_line (OPT_TRACING_UNCATEGORIZED, "Trace uncategorized resource utilization",
329       "  It activates the uncategorized resource utilization tracing. Use it if\n"
330       "  this simulator do not use tracing categories and resource use have to be\n"
331       "  traced.",
332       detailed);
333   print_line (OPT_TRACING_FILENAME, "Filename to register traces",
334       "  A file with this name will be created to register the simulation. The file\n"
335       "  is in the Paje format and can be analyzed using Triva or Paje visualization\n"
336       "  tools. More information can be found in these webpages:\n"
337       "     http://triva.gforge.inria.fr/\n"
338       "     http://paje.sourceforge.net/",
339       detailed);
340   print_line (OPT_TRACING_SMPI, "Trace the MPI Interface (SMPI)",
341       "  This option only has effect if this simulator is SMPI-based. Traces the MPI\n"
342       "  interface and generates a trace that can be analyzed using Gantt-like\n"
343       "  visualizations. Every MPI function (implemented by SMPI) is transformed in a\n"
344       "  state, and point-to-point communications can be analyzed with arrows.",
345       detailed);
346   print_line (OPT_TRACING_SMPI_GROUP, "Group MPI processes by host (SMPI)",
347       "  This option only has effect if this simulator is SMPI-based. The processes\n"
348       "  are grouped by the hosts where they were executed.",
349       detailed);
350   print_line (OPT_TRACING_MSG_PROCESS, "Trace processes behavior (MSG)",
351       "  This option only has effect if this simulator is MSG-based. It traces the\n"
352       "  behavior of all categorized MSG processes, grouping them by hosts. This option\n"
353       "  can be used to track process location if this simulator has process migration.",
354       detailed);
355   print_line (OPT_TRACING_BUFFER, "Buffer events to put them in temporal order",
356       "  This option put some events in a time-ordered buffer using the insertion\n"
357       "  sort algorithm. The process of acquiring and releasing locks to access this\n"
358       "  buffer and the cost of the sorting algorithm make this process slow. The\n"
359       "  simulator performance can be severely impacted if this option is activated,\n"
360       "  but you are sure to get a trace file with events sorted.",
361       detailed);
362   print_line (OPT_TRACING_ONELINK_ONLY, "Consider only one link routes to trace platform",
363       "  This option changes the way SimGrid register its platform on the trace file.\n"
364       "  Normally, the tracing considers all routes (no matter their size) on the\n"
365       "  platform file to re-create the resource topology. If this option is activated,\n"
366       "  only the routes with one link are used to register the topology within an AS.\n"
367       "  Routes among AS continue to be traced as usual.",
368       detailed);
369   print_line (OPT_TRACING_DISABLE_DESTROY, "Disable platform containers destruction",
370       "  Disable the destruction of containers at the end of simulation. This can be\n"
371       "  used with simulators that have a different notion of time (different from\n"
372       "  the simulated time).",
373       detailed);
374   print_line (OPT_TRIVA_UNCAT_CONF, "Generate graph configuration for Triva",
375       "  This option can be used in all types of simulators build with SimGrid\n"
376       "  to generate a uncategorized resource utilization graph to be used as\n"
377       "  configuration for the Triva visualization analysis. This option\n"
378       "  can be used with tracing/categorized:1 and tracing:1 options to\n"
379       "  analyze an unmodified simulator before changing it to contain\n"
380       "  categories.",
381       detailed);
382   print_line (OPT_TRIVA_CAT_CONF, "generate uncategorized graph configuration for Triva",
383       "  This option can be used if this simulator uses tracing categories\n"
384       "  in its code. The file specified by this option holds a graph configuration\n"
385       "  file for the Triva visualization tool that can be used to analyze a categorized\n"
386       "  resource utilization.",
387       detailed);
388 }
389
390 void TRACE_generate_triva_uncat_conf (void)
391 {
392   char *output = TRACE_get_triva_uncat_conf ();
393   if (output && strlen(output) > 0){
394     xbt_dict_cursor_t cursor=NULL;
395     char *name, *value;
396
397     FILE *file = fopen (output, "w");
398     xbt_assert (file != NULL,
399        "Unable to open file (%s) for writing triva graph "
400        "configuration (uncategorized).", output);
401
402     //open
403     fprintf (file, "{\n");
404
405     //register NODE types
406     fprintf (file, "  node = (");
407     xbt_dict_foreach(trivaNodeTypes, cursor, name, value) {
408       fprintf (file, "%s, ", name);
409     }
410
411     //register EDGE types
412     fprintf (file,
413         ");\n"
414         "  edge = (");
415     xbt_dict_foreach(trivaEdgeTypes, cursor, name, value) {
416       fprintf (file, "%s, ", name);
417     }
418     fprintf (file,
419         ");\n"
420         "\n");
421
422     //configuration for all nodes
423     fprintf (file,
424         "  host = {\n"
425         "    type = square;\n"
426         "    size = power;\n"
427         "    values = (power_used);\n"
428         "  };\n"
429         "  link = {\n"
430         "    type = rhombus;\n"
431         "    size = bandwidth;\n"
432         "    values = (bandwidth_used);\n"
433         "  };\n");
434     //close
435     fprintf (file, "}\n");
436     fclose (file);
437   }
438 }
439
440 void TRACE_generate_triva_cat_conf (void)
441 {
442   char *output = TRACE_get_triva_cat_conf();
443   if (output && strlen(output) > 0){
444     xbt_dict_cursor_t cursor=NULL, cursor2=NULL;
445     char *name, *name2, *value, *value2;
446
447     //check if we do have categories declared
448     if (xbt_dict_is_empty(created_categories)){
449       XBT_INFO("No categories declared, ignoring generation of triva graph configuration");
450       return;
451     }
452
453     FILE *file = fopen (output, "w");
454     xbt_assert (file != NULL,
455        "Unable to open file (%s) for writing triva graph "
456        "configuration (categorized).", output);
457
458     //open
459     fprintf (file, "{\n");
460
461     //register NODE types
462     fprintf (file, "  node = (");
463     xbt_dict_foreach(trivaNodeTypes, cursor, name, value) {
464       fprintf (file, "%s, ", name);
465     }
466
467     //register EDGE types
468     fprintf (file,
469         ");\n"
470         "  edge = (");
471     xbt_dict_foreach(trivaEdgeTypes, cursor, name, value) {
472       fprintf (file, "%s, ", name);
473     }
474     fprintf (file,
475         ");\n"
476         "\n");
477
478     //configuration for all nodes
479     fprintf (file,
480         "  host = {\n"
481         "    type = square;\n"
482         "    size = power;\n"
483         "    values = (");
484     xbt_dict_foreach(created_categories,cursor2,name2,value2) {
485       fprintf (file, "p%s, ", name2);
486     }
487     fprintf (file,
488         ");\n"
489         "  };\n"
490         "  link = {\n"
491         "    type = rhombus;\n"
492         "    size = bandwidth;\n"
493         "    values = (");
494     xbt_dict_foreach(created_categories,cursor2,name2,value2) {
495       fprintf (file, "b%s, ", name2);
496     }
497     fprintf (file,
498         ");\n"
499         "  };\n");
500     //close
501     fprintf (file, "}\n");
502     fclose (file);
503   }
504 }
505
506 void TRACE_set_network_update_mechanism (void)
507 {
508   if (TRACE_is_enabled()){
509     if (TRACE_categorized() || TRACE_uncategorized()){
510       XBT_INFO ("Tracing resource utilization active, network/optim configuration now set to Full.");
511       xbt_cfg_set_string (_surf_cfg_set, "network/optim", "Full");
512     }
513   }
514 }
515
516 #undef OPT_TRACING
517 #undef OPT_TRACING_PLATFORM
518 #undef OPT_TRACING_SMPI
519 #undef OPT_TRACING_SMPI_GROUP
520 #undef OPT_TRACING_CATEGORIZED
521 #undef OPT_TRACING_UNCATEGORIZED
522 #undef OPT_TRACING_MSG_PROCESS
523 #undef OPT_TRACING_FILENAME
524 #undef OPT_TRACING_BUFFER
525 #undef OPT_TRACING_ONELINK_ONLY
526 #undef OPT_TRACING_DISABLE_DESTROY
527 #undef OPT_TRIVA_UNCAT_CONF
528 #undef OPT_TRIVA_CAT_CONF
529
530 #endif /* HAVE_TRACING */