LoginSignup
0
0

More than 5 years have passed since last update.

dfコマンドのUse%計算方法

Posted at

完全にディスクフルだと後の祭りになるので、少しだけバッファを持たせて表示していると認識しているけど、実際にどうなんだっけ。
計算すると微妙に計算合わない。

$ df  /
Filesystem              1K-blocks     Used Available Use% Mounted on
/dev/mapper/centos-root  30909700 14577164  16332536  48% /

dfの表示は48%だけど、(use*100) / (Used + Available) この計算だと47%になる。

echo "(14577164 * 100) / (14577164 + 16332536)" | bc
47

core-utilsみましょうか。

df.c
        case PCENT_FIELD:
        case IPCENT_FIELD:
          {
            double pct = -1;
            if (! known_value (v->used) || ! known_value (v->available))
              ;
            else if (!v->negate_used
                     && v->used <= TYPE_MAXIMUM (uintmax_t) / 100
                     && v->used + v->available != 0
                     && (v->used + v->available < v->used)
                     == v->negate_available)
              {
                uintmax_t u100 = v->used * 100;
                uintmax_t nonroot_total = v->used + v->available;
                pct = u100 / nonroot_total + (u100 % nonroot_total != 0);
              }

概ね、+1されるという感じでしょうか。
そもそも、この (u100 % nonroot_total != 0) は 0 or 1 を返すんですね。

$ cat x.c
#include <stdio.h>
int main(void)
{
  printf("%d\n", (0 != 0));
  printf("%d\n", (1 != 0));
}
$ gcc x.c
$ ./a.out
0
1
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0