Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
This instrumentation module is a mess ...
[simgrid.git] / src / instr / instr_config.cpp
1 /* Copyright (c) 2010-2020. 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 "simgrid/Exception.hpp"
8 #include "simgrid/s4u/Engine.hpp"
9 #include "simgrid/version.h"
10 #include "src/instr/instr_private.hpp"
11 #include "surf/surf.hpp"
12 #include "xbt/virtu.h" /* xbt::cmdline */
13
14 #include <fstream>
15 #include <string>
16 #include <vector>
17
18 XBT_LOG_NEW_CATEGORY(instr, "Logging the behavior of the tracing system (used for Visualization/Analysis of simulations)");
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_config, instr, "Configuration");
20
21 std::ofstream tracing_file;
22
23 constexpr char OPT_TRACING_BASIC[]             = "tracing/basic";
24 constexpr char OPT_TRACING_COMMENT_FILE[]      = "tracing/comment-file";
25 constexpr char OPT_TRACING_DISABLE_DESTROY[]   = "tracing/disable-destroy";
26 constexpr char OPT_TRACING_FORMAT_TI_ONEFILE[] = "tracing/smpi/format/ti-one-file";
27 constexpr char OPT_TRACING_SMPI[]              = "tracing/smpi";
28 constexpr char OPT_TRACING_TOPOLOGY[]          = "tracing/platform/topology";
29
30 static simgrid::config::Flag<bool> trace_enabled{
31     "tracing", "Enable the tracing system. You have to enable this option to use other tracing options.", false};
32
33 static simgrid::config::Flag<bool> trace_actor_enabled{
34     "tracing/msg/process", // FIXME rename this flag
35     "Trace the behavior of all categorized actors, grouping them by host. "
36     "Can be used to track actor location if the simulator does actor migration.",
37     false};
38
39 static simgrid::config::Flag<bool> trace_vm_enabled{"tracing/vm", "Trace the behavior of all virtual machines.", false};
40
41 static simgrid::config::Flag<bool> trace_platform{"tracing/platform",
42                                                   "Register the platform in the trace as a hierarchy.", false};
43
44 static simgrid::config::Flag<bool> trace_platform_topology{
45     OPT_TRACING_TOPOLOGY, "Register the platform topology in the trace as a graph.", true};
46 static simgrid::config::Flag<bool> trace_smpi_enabled{OPT_TRACING_SMPI, "Tracing of the SMPI interface.", false};
47 static simgrid::config::Flag<bool> trace_smpi_grouped{"tracing/smpi/group", "Group MPI processes by host.", false};
48
49 static simgrid::config::Flag<bool> trace_smpi_computing{
50     "tracing/smpi/computing", "Generate 'Computing' states to trace the out-of-SMPI parts of the application", false};
51
52 static simgrid::config::Flag<bool> trace_smpi_sleeping{
53     "tracing/smpi/sleeping", "Generate 'Sleeping' states for the sleeps in the application that do not pertain to SMPI",
54     false};
55
56 static simgrid::config::Flag<bool> trace_view_internals{
57     "tracing/smpi/internals",
58     "Generate tracing events corresponding to point-to-point messages sent by SMPI collective communications", false};
59
60 static simgrid::config::Flag<bool> trace_categorized{
61     "tracing/categorized", "Trace categorized resource utilization of hosts and links.", false};
62
63 static simgrid::config::Flag<bool> trace_uncategorized{
64     "tracing/uncategorized",
65     "Trace uncategorized resource utilization of hosts and links. "
66     "To use if the simulator does not use tracing categories but resource utilization have to be traced.",
67     false};
68
69 static simgrid::config::Flag<bool> trace_disable_destroy{
70     OPT_TRACING_DISABLE_DESTROY, {"tracing/disable_destroy"}, "Disable platform containers destruction.", false};
71 static simgrid::config::Flag<bool> trace_basic{OPT_TRACING_BASIC, "Avoid extended events (impoverished trace file).",
72                                                false};
73
74 static simgrid::config::Flag<bool> trace_display_sizes{
75     "tracing/smpi/display-sizes",
76     "Add message size information (in bytes) to the to links and states (SMPI only). "
77     "For collectives, it usually corresponds to the total number of bytes sent by a process.",
78     false};
79
80 static simgrid::config::Flag<bool> trace_disable_link{"tracing/disable_link",
81                                                       "Do not trace link bandwidth and latency.", false};
82 static simgrid::config::Flag<bool> trace_disable_power{"tracing/disable_power", "Do not trace host power.", false};
83
84 simgrid::instr::TraceFormat simgrid::instr::trace_format = simgrid::instr::TraceFormat::Paje;
85
86 bool TRACE_needs_platform ()
87 {
88   return TRACE_actor_is_enabled() || TRACE_vm_is_enabled() || TRACE_categorized() || TRACE_uncategorized() ||
89          TRACE_platform() || (TRACE_smpi_is_enabled() && TRACE_smpi_is_grouped());
90 }
91
92 bool TRACE_is_enabled()
93 {
94   return trace_enabled;
95 }
96
97 bool TRACE_platform()
98 {
99   return trace_platform;
100 }
101
102 bool TRACE_platform_topology()
103 {
104   return trace_platform_topology;
105 }
106
107 bool TRACE_smpi_is_enabled()
108 {
109   return (trace_smpi_enabled || TRACE_smpi_is_grouped()) && TRACE_is_enabled();
110 }
111
112 bool TRACE_smpi_is_grouped()
113 {
114   return trace_smpi_grouped;
115 }
116
117 bool TRACE_smpi_is_computing()
118 {
119   return trace_smpi_computing;
120 }
121
122 bool TRACE_smpi_is_sleeping()
123 {
124   return trace_smpi_sleeping;
125 }
126
127 bool TRACE_smpi_view_internals()
128 {
129   return trace_view_internals;
130 }
131
132 bool TRACE_categorized ()
133 {
134   return trace_categorized;
135 }
136
137 bool TRACE_uncategorized ()
138 {
139   return trace_uncategorized;
140 }
141
142 bool TRACE_actor_is_enabled()
143 {
144   return trace_actor_enabled && trace_enabled;
145 }
146
147 bool TRACE_vm_is_enabled()
148 {
149   return trace_vm_enabled && trace_enabled;
150 }
151
152 bool TRACE_disable_link()
153 {
154   return trace_disable_link && trace_enabled;
155 }
156
157 bool TRACE_disable_speed()
158 {
159   return trace_disable_power && trace_enabled;
160 }
161
162 bool TRACE_disable_destroy ()
163 {
164   return trace_disable_destroy && trace_enabled;
165 }
166
167 bool TRACE_basic ()
168 {
169   return trace_basic && trace_enabled;
170 }
171
172 bool TRACE_display_sizes ()
173 {
174   return trace_display_sizes && trace_smpi_enabled && trace_enabled;
175 }
176
177 int TRACE_precision ()
178 {
179   return simgrid::config::get_value<int>("tracing/precision");
180 }
181
182 std::string TRACE_get_filename()
183 {
184   return simgrid::config::get_value<std::string>("tracing/filename");
185 }
186
187 static void print_line(const char* option, const char* desc, const char* longdesc)
188 {
189   std::string str = std::string("--cfg=") + option + " ";
190
191   int len = str.size();
192   XBT_HELP("%s%*.*s %s", str.c_str(), 30 - len, 30 - len, "", desc);
193   if (longdesc != nullptr) {
194     XBT_HELP("%s\n", longdesc);
195   }
196 }
197
198 void TRACE_help()
199 {
200   XBT_HELP("Description of the tracing options accepted by this simulator:\n");
201   print_line(OPT_TRACING_SMPI, "Trace the MPI Interface (SMPI)",
202              "  This option only has effect if this simulator is SMPI-based. Traces the MPI\n"
203              "  interface and generates a trace that can be analyzed using Gantt-like\n"
204              "  visualizations. Every MPI function (implemented by SMPI) is transformed in a\n"
205              "  state, and point-to-point communications can be analyzed with arrows.");
206   print_line(OPT_TRACING_DISABLE_DESTROY, "Disable platform containers destruction",
207              "  Disable the destruction of containers at the end of simulation. This can be\n"
208              "  used with simulators that have a different notion of time (different from\n"
209              "  the simulated time).");
210   print_line(OPT_TRACING_BASIC, "Avoid extended events (impoverished trace file).",
211              "  Some visualization tools are not able to parse correctly the Paje file format.\n"
212              "  Use this option if you are using one of these tools to visualize the simulation\n"
213              "  trace. Keep in mind that the trace might be incomplete, without all the\n"
214              "  information that would be registered otherwise.");
215   print_line(OPT_TRACING_FORMAT_TI_ONEFILE, "Only works for SMPI now, and TI output format",
216              "  By default, each process outputs to a separate file, inside a filename_files folder\n"
217              "  By setting this option to yes, all processes will output to only one file\n"
218              "  This is meant to avoid opening thousands of files with large simulations");
219   print_line(OPT_TRACING_TOPOLOGY, "Register the platform topology as a graph",
220              "  This option (enabled by default) can be used to disable the tracing of\n"
221              "  the platform topology in the trace file. Sometimes, such task is really\n"
222              "  time consuming, since it must get the route from each host to other hosts\n"
223              "  within the same Autonomous System (AS).");
224 }
225
226 namespace simgrid {
227 namespace instr {
228
229 static bool trace_active = false;
230
231 static void on_simulation_start()
232 {
233   if (trace_active)
234     return;
235
236   // tracing system must be:
237   //    - enabled (with --cfg=tracing:yes)
238   //    - already configured (simgrid::instr::init already called)
239   if (TRACE_is_enabled()) {
240     define_callbacks();
241
242     XBT_DEBUG("Tracing starts");
243
244     /* init the tracing module to generate the right output */
245     std::string format = config::get_value<std::string>("tracing/smpi/format");
246     XBT_DEBUG("Tracing format %s", format.c_str());
247
248     /* open the trace file(s) */
249     std::string filename = TRACE_get_filename();
250     tracing_file.open(filename.c_str(), std::ofstream::out);
251     if (tracing_file.fail()) {
252       throw TracingError(XBT_THROW_POINT,
253                          xbt::string_printf("Tracefile %s could not be opened for writing.", filename.c_str()));
254     }
255
256     XBT_DEBUG("Filename %s is open for writing", filename.c_str());
257
258     if (format == "Paje") {
259       /* output generator version */
260       tracing_file << "#This file was generated using SimGrid-" << SIMGRID_VERSION_MAJOR << "." << SIMGRID_VERSION_MINOR
261                    << "." << SIMGRID_VERSION_PATCH << std::endl;
262       tracing_file << "#[";
263       for (auto str : simgrid::xbt::cmdline) {
264         tracing_file << str << " ";
265       }
266       tracing_file << "]" << std::endl;
267     }
268
269     /* output one line comment */
270     std::string comment = simgrid::config::get_value<std::string>("tracing/comment");
271     if (not comment.empty())
272       tracing_file << "# " << comment << std::endl;
273
274     /* output comment file */
275     dump_comment_file(simgrid::config::get_value<std::string>(OPT_TRACING_COMMENT_FILE));
276
277     if (format == "Paje") {
278       /* output Pajé header */
279       TRACE_header(TRACE_basic(), TRACE_display_sizes());
280     } else
281       trace_format = TraceFormat::Ti;
282
283     trace_active = true;
284     XBT_DEBUG("Tracing is on");
285   }
286 }
287
288 static void on_simulation_end()
289 {
290   if (not trace_active)
291     return;
292
293   /* dump trace buffer */
294   TRACE_last_timestamp_to_dump = surf_get_clock();
295   TRACE_paje_dump_buffer(true);
296
297   const Type* root_type = Container::get_root()->type_;
298   /* destroy all data structures of tracing (and free) */
299   delete Container::get_root();
300   delete root_type;
301
302   /* close the trace files */
303   tracing_file.close();
304   XBT_DEBUG("Filename %s is closed", TRACE_get_filename().c_str());
305
306   /* de-activate trace */
307   trace_active = false;
308   XBT_DEBUG("Tracing is off");
309   XBT_DEBUG("Tracing system is shutdown");
310 }
311
312 void init()
313 {
314   static bool is_initialized = false;
315   if (is_initialized)
316     return;
317
318   is_initialized = true;
319
320   /* name of the tracefile */
321   config::declare_flag<std::string>("tracing/filename", "Trace file created by the instrumented SimGrid.",
322                                     "simgrid.trace");
323   config::declare_flag<std::string>("tracing/smpi/format",
324                                     "Select trace output format used by SMPI. The default is the 'Paje' format. "
325                                     "The 'TI' (Time-Independent) format allows for trace replay.",
326                                     "Paje");
327
328   config::declare_flag<bool>(OPT_TRACING_FORMAT_TI_ONEFILE,
329                              "(smpi only) For replay format only : output to one file only", false);
330   config::alias(OPT_TRACING_FORMAT_TI_ONEFILE, {"tracing/smpi/format/ti_one_file"});
331   config::declare_flag<std::string>("tracing/comment", "Add a comment line to the top of the trace file.", "");
332   config::declare_flag<std::string>(OPT_TRACING_COMMENT_FILE,
333                                     "Add the contents of a file as comments to the top of the trace.", "");
334   config::alias(OPT_TRACING_COMMENT_FILE, {"tracing/comment_file"});
335   config::declare_flag<int>("tracing/precision",
336                             "Numerical precision used when timestamping events "
337                             "(expressed in number of digits after decimal point)",
338                             6);
339
340   /* Connect callbacks */
341   s4u::Engine::on_platform_creation.connect(on_simulation_start);
342   s4u::Engine::on_deadlock.connect(on_simulation_end);
343   s4u::Engine::on_simulation_end.connect(on_simulation_end);
344 }
345 } // namespace instr
346 } // namespace simgrid