Switch AppleWin build to use CMake
[WebKit-https.git] / Source / WTF / benchmarks / LockSpeedTest.cpp
1 /*
2  * Copyright (C) 2015 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 // On Mac, you can build this like so:
27 // clang++ -o LockSpeedTest Source/WTF/benchmarks/LockSpeedTest.cpp -O3 -W -ISource/WTF -LWebKitBuild/Release -lWTF -framework Foundation -licucore -std=c++11
28
29 #include "config.h"
30
31 #include <thread>
32 #include <unistd.h>
33 #include <wtf/CurrentTime.h>
34 #include <wtf/Lock.h>
35 #include <wtf/StdLibExtras.h>
36 #include <wtf/Threading.h>
37 #include <wtf/ThreadingPrimitives.h>
38 #include <wtf/WordLock.h>
39
40 namespace {
41
42 // This is the old WTF::SpinLock class, included here so that we can still compare our new locks to a
43 // spinlock baseline.
44 class SpinLock {
45 public:
46     SpinLock()
47     {
48         m_lock.store(0, std::memory_order_relaxed);
49     }
50
51     void lock()
52     {
53         while (!m_lock.compareExchangeWeak(0, 1, std::memory_order_acquire))
54             std::this_thread::yield();
55     }
56
57     void unlock()
58     {
59         m_lock.store(0, std::memory_order_release);
60     }
61
62     bool isLocked() const
63     {
64         return m_lock.load(std::memory_order_acquire);
65     }
66
67 private:
68     Atomic<unsigned> m_lock;
69 };
70
71 class ByteSpinLock {
72 public:
73     ByteSpinLock()
74     {
75         m_lock.store(0, std::memory_order_relaxed);
76     }
77
78     void lock()
79     {
80         while (!m_lock.compareExchangeWeak(0, 1, std::memory_order_acquire))
81             std::this_thread::yield();
82     }
83
84     void unlock()
85     {
86         m_lock.store(0, std::memory_order_release);
87     }
88
89     bool isLocked() const
90     {
91         return m_lock.load(std::memory_order_acquire);
92     }
93
94 private:
95     Atomic<uint8_t> m_lock;
96 };
97
98 unsigned numThreadGroups;
99 unsigned numThreadsPerGroup;
100 int workPerCriticalSection;
101 unsigned numNoiseThreads;
102 unsigned numIterations;
103     
104 NO_RETURN void usage()
105 {
106     printf("Usage: LockSpeedTest spinlock|bytespinlock|wordlock|lock|mutex|all <num thread groups> <num threads per group> <work per critical section> <num noise threads> <num iterations>\n");
107     exit(1);
108 }
109
110 template<typename LockType>
111 void runBenchmark(const char* name)
112 {
113     std::unique_ptr<LockType[]> locks = std::make_unique<LockType[]>(numThreadGroups);
114     std::unique_ptr<double[]> words = std::make_unique<double[]>(numThreadGroups);
115     std::unique_ptr<ThreadIdentifier[]> threads = std::make_unique<ThreadIdentifier[]>(numThreadGroups * numThreadsPerGroup);
116     std::unique_ptr<ThreadIdentifier[]> noiseThreads = std::make_unique<ThreadIdentifier[]>(numNoiseThreads);
117     std::unique_ptr<double[]> noiseCounts = std::make_unique<double[]>(numNoiseThreads);
118
119     volatile bool shouldStop = false;
120     for (unsigned threadIndex = numNoiseThreads; threadIndex--;) {
121         noiseCounts[threadIndex] = 0;
122         noiseThreads[threadIndex] = createThread(
123             "Noise Thread",
124             [&shouldStop, &noiseCounts, threadIndex] () {
125                 while (!shouldStop)
126                     noiseCounts[threadIndex]++;
127             });
128     }
129
130     double before = monotonicallyIncreasingTimeMS();
131     
132     for (unsigned threadGroupIndex = numThreadGroups; threadGroupIndex--;) {
133         words[threadGroupIndex] = 0;
134
135         for (unsigned threadIndex = numThreadsPerGroup; threadIndex--;) {
136             threads[threadGroupIndex * numThreadsPerGroup + threadIndex] = createThread(
137                 "Benchmark thread",
138                 [threadGroupIndex, &locks, &words] () {
139                     for (unsigned i = numIterations; i--;) {
140                         locks[threadGroupIndex].lock();
141                         if (workPerCriticalSection < 0)
142                             sleep(-workPerCriticalSection);
143                         else {
144                             for (unsigned j = workPerCriticalSection; j--;) {
145                                 words[threadGroupIndex]++;
146                                 words[threadGroupIndex] *= 1.01;
147                             }
148                         }
149                         locks[threadGroupIndex].unlock();
150                     }
151                 });
152         }
153     }
154
155     for (unsigned threadIndex = numThreadGroups * numThreadsPerGroup; threadIndex--;)
156         waitForThreadCompletion(threads[threadIndex]);
157     shouldStop = true;
158     double noiseCount = 0;
159     for (unsigned threadIndex = numNoiseThreads; threadIndex--;) {
160         waitForThreadCompletion(noiseThreads[threadIndex]);
161         noiseCount += noiseCounts[threadIndex];
162     }
163
164     double after = monotonicallyIncreasingTimeMS();
165
166     printf("%s: %.3lf ms, %.0lf noise.\n", name, after - before, noiseCount);
167 }
168
169 } // anonymous namespace
170
171 int main(int argc, char** argv)
172 {
173     WTF::initializeThreading();
174     
175     if (argc != 7
176         || sscanf(argv[2], "%u", &numThreadGroups) != 1
177         || sscanf(argv[3], "%u", &numThreadsPerGroup) != 1
178         || sscanf(argv[4], "%d", &workPerCriticalSection) != 1
179         || sscanf(argv[5], "%u", &numNoiseThreads) != 1
180         || sscanf(argv[6], "%u", &numIterations) != 1)
181         usage();
182
183     bool didRun = false;
184     if (!strcmp(argv[1], "spinlock") || !strcmp(argv[1], "all")) {
185         runBenchmark<SpinLock>("SpinLock");
186         didRun = true;
187     }
188     if (!strcmp(argv[1], "bytespinlock") || !strcmp(argv[1], "all")) {
189         runBenchmark<SpinLock>("ByteSpinLock");
190         didRun = true;
191     }
192     if (!strcmp(argv[1], "wordlock") || !strcmp(argv[1], "all")) {
193         runBenchmark<WordLock>("WTF WordLock");
194         didRun = true;
195     }
196     if (!strcmp(argv[1], "lock") || !strcmp(argv[1], "all")) {
197         runBenchmark<Lock>("WTF Lock");
198         didRun = true;
199     }
200     if (!strcmp(argv[1], "mutex") || !strcmp(argv[1], "all")) {
201         runBenchmark<Mutex>("Platform Mutex");
202         didRun = true;
203     }
204
205     if (!didRun)
206         usage();
207
208     return 0;
209 }