Logo AND Algorithmique Numérique Distribuée

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