forked from triton-inference-server/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnuma_utils.cc
237 lines (217 loc) · 7.13 KB
/
numa_utils.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright 2021-2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of NVIDIA CORPORATION nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "numa_utils.h"
#ifndef _WIN32
#include <numa.h>
#include <numaif.h>
#endif
#include "triton/common/logging.h"
namespace triton { namespace core {
namespace {
std::string
VectorToString(const std::vector<int>& vec)
{
std::string str("[");
for (const auto& element : vec) {
str += std::to_string(element);
str += ",";
}
str += "]";
return str;
}
Status
ParseIntOption(const std::string& msg, const std::string& arg, int* value)
{
try {
*value = std::stoi(arg);
}
catch (const std::invalid_argument& ia) {
return Status(
Status::Code::INVALID_ARG,
msg + ": Can't parse '" + arg + "' to integer");
}
return Status::Success;
}
} // namespace
// NUMA setting will be ignored on Windows platform
#ifdef _WIN32
Status
SetNumaConfigOnThread(
const triton::common::HostPolicyCmdlineConfig& host_policy)
{
return Status::Success;
}
Status
SetNumaMemoryPolicy(const triton::common::HostPolicyCmdlineConfig& host_policy)
{
return Status::Success;
}
Status
GetNumaMemoryPolicyNodeMask(unsigned long* node_mask)
{
*node_mask = 0;
return Status::Success;
}
Status
ResetNumaMemoryPolicy()
{
return Status::Success;
}
Status
SetNumaThreadAffinity(
std::thread::native_handle_type thread,
const triton::common::HostPolicyCmdlineConfig& host_policy)
{
return Status::Success;
}
#else
// Use variable to make sure no NUMA related function is actually called
// if Triton is not running with NUMA awareness. i.e. Extra docker permission
// is needed to call the NUMA functions and this ensures backward compatibility.
thread_local bool numa_set = false;
Status
SetNumaConfigOnThread(
const triton::common::HostPolicyCmdlineConfig& host_policy)
{
// Set thread affinity
RETURN_IF_ERROR(SetNumaThreadAffinity(pthread_self(), host_policy));
// Set memory policy
RETURN_IF_ERROR(SetNumaMemoryPolicy(host_policy));
return Status::Success;
}
Status
SetNumaMemoryPolicy(const triton::common::HostPolicyCmdlineConfig& host_policy)
{
const auto it = host_policy.find("numa-node");
if (it != host_policy.end()) {
int node_id;
RETURN_IF_ERROR(
ParseIntOption("Parsing 'numa-node' value", it->second, &node_id));
LOG_VERBOSE(1) << "Thread is binding to NUMA node " << it->second
<< ". Max NUMA node count: " << (numa_max_node() + 1);
numa_set = true;
unsigned long node_mask = 1UL << node_id;
if (set_mempolicy(MPOL_BIND, &node_mask, (numa_max_node() + 1) + 1) != 0) {
return Status(
Status::Code::INTERNAL,
std::string("Unable to set NUMA memory policy: ") + strerror(errno));
}
}
return Status::Success;
}
Status
GetNumaMemoryPolicyNodeMask(unsigned long* node_mask)
{
*node_mask = 0;
int mode;
if (numa_set &&
get_mempolicy(&mode, node_mask, numa_max_node() + 1, NULL, 0) != 0) {
return Status(
Status::Code::INTERNAL,
std::string("Unable to get NUMA node for current thread: ") +
strerror(errno));
}
return Status::Success;
}
Status
ResetNumaMemoryPolicy()
{
if (numa_set && (set_mempolicy(MPOL_DEFAULT, nullptr, 0) != 0)) {
return Status(
Status::Code::INTERNAL,
std::string("Unable to reset NUMA memory policy: ") + strerror(errno));
}
numa_set = false;
return Status::Success;
}
Status
SetNumaThreadAffinity(
std::thread::native_handle_type thread,
const triton::common::HostPolicyCmdlineConfig& host_policy)
{
const auto it = host_policy.find("cpu-cores");
if (it != host_policy.end()) {
// Parse CPUs
std::vector<int> cpus;
{
const auto& cpu_str = it->second;
auto delim_cpus = cpu_str.find(",");
int current_pos = 0;
while (true) {
auto delim_range = cpu_str.find("-", current_pos);
if (delim_range == std::string::npos) {
return Status(
Status::Code::INVALID_ARG,
std::string("host policy setting 'cpu-cores' format is "
"'<lower_cpu_core_id>-<upper_cpu_core_id>'. Got ") +
cpu_str.substr(
current_pos, ((delim_cpus == std::string::npos)
? (cpu_str.length() + 1)
: delim_cpus) -
current_pos));
}
int lower, upper;
RETURN_IF_ERROR(ParseIntOption(
"Parsing 'cpu-cores' value",
cpu_str.substr(current_pos, delim_range - current_pos), &lower));
RETURN_IF_ERROR(ParseIntOption(
"Parsing 'cpu-cores' value",
(delim_cpus == std::string::npos)
? cpu_str.substr(delim_range + 1)
: cpu_str.substr(
delim_range + 1, delim_cpus - (delim_range + 1)),
&upper));
for (; lower <= upper; ++lower) {
cpus.push_back(lower);
}
// break if the processed range is the last specified range
if (delim_cpus != std::string::npos) {
current_pos = delim_cpus + 1;
delim_cpus = cpu_str.find(",", current_pos);
} else {
break;
}
}
}
LOG_VERBOSE(1) << "Thread is binding to one of the CPUs: "
<< VectorToString(cpus);
numa_set = true;
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
for (int cpu : cpus) {
CPU_SET(cpu, &cpuset);
}
if (pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset) != 0) {
return Status(
Status::Code::INTERNAL,
std::string("Unable to set NUMA thread affinity: ") +
strerror(errno));
}
}
return Status::Success;
}
#endif
}} // namespace triton::core