]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Extract internal tracing stuff to a separate file
authorJoel Rosdahl <joel@rosdahl.net>
Thu, 14 May 2020 19:12:20 +0000 (21:12 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Fri, 15 May 2020 17:06:00 +0000 (19:06 +0200)
Makefile.in
doc/DEVELOPER.md
src/Context.hpp
src/MiniTrace.cpp [new file with mode: 0644]
src/MiniTrace.hpp [new file with mode: 0644]
src/ccache.cpp

index 9ca656062175bc9c33bc7b0cafe23a431bfb1d0b..25ccdd4c02e0ac12064e7ee971cfb52d67d3c806 100644 (file)
@@ -43,6 +43,7 @@ non_third_party_sources = \
     src/Counters.cpp \
     src/Decompressor.cpp \
     src/Lockfile.cpp \
+    src/MiniTrace.cpp \
     src/NullCompressor.cpp \
     src/NullDecompressor.cpp \
     src/ProgressBar.cpp \
index 7401cc65601002565581792e21c8a847ee688e38..c856f1fb76113a8b7c96828f9639c05609594538 100644 (file)
@@ -4,7 +4,7 @@ Developer manual
 Tracing
 -------
 
-In order to see what ccache is doing, it is possible to enable microsecond
+In order to see what ccache is doing, it is possible to enable internal
 tracing:
 
 * Build ccache with the `--enable-tracing` configure option.
index 7dbd70660cfed9288dcc5c43b68a3c0edf2197f9..0f5b4c9cf72818ad59776518568e89bc29a7d688 100644 (file)
@@ -23,6 +23,7 @@
 #include "Args.hpp"
 #include "ArgsInfo.hpp"
 #include "Config.hpp"
+#include "MiniTrace.hpp"
 #include "NonCopyable.hpp"
 #include "ccache.hpp"
 #include "hash.hpp"
@@ -104,6 +105,11 @@ public:
   // Statistics which get written into the `stats_file` upon exit.
   Counters counter_updates;
 
+#ifdef MTR_ENABLED
+  // Internal tracing.
+  std::unique_ptr<MiniTrace> mini_trace;
+#endif
+
   void set_manifest_name(const struct digest& name);
   void set_result_name(const struct digest& name);
 
diff --git a/src/MiniTrace.cpp b/src/MiniTrace.cpp
new file mode 100644 (file)
index 0000000..b8d99f3
--- /dev/null
@@ -0,0 +1,77 @@
+// Copyright (C) 2020 Joel Rosdahl and other contributors
+//
+// See doc/AUTHORS.adoc for a complete list of contributors.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+//
+// This program is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+// more details.
+//
+// You should have received a copy of the GNU General Public License along with
+// this program; if not, write to the Free Software Foundation, Inc., 51
+// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+#ifdef MTR_ENABLED
+
+#  include "MiniTrace.hpp"
+
+#  include "ArgsInfo.hpp"
+#  include "legacy_util.hpp"
+
+namespace {
+
+std::string
+get_system_tmp_dir()
+{
+#  ifndef _WIN32
+  const char* tmpdir = getenv("TMPDIR");
+  if (tmpdir != NULL) {
+    return tmpdir;
+  }
+#  else
+  static char dirbuf[PATH_MAX];
+  DWORD retval = GetTempPath(PATH_MAX, dirbuf);
+  if (retval > 0 && retval < PATH_MAX) {
+    return dirbuf;
+  }
+#  endif
+  return "/tmp";
+}
+
+} // namespace
+
+MiniTrace::MiniTrace(const ArgsInfo& args_info)
+  : m_args_info(args_info), m_trace_id(reinterpret_cast<void*>(getpid()))
+{
+  auto fd_and_path =
+    Util::create_temp_fd(get_system_tmp_dir() + "/ccache-trace");
+  m_tmp_trace_file = fd_and_path.second;
+  close(fd_and_path.first);
+
+  mtr_init(m_tmp_trace_file.c_str());
+  MTR_INSTANT_C("", "", "time", fmt::format("{:f}", time_seconds()).c_str());
+  MTR_META_PROCESS_NAME("ccache");
+  MTR_START("program", "ccache", m_trace_id);
+}
+
+MiniTrace::~MiniTrace()
+{
+  MTR_FINISH("program", "ccache", m_trace_id);
+  mtr_flush();
+  mtr_shutdown();
+
+  if (!m_args_info.output_obj.empty()) {
+    std::string trace_file =
+      fmt::format("{}.ccache-trace", m_args_info.output_obj);
+    move_file(m_tmp_trace_file.c_str(), trace_file.c_str());
+  } else {
+    tmp_unlink(m_tmp_trace_file.c_str());
+  }
+}
+
+#endif
diff --git a/src/MiniTrace.hpp b/src/MiniTrace.hpp
new file mode 100644 (file)
index 0000000..3b44360
--- /dev/null
@@ -0,0 +1,43 @@
+// Copyright (C) 2020 Joel Rosdahl and other contributors
+//
+// See doc/AUTHORS.adoc for a complete list of contributors.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+//
+// This program is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+// more details.
+//
+// You should have received a copy of the GNU General Public License along with
+// this program; if not, write to the Free Software Foundation, Inc., 51
+// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+#pragma once
+
+#include "system.hpp"
+
+#include "third_party/minitrace.h"
+
+#ifdef MTR_ENABLED
+
+#  include <string>
+
+struct ArgsInfo;
+
+class MiniTrace
+{
+public:
+  MiniTrace(const ArgsInfo& args_info);
+  ~MiniTrace();
+
+private:
+  const ArgsInfo& m_args_info;
+  const void* const m_trace_id;
+  std::string m_tmp_trace_file;
+};
+
+#endif
index 0ec38285fcb0f7f65abfcd502dcadf8ff8ced9c5..c69a6ca712dc498b874995e2a9e0bf7380b4d96b 100644 (file)
@@ -24,7 +24,9 @@
 #include "Context.hpp"
 #include "File.hpp"
 #include "FormatNonstdStringView.hpp"
+#include "MiniTrace.hpp"
 #include "ProgressBar.hpp"
+#include "StdMakeUnique.hpp"
 #include "Util.hpp"
 #include "argprocessing.hpp"
 #include "cleanup.hpp"
@@ -42,7 +44,6 @@
 #include "stats.hpp"
 
 #include "third_party/fmt/core.h"
-#include "third_party/minitrace.h"
 #include "third_party/nonstd/string_view.hpp"
 
 #ifdef HAVE_GETOPT_LONG
@@ -2025,62 +2026,6 @@ create_initial_config_file(Config& config)
   fclose(f);
 }
 
-#ifdef MTR_ENABLED
-static void* trace_id;
-static char* tmp_trace_file;
-
-static void
-trace_init(char* path)
-{
-  tmp_trace_file = path;
-  mtr_init(tmp_trace_file);
-  char* s = format("%f", time_seconds());
-  MTR_INSTANT_C("", "", "time", s);
-}
-
-static void
-trace_start()
-{
-  MTR_META_PROCESS_NAME(MYNAME);
-  trace_id = (void*)((long)getpid());
-  MTR_START("program", "ccache", trace_id);
-}
-
-static void
-trace_stop(void* context)
-{
-  const Context& ctx = *static_cast<Context*>(context);
-
-  char* trace_file =
-    format("%s.ccache-trace", ctx.args_info.output_obj.c_str());
-  MTR_FINISH("program", "ccache", trace_id);
-  mtr_flush();
-  mtr_shutdown();
-  move_file(tmp_trace_file, trace_file);
-  free(trace_file);
-  free(tmp_trace_file);
-}
-
-static const char*
-tmpdir()
-{
-#  ifndef _WIN32
-  const char* tmpdir = getenv("TMPDIR");
-  if (tmpdir != NULL) {
-    return tmpdir;
-  }
-#  else
-  static char dirbuf[PATH_MAX];
-  DWORD retval = GetTempPath(PATH_MAX, dirbuf);
-  if (retval > 0 && retval < PATH_MAX) {
-    return dirbuf;
-  }
-#  endif
-  return "/tmp";
-}
-
-#endif // MTR_ENABLED
-
 // Read config file(s), populate variables, create configuration file in cache
 // directory if missing, etc.
 static void
@@ -2157,21 +2102,12 @@ initialize(int argc, const char* const* argv)
   exitfn_add(stats_flush, ctx);
   exitfn_add_nullary(clean_up_pending_tmp_files);
 
-  bool enable_internal_trace = getenv("CCACHE_INTERNAL_TRACE");
-  if (enable_internal_trace) {
-#ifdef MTR_ENABLED
-    // We don't have any conf yet, so we can't use temp_dir() here.
-    trace_init(format("%s/tmp.ccache-trace.%d", tmpdir(), (int)getpid()));
-#endif
-  }
-
   cc_log("=== CCACHE %s STARTED =========================================",
          CCACHE_VERSION);
 
-  if (enable_internal_trace) {
+  if (getenv("CCACHE_INTERNAL_TRACE")) {
 #ifdef MTR_ENABLED
-    trace_start();
-    exitfn_add(trace_stop, ctx);
+    ctx->mini_trace = std::make_unique<MiniTrace>(ctx->args_info);
 #else
     cc_log("Error: tracing is not enabled!");
 #endif