我进行了一次小调查,发现了一些东西:
首先,看起来
sysconf()
具有
_SC_LEVEL1_ICACHE_SIZE
,则,
_SC_LEVEL1_ICACHE_ASSOC
,则,
_SC_LEVEL1_ICACHE_LINESIZE
或其他与CPU缓存相关的标志始终返回-1(有时可能是0)和
it seems to be the reason for this
,它们根本没有实现。
但有一个解决方案。使用
this library
如果您能够在项目中使用JNI。此库对于检索有关CPU的信息非常有用(我的设备非常古老):
下面是我用来获取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();
}