Logo AND Algorithmique Numérique Distribuée

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