Logo AND Algorithmique Numérique Distribuée

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