[lld] 3669f0e - Refactor TimeProfiler write methods (NFC)
Reid Kleckner via llvm-commits
llvm-commits at lists.llvm.org
Fri Mar 6 14:44:45 PST 2020
Author: Andrew Monshizadeh
Date: 2020-03-06T14:34:56-08:00
New Revision: 3669f0ed4f962f253baa1c08256a3660aafbf9f1
URL: https://github.com/llvm/llvm-project/commit/3669f0ed4f962f253baa1c08256a3660aafbf9f1
DIFF: https://github.com/llvm/llvm-project/commit/3669f0ed4f962f253baa1c08256a3660aafbf9f1.diff
LOG: Refactor TimeProfiler write methods (NFC)
Added a write method for TimeTrace that takes two strings representing
file names. The first is any file name that may have been provided by the
user via `time-trace-file` flag, and the second is a fallback that should
be configured by the caller. This method makes it cleaner to write the
trace output because there is no longer a need to check file names at the
caller and simplifies future TimeTrace usages.
Reviewed By: modocache
Differential Revision: https://reviews.llvm.org/D74514
Added:
Modified:
lld/ELF/Driver.cpp
llvm/include/llvm/Support/TimeProfiler.h
llvm/lib/Support/TimeProfiler.cpp
Removed:
################################################################################
diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index c59776d6ef2f..8d919308126c 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -530,17 +530,14 @@ void LinkerDriver::main(ArrayRef<const char *> argsArr) {
}
if (config->timeTraceEnabled) {
- // Write the result of the time trace profiler.
- std::string path = args.getLastArgValue(OPT_time_trace_file_eq).str();
- if (path.empty())
- path = (config->outputFile + ".time-trace").str();
- std::error_code ec;
- raw_fd_ostream os(path, ec, sys::fs::OF_Text);
- if (ec) {
- error("cannot open " + path + ": " + ec.message());
+ if (auto E = timeTraceProfilerWrite(args.getLastArgValue(OPT_time_trace_file_eq).str(),
+ config->outputFile)) {
+ handleAllErrors(std::move(E), [&](const StringError &SE) {
+ error(SE.getMessage());
+ });
return;
}
- timeTraceProfilerWrite(os);
+
timeTraceProfilerCleanup();
}
}
diff --git a/llvm/include/llvm/Support/TimeProfiler.h b/llvm/include/llvm/Support/TimeProfiler.h
index 20d6b6916c06..b6f8a647e3ee 100644
--- a/llvm/include/llvm/Support/TimeProfiler.h
+++ b/llvm/include/llvm/Support/TimeProfiler.h
@@ -9,6 +9,7 @@
#ifndef LLVM_SUPPORT_TIME_PROFILER_H
#define LLVM_SUPPORT_TIME_PROFILER_H
+#include "llvm/Support/Error.h"
#include "llvm/Support/raw_ostream.h"
namespace llvm {
@@ -33,11 +34,19 @@ inline bool timeTraceProfilerEnabled() {
return getTimeTraceProfilerInstance() != nullptr;
}
-/// Write profiling data to output file.
+/// Write profiling data to output stream.
/// Data produced is JSON, in Chrome "Trace Event" format, see
/// https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview
void timeTraceProfilerWrite(raw_pwrite_stream &OS);
+/// Write profiling data to a file.
+/// The function will write to \p PreferredFileName if provided, if not
+/// then will write to \p FallbackFileName appending .time-trace.
+/// Returns a StringError indicating a failure if the function is
+/// unable to open the file for writing.
+Error timeTraceProfilerWrite(StringRef PreferredFileName,
+ StringRef FallbackFileName);
+
/// Manually begin a time section, with the given \p Name and \p Detail.
/// Profiler copies the string data, so the pointers can be given into
/// temporaries. Time sections can be hierarchical; every Begin must have a
diff --git a/llvm/lib/Support/TimeProfiler.cpp b/llvm/lib/Support/TimeProfiler.cpp
index 19caefe00f17..9c47cb10f33b 100644
--- a/llvm/lib/Support/TimeProfiler.cpp
+++ b/llvm/lib/Support/TimeProfiler.cpp
@@ -279,6 +279,26 @@ void timeTraceProfilerWrite(raw_pwrite_stream &OS) {
TimeTraceProfilerInstance->Write(OS);
}
+Error timeTraceProfilerWrite(StringRef PreferredFileName,
+ StringRef FallbackFileName) {
+ assert(TimeTraceProfilerInstance != nullptr &&
+ "Profiler object can't be null");
+
+ std::string Path = PreferredFileName.str();
+ if (Path.empty()) {
+ Path = FallbackFileName == "-" ? "out" : FallbackFileName.str();
+ Path += ".time-trace";
+ }
+
+ std::error_code EC;
+ raw_fd_ostream OS(Path, EC, sys::fs::OF_Text);
+ if (EC)
+ return createStringError(EC, "Could not open " + Path);
+
+ timeTraceProfilerWrite(OS);
+ return Error::success();
+}
+
void timeTraceProfilerBegin(StringRef Name, StringRef Detail) {
if (TimeTraceProfilerInstance != nullptr)
TimeTraceProfilerInstance->begin(std::string(Name),
More information about the llvm-commits
mailing list