14a1a113aSNico Weber //===-- thread_contention.cpp -----------------------------------*- C++ -*-===//
2a95edb9dSMitch Phillips //
3a95edb9dSMitch Phillips // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4a95edb9dSMitch Phillips // See https://llvm.org/LICENSE.txt for license information.
5a95edb9dSMitch Phillips // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6a95edb9dSMitch Phillips //
7a95edb9dSMitch Phillips //===----------------------------------------------------------------------===//
8a95edb9dSMitch Phillips
9a95edb9dSMitch Phillips #include "gwp_asan/tests/harness.h"
10a95edb9dSMitch Phillips
11a95edb9dSMitch Phillips // Note: Compilation of <atomic> and <thread> are extremely expensive for
12a95edb9dSMitch Phillips // non-opt builds of clang.
13a95edb9dSMitch Phillips #include <atomic>
14a95edb9dSMitch Phillips #include <cstdlib>
15a95edb9dSMitch Phillips #include <thread>
16a95edb9dSMitch Phillips #include <vector>
17a95edb9dSMitch Phillips
asyncTask(gwp_asan::GuardedPoolAllocator * GPA,std::atomic<bool> * StartingGun,unsigned NumIterations)18a95edb9dSMitch Phillips void asyncTask(gwp_asan::GuardedPoolAllocator *GPA,
19a95edb9dSMitch Phillips std::atomic<bool> *StartingGun, unsigned NumIterations) {
20a95edb9dSMitch Phillips while (!*StartingGun) {
21a95edb9dSMitch Phillips // Wait for starting gun.
22a95edb9dSMitch Phillips }
23a95edb9dSMitch Phillips
24a95edb9dSMitch Phillips // Get ourselves a new allocation.
25a95edb9dSMitch Phillips for (unsigned i = 0; i < NumIterations; ++i) {
26a95edb9dSMitch Phillips volatile char *Ptr = reinterpret_cast<volatile char *>(
27*a6258684SMitch Phillips GPA->allocate(GPA->getAllocatorState()->maximumAllocationSize()));
28a95edb9dSMitch Phillips // Do any other threads have access to this page?
29a95edb9dSMitch Phillips EXPECT_EQ(*Ptr, 0);
30a95edb9dSMitch Phillips
31a95edb9dSMitch Phillips // Mark the page as from malloc. Wait to see if another thread also takes
32a95edb9dSMitch Phillips // this page.
33a95edb9dSMitch Phillips *Ptr = 'A';
34a95edb9dSMitch Phillips std::this_thread::sleep_for(std::chrono::nanoseconds(10000));
35a95edb9dSMitch Phillips
36a95edb9dSMitch Phillips // Check we still own the page.
37a95edb9dSMitch Phillips EXPECT_EQ(*Ptr, 'A');
38a95edb9dSMitch Phillips
39a95edb9dSMitch Phillips // And now release it.
40a95edb9dSMitch Phillips *Ptr = 0;
41a95edb9dSMitch Phillips GPA->deallocate(const_cast<char *>(Ptr));
42a95edb9dSMitch Phillips }
43a95edb9dSMitch Phillips }
44a95edb9dSMitch Phillips
runThreadContentionTest(unsigned NumThreads,unsigned NumIterations,gwp_asan::GuardedPoolAllocator * GPA)45a95edb9dSMitch Phillips void runThreadContentionTest(unsigned NumThreads, unsigned NumIterations,
46a95edb9dSMitch Phillips gwp_asan::GuardedPoolAllocator *GPA) {
47a95edb9dSMitch Phillips std::atomic<bool> StartingGun{false};
48a95edb9dSMitch Phillips std::vector<std::thread> Threads;
49a95edb9dSMitch Phillips
50a95edb9dSMitch Phillips for (unsigned i = 0; i < NumThreads; ++i) {
51a95edb9dSMitch Phillips Threads.emplace_back(asyncTask, GPA, &StartingGun, NumIterations);
52a95edb9dSMitch Phillips }
53a95edb9dSMitch Phillips
54a95edb9dSMitch Phillips StartingGun = true;
55a95edb9dSMitch Phillips
56a95edb9dSMitch Phillips for (auto &T : Threads)
57a95edb9dSMitch Phillips T.join();
58a95edb9dSMitch Phillips }
59a95edb9dSMitch Phillips
TEST_F(CustomGuardedPoolAllocator,ThreadContention)60a95edb9dSMitch Phillips TEST_F(CustomGuardedPoolAllocator, ThreadContention) {
61a95edb9dSMitch Phillips unsigned NumThreads = 4;
62a95edb9dSMitch Phillips unsigned NumIterations = 10000;
63a95edb9dSMitch Phillips InitNumSlots(NumThreads);
64a95edb9dSMitch Phillips runThreadContentionTest(NumThreads, NumIterations, &GPA);
65a95edb9dSMitch Phillips }
66