Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3e6ed7682633a3ffa806f33adbdba19d25afc8a5
[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_BUFFER        "tracing/buffer"
25 #define OPT_TRACING_ONELINK_ONLY  "tracing/onelink_only"
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_smpi_enabled;
31 static int trace_smpi_grouped;
32 static int trace_categorized;
33 static int trace_uncategorized;
34 static int trace_msg_task_enabled;
35 static int trace_msg_process_enabled;
36 static int trace_msg_volume_enabled;
37 static int trace_buffer;
38 static int trace_onelink_only;
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_smpi_enabled = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_SMPI);
49   trace_smpi_grouped = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_SMPI_GROUP);
50   trace_categorized = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_CATEGORIZED);
51   trace_uncategorized = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_UNCATEGORIZED);
52   trace_msg_task_enabled = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_MSG_TASK);
53   trace_msg_process_enabled = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_MSG_PROCESS);
54   trace_msg_volume_enabled = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_MSG_VOLUME);
55   trace_buffer = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_BUFFER);
56   trace_onelink_only = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_ONELINK_ONLY);
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   TRACE_activate ();
77
78   /* other trace initialization */
79   created_categories = xbt_dict_new();
80   TRACE_surf_alloc();
81   TRACE_smpi_alloc();
82   return 0;
83 }
84
85 int TRACE_end()
86 {
87   if (!TRACE_is_active())
88     return 1;
89
90   /* generate uncategorized graph configuration for triva */
91   if (TRACE_get_triva_uncat_conf()){
92     TRACE_generate_triva_uncat_conf();
93   }
94
95   /* generate categorized graph configuration for triva */
96   if (TRACE_get_triva_cat_conf()){
97     TRACE_generate_triva_cat_conf();
98   }
99
100   /* dump trace buffer */
101   TRACE_last_timestamp_to_dump = surf_get_clock();
102   TRACE_paje_dump_buffer(1);
103
104   /* destroy all data structures of tracing (and free) */
105   destroyAllContainers();
106
107   /* close the trace file */
108   TRACE_paje_end();
109
110   /* activate trace */
111   TRACE_desactivate ();
112   XBT_DEBUG("Tracing system is shutdown");
113   return 0;
114 }
115
116 void TRACE_activate (void)
117 {
118   xbt_assert (trace_active==0, "Tracing is already active.");
119   trace_active = 1;
120   XBT_DEBUG ("Tracing is on");
121 }
122
123 void TRACE_desactivate (void)
124 {
125   trace_active = 0;
126   XBT_DEBUG ("Tracing is off");
127 }
128
129 int TRACE_is_active (void)
130 {
131   return trace_active;
132 }
133
134 int TRACE_needs_platform (void)
135 {
136   return TRACE_categorized() ||
137          TRACE_uncategorized() ||
138          (TRACE_smpi_is_enabled() && TRACE_smpi_is_grouped());
139 }
140
141 int TRACE_is_enabled(void)
142 {
143   return trace_enabled;
144 }
145
146 int TRACE_is_configured(void)
147 {
148   return trace_configured;
149 }
150
151 int TRACE_smpi_is_enabled(void)
152 {
153   return (xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_SMPI) ||
154        TRACE_smpi_is_grouped())&&
155       TRACE_is_enabled();
156 }
157
158 int TRACE_smpi_is_grouped(void)
159 {
160   return trace_smpi_grouped;
161 }
162
163 int TRACE_categorized (void)
164 {
165   return trace_categorized;
166 }
167
168 int TRACE_uncategorized (void)
169 {
170   return trace_uncategorized;
171 }
172
173 int TRACE_msg_task_is_enabled(void)
174 {
175   return trace_msg_task_enabled && TRACE_is_enabled();
176 }
177
178 int TRACE_msg_process_is_enabled(void)
179 {
180   return trace_msg_process_enabled && TRACE_is_enabled();
181 }
182
183 int TRACE_msg_volume_is_enabled(void)
184 {
185   return trace_msg_volume_enabled && TRACE_is_enabled();
186 }
187
188 int TRACE_buffer (void)
189 {
190   return trace_buffer && TRACE_is_enabled();
191 }
192
193 int TRACE_onelink_only (void)
194 {
195   return trace_onelink_only && TRACE_is_enabled();
196 }
197
198 char *TRACE_get_filename(void)
199 {
200   return xbt_cfg_get_string(_surf_cfg_set, OPT_TRACING_FILENAME);
201 }
202
203 char *TRACE_get_triva_uncat_conf (void)
204 {
205   return xbt_cfg_get_string(_surf_cfg_set, OPT_TRIVA_UNCAT_CONF);
206 }
207
208 char *TRACE_get_triva_cat_conf (void)
209 {
210   return xbt_cfg_get_string(_surf_cfg_set, OPT_TRIVA_CAT_CONF);
211 }
212
213 void TRACE_global_init(int *argc, char **argv)
214 {
215   /* name of the tracefile */
216   char *default_tracing_filename = xbt_strdup("simgrid.trace");
217   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_FILENAME,
218                    "Trace file created by the instrumented SimGrid.",
219                    xbt_cfgelm_string, &default_tracing_filename, 1, 1,
220                    NULL, NULL);
221
222   /* tracing */
223   int default_tracing = 0;
224   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING,
225                    "Enable Tracing.",
226                    xbt_cfgelm_int, &default_tracing, 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   /* platform */
245   int default_tracing_platform = 0;
246   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_CATEGORIZED,
247                    "Tracing of categorized platform (host and link) utilization.",
248                    xbt_cfgelm_int, &default_tracing_platform, 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 of uncategorized resource (host and link) utilization.",
255                    xbt_cfgelm_int, &default_tracing_uncategorized, 0, 1,
256                    NULL, NULL);
257
258   /* msg task */
259   int default_tracing_msg_task = 0;
260   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_MSG_TASK,
261                    "Tracing of MSG task behavior.",
262                    xbt_cfgelm_int, &default_tracing_msg_task, 0, 1,
263                    NULL, NULL);
264
265   /* msg process */
266   int default_tracing_msg_process = 0;
267   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_MSG_PROCESS,
268                    "Tracing of MSG process behavior.",
269                    xbt_cfgelm_int, &default_tracing_msg_process, 0, 1,
270                    NULL, NULL);
271
272   /* msg volume (experimental) */
273   int default_tracing_msg_volume = 0;
274   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_MSG_VOLUME,
275                    "Tracing of MSG communication volume (experimental).",
276                    xbt_cfgelm_int, &default_tracing_msg_volume, 0, 1,
277                    NULL, NULL);
278
279   /* msg volume (experimental) */
280   int default_buffer = 0;
281   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_BUFFER,
282                    "Buffer trace events to put them in temporal order.",
283                    xbt_cfgelm_int, &default_buffer, 0, 1,
284                    NULL, NULL);
285
286   /* msg volume (experimental) */
287   int default_onelink_only = 0;
288   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_ONELINK_ONLY,
289                    "Use only routes with one link to trace platform.",
290                    xbt_cfgelm_int, &default_onelink_only, 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_FILENAME, "Filename to register traces",
341       "  A file with this name will be created to register the simulation. The file\n"
342       "  is in the Paje format and can be analyzed using Triva or Paje visualization\n"
343       "  tools. More information can be found in these webpages:\n"
344       "     http://triva.gforge.inria.fr/\n"
345       "     http://paje.sourceforge.net/",
346       detailed);
347   print_line (OPT_TRACING_SMPI, "Trace the MPI Interface (SMPI)",
348       "  This option only has effect if this simulator is SMPI-based. Traces the MPI\n"
349       "  interface and generates a trace that can be analyzed using Gantt-like\n"
350       "  visualizations. Every MPI function (implemented by SMPI) is transformed in a\n"
351       "  state, and point-to-point communications can be analyzed with arrows.",
352       detailed);
353   print_line (OPT_TRACING_SMPI_GROUP, "Group MPI processes by host (SMPI)",
354       "  This option only has effect if this simulator is SMPI-based. The processes\n"
355       "  are grouped by the hosts where they were executed.",
356       detailed);
357   print_line (OPT_TRACING_MSG_TASK, "Trace task behavior (MSG)",
358       "  This option only has effect if this simulator is MSG-based. It traces the\n"
359       "  behavior of all categorized MSG tasks, grouping them by hosts.",
360       detailed);
361   print_line (OPT_TRACING_MSG_PROCESS, "Trace processes behavior (MSG)",
362       "  This option only has effect if this simulator is MSG-based. It traces the\n"
363       "  behavior of all categorized MSG processes, grouping them by hosts. This option\n"
364       "  can be used to track process location if this simulator has process migration.",
365       detailed);
366   print_line (OPT_TRACING_MSG_VOLUME, "Tracing of communication volume (MSG)",
367       "  This experimental option only has effect if this simulator is MSG-based.\n"
368       "  It traces the communication volume of MSG send/receive.",
369       detailed);
370   print_line (OPT_TRACING_BUFFER, "Buffer events to put them in temporal order",
371       "  This option put some events in a time-ordered buffer using the insertion\n"
372       "  sort algorithm. The process of acquiring and releasing locks to access this\n"
373       "  buffer and the cost of the sorting algorithm make this process slow. The\n"
374       "  simulator performance can be severely impacted if this option is activated,\n"
375       "  but you are sure to get a trace file with events sorted.",
376       detailed);
377   print_line (OPT_TRACING_ONELINK_ONLY, "Consider only one link routes to trace platform",
378       "  This option changes the way SimGrid register its platform on the trace file.\n"
379       "  Normally, the tracing considers all routes (no matter their size) on the\n"
380       "  platform file to re-create the resource topology. If this option is activated,\n"
381       "  only the routes with one link are used to register the topology within an AS.\n"
382       "  Routes among AS continue to be traced as usual.",
383       detailed);
384   print_line (OPT_TRIVA_UNCAT_CONF, "Generate graph configuration for Triva",
385       "  This option can be used in all types of simulators build with SimGrid\n"
386       "  to generate a uncategorized resource utilization graph to be used as\n"
387       "  configuration for the Triva visualization analysis. This option\n"
388       "  can be used with tracing/categorized:1 and tracing:1 options to\n"
389       "  analyze an unmodified simulator before changing it to contain\n"
390       "  categories.",
391       detailed);
392   print_line (OPT_TRIVA_CAT_CONF, "generate uncategorized graph configuration for Triva",
393       "  This option can be used if this simulator uses tracing categories\n"
394       "  in its code. The file specified by this option holds a graph configuration\n"
395       "  file for the Triva visualization tool that can be used to analyze a categorized\n"
396       "  resource utilization.",
397       detailed);
398 }
399
400 void TRACE_generate_triva_uncat_conf (void)
401 {
402   char *output = TRACE_get_triva_uncat_conf ();
403   if (output && strlen(output) > 0){
404     xbt_dict_cursor_t cursor=NULL;
405     char *name, *value;
406
407     FILE *file = fopen (output, "w");
408     xbt_assert (file != NULL,
409        "Unable to open file (%s) for writing triva graph "
410        "configuration (uncategorized).", output);
411
412     //open
413     fprintf (file, "{\n");
414
415     //register NODE types
416     fprintf (file, "  node = (");
417     xbt_dict_foreach(trivaNodeTypes, cursor, name, value) {
418       fprintf (file, "%s, ", name);
419     }
420
421     //register EDGE types
422     fprintf (file,
423         ");\n"
424         "  edge = (");
425     xbt_dict_foreach(trivaEdgeTypes, cursor, name, value) {
426       fprintf (file, "%s, ", name);
427     }
428     fprintf (file,
429         ");\n"
430         "\n");
431
432     //configuration for all nodes
433     fprintf (file,
434         "  host = {\n"
435         "    type = square;\n"
436         "    size = power;\n"
437         "    values = (power_used);\n"
438         "  };\n"
439         "  link = {\n"
440         "    type = rhombus;\n"
441         "    size = bandwidth;\n"
442         "    values = (bandwidth_used);\n"
443         "  };\n");
444     //close
445     fprintf (file, "}\n");
446     fclose (file);
447   }
448 }
449
450 void TRACE_generate_triva_cat_conf (void)
451 {
452   char *output = TRACE_get_triva_cat_conf();
453   if (output && strlen(output) > 0){
454     xbt_dict_cursor_t cursor=NULL, cursor2=NULL;
455     char *name, *name2, *value, *value2;
456
457     //check if we do have categories declared
458     if (xbt_dict_length(created_categories) == 0){
459       XBT_INFO("No categories declared, ignoring generation of triva graph configuration");
460       return;
461     }
462
463     FILE *file = fopen (output, "w");
464     xbt_assert (file != NULL,
465        "Unable to open file (%s) for writing triva graph "
466        "configuration (categorized).", output);
467
468     //open
469     fprintf (file, "{\n");
470
471     //register NODE types
472     fprintf (file, "  node = (");
473     xbt_dict_foreach(trivaNodeTypes, cursor, name, value) {
474       fprintf (file, "%s, ", name);
475     }
476
477     //register EDGE types
478     fprintf (file,
479         ");\n"
480         "  edge = (");
481     xbt_dict_foreach(trivaEdgeTypes, cursor, name, value) {
482       fprintf (file, "%s, ", name);
483     }
484     fprintf (file,
485         ");\n"
486         "\n");
487
488     //configuration for all nodes
489     fprintf (file,
490         "  host = {\n"
491         "    type = square;\n"
492         "    size = power;\n"
493         "    values = (");
494     xbt_dict_foreach(created_categories,cursor2,name2,value2) {
495       fprintf (file, "p%s, ", name2);
496     }
497     fprintf (file,
498         ");\n"
499         "  };\n"
500         "  link = {\n"
501         "    type = rhombus;\n"
502         "    size = bandwidth;\n"
503         "    values = (");
504     xbt_dict_foreach(created_categories,cursor2,name2,value2) {
505       fprintf (file, "b%s, ", name2);
506     }
507     fprintf (file,
508         ");\n"
509         "  };\n");
510     //close
511     fprintf (file, "}\n");
512     fclose (file);
513   }
514 }
515
516 #undef OPT_TRACING
517 #undef OPT_TRACING_SMPI
518 #undef OPT_TRACING_SMPI_GROUP
519 #undef OPT_TRACING_CATEGORIZED
520 #undef OPT_TRACING_UNCATEGORIZED
521 #undef OPT_TRACING_MSG_TASK
522 #undef OPT_TRACING_MSG_PROCESS
523 #undef OPT_TRACING_MSG_VOLUME
524 #undef OPT_TRACING_FILENAME
525 #undef OPT_TRACING_PLATFORM_METHOD
526 #undef OPT_TRIVA_UNCAT_CONF
527 #undef OPT_TRIVA_CAT_CONF
528
529 #endif /* HAVE_TRACING */