Logo AND Algorithmique Numérique Distribuée

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