Physical disk block size on POSIX using C/C++

早过忘川 提交于 2019-12-14 03:39:41

问题


I'm working on a high performance I/O program and I'm trying to find the best way to determine the physical (and not the logical) byte size of a device's disk blocks with C++. My research so far has led me to the following code snippet:

#include <iostream>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>

int main(int argc, char ** argv)
{
// file information including block size of the device
struct stat info;
// device to get block size from
char * device = "/mnt/hdb1";

if (stat(device, &info))
{
printf("stat() error");
strerror(errno);
exit(1);
}
printf("Prefered block size for '%s' is %i byte\n", device, info.st_blksize);
return 0;
}

The man pages says the following about st_blksize:

The st_blksize field gives the "preferred" blocksize for efficient file system I/O. (Writing to a file in smaller chunks may cause an inefficient read-modify-rewrite.)

, but it does not mention if st_blksize is the logical or the physical disk block size.

So, is st_blksize the physical disk block size, and if so, then is this the most POSIX OS portable way of detecting the physical disk block size.


回答1:


I wrote an answer, that while hopeful did not work correctly for block devices.

There is no POSIX mechanism for obtaining the fundamental physical block size of a device, you will have to resort to ioctl, which is platform dependent.

For linux there's ioctl(fd, BLKPBSZGET, &block_size)

For Solaris there's the dkio interface, which allows you to get the physical block size.

dk_minfo_ext media_info;
if (-1 != ioctl(fd, DKIOMEDIAINFOEXT, &media_info))
    block_size = media_info.dki_pbsize;

For Mac OS X, it's ioctl(fd, DKIOCGETPHYSICALBLOCKSIZE, &block_size).

For FreeBSD, it should be iotcl(fd, DIOCGSECTORSIZE, &block_size).



来源:https://stackoverflow.com/questions/15944254/physical-disk-block-size-on-posix-using-c-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!