From 0458ed8c7f5c46e1fb56c8cb920ddbbc7adda8a8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Galarneau?= Date: Thu, 12 Nov 2015 16:42:05 -0500 Subject: [PATCH] Tests: Add a test utils library MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérémie Galarneau --- tests/utils/Makefile.am | 5 ++- tests/utils/utils.c | 70 +++++++++++++++++++++++++++++++++++++++++ tests/utils/utils.h | 23 ++++++++++++++ 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 tests/utils/utils.c create mode 100644 tests/utils/utils.h diff --git a/tests/utils/Makefile.am b/tests/utils/Makefile.am index f43233d86..eac5bdbc8 100644 --- a/tests/utils/Makefile.am +++ b/tests/utils/Makefile.am @@ -1,7 +1,10 @@ -SUBDIRS = tap testapp +SUBDIRS = . tap testapp EXTRA_DIST = utils.sh test_utils.py babelstats.pl warn_lttng_processes.sh dist_noinst_SCRIPTS = utils.sh test_utils.py babelstats.pl warn_lttng_processes.sh +noinst_LTLIBRARIES = libtestutils.la + +libtestutils_la_SOURCES = utils.c utils.h all-local: @if [ x"$(srcdir)" != x"$(builddir)" ]; then \ diff --git a/tests/utils/utils.c b/tests/utils/utils.c new file mode 100644 index 000000000..e7bd9ce9f --- /dev/null +++ b/tests/utils/utils.c @@ -0,0 +1,70 @@ +/* + * Copyright (C) - 2015 Jérémie Galarneau + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation; version 2.1 of the License. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +#include +#include +#include +#include + +#define NSEC_PER_SEC 1000000000ULL +#define NSEC_PER_USEC 1000ULL + +static inline +int64_t elapsed_time_ns(struct timespec *t1, struct timespec *t2) +{ + struct timespec delta; + + assert(t1 && t2); + delta.tv_sec = t2->tv_sec - t1->tv_sec; + delta.tv_nsec = t2->tv_nsec - t1->tv_nsec; + return ((int64_t) NSEC_PER_SEC * (int64_t) delta.tv_sec) + + (int64_t) delta.tv_nsec; +} + +int usleep_safe(useconds_t usec) +{ + int ret = 0; + struct timespec t1, t2; + int64_t time_remaining_ns = (int64_t) usec * (int64_t) NSEC_PER_USEC; + + ret = clock_gettime(CLOCK_MONOTONIC, &t1); + if (ret) { + ret = -1; + perror("clock_gettime"); + goto end; + } + + while (time_remaining_ns > 0) { + ret = usleep(time_remaining_ns / (int64_t) NSEC_PER_USEC); + if (ret && errno != EINTR) { + perror("usleep"); + goto end; + } + + ret = clock_gettime(CLOCK_MONOTONIC, &t2); + if (ret) { + perror("clock_gettime"); + goto end; + } + + time_remaining_ns -= elapsed_time_ns(&t1, &t2); + } +end: + return ret; +} diff --git a/tests/utils/utils.h b/tests/utils/utils.h new file mode 100644 index 000000000..e5a5310d9 --- /dev/null +++ b/tests/utils/utils.h @@ -0,0 +1,23 @@ +/* + * Copyright (C) - 2015 Jérémie Galarneau + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation; version 2.1 of the License. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef TEST_UTILS_H +#define TEST_UTILS_H + +int usleep_safe(useconds_t usec); + +#endif /* TEST_UTILS_H */ -- 2.34.1