Ugh. Once again, unreviewed, roll out unintentional commit in r202790.
[WebKit-https.git] / Source / WTF / benchmarks / LockFairnessTest.cpp
1 /*
2  * Copyright (C) 2016 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/benchmarks -LWebKitBuild/Release -lWTF -framework Foundation -licucore -std=c++11 -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/CurrentTime.h>
36 #include <wtf/DataLog.h>
37 #include <wtf/HashMap.h>
38 #include <wtf/Lock.h>
39 #include <wtf/ParkingLot.h>
40 #include <wtf/StdLibExtras.h>
41 #include <wtf/Threading.h>
42 #include <wtf/ThreadingPrimitives.h>
43 #include <wtf/Vector.h>
44 #include <wtf/WordLock.h>
45 #include <wtf/text/CString.h>
46
47 namespace {
48
49 NO_RETURN void usage()
50 {
51     printf("Usage: LockFairnessTest yieldspinlock|pausespinlock|wordlock|lock|barginglock|bargingwordlock|thunderlock|thunderwordlock|cascadelock|cascadewordlockhandofflock|mutex|all <num threads> <seconds per test>\n");
52     exit(1);
53 }
54
55 unsigned numThreads;
56 double secondsPerTest;
57
58 struct Benchmark {
59     template<typename LockType>
60     static void run(const char* name)
61     {
62         LockType lock;
63         std::unique_ptr<unsigned[]> counts = std::make_unique<unsigned[]>(numThreads);
64         std::unique_ptr<ThreadIdentifier[]> threads = std::make_unique<ThreadIdentifier[]>(numThreads);
65     
66         volatile bool keepGoing = true;
67     
68         lock.lock();
69     
70         for (unsigned threadIndex = numThreads; threadIndex--;) {
71             counts[threadIndex] = 0;
72             threads[threadIndex] = createThread(
73                 "Benchmark Thread",
74                 [&, threadIndex] () {
75                     while (keepGoing) {
76                         lock.lock();
77                         counts[threadIndex]++;
78                         lock.unlock();
79                     }
80                 });
81         }
82     
83         sleepMS(100);
84         lock.unlock();
85     
86         sleep(secondsPerTest);
87     
88         lock.lock();
89         keepGoing = false;
90     
91         dataLog(name, ": ");
92         CommaPrinter comma;
93         for (unsigned threadIndex = numThreads; threadIndex--;)
94             dataLog(comma, counts[threadIndex]);
95         dataLog("\n");
96     
97         lock.unlock();
98         for (unsigned threadIndex = numThreads; threadIndex--;)
99             waitForThreadCompletion(threads[threadIndex]);
100     }
101 };
102
103 } // anonymous namespace
104
105 int main(int argc, char** argv)
106 {
107     WTF::initializeThreading();
108     
109     if (argc != 4
110         || sscanf(argv[2], "%u", &numThreads) != 1
111         || sscanf(argv[3], "%lf", &secondsPerTest) != 1)
112         usage();
113     
114     runEverything<Benchmark>(argv[1]);
115     
116     return 0;
117 }