Tests: cleanly exit from test apps on reception of SIGTERM
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Thu, 15 Feb 2018 16:53:17 +0000 (11:53 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Thu, 15 Feb 2018 16:53:17 +0000 (11:53 -0500)
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 <jeremie.galarneau@efficios.com>
tests/utils/testapp/Makefile.am
tests/utils/testapp/gen-ust-events/Makefile.am
tests/utils/testapp/gen-ust-events/gen-ust-events.c
tests/utils/testapp/gen-ust-nevents-str/Makefile.am
tests/utils/testapp/gen-ust-nevents-str/gen-ust-nevents-str.c
tests/utils/testapp/gen-ust-nevents/Makefile.am
tests/utils/testapp/gen-ust-nevents/gen-ust-nevents.c
tests/utils/testapp/gen-ust-tracef/Makefile.am
tests/utils/testapp/gen-ust-tracef/gen-ust-tracef.c
tests/utils/testapp/signal-helper.h [new file with mode: 0644]

index 84113a23c3176dd5f975f59f90cae3ce18ff7e62..036561e307c1b0e38bb5c5c14a52b2b1ee405381 100644 (file)
@@ -1,2 +1,3 @@
 SUBDIRS = gen-ust-events gen-ust-nevents gen-ust-nevents-str gen-ust-tracef
 
+noinst_HEADERS = signal-helper.h
index 1c2fcf845bfa703881f940013442fc98c3557cbc..3169b6ef450781b0069d6f994a53a327b0c5a73d 100644 (file)
@@ -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
index 3cfadbe9976a70aa6bdc16240707d49160ca01b0..c5dd31a355e1294229529a4ff20c3a3026bf8347 100644 (file)
@@ -32,6 +32,7 @@
 #include <poll.h>
 #include <errno.h>
 #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:
index ed0e6593722a4e06b5dc07d75d6969c69287332b..5f07e795be18ece2137d422968c3de69d3a41a40 100644 (file)
@@ -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
index 6d90bfc66524c49cf961e7e624538d0e53905cbc..904991a4d40cf055cfa8340c3d718c9854091a2e 100644 (file)
@@ -17,6 +17,7 @@
 
 #define _LGPL_SOURCE
 #include <stdio.h>
+#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;
index 27dc1dc4a05e17f5a766edb775c781c532a14d31..33c2d41efa8795b960ea08ccc864373a7fbeaf06 100644 (file)
@@ -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
index 9d9f171b4616425b99116088306dedb9d16d7990..17fd8626ca5e03b14852a5c1466313aa5fc5924b 100644 (file)
@@ -26,6 +26,7 @@
 #include <sys/types.h>
 #include <unistd.h>
 #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:
index 6e746f05266fa7e5aaa50f64456de0d34de7c0ad..60ed301d1741110fa390f95a39043f9e3c730805 100644 (file)
@@ -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
index 0f70ef0f32134acc4e9aa81f3898a2bab24476cc..971bfd8d16a5f6d82de2481a5657f7a19c6db1b5 100644 (file)
@@ -29,6 +29,7 @@
 #include <unistd.h>
 
 #include <lttng/tracef.h>
+#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 (file)
index 0000000..4169e15
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) - 2018 Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * 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 <signal.h>
+
+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 */
This page took 0.02995 seconds and 4 git commands to generate.