d7fceda834696a569c846a04d99760e0f35d16b7
[WebKit-https.git] / Source / WTF / benchmarks / LockFairnessTest.cpp
1 /*
2  * Copyright (C) 2016-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 LockFairnessTest Source/WTF/benchmarks/LockFairnessTest.cpp -O3 -W -ISource/WTF -ISource/WTF/icu -ISource/WTF/benchmarks -LWebKitBuild/Release -lWTF -framework Foundation -licucore -std=c++14 -fvisibility=hidden
28
29 #include "config.h"
30
31 #include "ToyLocks.h"
32 #include <thread>
33 #include <unistd.h>
34 #include <wtf/CommaPrinter.h>
35 #include <wtf/Compiler.h>
36 #include <wtf/CurrentTime.h>
37 #include <wtf/DataLog.h>
38 #include <wtf/HashMap.h>
39 #include <wtf/Lock.h>
40 #include <wtf/ParkingLot.h>
41 #include <wtf/StdLibExtras.h>
42 #include <wtf/Threading.h>
43 #include <wtf/ThreadingPrimitives.h>
44 #include <wtf/Vector.h>
45 #include <wtf/WordLock.h>
46 #include <wtf/text/CString.h>
47
48 namespace {
49
50 NO_RETURN void usage()
51 {
52     printf("Usage: LockFairnessTest yieldspinlock|pausespinlock|wordlock|lock|barginglock|bargingwordlock|thunderlock|thunderwordlock|cascadelock|cascadewordlockhandofflock|unfairlock|mutex|all <num threads> <seconds per test> <microseconds in critical section>\n");
53     exit(1);
54 }
55
56 unsigned numThreads;
57 double secondsPerTest;
58 unsigned microsecondsInCriticalSection;
59
60 struct Benchmark {
61     template<typename LockType>
62     static void run(const char* name)
63     {
64         LockType lock;
65         std::unique_ptr<unsigned[]> counts = std::make_unique<unsigned[]>(numThreads);
66         std::unique_ptr<RefPtr<Thread>[]> threads = std::make_unique<RefPtr<Thread>[]>(numThreads);
67     
68         volatile bool keepGoing = true;
69     
70         lock.lock();
71     
72         for (unsigned threadIndex = numThreads; threadIndex--;) {
73             counts[threadIndex] = 0;
74             threads[threadIndex] = Thread::create(
75                 "Benchmark Thread",
76                 [&, threadIndex] () {
77                     if (!microsecondsInCriticalSection) {
78                         while (keepGoing) {
79                             lock.lock();
80                             counts[threadIndex]++;
81                             lock.unlock();
82                         }
83                         return;
84                     }
85                     
86                     while (keepGoing) {
87                         lock.lock();
88                         counts[threadIndex]++;
89                         usleep(microsecondsInCriticalSection);
90                         lock.unlock();
91                     }
92                 });
93         }
94     
95         sleep(100_ms);
96         lock.unlock();
97     
98         sleep(Seconds { secondsPerTest });
99     
100         keepGoing = false;
101         lock.lock();
102     
103         dataLog(name, ": ");
104         CommaPrinter comma;
105         for (unsigned threadIndex = numThreads; threadIndex--;)
106             dataLog(comma, counts[threadIndex]);
107         dataLog("\n");
108     
109         lock.unlock();
110         for (unsigned threadIndex = numThreads; threadIndex--;)
111             threads[threadIndex]->waitForCompletion();
112     }
113 };
114
115 } // anonymous namespace
116
117 int main(int argc, char** argv)
118 {
119     WTF::initializeThreading();
120     
121     if (argc != 5
122         || sscanf(argv[2], "%u", &numThreads) != 1
123         || sscanf(argv[3], "%lf", &secondsPerTest) != 1
124         || sscanf(argv[4], "%u", &microsecondsInCriticalSection) != 1)
125         usage();
126     
127     runEverything<Benchmark>(argv[1]);
128     
129     return 0;
130 }