YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/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
478k
    : shard_block_size_(block_size / 8), arena_(block_size, huge_page_size) {
39
  // find a power of two >= num_cpus and >= 8
40
478k
  index_mask_ = 7;
41
478k
  size_t num_cpus = base::NumCPUs();
42
955k
  while (index_mask_ + 1 < num_cpus) {
43
477k
    index_mask_ = index_mask_ * 2 + 1;
44
477k
  }
45
46
478k
  shards_.reset(new Shard[index_mask_ + 1]);
47
478k
  Fixup();
48
478k
}
49
50
7.23k
ConcurrentArena::Shard* ConcurrentArena::Repick() {
51
7.23k
  int cpuid = port::PhysicalCoreID();
52
7.24k
  if (
UNLIKELY7.23k
(cpuid < 0)) {
53
    // cpu id unavailable, just pick randomly
54
7.24k
    cpuid =
55
7.24k
        Random::GetTLSInstance()->Uniform(static_cast<int>(index_mask_) + 1);
56
7.24k
  }
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
7.23k
  return &shards_[cpuid & index_mask_];
63
7.23k
}
64
65
425k
void ConcurrentArena::SetMemTracker(std::shared_ptr<yb::MemTracker> mem_tracker) {
66
425k
  arena_.SetMemTracker(std::move(mem_tracker));
67
425k
}
68
69
}  // namespace rocksdb