问题描述
我正在开发高性能I/O程序,并且我试图找到确定物理(而不是逻辑)字节的最佳方法使用C ++的设备磁盘块的大小.到目前为止,我的研究使我想到了以下代码片段:
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;
}
手册页中关于st_blksize
的内容如下:
The man pages says the following about st_blksize
:
,但没有提及st_blksize
是逻辑磁盘块大小还是物理磁盘块大小.
, but it does not mention if st_blksize
is the logical or the physical disk block size.
因此,st_blksize
是 physical 磁盘块大小,如果是,则这是检测物理磁盘块大小的最POSIX OS可移植方式.
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.
推荐答案
我写了一个答案,虽然希望不能对块设备正确工作.
I wrote an answer, that while hopeful did not work correctly for block devices.
没有用于获取设备基本物理块大小的POSIX机制,您将不得不求助于ioctl
,这取决于平台.
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.
对于linux,有 ioctl(fd, BLKPBSZGET, &block_size)
For linux there's ioctl(fd, BLKPBSZGET, &block_size)
对于Solaris,有 dkio
界面,该界面可让您获取物理块大小.
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;
对于Mac OS X,它是 ioctl(fd, DKIOCGETPHYSICALBLOCKSIZE, &block_size)
.
For Mac OS X, it's ioctl(fd, DKIOCGETPHYSICALBLOCKSIZE, &block_size)
.
对于FreeBSD,它应该为 iotcl(fd, DIOCGSECTORSIZE, &block_size)
.
For FreeBSD, it should be iotcl(fd, DIOCGSECTORSIZE, &block_size)
.
这篇关于使用C/C ++的POSIX上的物理磁盘块大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!