Commit f0d7b6d5 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook GitHub Bot

switch tests/benchmarks to folly native semaphore wrapper

Summary: On apple platforms, posix sem is deprecated. On windows, it does not exist. So switch to the native-semaphore wrapper.

Reviewed By: chadaustin

Differential Revision: D27903002

fbshipit-source-id: d53c7762fea3123cf67cda48fdd87489a0ee496c
parent 58a889d7
...@@ -23,8 +23,8 @@ ...@@ -23,8 +23,8 @@
#include <folly/futures/Promise.h> #include <folly/futures/Promise.h>
#include <folly/futures/test/TestExecutor.h> #include <folly/futures/test/TestExecutor.h>
#include <folly/portability/GFlags.h> #include <folly/portability/GFlags.h>
#include <folly/portability/Semaphore.h>
#include <folly/synchronization/Baton.h> #include <folly/synchronization/Baton.h>
#include <folly/synchronization/NativeSemaphore.h>
using namespace folly; using namespace folly;
...@@ -167,8 +167,7 @@ BENCHMARK_RELATIVE(contention) { ...@@ -167,8 +167,7 @@ BENCHMARK_RELATIVE(contention) {
std::vector<Promise<int>> promises(10000); std::vector<Promise<int>> promises(10000);
std::vector<Future<int>> futures; std::vector<Future<int>> futures;
std::thread producer, consumer; std::thread producer, consumer;
sem_t sem; folly::NativeSemaphore sem;
sem_init(&sem, 0, 0);
BENCHMARK_SUSPEND { BENCHMARK_SUSPEND {
folly::Baton<> b1, b2; folly::Baton<> b1, b2;
...@@ -179,7 +178,7 @@ BENCHMARK_RELATIVE(contention) { ...@@ -179,7 +178,7 @@ BENCHMARK_RELATIVE(contention) {
consumer = std::thread([&] { consumer = std::thread([&] {
b1.post(); b1.post();
for (auto& f : futures) { for (auto& f : futures) {
sem_wait(&sem); sem.wait();
std::move(f).then(incr<int>); std::move(f).then(incr<int>);
} }
}); });
...@@ -187,7 +186,7 @@ BENCHMARK_RELATIVE(contention) { ...@@ -187,7 +186,7 @@ BENCHMARK_RELATIVE(contention) {
producer = std::thread([&] { producer = std::thread([&] {
b2.post(); b2.post();
for (auto& p : promises) { for (auto& p : promises) {
sem_post(&sem); sem.post();
p.setValue(42); p.setValue(42);
} }
}); });
......
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
#include <thread> #include <thread>
#include <folly/Benchmark.h> #include <folly/Benchmark.h>
#include <folly/portability/Semaphore.h> #include <folly/synchronization/NativeSemaphore.h>
#include <folly/synchronization/test/BatonTestHelpers.h> #include <folly/synchronization/test/BatonTestHelpers.h>
#include <folly/test/DeterministicSchedule.h> #include <folly/test/DeterministicSchedule.h>
...@@ -46,22 +46,21 @@ BENCHMARK(baton_pingpong_emulated_futex_nonblocking, iters) { ...@@ -46,22 +46,21 @@ BENCHMARK(baton_pingpong_emulated_futex_nonblocking, iters) {
BENCHMARK_DRAW_LINE(); BENCHMARK_DRAW_LINE();
BENCHMARK(posix_sem_pingpong, iters) { BENCHMARK(native_sem_pingpong, iters) {
sem_t sems[3]; alignas(folly::hardware_destructive_interference_size)
sem_t* a = sems + 0; folly::NativeSemaphore a;
sem_t* b = sems + 2; // to get it on a different cache line alignas(folly::hardware_destructive_interference_size)
folly::NativeSemaphore b;
sem_init(a, 0, 0); auto thr = std::thread([&] {
sem_init(b, 0, 0);
auto thr = std::thread([=] {
for (size_t i = 0; i < iters; ++i) { for (size_t i = 0; i < iters; ++i) {
sem_wait(a); a.wait();
sem_post(b); b.post();
} }
}); });
for (size_t i = 0; i < iters; ++i) { for (size_t i = 0; i < iters; ++i) {
sem_post(a); a.post();
sem_wait(b); b.wait();
} }
thr.join(); thr.join();
} }
......
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
#include <folly/portability/Asm.h> #include <folly/portability/Asm.h>
#include <folly/portability/GFlags.h> #include <folly/portability/GFlags.h>
#include <folly/portability/GTest.h> #include <folly/portability/GTest.h>
#include <folly/portability/Semaphore.h> #include <folly/synchronization/NativeSemaphore.h>
#include <folly/test/DeterministicSchedule.h> #include <folly/test/DeterministicSchedule.h>
using namespace folly; using namespace folly;
...@@ -408,23 +408,19 @@ BENCHMARK(single_thread_lifo_trywait, iters) { ...@@ -408,23 +408,19 @@ BENCHMARK(single_thread_lifo_trywait, iters) {
} }
} }
BENCHMARK(single_thread_posix_postwait, iters) { BENCHMARK(single_thread_native_postwait, iters) {
sem_t sem; folly::NativeSemaphore sem;
EXPECT_EQ(sem_init(&sem, 0, 0), 0);
for (size_t n = 0; n < iters; ++n) { for (size_t n = 0; n < iters; ++n) {
EXPECT_EQ(sem_post(&sem), 0); sem.post();
EXPECT_EQ(sem_wait(&sem), 0); sem.wait();
} }
EXPECT_EQ(sem_destroy(&sem), 0);
} }
BENCHMARK(single_thread_posix_trywait, iters) { BENCHMARK(single_thread_native_trywait, iters) {
sem_t sem; folly::NativeSemaphore sem;
EXPECT_EQ(sem_init(&sem, 0, 0), 0);
for (size_t n = 0; n < iters; ++n) { for (size_t n = 0; n < iters; ++n) {
EXPECT_EQ(sem_trywait(&sem), -1); EXPECT_FALSE(sem.try_wait());
} }
EXPECT_EQ(sem_destroy(&sem), 0);
} }
static void contendedUse(uint32_t n, int posters, int waiters) { static void contendedUse(uint32_t n, int posters, int waiters) {
...@@ -482,8 +478,8 @@ BENCHMARK_NAMED_PARAM(contendedUse, 32_to_1000, 32, 1000) ...@@ -482,8 +478,8 @@ BENCHMARK_NAMED_PARAM(contendedUse, 32_to_1000, 32, 1000)
// single_thread_lifo_wait 13.60ns 73.53M // single_thread_lifo_wait 13.60ns 73.53M
// single_thread_lifo_postwait 29.43ns 33.98M // single_thread_lifo_postwait 29.43ns 33.98M
// single_thread_lifo_trywait 677.69ps 1.48G // single_thread_lifo_trywait 677.69ps 1.48G
// single_thread_posix_postwait 25.03ns 39.95M // single_thread_native_postwait 25.03ns 39.95M
// single_thread_posix_trywait 7.30ns 136.98M // single_thread_native_trywait 7.30ns 136.98M
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// contendedUse(1_to_1) 158.22ns 6.32M // contendedUse(1_to_1) 158.22ns 6.32M
// contendedUse(1_to_4) 574.73ns 1.74M // contendedUse(1_to_4) 574.73ns 1.74M
......
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