summaryrefslogtreecommitdiff
path: root/stackstash.c
blob: 8c06f7b4048da7f26c77ce0e6ed4e1c5b5b6e3ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/* Sysprof -- Sampling, systemwide CPU profiler
 * Copyright 2004, Red Hat, Inc.
 * Copyright 2004, 2005, Soeren Sandmann
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "stackstash.h"

struct StackStash
{
    int			ref_count;
    StackNode *		root;
    GHashTable *	nodes_by_data;
    GDestroyNotify	destroy;

    StackNode *		cached_nodes;
    GPtrArray *		blocks;
};

static void
decorate_node (StackNode *node,
	       StackStash *stash)
{
    StackNode *n;

    if (!node)
	return;

    decorate_node (node->siblings, stash);
    decorate_node (node->children, stash);

    node->next = g_hash_table_lookup (stash->nodes_by_data, &node->data);
    g_hash_table_insert (stash->nodes_by_data, &node->data, node);
	
    /* FIXME: This could be done more efficiently
     * by keeping track of the ancestors we have seen.
     */
    node->toplevel = TRUE;
    for (n = node->parent; n != NULL; n = n->parent)
    {
	if (n->data == node->data)
	{
	    node->toplevel = FALSE;
	    break;
	}
    }
}

static unsigned int
address_hash (gconstpointer key)
{
    const uint64_t *addr = key;

    return *addr;
}

static gboolean
address_equal (gconstpointer key1, gconstpointer key2)
{
    const uint64_t *addr1 = key1;
    const uint64_t *addr2 = key2;

    return *addr1 == *addr2;
}

static void
stack_stash_decorate (StackStash *stash)
{
    if (stash->nodes_by_data)
	return;

    stash->nodes_by_data = g_hash_table_new (address_hash, address_equal);

    decorate_node (stash->root, stash);
}

static void
free_key (gpointer key,
	  gpointer value,
	  gpointer data)
{
    GDestroyNotify destroy = data;
    uint64_t u64 = *(uint64_t *)key;

    destroy (U64_TO_POINTER (u64));
}

static void
stack_stash_undecorate (StackStash *stash)
{
    if (stash->nodes_by_data)
    {
	if (stash->destroy)
	{
	    g_hash_table_foreach (
		stash->nodes_by_data, free_key, stash->destroy);
	}
	
	g_hash_table_destroy (stash->nodes_by_data);
	stash->nodes_by_data = NULL;
    }
}

static GHashTable *
get_nodes_by_data (StackStash *stash)
{
    if (!stash->nodes_by_data)
	stack_stash_decorate (stash);

    return stash->nodes_by_data;
}

StackNode *
stack_node_new (StackStash *stash)
{
    StackNode *node;

    if (!stash->cached_nodes)
    {
#define BLOCK_SIZE 32768
#define N_NODES (BLOCK_SIZE / sizeof (StackNode))

	StackNode *block = g_malloc (BLOCK_SIZE);
	int i;

	for (i = 0; i < N_NODES; ++i)
	{
	    block[i].next = stash->cached_nodes;
	    stash->cached_nodes = &(block[i]);
	}

	g_ptr_array_add (stash->blocks, block);
    }

    node = stash->cached_nodes;
    stash->cached_nodes = node->next;

    node->siblings = NULL;
    node->children = NULL;
    node->data = 0;
    node->parent = NULL;
    node->size = 0;
    node->next = NULL;
    node->total = 0;
    
    return node;
}

/* "destroy", if non-NULL, is called once on every address */
static StackStash *
create_stack_stash (GDestroyNotify destroy)
{
    StackStash *stash = g_new (StackStash, 1);

    stash->root = NULL;
    stash->nodes_by_data = NULL;
    stash->ref_count = 1;
    stash->destroy = destroy;
    
    stash->cached_nodes = NULL;
    stash->blocks = g_ptr_array_new ();

    return stash;
}

/* Stach */
StackStash *
stack_stash_new (GDestroyNotify destroy)
{
    return create_stack_stash (destroy);
}


static void
stack_stash_free (StackStash *stash)
{
    int i;
    
    stack_stash_undecorate (stash);

    for (i = 0; i < stash->blocks->len; ++i)
	g_free (stash->blocks->pdata[i]);
    
    g_ptr_array_free (stash->blocks, TRUE);
    
    g_free (stash);
}

StackNode *
stack_stash_add_trace (StackStash *stash,
		       uint64_t   *addrs,
		       int         n_addrs,
		       int         size)
{
    StackNode **location = &(stash->root);
    StackNode *parent = NULL;
    int i;

    if (!n_addrs)
	return NULL;

    if (stash->nodes_by_data)
	stack_stash_undecorate (stash);
    
    for (i = n_addrs - 1; i >= 0; --i)
    {
	StackNode *match = NULL;
	StackNode *prev;

	prev = NULL;
	for (match = *location; match; prev = match, match = match->siblings)
	{
	    if (match->data == addrs[i])
	    {
		if (prev)
		{
		    /* move to front */
		    prev->siblings = match->siblings;
		    match->siblings = *location;
		    *location = match;
		}
		
		break;
	    }
	}

	if (!match)
	{
	    match = stack_node_new (stash);
	    match->data = addrs[i];
	    match->siblings = *location;
	    match->parent = parent;
	    *location = match;
	}

	match->total += size;

	location = &(match->children);
	parent = match;
    }

    parent->size += size;

    return parent;
}

static void
do_callback (StackNode *node,
	     StackLink *trace,
	     StackFunction func,
	     gpointer data)
{
    StackLink link;

    if (trace)
	trace->prev = &link;
	
    link.next = trace;
    link.prev = NULL;
    
    while (node)
    {
	link.data = node->data;
	
	if (node->size)
	    func (&link, node->size, data);
	
	do_callback (node->children, &link, func, data);
	
	node = node->siblings;
    }

    if (trace)
	trace->prev = NULL;
}

void
stack_stash_foreach (StackStash      *stash,
		     StackFunction    stack_func,
		     gpointer         data)
{
    do_callback (stash->root, NULL, stack_func, data);
}

void
stack_node_foreach_trace (StackNode     *node,
			  StackFunction  func,
			  gpointer       data)
{
    StackLink link;

    link.next = NULL;
    link.data = node->data;
    link.prev = NULL;

    if (node->size)
	func (&link, node->size, data);
    
    do_callback (node->children, &link, func, data);
}

void
stack_stash_unref (StackStash *stash)
{
    stash->ref_count--;
    if (stash->ref_count == 0)
	stack_stash_free (stash);
}

StackStash *
stack_stash_ref (StackStash *stash)
{
    stash->ref_count++;
    return stash;
}

StackNode *
stack_stash_find_node (StackStash      *stash,
		       gpointer         data)
{
    uint64_t u64 = POINTER_TO_U64 (data);
    
    g_return_val_if_fail (stash != NULL, NULL);
    
    return g_hash_table_lookup (get_nodes_by_data (stash), &u64);
}

typedef struct
{
    StackNodeFunc func;
    gpointer	  data;
} Info;

static void
do_foreach (gpointer key, gpointer value, gpointer data)
{
    Info *info = data;

    info->func (value, info->data);
}

void
stack_stash_foreach_by_address (StackStash *stash,
				StackNodeFunc func,
				gpointer      data)
{
    Info info;
    info.func = func;
    info.data = data;
	
    g_hash_table_foreach (get_nodes_by_data (stash), do_foreach, &info);
}

StackNode  *
stack_stash_get_root (StackStash *stash)
{
    return stash->root;
}

void
stack_stash_set_root (StackStash     *stash,
		      StackNode      *root)
{
    g_return_if_fail (stash->root == NULL);
    
    stash->root = root;
}