Logo AND Algorithmique Numérique Distribuée

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