Bug 15751 - `time` function reports more `maxrss` usage than GNU time
Summary: `time` function reports more `maxrss` usage than GNU time
Status: NEW
Alias: None
Product: Busybox
Classification: Unclassified
Component: Standard Compliance (show other bugs)
Version: 1.34.x
Hardware: PC Linux
: P1 critical
Target Milestone: ---
Assignee: unassigned
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-08-31 09:28 UTC by herano1999
Modified: 2024-04-25 06:17 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description herano1999 2023-08-31 09:28:05 UTC
use the binary from this C++ file produces:

```
// add.cpp
#include <cstdio>

char bulk[1 << 20];

int main()
{
	for (int i = 1; i < 1048576; i++) bulk[i] = bulk[i - 1] + 'a';
	printf("hello, world\n");
	return 0;
}
```

and compile it with

```
g++ add.cpp -o add -O2 -static
```

using GNU time 1.7 to monitor the usage

```
/usr/bin/time -v ./add
```

it reports that `add` uses ~1800 kbytes of maxrss, but uses busybox 1.34.1

```
time -v ./add
```

it reports that `add` uses ~7300 kbytes of maxrss. It's rather 4x more than the GNU time gives. I think there should not be too much gap between the two `time` functions.
Comment 1 Wesley Moore 2023-11-10 04:36:16 UTC
It seems that time is treating the the `ru_maxrss` value as a count of pages[1][2] but it's already in kilobytes[3]:

> ru_maxrss (since Linux 2.6.32)
> This is the maximum resident set size used (in kilobytes).

[1]: https://git.busybox.net/busybox/tree/miscutils/time.c#n125
[2]: https://git.busybox.net/busybox/tree/miscutils/time.c#n284
[3]: https://man7.org/linux/man-pages/man2/getrusage.2.html
Comment 2 herano1999 2023-12-20 06:58:49 UTC
(In reply to Wesley Moore from comment #1)

Thanks for your information. I've checked the code of GNU time[1] and found that the reporting unit depends on the OS[2]. So the busybox's implementation here may be wrong.

[1] https://git.savannah.gnu.org/cgit/time.git/tree/src/time.c
[2] https://git.savannah.gnu.org/cgit/time.git/tree/configure.ac#n77
Comment 3 Wesley Moore 2023-12-20 07:03:12 UTC
(In reply to herano1999 from comment #2)

> So the busybox's implementation here may be wrong.

Yep that's what I was pointing out in my comment above.