Logo AND Algorithmique Numérique Distribuée

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