Logo AND Algorithmique Numérique Distribuée

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