[PATCH] D78408: [llvm-cov] Prevent llvm-cov from using too many threads

Alexandre Ganea via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 17 17:34:20 PDT 2020


aganea created this revision.
aganea added reviewers: phosek, MaskRay.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.

As requested here: https://reviews.llvm.org/D75153#1987272

Petr, could you please confirm that this patch fixes the issue?


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D78408

Files:
  llvm/include/llvm/Support/Threading.h
  llvm/lib/Support/Threading.cpp
  llvm/tools/llvm-cov/CodeCoverage.cpp


Index: llvm/tools/llvm-cov/CodeCoverage.cpp
===================================================================
--- llvm/tools/llvm-cov/CodeCoverage.cpp
+++ llvm/tools/llvm-cov/CodeCoverage.cpp
@@ -946,8 +946,13 @@
   auto NumThreads = ViewOpts.NumThreads;
 
   // If NumThreads is not specified, auto-detect a good default.
-  if (NumThreads == 0)
-    NumThreads = SourceFiles.size();
+  ThreadPoolStrategy S;
+  if (NumThreads > 0)
+    S = hardware_concurrency(NumThreads); // no upper limit on the # of threads
+  else {
+    S = heavyweight_hardware_concurrency(SourceFiles.size());
+    S.Limit = true; // limit to max hardware cores
+  }
 
   if (!ViewOpts.hasOutputDirectory() || NumThreads == 1) {
     for (const std::string &SourceFile : SourceFiles)
@@ -955,7 +960,7 @@
                           ShowFilenames);
   } else {
     // In -output-dir mode, it's safe to use multiple threads to print files.
-    ThreadPool Pool(heavyweight_hardware_concurrency(NumThreads));
+    ThreadPool Pool(S);
     for (const std::string &SourceFile : SourceFiles)
       Pool.async(&CodeCoverageTool::writeSourceFileView, this, SourceFile,
                  Coverage.get(), Printer.get(), ShowFilenames);
Index: llvm/lib/Support/Threading.cpp
===================================================================
--- llvm/lib/Support/Threading.cpp
+++ llvm/lib/Support/Threading.cpp
@@ -84,14 +84,16 @@
 int computeHostNumHardwareThreads();
 
 unsigned llvm::ThreadPoolStrategy::compute_thread_count() const {
-  if (ThreadsRequested > 0)
-    return ThreadsRequested;
-
   int MaxThreadCount = UseHyperThreads ? computeHostNumHardwareThreads()
                                        : sys::getHostNumPhysicalCores();
   if (MaxThreadCount <= 0)
     MaxThreadCount = 1;
-  return MaxThreadCount;
+
+  if (ThreadsRequested == 0)
+    return MaxThreadCount;
+
+  return Limit ? std::min((unsigned)MaxThreadCount, ThreadsRequested)
+               : ThreadsRequested;
 }
 
 namespace {
Index: llvm/include/llvm/Support/Threading.h
===================================================================
--- llvm/include/llvm/Support/Threading.h
+++ llvm/include/llvm/Support/Threading.h
@@ -157,6 +157,9 @@
     // std::thread per core.
     bool UseHyperThreads = true;
 
+    // Sets an upper bound to the number of hardware threads, or hardware cores.
+    bool Limit = false;
+
     /// Retrieves the max available threads for the current strategy. This
     /// accounts for affinity masks and takes advantage of all CPU sockets.
     unsigned compute_thread_count() const;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D78408.258450.patch
Type: text/x-patch
Size: 2573 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200418/cef53437/attachment.bin>


More information about the llvm-commits mailing list