Commit 49838bbf authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Avoid measuring thread::join in UnboundedQueue benchmark

Summary:
[Folly] Avoid measuring `thread::join` in `UnboundedQueue` benchmark.

Use a single `barrier` which also replaces the technique which avoids measuring thread start.

Reviewed By: magedm

Differential Revision: D13192004

fbshipit-source-id: 9b86bed2efb110dc863994e1114b4e7c40f9f225
parent c892be25
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include <folly/ProducerConsumerQueue.h> #include <folly/ProducerConsumerQueue.h>
#include <folly/portability/GTest.h> #include <folly/portability/GTest.h>
#include <boost/thread/barrier.hpp>
#include <glog/logging.h> #include <glog/logging.h>
#include <atomic> #include <atomic>
...@@ -161,18 +162,16 @@ inline uint64_t run_once( ...@@ -161,18 +162,16 @@ inline uint64_t run_once(
const ProdFunc& prodFn, const ProdFunc& prodFn,
const ConsFunc& consFn, const ConsFunc& consFn,
const EndFunc& endFn) { const EndFunc& endFn) {
std::atomic<bool> start{false}; boost::barrier barrier(1 + nprod + ncons);
std::atomic<int> ready{0};
/* producers */ /* producers */
std::vector<std::thread> prodThr(nprod); std::vector<std::thread> prodThr(nprod);
for (int tid = 0; tid < nprod; ++tid) { for (int tid = 0; tid < nprod; ++tid) {
prodThr[tid] = std::thread([&, tid] { prodThr[tid] = std::thread([&, tid] {
++ready; barrier.wait(); // A - wait for thread start
while (!start.load()) { barrier.wait(); // B - init the work
/* spin */;
}
prodFn(tid); prodFn(tid);
barrier.wait(); // C - join the work
}); });
} }
...@@ -180,22 +179,23 @@ inline uint64_t run_once( ...@@ -180,22 +179,23 @@ inline uint64_t run_once(
std::vector<std::thread> consThr(ncons); std::vector<std::thread> consThr(ncons);
for (int tid = 0; tid < ncons; ++tid) { for (int tid = 0; tid < ncons; ++tid) {
consThr[tid] = std::thread([&, tid] { consThr[tid] = std::thread([&, tid] {
++ready; barrier.wait(); // A - wait for thread start
while (!start.load()) { barrier.wait(); // B - init the work
/* spin */;
}
consFn(tid); consFn(tid);
barrier.wait(); // C - join the work
}); });
} }
/* wait for all producers and consumers to be ready */ barrier.wait(); // A - wait for thread start
while (ready.load() < (nprod + ncons)) {
/* spin */;
}
/* begin time measurement */ /* begin time measurement */
auto tbegin = std::chrono::steady_clock::now(); auto const tbegin = std::chrono::steady_clock::now();
start.store(true);
barrier.wait(); // B - init the work
barrier.wait(); // C - join the work
/* end time measurement */
auto const tend = std::chrono::steady_clock::now();
/* wait for completion */ /* wait for completion */
for (int i = 0; i < nprod; ++i) { for (int i = 0; i < nprod; ++i) {
...@@ -205,11 +205,9 @@ inline uint64_t run_once( ...@@ -205,11 +205,9 @@ inline uint64_t run_once(
consThr[i].join(); consThr[i].join();
} }
/* end time measurement */
auto tend = std::chrono::steady_clock::now();
endFn(); endFn();
return std::chrono::duration_cast<std::chrono::nanoseconds>(tend - tbegin) auto const dur = tend - tbegin;
.count(); return std::chrono::duration_cast<std::chrono::nanoseconds>(dur).count();
} }
template <bool SingleProducer, bool SingleConsumer, bool MayBlock> template <bool SingleProducer, bool SingleConsumer, bool MayBlock>
......
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