跨平台获取CPU cache line大小的方法
来源:互联网 发布:百叶窗js 编辑:程序博客网 时间:2024/05/27 20:42
Reprint:http://strupat.ca/2010/10/cross-platform-function-to-get-the-line-size-of-your-cache/
注意获得的cache line大小单位为字节。
#ifndef GET_CACHE_LINE_SIZE_H_INCLUDED#define GET_CACHE_LINE_SIZE_H_INCLUDED // Author: Nick Strupat// Date: October 29, 2010// Returns the cache line size (in bytes) of the processor, or 0 on failure #include <stddef.h>size_t cache_line_size(); #if defined(__APPLE__) #include <sys/sysctl.h>size_t cache_line_size() {size_t line_size = 0;size_t sizeof_line_size = sizeof(line_size);sysctlbyname("hw.cachelinesize", &line_size, &sizeof_line_size, 0, 0);return line_size;} #elif defined(_WIN32) #include <stdlib.h>#include <windows.h>size_t cache_line_size() {size_t line_size = 0;DWORD buffer_size = 0;DWORD i = 0;SYSTEM_LOGICAL_PROCESSOR_INFORMATION * buffer = 0; GetLogicalProcessorInformation(0, &buffer_size);buffer = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION *)malloc(buffer_size);GetLogicalProcessorInformation(&buffer[0], &buffer_size); for (i = 0; i != buffer_size / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); ++i) {if (buffer[i].Relationship == RelationCache && buffer[i].Cache.Level == 1) {line_size = buffer[i].Cache.LineSize;break;}} free(buffer);return line_size;} #elif defined(linux) #include <stdio.h>size_t cache_line_size() {FILE * p = 0;p = fopen("/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size", "r");unsigned int i = 0;if (p) {fscanf(p, "%d", &i);fclose(p);}return i;} #else#error Unrecognized platform#endif #endif
- 跨平台获取CPU cache line大小的方法
- CPU的cache line原理
- CPU Cache line
- CPU Cache line
- cpu cache line 原理
- cpu cache line 原理
- cpu cache line 原理
- cpu cache line 原理
- 查看CPU Cache大小
- 查看CPU Cache大小
- 查看CPU Cache大小
- 关于CPU Cache和Cache Line
- cpu性能探究 :cache line 原理
- CPU高速缓存行对齐(cache line)
- 7个示例科普CPU Cache line
- 7个示例科普CPU Cache line
- 7个示例科普CPU Cache line
- 7个示例科普CPU Cache line
- poj1459Power Network(最大流)
- 学习learn python the hard way习题46, ImportError问题
- W3C之SQL函数
- HDU 1078 FatMouse and Cheese
- 程序员编程艺术第三十二~三十三章:最小操作数,木块砌墙问题
- 跨平台获取CPU cache line大小的方法
- 程序员编程艺术第三十~三十一章:字符串转换成整数,通配符字符串匹配
- linux 线程私有数据之一键多值技术TSD池
- 程序员编程艺术第二十八~二十九章:最大连续乘积子串、字符串编辑距离
- 找工作笔试面试那些事儿(16)---linux相关知识点(1)
- HTML5画布kineticjs路径文字教程
- Java学习笔记6 —— 操作符
- android应用程序安全卫士——1、splash界面实现方法
- 【书评】RHCSA/RHCE Red Hat Linux 认证学习指南(第6版)EX200 & EX300