如何才能找出文件在磁盘上的物理位置(块号)?

如何才能找出文件在磁盘上的物理位置(块号)?

This is an obscure question, I know. I'm trying to do some performance testing of some disks on a Linux box. I'm getting some inconsistent results, running the same test on the same disk. I know that disks have different performance depending on which part of the disk is being accessed. In particular, reads and writes to the outside of the disk have much higher throughput than reads and writes to the inside part of the disk, due to near-constant data density and constant rotational speed.

I'd like to see if my inconsistencies can be attributed to this geometry-induced variance in throughput. Is it possible, using existing tools, to find out where on the disk a file has been placed?

If not, I suppose I can write something to directly seek, read, and write to the device file itself, bypassing (and destroying) the filesystem, but I'm hoping to avoid that. I'm currently using ext4 on a 3.0 kernel (Arch Linux, if it matters), but I'm interested in techniques for other filesystems as well.

答案1

You could use debugfs for this:

debugfs -R "stat ~/myfile" /dev/hda1

Change the hard/partition drive accordingly and make sure the drive is unmounted. You will get a list with all the blocks used:

BLOCKS:
(0):1643532
TOTAL: 1

答案2

You can use the FIBMAP ioctl, as exemplified here, or using hdparm:

/ $ sudo /sbin/hdparm --fibmap /etc/X11/xorg.conf

/etc/X11/xorg.conf:
 filesystem blocksize 4096, begins at LBA 0; assuming 512 byte sectors.
 byte_offset  begin_LBA    end_LBA    sectors
           0    1579088    1579095          8

答案3

This thread may give you some insight into ext4 file placement algorithm.

debugfs有一个bmap函数,它似乎可以提供您想要的数据。您应该能够为其提供文件的连续块并获取物理块编号。

答案4

这个问题相当老了,但对于那些在 Google 上找到这个问题的人来说,还有另一个答案可能有用:(filefrag在 Debian 中它位于包内e2fsprogs)。

# filefrag -eX /usr/bin/aptitude
Filesystem type is: ef53
File size of /usr/bin/aptitude is 4261400 (1041 blocks of 4096 bytes)
 ext:     logical_offset:        physical_offset: length:   expected: flags:
   0:        0..     1fa:    15bd805..   15bd9ff:    1fb:            
   1:      1fb..     3f2:    15c6608..   15c67ff:    1f8:    15bda00:
   2:      3f3..     410:    15c8680..   15c869d:     1e:    15c6800: last,eof
/usr/bin/aptitude: 3 extents found

它的优点是它也适用于其他文件系统(我将它用于 UDF),而这里描述的其他工具似乎不支持这些文件系统。

输出中显示的偏移量应为第二行中写入的块大小的倍数(此处为 4096)。请注意,逻辑偏移量可能不连续,因为文件中可能有空洞(当文件系统支持时)。

相关内容