ccan: update for better tal debugging checks.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2017-09-04 17:04:55 +09:30 committed by Christian Decker
parent 1047e891b7
commit c92dd56dd5
4 changed files with 55 additions and 40 deletions

View file

@ -1,3 +1,3 @@
CCAN imported from http://ccodearchive.net. CCAN imported from http://ccodearchive.net.
CCAN version: init-2388-ge6abb93d CCAN version: init-2393-g9728f1d9

View file

@ -10,6 +10,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <inttypes.h>
struct node { struct node {
void *n; void *n;
@ -185,7 +186,7 @@ static void do_tals(struct node *node)
char, node->len); char, node->len);
else else
node->n = tal_alloc_(node->parent ? node->parent->n : NULL, node->n = tal_alloc_(node->parent ? node->parent->n : NULL,
node->len, false, TAL_LABEL(type, "")); node->len, false, false, TAL_LABEL(type, ""));
if (node->destructor) if (node->destructor)
tal_add_destructor(node->n, unused_tal_destructor); tal_add_destructor(node->n, unused_tal_destructor);
@ -253,7 +254,8 @@ static void dump_vsize(void)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
struct timespec start, alloc_time, free_time; struct timeabs start;
struct timerel alloc_time, free_time;
struct node *root; struct node *root;
unsigned int i; unsigned int i;
FILE *f; FILE *f;
@ -288,83 +290,91 @@ int main(int argc, char *argv[])
if (!run_malloc) if (!run_malloc)
goto after_malloc; goto after_malloc;
alloc_time.tv_sec = alloc_time.tv_nsec = 0; alloc_time.ts.tv_sec = alloc_time.ts.tv_nsec = 0;
free_time.tv_sec = free_time.tv_nsec = 0; free_time.ts.tv_sec = free_time.ts.tv_nsec = 0;
for (i = 0; i < LOOPS; i++) { for (i = 0; i < LOOPS; i++) {
start = time_now(); start = time_now();
do_mallocs(root); do_mallocs(root);
alloc_time = time_add(alloc_time, time_sub(time_now(), start)); alloc_time = timerel_add(alloc_time,
time_between(time_now(), start));
start = time_now(); start = time_now();
free_mallocs(root); free_mallocs(root);
free_time = time_add(free_time, time_sub(time_now(), start)); free_time = timerel_add(free_time,
time_between(time_now(), start));
} }
alloc_time = time_divide(alloc_time, i); alloc_time = time_divide(alloc_time, i);
free_time = time_divide(free_time, i); free_time = time_divide(free_time, i);
printf("Malloc time: %lluns\n", time_to_nsec(alloc_time)); printf("Malloc time: %"PRIu64"ns\n", time_to_nsec(alloc_time));
printf("Free time: %lluns\n", time_to_nsec(free_time)); printf("Free time: %"PRIu64"ns\n", time_to_nsec(free_time));
after_malloc: after_malloc:
if (!run_talloc) if (!run_talloc)
goto after_talloc; goto after_talloc;
alloc_time.tv_sec = alloc_time.tv_nsec = 0; alloc_time.ts.tv_sec = alloc_time.ts.tv_nsec = 0;
free_time.tv_sec = free_time.tv_nsec = 0; free_time.ts.tv_sec = free_time.ts.tv_nsec = 0;
for (i = 0; i < LOOPS; i++) { for (i = 0; i < LOOPS; i++) {
start = time_now(); start = time_now();
do_tallocs(root); do_tallocs(root);
alloc_time = time_add(alloc_time, time_sub(time_now(), start)); alloc_time = timerel_add(alloc_time,
time_between(time_now(), start));
start = time_now(); start = time_now();
free_tallocs(root); free_tallocs(root);
free_time = time_add(free_time, time_sub(time_now(), start)); free_time = timerel_add(free_time,
time_between(time_now(), start));
} }
alloc_time = time_divide(alloc_time, i); alloc_time = time_divide(alloc_time, i);
free_time = time_divide(free_time, i); free_time = time_divide(free_time, i);
printf("Talloc time: %lluns\n", time_to_nsec(alloc_time)); printf("Talloc time: %"PRIu64"ns\n", time_to_nsec(alloc_time));
printf("talloc_free time: %lluns\n", time_to_nsec(free_time)); printf("talloc_free time: %"PRIu64"ns\n", time_to_nsec(free_time));
free_time.tv_sec = free_time.tv_nsec = 0; free_time.ts.tv_sec = free_time.ts.tv_nsec = 0;
for (i = 0; i < LOOPS; i++) { for (i = 0; i < LOOPS; i++) {
do_tallocs(root); do_tallocs(root);
start = time_now(); start = time_now();
talloc_free(root->n); talloc_free(root->n);
free_time = time_add(free_time, time_sub(time_now(), start)); free_time = timerel_add(free_time,
time_between(time_now(), start));
} }
free_time = time_divide(free_time, i); free_time = time_divide(free_time, i);
printf("Single talloc_free time: %lluns\n", time_to_nsec(free_time)); printf("Single talloc_free time: %"PRIu64"\n", time_to_nsec(free_time));
after_talloc: after_talloc:
if (!run_tal) if (!run_tal)
goto after_tal; goto after_tal;
alloc_time.tv_sec = alloc_time.tv_nsec = 0; alloc_time.ts.tv_sec = alloc_time.ts.tv_nsec = 0;
free_time.tv_sec = free_time.tv_nsec = 0; free_time.ts.tv_sec = free_time.ts.tv_nsec = 0;
for (i = 0; i < LOOPS; i++) { for (i = 0; i < LOOPS; i++) {
start = time_now(); start = time_now();
do_tals(root); do_tals(root);
alloc_time = time_add(alloc_time, time_sub(time_now(), start)); alloc_time = timerel_add(alloc_time,
time_between(time_now(), start));
start = time_now(); start = time_now();
free_tals(root); free_tals(root);
free_time = time_add(free_time, time_sub(time_now(), start)); free_time = timerel_add(free_time,
time_between(time_now(), start));
} }
alloc_time = time_divide(alloc_time, i); alloc_time = time_divide(alloc_time, i);
free_time = time_divide(free_time, i); free_time = time_divide(free_time, i);
printf("Tal time: %lluns\n", time_to_nsec(alloc_time)); printf("Tal time: %"PRIu64"ns\n", time_to_nsec(alloc_time));
printf("Tal_free time: %lluns\n", time_to_nsec(free_time)); printf("Tal_free time: %"PRIu64"ns\n", time_to_nsec(free_time));
free_time.tv_sec = free_time.tv_nsec = 0; free_time.ts.tv_sec = free_time.ts.tv_nsec = 0;
for (i = 0; i < LOOPS; i++) { for (i = 0; i < LOOPS; i++) {
do_tals(root); do_tals(root);
start = time_now(); start = time_now();
tal_free(root->n); tal_free(root->n);
free_time = time_add(free_time, time_sub(time_now(), start)); free_time = timerel_add(free_time,
time_between(time_now(), start));
} }
free_time = time_divide(free_time, i); free_time = time_divide(free_time, i);
printf("Single tal_free time: %lluns\n", time_to_nsec(free_time)); printf("Single tal_free time: %"PRIu64"ns\n", time_to_nsec(free_time));
after_tal: after_tal:
return 0; return 0;

View file

@ -36,7 +36,7 @@ int main(int argc, char *argv[])
void *ctx; void *ctx;
unsigned count; unsigned count;
int i, j; int i, j;
struct timespec tv; struct timeabs tv;
void *p1, *p2[100], *p3[100]; void *p1, *p2[100], *p3[100];
bool run_talloc = true, run_tal = true, run_malloc = true; bool run_talloc = true, run_tal = true, run_malloc = true;
@ -67,7 +67,7 @@ int main(int argc, char *argv[])
talloc_free(p1); talloc_free(p1);
} }
count += (1 + 200) * LOOPS; count += (1 + 200) * LOOPS;
} while (time_sub(time_now(), tv).tv_sec < 5); } while (time_between(time_now(), tv).ts.tv_sec < 5);
fprintf(stderr, "talloc: %.0f ops/sec\n", count/5.0); fprintf(stderr, "talloc: %.0f ops/sec\n", count/5.0);
@ -90,7 +90,7 @@ after_talloc:
tal_free(p1); tal_free(p1);
} }
count += (1 + 200) * LOOPS; count += (1 + 200) * LOOPS;
} while (time_sub(time_now(), tv).tv_sec < 5); } while (time_between(time_now(), tv).ts.tv_sec < 5);
fprintf(stderr, "tal: %.0f ops/sec\n", count/5.0); fprintf(stderr, "tal: %.0f ops/sec\n", count/5.0);
tal_free(ctx); tal_free(ctx);
@ -115,7 +115,7 @@ after_tal:
free(p1); free(p1);
} }
count += (1 + 200) * LOOPS; count += (1 + 200) * LOOPS;
} while (time_sub(time_now(), tv).tv_sec < 5); } while (time_between(time_now(), tv).ts.tv_sec < 5);
fprintf(stderr, "malloc: %.0f ops/sec\n", count/5.0); fprintf(stderr, "malloc: %.0f ops/sec\n", count/5.0);
after_malloc: after_malloc:

View file

@ -8,6 +8,7 @@
#include <stddef.h> #include <stddef.h>
#include <string.h> #include <string.h>
#include <limits.h> #include <limits.h>
#include <stdint.h>
#include <errno.h> #include <errno.h>
//#define TAL_DEBUG 1 //#define TAL_DEBUG 1
@ -15,6 +16,9 @@
#define NOTIFY_IS_DESTRUCTOR 512 #define NOTIFY_IS_DESTRUCTOR 512
#define NOTIFY_EXTRA_ARG 1024 #define NOTIFY_EXTRA_ARG 1024
/* This makes our parent_child ptr stand out for to_tal_hdr checks */
#define TAL_PTR_OBFUSTICATOR ((intptr_t)0x1984200820142016ULL)
/* 32-bit type field, first byte 0 in either endianness. */ /* 32-bit type field, first byte 0 in either endianness. */
enum prop_type { enum prop_type {
CHILDREN = 0x00c1d500, CHILDREN = 0x00c1d500,
@ -26,7 +30,8 @@ enum prop_type {
struct tal_hdr { struct tal_hdr {
struct list_node list; struct list_node list;
struct prop_hdr *prop; struct prop_hdr *prop;
struct children *parent_child; /* XOR with TAL_PTR_OBFUSTICATOR */
intptr_t parent_child;
}; };
struct prop_hdr { struct prop_hdr {
@ -72,7 +77,7 @@ static struct {
struct tal_hdr hdr; struct tal_hdr hdr;
struct children c; struct children c;
} null_parent = { { { &null_parent.hdr.list, &null_parent.hdr.list }, } null_parent = { { { &null_parent.hdr.list, &null_parent.hdr.list },
&null_parent.c.hdr, NULL }, &null_parent.c.hdr, TAL_PTR_OBFUSTICATOR },
{ { CHILDREN, NULL }, { { CHILDREN, NULL },
&null_parent.hdr, &null_parent.hdr,
{ { &null_parent.c.children.n, { { &null_parent.c.children.n,
@ -93,19 +98,19 @@ static inline void COLD call_error(const char *msg)
errorfn(msg); errorfn(msg);
} }
static bool get_destroying_bit(struct children *parent_child) static bool get_destroying_bit(intptr_t parent_child)
{ {
return (size_t)parent_child & 1; return parent_child & 1;
} }
static void set_destroying_bit(struct children **parent_child) static void set_destroying_bit(intptr_t *parent_child)
{ {
*parent_child = (void *)((size_t)*parent_child | 1); *parent_child |= 1;
} }
static struct children *ignore_destroying_bit(struct children *parent_child) static struct children *ignore_destroying_bit(intptr_t parent_child)
{ {
return (void *)((size_t)parent_child & ~(size_t)1); return (void *)((parent_child ^ TAL_PTR_OBFUSTICATOR) & ~(intptr_t)1);
} }
/* This means valgrind can see leaks. */ /* This means valgrind can see leaks. */
@ -377,7 +382,7 @@ static bool add_child(struct tal_hdr *parent, struct tal_hdr *child)
return false; return false;
} }
list_add(&children->children, &child->list); list_add(&children->children, &child->list);
child->parent_child = children; child->parent_child = (intptr_t)children ^ TAL_PTR_OBFUSTICATOR;
return true; return true;
} }