Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Stop trying to build on native WIN32, it's broken anyway
[simgrid.git] / src / instr / instr_config.cpp
1 /* Copyright (c) 2010-2023. 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 <simgrid/Exception.hpp>
7 #include <simgrid/s4u/Engine.hpp>
8
9 #include "src/instr/instr_private.hpp"
10 #include "xbt/config.hpp"
11 #include "xbt/xbt_os_time.h"
12
13 #include <sys/stat.h>
14
15 #include <fstream>
16 #include <string>
17 #include <vector>
18
19 XBT_LOG_NEW_CATEGORY(instr, "Logging the behavior of the tracing system (used for Visualization/Analysis of simulations)");
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY (instr_config, instr, "Configuration");
21
22 std::ofstream tracing_file;
23 std::map<const simgrid::instr::Container*, std::ofstream*> tracing_files; // TI specific
24
25 constexpr char OPT_TRACING_BASIC[]             = "tracing/basic";
26 constexpr char OPT_TRACING_COMMENT_FILE[]      = "tracing/comment-file";
27 constexpr char OPT_TRACING_DISABLE_DESTROY[]   = "tracing/disable-destroy";
28 constexpr char OPT_TRACING_FORMAT_TI_ONEFILE[] = "tracing/smpi/format/ti-one-file";
29 constexpr char OPT_TRACING_SMPI[]              = "tracing/smpi";
30 constexpr char OPT_TRACING_TOPOLOGY[]          = "tracing/platform/topology";
31
32 static simgrid::config::Flag<bool> trace_enabled{
33     "tracing", "Enable the tracing system. You have to enable this option to use other tracing options.", false};
34
35 static simgrid::config::Flag<bool> trace_actor_enabled{
36     "tracing/actor",
37     "Trace the behavior of all categorized actors, grouping them by host. "
38     "Can be used to track actor location if the simulator does actor migration.",
39     false};
40
41 static simgrid::config::Flag<bool> trace_vm_enabled{"tracing/vm", "Trace the behavior of all virtual machines.", false};
42
43 static simgrid::config::Flag<bool> trace_platform{"tracing/platform",
44                                                   "Register the platform in the trace as a hierarchy.", false};
45
46 static simgrid::config::Flag<bool> trace_platform_topology{
47     OPT_TRACING_TOPOLOGY, "Register the platform topology in the trace as a graph.", true};
48 static simgrid::config::Flag<bool> trace_smpi_enabled{OPT_TRACING_SMPI, "Tracing of the SMPI interface.", false};
49 static simgrid::config::Flag<bool> trace_smpi_grouped{"tracing/smpi/group", "Group MPI processes by host.", false};
50
51 static simgrid::config::Flag<bool> trace_smpi_computing{
52     "tracing/smpi/computing", "Generate 'Computing' states to trace the out-of-SMPI parts of the application", false};
53
54 static simgrid::config::Flag<bool> trace_smpi_sleeping{
55     "tracing/smpi/sleeping", "Generate 'Sleeping' states for the sleeps in the application that do not pertain to SMPI",
56     false};
57
58 static simgrid::config::Flag<bool> trace_view_internals{
59     "tracing/smpi/internals",
60     "Generate tracing events corresponding to point-to-point messages sent by SMPI collective communications", false};
61
62 static simgrid::config::Flag<bool> trace_categorized{
63     "tracing/categorized", "Trace categorized resource utilization of hosts and links.", false};
64
65 static simgrid::config::Flag<bool> trace_uncategorized{
66     "tracing/uncategorized",
67     "Trace uncategorized resource utilization of hosts and links. "
68     "To use if the simulator does not use tracing categories but resource utilization have to be traced.",
69     false};
70
71 static simgrid::config::Flag<bool> trace_disable_destroy{OPT_TRACING_DISABLE_DESTROY,
72                                                          "Disable platform containers destruction.", false};
73 static simgrid::config::Flag<bool> trace_basic{OPT_TRACING_BASIC, "Avoid extended events (impoverished trace file).",
74                                                false};
75
76 static simgrid::config::Flag<bool> trace_display_sizes{
77     "tracing/smpi/display-sizes",
78     "Add message size information (in bytes) to the to links and states (SMPI only). "
79     "For collectives, it usually corresponds to the total number of bytes sent by a process.",
80     false};
81
82 static simgrid::config::Flag<bool> trace_disable_link{"tracing/disable_link",
83                                                       "Do not trace link bandwidth and latency.", false};
84 static simgrid::config::Flag<bool> trace_disable_power{"tracing/disable_power", "Do not trace host power.", false};
85
86 bool TRACE_needs_platform ()
87 {
88   return TRACE_actor_is_enabled() || TRACE_vm_is_enabled() || TRACE_categorized() || TRACE_uncategorized() ||
89          TRACE_platform() || (TRACE_smpi_is_enabled() && TRACE_smpi_is_grouped());
90 }
91
92 bool TRACE_is_enabled()
93 {
94   return trace_enabled;
95 }
96
97 bool TRACE_platform()
98 {
99   return trace_platform;
100 }
101
102 bool TRACE_platform_topology()
103 {
104   return trace_platform_topology;
105 }
106
107 bool TRACE_smpi_is_enabled()
108 {
109   return (trace_smpi_enabled || TRACE_smpi_is_grouped()) && TRACE_is_enabled();
110 }
111
112 bool TRACE_smpi_is_grouped()
113 {
114   return trace_smpi_grouped;
115 }
116
117 bool TRACE_smpi_is_computing()
118 {
119   return trace_smpi_computing;
120 }
121
122 bool TRACE_smpi_is_sleeping()
123 {
124   return trace_smpi_sleeping;
125 }
126
127 bool TRACE_smpi_view_internals()
128 {
129   return trace_view_internals;
130 }
131
132 bool TRACE_categorized ()
133 {
134   return trace_categorized;
135 }
136
137 bool TRACE_uncategorized ()
138 {
139   return trace_uncategorized;
140 }
141
142 bool TRACE_actor_is_enabled()
143 {
144   return trace_actor_enabled && trace_enabled;
145 }
146
147 bool TRACE_vm_is_enabled()
148 {
149   return trace_vm_enabled && trace_enabled;
150 }
151
152 bool TRACE_disable_link()
153 {
154   return trace_disable_link && trace_enabled;
155 }
156
157 bool TRACE_disable_speed()
158 {
159   return trace_disable_power && trace_enabled;
160 }
161
162 bool TRACE_display_sizes ()
163 {
164   return trace_display_sizes && trace_smpi_enabled && trace_enabled;
165 }
166
167 static void print_line(const char* option, const char* desc, const char* longdesc)
168 {
169   std::string str = std::string("--cfg=") + option + " ";
170
171   int len = static_cast<int>(str.size());
172   XBT_HELP("%s%*.*s %s", str.c_str(), 30 - len, 30 - len, "", desc);
173   if (longdesc != nullptr) {
174     XBT_HELP("%s\n", longdesc);
175   }
176 }
177
178 void TRACE_help()
179 {
180   XBT_HELP("Description of the tracing options accepted by this simulator:\n");
181   print_line(OPT_TRACING_SMPI, "Trace the MPI Interface (SMPI)",
182              "  This option only has effect if this simulator is SMPI-based. Traces the MPI\n"
183              "  interface and generates a trace that can be analyzed using Gantt-like\n"
184              "  visualizations. Every MPI function (implemented by SMPI) is transformed in a\n"
185              "  state, and point-to-point communications can be analyzed with arrows.");
186   print_line(OPT_TRACING_DISABLE_DESTROY, "Disable platform containers destruction",
187              "  Disable the destruction of containers at the end of simulation. This can be\n"
188              "  used with simulators that have a different notion of time (different from\n"
189              "  the simulated time).");
190   print_line(OPT_TRACING_BASIC, "Avoid extended events (impoverished trace file).",
191              "  Some visualization tools are not able to parse correctly the Paje file format.\n"
192              "  Use this option if you are using one of these tools to visualize the simulation\n"
193              "  trace. Keep in mind that the trace might be incomplete, without all the\n"
194              "  information that would be registered otherwise.");
195   print_line(OPT_TRACING_FORMAT_TI_ONEFILE, "Only works for SMPI now, and TI output format",
196              "  By default, each process outputs to a separate file, inside a filename_files folder\n"
197              "  By setting this option to yes, all processes will output to only one file\n"
198              "  This is meant to avoid opening thousands of files with large simulations");
199   print_line(OPT_TRACING_TOPOLOGY, "Register the platform topology as a graph",
200              "  This option (enabled by default) can be used to disable the tracing of\n"
201              "  the platform topology in the trace file. Sometimes, such task is really\n"
202              "  time consuming, since it must get the route from each host to other hosts\n"
203              "  within the same Autonomous System (AS).");
204 }
205
206 namespace simgrid::instr {
207 static bool trace_active = false;
208 TraceFormat trace_format = TraceFormat::Paje;
209 int trace_precision;
210
211 /*************
212  * Callbacks *
213  *************/
214 xbt::signal<void(Container const&)> Container::on_creation;
215 xbt::signal<void(Container const&)> Container::on_destruction;
216 xbt::signal<void(Type const&, PajeEventType)> Type::on_creation;
217 xbt::signal<void(LinkType const&, Type const&, Type const&)> LinkType::on_creation;
218 xbt::signal<void(PajeEvent&)> PajeEvent::on_creation;
219 xbt::signal<void(PajeEvent const&)> PajeEvent::on_destruction;
220 xbt::signal<void(StateEvent const&)> StateEvent::on_destruction;
221 xbt::signal<void(EntityValue const&)> EntityValue::on_creation;
222
223 static void on_container_creation_paje(const Container& c)
224 {
225   double timestamp = simgrid_get_clock();
226   std::stringstream stream;
227
228   XBT_DEBUG("%s: event_type=%u, timestamp=%f", __func__, static_cast<unsigned>(PajeEventType::CreateContainer),
229             timestamp);
230
231   stream << std::fixed << std::setprecision(trace_precision) << PajeEventType::CreateContainer << " ";
232   stream << timestamp << " " << c.get_id() << " " << c.get_type()->get_id() << " " << c.get_parent()->get_id() << " \"";
233   if (c.get_name().find("rank-") != 0)
234     stream << c.get_name() << "\"";
235   else
236     /* Subtract -1 because this is the process id and we transform it to the rank id */
237     stream << "rank-" << stoi(c.get_name().substr(5)) - 1 << "\"";
238
239   XBT_DEBUG("Dump %s", stream.str().c_str());
240   tracing_file << stream.str() << '\n';
241 }
242
243 static void on_container_destruction_paje(const Container& c)
244 {
245   // trace my destruction, but not if user requests so or if the container is root
246   if (not trace_disable_destroy && &c != Container::get_root()) {
247     std::stringstream stream;
248     double timestamp = simgrid_get_clock();
249
250     XBT_DEBUG("%s: event_type=%u, timestamp=%f", __func__, static_cast<unsigned>(PajeEventType::DestroyContainer),
251               timestamp);
252
253     stream << std::fixed << std::setprecision(trace_precision) << PajeEventType::DestroyContainer << " ";
254     stream << timestamp << " " << c.get_type()->get_id() << " " << c.get_id();
255     XBT_DEBUG("Dump %s", stream.str().c_str());
256     tracing_file << stream.str() << '\n';
257   }
258 }
259
260 static void on_container_creation_ti(const Container& c)
261 {
262   XBT_DEBUG("%s: event_type=%u, timestamp=%f", __func__, static_cast<unsigned>(PajeEventType::CreateContainer),
263             simgrid_get_clock());
264   // if we are in the mode with only one file
265   static std::ofstream* ti_unique_file = nullptr;
266   static double prefix                 = 0.0;
267
268   if (tracing_files.empty()) {
269     // generate unique run id with time
270     prefix = xbt_os_time();
271   }
272
273   if (not simgrid::config::get_value<bool>("tracing/smpi/format/ti-one-file") || ti_unique_file == nullptr) {
274     std::string folder_name = simgrid::config::get_value<std::string>("tracing/filename") + "_files";
275     std::string filename    = folder_name + "/" + std::to_string(prefix) + "_" + c.get_name() + ".txt";
276     mkdir(folder_name.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
277     ti_unique_file = new std::ofstream(filename.c_str(), std::ofstream::out);
278     xbt_assert(not ti_unique_file->fail(), "Tracefile %s could not be opened for writing", filename.c_str());
279     tracing_file << filename << '\n';
280   }
281   tracing_files.insert({&c, ti_unique_file});
282 }
283
284 static void on_container_destruction_ti(const Container& c)
285 {
286   if (not trace_disable_destroy && &c != Container::get_root()) {
287     if (not simgrid::config::get_value<bool>("tracing/smpi/format/ti-one-file") || tracing_files.size() == 1) {
288       tracing_files.at(&c)->close();
289       delete tracing_files.at(&c);
290     }
291     tracing_files.erase(&c);
292   }
293 }
294
295 static void on_entity_value_creation(const EntityValue& value)
296 {
297   std::stringstream stream;
298   XBT_DEBUG("%s: event_type=%u", __func__, static_cast<unsigned>(PajeEventType::DefineEntityValue));
299   stream << std::fixed << std::setprecision(trace_precision) << PajeEventType::DefineEntityValue;
300   stream << " " << value.get_id() << " " << value.get_parent()->get_id() << " " << value.get_name();
301   if (not value.get_color().empty())
302     stream << " \"" << value.get_color() << "\"";
303   XBT_DEBUG("Dump %s", stream.str().c_str());
304   tracing_file << stream.str() << '\n';
305 }
306
307 static void on_event_creation(PajeEvent& event)
308 {
309   XBT_DEBUG("%s: event_type=%u, timestamp=%.*f", __func__, static_cast<unsigned>(event.eventType_), trace_precision,
310             event.timestamp_);
311   event.stream_ << std::fixed << std::setprecision(trace_precision);
312   event.stream_ << event.eventType_ << " " << event.timestamp_ << " ";
313   event.stream_ << event.get_type()->get_id() << " " << event.get_container()->get_id();
314 }
315
316 static void on_event_destruction(const PajeEvent& event)
317 {
318   XBT_DEBUG("Dump %s", event.stream_.str().c_str());
319   tracing_file << event.stream_.str() << '\n';
320 }
321
322 static void on_state_event_destruction(const StateEvent& event)
323 {
324   if (event.has_extra())
325     *tracing_files.at(event.get_container()) << event.stream_.str() << '\n';
326 }
327
328 static void on_type_creation(const Type& type, PajeEventType event_type)
329 {
330   if (event_type == PajeEventType::DefineLinkType)
331     return; // this kind of type has to be handled differently
332
333   std::stringstream stream;
334   stream << std::fixed << std::setprecision(trace_precision);
335   XBT_DEBUG("%s: event_type=%u, timestamp=%.*f", __func__, static_cast<unsigned>(event_type), trace_precision, 0.);
336   stream << event_type << " " << type.get_id() << " " << type.get_parent()->get_id() << " " << type.get_name();
337   if (type.is_colored())
338     stream << " \"" << type.get_color() << "\"";
339   XBT_DEBUG("Dump %s", stream.str().c_str());
340   tracing_file << stream.str() << '\n';
341 }
342
343 static void on_link_type_creation(const Type& type, const Type& source, const Type& dest)
344 {
345   std::stringstream stream;
346   XBT_DEBUG("%s: event_type=%u, timestamp=%.*f", __func__, static_cast<unsigned>(PajeEventType::DefineLinkType),
347             trace_precision, 0.);
348   stream << PajeEventType::DefineLinkType << " " << type.get_id() << " " << type.get_parent()->get_id();
349   stream << " " << source.get_id() << " " << dest.get_id() << " " << type.get_name();
350   XBT_DEBUG("Dump %s", stream.str().c_str());
351   tracing_file << stream.str() << '\n';
352 }
353
354 static void on_simulation_start()
355 {
356   if (trace_active || not TRACE_is_enabled())
357     return;
358
359   define_callbacks();
360
361   XBT_DEBUG("Tracing starts");
362   trace_precision = config::get_value<int>("tracing/precision");
363
364   /* init the tracing module to generate the right output */
365   std::string format = config::get_value<std::string>("tracing/smpi/format");
366   XBT_DEBUG("Tracing format %s", format.c_str());
367
368   /* open the trace file(s) */
369   std::string filename = simgrid::config::get_value<std::string>("tracing/filename");
370   tracing_file.open(filename.c_str(), std::ofstream::out);
371   if (tracing_file.fail()) {
372     throw TracingError(XBT_THROW_POINT,
373                        xbt::string_printf("Tracefile %s could not be opened for writing.", filename.c_str()));
374   }
375
376   XBT_DEBUG("Filename %s is open for writing", filename.c_str());
377
378   if (format == "Paje") {
379     Container::on_creation_cb(on_container_creation_paje);
380     Container::on_destruction_cb(on_container_destruction_paje);
381     EntityValue::on_creation_cb(on_entity_value_creation);
382     Type::on_creation_cb(on_type_creation);
383     LinkType::on_creation_cb(on_link_type_creation);
384     PajeEvent::on_creation_cb(on_event_creation);
385     PajeEvent::on_destruction_cb(on_event_destruction);
386
387     paje::dump_generator_version();
388
389     /* output one line comment */
390     if (auto comment = simgrid::config::get_value<std::string>("tracing/comment"); not comment.empty())
391       tracing_file << "# " << comment << '\n';
392
393     /* output comment file */
394     paje::dump_comment_file(config::get_value<std::string>(OPT_TRACING_COMMENT_FILE));
395     paje::dump_header(trace_basic, TRACE_display_sizes());
396   } else {
397     trace_format = TraceFormat::Ti;
398     Container::on_creation_cb(on_container_creation_ti);
399     Container::on_destruction_cb(on_container_destruction_ti);
400     StateEvent::on_destruction_cb(on_state_event_destruction);
401   }
402
403   trace_active = true;
404   XBT_DEBUG("Tracing is on");
405 }
406
407 static void on_simulation_end()
408 {
409   if (not trace_active)
410     return;
411
412   /* dump trace buffer */
413   last_timestamp_to_dump = simgrid_get_clock();
414   dump_buffer(true);
415
416   const Type* root_type = Container::get_root()->get_type();
417   /* destroy all data structures of tracing (and free) */
418   delete Container::get_root();
419   delete root_type;
420
421   /* close the trace files */
422   tracing_file.close();
423   XBT_DEBUG("Filename %s is closed", config::get_value<std::string>("tracing/filename").c_str());
424
425   /* de-activate trace */
426   trace_active = false;
427   XBT_DEBUG("Tracing is off");
428   XBT_DEBUG("Tracing system is shutdown");
429 }
430
431 void init()
432 {
433   static bool is_initialized = false;
434   if (is_initialized)
435     return;
436
437   is_initialized = true;
438
439   /* name of the tracefile */
440   config::declare_flag<std::string>("tracing/filename", "Trace file created by the instrumented SimGrid.",
441                                     "simgrid.trace");
442   config::declare_flag<std::string>("tracing/smpi/format",
443                                     "Select trace output format used by SMPI. The default is the 'Paje' format. "
444                                     "The 'TI' (Time-Independent) format allows for trace replay.",
445                                     "Paje");
446
447   config::declare_flag<bool>(OPT_TRACING_FORMAT_TI_ONEFILE,
448                              "(smpi only) For replay format only : output to one file only", false);
449   config::declare_flag<std::string>("tracing/comment", "Add a comment line to the top of the trace file.", "");
450   config::declare_flag<std::string>(OPT_TRACING_COMMENT_FILE,
451                                     "Add the contents of a file as comments to the top of the trace.", "");
452   config::declare_flag<int>("tracing/precision",
453                             "Numerical precision used when timestamping events "
454                             "(expressed in number of digits after decimal point)",
455                             6);
456
457   /* Connect Engine callbacks */
458   s4u::Engine::on_platform_creation_cb(on_simulation_start);
459   s4u::Engine::on_time_advance_cb([](double /*time_delta*/) { dump_buffer(false); });
460   s4u::Engine::on_deadlock_cb(on_simulation_end);
461   s4u::Engine::on_simulation_end_cb(on_simulation_end);
462 }
463 } // namespace simgrid::instr