The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Thomas Gleixner <tglx@kernel.org>
To: Wake Liu <wakel@google.com>, Shuah Khan <shuah@kernel.org>
Cc: linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org,
	Wake Liu <wakel@google.com>
Subject: Re: [PATCH] selftests: timers: Use CPU time for CPU timers in posix_timers test
Date: Mon, 20 Jul 2026 16:14:52 +0200	[thread overview]
Message-ID: <87fr1dn3xf.ffs@fw13> (raw)
In-Reply-To: <20260708121544.583552-1-wakel@google.com>

On Wed, Jul 08 2026 at 12:15, Wake Liu wrote:
> The posix_timers test compares the elapsed real time (GTOD) with the
> expected timer delay to verify if the timer expired. This works fine for
> real-time timers (ITIMER_REAL), but is flaky for CPU-time timers
> (ITIMER_PROF, ITIMER_VIRTUAL, and CLOCK_*_CPUTIME_ID) on loaded systems
> (like shared test runners) because the test process might be scheduled
> out, resulting in elapsed real time being larger than the consumed CPU
> time.
>
> Fix this by using the appropriate measurement method for each timer:
> - Use CPU process/thread time (via clock_gettime or getrusage) for CPU
>   timers.
> - Keep using monotonic clock (real time) for real-time timers.
>
> This makes the test robust against scheduling delays.

Lacks Signed-off-by ....

> +#include <sys/resource.h>
> +
> +static long long get_clock_time_us(clockid_t clock_id)
> +{
> +	struct timespec ts;

Newline between declaration and code.

> +	if (clock_gettime(clock_id, &ts) < 0)
> +		return -1;
> +	return (long long)ts.tv_sec * USECS_PER_SEC + ts.tv_nsec / 1000;
> +}
> +
> +static long long get_itimer_time_us(int which)
> +{
> +	struct rusage usage;
> +
> +	switch (which) {
> +	case ITIMER_REAL:
> +		return get_clock_time_us(CLOCK_MONOTONIC);
> +	case ITIMER_PROF:
> +		return get_clock_time_us(CLOCK_PROCESS_CPUTIME_ID);
> +	case ITIMER_VIRTUAL:
> +		if (getrusage(RUSAGE_SELF, &usage) < 0)
> +			return -1;
> +		return (long long)usage.ru_utime.tv_sec * USECS_PER_SEC + usage.ru_utime.tv_usec;
> +	default:
> +		return -1;
> +	}
> +}
> +
> +static int check_diff_us(long long start, long long end)
>  {
>  	long long diff;
>  
> -	diff = end.tv_usec - start.tv_usec;
> -	diff += (end.tv_sec - start.tv_sec) * USECS_PER_SEC;
> +	diff = end - start;

Move that to the declaration line.

>  	if (llabs(diff - DELAY * USECS_PER_SEC) > USECS_PER_SEC / 2) {
>  		printf("Diff too high: %lld..", diff);
> @@ -100,7 +123,7 @@ static int check_diff(struct timeval start, struct timeval end)
>  
> -	if (gettimeofday(&end, NULL) < 0)
> -		fatal_error(name, "gettimeofday()");
> +	end = get_clock_time_us(which);
> +	if (end < 0)
> +		fatal_error(name, "get_clock_time_us()");
>  
> -	ksft_test_result(check_diff(start, end) == 0,
> +	ksft_test_result(check_diff_us(start, end) == 0,
>  			 "timer_create() per %s\n", name);

Please get rid of the line break.


  reply	other threads:[~2026-07-20 14:14 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-08 12:15 [PATCH] selftests: timers: Use CPU time for CPU timers in posix_timers test Wake Liu
2026-07-20 14:14 ` Thomas Gleixner [this message]
2026-07-21 10:37   ` [PATCH v2] " Wake Liu
2026-07-21 10:38   ` [PATCH] " Wake Liu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87fr1dn3xf.ffs@fw13 \
    --to=tglx@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=shuah@kernel.org \
    --cc=wakel@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox