gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 23:07:35 24 4
gpt4 key购买 nike

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

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

我假设这里的 K 表示 SI“Kilo”前缀,也就是千字节。
但是,通过各种工具查看了对各种事物的其他一些内存测量结果后,我相信这个假设,就像我相信饥饿的狮子会在一周的假期中照看我的狗一样。

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

最佳答案

使用以下测试设置,我确定系统上的 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;
}

编译: 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
====================================================

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

我系统上的版本:
> 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

关于memory - GNU 时间是千字节还是千字节?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61392725/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com