1 /* Copyright (c) 2010-2023. The SimGrid Team. All rights reserved. */
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. */
6 #include <simgrid/Exception.hpp>
7 #include <simgrid/s4u/Engine.hpp>
9 #include "src/instr/instr_private.hpp"
10 #include "xbt/config.hpp"
11 #include "xbt/xbt_os_time.h"
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");
22 std::ofstream tracing_file;
23 static std::map<const simgrid::instr::Container*, std::ofstream*> tracing_files; // TI specific
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";
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};
35 static simgrid::config::Flag<bool> trace_actor_enabled{
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.",
41 static simgrid::config::Flag<bool> trace_vm_enabled{"tracing/vm", "Trace the behavior of all virtual machines.", false};
43 static simgrid::config::Flag<bool> trace_platform{"tracing/platform",
44 "Register the platform in the trace as a hierarchy.", false};
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};
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};
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",
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};
62 static simgrid::config::Flag<bool> trace_categorized{
63 "tracing/categorized", "Trace categorized resource utilization of hosts and links.", false};
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.",
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).",
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.",
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};
86 bool TRACE_needs_platform ()
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());
92 bool TRACE_is_enabled()
99 return trace_platform;
102 bool TRACE_platform_topology()
104 return trace_platform_topology;
107 bool TRACE_smpi_is_enabled()
109 return (trace_smpi_enabled || TRACE_smpi_is_grouped()) && TRACE_is_enabled();
112 bool TRACE_smpi_is_grouped()
114 return trace_smpi_grouped;
117 bool TRACE_smpi_is_computing()
119 return trace_smpi_computing;
122 bool TRACE_smpi_is_sleeping()
124 return trace_smpi_sleeping;
127 bool TRACE_smpi_view_internals()
129 return trace_view_internals;
132 bool TRACE_categorized ()
134 return trace_categorized;
137 bool TRACE_uncategorized ()
139 return trace_uncategorized;
142 bool TRACE_actor_is_enabled()
144 return trace_actor_enabled && trace_enabled;
147 bool TRACE_vm_is_enabled()
149 return trace_vm_enabled && trace_enabled;
152 bool TRACE_disable_link()
154 return trace_disable_link && trace_enabled;
157 bool TRACE_disable_speed()
159 return trace_disable_power && trace_enabled;
162 bool TRACE_display_sizes ()
164 return trace_display_sizes && trace_smpi_enabled && trace_enabled;
167 static void print_line(const char* option, const char* desc, const char* longdesc)
169 std::string str = std::string("--cfg=") + option + " ";
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);
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).");
206 namespace simgrid::instr {
207 static bool trace_active = false;
208 TraceFormat trace_format = TraceFormat::Paje;
209 static int trace_precision;
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;
223 static void on_container_creation_paje(const Container& c)
225 double timestamp = simgrid_get_clock();
226 std::stringstream stream;
228 XBT_DEBUG("%s: event_type=%u, timestamp=%f", __func__, static_cast<unsigned>(PajeEventType::CreateContainer),
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() << "\"";
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 << "\"";
239 XBT_DEBUG("Dump %s", stream.str().c_str());
240 tracing_file << stream.str() << '\n';
243 static void on_container_destruction_paje(const Container& c)
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();
250 XBT_DEBUG("%s: event_type=%u, timestamp=%f", __func__, static_cast<unsigned>(PajeEventType::DestroyContainer),
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';
260 static void on_container_creation_ti(const Container& c)
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;
268 if (tracing_files.empty()) {
269 // generate unique run id with time
270 prefix = xbt_os_time();
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';
281 tracing_files.insert({&c, ti_unique_file});
284 static void on_container_destruction_ti(const Container& c)
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);
291 tracing_files.erase(&c);
295 static void on_entity_value_creation(const EntityValue& value)
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';
307 static void on_event_creation(PajeEvent& event)
309 XBT_DEBUG("%s: event_type=%u, timestamp=%.*f", __func__, static_cast<unsigned>(event.eventType_), trace_precision,
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();
316 static void on_event_destruction(const PajeEvent& event)
318 XBT_DEBUG("Dump %s", event.stream_.str().c_str());
319 tracing_file << event.stream_.str() << '\n';
322 static void on_state_event_destruction(const StateEvent& event)
324 if (event.has_extra())
325 *tracing_files.at(event.get_container()) << event.stream_.str() << '\n';
328 static void on_type_creation(const Type& type, PajeEventType event_type)
330 if (event_type == PajeEventType::DefineLinkType)
331 return; // this kind of type has to be handled differently
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';
343 static void on_link_type_creation(const Type& type, const Type& source, const Type& dest)
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';
354 static void on_simulation_start()
356 if (trace_active || not TRACE_is_enabled())
361 XBT_DEBUG("Tracing starts");
362 trace_precision = config::get_value<int>("tracing/precision");
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());
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()));
376 XBT_DEBUG("Filename %s is open for writing", filename.c_str());
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);
387 paje::dump_generator_version();
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';
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());
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);
404 XBT_DEBUG("Tracing is on");
407 static void on_simulation_end()
409 if (not trace_active)
412 /* dump trace buffer */
413 last_timestamp_to_dump = simgrid_get_clock();
416 const Type* root_type = Container::get_root()->get_type();
417 /* destroy all data structures of tracing (and free) */
418 delete Container::get_root();
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());
425 /* de-activate trace */
426 trace_active = false;
427 XBT_DEBUG("Tracing is off");
428 XBT_DEBUG("Tracing system is shutdown");
433 static bool is_initialized = false;
437 is_initialized = true;
439 /* name of the tracefile */
440 config::declare_flag<std::string>("tracing/filename", "Trace file created by the instrumented SimGrid.",
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.",
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)",
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);
463 } // namespace simgrid::instr