Skip to content

Commit

Permalink
test: move mtime helpers into helpers.h
Browse files Browse the repository at this point in the history
Lots of test have copied this helper, move it into a our helpers
library and kill the private copies.

Signed-off-by: Jens Axboe <[email protected]>
  • Loading branch information
axboe committed Aug 31, 2024
1 parent 082c3c0 commit 942f348
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 251 deletions.
25 changes: 0 additions & 25 deletions test/7ad0e4b2f83c.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
24 changes: 24 additions & 0 deletions test/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
4 changes: 4 additions & 0 deletions test/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ extern "C" {
#include "liburing.h"
#include "../src/setup.h"
#include <arpa/inet.h>
#include <sys/time.h>

enum t_setup_ret {
T_SETUP_OK = 0,
Expand Down Expand Up @@ -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
Expand Down
25 changes: 0 additions & 25 deletions test/link-timeout.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
25 changes: 0 additions & 25 deletions test/min-timeout-wait.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
25 changes: 0 additions & 25 deletions test/min-timeout.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
26 changes: 1 addition & 25 deletions test/sqpoll-exit-hang.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,7 @@
#include <sys/time.h>
#include <poll.h>
#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[])
{
Expand Down
26 changes: 1 addition & 25 deletions test/sqpoll-sleep.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,7 @@
#include <unistd.h>
#include <sys/time.h>
#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[])
{
Expand Down
26 changes: 1 addition & 25 deletions test/submit-and-wait.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,9 @@
#include <sys/time.h>

#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;
Expand Down
25 changes: 0 additions & 25 deletions test/submit-reuse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
27 changes: 1 addition & 26 deletions test/timeout-new.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <unistd.h>
#include <pthread.h>
#include "liburing.h"
#include "helpers.h"

#define TIMEOUT_MSEC 200
#define TIMEOUT_SEC 10
Expand All @@ -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;
Expand Down
25 changes: 0 additions & 25 deletions test/timeout.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 942f348

Please sign in to comment.