[PATCH] D74514: Refactor TimeProfiler write methods (NFC)

Andrew Monshizadeh via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 12 15:46:11 PST 2020


amonshiz created this revision.
amonshiz added reviewers: modocache, anton-afanasyev, rnk, rsmith, MatzeB, mzolotukhin.
Herald added subscribers: MaskRay, hiraditya, arichardson, emaste.
Herald added a reviewer: espindola.
Herald added a project: LLVM.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D74514

Files:
  lld/ELF/Driver.cpp
  llvm/include/llvm/Support/TimeProfiler.h
  llvm/lib/Support/TimeProfiler.cpp


Index: llvm/lib/Support/TimeProfiler.cpp
===================================================================
--- llvm/lib/Support/TimeProfiler.cpp
+++ llvm/lib/Support/TimeProfiler.cpp
@@ -279,6 +279,25 @@
   TimeTraceProfilerInstance->Write(OS);
 }
 
+void 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)
+    report_fatal_error("Could not open " + Path + ": " + EC.message());
+
+  timeTraceProfilerWrite(OS);
+}
+
 void timeTraceProfilerBegin(StringRef Name, StringRef Detail) {
   if (TimeTraceProfilerInstance != nullptr)
     TimeTraceProfilerInstance->begin(std::string(Name),
Index: llvm/include/llvm/Support/TimeProfiler.h
===================================================================
--- llvm/include/llvm/Support/TimeProfiler.h
+++ llvm/include/llvm/Support/TimeProfiler.h
@@ -33,11 +33,18 @@
   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.
+/// Triggers a fatal error if a file cannot be opened for writing.
+void 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
Index: lld/ELF/Driver.cpp
===================================================================
--- lld/ELF/Driver.cpp
+++ lld/ELF/Driver.cpp
@@ -530,17 +530,8 @@
   }
 
   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());
-      return;
-    }
-    timeTraceProfilerWrite(os);
+    timeTraceProfilerWrite(args.getLastArgValue(OPT_time_trace_file_eq).str(),
+                           config->outputFile);
     timeTraceProfilerCleanup();
   }
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D74514.244294.patch
Type: text/x-patch
Size: 2959 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200212/d6b401cd/attachment.bin>


More information about the llvm-commits mailing list