2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 // @author: Bert Maher <bertrand@fb.com>
19 #include <folly/ProducerConsumerQueue.h>
27 #include <glog/logging.h>
29 #include <folly/Benchmark.h>
30 #include <folly/portability/GFlags.h>
31 #include <folly/stats/Histogram.h>
32 #include <folly/stats/Histogram-defs.h>
36 using namespace folly;
38 typedef unsigned int ThroughputType;
39 typedef ProducerConsumerQueue<ThroughputType> ThroughputQueueType;
41 typedef unsigned long LatencyType;
42 typedef ProducerConsumerQueue<LatencyType> LatencyQueueType;
44 template<class QueueType>
45 struct ThroughputTest {
46 explicit ThroughputTest(size_t size, int iters, int cpu0, int cpu1)
58 CPU_SET(cpu0_, &cpuset);
59 pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
61 for (int i = 0; i < iters_; ++i) {
62 ThroughputType item = i;
63 while (!queue_.write((ThroughputType) item)) {
72 CPU_SET(cpu1_, &cpuset);
73 pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
75 for (int i = 0; i < iters_; ++i) {
76 ThroughputType item = 0;
77 while (!queue_.read(item)) {
79 doNotOptimizeAway(item);
84 std::atomic<bool> done_;
90 template<class QueueType>
92 explicit LatencyTest(size_t size, int iters, int cpu0, int cpu1)
103 static uint64_t timespecDiff(timespec end, timespec start) {
104 if (end.tv_sec == start.tv_sec) {
105 assert(end.tv_nsec >= start.tv_nsec);
106 return uint64_t(end.tv_nsec - start.tv_nsec);
108 assert(end.tv_sec > start.tv_sec);
109 auto diff = uint64_t(end.tv_sec - start.tv_sec);
110 assert(diff < std::numeric_limits<uint64_t>::max() / 1000000000ULL);
111 return diff * 1000000000ULL + end.tv_nsec - start.tv_nsec;
114 void computeTimeCost() {
116 clock_gettime(CLOCK_REALTIME, &start);
117 for (int i = 0; i < iters_; ++i) {
119 clock_gettime(CLOCK_REALTIME, &tv);
121 clock_gettime(CLOCK_REALTIME, &end);
122 time_cost_ = 2 * timespecDiff(end, start) / iters_;
129 CPU_SET(cpu0_, &cpuset);
130 pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
132 for (int i = 0; i < iters_; ++i) {
133 timespec sleeptime, sleepstart;
134 clock_gettime(CLOCK_REALTIME, &sleepstart);
136 clock_gettime(CLOCK_REALTIME, &sleeptime);
137 } while (timespecDiff(sleeptime, sleepstart) < 1000000);
140 clock_gettime(CLOCK_REALTIME, &tv);
141 while (!queue_.write((LatencyType) tv.tv_nsec)) {
150 CPU_SET(cpu1_, &cpuset);
151 pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
153 for (int i = 0; i < iters_; ++i) {
154 unsigned long enqueue_nsec;
155 while (!queue_.read(enqueue_nsec)) {
159 clock_gettime(CLOCK_REALTIME, &tv);
160 int diff = tv.tv_nsec - enqueue_nsec - time_cost_;
165 // Naive log-scale bucketing.
168 bucket <= 30 && (1 << bucket) <= diff;
171 hist_.addValue(bucket - 1);
175 void printHistogram() {
176 hist_.toTSV(std::cout);
180 std::atomic<bool> done_;
185 Histogram<int> hist_;
188 void BM_ProducerConsumer(int iters, int size) {
189 BenchmarkSuspender susp;
191 ThroughputTest<ThroughputQueueType> *test =
192 new ThroughputTest<ThroughputQueueType>(size, iters, -1, -1);
195 std::thread producer( [test] { test->producer(); } );
196 std::thread consumer( [test] { test->consumer(); } );
204 void BM_ProducerConsumerAffinity(int iters, int size) {
205 BenchmarkSuspender susp;
207 ThroughputTest<ThroughputQueueType> *test =
208 new ThroughputTest<ThroughputQueueType>(size, iters, 0, 1);
211 std::thread producer( [test] { test->producer(); } );
212 std::thread consumer( [test] { test->consumer(); } );
220 void BM_ProducerConsumerLatency(int /* iters */, int size) {
221 BenchmarkSuspender susp;
223 LatencyTest<LatencyQueueType> *test =
224 new LatencyTest<LatencyQueueType>(size, 100000, 0, 1);
227 std::thread producer( [test] { test->producer(); } );
228 std::thread consumer( [test] { test->consumer(); } );
233 test->printHistogram();
238 BENCHMARK_DRAW_LINE();
240 BENCHMARK_PARAM(BM_ProducerConsumer, 1048574);
241 BENCHMARK_PARAM(BM_ProducerConsumerAffinity, 1048574);
242 BENCHMARK_PARAM(BM_ProducerConsumerLatency, 1048574);
246 int main(int argc, char** argv) {
247 google::InitGoogleLogging(argv[0]);
248 gflags::ParseCommandLineFlags(&argc, &argv, true);
260 CPU op-mode(s): 32-bit, 64-bit
261 Byte Order: Little Endian
263 On-line CPU(s) list: 0-23
264 Thread(s) per core: 1
265 Core(s) per socket: 1
268 Vendor ID: GenuineIntel
271 Model name: Intel Core Processor (Haswell, no TSX)
275 Hypervisor vendor: KVM
276 Virtualization type: full
280 NUMA node0 CPU(s): 0-23
282 $ ../buck-out/gen/folly/test/producer_consumer_queue_benchmark
296 ============================================================================
297 folly/test/ProducerConsumerQueueBenchmark.cpp relative time/iter iters/s
298 ============================================================================
299 ----------------------------------------------------------------------------
300 BM_ProducerConsumer(1048574) 5.82ns 171.75M
301 BM_ProducerConsumerAffinity(1048574) 7.36ns 135.83M
302 BM_ProducerConsumerLatency(1048574) 1.67min 9.99m
303 ============================================================================