src/Counters.cpp \
src/Decompressor.cpp \
src/Lockfile.cpp \
+ src/MiniTrace.cpp \
src/NullCompressor.cpp \
src/NullDecompressor.cpp \
src/ProgressBar.cpp \
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.
#include "Args.hpp"
#include "ArgsInfo.hpp"
#include "Config.hpp"
+#include "MiniTrace.hpp"
#include "NonCopyable.hpp"
#include "ccache.hpp"
#include "hash.hpp"
// 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);
--- /dev/null
+// 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
--- /dev/null
+// 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
#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"
#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
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
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