xref: /aosp_15_r20/external/executorch/extension/threadpool/cpuinfo_utils.cpp (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
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/extension/threadpool/cpuinfo_utils.h>
10 
11 #include <fstream>
12 #include <mutex>
13 #include <string>
14 #include <vector>
15 
16 #include <executorch/runtime/platform/assert.h>
17 
18 namespace executorch::extension::cpuinfo {
19 
20 // Ignore revisions (last digit (4 LSBs))
21 #define CPUINFO_ARM_MIDR_CORTEX_A520 UINT32_C(0x410FD800)
22 #define CPUINFO_ARM_MIDR_CORTEX_A53 UINT32_C(0x410FD030)
23 #define CPUINFO_ARM_MIDR_CORTEX_A55 UINT32_C(0x410FD050)
24 #define CPUINFO_ARM_MIDR_CORTEX_A57 UINT32_C(0x410FD070)
25 
26 #define RIVISION_MASK UINT32_C(0xFFFFFFF0)
27 
28 namespace {
is_non_performant_core(const struct cpuinfo_uarch_info * uarch_info)29 bool is_non_performant_core(const struct cpuinfo_uarch_info* uarch_info) {
30   switch (uarch_info->uarch) {
31     case cpuinfo_uarch_cortex_a55:
32     case cpuinfo_uarch_cortex_a53:
33     case cpuinfo_uarch_cortex_a510:
34     case cpuinfo_uarch_icestorm:
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 
get_static_cpu_midr_vector()53 std::vector<uint32_t>* get_static_cpu_midr_vector() {
54   static std::vector<uint32_t> cpu_midrs;
55   return &cpu_midrs;
56 }
57 
_get_model_specific_num_cores()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 
populate_available_cpu_mids()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 
_get_num_performant_cores()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 
get_num_performant_cores()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 executorch::extension::cpuinfo
174