From: Cyril Hrubis <chrubis@suse.cz>
To: Andrea Cervesato <andrea.cervesato@suse.de>
Cc: Linux Test Project <ltp@lists.linux.it>
Subject: Re: [LTP] [PATCH v3] clock_settime: use POSIX runtime detection for CLOCK_MONOTONIC
Date: Mon, 4 May 2026 11:07:52 +0200 [thread overview]
Message-ID: <afhh6HGimjQqHvWf@yuki.lan> (raw)
In-Reply-To: <20260429-fix_clock_settime_helper-v3-1-2608c39d8499@suse.com>
Hi!
> diff --git a/testcases/open_posix_testsuite/include/clock.h b/testcases/open_posix_testsuite/include/clock.h
> new file mode 100644
> index 0000000000000000000000000000000000000000..221dd38ff34b627aa5f6c07825cd7439cf559c74
> --- /dev/null
> +++ b/testcases/open_posix_testsuite/include/clock.h
> @@ -0,0 +1,77 @@
> +/*
> + * Copyright (c) 2026, Linux Test Project
> + * SPDX-License-Identifier: GPL-2.0-or-later
> + */
> +
> +#ifndef PTS_CLOCK_H
> +#define PTS_CLOCK_H
> +
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <time.h>
> +#include <unistd.h>
> +
> +static inline int pts_mono_available(void)
> +{
> +#ifdef _POSIX_MONOTONIC_CLOCK
> + if (_POSIX_MONOTONIC_CLOCK > 0)
> + return 1;
> +
> + if (!_POSIX_MONOTONIC_CLOCK && sysconf(_SC_MONOTONIC_CLOCK) > 0)
> + return 1;
> +#endif
> + return 0;
> +}
> +
> +#define PTS_MONO_MAX_RETRIES 3
> +
> +static struct timespec pts_mono_start;
> +
> +static inline int pts_mono_time_start(void)
> +{
> + if (!pts_mono_available()) {
> + static int warned;
> +
> + if (!warned) {
> + printf("CLOCK_MONOTONIC unavailable, test may fail due to clock adjustment\n");
> + warned = 1;
> + }
> + return 0;
> + }
> +
> +#ifdef _POSIX_MONOTONIC_CLOCK
I suppose that this not correct either. If _POSIX_MONOTONIC_CLOCK is set
to -1 the CLOCK_MONOTONIC constant may not be defined.
So we either have to to do
#if _POSIX_MONOTONIC_CLOCK >= 0
or
#ifdef CLOCK_MONOTONIC
> + if (clock_gettime(CLOCK_MONOTONIC, &pts_mono_start) != 0) {
> + perror("clock_gettime(CLOCK_MONOTONIC) failed");
> + return -1;
> + }
> +#endif
> + return 0;
> +}
> +
> +static inline int pts_mono_time_check(unsigned int expected_secs)
> +{
> +#ifdef _POSIX_MONOTONIC_CLOCK
> + if (pts_mono_available()) {
> + struct timespec now;
> + long elapsed;
> +
> + if (clock_gettime(CLOCK_MONOTONIC, &now) != 0) {
> + perror("clock_gettime(CLOCK_MONOTONIC) failed");
> + return -1;
> + }
> +
> + elapsed = now.tv_sec - pts_mono_start.tv_sec;
> +
> + if (labs(elapsed - (long)expected_secs) > 1) {
> + printf("Clock adjustment detected (elapsed %lds, expected ~%us)\n",
> + elapsed, expected_secs);
> + return 1;
> + }
> + return 0;
> + }
> +#endif
> + (void)expected_secs;
> + return 0;
> +}
> +
> +#endif /* PTS_CLOCK_H */
>
> ---
> base-commit: 1d2ea5b042a6c6c169689efebd856583200c564f
> change-id: 20260429-fix_clock_settime_helper-7670708ed94f
>
> Best regards,
> --
> Andrea Cervesato <andrea.cervesato@suse.com>
>
>
> --
> Mailing list info: https://lists.linux.it/listinfo/ltp
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
next prev parent reply other threads:[~2026-05-04 9:08 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-29 8:28 [LTP] [PATCH v3] clock_settime: use POSIX runtime detection for CLOCK_MONOTONIC Andrea Cervesato
2026-04-29 10:12 ` [LTP] " linuxtestproject.agent
2026-05-04 9:07 ` Cyril Hrubis [this message]
2026-05-04 9:26 ` [LTP] [PATCH v3] " Andrea Cervesato via ltp
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=afhh6HGimjQqHvWf@yuki.lan \
--to=chrubis@suse.cz \
--cc=andrea.cervesato@suse.de \
--cc=ltp@lists.linux.it \
/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