Logo AND Algorithmique Numérique Distribuée

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