133a7ea4bSMehdi Amini //========- unittests/Support/ThreadPools.cpp - ThreadPools.h tests --========//
233a7ea4bSMehdi Amini //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
633a7ea4bSMehdi Amini //
733a7ea4bSMehdi Amini //===----------------------------------------------------------------------===//
833a7ea4bSMehdi Amini 
933a7ea4bSMehdi Amini #include "llvm/Support/ThreadPool.h"
1033a7ea4bSMehdi Amini 
1133a7ea4bSMehdi Amini #include "llvm/ADT/STLExtras.h"
124fcb2558SAlexandre Ganea #include "llvm/ADT/SetVector.h"
13942e52c7SMehdi Amini #include "llvm/ADT/SmallVector.h"
14942e52c7SMehdi Amini #include "llvm/ADT/Triple.h"
154fcb2558SAlexandre Ganea #include "llvm/Support/CommandLine.h"
16942e52c7SMehdi Amini #include "llvm/Support/Host.h"
174fcb2558SAlexandre Ganea #include "llvm/Support/Program.h"
18942e52c7SMehdi Amini #include "llvm/Support/TargetSelect.h"
198404aeb5SAlexandre Ganea #include "llvm/Support/Threading.h"
2033a7ea4bSMehdi Amini 
21*8ef5710eSLuboš Luňák #include <chrono>
22*8ef5710eSLuboš Luňák #include <thread>
23*8ef5710eSLuboš Luňák 
2433a7ea4bSMehdi Amini #include "gtest/gtest.h"
2533a7ea4bSMehdi Amini 
2633a7ea4bSMehdi Amini using namespace llvm;
2733a7ea4bSMehdi Amini 
28942e52c7SMehdi Amini // Fixture for the unittests, allowing to *temporarily* disable the unittests
29942e52c7SMehdi Amini // on a particular platform
30942e52c7SMehdi Amini class ThreadPoolTest : public testing::Test {
31942e52c7SMehdi Amini   Triple Host;
32942e52c7SMehdi Amini   SmallVector<Triple::ArchType, 4> UnsupportedArchs;
33942e52c7SMehdi Amini   SmallVector<Triple::OSType, 4> UnsupportedOSs;
34942e52c7SMehdi Amini   SmallVector<Triple::EnvironmentType, 1> UnsupportedEnvironments;
35*8ef5710eSLuboš Luňák 
36942e52c7SMehdi Amini protected:
37942e52c7SMehdi Amini   // This is intended for platform as a temporary "XFAIL"
isUnsupportedOSOrEnvironment()38942e52c7SMehdi Amini   bool isUnsupportedOSOrEnvironment() {
39942e52c7SMehdi Amini     Triple Host(Triple::normalize(sys::getProcessTriple()));
40942e52c7SMehdi Amini 
410d955d0bSDavid Majnemer     if (find(UnsupportedEnvironments, Host.getEnvironment()) !=
420d955d0bSDavid Majnemer         UnsupportedEnvironments.end())
43942e52c7SMehdi Amini       return true;
44942e52c7SMehdi Amini 
450d955d0bSDavid Majnemer     if (is_contained(UnsupportedOSs, Host.getOS()))
46942e52c7SMehdi Amini       return true;
47942e52c7SMehdi Amini 
480d955d0bSDavid Majnemer     if (is_contained(UnsupportedArchs, Host.getArch()))
49942e52c7SMehdi Amini       return true;
50942e52c7SMehdi Amini 
51942e52c7SMehdi Amini     return false;
52942e52c7SMehdi Amini   }
53942e52c7SMehdi Amini 
ThreadPoolTest()54942e52c7SMehdi Amini   ThreadPoolTest() {
55942e52c7SMehdi Amini     // Add unsupported configuration here, example:
56942e52c7SMehdi Amini     //   UnsupportedArchs.push_back(Triple::x86_64);
574b8d75b5SMehdi Amini 
584b8d75b5SMehdi Amini     // See https://llvm.org/bugs/show_bug.cgi?id=25829
594b8d75b5SMehdi Amini     UnsupportedArchs.push_back(Triple::ppc64le);
604b8d75b5SMehdi Amini     UnsupportedArchs.push_back(Triple::ppc64);
61942e52c7SMehdi Amini   }
620129fca1SMehdi Amini 
630129fca1SMehdi Amini   /// Make sure this thread not progress faster than the main thread.
waitForMainThread()64*8ef5710eSLuboš Luňák   void waitForMainThread() { waitForPhase(1); }
652cf75338SVedant Kumar 
662cf75338SVedant Kumar   /// Set the readiness of the main thread.
setMainThreadReady()67*8ef5710eSLuboš Luňák   void setMainThreadReady() { setPhase(1); }
68*8ef5710eSLuboš Luňák 
69*8ef5710eSLuboš Luňák   /// Wait until given phase is set using setPhase(); first "main" phase is 1.
70*8ef5710eSLuboš Luňák   /// See also PhaseResetHelper below.
waitForPhase(int Phase)71*8ef5710eSLuboš Luňák   void waitForPhase(int Phase) {
72*8ef5710eSLuboš Luňák     std::unique_lock<std::mutex> LockGuard(CurrentPhaseMutex);
73*8ef5710eSLuboš Luňák     CurrentPhaseCondition.wait(
74*8ef5710eSLuboš Luňák         LockGuard, [&] { return CurrentPhase == Phase || CurrentPhase < 0; });
75fa4e4747SMehdi Amini   }
76*8ef5710eSLuboš Luňák   /// If a thread waits on another phase, the test could bail out on a failed
77*8ef5710eSLuboš Luňák   /// assertion and ThreadPool destructor would wait() on all threads, which
78*8ef5710eSLuboš Luňák   /// would deadlock on the task waiting. Create this helper to automatically
79*8ef5710eSLuboš Luňák   /// reset the phase and unblock such threads.
80*8ef5710eSLuboš Luňák   struct PhaseResetHelper {
PhaseResetHelperThreadPoolTest::PhaseResetHelper81*8ef5710eSLuboš Luňák     PhaseResetHelper(ThreadPoolTest *test) : test(test) {}
~PhaseResetHelperThreadPoolTest::PhaseResetHelper82*8ef5710eSLuboš Luňák     ~PhaseResetHelper() { test->setPhase(-1); }
83*8ef5710eSLuboš Luňák     ThreadPoolTest *test;
84*8ef5710eSLuboš Luňák   };
85*8ef5710eSLuboš Luňák 
86*8ef5710eSLuboš Luňák   /// Advance to the given phase.
setPhase(int Phase)87*8ef5710eSLuboš Luňák   void setPhase(int Phase) {
88*8ef5710eSLuboš Luňák     {
89*8ef5710eSLuboš Luňák       std::unique_lock<std::mutex> LockGuard(CurrentPhaseMutex);
90*8ef5710eSLuboš Luňák       assert(Phase == CurrentPhase + 1 || Phase < 0);
91*8ef5710eSLuboš Luňák       CurrentPhase = Phase;
92*8ef5710eSLuboš Luňák     }
93*8ef5710eSLuboš Luňák     CurrentPhaseCondition.notify_all();
942cf75338SVedant Kumar   }
952cf75338SVedant Kumar 
SetUp()96*8ef5710eSLuboš Luňák   void SetUp() override { CurrentPhase = 0; }
97fa4e4747SMehdi Amini 
984fcb2558SAlexandre Ganea   std::vector<llvm::BitVector> RunOnAllSockets(ThreadPoolStrategy S);
998404aeb5SAlexandre Ganea 
100*8ef5710eSLuboš Luňák   std::condition_variable CurrentPhaseCondition;
101*8ef5710eSLuboš Luňák   std::mutex CurrentPhaseMutex;
102*8ef5710eSLuboš Luňák   int CurrentPhase; // -1 = error, 0 = setup, 1 = ready, 2+ = custom
103942e52c7SMehdi Amini };
104942e52c7SMehdi Amini 
105942e52c7SMehdi Amini #define CHECK_UNSUPPORTED()                                                    \
106942e52c7SMehdi Amini   do {                                                                         \
107942e52c7SMehdi Amini     if (isUnsupportedOSOrEnvironment())                                        \
1086aa8a836SPaul Robinson       GTEST_SKIP();                                                            \
1094fcb2558SAlexandre Ganea   } while (0);
1104fcb2558SAlexandre Ganea 
TEST_F(ThreadPoolTest,AsyncBarrier)111942e52c7SMehdi Amini TEST_F(ThreadPoolTest, AsyncBarrier) {
112942e52c7SMehdi Amini   CHECK_UNSUPPORTED();
11333a7ea4bSMehdi Amini   // test that async & barrier work together properly.
11433a7ea4bSMehdi Amini 
11533a7ea4bSMehdi Amini   std::atomic_int checked_in{0};
11633a7ea4bSMehdi Amini 
11733a7ea4bSMehdi Amini   ThreadPool Pool;
11833a7ea4bSMehdi Amini   for (size_t i = 0; i < 5; ++i) {
11917d266bcSMalcolm Parsons     Pool.async([this, &checked_in] {
1200129fca1SMehdi Amini       waitForMainThread();
12133a7ea4bSMehdi Amini       ++checked_in;
12233a7ea4bSMehdi Amini     });
12333a7ea4bSMehdi Amini   }
1240129fca1SMehdi Amini   ASSERT_EQ(0, checked_in);
125fa4e4747SMehdi Amini   setMainThreadReady();
12633a7ea4bSMehdi Amini   Pool.wait();
12733a7ea4bSMehdi Amini   ASSERT_EQ(5, checked_in);
12833a7ea4bSMehdi Amini }
12933a7ea4bSMehdi Amini 
TestFunc(std::atomic_int & checked_in,int i)130f064d622STeresa Johnson static void TestFunc(std::atomic_int &checked_in, int i) { checked_in += i; }
131f064d622STeresa Johnson 
TEST_F(ThreadPoolTest,AsyncBarrierArgs)132942e52c7SMehdi Amini TEST_F(ThreadPoolTest, AsyncBarrierArgs) {
133942e52c7SMehdi Amini   CHECK_UNSUPPORTED();
134f064d622STeresa Johnson   // Test that async works with a function requiring multiple parameters.
135f064d622STeresa Johnson   std::atomic_int checked_in{0};
136f064d622STeresa Johnson 
137f064d622STeresa Johnson   ThreadPool Pool;
138f064d622STeresa Johnson   for (size_t i = 0; i < 5; ++i) {
139f064d622STeresa Johnson     Pool.async(TestFunc, std::ref(checked_in), i);
140f064d622STeresa Johnson   }
141f064d622STeresa Johnson   Pool.wait();
142f064d622STeresa Johnson   ASSERT_EQ(10, checked_in);
143f064d622STeresa Johnson }
144f064d622STeresa Johnson 
TEST_F(ThreadPoolTest,Async)145942e52c7SMehdi Amini TEST_F(ThreadPoolTest, Async) {
146942e52c7SMehdi Amini   CHECK_UNSUPPORTED();
14733a7ea4bSMehdi Amini   ThreadPool Pool;
14833a7ea4bSMehdi Amini   std::atomic_int i{0};
1490129fca1SMehdi Amini   Pool.async([this, &i] {
1500129fca1SMehdi Amini     waitForMainThread();
15133a7ea4bSMehdi Amini     ++i;
15233a7ea4bSMehdi Amini   });
15333a7ea4bSMehdi Amini   Pool.async([&i] { ++i; });
1540129fca1SMehdi Amini   ASSERT_NE(2, i.load());
155fa4e4747SMehdi Amini   setMainThreadReady();
15633a7ea4bSMehdi Amini   Pool.wait();
15733a7ea4bSMehdi Amini   ASSERT_EQ(2, i.load());
15833a7ea4bSMehdi Amini }
15933a7ea4bSMehdi Amini 
TEST_F(ThreadPoolTest,GetFuture)1600f0d5d8fSDavide Italiano TEST_F(ThreadPoolTest, GetFuture) {
1610f0d5d8fSDavide Italiano   CHECK_UNSUPPORTED();
1628404aeb5SAlexandre Ganea   ThreadPool Pool(hardware_concurrency(2));
1630f0d5d8fSDavide Italiano   std::atomic_int i{0};
1640f0d5d8fSDavide Italiano   Pool.async([this, &i] {
1650f0d5d8fSDavide Italiano     waitForMainThread();
1660f0d5d8fSDavide Italiano     ++i;
1670f0d5d8fSDavide Italiano   });
1680f0d5d8fSDavide Italiano   // Force the future using get()
1690f0d5d8fSDavide Italiano   Pool.async([&i] { ++i; }).get();
1700f0d5d8fSDavide Italiano   ASSERT_NE(2, i.load());
1710f0d5d8fSDavide Italiano   setMainThreadReady();
1720f0d5d8fSDavide Italiano   Pool.wait();
1730f0d5d8fSDavide Italiano   ASSERT_EQ(2, i.load());
1740f0d5d8fSDavide Italiano }
1750f0d5d8fSDavide Italiano 
TEST_F(ThreadPoolTest,GetFutureWithResult)1768cb1af73SFlorian Hahn TEST_F(ThreadPoolTest, GetFutureWithResult) {
1778cb1af73SFlorian Hahn   CHECK_UNSUPPORTED();
1788cb1af73SFlorian Hahn   ThreadPool Pool(hardware_concurrency(2));
1798cb1af73SFlorian Hahn   auto F1 = Pool.async([] { return 1; });
1808cb1af73SFlorian Hahn   auto F2 = Pool.async([] { return 2; });
1818cb1af73SFlorian Hahn 
1828cb1af73SFlorian Hahn   setMainThreadReady();
1838cb1af73SFlorian Hahn   Pool.wait();
1848cb1af73SFlorian Hahn   ASSERT_EQ(1, F1.get());
1858cb1af73SFlorian Hahn   ASSERT_EQ(2, F2.get());
1868cb1af73SFlorian Hahn }
1878cb1af73SFlorian Hahn 
TEST_F(ThreadPoolTest,GetFutureWithResultAndArgs)1888cb1af73SFlorian Hahn TEST_F(ThreadPoolTest, GetFutureWithResultAndArgs) {
1898cb1af73SFlorian Hahn   CHECK_UNSUPPORTED();
1908cb1af73SFlorian Hahn   ThreadPool Pool(hardware_concurrency(2));
1918cb1af73SFlorian Hahn   auto Fn = [](int x) { return x; };
1928cb1af73SFlorian Hahn   auto F1 = Pool.async(Fn, 1);
1938cb1af73SFlorian Hahn   auto F2 = Pool.async(Fn, 2);
1948cb1af73SFlorian Hahn 
1958cb1af73SFlorian Hahn   setMainThreadReady();
1968cb1af73SFlorian Hahn   Pool.wait();
1978cb1af73SFlorian Hahn   ASSERT_EQ(1, F1.get());
1988cb1af73SFlorian Hahn   ASSERT_EQ(2, F2.get());
1998cb1af73SFlorian Hahn }
2008cb1af73SFlorian Hahn 
TEST_F(ThreadPoolTest,PoolDestruction)201942e52c7SMehdi Amini TEST_F(ThreadPoolTest, PoolDestruction) {
202942e52c7SMehdi Amini   CHECK_UNSUPPORTED();
20333a7ea4bSMehdi Amini   // Test that we are waiting on destruction
20433a7ea4bSMehdi Amini   std::atomic_int checked_in{0};
20533a7ea4bSMehdi Amini   {
20633a7ea4bSMehdi Amini     ThreadPool Pool;
20733a7ea4bSMehdi Amini     for (size_t i = 0; i < 5; ++i) {
20817d266bcSMalcolm Parsons       Pool.async([this, &checked_in] {
2090129fca1SMehdi Amini         waitForMainThread();
21033a7ea4bSMehdi Amini         ++checked_in;
21133a7ea4bSMehdi Amini       });
21233a7ea4bSMehdi Amini     }
2130129fca1SMehdi Amini     ASSERT_EQ(0, checked_in);
214fa4e4747SMehdi Amini     setMainThreadReady();
21533a7ea4bSMehdi Amini   }
21633a7ea4bSMehdi Amini   ASSERT_EQ(5, checked_in);
21733a7ea4bSMehdi Amini }
2188404aeb5SAlexandre Ganea 
219*8ef5710eSLuboš Luňák // Check running tasks in different groups.
TEST_F(ThreadPoolTest,Groups)220*8ef5710eSLuboš Luňák TEST_F(ThreadPoolTest, Groups) {
221*8ef5710eSLuboš Luňák   CHECK_UNSUPPORTED();
222*8ef5710eSLuboš Luňák   // Need at least two threads, as the task in group2
223*8ef5710eSLuboš Luňák   // might block a thread until all tasks in group1 finish.
224*8ef5710eSLuboš Luňák   ThreadPoolStrategy S = hardware_concurrency(2);
225*8ef5710eSLuboš Luňák   if (S.compute_thread_count() < 2)
226*8ef5710eSLuboš Luňák     return;
227*8ef5710eSLuboš Luňák   ThreadPool Pool(S);
228*8ef5710eSLuboš Luňák   PhaseResetHelper Helper(this);
229*8ef5710eSLuboš Luňák   ThreadPoolTaskGroup Group1(Pool);
230*8ef5710eSLuboš Luňák   ThreadPoolTaskGroup Group2(Pool);
231*8ef5710eSLuboš Luňák 
232*8ef5710eSLuboš Luňák   // Check that waiting for an empty group is a no-op.
233*8ef5710eSLuboš Luňák   Group1.wait();
234*8ef5710eSLuboš Luňák 
235*8ef5710eSLuboš Luňák   std::atomic_int checked_in1{0};
236*8ef5710eSLuboš Luňák   std::atomic_int checked_in2{0};
237*8ef5710eSLuboš Luňák 
238*8ef5710eSLuboš Luňák   for (size_t i = 0; i < 5; ++i) {
239*8ef5710eSLuboš Luňák     Group1.async([this, &checked_in1] {
240*8ef5710eSLuboš Luňák       waitForMainThread();
241*8ef5710eSLuboš Luňák       ++checked_in1;
242*8ef5710eSLuboš Luňák     });
243*8ef5710eSLuboš Luňák   }
244*8ef5710eSLuboš Luňák   Group2.async([this, &checked_in2] {
245*8ef5710eSLuboš Luňák     waitForPhase(2);
246*8ef5710eSLuboš Luňák     ++checked_in2;
247*8ef5710eSLuboš Luňák   });
248*8ef5710eSLuboš Luňák   ASSERT_EQ(0, checked_in1);
249*8ef5710eSLuboš Luňák   ASSERT_EQ(0, checked_in2);
250*8ef5710eSLuboš Luňák   // Start first group and wait for it.
251*8ef5710eSLuboš Luňák   setMainThreadReady();
252*8ef5710eSLuboš Luňák   Group1.wait();
253*8ef5710eSLuboš Luňák   ASSERT_EQ(5, checked_in1);
254*8ef5710eSLuboš Luňák   // Second group has not yet finished, start it and wait for it.
255*8ef5710eSLuboš Luňák   ASSERT_EQ(0, checked_in2);
256*8ef5710eSLuboš Luňák   setPhase(2);
257*8ef5710eSLuboš Luňák   Group2.wait();
258*8ef5710eSLuboš Luňák   ASSERT_EQ(5, checked_in1);
259*8ef5710eSLuboš Luňák   ASSERT_EQ(1, checked_in2);
260*8ef5710eSLuboš Luňák }
261*8ef5710eSLuboš Luňák 
262*8ef5710eSLuboš Luňák // Check recursive tasks.
TEST_F(ThreadPoolTest,RecursiveGroups)263*8ef5710eSLuboš Luňák TEST_F(ThreadPoolTest, RecursiveGroups) {
264*8ef5710eSLuboš Luňák   CHECK_UNSUPPORTED();
265*8ef5710eSLuboš Luňák   ThreadPool Pool;
266*8ef5710eSLuboš Luňák   ThreadPoolTaskGroup Group(Pool);
267*8ef5710eSLuboš Luňák 
268*8ef5710eSLuboš Luňák   std::atomic_int checked_in1{0};
269*8ef5710eSLuboš Luňák 
270*8ef5710eSLuboš Luňák   for (size_t i = 0; i < 5; ++i) {
271*8ef5710eSLuboš Luňák     Group.async([this, &Pool, &checked_in1] {
272*8ef5710eSLuboš Luňák       waitForMainThread();
273*8ef5710eSLuboš Luňák 
274*8ef5710eSLuboš Luňák       ThreadPoolTaskGroup LocalGroup(Pool);
275*8ef5710eSLuboš Luňák 
276*8ef5710eSLuboš Luňák       // Check that waiting for an empty group is a no-op.
277*8ef5710eSLuboš Luňák       LocalGroup.wait();
278*8ef5710eSLuboš Luňák 
279*8ef5710eSLuboš Luňák       std::atomic_int checked_in2{0};
280*8ef5710eSLuboš Luňák       for (size_t i = 0; i < 5; ++i) {
281*8ef5710eSLuboš Luňák         LocalGroup.async([&checked_in2] { ++checked_in2; });
282*8ef5710eSLuboš Luňák       }
283*8ef5710eSLuboš Luňák       LocalGroup.wait();
284*8ef5710eSLuboš Luňák       ASSERT_EQ(5, checked_in2);
285*8ef5710eSLuboš Luňák 
286*8ef5710eSLuboš Luňák       ++checked_in1;
287*8ef5710eSLuboš Luňák     });
288*8ef5710eSLuboš Luňák   }
289*8ef5710eSLuboš Luňák   ASSERT_EQ(0, checked_in1);
290*8ef5710eSLuboš Luňák   setMainThreadReady();
291*8ef5710eSLuboš Luňák   Group.wait();
292*8ef5710eSLuboš Luňák   ASSERT_EQ(5, checked_in1);
293*8ef5710eSLuboš Luňák }
294*8ef5710eSLuboš Luňák 
TEST_F(ThreadPoolTest,RecursiveWaitDeadlock)295*8ef5710eSLuboš Luňák TEST_F(ThreadPoolTest, RecursiveWaitDeadlock) {
296*8ef5710eSLuboš Luňák   CHECK_UNSUPPORTED();
297*8ef5710eSLuboš Luňák   ThreadPoolStrategy S = hardware_concurrency(2);
298*8ef5710eSLuboš Luňák   if (S.compute_thread_count() < 2)
299*8ef5710eSLuboš Luňák     return;
300*8ef5710eSLuboš Luňák   ThreadPool Pool(S);
301*8ef5710eSLuboš Luňák   PhaseResetHelper Helper(this);
302*8ef5710eSLuboš Luňák   ThreadPoolTaskGroup Group(Pool);
303*8ef5710eSLuboš Luňák 
304*8ef5710eSLuboš Luňák   // Test that a thread calling wait() for a group and is waiting for more tasks
305*8ef5710eSLuboš Luňák   // returns when the last task finishes in a different thread while the waiting
306*8ef5710eSLuboš Luňák   // thread was waiting for more tasks to process while waiting.
307*8ef5710eSLuboš Luňák 
308*8ef5710eSLuboš Luňák   // Task A runs in the first thread. It finishes and leaves
309*8ef5710eSLuboš Luňák   // the background thread waiting for more tasks.
310*8ef5710eSLuboš Luňák   Group.async([this] {
311*8ef5710eSLuboš Luňák     waitForMainThread();
312*8ef5710eSLuboš Luňák     setPhase(2);
313*8ef5710eSLuboš Luňák   });
314*8ef5710eSLuboš Luňák   // Task B is run in a second thread, it launches yet another
315*8ef5710eSLuboš Luňák   // task C in a different group, which will be handled by the waiting
316*8ef5710eSLuboš Luňák   // thread started above.
317*8ef5710eSLuboš Luňák   Group.async([this, &Pool] {
318*8ef5710eSLuboš Luňák     waitForPhase(2);
319*8ef5710eSLuboš Luňák     ThreadPoolTaskGroup LocalGroup(Pool);
320*8ef5710eSLuboš Luňák     LocalGroup.async([this] {
321*8ef5710eSLuboš Luňák       waitForPhase(3);
322*8ef5710eSLuboš Luňák       // Give the other thread enough time to check that there's no task
323*8ef5710eSLuboš Luňák       // to process and suspend waiting for a notification. This is indeed racy,
324*8ef5710eSLuboš Luňák       // but probably the best that can be done.
325*8ef5710eSLuboš Luňák       std::this_thread::sleep_for(std::chrono::milliseconds(10));
326*8ef5710eSLuboš Luňák     });
327*8ef5710eSLuboš Luňák     // And task B only now will wait for the tasks in the group (=task C)
328*8ef5710eSLuboš Luňák     // to finish. This test checks that it does not deadlock. If the
329*8ef5710eSLuboš Luňák     // `NotifyGroup` handling in ThreadPool::processTasks() didn't take place,
330*8ef5710eSLuboš Luňák     // this task B would be stuck waiting for tasks to arrive.
331*8ef5710eSLuboš Luňák     setPhase(3);
332*8ef5710eSLuboš Luňák     LocalGroup.wait();
333*8ef5710eSLuboš Luňák   });
334*8ef5710eSLuboš Luňák   setMainThreadReady();
335*8ef5710eSLuboš Luňák   Group.wait();
336*8ef5710eSLuboš Luňák }
337*8ef5710eSLuboš Luňák 
3388404aeb5SAlexandre Ganea #if LLVM_ENABLE_THREADS == 1
3398404aeb5SAlexandre Ganea 
340206343f3SPaul Robinson // FIXME: Skip some tests below on non-Windows because multi-socket systems
341206343f3SPaul Robinson // were not fully tested on Unix yet, and llvm::get_thread_affinity_mask()
342206343f3SPaul Robinson // isn't implemented for Unix (need AffinityMask in Support/Unix/Program.inc).
343206343f3SPaul Robinson #ifdef _WIN32
344206343f3SPaul Robinson 
3454fcb2558SAlexandre Ganea std::vector<llvm::BitVector>
RunOnAllSockets(ThreadPoolStrategy S)3464fcb2558SAlexandre Ganea ThreadPoolTest::RunOnAllSockets(ThreadPoolStrategy S) {
3474fcb2558SAlexandre Ganea   llvm::SetVector<llvm::BitVector> ThreadsUsed;
3488404aeb5SAlexandre Ganea   std::mutex Lock;
3498404aeb5SAlexandre Ganea   {
35072b6fcbeSAlexandre Ganea     std::condition_variable AllThreads;
35172b6fcbeSAlexandre Ganea     std::mutex AllThreadsLock;
35272b6fcbeSAlexandre Ganea     unsigned Active = 0;
35372b6fcbeSAlexandre Ganea 
3548404aeb5SAlexandre Ganea     ThreadPool Pool(S);
35572b6fcbeSAlexandre Ganea     for (size_t I = 0; I < S.compute_thread_count(); ++I) {
3568404aeb5SAlexandre Ganea       Pool.async([&] {
35772b6fcbeSAlexandre Ganea         {
35872b6fcbeSAlexandre Ganea           std::lock_guard<std::mutex> Guard(AllThreadsLock);
35972b6fcbeSAlexandre Ganea           ++Active;
36072b6fcbeSAlexandre Ganea           AllThreads.notify_one();
36172b6fcbeSAlexandre Ganea         }
3628404aeb5SAlexandre Ganea         waitForMainThread();
3638404aeb5SAlexandre Ganea         std::lock_guard<std::mutex> Guard(Lock);
3648404aeb5SAlexandre Ganea         auto Mask = llvm::get_thread_affinity_mask();
3658404aeb5SAlexandre Ganea         ThreadsUsed.insert(Mask);
3668404aeb5SAlexandre Ganea       });
3678404aeb5SAlexandre Ganea     }
3684fcb2558SAlexandre Ganea     EXPECT_EQ(true, ThreadsUsed.empty());
36972b6fcbeSAlexandre Ganea     {
37072b6fcbeSAlexandre Ganea       std::unique_lock<std::mutex> Guard(AllThreadsLock);
37172b6fcbeSAlexandre Ganea       AllThreads.wait(Guard,
37272b6fcbeSAlexandre Ganea                       [&]() { return Active == S.compute_thread_count(); });
37372b6fcbeSAlexandre Ganea     }
3748404aeb5SAlexandre Ganea     setMainThreadReady();
3758404aeb5SAlexandre Ganea   }
3764fcb2558SAlexandre Ganea   return ThreadsUsed.takeVector();
3778404aeb5SAlexandre Ganea }
3788404aeb5SAlexandre Ganea 
TEST_F(ThreadPoolTest,AllThreads_UseAllRessources)3798404aeb5SAlexandre Ganea TEST_F(ThreadPoolTest, AllThreads_UseAllRessources) {
3808404aeb5SAlexandre Ganea   CHECK_UNSUPPORTED();
3814fcb2558SAlexandre Ganea   std::vector<llvm::BitVector> ThreadsUsed = RunOnAllSockets({});
3824fcb2558SAlexandre Ganea   ASSERT_EQ(llvm::get_cpus(), ThreadsUsed.size());
3838404aeb5SAlexandre Ganea }
3848404aeb5SAlexandre Ganea 
TEST_F(ThreadPoolTest,AllThreads_OneThreadPerCore)3858404aeb5SAlexandre Ganea TEST_F(ThreadPoolTest, AllThreads_OneThreadPerCore) {
3868404aeb5SAlexandre Ganea   CHECK_UNSUPPORTED();
3874fcb2558SAlexandre Ganea   std::vector<llvm::BitVector> ThreadsUsed =
38872b6fcbeSAlexandre Ganea       RunOnAllSockets(llvm::heavyweight_hardware_concurrency());
3894fcb2558SAlexandre Ganea   ASSERT_EQ(llvm::get_cpus(), ThreadsUsed.size());
3908404aeb5SAlexandre Ganea }
3918404aeb5SAlexandre Ganea 
3924fcb2558SAlexandre Ganea // From TestMain.cpp.
3934fcb2558SAlexandre Ganea extern const char *TestMainArgv0;
3944fcb2558SAlexandre Ganea 
3954fcb2558SAlexandre Ganea // Just a reachable symbol to ease resolving of the executable's path.
3964fcb2558SAlexandre Ganea static cl::opt<std::string> ThreadPoolTestStringArg1("thread-pool-string-arg1");
3974fcb2558SAlexandre Ganea 
3981956288fSMarkus Böck #ifdef _WIN32
3994fcb2558SAlexandre Ganea #define setenv(name, var, ignore) _putenv_s(name, var)
4008404aeb5SAlexandre Ganea #endif
4014fcb2558SAlexandre Ganea 
TEST_F(ThreadPoolTest,AffinityMask)4024fcb2558SAlexandre Ganea TEST_F(ThreadPoolTest, AffinityMask) {
4034fcb2558SAlexandre Ganea   CHECK_UNSUPPORTED();
4044fcb2558SAlexandre Ganea 
4054fcb2558SAlexandre Ganea   // Skip this test if less than 4 threads are available.
4064fcb2558SAlexandre Ganea   if (llvm::hardware_concurrency().compute_thread_count() < 4)
4076aa8a836SPaul Robinson     GTEST_SKIP();
4084fcb2558SAlexandre Ganea 
4094fcb2558SAlexandre Ganea   using namespace llvm::sys;
4104fcb2558SAlexandre Ganea   if (getenv("LLVM_THREADPOOL_AFFINITYMASK")) {
4114fcb2558SAlexandre Ganea     std::vector<llvm::BitVector> ThreadsUsed = RunOnAllSockets({});
4124fcb2558SAlexandre Ganea     // Ensure the threads only ran on CPUs 0-3.
413206343f3SPaul Robinson     // NOTE: Don't use ASSERT* here because this runs in a subprocess,
414206343f3SPaul Robinson     // and will show up as un-executed in the parent.
4157b75a3a8SAlexandre Ganea     assert(llvm::all_of(ThreadsUsed,
4167b75a3a8SAlexandre Ganea                         [](auto &T) { return T.getData().front() < 16UL; }) &&
4177b75a3a8SAlexandre Ganea            "Threads ran on more CPUs than expected! The affinity mask does not "
4187b75a3a8SAlexandre Ganea            "seem to work.");
4196aa8a836SPaul Robinson     GTEST_SKIP();
4204fcb2558SAlexandre Ganea   }
4214fcb2558SAlexandre Ganea   std::string Executable =
4224fcb2558SAlexandre Ganea       sys::fs::getMainExecutable(TestMainArgv0, &ThreadPoolTestStringArg1);
4234fcb2558SAlexandre Ganea   StringRef argv[] = {Executable, "--gtest_filter=ThreadPoolTest.AffinityMask"};
4244fcb2558SAlexandre Ganea 
4254fcb2558SAlexandre Ganea   // Add environment variable to the environment of the child process.
4264fcb2558SAlexandre Ganea   int Res = setenv("LLVM_THREADPOOL_AFFINITYMASK", "1", false);
4274fcb2558SAlexandre Ganea   ASSERT_EQ(Res, 0);
4284fcb2558SAlexandre Ganea 
4294fcb2558SAlexandre Ganea   std::string Error;
4304fcb2558SAlexandre Ganea   bool ExecutionFailed;
4314fcb2558SAlexandre Ganea   BitVector Affinity;
4324fcb2558SAlexandre Ganea   Affinity.resize(4);
4334fcb2558SAlexandre Ganea   Affinity.set(0, 4); // Use CPUs 0,1,2,3.
4344fcb2558SAlexandre Ganea   int Ret = sys::ExecuteAndWait(Executable, argv, {}, {}, 0, 0, &Error,
4354fcb2558SAlexandre Ganea                                 &ExecutionFailed, nullptr, &Affinity);
4364fcb2558SAlexandre Ganea   ASSERT_EQ(0, Ret);
4374fcb2558SAlexandre Ganea }
4384fcb2558SAlexandre Ganea 
439206343f3SPaul Robinson #endif // #ifdef _WIN32
4404fcb2558SAlexandre Ganea #endif // #if LLVM_ENABLE_THREADS == 1
441