简体   繁体   English

/ usr / bin / time如何衡量内存使用情况?

[英]How does /usr/bin/time measure memory usage?

I am implementing an algorithm and want to measure the time and memory consumption of it. 我正在实现一个算法,并希望测量它的时间和内存消耗。 To my aid I've written my own measuring utility, which reads from /proc/[pid]/stat the amount of user and system time consumed, and also the virtual memory and resident set peak sizes. 在我的帮助下,我编写了自己的测量实用程序,它从/proc/[pid]/stat读取消耗的用户和系统时间量,以及虚拟内存和驻留设置峰值大小。 (I am not 100% clear on what the difference is between these two memory statistics, but that's not the question at hand.) (我不是100%清楚这两个内存统计数据之间的差异,但这不是手头的问题。)

So far so good, but along comes a third-party implementation against which I wish to compare my own programs. 到目前为止一直很好,但随之而来的是第三方实施,我希望比较我自己的程序。 As I do not want to fiddle with its source, I am unable to use my own measuring program to collect data on its efficiency (I might be able to, but it would require me to rethink my measuring scheme). 由于我不想摆弄它的来源,我无法使用我自己的测量程序收集有关其效率的数据(我可能会,但它需要我重新考虑我的测量方案)。 So I find /usr/bin/time also takes these measures. 所以我发现/usr/bin/time也采取了这些措施。

As I compare the output, I find /usr/bin/time does report the same time usage as my own tool, but memory data is much different. 当我比较输出时,我发现/usr/bin/time确实报告了与我自己的工具相同的时间用法,但内存数据却大不相同。 First of all, /usr/bin/time does not report anything on virtual memory usage, it only provides a post on max resident set size. 首先, /usr/bin/time不报告有关虚拟内存使用情况的任何内容,它仅提供最大驻留集大小的帖子。 Second, the resident set size reported by /usr/bin/time is about six to eight times larger than my own measure. 其次, /usr/bin/time报告的驻留集大小比我自己的度量大6到8倍。

So, I've been trying to find out how /usr/bin/time actually performs its measuring, and why the difference is so fundamental. 所以,我一直试图找出/usr/bin/time如何实际执行其测量,以及为什么差异是如此根本。 Also, which is the correct value? 另外,哪个是正确的值?

Example of output (units are in MB): 输出示例(单位为MB):

<program>: <virtual mem> <resident set size>
autotest (my own utility): 23.2266 2.19531
/usr/bin/time: N/A 11.23437

I am using GNU/Linux 3.8.13 Mageia 3 x86_64. 我使用的是GNU / Linux 3.8.13 Mageia 3 x86_64。

EDIT: As a confidence boost, I find that the KDE System Monitor supports the data that my own utility reports. 编辑:作为一个信心提升,我发现KDE系统监视器支持我自己的实用程序报告的数据。 In fact, it gets its information from the same place as I do. 事实上,它从我所在的地方获取信息。 So trusting data from /proc/[pid]/stat should be pretty safe. 所以来自/proc/[pid]/stat信任数据应该是非常安全的。 But the question remains... 但问题仍然存在......

EDIT2: From the help of the answers below, it's deduced that wait3() is the command used by GNU time . 编辑2:在下面的答案的帮助下,推断出wait3()是GNU time使用的命令。 It returns data in the form described in man page getrusage(2). 它以手册页getrusage(2)中描述的形式返回数据。 On RSS, it says: 在RSS上,它说:

This is the maximum resident set size used (in kilobytes). 这是使用的最大驻留集大小(以千字节为单位)。

The man page also refers to proc/[pid]/stat , which says 手册页也指proc/[pid]/stat ,它说

Resident Set Size: number of pages the process has in real memory. 驻留集大小:进程在实际内存中的页数。 This is just the pages which count toward text, data, or stack space. 这只是计算文本,数据或堆栈空间的页面。 This does not include pages which have not been demand-loaded in, or which are swapped out. 这不包括未加载需求或换出的页面。

So, is the second version more accurate in just measuring my program's memory usage, and the first version also measures some kind of external-library-usage? 那么,第二个版本中只是衡量我的程序的内存使用情况更精确,和第一个版本还可以测量某种外部库使用的?

I didn't check source of time . 我没有查看time来源。 But There are wait3 and wait4 functions for getting rusage of child process: 但是有wait3wait4函数来获取子进程的rusage

pid_t wait3(int *status, int options, struct rusage *rusage);
pid_t wait4(pid_t pid, int *status, int options, struct rusage *rusage);

struct rusage contains the maximum resident set size value. struct rusage包含最大驻留集大小值。

Man page getrusage(2) describes the struct rusage . 手册页getrusage(2)描述了struct rusage

I looked at the source (it's very short). 我查看了源代码(很短)。 It uses getrusage() . 它使用getrusage()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM