Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does /usr/bin/time measure memory usage?

Tags:

c++

linux

time

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. (I am not 100% clear on what the difference is between these two memory statistics, but that's not the question at hand.)

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.

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. First of all, /usr/bin/time does not report anything on virtual memory usage, it only provides a post on max resident set size. Second, the resident set size reported by /usr/bin/time is about six to eight times larger than my own measure.

So, I've been trying to find out how /usr/bin/time actually performs its measuring, and why the difference is so fundamental. Also, which is the correct value?

Example of output (units are in 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.

EDIT: As a confidence boost, I find that the KDE System Monitor supports the data that my own utility reports. In fact, it gets its information from the same place as I do. So trusting data from /proc/[pid]/stat should be pretty safe. But the question remains...

EDIT2: From the help of the answers below, it's deduced that wait3() is the command used by GNU time. It returns data in the form described in man page getrusage(2). On RSS, it says:

This is the maximum resident set size used (in kilobytes).

The man page also refers to proc/[pid]/stat, which says

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?

like image 204
Mats_SX Avatar asked Oct 21 '13 12:10

Mats_SX


2 Answers

I didn't check source of time. But There are wait3 and wait4 functions for getting rusage of child process:

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.

Man page getrusage(2) describes the struct rusage.

like image 99
SKi Avatar answered Oct 15 '22 20:10

SKi


I looked at the source (it's very short). It uses getrusage().

like image 27
Timmmm Avatar answered Oct 15 '22 19:10

Timmmm