磁盘读写测试

一、目的

        验证针对同一文件的同一位置的写入操作,是否会存在磁盘的同一位置。磁盘同一位置的频繁写入,是否影响磁盘的寿命?固态硬盘的磨损均衡(wear leveling)是否有效?

二、实验方法

        1. 利用fseek(fd, 0 , SEEK_SET)在同一位置写入;

        2. 利用 echo 1 >/proc/sys/vm/block_dump 记录系统I/O;

        3. 利用 dmesg -c –s 50000 >> IO.log 记录运行状态 

三、代码实现

        1. 写文件代码

#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
int main()
{
	FILE* 		fd;
	int 		offset = -1;
	char 		buf[20]; //it is enough size
	int 		size = -1;
	unsigned int 	writeTimes = 1;
	char		ret[20] = {"Success!"};
	clock_t 	start, end;
	FILE* 		out; 

	start = clock();
	
	//write until wrong
	while(1)
	{	
		fd = fopen("test","w+");
		if(fd==NULL)
		{
			sprintf(ret,"%s\n","open filed!");
			goto End;
		}
		//write on the same position
		offset = fseek(fd,0,SEEK_SET);
		if(offset)
		{
			sprintf(ret,"%s\n","offset filed!");
			goto End;
		}
		else
		{
			sprintf(buf,"%d\n",writeTimes);
			size = fwrite(buf,sizeof(buf),1,fd);
			if(size!=1)
			{
				sprintf(ret,"%s\n","write error!");
				goto End;
			}
			
			//sync to hard disk
			if(fflush(fd))
			{
				sprintf(ret,"%s\n","fflush error!");
				goto End;
			}

			fsync(fileno(fd));
			//sync();
			++writeTimes;
		}
	End:
		fclose(fd);
	
		//dmesg after the error happened
		if(strcmp(ret,"Success!"))
		{
			//output message
			end = clock();
			double hours = (double)(end - start)/CLOCKS_PER_SEC/(3600);

			out = fopen("mytest.log","a");
			fprintf(out,"%s\n","------------------------");
			fprintf(out,"%s\n",ret);
			fprintf(out,"%d\n",writeTimes);
			fprintf(out,"%f h \n",hours);
			fclose(out);
		}
	}
	return 0;
}

        2. 记录日志

#!/bin/bash
while (true) 
do
	if [ ! -e mytest.log ]
	then
		dmesg -c -s 50000 |grep write > IO.log	
	else
		dmesg -c -s 50000 |grep write >> IO.log
	fi
	sleep 2
done

        注意:

                echo 1 >/proc/sys/vm/block_dump 开启内核I/O记录功能。

三、实验结果

 

A. 测试无sleep直接写入,测试数据1000条,仅统计写入sda情况。

第一次测试,总写入sda7次数152次。

写入位置

次数

占比

0

21

13.82%

8

32

21.05%

524288

32

21.05%

524704

22

14.47%

524712

30

19.74%

690496

11

7.24%

573496

2

1.32%

524296

2

1.32%

第二次测试,总写入sda7次数727次。

写入位置

写入次数

占比

8

145

19.94%

524712

143

19.67%

524288

141

19.39%

0

133

18.29%

524704

129

17.74%

547848

13

1.79%

524304

4

0.55%

606216

4

0.55%

524296

3

0.41%

524720

3

0.41%

1572872

2

0.28%

1572880

2

0.28%

1572912

2

0.28%

1741480

2

0.28%

573496

1

0.14%

写入次数最频繁的位置:

8

524712

524288

0

524704

B. 每次写入后sleep 10,然后关闭文件。测试数据1000条

第一次测试,总写入sda7次数 120

写入位置

写入次数

占比

0

19

15.83%

8

19

15.83%

524288

19

15.83%

524704

19

15.83%

524712

19

15.83%

748992

13

10.83%

1572912

2

1.67%

1741480

2

1.67%

606216

4

3.33%

524304

2

1.67%

524296

2

1.67%

第二次测试,总写入sda7次数 1587

写入位置

写入次数

占比

524712

288

18.15%

524704

276

17.39%

524288

264

16.64%

8

246

15.50%

0

227

14.30%

573600

102

6.43%

1741480

44

2.77%

1572912

42

2.65%

1572880

33

2.08%

1572872

25

1.58%

606216

22

1.39%

524304

10

0.63%

524296

7

0.44%

573496

1

0.06%

写入最频繁的位置:

524712

524704

524288

8

0

C. 每次写入后关闭文件,然后sleep10 。测试数据1000条。

第一次测试,总写入sda7 次数4909

写入位置

写入次数

占比

524712

936

19.07%

524704

919

18.72%

524288

909

18.52%

8

895

18.23%

0

880

17.93%

748992

116

2.36%

1741480

36

0.73%

1572912

35

0.71%

524296

35

0.71%

524304

35

0.71%

606216

35

0.71%

1572880

32

0.65%

1572872

30

0.61%

590024

14

0.29%

2097168

1

0.02%

2097176

1

0.02%

第二次测试,硬盘总写入次数4175

写入位置

写入次数

占比

0

730

17.49%

524288

730

17.49%

524704

730

17.49%

524712

730

17.49%

8

730

17.49%

690520

149

3.57%

606216

65

1.56%

524296

64

1.53%

524304

64

1.53%

573600

47

1.13%

1572872

33

0.79%

1572880

33

0.79%

1572912

33

0.79%

1741480

33

0.79%

2097168

2

0.05%

2097176

2

0.05%

第三次测试,在同一位置分别写入0-9个字节,各执行100write,硬盘共写入5644次,

写入位置

写入次数

占比

8

979

17.35%

524288

978

17.33%

0

977

17.31%

524704

977

17.31%

524712

880

15.59%

524720

471

8.35%

590632

316

5.60%

1572912

11

0.19%

1572880

10

0.18%

1572872

9

0.16%

1741480

6

0.11%

1572864

5

0.09%

2097168

4

0.07%

2097176

4

0.07%

1741488

3

0.05%

1742264

3

0.05%

573496

3

0.05%

524296

2

0.04%

786448

2

0.04%

1572928

1

0.02%

1572976

1

0.02%

1760336

1

0.02%

1760432

1

0.02%

写入最频繁位置:

0

524288

524704

524712

8


四、初步结论

        在同一文件的同一位置的写入过程中,存在一些频繁写入磁盘的位置。根据记录的无线写入次数,如下图,可知wear leveling 具有一定的作用。


猜你喜欢

转载自blog.csdn.net/u011285208/article/details/79448054
今日推荐