Author: sammccall Date: Tue Nov 27 04:09:13 2018 New Revision: 347655 URL: http://llvm.org/viewvc/llvm-project?rev=347655&view=rev Log: [clangd] Prevent thread starvation in tests on loaded systems.
Summary: Background index deliberately runs low-priority, but for tests this may stop them making progress. Reviewers: kadircet Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, arphaman, jfb, cfe-commits Differential Revision: https://reviews.llvm.org/D54938 Modified: clang-tools-extra/trunk/clangd/Threading.cpp clang-tools-extra/trunk/clangd/Threading.h clang-tools-extra/trunk/clangd/tool/ClangdMain.cpp clang-tools-extra/trunk/unittests/clangd/BackgroundIndexTests.cpp Modified: clang-tools-extra/trunk/clangd/Threading.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/Threading.cpp?rev=347655&r1=347654&r2=347655&view=diff ============================================================================== --- clang-tools-extra/trunk/clangd/Threading.cpp (original) +++ clang-tools-extra/trunk/clangd/Threading.cpp Tue Nov 27 04:09:13 2018 @@ -3,6 +3,7 @@ #include "llvm/ADT/ScopeExit.h" #include "llvm/Support/FormatVariadic.h" #include "llvm/Support/Threading.h" +#include <atomic> #include <thread> #ifdef __USE_POSIX #include <pthread.h> @@ -100,6 +101,8 @@ void wait(std::unique_lock<std::mutex> & CV.wait_until(Lock, D.time()); } +static std::atomic<bool> AvoidThreadStarvation = {false}; + void setThreadPriority(std::thread &T, ThreadPriority Priority) { // Some *really* old glibcs are missing SCHED_IDLE. #if defined(__linux__) && defined(SCHED_IDLE) @@ -107,9 +110,13 @@ void setThreadPriority(std::thread &T, T priority.sched_priority = 0; pthread_setschedparam( T.native_handle(), - Priority == ThreadPriority::Low ? SCHED_IDLE : SCHED_OTHER, &priority); + Priority == ThreadPriority::Low && !AvoidThreadStarvation ? SCHED_IDLE + : SCHED_OTHER, + &priority); #endif } +void preventThreadStarvationInTests() { AvoidThreadStarvation = true; } + } // namespace clangd } // namespace clang Modified: clang-tools-extra/trunk/clangd/Threading.h URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/Threading.h?rev=347655&r1=347654&r2=347655&view=diff ============================================================================== --- clang-tools-extra/trunk/clangd/Threading.h (original) +++ clang-tools-extra/trunk/clangd/Threading.h Tue Nov 27 04:09:13 2018 @@ -122,6 +122,10 @@ enum class ThreadPriority { Normal = 1, }; void setThreadPriority(std::thread &T, ThreadPriority Priority); +// Avoid the use of scheduler policies that may starve low-priority threads. +// This prevents tests from timing out on loaded systems. +// Affects subsequent setThreadPriority() calls. +void preventThreadStarvationInTests(); } // namespace clangd } // namespace clang Modified: clang-tools-extra/trunk/clangd/tool/ClangdMain.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/tool/ClangdMain.cpp?rev=347655&r1=347654&r2=347655&view=diff ============================================================================== --- clang-tools-extra/trunk/clangd/tool/ClangdMain.cpp (original) +++ clang-tools-extra/trunk/clangd/tool/ClangdMain.cpp Tue Nov 27 04:09:13 2018 @@ -254,6 +254,7 @@ int main(int argc, char *argv[]) { RunSynchronously = true; InputStyle = JSONStreamStyle::Delimited; PrettyPrint = true; + preventThreadStarvationInTests(); // Ensure background index makes progress. } if (Test || EnableTestScheme) { static URISchemeRegistry::Add<TestScheme> X( Modified: clang-tools-extra/trunk/unittests/clangd/BackgroundIndexTests.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clangd/BackgroundIndexTests.cpp?rev=347655&r1=347654&r2=347655&view=diff ============================================================================== --- clang-tools-extra/trunk/unittests/clangd/BackgroundIndexTests.cpp (original) +++ clang-tools-extra/trunk/unittests/clangd/BackgroundIndexTests.cpp Tue Nov 27 04:09:13 2018 @@ -56,7 +56,12 @@ public: } }; -TEST(BackgroundIndexTest, IndexTwoFiles) { +class BackgroundIndexTest : public ::testing::Test { +protected: + BackgroundIndexTest() { preventThreadStarvationInTests(); } +}; + +TEST_F(BackgroundIndexTest, IndexTwoFiles) { MockFSProvider FS; // a.h yields different symbols when included by A.cc vs B.cc. FS.Files[testPath("root/A.h")] = R"cpp( @@ -115,7 +120,7 @@ TEST(BackgroundIndexTest, IndexTwoFiles) FileURI("unittest:///root/B.cc")})); } -TEST(BackgroundIndexTest, ShardStorageWriteTest) { +TEST_F(BackgroundIndexTest, ShardStorageWriteTest) { MockFSProvider FS; FS.Files[testPath("root/A.h")] = R"cpp( void common(); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits