Ubuntu下内存写入速度仅为标称值1/6的问题排查求助
Ubuntu下内存写入速度仅为标称值1/6的问题排查求助
大家好,我最近碰到个糟心的问题:自己写了个简单的内存写入速度测试程序(代码附在后面),结果测出来我的内存实际速度居然只有标称值的1/6左右,想请各位大佬帮忙分析下原因。
先说说我的硬件情况:
- 笔记本:联想Legion 5 Pro
- 处理器:AMD® Ryzen 7 5800H with Radeon Graphics
- 内存参数:DDR4-3200(PC4-25600),峰值传输速率25600 MB/s,每秒数据传输次数3200M次(按公式3200*8=25600算,理论写入速度应该是25.6字节/ns)
但实际跑测试的结果却是这样:
$ ./writetest64 testing RAM write speed writing 8 bytes at a time total 8192000000 bytes 1842506647 ns speed: 4.44612 bytes/ns speed: 4446116931 bytes/s speed: 4446.12 Mbytes/s speed: 555.765 M writes/s done!
算下来,实际每秒只有556M次数据传输,和标称的3200M次差了一大截,相当于慢了5.75倍。我实在搞不懂这是咋回事,难道是Ubuntu下需要装什么特殊驱动吗?
下面是我用的测试程序代码(完整核心逻辑):
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { const size_t total_bytes = 8LL * 1024 * 1024 * 100; // 总共写8GB数据 const size_t step = 8; // 每次写8字节 char *buf = malloc(total_bytes); if (!buf) { perror("malloc申请内存失败"); return 1; } printf("testing RAM write speed\n"); printf("writing %zu bytes at a time\n", step); printf("total %zu bytes\n", total_bytes); clock_t start = clock(); // 循环写入数据 for (size_t i = 0; i < total_bytes; i += step) { *((unsigned long long *)(buf + i)) = 0xdeadbeef; } clock_t end = clock(); // 计算各种速度指标 double ns = (double)(end - start) * 1e9 / CLOCKS_PER_SEC; double bytes_per_ns = total_bytes / ns; double bytes_per_sec = total_bytes / (ns / 1e9); double mbytes_per_sec = bytes_per_sec / 1e6; double mwrites_per_sec = (total_bytes / step) / (ns / 1e9); printf("%f ns\n", ns); printf("speed: %f bytes/ns\n", bytes_per_ns); printf("speed: %f bytes/s\n", bytes_per_sec); printf("speed: %f Mbytes/s\n", mbytes_per_sec); printf("speed: %f M writes/s\n", mwrites_per_sec); printf("done!\n"); free(buf); return 0; }
我自己瞎琢磨了几个可能的方向,但拿不准:
- 会不会是我的测试程序写得有问题?比如每次只写8字节,没利用到内存的带宽优势?
- BIOS里有没有没开启的内存优化选项?比如XMP或者AMD的DOCP?我之前没怎么动过BIOS设置
- Ubuntu对AMD Ryzen平台的内存支持有没有什么特殊配置要弄?
- 会不会是核显占用了部分内存带宽,拖慢了速度?
有没有大佬能帮忙指点下问题出在哪,或者给我一些排查的步骤?万分感谢!
备注:内容来源于stack exchange,提问作者18446744073709551615
相关产品推荐
相关产品推荐

