failtest: internally eliminate duplicate calls.
authorRusty Russell <rusty@rustcorp.com.au>
Tue, 29 Nov 2011 22:39:11 +0000 (09:09 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Tue, 29 Nov 2011 22:39:11 +0000 (09:09 +1030)
If we can get a backtrace, we can automatically eliminate identical
failures.  Surprisingly backtrace() is quite fast, but the savings for
the (naively-written) rbtree tests are impressive.  ccanlint -v time
drops from 43 seconds to 6 seconds.

ccan/failtest/_info
ccan/failtest/failtest.c
ccan/failtest/failtest.h
ccan/failtest/test/run-failpath.c
ccan/failtest/test/run-locking.c
ccan/failtest/test/run-malloc.c
ccan/failtest/test/run-open.c

index d8b2a252b779a21451ba5f6c259e4e6dfdc21c36..dd90c3d9b26e1e222a99d6b25262079537385845 100644 (file)
@@ -63,6 +63,8 @@ int main(int argc, char *argv[])
        if (strcmp(argv[1], "depends") == 0) {
                printf("ccan/build_assert\n");
                printf("ccan/compiler\n");
+               printf("ccan/hash\n");
+               printf("ccan/htable\n");
                printf("ccan/read_write_all\n");
                printf("ccan/str\n");
                printf("ccan/time\n");
index 3c4cd62affae9ce9d5c2c7b437055bfbfa0d7830..ec41e92b46e4cc514bdecb54598686b0a5e9b716 100644 (file)
@@ -19,6 +19,8 @@
 #include <ccan/read_write_all/read_write_all.h>
 #include <ccan/failtest/failtest_proto.h>
 #include <ccan/build_assert/build_assert.h>
+#include <ccan/hash/hash.h>
+#include <ccan/htable/htable_type.h>
 #include <ccan/str/str.h>
 
 enum failtest_result (*failtest_hook)(struct tlist_calls *);
@@ -46,12 +48,44 @@ struct lock_info {
        int type;
 };
 
+/* We hash the call location together with its backtrace. */
+static size_t hash_call(const struct failtest_call *call)
+{
+       return hash(call->file, strlen(call->file),
+                   hash(&call->line, 1,
+                        hash(call->backtrace, call->backtrace_num,
+                             call->type)));
+}
+
+static bool call_eq(const struct failtest_call *call1,
+                   const struct failtest_call *call2)
+{
+       unsigned int i;
+
+       if (strcmp(call1->file, call2->file) != 0
+           || call1->line != call2->line
+           || call1->type != call2->type
+           || call1->backtrace_num != call2->backtrace_num)
+               return false;
+
+       for (i = 0; i < call1->backtrace_num; i++)
+               if (call1->backtrace[i] != call2->backtrace[i])
+                       return false;
+
+       return true;
+}
+
+/* Defines struct failtable. */
+HTABLE_DEFINE_TYPE(struct failtest_call, (struct failtest_call *), hash_call,
+                  call_eq, failtable);
+
 bool (*failtest_exit_check)(struct tlist_calls *history);
 
 static struct tlist_calls history = TLIST_INIT(history);
 static int control_fd = -1;
 static struct timeval start;
 static bool probing = false;
+static struct failtable failtable;
 
 static struct write_call *child_writes = NULL;
 static unsigned int child_writes_num = 0;
@@ -67,6 +101,34 @@ static const char info_to_arg[] = "mceoxprwf";
 /* Dummy call used for failtest_undo wrappers. */
 static struct failtest_call unrecorded_call;
 
+#if HAVE_BACKTRACE
+#include <execinfo.h>
+
+static void **get_backtrace(unsigned int *num)
+{
+       static unsigned int max_back = 100;
+       void **ret;
+
+again:
+       ret = malloc(max_back * sizeof(void *));
+       *num = backtrace(ret, max_back);
+       if (*num == max_back) {
+               free(ret);
+               max_back *= 2;
+               goto again;
+       }
+       return ret;
+}
+#else
+/* This will test slightly less, since will consider all of the same
+ * calls as identical.  But, it's slightly faster! */
+static void **get_backtrace(unsigned int *num)
+{
+       *num = 0;
+       return NULL;
+}
+#endif /* HAVE_BACKTRACE */
+
 static struct failtest_call *add_history_(enum failtest_call_type type,
                                          const char *file,
                                          unsigned int line,
@@ -84,6 +146,7 @@ static struct failtest_call *add_history_(enum failtest_call_type type,
        call->file = file;
        call->line = line;
        call->cleanup = NULL;
+       call->backtrace = get_backtrace(&call->backtrace_num);
        memcpy(&call->u, elem, elem_size);
        tlist_add_tail(&history, call, list);
        return call;
@@ -381,6 +444,7 @@ static void free_call(struct failtest_call *call)
        /* We don't do this in cleanup: needed even for failed opens. */
        if (call->type == FAILTEST_OPEN)
                free((char *)call->u.open.pathname);
+       free(call->backtrace);
        tlist_del_from(&history, call, list);
        free(call);
 }
@@ -392,6 +456,8 @@ static void free_everything(void)
 
        while ((i = tlist_top(&history, struct failtest_call, list)) != NULL)
                free_call(i);
+
+       failtable_clear(&failtable);
 }
 
 static NORETURN void failtest_cleanup(bool forced_cleanup, int status)
@@ -433,6 +499,7 @@ static bool should_fail(struct failtest_call *call)
        char *out = NULL;
        size_t outlen = 0;
        struct saved_file *files;
+       struct failtest_call *dup;
 
        if (call == &unrecorded_call)
                return false;
@@ -478,6 +545,11 @@ static bool should_fail(struct failtest_call *call)
        if (probing)
                return call->fail = false;
 
+       /* Don't more than once in the same place. */
+       dup = failtable_get(&failtable, call);
+       if (dup)
+               return call->fail = false;
+
        if (failtest_hook) {
                switch (failtest_hook(&history)) {
                case FAIL_OK:
@@ -493,6 +565,9 @@ static bool should_fail(struct failtest_call *call)
                }
        }
 
+       /* Add it to our table of calls. */
+       failtable_add(&failtable, call);
+
        files = save_files();
 
        /* We're going to fail in the child. */
@@ -1123,6 +1198,7 @@ void failtest_init(int argc, char *argv[])
                        debugpath = argv[i] + strlen("--debugpath=");
                }
        }
+       failtable_init(&failtable);
        start = time_now();
 }
 
index 294c28b970066fda3b0e22fb3bf7ffe03140ad72..f473f14f110f7b8095daafbe2f10a6a8ec7e2897 100644 (file)
@@ -137,6 +137,9 @@ struct failtest_call {
        int error;
        /* How do we clean this up? */
        void (*cleanup)(void *u);
+       /* Backtrace of call chain. */
+       void **backtrace;
+       unsigned int backtrace_num;
        /* The actual call data. */
        union {
                struct calloc_call calloc;
index 3c93c2ad988e3f1f12b5c590d53caac6b6eecdb9..84f473c740d4be22a4a923b73e6f06ef7ae29cb7 100644 (file)
@@ -11,6 +11,7 @@ int main(void)
        void *p;
 
        plan_tests(14);
+       failtest_init(0, NULL);
 
        failpath = "mceopwrMCEOPWR";
 
index f29f213d061ee62a9647919c5d277f9abf716d10..13fe0b97b3778d9ccc24df11b739b4f2373cab52 100644 (file)
@@ -95,6 +95,7 @@ int main(void)
        unsigned int isize;
 
        plan_tests(5835);
+       failtest_init(0, NULL);
        failtest_hook = dont_fail;
 
        fd = open("run-locking-scratch", O_RDWR|O_CREAT, 0600);
index 3912bfd060794300c8456a5ca7a3897637ddee25..e00ec700bb4da56491ae18ff409658c6811fd171 100644 (file)
@@ -95,6 +95,7 @@ int main(void)
        int status;
 
        plan_tests(3);
+       failtest_init(0, NULL);
 
        status = setjmp(exited);
        if (status == 0) {
index e055045739d619db301f3cf98cddcb24b0def734..b2a5aab145b5ac814b1ed962ee763c61a478954f 100644 (file)
@@ -14,6 +14,7 @@ int main(void)
        struct stat st;
 
        plan_tests(12);
+       failtest_init(0, NULL);
 
        if (pipe(pfd))
                abort();