Commit b782340c authored by Maged Michael's avatar Maged Michael Committed by Facebook Github Bot

UnboundedQueue: Add LgAlign template parameter - Refactor code

Summary:
- Add a template parameter, LgAlign, to control memory usage. The parameter is used in DynamicBoundedQueue.
- Refactor code.

Reviewed By: yfeldblum

Differential Revision: D6508015

fbshipit-source-id: 6e17b1d8fd900595147dc4217e04d379a13fbdf8
parent 35054c8b
This diff is collapsed.
This diff is collapsed.
/* /*
* Copyright 2017 Facebook, Inc. * Copyright 2017-present Facebook, Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
DEFINE_bool(bench, false, "run benchmark"); DEFINE_bool(bench, false, "run benchmark");
DEFINE_int32(reps, 10, "number of reps"); DEFINE_int32(reps, 10, "number of reps");
DEFINE_int32(ops, 1000000, "number of operations per rep"); DEFINE_int32(ops, 1000000, "number of operations per rep");
DEFINE_int64(capacity, 256 * 1024, "capacity");
template <typename T, bool MayBlock> template <typename T, bool MayBlock>
using USPSC = folly::USPSCQueue<T, MayBlock>; using USPSC = folly::USPSCQueue<T, MayBlock>;
...@@ -81,7 +82,7 @@ TEST(UnboundedQueue, basic) { ...@@ -81,7 +82,7 @@ TEST(UnboundedQueue, basic) {
template <template <typename, bool> class Q, bool MayBlock> template <template <typename, bool> class Q, bool MayBlock>
void timeout_test() { void timeout_test() {
Q<int, MayBlock> q; Q<int, MayBlock> q;
int v = -1; int v;
ASSERT_FALSE(q.try_dequeue_until( ASSERT_FALSE(q.try_dequeue_until(
v, std::chrono::steady_clock::now() + std::chrono::microseconds(1))); v, std::chrono::steady_clock::now() + std::chrono::microseconds(1)));
ASSERT_FALSE(q.try_dequeue_for(v, std::chrono::microseconds(1))); ASSERT_FALSE(q.try_dequeue_for(v, std::chrono::microseconds(1)));
...@@ -189,9 +190,8 @@ void enq_deq_test(const int nprod, const int ncons) { ...@@ -189,9 +190,8 @@ void enq_deq_test(const int nprod, const int ncons) {
/* keep trying */; /* keep trying */;
} }
} else if ((i % 3) == 1) { } else if ((i % 3) == 1) {
std::chrono::steady_clock::time_point deadline = auto duration = std::chrono::milliseconds(1);
std::chrono::steady_clock::now() + std::chrono::milliseconds(1); while (!q.try_dequeue_for(v, duration)) {
while (!q.try_dequeue_until(v, deadline)) {
/* keep trying */; /* keep trying */;
} }
} else { } else {
...@@ -293,8 +293,6 @@ uint64_t bench(const int nprod, const int ncons, const std::string& name) { ...@@ -293,8 +293,6 @@ uint64_t bench(const int nprod, const int ncons, const std::string& name) {
} }
}; };
auto cons = [&](int tid) { auto cons = [&](int tid) {
std::chrono::steady_clock::time_point deadline =
std::chrono::steady_clock::now() + std::chrono::hours(24);
uint64_t mysum = 0; uint64_t mysum = 0;
for (int i = tid; i < ops; i += ncons) { for (int i = tid; i < ops; i += ncons) {
T v; T v;
...@@ -303,7 +301,8 @@ uint64_t bench(const int nprod, const int ncons, const std::string& name) { ...@@ -303,7 +301,8 @@ uint64_t bench(const int nprod, const int ncons, const std::string& name) {
/* keep trying */; /* keep trying */;
} }
} else if (Op == 1 || Op == 4) { } else if (Op == 1 || Op == 4) {
while (UNLIKELY(!q.try_dequeue_until(v, deadline))) { auto duration = std::chrono::microseconds(1000);
while (UNLIKELY(!q.try_dequeue_for(v, duration))) {
/* keep trying */; /* keep trying */;
} }
} else { } else {
...@@ -329,12 +328,12 @@ uint64_t bench(const int nprod, const int ncons, const std::string& name) { ...@@ -329,12 +328,12 @@ uint64_t bench(const int nprod, const int ncons, const std::string& name) {
} }
/* For performance comparison */ /* For performance comparison */
template <typename T, size_t capacity> template <typename T>
class MPMC { class MPMC {
folly::MPMCQueue<T> q_; folly::MPMCQueue<T> q_;
public: public:
MPMC() : q_(capacity) {} MPMC() : q_(FLAGS_capacity) {}
template <typename... Args> template <typename... Args>
void enqueue(Args&&... args) { void enqueue(Args&&... args) {
...@@ -349,23 +348,24 @@ class MPMC { ...@@ -349,23 +348,24 @@ class MPMC {
return q_.read(item); return q_.read(item);
} }
template <typename Clock, typename Duration> template <typename Rep, typename Period>
bool try_dequeue_until( bool try_dequeue_for(
T& item, T& item,
const std::chrono::time_point<Clock, Duration>& deadline) { const std::chrono::duration<Rep, Period>& duration) noexcept {
auto deadline = std::chrono::steady_clock::now() + duration;
return q_.tryReadUntil(deadline, item); return q_.tryReadUntil(deadline, item);
} }
}; };
template <typename T, bool ignore> template <typename T, bool ignore>
using FMPMC = MPMC<T, 256 * 1024>; using FMPMC = MPMC<T>;
template <typename T, size_t capacity> template <typename T>
class PCQ { class PCQ {
folly::ProducerConsumerQueue<T> q_; folly::ProducerConsumerQueue<T> q_;
public: public:
PCQ() : q_(capacity) {} PCQ() : q_(FLAGS_capacity) {}
template <typename... Args> template <typename... Args>
void enqueue(Args&&... args) { void enqueue(Args&&... args) {
...@@ -382,14 +382,14 @@ class PCQ { ...@@ -382,14 +382,14 @@ class PCQ {
return q_.read(item); return q_.read(item);
} }
template <typename Clock, typename Duration> template <typename Rep, typename Period>
bool try_dequeue_until(T&, const std::chrono::time_point<Clock, Duration>&) { bool try_dequeue_for(T&, const std::chrono::duration<Rep, Period>&) noexcept {
return false; return false;
} }
}; };
template <typename T, bool ignore> template <typename T, bool ignore>
using FPCQ = PCQ<T, 256 * 1024>; using FPCQ = PCQ<T>;
template <size_t M> template <size_t M>
struct IntArray { struct IntArray {
...@@ -479,8 +479,9 @@ TEST(UnboundedQueue, bench) { ...@@ -479,8 +479,9 @@ TEST(UnboundedQueue, bench) {
dottedLine(); dottedLine();
std::cout << "$ numactl -N 1 $dir/unbounded_queue_test --bench\n"; std::cout << "$ numactl -N 1 $dir/unbounded_queue_test --bench\n";
dottedLine(); dottedLine();
std::cout << "Using a capacity of 256K for folly::ProducerConsumerQueue\n" std::cout << "Using capacity " << FLAGS_capacity
<< "and folly::MPMCQueue\n"; << " for folly::ProducerConsumerQueue and\n"
<< "folly::MPMCQueue\n";
std::cout << "==============================================================" std::cout << "=============================================================="
<< std::endl; << std::endl;
std::cout << "Test name Max time Avg time Min time" std::cout << "Test name Max time Avg time Min time"
...@@ -509,8 +510,8 @@ TEST(UnboundedQueue, bench) { ...@@ -509,8 +510,8 @@ TEST(UnboundedQueue, bench) {
.............................................................. ..............................................................
$ numactl -N 1 $dir/unbounded_queue_test --bench $ numactl -N 1 $dir/unbounded_queue_test --bench
.............................................................. ..............................................................
Using a capacity of 256K for folly::ProducerConsumerQueue Using capacity 262144 for folly::ProducerConsumerQueue and
and folly::MPMCQueue folly::MPMCQueue
============================================================== ==============================================================
Test name Max time Avg time Min time Test name Max time Avg time Min time
====================== 1 prod 1 cons ====================== ====================== 1 prod 1 cons ======================
...@@ -616,14 +617,14 @@ Unbounded SPMC timed spin only 202 ns 198 ns 193 ns ...@@ -616,14 +617,14 @@ Unbounded SPMC timed spin only 202 ns 198 ns 193 ns
Unbounded SPMC wait spin only 36 ns 36 ns 35 ns Unbounded SPMC wait spin only 36 ns 36 ns 35 ns
Unbounded SPMC try may block 202 ns 195 ns 190 ns Unbounded SPMC try may block 202 ns 195 ns 190 ns
Unbounded SPMC timed may block 208 ns 197 ns 190 ns Unbounded SPMC timed may block 208 ns 197 ns 190 ns
Unbounded SPMC wait may block 1645 ns 1427 ns 36 ns Unbounded SPMC wait may block 96 ns 77 ns 64 ns
.............................................................. ..............................................................
Unbounded MPMC try spin only 204 ns 198 ns 194 ns Unbounded MPMC try spin only 204 ns 198 ns 194 ns
Unbounded MPMC timed spin only 202 ns 195 ns 190 ns Unbounded MPMC timed spin only 202 ns 195 ns 190 ns
Unbounded MPMC wait spin only 61 ns 59 ns 57 ns Unbounded MPMC wait spin only 61 ns 59 ns 57 ns
Unbounded MPMC try may block 206 ns 196 ns 191 ns Unbounded MPMC try may block 206 ns 196 ns 191 ns
Unbounded MPMC timed may block 204 ns 198 ns 192 ns Unbounded MPMC timed may block 204 ns 198 ns 192 ns
Unbounded MPMC wait may block 1658 ns 1293 ns 70 ns Unbounded MPMC wait may block 100 ns 88 ns 84 ns
.............................................................. ..............................................................
folly::MPMC read 210 ns 191 ns 182 ns folly::MPMC read 210 ns 191 ns 182 ns
folly::MPMC tryReadUntil 574 ns 248 ns 192 ns folly::MPMC tryReadUntil 574 ns 248 ns 192 ns
...@@ -636,14 +637,14 @@ Unbounded SPMC timed spin only 208 ns 205 ns 200 ns ...@@ -636,14 +637,14 @@ Unbounded SPMC timed spin only 208 ns 205 ns 200 ns
Unbounded SPMC wait spin only 175 ns 51 ns 33 ns Unbounded SPMC wait spin only 175 ns 51 ns 33 ns
Unbounded SPMC try may block 215 ns 203 ns 186 ns Unbounded SPMC try may block 215 ns 203 ns 186 ns
Unbounded SPMC timed may block 453 ns 334 ns 204 ns Unbounded SPMC timed may block 453 ns 334 ns 204 ns
Unbounded SPMC wait may block 1601 ns 1514 ns 1373 ns Unbounded SPMC wait may block 110 ns 87 ns 55 ns
.............................................................. ..............................................................
Unbounded MPMC try spin only 328 ns 218 ns 197 ns Unbounded MPMC try spin only 328 ns 218 ns 197 ns
Unbounded MPMC timed spin only 217 ns 206 ns 200 ns Unbounded MPMC timed spin only 217 ns 206 ns 200 ns
Unbounded MPMC wait spin only 147 ns 85 ns 58 ns Unbounded MPMC wait spin only 147 ns 85 ns 58 ns
Unbounded MPMC try may block 310 ns 223 ns 199 ns Unbounded MPMC try may block 310 ns 223 ns 199 ns
Unbounded MPMC timed may block 461 ns 275 ns 196 ns Unbounded MPMC timed may block 461 ns 275 ns 196 ns
Unbounded MPMC wait may block 1623 ns 1526 ns 888 ns Unbounded MPMC wait may block 148 ns 111 ns 78 ns
.............................................................. ..............................................................
folly::MPMC read 280 ns 215 ns 194 ns folly::MPMC read 280 ns 215 ns 194 ns
folly::MPMC tryReadUntil 28740 ns 13508 ns 212 ns folly::MPMC tryReadUntil 28740 ns 13508 ns 212 ns
......
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