[WTF] Move currentCPUTime and sleep(Seconds) to CPUTime.h and Seconds.h respectively
[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/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|unfairlock|mutex|all <num threads> <seconds per test> <microseconds in critical section>\n");
52     exit(1);
53 }
54
55 unsigned numThreads;
56 double secondsPerTest;
57 unsigned microsecondsInCriticalSection;
58
59 struct Benchmark {
60     template<typename LockType>
61     static void run(const char* name)
62     {
63         LockType lock;
64         std::unique_ptr<unsigned[]> counts = std::make_unique<unsigned[]>(numThreads);
65         std::unique_ptr<RefPtr<Thread>[]> threads = std::make_unique<RefPtr<Thread>[]>(numThreads);
66     
67         volatile bool keepGoing = true;
68     
69         lock.lock();
70     
71         for (unsigned threadIndex = numThreads; threadIndex--;) {
72             counts[threadIndex] = 0;
73             threads[threadIndex] = Thread::create(
74                 "Benchmark Thread",
75                 [&, threadIndex] () {
76                     if (!microsecondsInCriticalSection) {
77                         while (keepGoing) {
78                             lock.lock();
79                             counts[threadIndex]++;
80                             lock.unlock();
81                         }
82                         return;
83                     }
84                     
85                     while (keepGoing) {
86                         lock.lock();
87                         counts[threadIndex]++;
88                         usleep(microsecondsInCriticalSection);
89                         lock.unlock();
90                     }
91                 });
92         }
93     
94         sleep(100_ms);
95         lock.unlock();
96     
97         sleep(Seconds { secondsPerTest });
98     
99         keepGoing = false;
100         lock.lock();
101     
102         dataLog(name, ": ");
103         CommaPrinter comma;
104         for (unsigned threadIndex = numThreads; threadIndex--;)
105             dataLog(comma, counts[threadIndex]);
106         dataLog("\n");
107     
108         lock.unlock();
109         for (unsigned threadIndex = numThreads; threadIndex--;)
110             threads[threadIndex]->waitForCompletion();
111     }
112 };
113
114 } // anonymous namespace
115
116 int main(int argc, char** argv)
117 {
118     WTF::initializeThreading();
119     
120     if (argc != 5
121         || sscanf(argv[2], "%u", &numThreads) != 1
122         || sscanf(argv[3], "%lf", &secondsPerTest) != 1
123         || sscanf(argv[4], "%u", &microsecondsInCriticalSection) != 1)
124         usage();
125     
126     runEverything<Benchmark>(argv[1]);
127     
128     return 0;
129 }