From 942f3487bfe3eb31473e09250819ca9d335b2eaa Mon Sep 17 00:00:00 2001 From: Jens Axboe Date: Sat, 31 Aug 2024 05:38:50 -0600 Subject: [PATCH] test: move mtime helpers into helpers.h Lots of test have copied this helper, move it into a our helpers library and kill the private copies. Signed-off-by: Jens Axboe --- test/7ad0e4b2f83c.c | 25 ------------------------- test/helpers.c | 24 ++++++++++++++++++++++++ test/helpers.h | 4 ++++ test/link-timeout.c | 25 ------------------------- test/min-timeout-wait.c | 25 ------------------------- test/min-timeout.c | 25 ------------------------- test/sqpoll-exit-hang.c | 26 +------------------------- test/sqpoll-sleep.c | 26 +------------------------- test/submit-and-wait.c | 26 +------------------------- test/submit-reuse.c | 25 ------------------------- test/timeout-new.c | 27 +-------------------------- test/timeout.c | 25 ------------------------- 12 files changed, 32 insertions(+), 251 deletions(-) diff --git a/test/7ad0e4b2f83c.c b/test/7ad0e4b2f83c.c index 0b7e74f5e..6b3bbc9af 100644 --- a/test/7ad0e4b2f83c.c +++ b/test/7ad0e4b2f83c.c @@ -5,31 +5,6 @@ #include "liburing.h" #include "helpers.h" -static unsigned long long mtime_since(const struct timeval *s, - const struct timeval *e) -{ - long long sec, usec; - - sec = e->tv_sec - s->tv_sec; - usec = (e->tv_usec - s->tv_usec); - if (sec > 0 && usec < 0) { - sec--; - usec += 1000000; - } - - sec *= 1000; - usec /= 1000; - return sec + usec; -} - -static unsigned long long mtime_since_now(struct timeval *tv) -{ - struct timeval end; - - gettimeofday(&end, NULL); - return mtime_since(tv, &end); -} - int main(int argc, char *argv[]) { struct __kernel_timespec ts1, ts2; diff --git a/test/helpers.c b/test/helpers.c index 779347f63..e32cc1809 100644 --- a/test/helpers.c +++ b/test/helpers.c @@ -316,3 +316,27 @@ void t_error(int status, int errnum, const char *format, ...) va_end(args); exit(status); } + +unsigned long long mtime_since(const struct timeval *s, const struct timeval *e) +{ + long long sec, usec; + + sec = e->tv_sec - s->tv_sec; + usec = (e->tv_usec - s->tv_usec); + if (sec > 0 && usec < 0) { + sec--; + usec += 1000000; + } + + sec *= 1000; + usec /= 1000; + return sec + usec; +} + +unsigned long long mtime_since_now(struct timeval *tv) +{ + struct timeval end; + + gettimeofday(&end, NULL); + return mtime_since(tv, &end); +} diff --git a/test/helpers.h b/test/helpers.h index 9f62a5f63..ea20c8efb 100644 --- a/test/helpers.h +++ b/test/helpers.h @@ -12,6 +12,7 @@ extern "C" { #include "liburing.h" #include "../src/setup.h" #include +#include enum t_setup_ret { T_SETUP_OK = 0, @@ -101,6 +102,9 @@ static inline int t_io_uring_init_sqarray(unsigned entries, struct io_uring *rin void t_error(int status, int errnum, const char *format, ...); +unsigned long long mtime_since(const struct timeval *s, const struct timeval *e); +unsigned long long mtime_since_now(struct timeval *tv); + #ifdef __cplusplus } #endif diff --git a/test/link-timeout.c b/test/link-timeout.c index a110effe1..63de29ec2 100644 --- a/test/link-timeout.c +++ b/test/link-timeout.c @@ -538,31 +538,6 @@ static int test_single_link_timeout(struct io_uring *ring, unsigned nsec) return 1; } -static unsigned long long mtime_since(const struct timeval *s, - const struct timeval *e) -{ - long long sec, usec; - - sec = e->tv_sec - s->tv_sec; - usec = (e->tv_usec - s->tv_usec); - if (sec > 0 && usec < 0) { - sec--; - usec += 1000000; - } - - sec *= 1000; - usec /= 1000; - return sec + usec; -} - -static unsigned long long mtime_since_now(struct timeval *tv) -{ - struct timeval end; - - gettimeofday(&end, NULL); - return mtime_since(tv, &end); -} - static int test_link_timeout_update(struct io_uring *ring, int async) { struct __kernel_timespec ts; diff --git a/test/min-timeout-wait.c b/test/min-timeout-wait.c index dfb237c5b..477a68e4a 100644 --- a/test/min-timeout-wait.c +++ b/test/min-timeout-wait.c @@ -22,31 +22,6 @@ struct data { int nr_fds; }; -static unsigned long long mtime_since(const struct timeval *s, - const struct timeval *e) -{ - long long sec, usec; - - sec = e->tv_sec - s->tv_sec; - usec = (e->tv_usec - s->tv_usec); - if (sec > 0 && usec < 0) { - sec--; - usec += 1000000; - } - - sec *= 1000; - usec /= 1000; - return sec + usec; -} - -static unsigned long long mtime_since_now(struct timeval *tv) -{ - struct timeval end; - - gettimeofday(&end, NULL); - return mtime_since(tv, &end); -} - static int time_pass(struct timeval *start, unsigned long min_t, unsigned long max_t, const char *name) { diff --git a/test/min-timeout.c b/test/min-timeout.c index f4a10ba2e..7f090a24f 100644 --- a/test/min-timeout.c +++ b/test/min-timeout.c @@ -61,31 +61,6 @@ static int within_range(unsigned int target, unsigned int msec) return (msec >= low && msec <= high); } -static unsigned long long mtime_since(const struct timeval *s, - const struct timeval *e) -{ - long long sec, usec; - - sec = e->tv_sec - s->tv_sec; - usec = (e->tv_usec - s->tv_usec); - if (sec > 0 && usec < 0) { - sec--; - usec += 1000000; - } - - sec *= 1000; - usec /= 1000; - return sec + usec; -} - -static unsigned long long mtime_since_now(struct timeval *tv) -{ - struct timeval end; - - gettimeofday(&end, NULL); - return mtime_since(tv, &end); -} - static int test(int flags, int expected_ctx, int min_wait, int write_delay, int nr_cqes, int msec_target) { diff --git a/test/sqpoll-exit-hang.c b/test/sqpoll-exit-hang.c index cde211500..ff1924a98 100644 --- a/test/sqpoll-exit-hang.c +++ b/test/sqpoll-exit-hang.c @@ -10,31 +10,7 @@ #include #include #include "liburing.h" - -static unsigned long long mtime_since(const struct timeval *s, - const struct timeval *e) -{ - long long sec, usec; - - sec = e->tv_sec - s->tv_sec; - usec = (e->tv_usec - s->tv_usec); - if (sec > 0 && usec < 0) { - sec--; - usec += 1000000; - } - - sec *= 1000; - usec /= 1000; - return sec + usec; -} - -static unsigned long long mtime_since_now(struct timeval *tv) -{ - struct timeval end; - - gettimeofday(&end, NULL); - return mtime_since(tv, &end); -} +#include "helpers.h" int main(int argc, char *argv[]) { diff --git a/test/sqpoll-sleep.c b/test/sqpoll-sleep.c index 9d1cff6d3..5cf4210aa 100644 --- a/test/sqpoll-sleep.c +++ b/test/sqpoll-sleep.c @@ -9,31 +9,7 @@ #include #include #include "liburing.h" - -static unsigned long long mtime_since(const struct timeval *s, - const struct timeval *e) -{ - long long sec, usec; - - sec = e->tv_sec - s->tv_sec; - usec = (e->tv_usec - s->tv_usec); - if (sec > 0 && usec < 0) { - sec--; - usec += 1000000; - } - - sec *= 1000; - usec /= 1000; - return sec + usec; -} - -static unsigned long long mtime_since_now(struct timeval *tv) -{ - struct timeval end; - - gettimeofday(&end, NULL); - return mtime_since(tv, &end); -} +#include "helpers.h" int main(int argc, char *argv[]) { diff --git a/test/submit-and-wait.c b/test/submit-and-wait.c index 70d42d9b1..249a372b5 100644 --- a/test/submit-and-wait.c +++ b/test/submit-and-wait.c @@ -13,33 +13,9 @@ #include #include "liburing.h" +#include "helpers.h" #include "test.h" -static unsigned long long mtime_since(const struct timeval *s, - const struct timeval *e) -{ - long long sec, usec; - - sec = e->tv_sec - s->tv_sec; - usec = (e->tv_usec - s->tv_usec); - if (sec > 0 && usec < 0) { - sec--; - usec += 1000000; - } - - sec *= 1000; - usec /= 1000; - return sec + usec; -} - -static unsigned long long mtime_since_now(struct timeval *tv) -{ - struct timeval end; - - gettimeofday(&end, NULL); - return mtime_since(tv, &end); -} - static int test(struct io_uring *ring) { struct io_uring_cqe *cqe; diff --git a/test/submit-reuse.c b/test/submit-reuse.c index d5ccdd449..1fbcc78c2 100644 --- a/test/submit-reuse.c +++ b/test/submit-reuse.c @@ -102,31 +102,6 @@ static int wait_nr(int nr) return 0; } -static unsigned long long mtime_since(const struct timeval *s, - const struct timeval *e) -{ - long long sec, usec; - - sec = e->tv_sec - s->tv_sec; - usec = (e->tv_usec - s->tv_usec); - if (sec > 0 && usec < 0) { - sec--; - usec += 1000000; - } - - sec *= 1000; - usec /= 1000; - return sec + usec; -} - -static unsigned long long mtime_since_now(struct timeval *tv) -{ - struct timeval end; - - gettimeofday(&end, NULL); - return mtime_since(tv, &end); -} - static int test_reuse(int argc, char *argv[], int split, int async) { struct thread_data data; diff --git a/test/timeout-new.c b/test/timeout-new.c index 35cb7bbff..21d140d72 100644 --- a/test/timeout-new.c +++ b/test/timeout-new.c @@ -8,6 +8,7 @@ #include #include #include "liburing.h" +#include "helpers.h" #define TIMEOUT_MSEC 200 #define TIMEOUT_SEC 10 @@ -22,32 +23,6 @@ static void msec_to_ts(struct __kernel_timespec *ts, unsigned int msec) ts->tv_nsec = (msec % 1000) * 1000000; } -static unsigned long long mtime_since(const struct timeval *s, - const struct timeval *e) -{ - long long sec, usec; - - sec = e->tv_sec - s->tv_sec; - usec = (e->tv_usec - s->tv_usec); - if (sec > 0 && usec < 0) { - sec--; - usec += 1000000; - } - - sec *= 1000; - usec /= 1000; - return sec + usec; -} - -static unsigned long long mtime_since_now(struct timeval *tv) -{ - struct timeval end; - - gettimeofday(&end, NULL); - return mtime_since(tv, &end); -} - - static int test_return_before_timeout(struct io_uring *ring) { struct io_uring_cqe *cqe; diff --git a/test/timeout.c b/test/timeout.c index 89854c039..f7dd7fa90 100644 --- a/test/timeout.c +++ b/test/timeout.c @@ -29,31 +29,6 @@ static void msec_to_ts(struct __kernel_timespec *ts, unsigned int msec) ts->tv_nsec = (msec % 1000) * 1000000; } -static unsigned long long mtime_since(const struct timeval *s, - const struct timeval *e) -{ - long long sec, usec; - - sec = e->tv_sec - s->tv_sec; - usec = (e->tv_usec - s->tv_usec); - if (sec > 0 && usec < 0) { - sec--; - usec += 1000000; - } - - sec *= 1000; - usec /= 1000; - return sec + usec; -} - -static unsigned long long mtime_since_now(struct timeval *tv) -{ - struct timeval end; - - gettimeofday(&end, NULL); - return mtime_since(tv, &end); -} - /* * Test that we return to userspace if a timeout triggers, even if we * don't satisfy the number of events asked for.