Skip to content

Commit daa9de2

Browse files
authored
Fix: fiber not starting in lxcfs environment with CPU limits (#57)
* Fix: fiber not starting in lxcfs environment with CPU limits * Style: fix variable naming
1 parent 753e4d9 commit daa9de2

3 files changed

Lines changed: 25 additions & 17 deletions

File tree

trpc/runtime/threadmodel/fiber/fiber_thread_model.cc

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ namespace trpc::fiber {
3232

3333
namespace {
3434

35+
const std::vector<unsigned> kNoAffinity;
36+
3537
std::uint64_t DivideRoundUp(std::uint64_t divisor, std::uint64_t dividend) {
3638
return divisor / dividend + (divisor % dividend != 0);
3739
}
@@ -88,7 +90,7 @@ void FiberThreadModel::Start() noexcept {
8890
InitializeConcurrency();
8991
InitializeNumaAwareness();
9092

91-
bool is_scheduling_group_size_setted = options_.scheduling_group_size != 0;
93+
bool is_scheduling_group_size_set = options_.scheduling_group_size != 0;
9294
InitializeSchedulingGroupSize();
9395

9496
// If CPU migration is explicit disallowed, we need to make sure there are
@@ -105,7 +107,7 @@ void FiberThreadModel::Start() noexcept {
105107
if (options_.numa_aware) {
106108
StartWorkersNuma();
107109
} else {
108-
StartWorkersUma(is_scheduling_group_size_setted);
110+
StartWorkersUma(is_scheduling_group_size_set);
109111
}
110112

111113
// Fill `flatten_scheduling_groups_`.
@@ -266,28 +268,33 @@ void FiberThreadModel::InitializeForeignSchedulingGroups(
266268
}
267269
}
268270

269-
void FiberThreadModel::StartWorkersUma(bool is_scheduling_group_size_setted) {
271+
void FiberThreadModel::StartWorkersUma(bool is_scheduling_group_size_set) {
270272
std::uint64_t groups = 1;
271-
if (is_scheduling_group_size_setted) {
273+
if (is_scheduling_group_size_set) {
272274
groups = options_.concurrency_hint < options_.scheduling_group_size
273-
? 1
274-
: DivideRoundUp(options_.concurrency_hint, options_.scheduling_group_size);
275+
? 1
276+
: DivideRoundUp(options_.concurrency_hint, options_.scheduling_group_size);
275277
}
276278
TRPC_FMT_DEBUG("Starting {} worker threads per group, for a total of {} groups.", options_.scheduling_group_size,
277279
groups);
278280
TRPC_FMT_WARN_IF(options_.worker_disallow_cpu_migration && GetFiberWorkerAccessibleNodes().size() > 1,
279281
"CPU migration of fiber worker is disallowed, and we're trying to start "
280282
"in UMA way on NUMA architecture. Performance will likely degrade.");
283+
const auto& cpus = GetFiberWorkerAccessibleCPUs();
284+
TRPC_FMT_WARN_IF(options_.worker_disallow_cpu_migration && cpus.size() < GetNumberOfProcessorsAvailable(),
285+
"CPU migration of fiber worker is disallowed, and we're trying to start "
286+
"with CPU {} in the system. Enable migration if affinity is not desired.",
287+
StringifyCPUs(cpus));
281288

282289
for (std::size_t index = 0; index != groups; ++index) {
283290
if (!options_.worker_disallow_cpu_migration) {
284-
scheduling_groups_[0].push_back(CreateFullyFledgedSchedulingGroup(0, index, GetFiberWorkerAccessibleCPUs()));
291+
scheduling_groups_[0].push_back(CreateFullyFledgedSchedulingGroup(
292+
0, index, cpus.size() < GetNumberOfProcessorsAvailable() ? cpus : kNoAffinity));
285293
} else {
286294
// Each group of processors is dedicated to a scheduling group.
287295
//
288296
// Later when we start the fiber workers, we'll instruct them to set their
289297
// affinity to their dedicated processors.
290-
auto&& cpus = GetFiberWorkerAccessibleCPUs();
291298
TRPC_CHECK_LE((index + 1) * options_.scheduling_group_size, cpus.size());
292299
scheduling_groups_[0].push_back(
293300
CreateFullyFledgedSchedulingGroup(0, index,
@@ -383,16 +390,17 @@ std::vector<unsigned> FiberThreadModel::GetFiberWorkerAccessibleCPUsImpl() {
383390
}
384391

385392
auto affinity = GetCurrentThreadAffinity();
386-
if (affinity.size() && affinity.size() != GetNumberOfProcessorsConfigured()) {
387-
return affinity;
393+
if (affinity.empty()) {
394+
affinity.resize(GetNumberOfProcessorsConfigured());
395+
std::iota(affinity.begin(), affinity.end(), 0);
388396
}
389397

390398
std::vector<unsigned> result;
391-
for (std::size_t i = 0; i != GetNumberOfProcessorsConfigured(); ++i) {
392-
if (IsProcessorAccessible(i)) {
393-
result.push_back(i);
394-
} else { // Containerized environment otherwise?
395-
TRPC_FMT_DEBUG("Processor #{} is not accessible to us, ignored.", i);
399+
for (unsigned cpu : affinity) {
400+
if (IsProcessorAccessible(cpu)) {
401+
result.push_back(cpu);
402+
} else { // containerized environment?
403+
TRPC_FMT_DEBUG("Processor #{} is not accessible to us, ignored.", cpu);
396404
}
397405
}
398406
return result;

trpc/runtime/threadmodel/fiber/fiber_thread_model.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ class FiberThreadModel final : public ThreadModel {
139139
void InitializeConcurrency();
140140
void InitializeNumaAwareness();
141141
void InitializeSchedulingGroupSize();
142-
void StartWorkersUma(bool is_scheduling_group_size_setted);
142+
void StartWorkersUma(bool is_scheduling_group_size_set);
143143
void StartWorkersNuma();
144144
bool IsNumaAwareQualified();
145145
const std::vector<numa::Node>& GetFiberWorkerAccessibleNodes();

trpc/util/thread/cpu.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ std::size_t GetNumberOfProcessorsConfigured() {
267267

268268
bool IsInaccessibleProcessorPresent() { return inaccessible_cpus_present; }
269269

270-
bool IsProcessorAccessible(unsigned cpu) { return node_of_cpus[cpu] != -1; }
270+
bool IsProcessorAccessible(unsigned cpu) { return cpu < node_of_cpus.size() && node_of_cpus[cpu] != -1; }
271271

272272
std::optional<std::vector<unsigned>> TryParseProcesserList(const std::string& s) {
273273
std::vector<unsigned> result;

0 commit comments

Comments
 (0)