Skip to content

Commit

Permalink
[clang][deps] Print timing information
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jansvoboda11 committed Apr 20, 2023
1 parent ab2fc95 commit e766e3a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
9 changes: 9 additions & 0 deletions clang/test/ClangScanDeps/print-timing.c
Original file line number Diff line number Diff line change
@@ -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
[]
15 changes: 15 additions & 0 deletions clang/tools/clang-scan-deps/ClangScanDeps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
#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"
#include "llvm/Support/Program.h"
#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>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions clang/tools/clang-scan-deps/Opts.td
Original file line number Diff line number Diff line change
Expand Up @@ -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">;
Expand Down

0 comments on commit e766e3a

Please sign in to comment.