如何在 ARM Android 上获取缓存行大小?这等效于以下页面,但专门针对 Android:

Programmatically get the cache line size?

该页面上的答案以及我知道的其他方式不适用于 Android:

  • /sys/devices/system/cpu/cpu0/cache/ 不存在。
  • _SC_LEVEL1_DCACHE_LINESIZE 不作为 sysconf 参数存在,即使我手动传入值 190
  • AT_DCACHEBSIZE 不作为 getauxval 参数存在,即使我手动传入值 19
  • /proc/cpuinfo 不包含缓存行信息。

  • 与 x86 不同,ARM 的 CPU 信息仅在内核模式下可用,因此应用程序没有可用的 cpuid 等效项。

    最佳答案

    我进行了一个小调查,发现了一些东西:

    首先,似乎带有 sysconf()_SC_LEVEL1_ICACHE_SIZE_SC_LEVEL1_ICACHE_ASSOC 或其他 CPU 缓存相关标志的 _SC_LEVEL1_ICACHE_LINESIZE 总是返回 -1(有时可能是 0)和 it seems to be the reason for this ,它们根本没有实现。

    但是有一个解决方案。如果您可以在项目中使用 JNI,请使用 this library。这个库对于检索有关 CPU 的信息非常有帮助(我的设备和山一样古老):
    android - 以编程方式获取 Android 上的缓存行大小-LMLPHP

    这是我用来获取有关我的 CPU 缓存的信息的代码:

    #include <string>
    #include <sstream>
    #include <cpuinfo.h>
    
    void get_cache_info(const char* name, const struct cpuinfo_cache* cache, std::ostringstream& oss)
    {
        oss << "CPU Cache: " << name << std::endl;
        oss << " > size            : " << cache->size << std::endl;
        oss << " > associativity   : " << cache->associativity << std::endl;
        oss << " > sets            : " << cache->sets << std::endl;
        oss << " > partitions      : " << cache->partitions << std::endl;
        oss << " > line_size       : " << cache->line_size << std::endl;
        oss << " > flags           : " << cache->flags << std::endl;
        oss << " > processor_start : " << cache->processor_start << std::endl;
        oss << " > processor_count : " << cache->processor_count << std::endl;
        oss << std::endl;
    }
    
    const std::string get_cpu_info()
    {
        cpuinfo_initialize();
        const struct cpuinfo_processor* proc = cpuinfo_get_current_processor();
    
        std::ostringstream oss;
    
        if (proc->cache.l1d)
            get_cache_info("L1 Data", proc->cache.l1d, oss);
    
        if (proc->cache.l1i)
            get_cache_info("L1 Instruction", proc->cache.l1i, oss);
    
        if (proc->cache.l2)
            get_cache_info("L2", proc->cache.l2, oss);
    
        if (proc->cache.l3)
            get_cache_info("L3", proc->cache.l3, oss);
    
        if (proc->cache.l4)
            get_cache_info("L4", proc->cache.l4, oss);
    
        return oss.str();
    }
    

    关于android - 以编程方式获取 Android 上的缓存行大小,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49619909/

    10-11 22:16
    查看更多