1 Patch to improve MIPS call stack unwind performance by caching the results
3 by Dan Howell <dahowell@directv.com>
5 diff -urN mpatrol-uclibc/src/stack.c mpatrol-unwindcache/src/stack.c
6 --- mpatrol-uclibc/src/stack.c 2006-06-22 15:39:04.000000000 -0700
7 +++ mpatrol-unwindcache/src/stack.c 2006-06-22 15:42:20.000000000 -0700
9 #define ucontext asm_ucontext
10 #include <asm/ucontext.h>
18 #if !MP_BUILTINSTACK_SUPPORT && !MP_LIBRARYSTACK_SUPPORT
19 #if TARGET == TARGET_UNIX && ARCH == ARCH_MIPS
20 +/* Set up a tree to cache the results of code searching to determine the
21 + location of the return address for each code point encountered. */
23 +/* An unwind node belongs to a binary search tree of nodes, ordered by
24 + * code address, and contains call stack unwinding details for a given
25 + * code address. An internal index node stores details of a single memory
26 + * block allocated for unwind node slots.
28 +typedef union unwindnode
32 + treenode node; /* internal tree node */
33 + void *block; /* pointer to block of memory */
34 + size_t size; /* size of block of memory */
39 + treenode node; /* tree node */
40 + long p; /* return address offset in the stack */
41 + long m; /* frame pointer offset in stack */
42 + long s; /* stack pointer offset from previous frame */
43 + unsigned long a; /* flags */
49 +/* An unwindhead holds the table of address node slots as well as the
50 + * internal list of memory blocks allocated for address node slots.
52 +typedef struct unwindhead
54 + heaphead heap; /* pointer to heap */
55 + slottable table; /* table of address nodes */
56 + treeroot itree; /* internal list of memory blocks */
57 + treeroot dtree; /* tree for sorting */
58 + size_t size; /* memory used by internal blocks */
59 + char init; /* initialization flag */
63 +static unwindhead unwindcache;
65 +/* Initialise the fields of an unwindhead so that there are no allocated,
66 + * freed or free blocks.
73 + struct { char x; unwindnode y; } z;
76 + __mp_newheap(&unwindcache.heap);
77 + /* Determine the minimum alignment for an unwind node on this
78 + * system and force the alignment to be a power of two. This
79 + * information is used when initialising the slot table.
81 + n = (char *) &z.y - &z.x;
82 + __mp_newslots(&unwindcache.table, sizeof(unwindnode), __mp_poweroftwo(n));
83 + __mp_newtree(&unwindcache.itree);
84 + __mp_newtree(&unwindcache.dtree);
85 + unwindcache.size = 0;
86 + unwindcache.init = 1;
90 +/* Forget all unwind information.
95 +deleteunwindcache(void)
97 + /* We don't need to explicitly free any memory as this is dealt with
98 + * at a lower level by the heap manager.
100 + __mp_deleteheap(&unwindcache.heap);
101 + unwindcache.table.free = NULL;
102 + unwindcache.table.size = 0;
103 + __mp_newtree(&unwindcache.itree);
104 + __mp_newtree(&unwindcache.dtree);
105 + unwindcache.size = 0;
106 + unwindcache.init = 0;
110 +/* Allocate a new unwind node.
120 + /* If we have no more allocation node slots left then we must allocate
121 + * some more memory for them. An extra MP_ALLOCFACTOR pages of memory
124 + if ((n = (unwindnode *) __mp_getslot(&unwindcache.table)) == NULL)
126 + if ((p = __mp_heapalloc(&unwindcache.heap, unwindcache.heap.memory.page * MP_ALLOCFACTOR,
127 + unwindcache.table.entalign, 1)) == NULL)
129 + __mp_initslots(&unwindcache.table, p->block, p->size);
130 + n = (unwindnode *) __mp_getslot(&unwindcache.table);
131 + __mp_treeinsert(&unwindcache.itree, &n->index.node, (unsigned long) p->block);
132 + n->index.block = p->block;
133 + n->index.size = p->size;
134 + unwindcache.size += p->size;
135 + n = (unwindnode *) __mp_getslot(&unwindcache.table);
140 +/* Search for the unwind node associated with a given address.
144 +findunwindnode(unsigned long p)
146 + return (unwindnode *) __mp_search(unwindcache.dtree.root, p);
150 /* Determine the stack pointer and return address of the previous stack frame
151 * by performing code reading.
157 - unsigned long a, i, q, t, b, r;
158 + unsigned long a, i, q, t, b, r, k;
160 + unwindnode *n = NULL;
166 /* Save initial code-reading starting point.
170 + /* Create the cache if not yet created.
172 + if (!unwindcache.init)
175 + __mp_atexit(deleteunwindcache);
177 + if ((n = findunwindnode(f->ra)) != NULL)
179 + /* We've been here before, so get the cached information.
186 /* Search for the return address offset in the stack frame.
188 while (!((a & RA_OFFSET) && (a & SP_OFFSET)) && (f->ra < q))
195 + if ((n = getunwindnode()) != NULL)
197 + /* Cache the information we just got in the tree.
203 + __mp_treeinsert(&unwindcache.dtree, &n->data.node, k);