|
| 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 |
0 commit comments