2011-09-30 22 views
0

我有一個處理大量文件(〜96,000個文件,〜12 TB數據)的進程。這個過程的幾次運行都讓文件分散在驅動器中。該過程中的每個迭代使用多個文件。這導致在收集文件的磁盤周圍出現大量的垃圾郵件。Linux分區上的訂購文件位置

理想情況下,我希望該過程按順序編寫它使用的文件,以便下一次運行將按順序(文件大小更改)讀取它們。有沒有辦法暗示物理排序/分組,而不是寫入原始分區?

任何其他建議將有所幫助。

感謝

+0

在堆棧交換下看起來像[超級用戶](http://superuser.com/)或[Linux/Unix](http://unix.stackexchange.com/)的問題。 –

回答

0

有兩個系統調用你可能查找:fadvise64fallocate告訴內核你打算如何讀取或寫入指定的文件。

另一個提示是「Orlov塊分配器」(Wikipedia,LWN)影響內核分配新目錄和文件條目的方式。

+0

謝謝,我會檢查出來 – Zach

0

最後,我決定不用擔心以任何特定順序編寫文件。相反,在開始運行之前,我會找出每個文件的第一個塊位於何處,然後按照第一個塊位置對文件處理順序進行排序。不完美,但它確實在處理時間方面有很大的不同。

下面是我用來獲取第一個提供的文件列表塊的C代碼我從我在網上找到的示例代碼中調整它(似乎無法找到原始來源)。

#include <stdio.h> 
#include <sys/ioctl.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <fcntl.h> 
#include <assert.h> 
#include <unistd.h> 
#include <string.h> 
#include <errno.h> 

#include <linux/fs.h> 

// 
// Get the first block for each file passed to stdin, 
// write filename & first block for each file to stdout 
// 


int main(int argc, char **argv) { 
    int  fd; 
    int  block; 
    char fname[512]; 

    while(fgets(fname, 511, stdin) != NULL) { 

     fname[strlen(fname) - 1] = '\0'; 
     assert(fd=open(fname, O_RDONLY)); 

     block = 0; 
     if (ioctl(fd, FIBMAP, &block)) { 
      printf("FIBMAP ioctl failed - errno: %s\n", strerror(errno)); 
     } 
     printf("%010d, %s\n", block, fname); 
     close(fd); 
    } 
    return 0; 
}