Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
use signal to decouple instr from surf code
[simgrid.git] / src / instr / instr_config.cpp
index 773b0ce..17faca1 100644 (file)
@@ -1,13 +1,15 @@
-/* Copyright (c) 2010-2019. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2010-2020. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
 #include "include/xbt/config.hpp"
+#include "simgrid/Exception.hpp"
 #include "simgrid/s4u/Engine.hpp"
+#include "simgrid/version.h"
 #include "src/instr/instr_private.hpp"
 #include "surf/surf.hpp"
-#include "xbt/virtu.h" /* xbt_cmdline */
+#include "xbt/virtu.h" /* xbt::cmdline */
 
 #include <fstream>
 #include <string>
@@ -79,89 +81,8 @@ static simgrid::config::Flag<bool> trace_disable_link{"tracing/disable_link",
                                                       "Do not trace link bandwidth and latency.", false};
 static simgrid::config::Flag<bool> trace_disable_power{"tracing/disable_power", "Do not trace host power.", false};
 
-static bool trace_active     = false;
-
 simgrid::instr::TraceFormat simgrid::instr::trace_format = simgrid::instr::TraceFormat::Paje;
 
-static void TRACE_start()
-{
-  if (trace_active)
-    return;
-
-  // tracing system must be:
-  //    - enabled (with --cfg=tracing:yes)
-  //    - already configured (TRACE_global_init already called)
-  if (TRACE_is_enabled()) {
-    instr_define_callbacks();
-
-    XBT_DEBUG("Tracing starts");
-    /* init the tracing module to generate the right output */
-    std::string format = simgrid::config::get_value<std::string>("tracing/smpi/format");
-    XBT_DEBUG("Tracing format %s", format.c_str());
-
-    /* open the trace file(s) */
-    std::string filename = TRACE_get_filename();
-    tracing_file.open(filename.c_str(), std::ofstream::out);
-    if (tracing_file.fail()) {
-      THROWF(system_error, 1, "Tracefile %s could not be opened for writing.", filename.c_str());
-    }
-
-    XBT_DEBUG("Filename %s is open for writing", filename.c_str());
-
-    if (format == "Paje") {
-      /* output generator version */
-      tracing_file << "#This file was generated using SimGrid-" << SIMGRID_VERSION_MAJOR << "." << SIMGRID_VERSION_MINOR
-                   << "." << SIMGRID_VERSION_PATCH << std::endl;
-      tracing_file << "#[";
-      unsigned int cpt;
-      char* str;
-      xbt_dynar_foreach (xbt_cmdline, cpt, str) {
-        tracing_file << str << " ";
-      }
-      tracing_file << "]" << std::endl;
-    }
-
-    /* output one line comment */
-    dump_comment(simgrid::config::get_value<std::string>("tracing/comment"));
-
-    /* output comment file */
-    dump_comment_file(simgrid::config::get_value<std::string>(OPT_TRACING_COMMENT_FILE));
-
-    if (format == "Paje") {
-      /* output Pajé header */
-      TRACE_header(TRACE_basic(), TRACE_display_sizes());
-    } else
-      simgrid::instr::trace_format = simgrid::instr::TraceFormat::Ti;
-
-    trace_active = true;
-    XBT_DEBUG("Tracing is on");
-  }
-}
-
-static void TRACE_end()
-{
-  if (not trace_active)
-    return;
-
-  /* dump trace buffer */
-  TRACE_last_timestamp_to_dump = surf_get_clock();
-  TRACE_paje_dump_buffer(true);
-
-  simgrid::instr::Type* root_type = simgrid::instr::Container::get_root()->type_;
-  /* destroy all data structures of tracing (and free) */
-  delete simgrid::instr::Container::get_root();
-  delete root_type;
-
-  /* close the trace files */
-  tracing_file.close();
-  XBT_DEBUG("Filename %s is closed", TRACE_get_filename().c_str());
-
-  /* de-activate trace */
-  trace_active = false;
-  XBT_DEBUG("Tracing is off");
-  XBT_DEBUG("Tracing system is shutdown");
-}
-
 bool TRACE_needs_platform ()
 {
   return TRACE_actor_is_enabled() || TRACE_vm_is_enabled() || TRACE_categorized() || TRACE_uncategorized() ||
@@ -263,39 +184,6 @@ std::string TRACE_get_filename()
   return simgrid::config::get_value<std::string>("tracing/filename");
 }
 
-void TRACE_global_init()
-{
-  static bool is_initialised = false;
-  if (is_initialised)
-    return;
-
-  is_initialised = true;
-
-  /* name of the tracefile */
-  simgrid::config::declare_flag<std::string>("tracing/filename", "Trace file created by the instrumented SimGrid.",
-                                             "simgrid.trace");
-  simgrid::config::declare_flag<std::string>(
-      "tracing/smpi/format", "Select trace output format used by SMPI. The default is the 'Paje' format. "
-                             "The 'TI' (Time-Independent) format allows for trace replay.",
-      "Paje");
-
-  simgrid::config::declare_flag<bool>(OPT_TRACING_FORMAT_TI_ONEFILE,
-                                      "(smpi only) For replay format only : output to one file only", false);
-  simgrid::config::alias(OPT_TRACING_FORMAT_TI_ONEFILE, {"tracing/smpi/format/ti_one_file"});
-  simgrid::config::declare_flag<std::string>("tracing/comment", "Add a comment line to the top of the trace file.", "");
-  simgrid::config::declare_flag<std::string>(OPT_TRACING_COMMENT_FILE,
-                                             "Add the contents of a file as comments to the top of the trace.", "");
-  simgrid::config::alias(OPT_TRACING_COMMENT_FILE, {"tracing/comment_file"});
-  simgrid::config::declare_flag<int>("tracing/precision", "Numerical precision used when timestamping events "
-                                                          "(expressed in number of digits after decimal point)",
-                                     6);
-
-  /* Connect callbacks */
-  simgrid::s4u::on_platform_creation.connect(TRACE_start);
-  simgrid::s4u::on_deadlock.connect(TRACE_end);
-  simgrid::s4u::on_simulation_end.connect(TRACE_end);
-}
-
 static void print_line(const char* option, const char* desc, const char* longdesc)
 {
   std::string str = std::string("--cfg=") + option + " ";
@@ -334,3 +222,125 @@ void TRACE_help()
              "  time consuming, since it must get the route from each host to other hosts\n"
              "  within the same Autonomous System (AS).");
 }
+
+namespace simgrid {
+namespace instr {
+
+static bool trace_active = false;
+
+static void on_simulation_start()
+{
+  if (trace_active)
+    return;
+
+  // tracing system must be:
+  //    - enabled (with --cfg=tracing:yes)
+  //    - already configured (simgrid::instr::init already called)
+  if (TRACE_is_enabled()) {
+    define_callbacks();
+
+    XBT_DEBUG("Tracing starts");
+
+    /* init the tracing module to generate the right output */
+    std::string format = config::get_value<std::string>("tracing/smpi/format");
+    XBT_DEBUG("Tracing format %s", format.c_str());
+
+    /* open the trace file(s) */
+    std::string filename = TRACE_get_filename();
+    tracing_file.open(filename.c_str(), std::ofstream::out);
+    if (tracing_file.fail()) {
+      throw TracingError(XBT_THROW_POINT,
+                         xbt::string_printf("Tracefile %s could not be opened for writing.", filename.c_str()));
+    }
+
+    XBT_DEBUG("Filename %s is open for writing", filename.c_str());
+
+    if (format == "Paje") {
+      /* output generator version */
+      tracing_file << "#This file was generated using SimGrid-" << SIMGRID_VERSION_MAJOR << "." << SIMGRID_VERSION_MINOR
+                   << "." << SIMGRID_VERSION_PATCH << std::endl;
+      tracing_file << "#[";
+      for (auto str : simgrid::xbt::cmdline) {
+        tracing_file << str << " ";
+      }
+      tracing_file << "]" << std::endl;
+    }
+
+    /* output one line comment */
+    std::string comment = simgrid::config::get_value<std::string>("tracing/comment");
+    if (not comment.empty())
+      tracing_file << "# " << comment << std::endl;
+
+    /* output comment file */
+    dump_comment_file(simgrid::config::get_value<std::string>(OPT_TRACING_COMMENT_FILE));
+
+    if (format == "Paje") {
+      /* output Pajé header */
+      TRACE_header(TRACE_basic(), TRACE_display_sizes());
+    } else
+      trace_format = TraceFormat::Ti;
+
+    trace_active = true;
+    XBT_DEBUG("Tracing is on");
+  }
+}
+
+static void on_simulation_end()
+{
+  if (not trace_active)
+    return;
+
+  /* dump trace buffer */
+  TRACE_last_timestamp_to_dump = surf_get_clock();
+  TRACE_paje_dump_buffer(true);
+
+  const Type* root_type = Container::get_root()->type_;
+  /* destroy all data structures of tracing (and free) */
+  delete Container::get_root();
+  delete root_type;
+
+  /* close the trace files */
+  tracing_file.close();
+  XBT_DEBUG("Filename %s is closed", TRACE_get_filename().c_str());
+
+  /* de-activate trace */
+  trace_active = false;
+  XBT_DEBUG("Tracing is off");
+  XBT_DEBUG("Tracing system is shutdown");
+}
+
+void init()
+{
+  static bool is_initialized = false;
+  if (is_initialized)
+    return;
+
+  is_initialized = true;
+
+  /* name of the tracefile */
+  config::declare_flag<std::string>("tracing/filename", "Trace file created by the instrumented SimGrid.",
+                                    "simgrid.trace");
+  config::declare_flag<std::string>("tracing/smpi/format",
+                                    "Select trace output format used by SMPI. The default is the 'Paje' format. "
+                                    "The 'TI' (Time-Independent) format allows for trace replay.",
+                                    "Paje");
+
+  config::declare_flag<bool>(OPT_TRACING_FORMAT_TI_ONEFILE,
+                             "(smpi only) For replay format only : output to one file only", false);
+  config::alias(OPT_TRACING_FORMAT_TI_ONEFILE, {"tracing/smpi/format/ti_one_file"});
+  config::declare_flag<std::string>("tracing/comment", "Add a comment line to the top of the trace file.", "");
+  config::declare_flag<std::string>(OPT_TRACING_COMMENT_FILE,
+                                    "Add the contents of a file as comments to the top of the trace.", "");
+  config::alias(OPT_TRACING_COMMENT_FILE, {"tracing/comment_file"});
+  config::declare_flag<int>("tracing/precision",
+                            "Numerical precision used when timestamping events "
+                            "(expressed in number of digits after decimal point)",
+                            6);
+
+  /* Connect callbacks */
+  s4u::Engine::on_platform_creation.connect(on_simulation_start);
+  s4u::Engine::on_deadlock.connect(on_simulation_end);
+  s4u::Engine::on_simulation_end.connect(on_simulation_end);
+}
+} // namespace instr
+} // namespace simgrid