在linux系统中查看cacheline的大小

为了上cache机制在程序运行时失效,因此将数据大小设置为cacheline的大小,避免cache命中,但是有关cacheline大小的信息,一直不知道怎么获取,于是在linux中查找,发现实验室路径下的一些关键信息。

在/sys/devices/system/cpu/cpu1/cache路径下,有index文件夹,者分别L1数据cache,L1指令cache,L2cache,L3cache

每个文件夹下有多个cache相关信息,可以查看,比如 index0

其中第一个文件夹 就是cacheline_size 这就是要找的信息。

直接在命令行:cat /sys/devices/system/cpu/cpu1/cache/index0/coherency_line_size 

会打印出64,这就是cacheline大小为64字节

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 C 语言,获取 cacheline大小需要使用特定的操作系统相关的函数或者编译器相关的宏。以下是一些常见的方法: 1. 使用 Linux 系统的 sysconf() 函数获取 cacheline 大小: ```c #include <unistd.h> #include <stdio.h> int main() { long cacheline_size = sysconf(_SC_LEVEL1_DCACHE_LINESIZE); printf("cacheline size on this system is %ld bytes\n", cacheline_size); return 0; } ``` 2. 使用 GCC 编译器的 __builtin_cpu_supports() 函数获取 cacheline 大小: ```c #include <stdio.h> #include <stdint.h> #include <stdbool.h> int main() { bool has_cacheline_size = false; uint32_t size = 0; if (__builtin_cpu_supports("avx")) { size = __builtin_ia32_cpuid_count(0x80000006, 0, &eax, &ebx, &ecx, &edx); if (size && (eax & 0xff) > 0) { has_cacheline_size = true; size = (eax & 0xff) * 64; } } if (has_cacheline_size) { printf("cacheline size on this system is %u bytes\n", size); } else { printf("cannot determine cacheline size on this system\n"); } return 0; } ``` 注意:这个方法需要编译器支持 AVX 指令集。 3. 使用 Windows 系统的 GetLogicalProcessorInformation() 函数获取 cacheline 大小: ```c #include <windows.h> #include <stdio.h> int main() { PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL; DWORD length = 0; BOOL result = GetLogicalProcessorInformation(buffer, &length); if (!result && GetLastError() == ERROR_INSUFFICIENT_BUFFER) { buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(length); result = GetLogicalProcessorInformation(buffer, &length); } if (result) { DWORD offset = 0; while (offset < length) { PSYSTEM_LOGICAL_PROCESSOR_INFORMATION info = buffer + offset / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); if (info->Relationship == RelationCache && info->Cache.Level == 1 && info->Cache.Type == CacheData) { printf("cacheline size on this system is %u bytes\n", info->Cache.LineSize); break; } offset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); } } else { printf("cannot determine cacheline size on this system\n"); } free(buffer); return 0; } ``` 注意:这个方法需要在 Windows 系统上运行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值