当前位置:网站首页>Test memory read rate
Test memory read rate
2022-06-30 10:22:00 【Yangxiangrui】
1. Ideas
At ordinary times CPU Read data from cache Or read data from memory . If you want to simply read data from memory , So you need to take cache close . Then read the array sequentially from memory . Read rate bandwidth、 Array size arraysize、 Read time time The calculation formula between is as follows :
b a n d w i d t h = a r r a y s i z e t i m e bandwidth = \frac{arraysize}{time} bandwidth=timearraysize
2. close cache
First, in the Linux System shutdown cache, Use the following code to cache close .( Code reference 1, Code reference 2)
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int disableCache_init(void)
{
printk(KERN_ALERT "Disabling L1 and L2 caches.\n");
__asm__(".intel_syntax noprefix\n\t"
"mov rax,cr0\n\t"
"or rax,(1 << 30)\n\t"
"mov cr0,rax\n\t"
"wbinvd\n\t"
".att_syntax noprefix\n\t"
: : : "rax" );
return 0;
}
static void disableCache_exit(void)
{
printk(KERN_ALERT "Enabling L1 and L2 caches.\n");
__asm__(".intel_syntax noprefix\n\t"
"mov rax,cr0\n\t"
"and rax,~(1 << 30)\n\t"
"mov cr0,rax\n\t"
"wbinvd\n\t"
".att_syntax noprefix\n\t"
: : : "rax" );
}
module_init(disableCache_init);
module_exit(disableCache_exit);
makefile as follows
EXTRA_CFLAGS = -m64
obj-m += disableCache.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
Use the following Linux Command to compile source code
$ make
After compilation, use the command insmod Load the program into kernel module in , And use dmesg Check whether the load is successful
$ insmod disableCache.ko
$ dmesg
Post load system log The following output will appear in

3. Start testing bandwidth
Closing cache Use the following code to test bandwidth
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
// Test read BIG_ARR How long does it take , And then calculate bandwidth
#define BIG_ARR 999999
int main()
{
int *block = (int *)calloc(BIG_ARR, sizeof(int));
int i, temp;
clock_t start = clock(), total_time;
for(i = 0; i < BIG_ARR; ++i)
temp += block[i];
total_time = clock() - start;
double sec = (double)total_time / (double) CLOCKS_PER_SEC;
printf("read %d times from main memory need %lf sec\n\ The bandwidth is %lfbyte/sec\n",\
BIG_ARR, sec, BIG_ARR / sec);
return 0;
}
The running results are as follows ( Here the code output forgets to multiply the number of bytes sizeof(int), So the final result needs to be multiplied by 4)

obtain b a n d w i d t h = 359453271 i n t / s e c ≈ 1.44 M / s e c bandwidth = 359453271 \ int/sec \approx 1.44M/sec bandwidth=359453271 int/sec≈1.44M/sec
4. Enable cache
Will actually close cache load kernel module Just uninstall the module , Use the following instructions
$ rmmod disableCache.ko
$ dmesg
Output in the system log You can see the following output in , Explain that you have cache Enable .

notes : This test is in Intel i3 The complete ,AMD Need to be right cr0 Register for other operations , Code reference 2.
边栏推荐
猜你喜欢

著名画家史国良《丰收时节》数字藏品上线长城数艺

2022 Season 6 perfect children's model toxon division finals came to a successful conclusion

Koreano essential creates a professional style

6. Redis new data type

【JVM】CMS简述

GNN动手实践(二):复现图注意力网络GAT

"Kunming City coffee map" activity was launched again

“昆明城市咖啡地图”活动再度开启

陈颢天 荣获第七届少儿模特明星盛典全国总决赛 全国总冠军

逸仙電商發布一季報:堅持研發及品牌投入,實現可持續高質量發展
随机推荐
开源了!文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开
JS get the substring of the specified character position and the specified character position interval of the specified string [simple and detailed]
Questions about cookies and sessions
Use and description of event delegation
Oracle creates a stored procedure successfully, but the compilation fails
‘Failed to fetch current robot state‘ when using the ‘plan_ kinematic_ path‘ service #868
Js獲取指定字符串指定字符比特置&指定字符比特置區間的子串【簡單詳細】
机械臂速成小指南(五):末端执行器
Action bright: take good care of children's eyes together -- a summary of the field investigation on the implementation of action bright in Guangxi
Appium automation test foundation - ADB shell command
Force buckle 428 Serialize and deserialize n-tree DFS
Article content cannot be copied
【JVM】G1垃圾回收器簡述
《锦绣中华》中老年公益文旅游-走进佛山敬老院
【JVM】CMS简述
李沐《动手学习深度学习》d2lbook环境搭建
GD32 RT-Thread PWM驱动函数
L'activité "Kunming City coffee map" a rouvert
AttributeError: ‘Version‘ object has no attribute ‘major‘
train_de.py: error: argument --save_steps: invalid int value: ‘$[$[889580/128/4]*10/2]‘