Commit 88324bac authored by Dan Melnic's avatar Dan Melnic Committed by Facebook Github Bot

Add SpinLock from google/base/spinlock.h to the small lock benchmark

Summary: Add SpinLock from google/base/spinlock.h to the small lock benchmark

Reviewed By: yfeldblum

Differential Revision: D9771633

fbshipit-source-id: 3cf01866ec6cd4a4a5d8c2fb4274ca20a6f545dd
parent 51991ebe
...@@ -21,6 +21,8 @@ ...@@ -21,6 +21,8 @@
#include <thread> #include <thread>
#include <vector> #include <vector>
#include <google/base/spinlock.h>
#include <folly/Benchmark.h> #include <folly/Benchmark.h>
#include <folly/synchronization/SmallLocks.h> #include <folly/synchronization/SmallLocks.h>
...@@ -81,6 +83,19 @@ class InitLock { ...@@ -81,6 +83,19 @@ class InitLock {
} }
}; };
class GoogleSpinLockAdapter {
public:
void lock() {
lock_.Lock();
}
void unlock() {
lock_.Unlock();
}
private:
SpinLock lock_;
};
template <typename Lock> template <typename Lock>
static void runContended(size_t numOps, size_t numThreads) { static void runContended(size_t numOps, size_t numThreads) {
folly::BenchmarkSuspender braces; folly::BenchmarkSuspender braces;
...@@ -238,6 +253,14 @@ BENCHMARK(StdMutexUncontendedBenchmark, iters) { ...@@ -238,6 +253,14 @@ BENCHMARK(StdMutexUncontendedBenchmark, iters) {
} }
} }
BENCHMARK(GoogleSpinUncontendedBenchmark, iters) {
SpinLock lock;
while (iters--) {
lock.Lock();
lock.Unlock();
}
}
BENCHMARK(MicroSpinLockUncontendedBenchmark, iters) { BENCHMARK(MicroSpinLockUncontendedBenchmark, iters) {
folly::MicroSpinLock lock; folly::MicroSpinLock lock;
lock.init(); lock.init();
...@@ -299,6 +322,9 @@ BENCHMARK_DRAW_LINE(); ...@@ -299,6 +322,9 @@ BENCHMARK_DRAW_LINE();
static void std_mutex(size_t numOps, size_t numThreads) { static void std_mutex(size_t numOps, size_t numThreads) {
runContended<std::mutex>(numOps, numThreads); runContended<std::mutex>(numOps, numThreads);
} }
static void google_spin(size_t numOps, size_t numThreads) {
runContended<GoogleSpinLockAdapter>(numOps, numThreads);
}
static void folly_microspin(size_t numOps, size_t numThreads) { static void folly_microspin(size_t numOps, size_t numThreads) {
runContended<InitLock<folly::MicroSpinLock>>(numOps, numThreads); runContended<InitLock<folly::MicroSpinLock>>(numOps, numThreads);
} }
...@@ -311,36 +337,43 @@ static void folly_microlock(size_t numOps, size_t numThreads) { ...@@ -311,36 +337,43 @@ static void folly_microlock(size_t numOps, size_t numThreads) {
BENCHMARK_DRAW_LINE(); BENCHMARK_DRAW_LINE();
BENCH_BASE(std_mutex, 1thread, 1) BENCH_BASE(std_mutex, 1thread, 1)
BENCH_BASE(google_spin, 1thread, 1)
BENCH_REL(folly_microspin, 1thread, 1) BENCH_REL(folly_microspin, 1thread, 1)
BENCH_REL(folly_picospin, 1thread, 1) BENCH_REL(folly_picospin, 1thread, 1)
BENCH_REL(folly_microlock, 1thread, 1) BENCH_REL(folly_microlock, 1thread, 1)
BENCHMARK_DRAW_LINE(); BENCHMARK_DRAW_LINE();
BENCH_BASE(std_mutex, 2thread, 2) BENCH_BASE(std_mutex, 2thread, 2)
BENCH_REL(google_spin, 2thread, 2)
BENCH_REL(folly_microspin, 2thread, 2) BENCH_REL(folly_microspin, 2thread, 2)
BENCH_REL(folly_picospin, 2thread, 2) BENCH_REL(folly_picospin, 2thread, 2)
BENCH_REL(folly_microlock, 2thread, 2) BENCH_REL(folly_microlock, 2thread, 2)
BENCHMARK_DRAW_LINE(); BENCHMARK_DRAW_LINE();
BENCH_BASE(std_mutex, 4thread, 4) BENCH_BASE(std_mutex, 4thread, 4)
BENCH_REL(google_spin, 4thread, 4)
BENCH_REL(folly_microspin, 4thread, 4) BENCH_REL(folly_microspin, 4thread, 4)
BENCH_REL(folly_picospin, 4thread, 4) BENCH_REL(folly_picospin, 4thread, 4)
BENCH_REL(folly_microlock, 4thread, 4) BENCH_REL(folly_microlock, 4thread, 4)
BENCHMARK_DRAW_LINE(); BENCHMARK_DRAW_LINE();
BENCH_BASE(std_mutex, 8thread, 8) BENCH_BASE(std_mutex, 8thread, 8)
BENCH_REL(google_spin, 8thread, 8)
BENCH_REL(folly_microspin, 8thread, 8) BENCH_REL(folly_microspin, 8thread, 8)
BENCH_REL(folly_picospin, 8thread, 8) BENCH_REL(folly_picospin, 8thread, 8)
BENCH_REL(folly_microlock, 8thread, 8) BENCH_REL(folly_microlock, 8thread, 8)
BENCHMARK_DRAW_LINE(); BENCHMARK_DRAW_LINE();
BENCH_BASE(std_mutex, 16thread, 16) BENCH_BASE(std_mutex, 16thread, 16)
BENCH_REL(google_spin, 16thread, 16)
BENCH_REL(folly_microspin, 16thread, 16) BENCH_REL(folly_microspin, 16thread, 16)
BENCH_REL(folly_picospin, 16thread, 16) BENCH_REL(folly_picospin, 16thread, 16)
BENCH_REL(folly_microlock, 16thread, 16) BENCH_REL(folly_microlock, 16thread, 16)
BENCHMARK_DRAW_LINE(); BENCHMARK_DRAW_LINE();
BENCH_BASE(std_mutex, 32thread, 32) BENCH_BASE(std_mutex, 32thread, 32)
BENCH_REL(google_spin, 32thread, 32)
BENCH_REL(folly_microspin, 32thread, 32) BENCH_REL(folly_microspin, 32thread, 32)
BENCH_REL(folly_picospin, 32thread, 32) BENCH_REL(folly_picospin, 32thread, 32)
BENCH_REL(folly_microlock, 32thread, 32) BENCH_REL(folly_microlock, 32thread, 32)
BENCHMARK_DRAW_LINE(); BENCHMARK_DRAW_LINE();
BENCH_BASE(std_mutex, 64thread, 64) BENCH_BASE(std_mutex, 64thread, 64)
BENCH_REL(google_spin, 64thread, 64)
BENCH_REL(folly_microspin, 64thread, 64) BENCH_REL(folly_microspin, 64thread, 64)
BENCH_REL(folly_picospin, 64thread, 64) BENCH_REL(folly_picospin, 64thread, 64)
BENCH_REL(folly_microlock, 64thread, 64) BENCH_REL(folly_microlock, 64thread, 64)
...@@ -355,6 +388,7 @@ int main(int argc, char** argv) { ...@@ -355,6 +388,7 @@ int main(int argc, char** argv) {
gflags::ParseCommandLineFlags(&argc, &argv, true); gflags::ParseCommandLineFlags(&argc, &argv, true);
FairnessTest(std::mutex); FairnessTest(std::mutex);
FairnessTest(GoogleSpinLockAdapter);
FairnessTest(InitLock<folly::MicroSpinLock>); FairnessTest(InitLock<folly::MicroSpinLock>);
FairnessTest(InitLock<folly::PicoSpinLock<uint16_t>>); FairnessTest(InitLock<folly::PicoSpinLock<uint16_t>>);
FairnessTest(folly::MicroLock); FairnessTest(folly::MicroLock);
...@@ -365,64 +399,75 @@ int main(int argc, char** argv) { ...@@ -365,64 +399,75 @@ int main(int argc, char** argv) {
} }
/* /*
locks_benchmark --bm_min_iters=100000 ./small_locks_benchmark --bm_min_iters=100000
56-core Intel(R) Xeon(R) CPU E5-2680 v4 @ 2.40GHz Intel(R) Xeon(R) CPU E5-2680 v4 @ 2.40GHz
std::mutex: std::mutex:
Sum: 3768590 Mean: 67296 stddev: 1318 Sum: 3108396 Mean: 55507 stddev: 381
Lock time stats in us: mean 15 stddev 1140 max 8002 Lock time stats in us: mean 18 stddev 1382 max 11421
GLock<SpinLock>:
Sum: 3975385 Mean: 70989 stddev: 4719
Lock time stats in us: mean 11 stddev 1080 max 11956
InitLock<folly::MicroSpinLock>: InitLock<folly::MicroSpinLock>:
Sum: 3548900 Mean: 63373 stddev: 31657 Sum: 2827915 Mean: 50498 stddev: 21612
Lock time stats in us: mean 30 stddev 1210 max 287010 Lock time stats in us: mean 38 stddev 1518 max 575420
InitLock<folly::PicoSpinLock<uint16_t>>: InitLock<folly::PicoSpinLock<uint16_t>>:
Sum: 1051996 Mean: 18785 stddev: 3026 Sum: 1018989 Mean: 18196 stddev: 13122
Lock time stats in us: mean 104 stddev 4082 max 376820 Lock time stats in us: mean 107 stddev 4214 max 414541
folly::MicroLock: folly::MicroLock:
Sum: 1871779 Mean: 33424 stddev: 10311 Sum: 1733925 Mean: 30962 stddev: 9727
Lock time stats in us: mean 47 stddev 2294 max 20486 Lock time stats in us: mean 51 stddev 2476 max 14267
============================================================================ ============================================================================
folly/synchronization/test/SmallLocksBenchmark.cpprelative time/iter iters/s folly/synchronization/test/SmallLocksBenchmark.cpprelative time/iter iters/s
============================================================================ ============================================================================
StdMutexUncontendedBenchmark 16.73ns 59.78M StdMutexUncontendedBenchmark 23.00ns 43.47M
MicroSpinLockUncontendedBenchmark 10.03ns 99.67M GoogleSpinUncontendedBenchmark 15.47ns 64.65M
PicoSpinLockUncontendedBenchmark 11.25ns 88.90M MicroSpinLockUncontendedBenchmark 13.80ns 72.48M
MicroLockUncontendedBenchmark 21.59ns 46.32M PicoSpinLockUncontendedBenchmark 15.47ns 64.65M
VirtualFunctionCall 76.02ps 13.15G MicroLockUncontendedBenchmark 29.70ns 33.67M
VirtualFunctionCall 104.66ps 9.55G
---------------------------------------------------------------------------- ----------------------------------------------------------------------------
---------------------------------------------------------------------------- ----------------------------------------------------------------------------
std_mutex(1thread) 655.60ns 1.53M std_mutex(1thread) 855.49ns 1.17M
folly_microspin(1thread) 96.79% 677.31ns 1.48M google_spin(1thread) 919.71ns 1.09M
folly_picospin(1thread) 118.13% 554.96ns 1.80M folly_microspin(1thread) 100.07% 919.12ns 1.09M
folly_microlock(1thread) 100.04% 655.31ns 1.53M folly_picospin(1thread) 99.08% 928.24ns 1.08M
folly_microlock(1thread) 108.74% 845.77ns 1.18M
---------------------------------------------------------------------------- ----------------------------------------------------------------------------
std_mutex(2thread) 1.22us 816.56K std_mutex(2thread) 1.35us 739.03K
folly_microspin(2thread) 146.21% 837.59ns 1.19M google_spin(2thread) 123.06% 1.10us 909.43K
folly_picospin(2thread) 168.33% 727.51ns 1.37M folly_microspin(2thread) 130.02% 1.04us 960.87K
folly_microlock(2thread) 136.43% 897.65ns 1.11M folly_picospin(2thread) 129.37% 1.05us 956.10K
folly_microlock(2thread) 122.24% 1.11us 903.42K
---------------------------------------------------------------------------- ----------------------------------------------------------------------------
std_mutex(4thread) 2.56us 390.99K std_mutex(4thread) 2.96us 338.26K
folly_microspin(4thread) 128.05% 2.00us 500.67K google_spin(4thread) 130.26% 2.27us 440.63K
folly_picospin(4thread) 117.95% 2.17us 461.18K folly_microspin(4thread) 124.37% 2.38us 420.71K
folly_microlock(4thread) 101.01% 2.53us 394.94K folly_picospin(4thread) 139.97% 2.11us 473.46K
folly_microlock(4thread) 109.60% 2.70us 370.75K
---------------------------------------------------------------------------- ----------------------------------------------------------------------------
std_mutex(8thread) 5.59us 179.01K std_mutex(8thread) 6.27us 159.56K
folly_microspin(8thread) 119.13% 4.69us 213.26K google_spin(8thread) 121.56% 5.16us 193.95K
folly_picospin(8thread) 82.00% 6.81us 146.79K folly_microspin(8thread) 112.33% 5.58us 179.23K
folly_microlock(8thread) 84.50% 6.61us 151.26K folly_picospin(8thread) 89.25% 7.02us 142.41K
folly_microlock(8thread) 77.43% 8.09us 123.55K
---------------------------------------------------------------------------- ----------------------------------------------------------------------------
std_mutex(16thread) 11.52us 86.80K std_mutex(16thread) 12.86us 77.77K
folly_microspin(16thread) 98.18% 11.73us 85.22K google_spin(16thread) 114.33% 11.25us 88.91K
folly_picospin(16thread) 61.18% 18.83us 53.10K folly_microspin(16thread) 104.78% 12.27us 81.49K
folly_microlock(16thread) 51.93% 22.19us 45.07K folly_picospin(16thread) 43.77% 29.38us 34.04K
folly_microlock(16thread) 52.30% 24.59us 40.68K
---------------------------------------------------------------------------- ----------------------------------------------------------------------------
std_mutex(32thread) 31.91us 31.34K std_mutex(32thread) 35.31us 28.32K
folly_microspin(32thread) 101.74% 31.37us 31.88K google_spin(32thread) 122.32% 28.87us 34.64K
folly_picospin(32thread) 32.43% 98.39us 10.16K folly_microspin(32thread) 96.66% 36.53us 27.37K
folly_microlock(32thread) 56.35% 56.63us 17.66K folly_picospin(32thread) 32.20% 109.66us 9.12K
folly_microlock(32thread) 55.95% 63.11us 15.84K
---------------------------------------------------------------------------- ----------------------------------------------------------------------------
std_mutex(64thread) 36.77us 27.20K std_mutex(64thread) 41.84us 23.90K
folly_microspin(64thread) 102.25% 35.96us 27.81K google_spin(64thread) 126.88% 32.97us 30.33K
folly_picospin(64thread) 32.30% 113.83us 8.78K folly_microspin(64thread) 99.29% 42.14us 23.73K
folly_microlock(64thread) 55.54% 66.21us 15.10K folly_picospin(64thread) 31.63% 132.26us 7.56K
folly_microlock(64thread) 57.99% 72.15us 13.86K
============================================================================ ============================================================================
*/ */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment