Skip to contentSkip to conversation

tst_timer: Add tst_timer_expired_ms()

RFC PATCH New Cyril Hrubis 2 participants 3 messages Jun 28, 2018 – Jun 29, 2018 Library code

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 tst_timer: Add tst_timer_expired_ms()

Recently we were in the need of easy way to run a testcase for a given time interval. We nearly had everything in the timer library already but the problem was that the sampling function i.e. tst_timer_stop() and conversion functions e.g. tst_timer_elapsed_ms() were separated.

This commit adds a tst_timer_expired_ms() function that combines the sampling and comparsion, the intended usage is:

static void setup(void)

{
	...
	tst_timer_check(CLOCK_MONOTONIC);
	...
}

static void run(void)

{
	...

	tst_timer_start(CLOCK_MONOTONIC);

	...

	while (!tst_timer_expired_ms(5000)) {
		...
	}

	...
}
---
 doc/test-writing-guidelines.txt         | 41 +++++++++++++++++++++++++++++-
 include/tst_timer.h                     |  8 ++++++
 lib/newlib_tests/.gitignore             |  1 +
 lib/newlib_tests/Makefile               |  1 +
 lib/newlib_tests/tst_expiration_timer.c | 45 +++++++++++++++++++++++++++++++++
 lib/tst_timer.c                         | 10 ++++++++
 6 files changed, 105 insertions(+), 1 deletion(-)
 create mode 100644 lib/newlib_tests/tst_expiration_timer.c
doc/test-writing-guidelines.txt +40 -1
index bf421e11d..97b4fd875 100644
@@ -1167,7 +1167,7 @@ const char *const cmd[] = { "ls", "-l", NULL };
 
 [source,c]
 -------------------------------------------------------------------------------
-#include "tst_test.h"
+#include "tst_timer.h"
 
 void tst_timer_check(clockid_t clk_id);
 
@@ -1180,6 +1180,8 @@ struct timespec tst_timer_elapsed(void);
 long long tst_timer_elapsed_ms(void);
 
 long long tst_timer_elapsed_us(void);
+
+int tst_timer_expired_ms(long long ms);
 -------------------------------------------------------------------------------
 
 The 'tst_timer_check()' function checks if specified 'clk_id' is suppored and
@@ -1196,12 +1198,49 @@ call to 'tst_timer_start()'.
 The 'tst_timer_elapsed*()' returns time difference between the timer start and
 last timer stop in several formats and units.
 
+The 'tst_timer_expired_ms()' function checks if the timer started by
+'tst_timer_start()' has been running longer than ms miliseconds. The function
+returns non-zero if timer has expired and zero otherwise.
+
 IMPORTANT: The timer functions use 'clock_gettime()' internally which needs to
            be linked with '-lrt' on older glibc. Please do not forget to add
 	   'LDLIBS+=-lrt' in Makefile.
 
 [source,c]
 -------------------------------------------------------------------------------
+#include "tst_test.h"
+#include "tst_timer.h"
+
+static void setup(void)
+{
+	...
+	tst_timer_check(CLOCK_MONOTONIC);
+	...
+}
+
+static void run(void)
+{
+	...
+	tst_timer_start(CLOCK_MONOTONIC);
+	...
+	while (!tst_timer_expired_ms(5000)) {
+		...
+	}
+	...
+}
+
+sturct tst_test test = {
+	...
+	.setup = setup,
+	.test_all = run,
+	...
+};
+-------------------------------------------------------------------------------
+
+Expiration timer example usage.
+
+[source,c]
+-------------------------------------------------------------------------------
 long long tst_timespec_to_us(struct timespec t);
 long long tst_timespec_to_ms(struct timespec t);
 
include/tst_timer.h +8 -0
index 1b00e3cd1..0fd7ed6cf 100644
@@ -252,6 +252,14 @@ void tst_timer_check(clockid_t clk_id);
 void tst_timer_start(clockid_t clk_id);
 
 /*
+ * Returns true if timer started by tst_timer_start() has been running for
+ * longer than ms seconds.
+ *
+ * @ms: Time interval in miliseconds.
+ */
+int tst_timer_expired_ms(long long ms);
+
+/*
  * Marks timer end time.
  */
 void tst_timer_stop(void);
lib/newlib_tests/.gitignore +1 -0
index d0ff41929..8c0981ec8 100644
@@ -19,3 +19,4 @@ tst_safe_fileops
 tst_res_hexd
 tst_strstatus
 test17
+tst_expiration_timer
lib/newlib_tests/Makefile +1 -0
index afa09373e..2fc50160a 100644
@@ -10,6 +10,7 @@ test09: CFLAGS+=-pthread
 test15: CFLAGS+=-pthread
 test16: CFLAGS+=-pthread
 test16: LDLIBS+=-lrt
+tst_expiration_timer: LDLIBS+=-lrt
 
 ifeq ($(ANDROID),1)
 FILTER_OUT_MAKE_TARGETS	+= test08
lib/newlib_tests/tst_expiration_timer.c new file +45 -0

45 lines not on this page · raw message

lib/tst_timer.c +10 -0
index afdb44172..53ff36777 100644
@@ -77,6 +77,16 @@ void tst_timer_start(clockid_t clk_id)
 		tst_resm(TWARN | TERRNO, "tst_clock_gettime() failed");
 }
 
+int tst_timer_expired_ms(long long ms)
+{
+	struct timespec cur_time;
+
+	if (tst_clock_gettime(clock_id, &cur_time))
+		tst_resm(TWARN | TERRNO, "tst_clock_gettime() failed");
+
+	return tst_timespec_diff_ms(cur_time, start_time) >= ms;
+}
+
 void tst_timer_stop(void)
 {
 	if (tst_clock_gettime(clock_id, &stop_time))
Signature · 1 line
2.13.6
#1 lore raw

RFC PATCH 1/1 tst_timer: Add tst_timer_expired_ms()

----- Original Message -----

Quoted 30 lines

Recently we were in the need of easy way to run a testcase for a given time interval. We nearly had everything in the timer library already but the problem was that the sampling function i.e. tst_timer_stop() and conversion functions e.g. tst_timer_elapsed_ms() were separated.

This commit adds a tst_timer_expired_ms() function that combines the sampling and comparsion, the intended usage is:

static void setup(void)

{
	...
	tst_timer_check(CLOCK_MONOTONIC);
	...
}

static void run(void)

{
	...

	tst_timer_start(CLOCK_MONOTONIC);

	...

	while (!tst_timer_expired_ms(5000)) {
		...
	}

	...
}
Quoted 2 lines

ACK

I'm thinking if it's worth skipping also tst_timer_check(), and adding some like "LTP_TIMER_ANY" that would pick first available clockid for you.

Regards,
Jan

#2 lore raw

RFC PATCH 1/1 tst_timer: Add tst_timer_expired_ms()

Hi!

Quoted 2 lines

ACK

Pushed, thanks.

Quoted 3 lines

I'm thinking if it's worth skipping also tst_timer_check(), and adding some like "LTP_TIMER_ANY" that would pick first available clockid for you.

For the expiration timer we probably want to try the monotonic timer and if that fail fall back to the realtime. But I wonder if it's a good idea to invent our specific timer ids. Maybe it would be better to indroduce tst_timer_start_any() function instead so that we can do:

static void run(void)

{
	tst_timer_start_any();

	while (!tst_timer_expired_ms(5000)) {
		...
	}
}
Signature · 2 lines
Cyril Hrubis
[email protected]