linux磁盘IO测试
man 2 fsync知fsync会真正把文件的数据写回磁盘,
但是在3.2的kernal上测试发现fsync最好集中一次做掉,
可能fsync执行过程不会判断每个块是否正在修改,都去刷盘。(有待确认)
const int SIZE = 1 << 24; char buf[SIZE]; void test(int size) { int fd = open("txt", O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU); int count = SIZE / size; for (int i = 0; i < count; ++i) { write(fd, buf, size); fsync(fd); } //fsync(fd); close(fd); }
[size] time consume:xxxms
此时测得
[131072] time consume: 5531ms
[262144] time consume: 2718ms
[524288] time consume: 1464ms
[1048576] time consume: 926ms
[2097152] time consume: 628ms
[4194304] time consume: 404ms
[8388608] time consume: 305ms
[16777216] time consume: 256ms
16777216 = 2^24
但是如果用下面的fsync发现
[131072] time consume: 331ms
[262144] time consume: 264ms
[524288] time consume: 239ms
[1048576] time consume: 264ms
[2097152] time consume: 239ms
[4194304] time consume: 264ms
[8388608] time consume: 239ms
[16777216] time consume: 263ms
还是mmap给力
....
[64] time consume: 41ms
[128] time consume: 42ms
[256] time consume: 41ms
[512] time consume: 41ms
[1024] time consume: 41ms
[2048] time consume: 41ms
[4096] time consume: 33ms
[8192] time consume: 33ms
[16384] time consume: 33ms
[32768] time consume: 33ms
[65536] time consume: 41ms
[131072] time consume: 42ms
[262144] time consume: 41ms
[524288] time consume: 41ms
[1048576] time consume: 50ms
[2097152] time consume: 74ms
[4194304] time consume: 107ms
[8388608] time consume: 141ms
struct node { int fd_, size_; node () { } node (int fd, int size) { fd_ = fd; size_ = size; } }; void test_prefetch(node t) { char *p = (char*)mmap(NULL, t.size_, PROT_WRITE, MAP_SHARED, t.fd_, 0); if (p == MAP_FAILED) { perror("mmap"); exit(1); } memcpy(p, buf, t.size_); msync(p, t.size_, MS_SYNC); munmap(p, t.size_); }
前面很多size虽然不一样,但是写入磁盘的时间却是一样的,说明磁盘的块特性,
fdisk -l
Disk /dev/sda: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders, total 976773168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0xe0940810