[clang] e766e3a - [clang][deps] Print timing information

Jan Svoboda via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 20 11:18:39 PDT 2023


Author: Jan Svoboda
Date: 2023-04-20T11:18:32-07:00
New Revision: e766e3afedeba90dfbd0513a675a9bf32c5cc8e9

URL: https://github.com/llvm/llvm-project/commit/e766e3afedeba90dfbd0513a675a9bf32c5cc8e9
DIFF: https://github.com/llvm/llvm-project/commit/e766e3afedeba90dfbd0513a675a9bf32c5cc8e9.diff

LOG: [clang][deps] Print timing information

This patch adds new `-print-timing` option to `clang-scan-deps`. It measures the wall and process time taken to scan dependencies for the compilation database. This provides more representative data compared to measuring the timing for the whole tool invocation, since that includes parsing and generating JSON files, which can be significant for larger inputs.

Reviewed By: akyrtzi

Differential Revision: https://reviews.llvm.org/D147815

Added: 
    clang/test/ClangScanDeps/print-timing.c

Modified: 
    clang/tools/clang-scan-deps/ClangScanDeps.cpp
    clang/tools/clang-scan-deps/Opts.td

Removed: 
    


################################################################################
diff  --git a/clang/test/ClangScanDeps/print-timing.c b/clang/test/ClangScanDeps/print-timing.c
new file mode 100644
index 0000000000000..f27df1ebf732a
--- /dev/null
+++ b/clang/test/ClangScanDeps/print-timing.c
@@ -0,0 +1,9 @@
+// RUN: rm -rf %t && mkdir %t
+// RUN: split-file %s %t
+
+// RUN: clang-scan-deps -compilation-database %t/cdb.json -print-timing > %t/result.json 2>%t/errs
+// RUN: cat %t/errs | FileCheck %s
+// CHECK: clang-scan-deps timing: {{[0-9]+}}.{{[0-9][0-9]}}s wall, {{[0-9]+}}.{{[0-9][0-9]}}s process
+
+//--- cdb.json
+[]

diff  --git a/clang/tools/clang-scan-deps/ClangScanDeps.cpp b/clang/tools/clang-scan-deps/ClangScanDeps.cpp
index 60be5d256b053..93c60eb2cdf59 100644
--- a/clang/tools/clang-scan-deps/ClangScanDeps.cpp
+++ b/clang/tools/clang-scan-deps/ClangScanDeps.cpp
@@ -19,6 +19,7 @@
 #include "llvm/ADT/Twine.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/FileUtilities.h"
+#include "llvm/Support/Format.h"
 #include "llvm/Support/InitLLVM.h"
 #include "llvm/Support/JSON.h"
 #include "llvm/Support/LLVMDriver.h"
@@ -26,6 +27,7 @@
 #include "llvm/Support/Signals.h"
 #include "llvm/Support/ThreadPool.h"
 #include "llvm/Support/Threading.h"
+#include "llvm/Support/Timer.h"
 #include "llvm/TargetParser/Host.h"
 #include <mutex>
 #include <optional>
@@ -90,6 +92,7 @@ static std::vector<std::string> ModuleDepTargets;
 static bool DeprecatedDriverCommand;
 static ResourceDirRecipeKind ResourceDirRecipe;
 static bool Verbose;
+static bool PrintTiming;
 static std::vector<const char *> CommandLine;
 
 #ifndef NDEBUG
@@ -200,6 +203,8 @@ static void ParseArgs(int argc, char **argv) {
     ResourceDirRecipe = *Kind;
   }
 
+  PrintTiming = Args.hasArg(OPT_print_timing);
+
   Verbose = Args.hasArg(OPT_verbose);
 
   RoundTripArgs = Args.hasArg(OPT_round_trip_args);
@@ -857,6 +862,10 @@ int clang_scan_deps_main(int argc, char **argv, const llvm::ToolContext &) {
     llvm::outs() << "Running clang-scan-deps on " << Inputs.size()
                  << " files using " << Pool.getThreadCount() << " workers\n";
   }
+
+  llvm::Timer T;
+  T.startTimer();
+
   for (unsigned I = 0; I < Pool.getThreadCount(); ++I) {
     Pool.async([&, I]() {
       llvm::StringSet<> AlreadySeenModules;
@@ -946,6 +955,12 @@ int clang_scan_deps_main(int argc, char **argv, const llvm::ToolContext &) {
   }
   Pool.wait();
 
+  T.stopTimer();
+  if (PrintTiming)
+    llvm::errs() << llvm::format(
+        "clang-scan-deps timing: %0.2fs wall, %0.2fs process\n",
+        T.getTotalTime().getWallTime(), T.getTotalTime().getProcessTime());
+
   if (RoundTripArgs)
     if (FD && FD->roundTripCommands(llvm::errs()))
       HadErrors = true;

diff  --git a/clang/tools/clang-scan-deps/Opts.td b/clang/tools/clang-scan-deps/Opts.td
index 0d121ed7f4f54..27917b4f4808a 100644
--- a/clang/tools/clang-scan-deps/Opts.td
+++ b/clang/tools/clang-scan-deps/Opts.td
@@ -31,6 +31,8 @@ def deprecated_driver_command : F<"deprecated-driver-command", "use a single dri
 
 defm resource_dir_recipe : Eq<"resource-dir-recipe", "How to produce missing '-resource-dir' argument">;
 
+def print_timing : F<"print-timing", "Print timing information">;
+
 def verbose : F<"v", "Use verbose output">;
 
 def round_trip_args : F<"round-trip-args", "verify that command-line arguments are canonical by parsing and re-serializing">;


        


More information about the cfe-commits mailing list