From 901cb87ccbcde99a10770f79df42c27f2f2d6eb7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Galarneau?= Date: Thu, 15 Feb 2018 11:53:17 -0500 Subject: [PATCH] Tests: cleanly exit from test apps on reception of SIGTERM MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit There is a known lttng-ust limitation that can cause a buffer to become unreadable if an application is killed or preempted indefinitely between the reserve and commit operations in while trying to record to a subbuffer. A buffer being unreadable will cause some tests to fail since events that are expected to be visible in a given stream may not be shown by the trace viewers as the consumer was unable to "get" that subbuffer. It was fairly easy to reproduce this failure scenario using the test_ust_fast snapshot test, in the "post_mortem" case. This test case performs the following sequence of operations: * setup a tracing session in snapshot mode * launch an app * kill(1) it after one event is known to have been produced * record a snapshot * try to read the resulting snapshot Adding logging allowed the confirmation that the "get" operation was indeed failing on the subbuffer to which the application had run. This resulted in an empty stream (file size == 0) being produced by the snapshot record operation. The test was then failing because babeltrace reported that no events were contained in the resulting trace. Since there are no concrete solution to this limitation yet, the test suite must ensure that the applications exit cleanly on reception of a signal. This patch introduces a SIGTERM signal handler in the test applications which sets a "should_quit" flag to 1 and is tested between every iteration of their event production loop. Signed-off-by: Jérémie Galarneau --- tests/utils/testapp/Makefile.am | 1 + .../utils/testapp/gen-ust-events/Makefile.am | 3 +- .../testapp/gen-ust-events/gen-ust-events.c | 9 +++ .../utils/testapp/gen-ust-nevents/Makefile.am | 3 +- .../testapp/gen-ust-nevents/gen-ust-nevents.c | 9 +++ .../utils/testapp/gen-ust-tracef/Makefile.am | 3 +- .../testapp/gen-ust-tracef/gen-ust-tracef.c | 8 +++ tests/utils/testapp/signal-helper.h | 57 +++++++++++++++++++ 8 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 tests/utils/testapp/signal-helper.h diff --git a/tests/utils/testapp/Makefile.am b/tests/utils/testapp/Makefile.am index 0341de42b..c77fcb8b0 100644 --- a/tests/utils/testapp/Makefile.am +++ b/tests/utils/testapp/Makefile.am @@ -1,2 +1,3 @@ SUBDIRS = gen-ust-events gen-ust-nevents gen-ust-tracef +noinst_HEADERS = signal-helper.h diff --git a/tests/utils/testapp/gen-ust-events/Makefile.am b/tests/utils/testapp/gen-ust-events/Makefile.am index 0cc7575d7..3945f779f 100644 --- a/tests/utils/testapp/gen-ust-events/Makefile.am +++ b/tests/utils/testapp/gen-ust-events/Makefile.am @@ -1,5 +1,6 @@ AM_CFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/src \ - -I$(top_srcdir)/tests/utils -I$(srcdir) -O2 -g + -I$(top_srcdir)/tests/utils -I$(top_srcdir)/tests/utils/testapp \ + -I$(srcdir) -O2 -g if LTTNG_TOOLS_BUILD_WITH_LIBDL LIBS += -ldl diff --git a/tests/utils/testapp/gen-ust-events/gen-ust-events.c b/tests/utils/testapp/gen-ust-events/gen-ust-events.c index 3cfadbe99..c5dd31a35 100644 --- a/tests/utils/testapp/gen-ust-events/gen-ust-events.c +++ b/tests/utils/testapp/gen-ust-events/gen-ust-events.c @@ -32,6 +32,7 @@ #include #include #include "utils.h" +#include "signal-helper.h" #define TRACEPOINT_DEFINE #include "tp.h" @@ -90,6 +91,11 @@ int main(int argc, char **argv) char *after_first_event_file_path = NULL; char *before_last_event_file_path = NULL; + if (set_signal_handler()) { + ret = -1; + goto end; + } + if (argc >= 2) { /* * If nr_iter is negative, do an infinite tracing loop. @@ -133,6 +139,9 @@ int main(int argc, char **argv) goto end; } } + if (should_quit) { + break; + } } end: diff --git a/tests/utils/testapp/gen-ust-nevents/Makefile.am b/tests/utils/testapp/gen-ust-nevents/Makefile.am index b3212228f..321630dc7 100644 --- a/tests/utils/testapp/gen-ust-nevents/Makefile.am +++ b/tests/utils/testapp/gen-ust-nevents/Makefile.am @@ -1,5 +1,6 @@ AM_CFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/src -I$(srcdir) \ - -I$(top_srcdir)/tests/utils -O2 -g + -I$(top_srcdir)/tests/utils -I$(top_srcdir)/tests/utils/testapp \ + -O2 -g if LTTNG_TOOLS_BUILD_WITH_LIBDL LIBS += -ldl diff --git a/tests/utils/testapp/gen-ust-nevents/gen-ust-nevents.c b/tests/utils/testapp/gen-ust-nevents/gen-ust-nevents.c index 9d9f171b4..17fd8626c 100644 --- a/tests/utils/testapp/gen-ust-nevents/gen-ust-nevents.c +++ b/tests/utils/testapp/gen-ust-nevents/gen-ust-nevents.c @@ -26,6 +26,7 @@ #include #include #include "utils.h" +#include "signal-helper.h" #define TRACEPOINT_DEFINE #include "tp.h" @@ -40,6 +41,11 @@ int main(int argc, char **argv) unsigned int nr_iter = 100; useconds_t nr_usec = 0; + if (set_signal_handler()) { + ret = -1; + goto end; + } + if (argc >= 2) { nr_iter = atoi(argv[1]); } @@ -67,6 +73,9 @@ int main(int argc, char **argv) goto end; } } + if (should_quit) { + break; + } } end: diff --git a/tests/utils/testapp/gen-ust-tracef/Makefile.am b/tests/utils/testapp/gen-ust-tracef/Makefile.am index 06171fa92..6b6099662 100644 --- a/tests/utils/testapp/gen-ust-tracef/Makefile.am +++ b/tests/utils/testapp/gen-ust-tracef/Makefile.am @@ -1,4 +1,5 @@ -AM_CFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/src -I$(srcdir) -O2 -g +AM_CFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/src -I$(srcdir) -O2 -g \ + -I$(top_srcdir)/tests/utils/testapp AM_LDFLAGS = if LTTNG_TOOLS_BUILD_WITH_LIBDL diff --git a/tests/utils/testapp/gen-ust-tracef/gen-ust-tracef.c b/tests/utils/testapp/gen-ust-tracef/gen-ust-tracef.c index 0f70ef0f3..971bfd8d1 100644 --- a/tests/utils/testapp/gen-ust-tracef/gen-ust-tracef.c +++ b/tests/utils/testapp/gen-ust-tracef/gen-ust-tracef.c @@ -29,6 +29,7 @@ #include #include +#include "signal-helper.h" const char *str = "test string"; @@ -54,6 +55,10 @@ int main(int argc, char **argv) useconds_t nr_usec = 0; char *tmp_file_path = NULL; + if (set_signal_handler()) { + return 1; + } + if (argc >= 2) { nr_iter = atoi(argv[1]); } @@ -78,6 +83,9 @@ int main(int argc, char **argv) create_file(tmp_file_path); } usleep(nr_usec); + if (should_quit) { + break; + } } return 0; diff --git a/tests/utils/testapp/signal-helper.h b/tests/utils/testapp/signal-helper.h new file mode 100644 index 000000000..4169e15e4 --- /dev/null +++ b/tests/utils/testapp/signal-helper.h @@ -0,0 +1,57 @@ +/* + * Copyright (C) - 2018 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 LTTNG_TESTAPP_SIGNAL_HELPER_H +#define LTTNG_TESTAPP_SIGNAL_HELPER_H + +#include + +static volatile int should_quit; + +static +void sighandler(int sig) +{ + if (sig == SIGTERM) { + should_quit = 1; + } +} + +static +int set_signal_handler(void) +{ + int ret; + struct sigaction sa = { + .sa_flags = 0, + .sa_handler = sighandler, + }; + + ret = sigemptyset(&sa.sa_mask); + if (ret) { + perror("sigemptyset"); + goto end; + } + + ret = sigaction(SIGTERM, &sa, NULL); + if (ret) { + perror("sigaction"); + goto end; + } +end: + return ret; +} + +#endif /* LTTNG_TESTAPP_SIGNAL_HELPER_H */ -- 2.34.1