GNU 时代的千字节还是千字节?

mem*_*ere 7 memory time byte units-of-measurement measure

我们正在进行一些性能测量,包括一些内存占用测量。我们一直在用 GNU 时间来做这件事。但是,我无法判断它们是以千字节(1000 字节)还是千字节(1024 字节)为单位进行测量的。

我的系统的手册页提到了%M格式键(我们用它来测量峰值内存使用量):“进程在其生命周期内的最大驻留集大小,以千字节为单位。”

我假设这里的 K 表示 SI“Kilo”前缀,即千字节。但在通过各种工具查看了其他一些对各种事物的记忆测量后,我相信这个假设,就像我相信一只饥饿的狮子在为期一周的假期期间看管我的狗一样。

我需要知道,因为对于我们的测试,1000 KB 与 1024 KB 的差异加起来接近 8 GB,而且我想我可以将测量中的潜在误差减少几十亿。

mem*_*ere 7

使用下面的测试设置,我确定系统上的 GNU 时间以千字节为单位。

下面的程序 (allocator.c) 分配数据并一次访问每个数据 1 KiB,以确保全部分页。注意:只有当您可以分页整个分配的数据时,此测试才有效,否则时间测量只会是内存中最大的常驻集合。

分配器.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define min(a,b) ( ( (a)>(b) )? (b) : (a) )

volatile char access;
volatile char* data;

const int step = 128;

int main(int argc, char** argv ){

  unsigned long k = strtoul( argv[1], NULL, 10 );
  if( k >= 0 ){
    printf( "Allocating %lu (%s) bytes\n", k, argv[1] );
    data = (char*) malloc( k );

    for( int i = 0; i < k; i += step ){
      data[min(i,k-1)] = (char) i;
    }

    free( data );
  } else {
    printf("Bad size: %s => %lu\n", argv[1], k ); 
  }
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译:gcc -O3 allocator.c -o allocator

跑步者 Bash 脚本:

kibibyte=1024
kilobyte=1000
mebibyte=$(expr 1024 \* ${kibibyte})
megabyte=$(expr 1000 \* ${kilobyte})
gibibyte=$(expr 1024 \* ${mebibyte})
gigabyte=$(expr 1000 \* ${megabyte})

for mult in $(seq 1 3);
do
  bytes=$(expr ${gibibyte} \* ${mult} )
  echo ${mult} GiB \(${bytes} bytes\)
  echo "... in kibibytes: $(expr ${bytes} / ${kibibyte})"
  echo "... in kilobytes: $(expr ${bytes} / ${kilobyte})"
  /usr/bin/time -v ./allocator ${bytes}
  echo "===================================================="
done
Run Code Online (Sandbox Code Playgroud)

对我来说,这会产生以下输出:

1 GiB (1073741824 bytes)
... in kibibytes: 1048576
... in kilobytes: 1073741
Allocating 1073741824 (1073741824) bytes
    Command being timed: "./a.out 1073741824"
    User time (seconds): 0.12
    System time (seconds): 0.52
    Percent of CPU this job got: 75%
    Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.86
    Average shared text size (kbytes): 0
    Average unshared data size (kbytes): 0
    Average stack size (kbytes): 0
    Average total size (kbytes): 0
    Maximum resident set size (kbytes): 1049068
    Average resident set size (kbytes): 0
    Major (requiring I/O) page faults: 0
    Minor (reclaiming a frame) page faults: 262309
    Voluntary context switches: 7
    Involuntary context switches: 2
    Swaps: 0
    File system inputs: 16
    File system outputs: 8
    Socket messages sent: 0
    Socket messages received: 0
    Signals delivered: 0
    Page size (bytes): 4096
    Exit status: 0
====================================================
2 GiB (2147483648 bytes)
... in kibibytes: 2097152
... in kilobytes: 2147483
Allocating 2147483648 (2147483648) bytes
    Command being timed: "./a.out 2147483648"
    User time (seconds): 0.21
    System time (seconds): 1.09
    Percent of CPU this job got: 99%
    Elapsed (wall clock) time (h:mm:ss or m:ss): 0:01.31
    Average shared text size (kbytes): 0
    Average unshared data size (kbytes): 0
    Average stack size (kbytes): 0
    Average total size (kbytes): 0
    Maximum resident set size (kbytes): 2097644
    Average resident set size (kbytes): 0
    Major (requiring I/O) page faults: 0
    Minor (reclaiming a frame) page faults: 524453
    Voluntary context switches: 4
    Involuntary context switches: 3
    Swaps: 0
    File system inputs: 0
    File system outputs: 8
    Socket messages sent: 0
    Socket messages received: 0
    Signals delivered: 0
    Page size (bytes): 4096
    Exit status: 0
====================================================
3 GiB (3221225472 bytes)
... in kibibytes: 3145728
... in kilobytes: 3221225
Allocating 3221225472 (3221225472) bytes
    Command being timed: "./a.out 3221225472"
    User time (seconds): 0.38
    System time (seconds): 1.60
    Percent of CPU this job got: 99%
    Elapsed (wall clock) time (h:mm:ss or m:ss): 0:01.98
    Average shared text size (kbytes): 0
    Average unshared data size (kbytes): 0
    Average stack size (kbytes): 0
    Average total size (kbytes): 0
    Maximum resident set size (kbytes): 3146220
    Average resident set size (kbytes): 0
    Major (requiring I/O) page faults: 0
    Minor (reclaiming a frame) page faults: 786597
    Voluntary context switches: 4
    Involuntary context switches: 3
    Swaps: 0
    File system inputs: 0
    File system outputs: 8
    Socket messages sent: 0
    Socket messages received: 0
    Signals delivered: 0
    Page size (bytes): 4096
    Exit status: 0
====================================================
Run Code Online (Sandbox Code Playgroud)

在“最大驻留集大小”条目中,我看到最接近我从原始字节计数中期望的千字节值的值。存在一些差异,因为某些内存可能被调出(在内存较低的情况下,它们都不在这里),并且因为消耗的内存多于程序分配的内存(即堆栈和实际内存)二进制图像本身)。

我的系统上的版本:

> gcc --version
gcc (GCC) 6.1.0
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

> /usr/bin/time --version
GNU time 1.7

> lsb_release -a
LSB Version:    :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch
Distributor ID: CentOS
Description:    CentOS release 6.10 (Final)
Release:    6.10
Codename:   Final
Run Code Online (Sandbox Code Playgroud)

  • GNU 时间源代码似乎也同意:https://git.savannah.gnu.org/cgit/time.git/tree/src/rusage-kb.c#n46 (6认同)