# Linux内核RAID5写性能分析

## Background

in A ，rmw //请求落在A块中，并采用rmw的方式进行写
read A,D：y$y$//需要从磁盘中读取A,D两块中的数据，耗时y$y$
compute：α$\alpha$//内存计算parity，耗时α$\alpha$
write A,D:x$x$//将A,D两块中的数据从内存中写到磁盘上，耗时x$x$
total latency:x+y+α$x+y+\alpha$//总的耗时情况

## 写延迟计算

1. bio_size < 4KB

in A , rmw, ：A块非overwrite
read A,D : y$y$
compute: α$α$
write A,D : x$x$
total latency:x+y+α$x+y+α$

2. bio_size = 4KB

in A , rcw:A块overwrite
read B,C:y$y$
compute: α$α$
write A,D:x$x$
total:x+y+α$x+y+α$

3. 4KB < bio_size <8KB

In A,E;A块overwrite，E块非overwrite
S0: rcw
Read B,C:y$y$
Compute: α$α$
Write A,D:x$x$
S1: rmw
Read E,H:y$y$
Compute: α$α$
Write E,H:x$x$

这里就不能单纯的累加了，要从CPU的角度考虑流水线的情况，并且考虑磁盘的并行性，具体耗时如下图：

图中y 0,3表示读0号盘和3号盘，由于可以并行，顾只消耗y$y$x 0,3 表示将内存数据写到0号和3号磁盘上，还要考虑相同盘之间的IO顺序，因为一块盘不可能同时处理两个IO，因此有：

total latency:2x+y+α$2x+y+\alpha$

4. bio_size = 8KB

in A,E：A,E块均为overwrite
s0: rcw
read B,C:y$y$
compute: α$α$
write A,D:x$x$
s1: rcw
read F,G:y$y$
compute: α$α$
write F,H:x$x$
流水线情况：

total latency:2x+y+α$2x+y+\alpha$

5. 8KB < bio_size < 12KB

In A,E,I:A,E块overwrite,I块非overwrite
S0: rcw
read B,C:y$y$
compute: α$α$
write A,D:x$x$
s1: rcw
read F,G:y$y$
compute: α$α$
write F,H:x$x$
s2: rmw
read I,L:y$y$
compute: α$α$
write I,L:x$x$
流水线情况：

total latency:3x+2y$3x+2y$

6. bio_size = 12KB

In A,E,I:A,E,I均为overwrite
S0: rcw
read B,C:y$y$
compute: α$α$
write A,D:x$x$
s1: rcw
read F,G:y$y$
compute: α$α$
write F,H:x$x$
s2: rcw
read J,K:y$y$
compute: α$α$
write J,K:x$x$
流水线情况：

total latency:3x+y+α$3x+y+\alpha$

7. 12KB < bio_size <16KB

In A,E,I,M:A,E,I均为overwrite，M块非overwrite
S0: rcw
read B,C:y$y$
compute: α$α$
write A,D:x$x$
s1: rcw
read F,G:y$y$
compute: α$α$
write F,H:x$x$
s2: rcw
read J,K:y$y$
compute:α$α$
write J,K:x$x$
s3: rmw
read M,P:y$y$
compute: α$α$
write M,P:x$x$
流水线情况：

total latency:4x+3y$4x+3y$

8. bio_size = 16KB

In A,E,I,M:均为overwrite
S0: rcw
read B,C:y$y$
compute: α$α$
write A,D:x$x$
s1: rcw
read F,G:y$y$
compute:α$α$
write F,H:x$x$
s2: rcw
read J,K:y$y$
compute: α$α$
write J,K:x$x$
s3: rcw
read N,O:y$y$
compute: α$α$
write M,P:x$x$
流水线情况：

total latency:4x+y+α$4x+y+\alpha$