|
| 1 | +// Copyright (c) 2025 dingodb.com, Inc. All Rights Reserved |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "utils/numa_manager.h" |
| 16 | + |
| 17 | +#include <numa.h> |
| 18 | +#include <numaif.h> |
| 19 | +#include <sched.h> |
| 20 | +#include <unistd.h> |
| 21 | + |
| 22 | +#include <sstream> |
| 23 | +#include <thread> |
| 24 | + |
| 25 | +#include "glog/logging.h" |
| 26 | + |
| 27 | +namespace dingofs { |
| 28 | +namespace utils { |
| 29 | + |
| 30 | +NumaManager& NumaManager::GetInstance() { |
| 31 | + static NumaManager instance; |
| 32 | + return instance; |
| 33 | +} |
| 34 | + |
| 35 | +NumaManager::NumaManager() { |
| 36 | + available_ = numa_available() >= 0; |
| 37 | + |
| 38 | + if (!available_) { |
| 39 | + nodes_ = 1; |
| 40 | + cpus_ = std::thread::hardware_concurrency(); |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + nodes_ = numa_max_node() + 1; |
| 45 | + cpus_ = numa_num_configured_cpus(); |
| 46 | +} |
| 47 | + |
| 48 | +void NumaManager::CheckNode(int node) const { |
| 49 | + CHECK(node >= 0 && node < nodes_) << "invalid numa node: " << node; |
| 50 | +} |
| 51 | + |
| 52 | +std::vector<int> NumaManager::GetNodeCpus(const int node) const { |
| 53 | + CheckNode(node); |
| 54 | + |
| 55 | + std::vector<int> cpulist; |
| 56 | + |
| 57 | + struct bitmask* mask = numa_allocate_cpumask(); |
| 58 | + numa_node_to_cpus(node, mask); |
| 59 | + |
| 60 | + for (int i = 0; i < cpus_; ++i) { |
| 61 | + if (numa_bitmask_isbitset(mask, i)) { |
| 62 | + cpulist.push_back(i); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + numa_free_cpumask(mask); |
| 67 | + return cpulist; |
| 68 | +} |
| 69 | + |
| 70 | +void NumaManager::BindCpuNode(const int node) { BindCpuNodes({node}); } |
| 71 | + |
| 72 | +void NumaManager::BindCpuNodes(const std::vector<int>& nodes) { |
| 73 | + CHECK(!nodes.empty()); |
| 74 | + |
| 75 | + struct bitmask* cpumask = numa_allocate_cpumask(); |
| 76 | + struct bitmask* tmpmask = numa_allocate_cpumask(); |
| 77 | + |
| 78 | + numa_bitmask_clearall(cpumask); |
| 79 | + |
| 80 | + for (int node : nodes) { |
| 81 | + CheckNode(node); |
| 82 | + |
| 83 | + numa_bitmask_clearall(tmpmask); |
| 84 | + numa_node_to_cpus(node, tmpmask); |
| 85 | + |
| 86 | + for (int cpu = 0; cpu < cpus_; ++cpu) { |
| 87 | + if (numa_bitmask_isbitset(tmpmask, cpu)) { |
| 88 | + numa_bitmask_setbit(cpumask, cpu); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + CHECK(numa_sched_setaffinity(0, cpumask) == 0) |
| 94 | + << "numa_sched_setaffinity failed"; |
| 95 | + |
| 96 | + numa_free_cpumask(tmpmask); |
| 97 | + numa_free_cpumask(cpumask); |
| 98 | +} |
| 99 | + |
| 100 | +void NumaManager::BindMemPreferredNode(int node) { |
| 101 | + CheckNode(node); |
| 102 | + numa_set_preferred(node); |
| 103 | +} |
| 104 | + |
| 105 | +void NumaManager::BindMemNode(const int node) { BindMemNodes({node}); } |
| 106 | + |
| 107 | +void NumaManager::BindMemNodes(const std::vector<int>& nodes) { |
| 108 | + CHECK(!nodes.empty()) << "nodes cannot be empty"; |
| 109 | + |
| 110 | + struct bitmask* mask = numa_allocate_nodemask(); |
| 111 | + numa_bitmask_clearall(mask); |
| 112 | + |
| 113 | + for (int node : nodes) { |
| 114 | + CheckNode(node); |
| 115 | + numa_bitmask_setbit(mask, node); |
| 116 | + } |
| 117 | + |
| 118 | + numa_set_membind(mask); |
| 119 | + |
| 120 | + numa_free_nodemask(mask); |
| 121 | +} |
| 122 | + |
| 123 | +void NumaManager::BindNode(int node) { |
| 124 | + BindCpuNode(node); |
| 125 | + BindMemNode(node); |
| 126 | +} |
| 127 | + |
| 128 | +void NumaManager::BindNodes(const std::vector<int>& nodes) { |
| 129 | + CHECK(!nodes.empty()) << "nodes cannot be empty"; |
| 130 | + |
| 131 | + BindCpuNodes(nodes); |
| 132 | + BindMemNodes(nodes); |
| 133 | +} |
| 134 | + |
| 135 | +int NumaManager::CpuToNode(int cpu) const { return numa_node_of_cpu(cpu); } |
| 136 | + |
| 137 | +void NumaManager::InterleaveAll() const { |
| 138 | + struct bitmask* mask = numa_allocate_nodemask(); |
| 139 | + |
| 140 | + for (int i = 0; i < nodes_; ++i) { |
| 141 | + numa_bitmask_setbit(mask, i); |
| 142 | + } |
| 143 | + |
| 144 | + numa_set_interleave_mask(mask); |
| 145 | + numa_free_nodemask(mask); |
| 146 | +} |
| 147 | + |
| 148 | +void NumaManager::BindThreadToCpu(const int id, const int cpu) const { |
| 149 | + CHECK(cpu >= 0 && cpu < cpus_); |
| 150 | + |
| 151 | + cpu_set_t cpuset; |
| 152 | + CPU_ZERO(&cpuset); |
| 153 | + CPU_SET(cpu, &cpuset); |
| 154 | + |
| 155 | + CHECK(sched_setaffinity(id, sizeof(cpuset), &cpuset) == 0) |
| 156 | + << "sched_setaffinity failed"; |
| 157 | +} |
| 158 | + |
| 159 | +// 0 means current thread |
| 160 | +void NumaManager::BindCurrentThreadToCpu(const int cpu) const { |
| 161 | + BindThreadToCpu(0, cpu); |
| 162 | +} |
| 163 | + |
| 164 | +void NumaManager::PrintTopology() const { |
| 165 | + std::stringstream ss; |
| 166 | + |
| 167 | + ss << "NUMA nodes: " << nodes_ << "\n"; |
| 168 | + ss << "CPU count : " << cpus_ << "\n"; |
| 169 | + |
| 170 | + for (int i = 0; i < nodes_; ++i) { |
| 171 | + auto cpus = GetNodeCpus(i); |
| 172 | + |
| 173 | + ss << "node " << i << " cpus: "; |
| 174 | + for (auto c : cpus) { |
| 175 | + ss << c << " "; |
| 176 | + } |
| 177 | + ss << "\n"; |
| 178 | + } |
| 179 | + |
| 180 | + LOG(INFO) << ss.str(); |
| 181 | +} |
| 182 | + |
| 183 | +} // namespace utils |
| 184 | +} // namespace dingofs |
0 commit comments