2012-10-07 40 views
3

我運行一個java代碼,該代碼產生大量的讀寫文本文件。程序源代碼非常簡單,在一個循環中,我在一個測試文件中寫入2000行,然後再次讀取它們,以便生成大量的讀取和寫入磁盤。但是當程序運行時我通過「iostat -d -x 1」監控磁盤我發現第二個「r/s」的讀取沒有變化,但是「w/s」增加了,正如我預期的那樣! 這iostat命令的示例輸出:爲什麼在linux命令(iostat)中每秒讀取(r/s)一直爲零?

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(); 
     } 
    } 
} 
} 

回答

4

最有可能的,操作系統是緩存在內存中的文件。由於它已經被緩存在內存中,所以當你用Java讀取時,操作系統不需要從磁盤上實際讀取 - 它只是給你它已經存在的緩存副本。這比從磁盤實際讀取要快得多。但是,請注意,由於iostat只報告實際的磁盤讀取和寫入,您的緩存讀取將不會顯示在那裏。

+0

我不這麼認爲,因爲首先我通過Riak DB測試一小時寫入數據並從DB讀取數據,但此參數爲零!我想很長一段時間,它不能緩存所有的交易。 –

+0

@SepehrSamini,時間不重要,大小確實。如果你的數據庫適合內存,你的讀取可以被緩存任意長的時間。 – bdonlan

+0

非常棒@bdonlan,但是如果緩存和使用密切相關,以至於沒有任何進程從磁盤讀取,我會感到驚訝。也許如果iostat運行60秒間隔仍然說r/s =零,那麼讀取磁盤元數據時會出現問題? –

相關問題