获取挂载点的已使用空间百分比和已使用inode百分比。

huangapple go评论92阅读模式
英文:

Getting the percentage of used space and used inodes in a mount

问题

我需要计算Go语言中挂载路径(例如/mnt/mycustommount)的已用空间和已用inode的百分比。

这是我的尝试:

var statFsOutput unix.Statfs_t

err := unix.Statfs(mount_path, &statFsOutput)
if err != nil {
    return err
}

totalBlockCount := statFsOutput.Blocks // 文件系统中的总数据块数
freeSystemBlockCount := statFsOutput.Bfree // 文件系统中的空闲块数
freeUserBlockCount := statFsOutput.Bavail // 对非特权用户可用的空闲块数

现在我需要的比例应该是这样的:

x : 100 = (totalBlockCount - free[which?]BlockCount) : totalBlockCount

x : 100 = usedBlockCount : totalBlockCount。但我不明白BfreeBavail之间的区别(非特权用户与文件系统块有什么关系?)。

对于inode的计算,我的尝试如下:

totalInodeCount := statFsOutput.Files
freeInodeCount := statFsOutput.Ffree
// 现在比例是
// x : 100 = (totalInodeCount - freeInodeCount) : totalInodeCount

如何计算已用存储的百分比?
我的inode计算是否正确?

英文:

I need to calculate the percentage of used space and used inodes for a mount path (e.g. /mnt/mycustommount) in Go.

This is my attempt:

var statFsOutput unix.Statfs_t

err := unix.Statfs(mount_path, &statFsOutput)
if err != nil {
	return err
}

totalBlockCount := statFsOutput.Blocks // Total data blocks in filesystem
freeSystemBlockCount = statFsOutput.Bfree // Free blocks in filesystem
freeUserBlockCount = statFsOutput.Bavail // Free blocks available to unprivileged user

Now the proportion I need would be something like this:

x : 100 = (totalBlockCount - free[which?]BlockCount) : totalBlockCount

i.e. x : 100 = usedBlockCount : totalBlockCount . But I don't understand the difference between Bfree and Bavail (what's 'unprivileged' user go to do with filesystem blocks?).

For inodes my attempt:

totalInodeCount = statFsOutput.Files
freeInodeCount = statFsOutput.Ffree
// so now the proportion is
// x : 100 = (totalInodeCount - freeInodeCount) : totalInodeCount

How to get the percentage for used storage?
And is the inodes calculation I did correct?

答案1

得分: 0

你的评论表达不是有效的Go代码,所以我无法在没有猜测的情况下真正解释它。通过猜测,我将其解释为正确的,但我是否猜对了你实际的意思,还是仅仅猜测了我认为你的意思是什么?换句话说,在没有显示实际代码的情况下,我只能想象你最终的代码将是什么样子。如果我想象的代码不是实际的代码,那么我想象中你将编写的代码的正确性就不重要了。

除此之外,我可以在这里回答你的问题:

('非特权'用户与文件系统块有什么关系?)

Linux的statfs调用使用与4.4BSD相同的字段。默认的4.4BSD文件系统(称为“快速文件系统”)使用一种带有碎片化的块分配方法,以一种类似于随机的方式。这种分配过程在文件系统上运行得非常好,并且在部分填满的文件系统上继续工作而不会出现极端的减速。然而,对其行为进行计算建模后发现,如果块使用率超过大约90%,可能会出现病态减速(或多或少相当于线性搜索)。

(后来,对真实文件系统的分析发现,减速通常不会发生,直到块使用率超过95%。但是,那时已经基本确立了10%的“保留”概念。)

因此,如果当时流行的400 MB<sup>1</sup>大容量磁盘驱动器为inode分配了10%,并为保留块分配了另外10%,那么普通用户可以分配约320 MB的文件数据。在那一点上,驱动器被认为是“100%满”,但通过使用剩余的块,它可以达到111%。然而,这些块是保留给超级用户的。

现在,与其拥有一个“超级用户”,我们可以拥有一个可以授予或撤销的能力。然而,现在我们也不再使用相同的文件系统。因此,在您的系统上,bfreebavail可能没有区别。


<sup>1</sup>是的,400 MB的Fujitsu Eagle当时是一种大型(在多个方面都是如此:它使用了19英寸机架安装设置)的硬盘驱动器。如今,人们对于他们的多TB固态硬盘已经变得宠坏了。 😀

英文:

Your comment expression isn't valid Go, so I can't really interpret it without guessing. With guessing, I interpret it as correct, but have I guessed what you actually mean, or merely what I think you mean? In other words, without showing actual code, I can only imagine what your final code will be. If the code I imagine isn't the actual code, the correctness of the code I imagine you will write is irrelevant.

That aside, I can answer your question here:

> (what's 'unprivileged' user go to do with filesystem blocks?)

The Linux statfs call uses the same fields as 4.4BSD. The default 4.4BSD file system (the one called the "fast file system") uses a blocks-with-fragmentation approach to allocate blocks in a sort of stochastic manner. This allocation process works very well on an empty file system, and continues to work well, without extreme slowdown, on somewhat-full file systems. Computerized modeling of its behavior, however, showed pathological slowdowns (amounting to linear search, more or less) were possible if the block usage exceeded somewhere around 90%.

(Later, analysis of real file systems found that the slowdowns generally did not hit until the block usage exceeded 95%. But the idea of a 10% "reserve" was pretty well established by then.)

Hence, if a then-popular large-size disk drive of 400 MB<sup>1</sup> gave 10% for inodes and another 10% for reserved blocks, that meant that ordinary users could allocate about 320 MB of file data. At that point the drive was "100% full", but it could go to 111% by using up the remaining blocks. Those blocks were reserved to the super-user though.

These days, instead of a "super user", one can have a capability that can be granted or revoked. However, these days we don't use the same file systems either. So there may be no difference between bfree and bavail on your system.


<sup>1</sup>Yes, the 400 MB Fujitsu Eagle was a large (in multiple senses: it used a 19 inch rack mount setup) drive back then. People are spoiled today with their multi-terabyte SSDs. 😀

huangapple
  • 本文由 发表于 2021年12月29日 23:57:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/70521846.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定