Skip to content

Commit 00ff5f6

Browse files
committed
[Execuotrch][cpuinfo] Add util to find out # of performant cores
Pull Request resolved: #2350 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 ghstack-source-id: 218743677 @exported-using-ghexport Differential Revision: [D54766068](https://our.internmc.facebook.com/intern/diff/D54766068/)
1 parent e98a7e0 commit 00ff5f6

File tree

3 files changed

+204
-0
lines changed

3 files changed

+204
-0
lines changed
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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+
uint32_t _get_model_specific_num_cores() {
52+
// Not sure how reliable this is but going with it for now.
53+
const std::string kImageVersionPath = "/sys/devices/soc0/image_version";
54+
ET_LOG(Info, "Reading file %s", kImageVersionPath.c_str());
55+
std::fstream image_version_file(kImageVersionPath, std::ios_base::in);
56+
uint32_t tmp{0};
57+
if (image_version_file.is_open()) {
58+
std::string x;
59+
std::getline(image_version_file, x);
60+
// Hardcoding some rules for now
61+
if (x.find("S911") != std::string::npos) {
62+
// Samsung S23 has:
63+
// 1x3.36 GHz Cortex-X3
64+
// 2x2.8 GHz Cortex-A715
65+
// 2x2.8 GHz Cortex-A710
66+
// 3x2.0 GHz Cortex-A510
67+
// And we have balanced execution with 4 cores.
68+
return 4;
69+
}
70+
}
71+
ET_LOG(Info, "Failed to open midr file %s", kImageVersionPath.c_str());
72+
return 0;
73+
}
74+
75+
bool populate_available_cpu_mids() {
76+
std::vector<uint32_t>* cpu_midrs = get_static_cpu_midr_vector();
77+
uint32_t num_possible_cores = cpuinfo_get_processors_count();
78+
cpu_midrs->resize(num_possible_cores);
79+
const std::string kMidrFilePathPrefix = "/sys/devices/system/cpu/cpu";
80+
const std::string kMidrFilePathSuffix = "/regs/identification/midr_el1";
81+
for (int32_t i = 0; i < num_possible_cores; ++i) {
82+
std::string midr_file_path =
83+
kMidrFilePathPrefix + std::to_string(i) + kMidrFilePathSuffix;
84+
ET_LOG(Info, "Reading file %s", midr_file_path.c_str());
85+
std::fstream midr_file(midr_file_path, std::ios_base::in);
86+
uint32_t tmp{0};
87+
if (midr_file.is_open()) {
88+
std::string x;
89+
std::getline(midr_file, x);
90+
tmp = std::stoi(x, nullptr, 16);
91+
(*cpu_midrs)[i] = tmp;
92+
} else {
93+
ET_LOG(Info, "Failed to open midr file %s", midr_file_path.c_str());
94+
cpu_midrs->clear();
95+
return false;
96+
}
97+
}
98+
return true;
99+
}
100+
101+
uint32_t _get_num_performant_cores() {
102+
static std::once_flag flag;
103+
std::call_once(flag, []() { populate_available_cpu_mids(); });
104+
std::vector<uint32_t>* cpu_midrs = get_static_cpu_midr_vector();
105+
uint32_t num_possible_cores = cpuinfo_get_processors_count();
106+
if (num_possible_cores != cpu_midrs->size()) {
107+
ET_LOG(Info, "CPU info and manual query on # of cpus dont match.");
108+
return 0;
109+
}
110+
for (int32_t i = 0; i < cpu_midrs->size(); ++i) {
111+
uint32_t masked_midr = (*cpu_midrs)[i] & RIVISION_MASK;
112+
switch (masked_midr) {
113+
case CPUINFO_ARM_MIDR_CORTEX_A520:
114+
case CPUINFO_ARM_MIDR_CORTEX_A53:
115+
case CPUINFO_ARM_MIDR_CORTEX_A55:
116+
case CPUINFO_ARM_MIDR_CORTEX_A57:
117+
num_possible_cores--;
118+
break;
119+
default:
120+
break;
121+
}
122+
}
123+
return num_possible_cores;
124+
}
125+
126+
} // namespace
127+
128+
uint32_t get_num_performant_cores() {
129+
ET_CHECK_MSG(cpuinfo_initialize(), "cpuinfo cannot be initialized.");
130+
// First try and see if we have number of cores profiled for this specific
131+
// device
132+
uint32_t model_specific_num_cores = _get_model_specific_num_cores();
133+
if (model_specific_num_cores > 0) {
134+
return model_specific_num_cores;
135+
}
136+
137+
// Else looks at either the # of litte cores if found
138+
// Or parse the midr in "Something seems wrong" section.
139+
const uint32_t uarch_count = cpuinfo_get_uarchs_count();
140+
uint32_t num_possible_cores = cpuinfo_get_processors_count();
141+
uint32_t num_non_performant_core = 0;
142+
if (uarch_count > 1) {
143+
for (int32_t i = 0; i < uarch_count; ++i) {
144+
const struct cpuinfo_uarch_info* uarch_info = cpuinfo_get_uarch(i);
145+
if (is_non_performant_core(uarch_info)) {
146+
num_non_performant_core += uarch_info->processor_count;
147+
}
148+
}
149+
ET_LOG(Info, "Number of efficient cores %d", num_non_performant_core);
150+
if (num_possible_cores <= num_non_performant_core) {
151+
ET_LOG(
152+
Info, "Total number of cores must be larger than efficient cores.");
153+
return 0;
154+
}
155+
return (num_possible_cores - num_non_performant_core);
156+
} else {
157+
// Something seems wrong. Lets check each processor's midr
158+
// In one plua 12 while it has 2 little cores, the topology
159+
// reported in /sys/devices/system/cpu/cpu* /topology/core_siblings_list
160+
// report wrong topology which results in wront configratuon
161+
return _get_num_performant_cores();
162+
}
163+
}
164+
165+
} // namespace cpuinfo
166+
} // namespace executorch
167+
} // 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)