perf test: Add -F/--dont-fork option
authorJiri Olsa <jolsa@kernel.org>
Tue, 28 Jun 2016 11:29:01 +0000 (13:29 +0200)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Thu, 30 Jun 2016 21:27:45 +0000 (18:27 -0300)
Adding -F/--dont-fork option to bypass forking for each test. It's
useful for debugging test.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Tested-by: Nilay Vaish <nilayvaish@gmail.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Link: http://lkml.kernel.org/r/1467113345-12669-1-git-send-email-jolsa@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/Documentation/perf-test.txt
tools/perf/tests/builtin-test.c

index 31a5c3ea7f74233271626f964dddc9cbd09f6526..b329c65d7f40b5585dc524fcf901657641166f0a 100644 (file)
@@ -30,3 +30,7 @@ OPTIONS
 -v::
 --verbose::
        Be more verbose.
+
+-F::
+--dont-fork::
+       Do not fork child for each test, run all tests within single process.
index 0e95c20ecf6e730d95d83bc720dc03a4c4fc47a4..5781c1640eae03e926f94d24cf8d1074a8425160 100644 (file)
@@ -14,6 +14,8 @@
 #include <subcmd/parse-options.h>
 #include "symbol.h"
 
+static bool dont_fork;
+
 struct test __weak arch_tests[] = {
        {
                .func = NULL,
@@ -247,7 +249,7 @@ static bool perf_test__matches(struct test *test, int curr, int argc, const char
 
 static int run_test(struct test *test, int subtest)
 {
-       int status, err = -1, child = fork();
+       int status, err = -1, child = dont_fork ? 0 : fork();
        char sbuf[STRERR_BUFSIZE];
 
        if (child < 0) {
@@ -257,34 +259,41 @@ static int run_test(struct test *test, int subtest)
        }
 
        if (!child) {
-               pr_debug("test child forked, pid %d\n", getpid());
-               if (!verbose) {
-                       int nullfd = open("/dev/null", O_WRONLY);
-                       if (nullfd >= 0) {
-                               close(STDERR_FILENO);
-                               close(STDOUT_FILENO);
-
-                               dup2(nullfd, STDOUT_FILENO);
-                               dup2(STDOUT_FILENO, STDERR_FILENO);
-                               close(nullfd);
+               if (!dont_fork) {
+                       pr_debug("test child forked, pid %d\n", getpid());
+
+                       if (!verbose) {
+                               int nullfd = open("/dev/null", O_WRONLY);
+
+                               if (nullfd >= 0) {
+                                       close(STDERR_FILENO);
+                                       close(STDOUT_FILENO);
+
+                                       dup2(nullfd, STDOUT_FILENO);
+                                       dup2(STDOUT_FILENO, STDERR_FILENO);
+                                       close(nullfd);
+                               }
+                       } else {
+                               signal(SIGSEGV, sighandler_dump_stack);
+                               signal(SIGFPE, sighandler_dump_stack);
                        }
-               } else {
-                       signal(SIGSEGV, sighandler_dump_stack);
-                       signal(SIGFPE, sighandler_dump_stack);
                }
 
                err = test->func(subtest);
-               exit(err);
+               if (!dont_fork)
+                       exit(err);
        }
 
-       wait(&status);
+       if (!dont_fork) {
+               wait(&status);
 
-       if (WIFEXITED(status)) {
-               err = (signed char)WEXITSTATUS(status);
-               pr_debug("test child finished with %d\n", err);
-       } else if (WIFSIGNALED(status)) {
-               err = -1;
-               pr_debug("test child interrupted\n");
+               if (WIFEXITED(status)) {
+                       err = (signed char)WEXITSTATUS(status);
+                       pr_debug("test child finished with %d\n", err);
+               } else if (WIFSIGNALED(status)) {
+                       err = -1;
+                       pr_debug("test child interrupted\n");
+               }
        }
 
        return err;
@@ -425,6 +434,8 @@ int cmd_test(int argc, const char **argv, const char *prefix __maybe_unused)
        OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
        OPT_INCR('v', "verbose", &verbose,
                    "be more verbose (show symbol address, etc)"),
+       OPT_BOOLEAN('F', "dont-fork", &dont_fork,
+                   "Do not fork for testcase"),
        OPT_END()
        };
        const char * const test_subcommands[] = { "list", NULL };