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.
16 #include <folly/AtomicUnorderedMap.h>
17 #include <folly/test/DeterministicSchedule.h>
19 #include <semaphore.h>
20 #include <gflags/gflags.h>
21 #include <gtest/gtest.h>
22 #include <folly/Benchmark.h>
23 #include <unordered_map>
25 using namespace folly;
26 using namespace folly::test;
32 non_atomic() = default;
33 non_atomic(const non_atomic&) = delete;
34 constexpr /* implicit */ non_atomic(T desired): value(desired) {}
36 T operator+=(T arg) { value += arg; return load();}
38 T load(std::memory_order order= std::memory_order_seq_cst) const {
43 operator T() const {return load();}
45 void store(T desired, std::memory_order order = std::memory_order_seq_cst) {
49 T exchange(T desired, std::memory_order order = std::memory_order_seq_cst) {
55 bool compare_exchange_weak(
56 T& expected, T desired,
57 std::memory_order success = std::memory_order_seq_cst,
58 std::memory_order failure = std::memory_order_seq_cst) {
59 if (value == expected) {
68 bool compare_exchange_strong(
69 T& expected, T desired,
70 std::memory_order success = std::memory_order_seq_cst,
71 std::memory_order failure = std::memory_order_seq_cst) {
72 if (value == expected) {
81 bool is_lock_free() const {return true;}
84 template <typename Key,
87 template <typename> class Atom = std::atomic,
88 typename Allocator = std::allocator<char>>
90 AtomicUnorderedInsertMap<Key,
94 (boost::has_trivial_destructor<Key>::value &&
95 boost::has_trivial_destructor<Value>::value),
101 template <typename T>
102 struct AtomicUnorderedInsertMapTest : public ::testing::Test {};
105 // uint16_t doesn't make sense for most platforms, but we might as well
107 using IndexTypesToTest = ::testing::Types<uint16_t, uint32_t, uint64_t>;
108 TYPED_TEST_CASE(AtomicUnorderedInsertMapTest, IndexTypesToTest);
110 TYPED_TEST(AtomicUnorderedInsertMapTest, basic) {
115 folly::detail::MMapAlloc> m(100);
117 m.emplace("abc", "ABC");
118 EXPECT_TRUE(m.find("abc") != m.cend());
119 EXPECT_EQ(m.find("abc")->first, "abc");
120 EXPECT_EQ(m.find("abc")->second, "ABC");
121 EXPECT_TRUE(m.find("def") == m.cend());
122 auto iter = m.cbegin();
123 EXPECT_TRUE(iter != m.cend());
124 EXPECT_TRUE(iter == m.find("abc"));
126 EXPECT_TRUE(a == iter);
129 EXPECT_TRUE(iter == m.cend());
131 EXPECT_TRUE(a != iter);
133 EXPECT_TRUE(a == iter);
137 TEST(AtomicUnorderedInsertMap, load_factor) {
138 AtomicUnorderedInsertMap<int, bool> m(5000, 0.5f);
140 // we should be able to put in much more than 5000 things because of
141 // our load factor request
142 for (int i = 0; i < 10000; ++i) {
147 TEST(AtomicUnorderedInsertMap, capacity_exceeded) {
148 AtomicUnorderedInsertMap<int, bool> m(5000, 1.0f);
151 for (int i = 0; i < 6000; ++i) {
157 TYPED_TEST(AtomicUnorderedInsertMapTest, value_mutation) {
158 UIM<int, MutableAtom<int>, TypeParam> m(100);
160 for (int i = 0; i < 50; ++i) {
164 m.find(1)->second.data++;
167 TEST(UnorderedInsertMap, value_mutation) {
168 UIM<int, MutableData<int>, uint32_t, non_atomic> m(100);
170 for (int i = 0; i < 50; ++i) {
174 m.find(1)->second.data++;
175 EXPECT_EQ(m.find(1)->second.data, 2);
178 // This test is too expensive to run automatically. On my dev server it
179 // takes about 10 minutes for dbg build, 2 for opt.
180 TEST(AtomicUnorderedInsertMap, DISABLED_mega_map) {
181 size_t capacity = 2000000000;
182 AtomicUnorderedInsertMap64<size_t,size_t> big(capacity);
183 for (size_t i = 0; i < capacity * 2; i += 2) {
184 big.emplace(i, i * 10);
186 for (size_t i = 0; i < capacity * 3; i += capacity / 1000 + 1) {
187 auto iter = big.find(i);
188 if ((i & 1) == 0 && i < capacity * 2) {
189 EXPECT_EQ(iter->second, i * 10);
191 EXPECT_TRUE(iter == big.cend());
196 BENCHMARK(lookup_int_int_hit, iters) {
197 std::unique_ptr<AtomicUnorderedInsertMap<int,size_t>> ptr = {};
199 size_t capacity = 100000;
202 ptr.reset(new AtomicUnorderedInsertMap<int,size_t>(capacity));
203 for (size_t i = 0; i < capacity; ++i) {
204 auto k = 3 * ((5641 * i) % capacity);
205 ptr->emplace(k, k + 1);
206 EXPECT_EQ(ptr->find(k)->second, k + 1);
210 for (size_t i = 0; i < iters; ++i) {
211 size_t k = 3 * (((i * 7919) ^ (i * 4001)) % capacity);
212 auto iter = ptr->find(k);
213 if (iter == ptr->cend() ||
214 iter->second != k + 1) {
215 auto jter = ptr->find(k);
216 EXPECT_TRUE(iter == jter);
218 EXPECT_EQ(iter->second, k + 1);
227 size_t operator()(const std::pair<uint64_t,uint64_t>& pr) const {
228 return pr.first ^ pr.second;
232 void contendedRW(size_t itersPerThread,
235 size_t readsPerWrite) {
236 typedef std::pair<uint64_t,uint64_t> Key;
237 typedef AtomicUnorderedInsertMap<Key,MutableAtom<uint32_t>,PairHash> Map;
239 std::unique_ptr<Map> ptr = {};
240 std::atomic<bool> go;
241 std::vector<std::thread> threads;
244 ptr.reset(new Map(capacity));
245 while (threads.size() < numThreads) {
246 threads.emplace_back([&](){
248 std::this_thread::yield();
253 while (reads + writes < itersPerThread) {
254 auto r = Random::rand32();
255 Key key(reads + writes, r);
256 if (reads < writes * readsPerWrite ||
257 writes >= capacity / numThreads) {
260 auto iter = ptr->find(key);
262 iter == ptr->cend() ||
263 iter->second.data.load(std::memory_order_acquire) >= key.first);
267 auto pr = ptr->emplace(key, key.first);
269 pr.first->second.data++;
271 } catch (std::bad_alloc& x) {
272 LOG(INFO) << "bad alloc";
282 for (auto& thr : threads) {
291 // sudo nice -n -20 ~/fbcode/_bin/common/concurrency/experimental/atomic_unordered_map --benchmark --bm_min_iters=1000000
293 // without MAP_HUGETLB (default)
295 // ============================================================================
296 // common/concurrency/experimental/AtomicUnorderedMapTest.cpprelative time/iter
298 // ============================================================================
299 // lookup_int_int_hit 20.05ns 49.89M
300 // contendedRW(small_32thr_99pct) 70.36ns 14.21M
301 // contendedRW(large_32thr_99pct) 164.23ns 6.09M
302 // contendedRW(large_32thr_99_9pct) 158.81ns 6.30M
303 // ============================================================================
305 // with MAP_HUGETLB hacked in
306 // ============================================================================
307 // lookup_int_int_hit 19.67ns 50.84M
308 // contendedRW(small_32thr_99pct) 62.46ns 16.01M
309 // contendedRW(large_32thr_99pct) 119.41ns 8.37M
310 // contendedRW(large_32thr_99_9pct) 111.23ns 8.99M
311 // ============================================================================
312 BENCHMARK_NAMED_PARAM(contendedRW, small_32thr_99pct, 100000, 32, 99)
313 BENCHMARK_NAMED_PARAM(contendedRW, large_32thr_99pct, 100000000, 32, 99)
314 BENCHMARK_NAMED_PARAM(contendedRW, large_32thr_99_9pct, 100000000, 32, 999)
316 BENCHMARK_DRAW_LINE();
318 // sudo nice -n -20 ~/fbcode/_build/opt/site_integrity/quasar/experimental/atomic_unordered_map_test --benchmark --bm_min_iters=10000
319 // Single threaded benchmarks to test how much better we are than
320 // std::unordered_map and what is the cost of using atomic operations
321 // in the uncontended use case
322 // ============================================================================
323 // std_map 1.20ms 832.58
324 // atomic_fast_map 511.35us 1.96K
325 // fast_map 196.28us 5.09K
326 // ============================================================================
329 std::unordered_map<long, long> m;
331 for (int i=0; i<10000; ++i) {
335 for (int i=0; i<10000; ++i) {
337 folly::doNotOptimizeAway(&*a);
341 BENCHMARK(atomic_fast_map) {
342 UIM<long, long, uint32_t, std::atomic> m(10000);
343 for (int i=0; i<10000; ++i) {
347 for (int i=0; i<10000; ++i) {
349 folly::doNotOptimizeAway(&*a);
353 BENCHMARK(fast_map) {
354 UIM<long, long, uint32_t, non_atomic> m(10000);
355 for (int i=0; i<10000; ++i) {
359 for (int i=0; i<10000; ++i) {
361 folly::doNotOptimizeAway(&*a);
365 BENCHMARK(atomic_fast_map_64) {
366 UIM<long, long, uint64_t, std::atomic> m(10000);
367 for (int i=0; i<10000; ++i) {
371 for (int i=0; i<10000; ++i) {
373 folly::doNotOptimizeAway(&*a);
377 BENCHMARK(fast_map_64) {
378 UIM<long, long, uint64_t, non_atomic> m(10000);
379 for (int i=0; i<10000; ++i) {
383 for (int i=0; i<10000; ++i) {
385 folly::doNotOptimizeAway(&*a);
390 int main(int argc, char ** argv) {
391 testing::InitGoogleTest(&argc, argv);
392 google::ParseCommandLineFlags(&argc, &argv, true);
393 int rv = RUN_ALL_TESTS();
394 folly::runBenchmarksOnFlag();