From 95170e51ab1938d82b929549e2d4d813a56c163e 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 +++ .../testapp/gen-ust-nevents-str/Makefile.am | 3 +- .../gen-ust-nevents-str/gen-ust-nevents-str.c | 8 +++ .../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 +++++++++++++++++++ 10 files changed, 100 insertions(+), 4 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 84113a23c..036561e30 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-nevents-str 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 1c2fcf845..3169b6ef4 100644 --- a/tests/utils/testapp/gen-ust-events/Makefile.am +++ b/tests/utils/testapp/gen-ust-events/Makefile.am @@ -1,4 +1,5 @@ -AM_CPPFLAGS += -I$(top_srcdir)/tests/utils -I$(srcdir) +AM_CPPFLAGS += -I$(top_srcdir)/tests/utils -I$(srcdir) \ + -I$(top_srcdir)/tests/utils/testapp if HAVE_LIBLTTNG_UST_CTL noinst_PROGRAMS = gen-ust-events 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-str/Makefile.am b/tests/utils/testapp/gen-ust-nevents-str/Makefile.am index ed0e65937..5f07e795b 100644 --- a/tests/utils/testapp/gen-ust-nevents-str/Makefile.am +++ b/tests/utils/testapp/gen-ust-nevents-str/Makefile.am @@ -1,4 +1,5 @@ -AM_CPPFLAGS += -I$(srcdir) -I$(top_srcdir)/tests/utils +AM_CPPFLAGS += -I$(srcdir) -I$(top_srcdir)/tests/utils \ + -I$(top_srcdir)/tests/utils/testapp if HAVE_LIBLTTNG_UST_CTL noinst_PROGRAMS = gen-ust-nevents-str diff --git a/tests/utils/testapp/gen-ust-nevents-str/gen-ust-nevents-str.c b/tests/utils/testapp/gen-ust-nevents-str/gen-ust-nevents-str.c index 6d90bfc66..904991a4d 100644 --- a/tests/utils/testapp/gen-ust-nevents-str/gen-ust-nevents-str.c +++ b/tests/utils/testapp/gen-ust-nevents-str/gen-ust-nevents-str.c @@ -17,6 +17,7 @@ #define _LGPL_SOURCE #include +#include "signal-helper.h" #define TRACEPOINT_DEFINE #include "tp.h" @@ -27,6 +28,10 @@ int main(int argc, char **argv) int i; int arg_i; + if (set_signal_handler()) { + return 1; + } + if (argc <= 3) { fprintf(stderr, "Usage: %s COUNT STRING [STRING]...\n", argv[0]); @@ -48,6 +53,9 @@ int main(int argc, char **argv) if (arg_i == argc) { arg_i = 2; } + if (should_quit) { + break; + } } return 0; diff --git a/tests/utils/testapp/gen-ust-nevents/Makefile.am b/tests/utils/testapp/gen-ust-nevents/Makefile.am index 27dc1dc4a..33c2d41ef 100644 --- a/tests/utils/testapp/gen-ust-nevents/Makefile.am +++ b/tests/utils/testapp/gen-ust-nevents/Makefile.am @@ -1,4 +1,5 @@ -AM_CPPFLAGS += -I$(srcdir) -I$(top_srcdir)/tests/utils +AM_CPPFLAGS += -I$(srcdir) -I$(top_srcdir)/tests/utils \ + -I$(top_srcdir)/tests/utils/testapp if HAVE_LIBLTTNG_UST_CTL noinst_PROGRAMS = gen-ust-nevents 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 6e746f052..60ed301d1 100644 --- a/tests/utils/testapp/gen-ust-tracef/Makefile.am +++ b/tests/utils/testapp/gen-ust-tracef/Makefile.am @@ -1,4 +1,5 @@ -AM_CPPFLAGS += -I$(srcdir) +AM_CPPFLAGS += -I$(srcdir) \ + -I$(top_srcdir)/tests/utils/testapp if HAVE_LIBLTTNG_UST_CTL noinst_PROGRAMS = gen-ust-tracef 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