I have a C++ program read_bin
to read hundreds of 200MB binary files on disk. The program to read a file using 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);
I start hundreds of read_bin
processes in the terminal to read individual binary files concurrently.
e.g.
read_bin 001.bin
read_bin 002.bin
read_bin 003.bin
read_bin 004.bin
read_bin 005.bin
...
read_bin 101.bin
It's a SSD disk, but I use iotop
to monitor the read speed is usually at 25MB/s
which is very slow.
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
I can see the IO is full at 99%.
and run 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
I tried to increase buffer bytesToRead
, but the read speed is still very slow at 25MB/s
. Normally, SSD speed can be fast as 500MB/s
.
How to fix my program to maximize disk READ speed?
I tested on google cloud. It's actually their disk performance limits at 25MB/s.