Skip to content

Commit 289d709

Browse files
committed
[Execuotrch][cpuinfo] Add util to find out # of performant cores
This helps with identifying the # of performant cores which will later be used when reseting the threadpool or using the guard to figure out how many threads should be used Differential Revision: [D54766068](https://our.internmc.facebook.com/intern/diff/D54766068/) ghstack-source-id: 218209329 Pull Request resolved: #2350
1 parent f84bd6a commit 289d709

File tree

3 files changed

+171
-0
lines changed

3 files changed

+171
-0
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#include <executorch/runtime/platform/assert.h>
2+
#include <fstream>
3+
#include <iostream>
4+
#include <mutex>
5+
#include <string>
6+
#include <vector>
7+
8+
#include "cpuinfo_utils.h"
9+
10+
namespace torch {
11+
namespace executorch {
12+
namespace cpuinfo {
13+
14+
// Ignore revisions (last digit (4 LSBs))
15+
#define CPUINFO_ARM_MIDR_CORTEX_A520 UINT32_C(0x410FD800)
16+
#define CPUINFO_ARM_MIDR_CORTEX_A53 UINT32_C(0x410FD030)
17+
#define CPUINFO_ARM_MIDR_CORTEX_A55 UINT32_C(0x410FD050)
18+
#define CPUINFO_ARM_MIDR_CORTEX_A57 UINT32_C(0x410FD070)
19+
20+
#define RIVISION_MASK UINT32_C(0xFFFFFFF0)
21+
22+
namespace {
23+
bool is_non_performant_core(const struct cpuinfo_uarch_info* uarch_info) {
24+
switch (uarch_info->uarch) {
25+
case cpuinfo_uarch_cortex_a55:
26+
case cpuinfo_uarch_cortex_a53:
27+
case cpuinfo_uarch_cortex_a510:
28+
return true;
29+
// This can be so many other cores.
30+
// Need to update this to better account for slow cores
31+
// Also does not account Apple's A/M series cores
32+
// And not yet qcomm's
33+
default:
34+
break;
35+
}
36+
// A520 is not yet updated in cpuinfo
37+
// Hence decode it separately.
38+
#if CPUINFO_ARCH_ARM || CPUINFO_ARCH_ARM64
39+
if ((uarch_info->midr & RIVISION_MASK) == CPUINFO_ARM_MIDR_CORTEX_A520) {
40+
return true;
41+
}
42+
#endif
43+
return false;
44+
}
45+
46+
std::vector<uint32_t>* get_static_cpu_midr_vector() {
47+
static std::vector<uint32_t> cpu_midrs;
48+
return &cpu_midrs;
49+
}
50+
51+
bool populate_available_cpu_mids() {
52+
std::vector<uint32_t>* cpu_midrs = get_static_cpu_midr_vector();
53+
uint32_t num_possible_cores = cpuinfo_get_processors_count();
54+
cpu_midrs->resize(num_possible_cores);
55+
const std::string kMidrFilePathPrefix = "/sys/devices/system/cpu/cpu";
56+
const std::string kMidrFilePathSuffix = "/regs/identification/midr_el1";
57+
for (int32_t i = 0; i < num_possible_cores; ++i) {
58+
std::string midr_file_path =
59+
kMidrFilePathPrefix + std::to_string(i) + kMidrFilePathSuffix;
60+
ET_LOG(Info, "Reading file %s", midr_file_path.c_str());
61+
std::fstream midr_file(midr_file_path, std::ios_base::in);
62+
uint32_t tmp{0};
63+
if (midr_file.is_open()) {
64+
std::string x;
65+
std::getline(midr_file, x);
66+
tmp = std::stoi(x, nullptr, 16);
67+
(*cpu_midrs)[i] = tmp;
68+
} else {
69+
ET_LOG(Info, "Failed to open midr file %s", midr_file_path.c_str());
70+
cpu_midrs->clear();
71+
return false;
72+
}
73+
}
74+
return true;
75+
}
76+
77+
uint32_t _get_num_performant_cores() {
78+
static std::once_flag flag;
79+
std::call_once(flag, []() { populate_available_cpu_mids(); });
80+
std::vector<uint32_t>* cpu_midrs = get_static_cpu_midr_vector();
81+
uint32_t num_possible_cores = cpuinfo_get_processors_count();
82+
if (num_possible_cores != cpu_midrs->size()) {
83+
ET_LOG(Info, "CPU info and manual query on # of cpus dont match.");
84+
return 0;
85+
}
86+
for (int32_t i = 0; i < cpu_midrs->size(); ++i) {
87+
uint32_t masked_midr = (*cpu_midrs)[i] & RIVISION_MASK;
88+
switch (masked_midr) {
89+
case CPUINFO_ARM_MIDR_CORTEX_A520:
90+
case CPUINFO_ARM_MIDR_CORTEX_A53:
91+
case CPUINFO_ARM_MIDR_CORTEX_A55:
92+
case CPUINFO_ARM_MIDR_CORTEX_A57:
93+
num_possible_cores--;
94+
break;
95+
default:
96+
break;
97+
}
98+
}
99+
return num_possible_cores;
100+
}
101+
102+
} // namespace
103+
104+
uint32_t get_num_performant_cores() {
105+
ET_CHECK_MSG(cpuinfo_initialize(), "cpuinfo cannot be initialized.");
106+
const uint32_t uarch_count = cpuinfo_get_uarchs_count();
107+
uint32_t num_possible_cores = cpuinfo_get_processors_count();
108+
uint32_t num_non_performant_core = 0;
109+
if (uarch_count > 1) {
110+
for (int32_t i = 0; i < uarch_count; ++i) {
111+
const struct cpuinfo_uarch_info* uarch_info = cpuinfo_get_uarch(i);
112+
if (is_non_performant_core(uarch_info)) {
113+
num_non_performant_core += uarch_info->processor_count;
114+
}
115+
}
116+
ET_LOG(Info, "Number of efficient cores %d", num_non_performant_core);
117+
if (num_possible_cores <= num_non_performant_core) {
118+
ET_LOG(
119+
Info, "Total number of cores must be larger than efficient cores.");
120+
return 0;
121+
}
122+
return (num_possible_cores - num_non_performant_core);
123+
} else {
124+
// Something seems wrong. Lets check each processor's midr
125+
// In one plua 12 while it has 2 little cores, the topology
126+
// reported in /sys/devices/system/cpu/cpu* /topology/core_siblings_list
127+
// report wrong topology which results in wront configratuon
128+
return _get_num_performant_cores();
129+
}
130+
}
131+
132+
} // namespace cpuinfo
133+
} // namespace executorch
134+
} // namespace torch
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
#include <cpuinfo.h>
4+
5+
namespace torch {
6+
namespace executorch {
7+
namespace cpuinfo {
8+
9+
uint32_t get_num_performant_cores();
10+
11+
} // namespace cpuinfo
12+
} // namespace executorch
13+
} // namespace torch

backends/xnnpack/threadpool/targets.bzl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,27 @@ def define_common_targets():
4040
"@EXECUTORCH_CLIENTS",
4141
],
4242
)
43+
44+
runtime.cxx_library(
45+
name = "cpuinfo_utils",
46+
srcs = [
47+
"cpuinfo_utils.cpp",
48+
],
49+
deps = [
50+
"//executorch/runtime/core:core",
51+
],
52+
exported_headers = [
53+
"cpuinfo_utils.h",
54+
],
55+
exported_deps = [
56+
third_party_dep("pthreadpool"),
57+
third_party_dep("cpuinfo"),
58+
],
59+
visibility = [
60+
"//executorch/...",
61+
"//executorch/backends/...",
62+
"//executorch/runtime/backend/...",
63+
"//executorch/extension/threadpool/test/...",
64+
"@EXECUTORCH_CLIENTS",
65+
],
66+
)

0 commit comments

Comments
 (0)