1 // Copyright 2021 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "partition_alloc/partition_lock.h"
6
7 #include <vector>
8
9 #include "base/timer/lap_timer.h"
10 #include "partition_alloc/partition_alloc_base/threading/platform_thread_for_testing.h"
11 #include "partition_alloc/partition_alloc_base/time/time.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "testing/perf/perf_result_reporter.h"
14
15 namespace partition_alloc::internal {
16
17 namespace {
18
19 constexpr int kWarmupRuns = 1;
20 constexpr ::base::TimeDelta kTimeLimit = ::base::Seconds(1);
21 constexpr int kTimeCheckInterval = 100000;
22
23 constexpr char kMetricPrefixLock[] = "PartitionLock.";
24 constexpr char kMetricLockUnlockThroughput[] = "lock_unlock_throughput";
25 constexpr char kMetricLockUnlockLatency[] = "lock_unlock_latency_ns";
26 constexpr char kStoryBaseline[] = "baseline_story";
27 constexpr char kStoryWithCompetingThread[] = "with_competing_thread";
28
SetUpReporter(const std::string & story_name)29 perf_test::PerfResultReporter SetUpReporter(const std::string& story_name) {
30 perf_test::PerfResultReporter reporter(kMetricPrefixLock, story_name);
31 reporter.RegisterImportantMetric(kMetricLockUnlockThroughput, "runs/s");
32 reporter.RegisterImportantMetric(kMetricLockUnlockLatency, "ns");
33 return reporter;
34 }
35
36 class Spin : public base::PlatformThreadForTesting::Delegate {
37 public:
Spin(Lock * lock,uint32_t * data)38 Spin(Lock* lock, uint32_t* data)
39 : lock_(lock), data_(data), should_stop_(false) {}
40 ~Spin() override = default;
41
ThreadMain()42 void ThreadMain() override {
43 started_count_++;
44 // Local variable to avoid "cache line ping-pong" from influencing the
45 // results.
46 uint32_t count = 0;
47 while (!should_stop_.load(std::memory_order_relaxed)) {
48 lock_->Acquire();
49 count++;
50 lock_->Release();
51 }
52
53 lock_->Acquire();
54 (*data_) += count;
55 lock_->Release();
56 }
57
58 // Called from another thread to stop the loop.
Stop()59 void Stop() { should_stop_ = true; }
started_count() const60 int started_count() const { return started_count_; }
61
62 private:
63 Lock* lock_;
64 uint32_t* data_ GUARDED_BY(lock_);
65 std::atomic<bool> should_stop_;
66 std::atomic<int> started_count_{0};
67 };
68
69 } // namespace
70
TEST(PartitionLockPerfTest,Simple)71 TEST(PartitionLockPerfTest, Simple) {
72 ::base::LapTimer timer(kWarmupRuns, kTimeLimit, kTimeCheckInterval);
73 [[maybe_unused]] uint32_t data = 0;
74
75 Lock lock;
76
77 do {
78 lock.Acquire();
79 data += 1;
80 lock.Release();
81 timer.NextLap();
82 } while (!timer.HasTimeLimitExpired());
83
84 auto reporter = SetUpReporter(kStoryBaseline);
85 reporter.AddResult(kMetricLockUnlockThroughput, timer.LapsPerSecond());
86 reporter.AddResult(kMetricLockUnlockLatency, 1e9 / timer.LapsPerSecond());
87 }
88
TEST(PartitionLockPerfTest,WithCompetingThreads)89 TEST(PartitionLockPerfTest, WithCompetingThreads) {
90 uint32_t data = 0;
91
92 Lock lock;
93
94 // Starts a competing thread executing the same loop as this thread.
95 Spin thread_main(&lock, &data);
96 std::vector<base::PlatformThreadHandle> thread_handles;
97 constexpr int kThreads = 4;
98
99 for (int i = 0; i < kThreads; i++) {
100 base::PlatformThreadHandle thread_handle;
101 ASSERT_TRUE(base::PlatformThreadForTesting::Create(0, &thread_main,
102 &thread_handle));
103 thread_handles.push_back(thread_handle);
104 }
105 // Wait for all the threads to start.
106 while (thread_main.started_count() != kThreads) {
107 }
108
109 ::base::LapTimer timer(kWarmupRuns, kTimeLimit, kTimeCheckInterval);
110 do {
111 lock.Acquire();
112 data += 1;
113 lock.Release();
114 timer.NextLap();
115 } while (!timer.HasTimeLimitExpired());
116
117 thread_main.Stop();
118 for (int i = 0; i < kThreads; i++) {
119 base::PlatformThreadForTesting::Join(thread_handles[i]);
120 }
121
122 auto reporter = SetUpReporter(kStoryWithCompetingThread);
123 reporter.AddResult(kMetricLockUnlockThroughput, timer.LapsPerSecond());
124 reporter.AddResult(kMetricLockUnlockLatency, 1e9 / timer.LapsPerSecond());
125 }
126
127 } // namespace partition_alloc::internal
128