GNU时间是千字节还是千字节?

问题描述 投票:0回答:1

我们正在做一些性能测量,包括一些内存占用量测量。我们一直在用GNU时间来做到这一点。但是,我无法确定它们的度量单位是千字节(1000字节)还是千字节(1024字节)。

我的系统的手册页说到%M格式键(我们正在使用它来测量峰值内存使用量:“该进程在其生存期内的最大驻留集大小,以KB为单位。”

我假设K在这里是SI的“ Kilo”前缀,因此是千字节。但是通过各种工具查看了其他事物对其他事物的记忆测量结果后,我相信这种假设,就像我相信一只饥饿的狮子在一个整整的假期里看我的狗一样。

我需要知道,因为对于我们的测试,1000 vs 1024 KB的总和相差近8 GB,我想我可以将测量中的潜在误差减少数十亿。

memory time byte measure units-of-measurement
1个回答
0
投票

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

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

allocator.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;
}

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

Runner 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

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

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
====================================================

在“最大居民集大小”条目中,我看到的值与该原始字节数所期望的千字节值最接近。之所以存在某些差异,是因为可能会调出某些内存(在内存较低的情况下,这里都没有内存),并且因为消耗的内存比程序分配的内存(即堆栈和实际内存)多。二进制映像本身)。

我系统上的版本:

> 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
© www.soinside.com 2019 - 2024. All rights reserved.