Skip to content

Commit e65358c

Browse files
kimishpatelfacebook-github-bot
authored andcommitted
Add util to find out # of performant cores (#2350)
Summary: 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 //unrelated failures github-bypass-export-checks ghstack-source-id: 218812793 exported-using-ghexport Reviewed By: digantdesai, kirklandsign Differential Revision: D54766068 fbshipit-source-id: 5bba8d763ccd414a897c477e6e49f7ed0159bf05
1 parent 2458717 commit e65358c

File tree

3 files changed

+220
-0
lines changed

3 files changed

+220
-0
lines changed
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
#include <executorch/runtime/platform/assert.h>
10+
#include <fstream>
11+
#include <mutex>
12+
#include <string>
13+
#include <vector>
14+
15+
#include "cpuinfo_utils.h"
16+
17+
namespace torch {
18+
namespace executorch {
19+
namespace cpuinfo {
20+
21+
// Ignore revisions (last digit (4 LSBs))
22+
#define CPUINFO_ARM_MIDR_CORTEX_A520 UINT32_C(0x410FD800)
23+
#define CPUINFO_ARM_MIDR_CORTEX_A53 UINT32_C(0x410FD030)
24+
#define CPUINFO_ARM_MIDR_CORTEX_A55 UINT32_C(0x410FD050)
25+
#define CPUINFO_ARM_MIDR_CORTEX_A57 UINT32_C(0x410FD070)
26+
27+
#define RIVISION_MASK UINT32_C(0xFFFFFFF0)
28+
29+
namespace {
30+
bool is_non_performant_core(const struct cpuinfo_uarch_info* uarch_info) {
31+
switch (uarch_info->uarch) {
32+
case cpuinfo_uarch_cortex_a55:
33+
case cpuinfo_uarch_cortex_a53:
34+
case cpuinfo_uarch_cortex_a510:
35+
return true;
36+
// This can be so many other cores.
37+
// Need to update this to better account for slow cores
38+
// Also does not account Apple's A/M series cores
39+
// And not yet qcomm's
40+
default:
41+
break;
42+
}
43+
// A520 is not yet updated in cpuinfo
44+
// Hence decode it separately.
45+
#if CPUINFO_ARCH_ARM || CPUINFO_ARCH_ARM64
46+
if ((uarch_info->midr & RIVISION_MASK) == CPUINFO_ARM_MIDR_CORTEX_A520) {
47+
return true;
48+
}
49+
#endif
50+
return false;
51+
}
52+
53+
std::vector<uint32_t>* get_static_cpu_midr_vector() {
54+
static std::vector<uint32_t> cpu_midrs;
55+
return &cpu_midrs;
56+
}
57+
58+
uint32_t _get_model_specific_num_cores() {
59+
// Not sure how reliable this is but going with it for now.
60+
const std::string kImageVersionPath = "/sys/devices/soc0/image_version";
61+
ET_LOG(Info, "Reading file %s", kImageVersionPath.c_str());
62+
std::fstream image_version_file(kImageVersionPath, std::ios_base::in);
63+
if (image_version_file.is_open()) {
64+
std::string x;
65+
std::getline(image_version_file, x);
66+
// Hardcoding some rules for now
67+
if (x.find("S911") != std::string::npos) {
68+
// Samsung S23 has:
69+
// 1x3.36 GHz Cortex-X3
70+
// 2x2.8 GHz Cortex-A715
71+
// 2x2.8 GHz Cortex-A710
72+
// 3x2.0 GHz Cortex-A510
73+
// And we have balanced execution with 4 cores.
74+
return 4;
75+
}
76+
}
77+
ET_LOG(Info, "Failed to open midr file %s", kImageVersionPath.c_str());
78+
return 0;
79+
}
80+
81+
bool populate_available_cpu_mids() {
82+
std::vector<uint32_t>* cpu_midrs = get_static_cpu_midr_vector();
83+
uint32_t num_possible_cores = cpuinfo_get_processors_count();
84+
cpu_midrs->resize(num_possible_cores);
85+
const std::string kMidrFilePathPrefix = "/sys/devices/system/cpu/cpu";
86+
const std::string kMidrFilePathSuffix = "/regs/identification/midr_el1";
87+
for (int32_t i = 0; i < num_possible_cores; ++i) {
88+
std::string midr_file_path =
89+
kMidrFilePathPrefix + std::to_string(i) + kMidrFilePathSuffix;
90+
ET_LOG(Info, "Reading file %s", midr_file_path.c_str());
91+
std::fstream midr_file(midr_file_path, std::ios_base::in);
92+
uint32_t tmp{0};
93+
if (midr_file.is_open()) {
94+
std::string x;
95+
std::getline(midr_file, x);
96+
tmp = std::stoi(x, nullptr, 16);
97+
(*cpu_midrs)[i] = tmp;
98+
} else {
99+
ET_LOG(Info, "Failed to open midr file %s", midr_file_path.c_str());
100+
cpu_midrs->clear();
101+
return false;
102+
}
103+
}
104+
return true;
105+
}
106+
107+
uint32_t _get_num_performant_cores() {
108+
// @lint-ignore CLANGTIDY facebook-hte-std::once_flag
109+
static std::once_flag flag;
110+
// @lint-ignore CLANGTIDY facebook-hte-std::call_once
111+
std::call_once(flag, []() { populate_available_cpu_mids(); });
112+
std::vector<uint32_t>* cpu_midrs = get_static_cpu_midr_vector();
113+
uint32_t num_possible_cores = cpuinfo_get_processors_count();
114+
if (num_possible_cores != cpu_midrs->size()) {
115+
ET_LOG(Info, "CPU info and manual query on # of cpus dont match.");
116+
return 0;
117+
}
118+
for (int32_t i = 0; i < cpu_midrs->size(); ++i) {
119+
uint32_t masked_midr = (*cpu_midrs)[i] & RIVISION_MASK;
120+
switch (masked_midr) {
121+
case CPUINFO_ARM_MIDR_CORTEX_A520:
122+
case CPUINFO_ARM_MIDR_CORTEX_A53:
123+
case CPUINFO_ARM_MIDR_CORTEX_A55:
124+
case CPUINFO_ARM_MIDR_CORTEX_A57:
125+
num_possible_cores--;
126+
break;
127+
default:
128+
break;
129+
}
130+
}
131+
return num_possible_cores;
132+
}
133+
134+
} // namespace
135+
136+
uint32_t get_num_performant_cores() {
137+
ET_CHECK_MSG(cpuinfo_initialize(), "cpuinfo cannot be initialized.");
138+
// First try and see if we have number of cores profiled for this specific
139+
// device
140+
uint32_t model_specific_num_cores = _get_model_specific_num_cores();
141+
if (model_specific_num_cores > 0) {
142+
return model_specific_num_cores;
143+
}
144+
145+
// Else looks at either the # of litte cores if found
146+
// Or parse the midr in "Something seems wrong" section.
147+
const uint32_t uarch_count = cpuinfo_get_uarchs_count();
148+
uint32_t num_possible_cores = cpuinfo_get_processors_count();
149+
uint32_t num_non_performant_core = 0;
150+
if (uarch_count > 1) {
151+
for (int32_t i = 0; i < uarch_count; ++i) {
152+
const struct cpuinfo_uarch_info* uarch_info = cpuinfo_get_uarch(i);
153+
if (is_non_performant_core(uarch_info)) {
154+
num_non_performant_core += uarch_info->processor_count;
155+
}
156+
}
157+
ET_LOG(Info, "Number of efficient cores %d", num_non_performant_core);
158+
if (num_possible_cores <= num_non_performant_core) {
159+
ET_LOG(
160+
Info, "Total number of cores must be larger than efficient cores.");
161+
return 0;
162+
}
163+
return (num_possible_cores - num_non_performant_core);
164+
} else {
165+
// Something seems wrong. Lets check each processor's midr
166+
// In one plua 12 while it has 2 little cores, the topology
167+
// reported in /sys/devices/system/cpu/cpu* /topology/core_siblings_list
168+
// report wrong topology which results in wront configratuon
169+
return _get_num_performant_cores();
170+
}
171+
}
172+
173+
} // namespace cpuinfo
174+
} // namespace executorch
175+
} // namespace torch
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
#pragma once
10+
11+
#include <cpuinfo.h>
12+
13+
namespace torch {
14+
namespace executorch {
15+
namespace cpuinfo {
16+
17+
uint32_t get_num_performant_cores();
18+
19+
} // namespace cpuinfo
20+
} // namespace executorch
21+
} // 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)