2 * Copyright 2016 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 #include <folly/AtomicUnorderedMap.h>
19 #include <semaphore.h>
21 #include <unordered_map>
23 #include <gtest/gtest.h>
25 #include <folly/Benchmark.h>
26 #include <folly/portability/GFlags.h>
27 #include <folly/test/DeterministicSchedule.h>
29 using namespace folly;
30 using namespace folly::test;
36 non_atomic() = default;
37 non_atomic(const non_atomic&) = delete;
38 constexpr /* implicit */ non_atomic(T desired): value(desired) {}
40 T operator+=(T arg) { value += arg; return load();}
42 T load(std::memory_order /* order */ = std::memory_order_seq_cst) const {
47 operator T() const {return load();}
50 std::memory_order /* order */ = std::memory_order_seq_cst) {
55 std::memory_order /* order */ = std::memory_order_seq_cst) {
61 bool compare_exchange_weak(
64 std::memory_order /* success */ = std::memory_order_seq_cst,
65 std::memory_order /* failure */ = std::memory_order_seq_cst) {
66 if (value == expected) {
75 bool compare_exchange_strong(
78 std::memory_order /* success */ = std::memory_order_seq_cst,
79 std::memory_order /* failure */ = std::memory_order_seq_cst) {
80 if (value == expected) {
89 bool is_lock_free() const {return true;}
92 template <typename Key,
95 template <typename> class Atom = std::atomic,
96 typename Allocator = std::allocator<char>>
98 AtomicUnorderedInsertMap<Key,
102 (boost::has_trivial_destructor<Key>::value &&
103 boost::has_trivial_destructor<Value>::value),
109 template <typename T>
110 struct AtomicUnorderedInsertMapTest : public ::testing::Test {};
113 // uint16_t doesn't make sense for most platforms, but we might as well
115 using IndexTypesToTest = ::testing::Types<uint16_t, uint32_t, uint64_t>;
116 TYPED_TEST_CASE(AtomicUnorderedInsertMapTest, IndexTypesToTest);
118 TYPED_TEST(AtomicUnorderedInsertMapTest, basic) {
123 folly::detail::MMapAlloc> m(100);
125 m.emplace("abc", "ABC");
126 EXPECT_TRUE(m.find("abc") != m.cend());
127 EXPECT_EQ(m.find("abc")->first, "abc");
128 EXPECT_EQ(m.find("abc")->second, "ABC");
129 EXPECT_TRUE(m.find("def") == m.cend());
130 auto iter = m.cbegin();
131 EXPECT_TRUE(iter != m.cend());
132 EXPECT_TRUE(iter == m.find("abc"));
134 EXPECT_TRUE(a == iter);
137 EXPECT_TRUE(iter == m.cend());
139 EXPECT_TRUE(a != iter);
141 EXPECT_TRUE(a == iter);
145 TEST(AtomicUnorderedInsertMap, load_factor) {
146 AtomicUnorderedInsertMap<int, bool> m(5000, 0.5f);
148 // we should be able to put in much more than 5000 things because of
149 // our load factor request
150 for (int i = 0; i < 10000; ++i) {
155 TEST(AtomicUnorderedInsertMap, capacity_exceeded) {
156 AtomicUnorderedInsertMap<int, bool> m(5000, 1.0f);
159 for (int i = 0; i < 6000; ++i) {
165 TYPED_TEST(AtomicUnorderedInsertMapTest, value_mutation) {
166 UIM<int, MutableAtom<int>, TypeParam> m(100);
168 for (int i = 0; i < 50; ++i) {
172 m.find(1)->second.data++;
175 TEST(UnorderedInsertMap, value_mutation) {
176 UIM<int, MutableData<int>, uint32_t, non_atomic> m(100);
178 for (int i = 0; i < 50; ++i) {
182 m.find(1)->second.data++;
183 EXPECT_EQ(m.find(1)->second.data, 2);
186 // This test is too expensive to run automatically. On my dev server it
187 // takes about 10 minutes for dbg build, 2 for opt.
188 TEST(AtomicUnorderedInsertMap, DISABLED_mega_map) {
189 size_t capacity = 2000000000;
190 AtomicUnorderedInsertMap64<size_t,size_t> big(capacity);
191 for (size_t i = 0; i < capacity * 2; i += 2) {
192 big.emplace(i, i * 10);
194 for (size_t i = 0; i < capacity * 3; i += capacity / 1000 + 1) {
195 auto iter = big.find(i);
196 if ((i & 1) == 0 && i < capacity * 2) {
197 EXPECT_EQ(iter->second, i * 10);
199 EXPECT_TRUE(iter == big.cend());
204 BENCHMARK(lookup_int_int_hit, iters) {
205 std::unique_ptr<AtomicUnorderedInsertMap<int,size_t>> ptr = {};
207 size_t capacity = 100000;
210 ptr.reset(new AtomicUnorderedInsertMap<int,size_t>(capacity));
211 for (size_t i = 0; i < capacity; ++i) {
212 auto k = 3 * ((5641 * i) % capacity);
213 ptr->emplace(k, k + 1);
214 EXPECT_EQ(ptr->find(k)->second, k + 1);
218 for (size_t i = 0; i < iters; ++i) {
219 size_t k = 3 * (((i * 7919) ^ (i * 4001)) % capacity);
220 auto iter = ptr->find(k);
221 if (iter == ptr->cend() ||
222 iter->second != k + 1) {
223 auto jter = ptr->find(k);
224 EXPECT_TRUE(iter == jter);
226 EXPECT_EQ(iter->second, k + 1);
235 size_t operator()(const std::pair<uint64_t,uint64_t>& pr) const {
236 return pr.first ^ pr.second;
240 void contendedRW(size_t itersPerThread,
243 size_t readsPerWrite) {
244 typedef std::pair<uint64_t,uint64_t> Key;
245 typedef AtomicUnorderedInsertMap<Key,MutableAtom<uint32_t>,PairHash> Map;
247 std::unique_ptr<Map> ptr = {};
248 std::atomic<bool> go;
249 std::vector<std::thread> threads;
252 ptr.reset(new Map(capacity));
253 while (threads.size() < numThreads) {
254 threads.emplace_back([&](){
256 std::this_thread::yield();
261 while (reads + writes < itersPerThread) {
262 auto r = Random::rand32();
263 Key key(reads + writes, r);
264 if (reads < writes * readsPerWrite ||
265 writes >= capacity / numThreads) {
268 auto iter = ptr->find(key);
270 iter == ptr->cend() ||
271 iter->second.data.load(std::memory_order_acquire) >= key.first);
275 auto pr = ptr->emplace(key, key.first);
277 pr.first->second.data++;
279 } catch (std::bad_alloc& x) {
280 LOG(INFO) << "bad alloc";
290 for (auto& thr : threads) {
299 // sudo nice -n -20 ~/fbcode/_bin/common/concurrency/experimental/atomic_unordered_map --benchmark --bm_min_iters=1000000
301 // without MAP_HUGETLB (default)
303 // ============================================================================
304 // common/concurrency/experimental/AtomicUnorderedMapTest.cpprelative time/iter
306 // ============================================================================
307 // lookup_int_int_hit 20.05ns 49.89M
308 // contendedRW(small_32thr_99pct) 70.36ns 14.21M
309 // contendedRW(large_32thr_99pct) 164.23ns 6.09M
310 // contendedRW(large_32thr_99_9pct) 158.81ns 6.30M
311 // ============================================================================
313 // with MAP_HUGETLB hacked in
314 // ============================================================================
315 // lookup_int_int_hit 19.67ns 50.84M
316 // contendedRW(small_32thr_99pct) 62.46ns 16.01M
317 // contendedRW(large_32thr_99pct) 119.41ns 8.37M
318 // contendedRW(large_32thr_99_9pct) 111.23ns 8.99M
319 // ============================================================================
320 BENCHMARK_NAMED_PARAM(contendedRW, small_32thr_99pct, 100000, 32, 99)
321 BENCHMARK_NAMED_PARAM(contendedRW, large_32thr_99pct, 100000000, 32, 99)
322 BENCHMARK_NAMED_PARAM(contendedRW, large_32thr_99_9pct, 100000000, 32, 999)
324 BENCHMARK_DRAW_LINE();
326 // sudo nice -n -20 ~/fbcode/_build/opt/site_integrity/quasar/experimental/atomic_unordered_map_test --benchmark --bm_min_iters=10000
327 // Single threaded benchmarks to test how much better we are than
328 // std::unordered_map and what is the cost of using atomic operations
329 // in the uncontended use case
330 // ============================================================================
331 // std_map 1.20ms 832.58
332 // atomic_fast_map 511.35us 1.96K
333 // fast_map 196.28us 5.09K
334 // ============================================================================
337 std::unordered_map<long, long> m;
339 for (int i=0; i<10000; ++i) {
343 for (int i=0; i<10000; ++i) {
345 folly::doNotOptimizeAway(&*a);
349 BENCHMARK(atomic_fast_map) {
350 UIM<long, long, uint32_t, std::atomic> m(10000);
351 for (int i=0; i<10000; ++i) {
355 for (int i=0; i<10000; ++i) {
357 folly::doNotOptimizeAway(&*a);
361 BENCHMARK(fast_map) {
362 UIM<long, long, uint32_t, non_atomic> m(10000);
363 for (int i=0; i<10000; ++i) {
367 for (int i=0; i<10000; ++i) {
369 folly::doNotOptimizeAway(&*a);
373 BENCHMARK(atomic_fast_map_64) {
374 UIM<long, long, uint64_t, std::atomic> m(10000);
375 for (int i=0; i<10000; ++i) {
379 for (int i=0; i<10000; ++i) {
381 folly::doNotOptimizeAway(&*a);
385 BENCHMARK(fast_map_64) {
386 UIM<long, long, uint64_t, non_atomic> m(10000);
387 for (int i=0; i<10000; ++i) {
391 for (int i=0; i<10000; ++i) {
393 folly::doNotOptimizeAway(&*a);
398 int main(int argc, char ** argv) {
399 testing::InitGoogleTest(&argc, argv);
400 google::ParseCommandLineFlags(&argc, &argv, true);
401 int rv = RUN_ALL_TESTS();
402 folly::runBenchmarksOnFlag();