Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #241 from barais/master
[simgrid.git] / src / instr / instr_config.cpp
1 /* Copyright (c) 2010-2017. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "include/xbt/config.hpp"
7 #include "src/instr/instr_private.hpp"
8 #include "surf/surf.hpp"
9 #include <string>
10 #include <vector>
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_BASIC                "tracing/basic"
16 #define OPT_TRACING_BUFFER               "tracing/buffer"
17 #define OPT_TRACING_CATEGORIZED          "tracing/categorized"
18 #define OPT_TRACING_COMMENT_FILE         "tracing/comment-file"
19 #define OPT_TRACING_COMMENT              "tracing/comment"
20 #define OPT_TRACING_DISABLE_DESTROY      "tracing/disable-destroy"
21 #define OPT_TRACING_DISABLE_LINK         "tracing/disable-link"
22 #define OPT_TRACING_DISABLE_POWER        "tracing/disable-power"
23 #define OPT_TRACING_DISPLAY_SIZES        "tracing/smpi/display-sizes"
24 #define OPT_TRACING_FILENAME             "tracing/filename"
25 #define OPT_TRACING_FORMAT_TI_ONEFILE    "tracing/smpi/format/ti-one-file"
26 #define OPT_TRACING_FORMAT               "tracing/smpi/format"
27 #define OPT_TRACING_MSG_PROCESS          "tracing/msg/process"
28 #define OPT_TRACING_MSG_VM               "tracing/msg/vm"
29 #define OPT_TRACING_ONELINK_ONLY         "tracing/onelink-only"
30 #define OPT_TRACING_PLATFORM             "tracing/platform"
31 #define OPT_TRACING_PRECISION            "tracing/precision"
32 #define OPT_TRACING_SMPI_COMPUTING       "tracing/smpi/computing"
33 #define OPT_TRACING_SMPI_GROUP           "tracing/smpi/group"
34 #define OPT_TRACING_SMPI_INTERNALS       "tracing/smpi/internals"
35 #define OPT_TRACING_SMPI_SLEEPING        "tracing/smpi/sleeping"
36 #define OPT_TRACING_SMPI                 "tracing/smpi"
37 #define OPT_TRACING_TOPOLOGY             "tracing/platform/topology"
38 #define OPT_TRACING                      "tracing"
39 #define OPT_TRACING_UNCATEGORIZED        "tracing/uncategorized"
40
41 static bool trace_enabled = false;
42 static bool trace_platform;
43 static bool trace_platform_topology;
44 static bool trace_smpi_enabled;
45 static bool trace_smpi_grouped;
46 static bool trace_smpi_computing;
47 static bool trace_smpi_sleeping;
48 static bool trace_view_internals;
49 static bool trace_categorized;
50 static bool trace_uncategorized;
51 static bool trace_msg_process_enabled;
52 static bool trace_msg_vm_enabled;
53 static bool trace_buffer;
54 static bool trace_onelink_only;
55 static bool trace_disable_destroy;
56 static bool trace_basic;
57 static bool trace_display_sizes = false;
58 static bool trace_disable_link;
59 static bool trace_disable_power;
60 static int trace_precision;
61
62 static bool trace_configured = false;
63 static bool trace_active     = false;
64
65 instr_fmt_type_t instr_fmt_type = instr_fmt_paje;
66
67 static void TRACE_getopts()
68 {
69   trace_enabled             = xbt_cfg_get_boolean(OPT_TRACING);
70   trace_platform            = xbt_cfg_get_boolean(OPT_TRACING_PLATFORM);
71   trace_platform_topology   = xbt_cfg_get_boolean(OPT_TRACING_TOPOLOGY);
72   trace_smpi_enabled        = xbt_cfg_get_boolean(OPT_TRACING_SMPI);
73   trace_smpi_grouped        = xbt_cfg_get_boolean(OPT_TRACING_SMPI_GROUP);
74   trace_smpi_computing      = xbt_cfg_get_boolean(OPT_TRACING_SMPI_COMPUTING);
75   trace_smpi_sleeping       = xbt_cfg_get_boolean(OPT_TRACING_SMPI_SLEEPING);
76   trace_view_internals      = xbt_cfg_get_boolean(OPT_TRACING_SMPI_INTERNALS);
77   trace_categorized         = xbt_cfg_get_boolean(OPT_TRACING_CATEGORIZED);
78   trace_uncategorized       = xbt_cfg_get_boolean(OPT_TRACING_UNCATEGORIZED);
79   trace_msg_process_enabled = xbt_cfg_get_boolean(OPT_TRACING_MSG_PROCESS);
80   trace_msg_vm_enabled      = xbt_cfg_get_boolean(OPT_TRACING_MSG_VM);
81   trace_buffer              = xbt_cfg_get_boolean(OPT_TRACING_BUFFER);
82   trace_onelink_only        = xbt_cfg_get_boolean(OPT_TRACING_ONELINK_ONLY);
83   trace_disable_destroy     = xbt_cfg_get_boolean(OPT_TRACING_DISABLE_DESTROY);
84   trace_basic               = xbt_cfg_get_boolean(OPT_TRACING_BASIC);
85   trace_display_sizes       = xbt_cfg_get_boolean(OPT_TRACING_DISPLAY_SIZES);
86   trace_disable_link        = xbt_cfg_get_boolean(OPT_TRACING_DISABLE_LINK);
87   trace_disable_power       = xbt_cfg_get_boolean(OPT_TRACING_DISABLE_POWER);
88   trace_precision           = xbt_cfg_get_int(OPT_TRACING_PRECISION);
89 }
90
91 int TRACE_start()
92 {
93   if (TRACE_is_configured())
94     TRACE_getopts();
95
96   // tracing system must be:
97   //    - enabled (with --cfg=tracing:yes)
98   //    - already configured (TRACE_global_init already called)
99   if (TRACE_is_enabled()) {
100
101     XBT_DEBUG("Tracing starts");
102     /* init the tracing module to generate the right output */
103
104     /* open the trace file(s) */
105     std::string format = xbt_cfg_get_string(OPT_TRACING_FORMAT);
106     XBT_DEBUG("Tracing format %s\n", format.c_str());
107     if (format == "Paje") {
108       TRACE_paje_start();
109     } else if (format == "TI") {
110       instr_fmt_type = instr_fmt_TI;
111       TRACE_TI_start();
112     }else{
113       xbt_die("Unknown trace format :%s ", format.c_str());
114     }
115
116     /* activate trace */
117     if (trace_active) {
118       THROWF(tracing_error, 0, "Tracing is already active");
119     }
120     trace_active = true;
121     XBT_DEBUG("Tracing is on");
122   }
123   return 0;
124 }
125
126 int TRACE_end()
127 {
128   int retval;
129   if (not trace_active) {
130     retval = 1;
131   } else {
132     retval = 0;
133
134     /* dump trace buffer */
135     TRACE_last_timestamp_to_dump = surf_get_clock();
136     TRACE_paje_dump_buffer(true);
137
138     simgrid::instr::Type* root_type = simgrid::instr::Container::getRoot()->type_;
139     /* destroy all data structures of tracing (and free) */
140     delete simgrid::instr::Container::getRoot();
141     delete root_type;
142
143     /* close the trace files */
144     std::string format = xbt_cfg_get_string(OPT_TRACING_FORMAT);
145     XBT_DEBUG("Tracing format %s\n", format.c_str());
146     if (format == "Paje") {
147       TRACE_paje_end();
148     } else if (format == "TI") {
149       TRACE_TI_end();
150     }else{
151       xbt_die("Unknown trace format :%s ", format.c_str());
152     }
153
154     /* de-activate trace */
155     trace_active = false;
156     XBT_DEBUG("Tracing is off");
157     XBT_DEBUG("Tracing system is shutdown");
158   }
159   return retval;
160 }
161
162 bool TRACE_needs_platform ()
163 {
164   return TRACE_msg_process_is_enabled() || TRACE_msg_vm_is_enabled() || TRACE_categorized() ||
165          TRACE_uncategorized() || TRACE_platform () || (TRACE_smpi_is_enabled() && TRACE_smpi_is_grouped());
166 }
167
168 bool TRACE_is_enabled()
169 {
170   return trace_enabled;
171 }
172
173 bool TRACE_platform()
174 {
175   return trace_platform;
176 }
177
178 bool TRACE_platform_topology()
179 {
180   return trace_platform_topology;
181 }
182
183 bool TRACE_is_configured()
184 {
185   return trace_configured;
186 }
187
188 bool TRACE_smpi_is_enabled()
189 {
190   return (trace_smpi_enabled || TRACE_smpi_is_grouped()) && TRACE_is_enabled();
191 }
192
193 bool TRACE_smpi_is_grouped()
194 {
195   return trace_smpi_grouped;
196 }
197
198 bool TRACE_smpi_is_computing()
199 {
200   return trace_smpi_computing;
201 }
202
203 bool TRACE_smpi_is_sleeping()
204 {
205   return trace_smpi_sleeping;
206 }
207
208 bool TRACE_smpi_view_internals()
209 {
210   return trace_view_internals;
211 }
212
213 bool TRACE_categorized ()
214 {
215   return trace_categorized;
216 }
217
218 bool TRACE_uncategorized ()
219 {
220   return trace_uncategorized;
221 }
222
223 bool TRACE_msg_process_is_enabled()
224 {
225   return trace_msg_process_enabled && TRACE_is_enabled();
226 }
227
228 bool TRACE_msg_vm_is_enabled()
229 {
230   return trace_msg_vm_enabled && TRACE_is_enabled();
231 }
232
233 bool TRACE_disable_link()
234 {
235   return trace_disable_link && TRACE_is_enabled();
236 }
237
238 bool TRACE_disable_speed()
239 {
240   return trace_disable_power && TRACE_is_enabled();
241 }
242
243 bool TRACE_buffer ()
244 {
245   return trace_buffer && TRACE_is_enabled();
246 }
247
248 bool TRACE_onelink_only ()
249 {
250   return trace_onelink_only && TRACE_is_enabled();
251 }
252
253 bool TRACE_disable_destroy ()
254 {
255   return trace_disable_destroy && TRACE_is_enabled();
256 }
257
258 bool TRACE_basic ()
259 {
260   return trace_basic && TRACE_is_enabled();
261 }
262
263 bool TRACE_display_sizes ()
264 {
265    return trace_display_sizes && trace_smpi_enabled && TRACE_is_enabled();
266 }
267
268 std::string TRACE_get_comment()
269 {
270   return xbt_cfg_get_string(OPT_TRACING_COMMENT);
271 }
272
273 std::string TRACE_get_comment_file()
274 {
275   return xbt_cfg_get_string(OPT_TRACING_COMMENT_FILE);
276 }
277
278 int TRACE_precision ()
279 {
280   return xbt_cfg_get_int(OPT_TRACING_PRECISION);
281 }
282
283 std::string TRACE_get_filename()
284 {
285   return xbt_cfg_get_string(OPT_TRACING_FILENAME);
286 }
287
288 void TRACE_global_init()
289 {
290   static bool is_initialised = false;
291   if (is_initialised)
292     return;
293
294   is_initialised = true;
295   /* name of the tracefile */
296   xbt_cfg_register_string (OPT_TRACING_FILENAME, "simgrid.trace", nullptr, "Trace file created by the instrumented SimGrid.");
297   xbt_cfg_register_boolean(OPT_TRACING, "no", nullptr, "Enable Tracing.");
298   xbt_cfg_register_boolean(OPT_TRACING_PLATFORM, "no", nullptr, "Register the platform in the trace as a hierarchy.");
299   xbt_cfg_register_boolean(OPT_TRACING_TOPOLOGY, "yes", nullptr, "Register the platform topology in the trace as a graph.");
300   xbt_cfg_register_boolean(OPT_TRACING_SMPI, "no", nullptr, "Tracing of the SMPI interface.");
301   xbt_cfg_register_boolean(OPT_TRACING_SMPI_GROUP,"no", nullptr, "Group MPI processes by host.");
302   xbt_cfg_register_boolean(OPT_TRACING_SMPI_COMPUTING, "no", nullptr, "Generate states for timing out of SMPI parts of the application");
303   xbt_cfg_register_boolean(OPT_TRACING_SMPI_SLEEPING, "no", nullptr, "Generate states for timing out of SMPI parts of the application");
304   xbt_cfg_register_boolean(OPT_TRACING_SMPI_INTERNALS, "no", nullptr, "View internal messages sent by Collective communications in SMPI");
305   xbt_cfg_register_boolean(OPT_TRACING_CATEGORIZED, "no", nullptr, "Tracing categorized resource utilization of hosts and links.");
306   xbt_cfg_register_boolean(OPT_TRACING_UNCATEGORIZED, "no", nullptr, "Tracing uncategorized resource utilization of hosts and links.");
307
308   xbt_cfg_register_boolean(OPT_TRACING_MSG_PROCESS, "no", nullptr, "Tracing of MSG process behavior.");
309   xbt_cfg_register_boolean(OPT_TRACING_MSG_VM, "no", nullptr, "Tracing of MSG process behavior.");
310   xbt_cfg_register_boolean(OPT_TRACING_DISABLE_LINK, "no", nullptr, "Do not trace link bandwidth and latency.");
311   xbt_cfg_register_boolean(OPT_TRACING_DISABLE_POWER, "no", nullptr, "Do not trace host power.");
312   xbt_cfg_register_boolean(OPT_TRACING_BUFFER, "yes", nullptr, "Buffer trace events to put them in temporal order.");
313
314   xbt_cfg_register_boolean(OPT_TRACING_ONELINK_ONLY, "no", nullptr, "Use only routes with one link to trace platform.");
315   xbt_cfg_register_boolean(OPT_TRACING_DISABLE_DESTROY, "no", nullptr, "Disable platform containers destruction.");
316   xbt_cfg_register_boolean(OPT_TRACING_BASIC, "no", nullptr, "Avoid extended events (impoverished trace file).");
317   xbt_cfg_register_boolean(OPT_TRACING_DISPLAY_SIZES, "no", nullptr, "(smpi only) Extended events with message size information");
318   xbt_cfg_register_string(OPT_TRACING_FORMAT, "Paje", nullptr, "(smpi only) Switch the output format of Tracing");
319   xbt_cfg_register_boolean(OPT_TRACING_FORMAT_TI_ONEFILE, "no", nullptr, "(smpi only) For replay format only : output to one file only");
320   xbt_cfg_register_string(OPT_TRACING_COMMENT, "", nullptr, "Comment to be added on the top of the trace file.");
321   xbt_cfg_register_string(OPT_TRACING_COMMENT_FILE, "", nullptr,
322       "The contents of the file are added to the top of the trace file as comment.");
323   xbt_cfg_register_int(OPT_TRACING_PRECISION, 6, nullptr, "Numerical precision used when timestamping events "
324       "(expressed in number of digits after decimal point)");
325
326   xbt_cfg_register_alias(OPT_TRACING_COMMENT_FILE,"tracing/comment_file");
327   xbt_cfg_register_alias(OPT_TRACING_DISABLE_DESTROY, "tracing/disable_destroy");
328   xbt_cfg_register_alias(OPT_TRACING_DISABLE_LINK, "tracing/disable_link");
329   xbt_cfg_register_alias(OPT_TRACING_DISABLE_POWER, "tracing/disable_power");
330   xbt_cfg_register_alias(OPT_TRACING_DISPLAY_SIZES, "tracing/smpi/display_sizes");
331   xbt_cfg_register_alias(OPT_TRACING_FORMAT_TI_ONEFILE, "tracing/smpi/format/ti_one_file");
332   xbt_cfg_register_alias(OPT_TRACING_ONELINK_ONLY, "tracing/onelink_only");
333
334   /* instrumentation can be considered configured now */
335   trace_configured = true;
336 }
337
338 static void print_line (const char *option, const char *desc, const char *longdesc, int detailed)
339 {
340   std::string str = std::string("--cfg=") + option + " ";
341
342   int len = str.size();
343   printf("%s%*.*s %s\n", str.c_str(), 30 - len, 30 - len, "", desc);
344   if (longdesc != nullptr && detailed){
345     printf ("%s\n\n", longdesc);
346   }
347 }
348
349 void TRACE_help (int detailed)
350 {
351   printf("Description of the tracing options accepted by this simulator:\n\n");
352   print_line (OPT_TRACING, "Enable the tracing system",
353       "  It activates the tracing system and register the simulation platform\n"
354       "  in the trace file. You have to enable this option to others take effect.", detailed);
355   print_line (OPT_TRACING_CATEGORIZED, "Trace categorized resource utilization",
356       "  It activates the categorized resource utilization tracing. It should\n"
357       "  be enabled if tracing categories are used by this simulator.", detailed);
358   print_line (OPT_TRACING_UNCATEGORIZED, "Trace uncategorized resource utilization",
359       "  It activates the uncategorized resource utilization tracing. Use it if\n"
360       "  this simulator do not use tracing categories and resource use have to be\n"
361       "  traced.", detailed);
362   print_line(OPT_TRACING_FILENAME, "Filename to register traces",
363              "  A file with this name will be created to register the simulation. The file\n"
364              "  is in the Paje format and can be analyzed using Paje, and PajeNG visualization\n"
365              "  tools. More information can be found in these webpages:\n"
366              "     http://github.com/schnorr/pajeng/\n"
367              "     http://paje.sourceforge.net/",
368              detailed);
369   print_line (OPT_TRACING_SMPI, "Trace the MPI Interface (SMPI)",
370       "  This option only has effect if this simulator is SMPI-based. Traces the MPI\n"
371       "  interface and generates a trace that can be analyzed using Gantt-like\n"
372       "  visualizations. Every MPI function (implemented by SMPI) is transformed in a\n"
373       "  state, and point-to-point communications can be analyzed with arrows.", detailed);
374   print_line (OPT_TRACING_SMPI_GROUP, "Group MPI processes by host (SMPI)",
375       "  This option only has effect if this simulator is SMPI-based. The processes\n"
376       "  are grouped by the hosts where they were executed.", detailed);
377   print_line (OPT_TRACING_SMPI_COMPUTING, "Generates a \" Computing \" State",
378       "  This option aims at tracing computations in the application, outside SMPI\n"
379       "  to allow further study of simulated or real computation time", detailed);
380    print_line (OPT_TRACING_SMPI_SLEEPING, "Generates a \" Sleeping \" State",
381       "  This option aims at tracing sleeps in the application, outside SMPI\n"
382       "  to allow further study of simulated or real sleep time", detailed);
383   print_line (OPT_TRACING_SMPI_INTERNALS, "Generates tracing events corresponding",
384       "  to point-to-point messages sent by collective communications", detailed);
385   print_line (OPT_TRACING_MSG_PROCESS, "Trace processes behavior (MSG)",
386       "  This option only has effect if this simulator is MSG-based. It traces the\n"
387       "  behavior of all categorized MSG processes, grouping them by hosts. This option\n"
388       "  can be used to track process location if this simulator has process migration.", detailed);
389   print_line (OPT_TRACING_BUFFER, "Buffer events to put them in temporal order",
390       "  This option put some events in a time-ordered buffer using the insertion\n"
391       "  sort algorithm. The process of acquiring and releasing locks to access this\n"
392       "  buffer and the cost of the sorting algorithm make this process slow. The\n"
393       "  simulator performance can be severely impacted if this option is activated,\n"
394       "  but you are sure to get a trace file with events sorted.", detailed);
395   print_line (OPT_TRACING_ONELINK_ONLY, "Consider only one link routes to trace platform",
396       "  This option changes the way SimGrid register its platform on the trace file.\n"
397       "  Normally, the tracing considers all routes (no matter their size) on the\n"
398       "  platform file to re-create the resource topology. If this option is activated,\n"
399       "  only the routes with one link are used to register the topology within an AS.\n"
400       "  Routes among AS continue to be traced as usual.", detailed);
401   print_line (OPT_TRACING_DISABLE_DESTROY, "Disable platform containers destruction",
402       "  Disable the destruction of containers at the end of simulation. This can be\n"
403       "  used with simulators that have a different notion of time (different from\n"
404       "  the simulated time).", detailed);
405   print_line (OPT_TRACING_BASIC, "Avoid extended events (impoverished trace file).",
406       "  Some visualization tools are not able to parse correctly the Paje file format.\n"
407       "  Use this option if you are using one of these tools to visualize the simulation\n"
408       "  trace. Keep in mind that the trace might be incomplete, without all the\n"
409       "  information that would be registered otherwise.", detailed);
410   print_line (OPT_TRACING_DISPLAY_SIZES, "Only works for SMPI now. Add message size information",
411       "  Message size (in bytes) is added to links, and to states. For collectives,\n"
412       "  the displayed value is the more relevant to the collective (total sent by\n"
413       "  the process, usually)", detailed);
414   print_line (OPT_TRACING_FORMAT, "Only works for SMPI now. Switch output format",
415       "  Default format is Paje. Time independent traces are also supported,\n"
416       "  to output traces that can later be used by the trace replay tool", detailed);
417   print_line (OPT_TRACING_FORMAT_TI_ONEFILE, "Only works for SMPI now, and TI output format",
418       "  By default, each process outputs to a separate file, inside a filename_files folder\n"
419       "  By setting this option to yes, all processes will output to only one file\n"
420       "  This is meant to avoid opening thousands of files with large simulations", detailed);
421   print_line (OPT_TRACING_COMMENT, "Comment to be added on the top of the trace file.",
422       "  Use this to add a comment line to the top of the trace file.", detailed);
423   print_line (OPT_TRACING_COMMENT_FILE, "File contents added to trace file as comment.",
424       "  Use this to add the contents of a file to the top of the trace file as comment.", detailed);
425   print_line (OPT_TRACING_TOPOLOGY, "Register the platform topology as a graph",
426         "  This option (enabled by default) can be used to disable the tracing of\n"
427         "  the platform topology in the trace file. Sometimes, such task is really\n"
428         "  time consuming, since it must get the route from each host to other hosts\n"
429         "  within the same Autonomous System (AS).", detailed);
430 }
431
432 static void output_types (const char *name, xbt_dynar_t types, FILE *file)
433 {
434   unsigned int i;
435   fprintf (file, "  %s = (", name);
436   for (i = xbt_dynar_length(types); i > 0; i--) {
437     char *type = *(static_cast<char**>(xbt_dynar_get_ptr(types, i - 1)));
438     fprintf (file, "\"%s\"", type);
439     if (i - 1 > 0){
440       fprintf (file, ",");
441     }else{
442       fprintf (file, ");\n");
443     }
444   }
445   xbt_dynar_free (&types);
446 }
447
448 static int previous_trace_state = -1;
449
450 void instr_pause_tracing ()
451 {
452   previous_trace_state = trace_enabled;
453   if (not TRACE_is_enabled()) {
454     XBT_DEBUG ("Tracing is already paused, therefore do nothing.");
455   }else{
456     XBT_DEBUG ("Tracing is being paused.");
457   }
458   trace_enabled = false;
459   XBT_DEBUG ("Tracing is paused.");
460 }
461
462 void instr_resume_tracing ()
463 {
464   if (TRACE_is_enabled()){
465     XBT_DEBUG ("Tracing is already running while trying to resume, therefore do nothing.");
466   }else{
467     XBT_DEBUG ("Tracing is being resumed.");
468   }
469
470   if (previous_trace_state != -1){
471     trace_enabled = previous_trace_state;
472   }else{
473     trace_enabled = true;
474   }
475   XBT_DEBUG ("Tracing is resumed.");
476   previous_trace_state = -1;
477 }