gpt4 book ai didi

linux - 为什么 linux 命令 (iostat) 中的每秒读取 (r/s) 始终为零?

转载 作者:IT王子 更新时间:2023-10-29 01:23:58 30 4
gpt4 key购买 nike

我运行了一个 java 代码,它产生大量的文本文件读写操作。程序源非常简单,在一个循环中我在一个测试文件中写入了 2000 行,然后我再次读取它们只是为了产生大量的磁盘读写操作。但是当程序运行时,我通过“iostat -d -x 1”监控磁盘,我发现第二个“r/s”但“w/s”中的读取没有变化如我所料增加!!!这是 iostat 命令的示例输出:

Device: rrqm/s wrqm/s  r/s   w/s    rsec/s wsec/s   avgrq-sz avgqu-sz await svctm  %util
sda 0.00 913.00 0.00 82.00 0.00 7872.00 96.00 0.58 7.09 7.11 58.30

Device: rrqm/s wrqm/s r/s w/s rsec/s wsec/s avgrq-sz avgqu-sz await svctm %util
sda 0.00 869.00 0.00 79.00 0.00 7584.00 96.00 0.57 7.11 7.18 56.70

Device: rrqm/s wrqm/s r/s w/s rsec/s wsec/s avgrq-sz avgqu-sz await svctm %util
sda 0.00 847.00 0.00 77.00 0.00 7392.00 96.00 0.57 7.42 7.43 57.20

Device: rrqm/s wrqm/s r/s w/s rsec/s wsec/s avgrq-sz avgqu-sz await svctm %util
sda 0.00 1221.00 0.00 113.00 0.00 10760.00 95.22 0.84 7.47 7.32 82.70

如图所示,所有“r/s”均为零!但是在 Java 程序中,我在文件中读的和写的一样多?!当我运行 Java 代码时,每秒写入次数增加,但“r/s”没有变化!!这是我在监视磁盘时运行的 java 代码:

import java.io.*;

public class Test {

public static void main(String [] args) throws IOException{

String fileName = "/scratch/dump_file.txt";
File f = new File(fileName);
// Attempt to delete it
boolean success = f.delete();
int j=0;
while(j<20000)
{
++j;
Writer output = null;
String text = "A test content";
File file = new File(fileName);
output = new BufferedWriter(new FileWriter(file));

int i=1;
while(i<2000)
{
//Here we start writing 2000 lines into file line by line
output.write("j==="+Integer.toString(j)+text+Integer.toString(i)+"\n");
++i;
}
output.close();
System.out.println("Your file has been written");

String line = null;
try {
// FileReader reads text files in the default encoding.
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader =
new BufferedReader(fileReader);
i=1;
while((line = bufferedReader.readLine()) != null) {
//Here we start reading from file line by line and show the result on screen
++i;
System.out.println(line);
}
// Always close file
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
}
}
}

最佳答案

很可能,操作系统正在 RAM 中缓存文件。因为它已经缓存在内存中,所以当你在 Java 中读取时,操作系统不需要实际从磁盘读取——它只是给你它已经缓存的副本。这比实际从磁盘读取要快得多;但是请注意,由于 iostat 仅报告实际 磁盘读取和写入,您缓存的读取不会显示在那里。

关于linux - 为什么 linux 命令 (iostat) 中的每秒读取 (r/s) 始终为零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12772088/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com