2 * Copyright 2015 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>
24 #include <gflags/gflags.h>
25 #include <glog/logging.h>
26 #include <folly/Benchmark.h>
27 #include <folly/stats/Histogram.h>
28 #include <folly/stats/Histogram-defs.h>
29 #include <folly/ProducerConsumerQueue.h>
33 using namespace folly;
35 typedef unsigned int ThroughputType;
36 typedef ProducerConsumerQueue<ThroughputType> ThroughputQueueType;
38 typedef unsigned long LatencyType;
39 typedef ProducerConsumerQueue<LatencyType> LatencyQueueType;
41 template<class QueueType>
42 struct ThroughputTest {
43 explicit ThroughputTest(size_t size, int iters, int cpu0, int cpu1)
55 CPU_SET(cpu0_, &cpuset);
56 pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
58 for (int i = 0; i < iters_; ++i) {
59 ThroughputType item = i;
60 while (!queue_.write((ThroughputType) item)) {
69 CPU_SET(cpu1_, &cpuset);
70 pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
72 for (int i = 0; i < iters_; ++i) {
73 ThroughputType item = 0;
74 while (!queue_.read(item)) {
76 doNotOptimizeAway(item);
81 std::atomic<bool> done_;
87 template<class QueueType>
89 explicit LatencyTest(size_t size, int iters, int cpu0, int cpu1)
100 void computeTimeCost() {
101 int iterations = 1000;
103 clock_gettime(CLOCK_REALTIME, &start);
104 for (int i = 0; i < iterations; ++i) {
106 clock_gettime(CLOCK_REALTIME, &tv);
108 clock_gettime(CLOCK_REALTIME, &end);
109 time_cost_ = 2 * detail::timespecDiff(end, start) / iterations;
116 CPU_SET(cpu0_, &cpuset);
117 pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
119 for (int i = 0; i < iters_; ++i) {
120 timespec sleeptime, sleepstart;
121 clock_gettime(CLOCK_REALTIME, &sleepstart);
123 clock_gettime(CLOCK_REALTIME, &sleeptime);
124 } while (detail::timespecDiff(sleeptime, sleepstart) < 1000000);
127 clock_gettime(CLOCK_REALTIME, &tv);
128 while (!queue_.write((LatencyType) tv.tv_nsec)) {
137 CPU_SET(cpu1_, &cpuset);
138 pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
140 for (int i = 0; i < iters_; ++i) {
141 unsigned long enqueue_nsec;
142 while (!queue_.read(enqueue_nsec)) {
146 clock_gettime(CLOCK_REALTIME, &tv);
147 int diff = tv.tv_nsec - enqueue_nsec - time_cost_;
152 // Naive log-scale bucketing.
155 bucket <= 30 && (1 << bucket) <= diff;
158 hist_.addValue(bucket - 1);
162 void printHistogram() {
163 hist_.toTSV(std::cout);
167 std::atomic<bool> done_;
172 Histogram<int> hist_;
175 void BM_ProducerConsumer(int iters, int size) {
176 BenchmarkSuspender susp;
178 ThroughputTest<ThroughputQueueType> *test =
179 new ThroughputTest<ThroughputQueueType>(size, iters, -1, -1);
182 std::thread producer( [test] { test->producer(); } );
183 std::thread consumer( [test] { test->consumer(); } );
191 void BM_ProducerConsumerAffinity(int iters, int size) {
192 BenchmarkSuspender susp;
194 ThroughputTest<ThroughputQueueType> *test =
195 new ThroughputTest<ThroughputQueueType>(size, iters, 0, 1);
198 std::thread producer( [test] { test->producer(); } );
199 std::thread consumer( [test] { test->consumer(); } );
207 void BM_ProducerConsumerLatency(int iters, int size) {
208 BenchmarkSuspender susp;
210 LatencyTest<LatencyQueueType> *test =
211 new LatencyTest<LatencyQueueType>(size, 100000, 0, 1);
214 std::thread producer( [test] { test->producer(); } );
215 std::thread consumer( [test] { test->consumer(); } );
220 test->printHistogram();
225 BENCHMARK_DRAW_LINE();
227 BENCHMARK_PARAM(BM_ProducerConsumer, 1048574);
228 BENCHMARK_PARAM(BM_ProducerConsumerAffinity, 1048574);
229 BENCHMARK_PARAM(BM_ProducerConsumerLatency, 1048574);
233 int main(int argc, char** argv) {
234 google::InitGoogleLogging(argv[0]);
235 gflags::ParseCommandLineFlags(&argc, &argv, true);
243 Benchmark on Intel(R) Xeon(R) CPU E5-2660 0 @ 2.20GHz
256 ============================================================================
257 folly/test/ProducerConsumerQueueBenchmark.cpp relative time/iter iters/s
258 ============================================================================
259 ----------------------------------------------------------------------------
260 BM_ProducerConsumer(1048574) 7.52ns 132.90M
261 BM_ProducerConsumerAffinity(1048574) 8.28ns 120.75M
262 BM_ProducerConsumerLatency(1048574) 10.00s 99.98m
263 ============================================================================