[LTP] [PATCH v4] clock_settime: use POSIX runtime detection for CLOCK_MONOTONIC
Cyril Hrubis
chrubis@suse.cz
Mon May 4 11:54:41 CEST 2026
Hi!
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <time.h>
> +#include <unistd.h>
> +
> +static inline int pts_mono_available(void)
> +{
> +#ifdef CLOCK_MONOTONIC
This one should have stayed _POSIX_MONOTONIC_CLOCK since we are doing a
presence checks and the -1 is correctly handled here (we get to return 0
case in this case).
> + 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 CLOCK_MONOTONIC
> + if (clock_gettime(CLOCK_MONOTONIC, &pts_mono_start) != 0) {
> + perror("clock_gettime(CLOCK_MONOTONIC) failed");
> + return -1;
> + }
> +#endif
These are guarding againts compilation failures and looks fine to me
now.
> + return 0;
> +}
> +
> +static inline int pts_mono_time_check(unsigned int expected_secs)
> +{
> +#ifdef CLOCK_MONOTONIC
> + 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: 73cfad509db014e5a631bd2298f4e9d8ea3ee1c1
> 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
More information about the ltp
mailing list