public inbox for linux-rt-users@vger.kernel.org
 help / color / mirror / Atom feed
From: "Thomas Weißschuh (Schneider Electric)" <thomas.weissschuh@linutronix.de>
To: Clark Williams <williams@redhat.com>, John Kacur <jkacur@redhat.com>
Cc: linux-rt-users@vger.kernel.org,
	"Thomas Weißschuh (Schneider Electric)"
	<thomas.weissschuh@linutronix.de>
Subject: [PATCH 1/6] cyclictest: Centralize clock option parsing
Date: Tue, 07 Apr 2026 08:48:41 +0200	[thread overview]
Message-ID: <20260407-auxclocks-v1-1-1ef80d14eb0a@linutronix.de> (raw)
In-Reply-To: <20260407-auxclocks-v1-0-1ef80d14eb0a@linutronix.de>

Currently the lookup of the clockid from the commandline options is done
in multiple places. This makes the addition of symbolic clock names and
new clocks cumbersome.

Move all of the clock option parsing into a new helper function.

As a side-effect fix an off-by-one error in the validation of the
clock command line argument.

The new variable can not be named 'clock' as that would conflict with
the libc function of the same name.

Signed-off-by: Thomas Weißschuh (Schneider Electric) <thomas.weissschuh@linutronix.de>
---
 src/cyclictest/cyclictest.c | 35 ++++++++++++++++++-----------------
 1 file changed, 18 insertions(+), 17 deletions(-)

diff --git a/src/cyclictest/cyclictest.c b/src/cyclictest/cyclictest.c
index 960c90560668..592cf9d9387d 100644
--- a/src/cyclictest/cyclictest.c
+++ b/src/cyclictest/cyclictest.c
@@ -1022,7 +1022,7 @@ static int priority;
 static int policy = SCHED_OTHER;	/* default policy if not specified */
 static int num_threads = 1;
 static int max_cycles;
-static int clocksel = 0;
+static clockid_t used_clock;
 static int quiet;
 static int interval = DEFAULT_INTERVAL;
 static int distance = -1;
@@ -1031,10 +1031,17 @@ static struct bitmask *main_affinity_mask = NULL;
 static int smp = 0;
 static int setaffinity = AFFINITY_UNSPECIFIED;
 
-static int clocksources[] = {
-	CLOCK_MONOTONIC,
-	CLOCK_REALTIME,
-};
+static int handleclock(const char *clockarg)
+{
+	if (strcmp(clockarg, "0") == 0)
+		used_clock = CLOCK_MONOTONIC;
+	else if (strcmp(clockarg, "1") == 0)
+		used_clock = CLOCK_REALTIME;
+	else
+		return 1;
+
+	return 0;
+}
 
 static void handlepolicy(char *polname)
 {
@@ -1195,7 +1202,7 @@ static void process_options(int argc, char *argv[], int max_cpus)
 			tracelimit = atoi(optarg); break;
 		case 'c':
 		case OPT_CLOCK:
-			clocksel = atoi(optarg); break;
+			error |= handleclock(optarg); break;
 		case OPT_DEFAULT_SYSTEM:
 			power_management = 1; break;
 		case 'd':
@@ -1377,9 +1384,6 @@ static void process_options(int argc, char *argv[], int max_cpus)
 			      "on this processor\n");
 	}
 
-	if (clocksel < 0 || clocksel > ARRAY_SIZE(clocksources))
-		error = 1;
-
 	if (oscope_reduction < 1)
 		error = 1;
 
@@ -1983,7 +1987,6 @@ int main(int argc, char **argv)
 		warn("High resolution timers not available\n");
 
 	if (check_clock_resolution) {
-		int clock;
 		uint64_t diff;
 		int k;
 		uint64_t min_non_zero_diff = UINT64_MAX;
@@ -1994,9 +1997,7 @@ int main(int argc, char **argv)
 		struct timespec *time;
 		int times;
 
-		clock = clocksources[clocksel];
-
-		if (clock_getres(clock, &res))
+		if (clock_getres(used_clock, &res))
 			warn("clock_getres failed");
 		else
 			reported_resolution = (NSEC_PER_SEC * res.tv_sec) + res.tv_nsec;
@@ -2009,9 +2010,9 @@ int main(int argc, char **argv)
 		 * This will reliably capture resolution <= 500 usec.
 		 */
 		times = 1000;
-		clock_gettime(clock, &prev);
+		clock_gettime(used_clock, &prev);
 		for (k=0; k < times; k++)
-			clock_gettime(clock, &now);
+			clock_gettime(used_clock, &now);
 
 		diff = calcdiff_ns(now, prev);
 		if (diff == 0) {
@@ -2035,7 +2036,7 @@ int main(int argc, char **argv)
 		time = calloc(times, sizeof(*time));
 
 		for (k=0; k < times; k++)
-			clock_gettime(clock, &time[k]);
+			clock_gettime(used_clock, &time[k]);
 
 		info(ct_debug, "For %d consecutive calls to clock_gettime():\n", times);
 		info(ct_debug, "time, delta time (nsec)\n");
@@ -2193,7 +2194,7 @@ int main(int argc, char **argv)
 		}
 		if (priospread)
 			priority--;
-		par->clock = clocksources[clocksel];
+		par->clock = used_clock;
 		par->mode = mode;
 		par->timermode = timermode;
 		par->signal = signum;

-- 
2.53.0


  reply	other threads:[~2026-04-07  6:51 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-07  6:48 [PATCH 0/6] cyclictest: Add support for auxiliary clocks Thomas Weißschuh (Schneider Electric)
2026-04-07  6:48 ` Thomas Weißschuh (Schneider Electric) [this message]
2026-04-07  8:27   ` [PATCH 1/6] cyclictest: Centralize clock option parsing Florian Bezdeka
2026-04-07  8:45     ` Thomas Weißschuh (Schneider Electric)
2026-04-15 14:46   ` John Kacur
2026-04-07  6:48 ` [PATCH 2/6] cyclictest: Test the return value of timer_create() Thomas Weißschuh (Schneider Electric)
2026-04-15 14:47   ` John Kacur
2026-04-07  6:48 ` [PATCH 3/6] cyclictest: Fix error error returns for uclibc clock_nanosleep() fallback Thomas Weißschuh (Schneider Electric)
2026-04-07  8:16   ` Florian Bezdeka
2026-04-07  8:50     ` Thomas Weißschuh (Schneider Electric)
2026-04-07  8:59       ` Florian Bezdeka
2026-04-15 14:47   ` John Kacur
2026-04-07  6:48 ` [PATCH 4/6] cyclictest: Correctly print clock_nanosleep() errors Thomas Weißschuh (Schneider Electric)
2026-04-15 14:47   ` John Kacur
2026-04-07  6:48 ` [PATCH 5/6] cyclictest: Allow symbolic clock names Thomas Weißschuh (Schneider Electric)
2026-04-15 14:48   ` John Kacur
2026-04-07  6:48 ` [PATCH 6/6] cyclictest: Add support for auxiliary clocks Thomas Weißschuh (Schneider Electric)
2026-04-15 14:48   ` John Kacur

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=20260407-auxclocks-v1-1-1ef80d14eb0a@linutronix.de \
    --to=thomas.weissschuh@linutronix.de \
    --cc=jkacur@redhat.com \
    --cc=linux-rt-users@vger.kernel.org \
    --cc=williams@redhat.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