我有一个 C++ 程序read_bin
来读取磁盘上数百个 200MB 的二进制文件。读取文件的程序使用ifstream
:
std::ifstream is(filename, std::ios::in | std::ios::binary);
is.seekg(startPos, std::ios::beg);
int8_t* buffer = new int8_t [bytesToRead];
is.read(reinterpret_cast<char*>(buffer), bytesToRead);
我在终端中启动数百个read_bin
进程以同时读取单个二进制文件。
例如
read_bin 001.bin
read_bin 002.bin
read_bin 003.bin
read_bin 004.bin
read_bin 005.bin
...
read_bin 101.bin
它是一个SSD磁盘,但我iotop
用来监控读取速度通常25MB/s
很慢。
Total DISK READ : 25.35 M/s | Total DISK WRITE : 0.00 B/s
Actual DISK READ: 25.35 M/s | Actual DISK WRITE: 7.84 K/s
TID PRIO USER DISK READ DISK WRITE SWAPIN IO> COMMAND
2636 be/4 ubuntu 501.71 K/s 0.00 B/s 0.00 % 99.99 % read_bin 001.bin
2631 be/4 ubuntu 501.71 K/s 0.00 B/s 0.00 % 99.99 % read_bin 002.bin
2660 be/4 ubuntu 501.71 K/s 0.00 B/s 0.00 % 99.99 % read_bin 003.bin
2662 be/4 ubuntu 501.71 K/s 0.00 B/s 0.00 % 99.99 % read_bin 004.bin
2665 be/4 ubuntu 501.71 K/s 0.00 B/s 0.00 % 99.99 % read_bin 005.bin
2668 be/4 ubuntu 501.71 K/s 0.00 B/s 0.00 % 99.99 % read_bin 006.bin
我可以看到 IO 已满 99%。
并运行top
:
top - 21:36:58 up 10 min, 3 users, load average: 95.28, 74.89, 37.27
Tasks: 250 total, 1 running, 188 sleeping, 0 stopped, 0 zombie
%Cpu(s): 1.2 us, 0.9 sy, 0.0 ni, 74.9 id, 23.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 30875564 total, 5388416 free, 12671052 used, 12816096 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 17743200 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
2633 ubuntu 20 0 147928 133532 3080 D 0.7 0.4 0:00.73 read_bin
2652 ubuntu 20 0 147928 133368 2916 D 0.7 0.4 0:00.72 read_bin
2697 ubuntu 20 0 145604 131764 2884 D 0.7 0.4 0:00.69 read_bin
2701 ubuntu 20 0 145604 131000 2928 D 0.7 0.4 0:00.70 read_bin
2630 ubuntu 20 0 147928 134680 2908 D 0.3 0.4 0:00.72 read_bin
2632 ubuntu 20 0 147928 134292 2772 D 0.3 0.4 0:00.72 read_bin
2634 ubuntu 20 0 147928 134120 2868 D 0.3 0.4 0:00.70 read_bin
我试图增加缓冲区bytesToRead
,但读取速度仍然很慢25MB/s
。通常,SSD 速度可以快到500MB/s
.
如何修复我的程序以最大化磁盘读取速度?