[JSC] Enable LLInt ASM interpreter on X64 and ARM64 in non JIT configuration
[WebKit-https.git] / Source / WTF / benchmarks / ConditionSpeedTest.cpp
1 /*
2  * Copyright (C) 2015-2017 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 // xcrun clang++ -o ConditionSpeedTest Source/WTF/benchmarks/ConditionSpeedTest.cpp -O3 -W -ISource/WTF -ISource/WTF/icu -LWebKitBuild/Release -lWTF -framework Foundation -licucore -std=c++14 -fvisibility=hidden
28
29 #include "config.h"
30
31 #include "ToyLocks.h"
32 #include <condition_variable>
33 #include <mutex>
34 #include <thread>
35 #include <type_traits>
36 #include <unistd.h>
37 #include <wtf/Condition.h>
38 #include <wtf/DataLog.h>
39 #include <wtf/Deque.h>
40 #include <wtf/Lock.h>
41 #include <wtf/StdLibExtras.h>
42 #include <wtf/StringPrintStream.h>
43 #include <wtf/Threading.h>
44 #include <wtf/ThreadingPrimitives.h>
45 #include <wtf/Vector.h>
46
47 namespace {
48
49 const bool verbose = false;
50
51 unsigned numProducers;
52 unsigned numConsumers;
53 unsigned maxQueueSize;
54 unsigned numMessagesPerProducer;
55     
56 NO_RETURN void usage()
57 {
58     printf("Usage: ConditionSpeedTest lock|mutex|all <num producers> <num consumers> <max queue size> <num messages per producer>\n");
59     exit(1);
60 }
61
62 template<typename Functor, typename ConditionType, typename LockType, typename std::enable_if<!std::is_same<ConditionType, std::condition_variable>::value>::type* = nullptr>
63 void wait(ConditionType& condition, LockType& lock, std::unique_lock<LockType>&, const Functor& predicate)
64 {
65     while (!predicate())
66         condition.wait(lock);
67 }
68
69 template<typename Functor, typename ConditionType, typename LockType, typename std::enable_if<std::is_same<ConditionType, std::condition_variable>::value>::type* = nullptr>
70 void wait(ConditionType& condition, LockType&, std::unique_lock<LockType>& locker, const Functor& predicate)
71 {
72     while (!predicate())
73         condition.wait(locker);
74 }
75
76 template<typename LockType, typename ConditionType, typename NotifyFunctor, typename NotifyAllFunctor>
77 void runTest(
78     unsigned numProducers,
79     unsigned numConsumers,
80     unsigned maxQueueSize,
81     unsigned numMessagesPerProducer,
82     const NotifyFunctor& notify,
83     const NotifyAllFunctor& notifyAll)
84 {
85     Deque<unsigned> queue;
86     bool shouldContinue = true;
87     LockType lock;
88     ConditionType emptyCondition;
89     ConditionType fullCondition;
90
91     Vector<Ref<Thread>> consumerThreads;
92     Vector<Ref<Thread>> producerThreads;
93
94     Vector<unsigned> received;
95     LockType receivedLock;
96     
97     for (unsigned i = numConsumers; i--;) {
98         consumerThreads.append(Thread::create(
99             "Consumer thread",
100             [&] () {
101                 for (;;) {
102                     unsigned result;
103                     unsigned mustNotify = false;
104                     {
105                         std::unique_lock<LockType> locker(lock);
106                         wait(
107                             emptyCondition, lock, locker,
108                             [&] () {
109                                 if (verbose)
110                                     dataLog(toString(Thread::current(), ": Checking consumption predicate with shouldContinue = ", shouldContinue, ", queue.size() == ", queue.size(), "\n"));
111                                 return !shouldContinue || !queue.isEmpty();
112                             });
113                         if (!shouldContinue && queue.isEmpty())
114                             return;
115                         mustNotify = queue.size() == maxQueueSize;
116                         result = queue.takeFirst();
117                     }
118                     notify(fullCondition, mustNotify);
119
120                     {
121                         std::lock_guard<LockType> locker(receivedLock);
122                         received.append(result);
123                     }
124                 }
125             }));
126     }
127
128     for (unsigned i = numProducers; i--;) {
129         producerThreads.append(Thread::create(
130             "Producer Thread",
131             [&] () {
132                 for (unsigned i = 0; i < numMessagesPerProducer; ++i) {
133                     bool mustNotify = false;
134                     {
135                         std::unique_lock<LockType> locker(lock);
136                         wait(
137                             fullCondition, lock, locker,
138                             [&] () {
139                                 if (verbose)
140                                     dataLog(toString(Thread::current(), ": Checking production predicate with shouldContinue = ", shouldContinue, ", queue.size() == ", queue.size(), "\n"));
141                                 return queue.size() < maxQueueSize;
142                             });
143                         mustNotify = queue.isEmpty();
144                         queue.append(i);
145                     }
146                     notify(emptyCondition, mustNotify);
147                 }
148             }));
149     }
150
151     for (auto& thread : producerThreads)
152         thread->waitForCompletion();
153
154     {
155         std::lock_guard<LockType> locker(lock);
156         shouldContinue = false;
157     }
158     notifyAll(emptyCondition);
159
160     for (auto& thread : consumerThreads)
161         thread->waitForCompletion();
162
163     RELEASE_ASSERT(numProducers * numMessagesPerProducer == received.size());
164     std::sort(received.begin(), received.end());
165     for (unsigned messageIndex = 0; messageIndex < numMessagesPerProducer; ++messageIndex) {
166         for (unsigned producerIndex = 0; producerIndex < numProducers; ++producerIndex)
167             RELEASE_ASSERT(messageIndex == received[messageIndex * numProducers + producerIndex]);
168     }
169 }
170
171 template<typename LockType, typename ConditionType, typename NotifyFunctor, typename NotifyAllFunctor>
172 void runBenchmark(
173     const char* name,
174     const NotifyFunctor& notify,
175     const NotifyAllFunctor& notifyAll)
176 {
177     MonotonicTime before = MonotonicTime::now();
178     
179     runTest<LockType, ConditionType>(
180         numProducers,
181         numConsumers,
182         maxQueueSize,
183         numMessagesPerProducer,
184         notify,
185         notifyAll);
186
187     MonotonicTime after = MonotonicTime::now();
188
189     printf("%s: %.3lf ms.\n", name, (after - before).milliseconds());
190 }
191
192 } // anonymous namespace
193
194 int main(int argc, char** argv)
195 {
196     WTF::initializeThreading();
197
198     if (argc != 6
199         || sscanf(argv[2], "%u", &numProducers) != 1
200         || sscanf(argv[3], "%u", &numConsumers) != 1
201         || sscanf(argv[4], "%u", &maxQueueSize) != 1
202         || sscanf(argv[5], "%u", &numMessagesPerProducer) != 1)
203         usage();
204
205     bool didRun = false;
206     if (!strcmp(argv[1], "lock") || !strcmp(argv[1], "all")) {
207         runBenchmark<Lock, Condition>(
208             "WTF Lock NotifyOne",
209             [&] (Condition& condition, bool) {
210                 condition.notifyOne();
211             },
212             [&] (Condition& condition) {
213                 condition.notifyAll();
214             });
215         runBenchmark<Lock, Condition>(
216             "WTF Lock NotifyAll",
217             [&] (Condition& condition, bool mustNotify) {
218                 if (mustNotify)
219                     condition.notifyAll();
220             },
221             [&] (Condition& condition) {
222                 condition.notifyAll();
223             });
224         didRun = true;
225     }
226     if (!strcmp(argv[1], "mutex") || !strcmp(argv[1], "all")) {
227         runBenchmark<std::mutex, std::condition_variable>(
228             "std::mutex NotifyOne",
229             [&] (std::condition_variable& condition, bool) {
230                 condition.notify_one();
231             },
232             [&] (std::condition_variable& condition) {
233                 condition.notify_all();
234             });
235         runBenchmark<std::mutex, std::condition_variable>(
236             "std::mutex NotifyAll",
237             [&] (std::condition_variable& condition, bool mustNotify) {
238                 if (mustNotify)
239                     condition.notify_all();
240             },
241             [&] (std::condition_variable& condition) {
242                 condition.notify_all();
243             });
244         didRun = true;
245     }
246
247     if (!didRun)
248         usage();
249
250     return 0;
251 }
252