Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
merge conflicts resolved
[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_TRACING_DISABLE_DESTROY "tracing/disable_destroy"
27 #define OPT_TRIVA_UNCAT_CONF      "triva/uncategorized"
28 #define OPT_TRIVA_CAT_CONF        "triva/categorized"
29
30 static int trace_enabled;
31 static int trace_platform;
32 static int trace_smpi_enabled;
33 static int trace_smpi_grouped;
34 static int trace_categorized;
35 static int trace_uncategorized;
36 static int trace_msg_task_enabled;
37 static int trace_msg_process_enabled;
38 static int trace_buffer;
39 static int trace_onelink_only;
40 static int trace_disable_destroy;
41
42 static int trace_configured = 0;
43 static int trace_active = 0;
44
45 xbt_dict_t created_categories; //declared in instr_interface.c
46
47 static void TRACE_getopts(void)
48 {
49   trace_enabled = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING);
50   trace_platform = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_PLATFORM);
51   trace_smpi_enabled = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_SMPI);
52   trace_smpi_grouped = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_SMPI_GROUP);
53   trace_categorized = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_CATEGORIZED);
54   trace_uncategorized = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_UNCATEGORIZED);
55   trace_msg_task_enabled = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_MSG_TASK);
56   trace_msg_process_enabled = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_MSG_PROCESS);
57   trace_buffer = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_BUFFER);
58   trace_onelink_only = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_ONELINK_ONLY);
59   trace_disable_destroy = xbt_cfg_get_int(_surf_cfg_set, OPT_TRACING_DISABLE_DESTROY);
60 }
61
62 int TRACE_start()
63 {
64   TRACE_getopts();
65
66   // tracing system must be:
67   //    - enabled (with --cfg=tracing:1)
68   //    - already configured (TRACE_global_init already called)
69   if (!(TRACE_is_enabled() && TRACE_is_configured())){
70     return 0;
71   }
72
73   XBT_DEBUG("Tracing starts");
74
75   /* open the trace file */
76   TRACE_paje_start();
77
78   /* activate trace */
79   xbt_assert (trace_active==0, "Tracing is already active.");
80   trace_active = 1;
81   XBT_DEBUG ("Tracing is on");
82
83   /* other trace initialization */
84   created_categories = xbt_dict_new_homogeneous(xbt_free);
85   TRACE_surf_alloc();
86   TRACE_smpi_alloc();
87   return 0;
88 }
89
90 int TRACE_end()
91 {
92   if (!trace_active)
93     return 1;
94
95   /* generate uncategorized graph configuration for triva */
96   if (TRACE_get_triva_uncat_conf()){
97     TRACE_generate_triva_uncat_conf();
98   }
99
100   /* generate categorized graph configuration for triva */
101   if (TRACE_get_triva_cat_conf()){
102     TRACE_generate_triva_cat_conf();
103   }
104
105   /* dump trace buffer */
106   TRACE_last_timestamp_to_dump = surf_get_clock();
107   TRACE_paje_dump_buffer(1);
108
109   /* destroy all data structures of tracing (and free) */
110   destroyAllContainers();
111   instr_paje_free();
112   TRACE_surf_release();
113   TRACE_smpi_release();
114   xbt_dict_free(&created_categories);
115
116   /* close the trace file */
117   TRACE_paje_end();
118
119   /* de-activate trace */
120   trace_active = 0;
121   XBT_DEBUG ("Tracing is off");
122   XBT_DEBUG("Tracing system is shutdown");
123   return 0;
124 }
125
126 int TRACE_needs_platform (void)
127 {
128   return TRACE_msg_process_is_enabled() ||
129          TRACE_msg_task_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 (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_buffer (void)
184 {
185   return trace_buffer && TRACE_is_enabled();
186 }
187
188 int TRACE_onelink_only (void)
189 {
190   return trace_onelink_only && TRACE_is_enabled();
191 }
192
193 int TRACE_disable_destroy (void)
194 {
195   return trace_disable_destroy && 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   /* register platform in the trace */
230   int default_tracing_platform = 0;
231   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_PLATFORM,
232                    "Register the platform in the trace as a graph.",
233                    xbt_cfgelm_int, &default_tracing_platform, 0, 1,
234                    NULL, NULL);
235
236   /* smpi */
237   int default_tracing_smpi = 0;
238   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_SMPI,
239                    "Tracing of the SMPI interface.",
240                    xbt_cfgelm_int, &default_tracing_smpi, 0, 1,
241                    NULL, NULL);
242
243   /* smpi grouped */
244   int default_tracing_smpi_grouped = 0;
245   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_SMPI_GROUP,
246                    "Group MPI processes by host.",
247                    xbt_cfgelm_int, &default_tracing_smpi_grouped, 0, 1,
248                    NULL, NULL);
249
250
251   /* tracing categorized resource utilization traces */
252   int default_tracing_categorized = 0;
253   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_CATEGORIZED,
254                    "Tracing categorized resource utilization of hosts and links.",
255                    xbt_cfgelm_int, &default_tracing_categorized, 0, 1,
256                    NULL, NULL);
257
258   /* tracing uncategorized resource utilization */
259   int default_tracing_uncategorized = 0;
260   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_UNCATEGORIZED,
261                    "Tracing uncategorized resource utilization of hosts and links.",
262                    xbt_cfgelm_int, &default_tracing_uncategorized, 0, 1,
263                    NULL, NULL);
264
265   /* msg task */
266   int default_tracing_msg_task = 0;
267   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_MSG_TASK,
268                    "Tracing of MSG task behavior.",
269                    xbt_cfgelm_int, &default_tracing_msg_task, 0, 1,
270                    NULL, NULL);
271
272   /* msg process */
273   int default_tracing_msg_process = 0;
274   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_MSG_PROCESS,
275                    "Tracing of MSG process behavior.",
276                    xbt_cfgelm_int, &default_tracing_msg_process, 0, 1,
277                    NULL, NULL);
278
279   /* tracing buffer */
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   /* tracing one link only */
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   /* disable destroy */
294   int default_disable_destroy = 0;
295   xbt_cfg_register(&_surf_cfg_set, OPT_TRACING_DISABLE_DESTROY,
296                    "Disable platform containers destruction.",
297                    xbt_cfgelm_int, &default_disable_destroy, 0, 1,
298                    NULL, NULL);
299
300   /* Triva graph configuration for uncategorized tracing */
301   char *default_triva_uncat_conf_file = xbt_strdup ("");
302   xbt_cfg_register(&_surf_cfg_set, OPT_TRIVA_UNCAT_CONF,
303                    "Triva Graph configuration file for uncategorized resource utilization traces.",
304                    xbt_cfgelm_string, &default_triva_uncat_conf_file, 1, 1,
305                    NULL, NULL);
306
307   /* Triva graph configuration for uncategorized tracing */
308   char *default_triva_cat_conf_file = xbt_strdup ("");
309   xbt_cfg_register(&_surf_cfg_set, OPT_TRIVA_CAT_CONF,
310                    "Triva Graph configuration file for categorized resource utilization traces.",
311                    xbt_cfgelm_string, &default_triva_cat_conf_file, 1, 1,
312                    NULL, NULL);
313
314   /* instrumentation can be considered configured now */
315   trace_configured = 1;
316 }
317
318 static void print_line (const char *option, const char *desc, const char *longdesc, int detailed)
319 {
320   char str[INSTR_DEFAULT_STR_SIZE];
321   snprintf (str, INSTR_DEFAULT_STR_SIZE, "--cfg=%s ", option);
322
323   int len = strlen (str);
324   printf ("%s%*.*s %s\n", str, 30-len, 30-len, "", desc);
325   if (!!longdesc && detailed){
326     printf ("%s\n\n", longdesc);
327   }
328 }
329
330 void TRACE_help (int detailed)
331 {
332   printf(
333       "Description of the tracing options accepted by this simulator:\n\n");
334   print_line (OPT_TRACING, "Enable the tracing system",
335       "  It activates the tracing system and register the simulation platform\n"
336       "  in the trace file. You have to enable this option to others take effect.",
337       detailed);
338   print_line (OPT_TRACING_CATEGORIZED, "Trace categorized resource utilization",
339       "  It activates the categorized resource utilization tracing. It should\n"
340       "  be enabled if tracing categories are used by this simulator.",
341       detailed);
342   print_line (OPT_TRACING_UNCATEGORIZED, "Trace uncategorized resource utilization",
343       "  It activates the uncategorized resource utilization tracing. Use it if\n"
344       "  this simulator do not use tracing categories and resource use have to be\n"
345       "  traced.",
346       detailed);
347   print_line (OPT_TRACING_FILENAME, "Filename to register traces",
348       "  A file with this name will be created to register the simulation. The file\n"
349       "  is in the Paje format and can be analyzed using Triva or Paje visualization\n"
350       "  tools. More information can be found in these webpages:\n"
351       "     http://triva.gforge.inria.fr/\n"
352       "     http://paje.sourceforge.net/",
353       detailed);
354   print_line (OPT_TRACING_SMPI, "Trace the MPI Interface (SMPI)",
355       "  This option only has effect if this simulator is SMPI-based. Traces the MPI\n"
356       "  interface and generates a trace that can be analyzed using Gantt-like\n"
357       "  visualizations. Every MPI function (implemented by SMPI) is transformed in a\n"
358       "  state, and point-to-point communications can be analyzed with arrows.",
359       detailed);
360   print_line (OPT_TRACING_SMPI_GROUP, "Group MPI processes by host (SMPI)",
361       "  This option only has effect if this simulator is SMPI-based. The processes\n"
362       "  are grouped by the hosts where they were executed.",
363       detailed);
364   print_line (OPT_TRACING_MSG_TASK, "Trace task behavior (MSG)",
365       "  This option only has effect if this simulator is MSG-based. It traces the\n"
366       "  behavior of all categorized MSG tasks, grouping them by hosts.",
367       detailed);
368   print_line (OPT_TRACING_MSG_PROCESS, "Trace processes behavior (MSG)",
369       "  This option only has effect if this simulator is MSG-based. It traces the\n"
370       "  behavior of all categorized MSG processes, grouping them by hosts. This option\n"
371       "  can be used to track process location if this simulator has process migration.",
372       detailed);
373   print_line (OPT_TRACING_BUFFER, "Buffer events to put them in temporal order",
374       "  This option put some events in a time-ordered buffer using the insertion\n"
375       "  sort algorithm. The process of acquiring and releasing locks to access this\n"
376       "  buffer and the cost of the sorting algorithm make this process slow. The\n"
377       "  simulator performance can be severely impacted if this option is activated,\n"
378       "  but you are sure to get a trace file with events sorted.",
379       detailed);
380   print_line (OPT_TRACING_ONELINK_ONLY, "Consider only one link routes to trace platform",
381       "  This option changes the way SimGrid register its platform on the trace file.\n"
382       "  Normally, the tracing considers all routes (no matter their size) on the\n"
383       "  platform file to re-create the resource topology. If this option is activated,\n"
384       "  only the routes with one link are used to register the topology within an AS.\n"
385       "  Routes among AS continue to be traced as usual.",
386       detailed);
387   print_line (OPT_TRACING_DISABLE_DESTROY, "Disable platform containers destruction",
388       "  Disable the destruction of containers at the end of simulation. This can be\n"
389       "  used with simulators that have a different notion of time (different from\n"
390       "  the simulated time).",
391       detailed);
392   print_line (OPT_TRIVA_UNCAT_CONF, "Generate graph configuration for Triva",
393       "  This option can be used in all types of simulators build with SimGrid\n"
394       "  to generate a uncategorized resource utilization graph to be used as\n"
395       "  configuration for the Triva visualization analysis. This option\n"
396       "  can be used with tracing/categorized:1 and tracing:1 options to\n"
397       "  analyze an unmodified simulator before changing it to contain\n"
398       "  categories.",
399       detailed);
400   print_line (OPT_TRIVA_CAT_CONF, "generate uncategorized graph configuration for Triva",
401       "  This option can be used if this simulator uses tracing categories\n"
402       "  in its code. The file specified by this option holds a graph configuration\n"
403       "  file for the Triva visualization tool that can be used to analyze a categorized\n"
404       "  resource utilization.",
405       detailed);
406 }
407
408 void TRACE_generate_triva_uncat_conf (void)
409 {
410   char *output = TRACE_get_triva_uncat_conf ();
411   if (output && strlen(output) > 0){
412     xbt_dict_cursor_t cursor=NULL;
413     char *name, *value;
414
415     FILE *file = fopen (output, "w");
416     xbt_assert (file != NULL,
417        "Unable to open file (%s) for writing triva graph "
418        "configuration (uncategorized).", output);
419
420     //open
421     fprintf (file, "{\n");
422
423     //register NODE types
424     fprintf (file, "  node = (");
425     xbt_dict_foreach(trivaNodeTypes, cursor, name, value) {
426       fprintf (file, "%s, ", name);
427     }
428
429     //register EDGE types
430     fprintf (file,
431         ");\n"
432         "  edge = (");
433     xbt_dict_foreach(trivaEdgeTypes, cursor, name, value) {
434       fprintf (file, "%s, ", name);
435     }
436     fprintf (file,
437         ");\n"
438         "\n");
439
440     //configuration for all nodes
441     fprintf (file,
442         "  host = {\n"
443         "    type = square;\n"
444         "    size = power;\n"
445         "    values = (power_used);\n"
446         "  };\n"
447         "  link = {\n"
448         "    type = rhombus;\n"
449         "    size = bandwidth;\n"
450         "    values = (bandwidth_used);\n"
451         "  };\n");
452     //close
453     fprintf (file, "}\n");
454     fclose (file);
455   }
456 }
457
458 void TRACE_generate_triva_cat_conf (void)
459 {
460   char *output = TRACE_get_triva_cat_conf();
461   if (output && strlen(output) > 0){
462     xbt_dict_cursor_t cursor=NULL, cursor2=NULL;
463     char *name, *name2, *value, *value2;
464
465     //check if we do have categories declared
466     if (xbt_dict_is_empty(created_categories)){
467       XBT_INFO("No categories declared, ignoring generation of triva graph configuration");
468       return;
469     }
470
471     FILE *file = fopen (output, "w");
472     xbt_assert (file != NULL,
473        "Unable to open file (%s) for writing triva graph "
474        "configuration (categorized).", output);
475
476     //open
477     fprintf (file, "{\n");
478
479     //register NODE types
480     fprintf (file, "  node = (");
481     xbt_dict_foreach(trivaNodeTypes, cursor, name, value) {
482       fprintf (file, "%s, ", name);
483     }
484
485     //register EDGE types
486     fprintf (file,
487         ");\n"
488         "  edge = (");
489     xbt_dict_foreach(trivaEdgeTypes, cursor, name, value) {
490       fprintf (file, "%s, ", name);
491     }
492     fprintf (file,
493         ");\n"
494         "\n");
495
496     //configuration for all nodes
497     fprintf (file,
498         "  host = {\n"
499         "    type = square;\n"
500         "    size = power;\n"
501         "    values = (");
502     xbt_dict_foreach(created_categories,cursor2,name2,value2) {
503       fprintf (file, "p%s, ", name2);
504     }
505     fprintf (file,
506         ");\n"
507         "  };\n"
508         "  link = {\n"
509         "    type = rhombus;\n"
510         "    size = bandwidth;\n"
511         "    values = (");
512     xbt_dict_foreach(created_categories,cursor2,name2,value2) {
513       fprintf (file, "b%s, ", name2);
514     }
515     fprintf (file,
516         ");\n"
517         "  };\n");
518     //close
519     fprintf (file, "}\n");
520     fclose (file);
521   }
522 }
523
524 void TRACE_set_network_update_mechanism (void)
525 {
526   if (TRACE_is_enabled()){
527     if (TRACE_categorized() || TRACE_uncategorized()){
528       XBT_INFO ("Tracing resource utilization active, network/optim configuration now set to Full.");
529       xbt_cfg_set_string (_surf_cfg_set, "network/optim", "Full");
530     }
531   }
532 }
533
534 #undef OPT_TRACING
535 #undef OPT_TRACING_PLATFORM
536 #undef OPT_TRACING_SMPI
537 #undef OPT_TRACING_SMPI_GROUP
538 #undef OPT_TRACING_CATEGORIZED
539 #undef OPT_TRACING_UNCATEGORIZED
540 #undef OPT_TRACING_MSG_TASK
541 #undef OPT_TRACING_MSG_PROCESS
542 #undef OPT_TRACING_FILENAME
543 #undef OPT_TRACING_BUFFER
544 #undef OPT_TRACING_ONELINK_ONLY
545 #undef OPT_TRACING_DISABLE_DESTROY
546 #undef OPT_TRIVA_UNCAT_CONF
547 #undef OPT_TRIVA_CAT_CONF
548
549 #endif /* HAVE_TRACING */