diff options
author | Henning Baldersheim <balder@oath.com> | 2018-08-28 15:34:32 +0200 |
---|---|---|
committer | Henning Baldersheim <balder@oath.com> | 2018-08-28 15:34:32 +0200 |
commit | 9ae2e92efcfa3a95a6bd988dc7e34d7c84350289 (patch) | |
tree | c2258436828f9ca8768bd3c43fef6d14109015e1 /fnet/src/tests/locking/lockspeed.cpp | |
parent | 7b6562b9b1e9da14386118826e7936ff5b8b5788 (diff) | |
parent | 2a606fb630519aaca8885a46b9b2d6b20c99afd2 (diff) |
Merge branch 'balder/update-attributes' of github.com:vespa-engine/vespa into balder/update-attributes
Diffstat (limited to 'fnet/src/tests/locking/lockspeed.cpp')
-rw-r--r-- | fnet/src/tests/locking/lockspeed.cpp | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/fnet/src/tests/locking/lockspeed.cpp b/fnet/src/tests/locking/lockspeed.cpp index b85777f264c..aa4f6649fd9 100644 --- a/fnet/src/tests/locking/lockspeed.cpp +++ b/fnet/src/tests/locking/lockspeed.cpp @@ -3,14 +3,30 @@ #include <vespa/fnet/fnet.h> #include "dummy.h" +class SpinLock { +private: + std::atomic_flag _lock; +public: + SpinLock() : _lock(false) {} + void lock() { + while (_lock.test_and_set(std::memory_order_acquire)) { + // spin + } + } + void unlock() { _lock.clear(std::memory_order_release); } +}; + TEST("lock speed") { FastOS_Time start; FastOS_Time stop; DummyLock dummy; std::mutex lock; + SpinLock spin; double dummyTime; double actualTime; double overhead; + double spin_overhead; + double spin_factor; uint32_t i; start.SetNow(); @@ -77,10 +93,53 @@ TEST("lock speed") { overhead = (actualTime - dummyTime) / 10000.0; + start.SetNow(); + for (i = 0; i < 1000000; i++) { + spin.lock(); + spin.unlock(); + spin.lock(); + spin.unlock(); + spin.lock(); + spin.unlock(); + spin.lock(); + spin.unlock(); + spin.lock(); + spin.unlock(); + spin.lock(); + spin.unlock(); + spin.lock(); + spin.unlock(); + spin.lock(); + spin.unlock(); + spin.lock(); + spin.unlock(); + spin.lock(); + spin.unlock(); + } + stop.SetNow(); + stop -= start; + actualTime = stop.MilliSecs(); + + fprintf(stderr, + "10M actual (spin) lock/unlock: %f ms (%1.2f/ms)\n", + stop.MilliSecs(), 10000000.0 / stop.MilliSecs()); + + spin_overhead = (actualTime - dummyTime) / 10000.0; + + spin_factor = overhead / spin_overhead; + fprintf(stderr, "approx overhead per lock/unlock: %f microseconds\n", overhead); + fprintf(stderr, + "approx overhead per lock/unlock (spin): %f microseconds\n", + spin_overhead); + + fprintf(stderr, + "spinlocks are %f times faster\n", + spin_factor); + //--------------------------------------------------------------------------- start.SetNow(); |