[PATCH] getrusage: Add smoke test for getrusage

Collin Funk collin.funk1@gmail.com
Sat Jun 20 09:17:07 GMT 2026


Hi Ondrej,

Ondrej Marek <ondrejm4rek@gmail.com> writes:

> Add smoke test verifying that getrusage returns non-negative user time and fails with invalid who value.
>
> Co-authored-by: Frantisek Cech <fr.cech@proton.me>
> Signed-off-by: Ondrej Marek <ondrejm4rek@gmail.com>
> ---
>  resource/Makefile        |  1 +
>  resource/tst-getrusage.c | 87 ++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 88 insertions(+)
>  create mode 100644 resource/tst-getrusage.c
>
> diff --git a/resource/Makefile b/resource/Makefile
> index 589b73f44f..dac48434dd 100644
> --- a/resource/Makefile
> +++ b/resource/Makefile
> @@ -28,6 +28,7 @@ routines := getrlimit setrlimit getrlimit64 setrlimit64 getrusage ulimit      \
>  tests := \
>    bug-ulimit1 \
>    tst-getrlimit \
> +  tst-getrusage \
>  # tests
>  
>  
> diff --git a/resource/tst-getrusage.c b/resource/tst-getrusage.c
> new file mode 100644
> index 0000000000..3ef2b15ddb
> --- /dev/null
> +++ b/resource/tst-getrusage.c
> @@ -0,0 +1,87 @@
> +/* Test of the getrusage function.
> +   Copyright (C) 2005-2026 Free Software Foundation, Inc.
> +   This file is part of the GNU C Library.
> +
> +   The GNU C Library is free software; you can redistribute it and/or
> +   modify it under the terms of the GNU Lesser General Public
> +   License as published by the Free Software Foundation; either
> +   version 2.1 of the License, or (at your option) any later version.
> +
> +   The GNU C Library is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +   Lesser General Public License for more details.
> +
> +   You should have received a copy of the GNU Lesser General Public
> +   License along with the GNU C Library; if not, see
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#include <errno.h>
> +#include <stdio.h>
> +#include <sys/resource.h>
> +
> +static int
> +test_getrusage (int who)
> +{
> +  struct rusage before_usage = { 0 };
> +  struct rusage after_usage = { 0 };
> +
> +  int ret_val = getrusage (who, &before_usage);
> +  if (ret_val != 0)
> +    {
> +      puts ("Valid who should return 0\n");
> +      return 1;
> +    }
> +  if (before_usage.ru_utime.tv_sec < 0)
> +    {
> +      puts ("Should return non-negative user time\n");
> +      return 1;
> +    }
> +
> +  /* Simulate some work */
> +  for (int i = 0; i < 10000000; i++) {}

I would be surprised if the compiler does not optimize this loop away.

> +  int ret_val2 = getrusage (who, &after_usage);
> +  if (ret_val2 != 0)
> +    {
> +      puts ("Valid who should return 0\n");
> +      return 1;
> +    }
> +  if (before_usage.ru_utime.tv_sec > after_usage.ru_utime.tv_sec)
> +    {
> +      puts ("User time after work should be same or greater than before work\n");
> +      return 1;
> +    }

This doesn't work if the program finishes executing in under a
second. You would have to check if the tv_sec fields are equal. If so,
then you would compare the tv_usec fields.

> +
> +  return 0;
> +}
> +
> +static int
> +do_test (void)
> +{
> +  int whos[] = { RUSAGE_SELF, RUSAGE_CHILDREN, RUSAGE_THREAD };
> +  for (int i = 0; i < sizeof (whos) / sizeof (int); i++)
> +    {
> +      int who = whos[i];
> +      int retval = test_getrusage (who);
> +      if (retval != 0)
> +        {
> +          return 1;
> +        }
> +    }

I haven't run the test, but I would assume that it fails with
RUSAGE_CHILDREN because we never fork a child process.

> +
> +  struct rusage usage_struct = { 0 };
> +  int invalid_who = 999;
> +  int ret_val3 = getrusage (invalid_who, &usage_struct);
> +  if (ret_val3 != -1)
> +    {
> +      puts ("Invalid who should return -1\n");
> +      return 1;
> +    }
> +
> +  puts ("Everything OK\n");
> +  return 0;
> +}
> +
> +#define TEST_FUNCTION do_test ()
> +#include "../test-skeleton.c"

New tests should support/test-driver.c. You can run the following
command to find some examples:

    $ git grep -F '<support/test-driver.c>' -- '*.c'

Collin


More information about the Libc-alpha mailing list