关于 “BlockSize”的详解

来源:互联网 发布:linux开启tftp服务器 编辑:程序博客网 时间:2024/04/27 20:27
最近在使用vmstat命令时,其中有一列是bi,意思是每秒从块设备中读入的块的数量。那每个块的大小是多少呢?
在vmstat的man手册中没有提及,这可能是因为vmstat这个程序所属的procps-2.0.13-9.2E程序包的版本太低。如果是procps 3.1.9版本之后的,block size就是1024bytes。在这之前的可能是512字节,2048或4096字节。

文件系统block size的确定可以用如下命令:
dumpe2fs /dev/sda1|grep -i "Block size"
tune2fs -l /dev/sda1
stat -f /dev/sda1

可以参考如下文字:
The problem with this is that there are four distinct units that youmust be keeping in mind. To make things even worse, two of these unitsbear the same name. These are the different units:

1. Hardware block size, "sector size"
2. Filesystem block size, "block size"
3. Kernel buffer cache block size, "block size"
4. Partition table block size, "cylinder size"


To differentiate between the filesystem block size and the buffer cacheblock size, I will follow FAT terminology and use "cluster size" forthe filesystem block size.

The sector size is the units that the hardware deals with. This rangesbetween different hardware types, but most PC-style hardware (floppies,IDE disks, etc.) use 512 byte sectors.

The cluster size is the allocation unit that the filesystem uses, andis what causes fragmentation - I'm sure you know about that. On amoderately sized ext3 filesystem, this is usually 4096 bytes, but youcan check that with dumpe2fs. Remember that these are also usuallycalled "blocks", only that I refer to them as clusters here.
The cluster size is what gets returned in st_blksize in the statbuffer, in order for programs to be able to calculate the actual diskusage of a file.

The block size is the size of the buffers that the kernel usesinternally when it caches sectors that have been read from storagedevices (hence the name "block device"). Since this is the mostprimitive form of storage in the kernel, all filesystem cluster sizesmust be multiples of this. This block size is also what is almostalways referred to by userspace programs. For example, when you run"du" without the -h or -H options, it will return how many of theseblocks a file takes up. df will also report sizes in these blocks, the"Blocks" column in the fdisk -l output is of this type, and so on. Itis what is most commonly referred to as a "block". Two disk sectors fitinto each block.

The cylinder size is only used in the partition table and by the BIOS (and the BIOS isn't used by Linux).

"df" only operates on filesystems, so, no, it can't be used without afilesystem - without a filesystem, the data that it would returndoesn't exist. "du" operates on individual files.





自从开始Linux 软件平台开发,经常遇到“Block Size”。但经常发现此block size非彼block size。意义不一样,大小值也不一样。Open Source的东东有时候也挺烦的。下面是自己的总结。通常Linux的“block size”指的是1024 bytes,Linux用1024-byte blocks 作为buffer cache的基本单位。但linux的文件系统的block确不一样。例如ext3系统,block size是4096。使用tune2fs可以查看带文件系统的磁盘分区的相关信息,包括block size。
例如:
tune2fs -l /dev/hda1 |grep "Block size"
Block size:               4096
另一个工具dumpe2fs也可以。

其实本来这几个概念不是很难,主要是NND他们的名字都一样,都叫“Block Size”。
1.     硬件上的 block size, 应该是"sector size",linux的扇区大小是512byte
2.       有文件系统的分区的block size, 是"block size",大小不一,可以用工具查看
3.       没有文件系统的分区的block size,也叫“block size”,大小指的是1024 byte
4.       Kernel buffer cache 的block size, 就是"block size",大部分PC是1024
5.       磁盘分区的"cylinder size",用fdisk 可以查看。

我们来看看fdisk显示的不同的信息,理解一下这几个概念:

Disk /dev/hda: 250.0 GB, 250059350016 bytes
255 heads, 63 sectors/track, 30401 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes  (heads*sectors*512)

   Device Boot    Start       End    Blocks   Id  System
/dev/hda1   *         1      1305  10482381   83  Linux
/dev/hda2          1306      1566   2096482+  82  Linux swap
/dev/hda3          1567     30401 231617137+  83  Linux

8225280就是cylinder size。一共有30401个cylinder。Start和End分别标记的是各个分区的起始cylinder。 第4列显示的就是以1024为单位的block(这一列最容易把人搞晕)。为什么“2096482+”有个“+”号呢?因为啊,总size除1024除不尽,是个约数,表示2096482强!哈哈。搞笑吧,只有opensource的人想得出来这么表示。


原创粉丝点击