/Users/deen/code/yugabyte-db/src/yb/rocksdb/util/concurrent_arena.cc
Line | Count | Source |
1 | | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
2 | | // This source code is licensed under the BSD-style license found in the |
3 | | // LICENSE file in the root directory of this source tree. An additional grant |
4 | | // of patent rights can be found in the PATENTS file in the same directory. |
5 | | // |
6 | | // The following only applies to changes made to this file as part of YugaByte development. |
7 | | // |
8 | | // Portions Copyright (c) YugaByte, Inc. |
9 | | // |
10 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
11 | | // in compliance with the License. You may obtain a copy of the License at |
12 | | // |
13 | | // http://www.apache.org/licenses/LICENSE-2.0 |
14 | | // |
15 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
16 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
17 | | // or implied. See the License for the specific language governing permissions and limitations |
18 | | // under the License. |
19 | | // |
20 | | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. |
21 | | // Use of this source code is governed by a BSD-style license that can be |
22 | | // found in the LICENSE file. See the AUTHORS file for names of contributors. |
23 | | |
24 | | #include "yb/rocksdb/util/concurrent_arena.h" |
25 | | #include <thread> |
26 | | #include "yb/rocksdb/port/likely.h" |
27 | | #include "yb/rocksdb/port/port.h" |
28 | | #include "yb/rocksdb/util/random.h" |
29 | | #include "yb/gutil/sysinfo.h" |
30 | | |
31 | | namespace rocksdb { |
32 | | |
33 | | #if ROCKSDB_SUPPORT_THREAD_LOCAL |
34 | | __thread uint32_t ConcurrentArena::tls_cpuid = 0; |
35 | | #endif |
36 | | |
37 | | ConcurrentArena::ConcurrentArena(size_t block_size, size_t huge_page_size) |
38 | 379k | : shard_block_size_(block_size / 8), arena_(block_size, huge_page_size) { |
39 | | // find a power of two >= num_cpus and >= 8 |
40 | 379k | index_mask_ = 7; |
41 | 379k | size_t num_cpus = base::NumCPUs(); |
42 | 759k | while (index_mask_ + 1 < num_cpus) { |
43 | 379k | index_mask_ = index_mask_ * 2 + 1; |
44 | 379k | } |
45 | | |
46 | 379k | shards_.reset(new Shard[index_mask_ + 1]); |
47 | 379k | Fixup(); |
48 | 379k | } |
49 | | |
50 | 8.09k | ConcurrentArena::Shard* ConcurrentArena::Repick() { |
51 | 8.09k | int cpuid = port::PhysicalCoreID(); |
52 | 8.09k | if (UNLIKELY(cpuid < 0)) { |
53 | | // cpu id unavailable, just pick randomly |
54 | 8.03k | cpuid = |
55 | 8.03k | Random::GetTLSInstance()->Uniform(static_cast<int>(index_mask_) + 1); |
56 | 8.03k | } |
57 | | #if ROCKSDB_SUPPORT_THREAD_LOCAL |
58 | | // even if we are cpu 0, use a non-zero tls_cpuid so we can tell we |
59 | | // have repicked |
60 | | tls_cpuid = cpuid | (static_cast<int>(index_mask_) + 1); |
61 | | #endif |
62 | 8.09k | return &shards_[cpuid & index_mask_]; |
63 | 8.09k | } |
64 | | |
65 | 331k | void ConcurrentArena::SetMemTracker(std::shared_ptr<yb::MemTracker> mem_tracker) { |
66 | 331k | arena_.SetMemTracker(std::move(mem_tracker)); |
67 | 331k | } |
68 | | |
69 | | } // namespace rocksdb |