summaryrefslogtreecommitdiff
path: root/ccan/tal/benchmark/samba-allocs.c
blob: e25c7e9b69163996c96ad22081307ad24b729355 (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
/* Grab dump of Samba4 talloc tree to do benchmarks on it. */
#include <ccan/talloc/talloc.h>
#include <ccan/tal/tal.h>
#include <ccan/time/time.h>
#include <ccan/err/err.h>
#include <ccan/str/str.h>
#include <string.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

struct node {
	void *n;
	struct node *parent;
	char *name;
	bool destructor;
	size_t len;
	unsigned int num_children;
	struct node *children[0];
};

static int node_count;

static struct node *new_node(void)
{
	node_count++;
	return calloc(sizeof(struct node), 1);
}

/* struct db_context              contains    282 bytes in   5 blocks (ref 0) d=(nil) 0x1f64e70 */
static struct node *parse(const char *line)
{
	struct node *n = new_node();
	const char *p;

	p = strstr(line, " contains ");
	p += strlen(" contains ");
	p += strspn(line, " ");
	n->len = strtol(p, NULL, 0);
	p = strstr(p, "d=");
	if (p[2] != '(')
		n->destructor = true;
	return n;
}

static void add_child(struct node *parent, struct node *child)
{
	unsigned int i;
	struct node *oldp = parent;

	parent = realloc(parent, sizeof(*parent)
			 + sizeof(parent->children[0]) * (parent->num_children+1));
	parent->children[parent->num_children++] = child;
	child->parent = parent;

	if (parent == oldp)
		return;

	/* Fix up children's parent pointers. */
	for (i = 0; i < parent->num_children-1; i++) {
		assert(parent->children[i]->parent == oldp);
		parent->children[i]->parent = parent;
	}

	/* Fix up parent's child pointer. */
	if (parent->parent) {
		assert(parent->parent->children[parent->parent->num_children-1]
		       == oldp);
		parent->parent->children[parent->parent->num_children-1]
			= parent;
	}
}

/* Random string of required length */
static char *namelen(int len)
{
	char *p = malloc(len);
	memset(p, 'x', len-1);
	p[len-1] = '\0';
	return p;
}

static struct node *read_nodes(FILE *f)
{
	char line[4096];
	unsigned int curr_indent = 0, indent;
	struct node *n, *curr = new_node();

	/* Ignore first line */
	fgets(line, 4096, f);

	while (fgets(line, 4096, f)) {
		bool is_name;

		indent = strspn(line, " ");

		/* Ignore references for now. */
		if (strstarts(line + indent, "reference to: "))
			continue;

		/* Blank name?  Use offset of 'contains' to guess indent! */
		if (strstarts(line + indent, "contains "))
			indent -= 31;

		is_name = strstarts(line + indent, ".name ");

		n = parse(line + indent);
		if (is_name) {
			curr->name = namelen(n->len);
			free(n);
		} else {
			if (indent > curr_indent) {
				assert(indent == curr_indent + 4);
				curr_indent += 4;
			} else {
				/* Go back up to parent. */
				for (curr_indent += 4;
				     curr_indent != indent;
				     curr_indent -= 4)
					curr = curr->parent;
			}
			add_child(curr, n);
			curr = n;
		}
	}
	while (curr->parent) {
		curr = curr->parent;
		curr_indent -= 4;
	}
	assert(curr_indent == 0);
	return curr;
}

static int unused_talloc_destructor(void *p)
{
	return 0;
}

static void do_tallocs(struct node *node)
{
	unsigned int i;
	static int count;

	if (count++ % 16 == 0)
		node->n = talloc_array(node->parent ? node->parent->n : NULL,
				       char, node->len);
	else
		node->n = talloc_size(node->parent ? node->parent->n : NULL,
				      node->len);
	if (node->destructor)
		talloc_set_destructor(node->n, unused_talloc_destructor);
	if (node->name)
		talloc_set_name(node->n, "%s", node->name);

	for (i = 0; i < node->num_children; i++)
		do_tallocs(node->children[i]);
}

static void free_tallocs(struct node *node)
{
	unsigned int i;

	for (i = 0; i < node->num_children; i++)
		free_tallocs(node->children[i]);

	talloc_free(node->n);
}

static void unused_tal_destructor(void *p)
{
}

static void do_tals(struct node *node)
{
	unsigned int i;
	static int count;

	/* Tal pays a penalty for arrays, but we can't tell which is an array
	 * and which isn't.  Grepping samba source gives 1221 talloc_array of
	 * 33137 talloc occurrences, so conservatively assume 1 in 16 */
	if (count++ % 16 == 0)
		node->n = tal_arr(node->parent ? node->parent->n : NULL,
				  char, node->len);
	else
		node->n = tal_alloc_(node->parent ? node->parent->n : NULL,
				     node->len, false, TAL_LABEL(type, ""));

	if (node->destructor)
		tal_add_destructor(node->n, unused_tal_destructor);
	if (node->name)
		tal_set_name(node->n, node->name);

	for (i = 0; i < node->num_children; i++)
		do_tals(node->children[i]);
}

static void free_tals(struct node *node)
{
	unsigned int i;

	for (i = 0; i < node->num_children; i++)
		free_tals(node->children[i]);

	tal_free(node->n);
}

static void do_mallocs(struct node *node)
{
	unsigned int i;

	node->n = malloc(node->len + (node->name ? strlen(node->name) + 1 : 1));

	for (i = 0; i < node->num_children; i++)
		do_mallocs(node->children[i]);
}

static void free_mallocs(struct node *node)
{
	unsigned int i;

	for (i = 0; i < node->num_children; i++)
		free_mallocs(node->children[i]);

	free(node->n);
}

/* See proc(5): field 23 is vsize, 24 is rss (in pages) */
static void dump_vsize(void)
{
	int fd, i;
	char buf[1000], *p = buf;

	sprintf(buf, "/proc/%u/stat", getpid());
	fd = open(buf, O_RDONLY);
	read(fd, buf, sizeof(buf));
	close(fd);

	for (i = 0; i < 22; i++) {
		p += strcspn(p, " ");
		p += strspn(p, " ");
	}
	i = atoi(p);
	printf("Virtual size = %i, ", i);
	p += strcspn(p, " ");
	p += strspn(p, " ");
	i = atoi(p);
	printf("RSS = %i\n", i * getpagesize());
}

#define LOOPS 1000

int main(int argc, char *argv[])
{
	struct timespec start, alloc_time, free_time;
	struct node *root;
	unsigned int i;
	FILE *f;
	bool run_talloc = true, run_tal = true, run_malloc = true;

	f = argv[1] ? fopen(argv[1], "r") : stdin;
	root = read_nodes(f);
	fclose(f);
	printf("Read %u nodes\n", node_count);

	if (argc > 2) {
		if (streq(argv[2], "--talloc-size")) {
			do_tallocs(root);
			dump_vsize();
			exit(0);
		}
		if (streq(argv[2], "--tal-size")) {
			do_tals(root);
			dump_vsize();
			exit(0);
		}
		if (strcmp(argv[2], "--talloc") == 0)
			run_tal = run_malloc = false;
		else if (strcmp(argv[2], "--tal") == 0)
			run_talloc = run_malloc = false;
		else if (strcmp(argv[2], "--malloc") == 0)
			run_talloc = run_tal = false;
		else
			errx(1, "Bad flag %s", argv[2]);
	}

	if (!run_malloc)
		goto after_malloc;

	alloc_time.tv_sec = alloc_time.tv_nsec = 0;
	free_time.tv_sec = free_time.tv_nsec = 0;
	for (i = 0; i < LOOPS; i++) {
		start = time_now();
		do_mallocs(root);
		alloc_time = time_add(alloc_time, time_sub(time_now(), start));

		start = time_now();
		free_mallocs(root);
		free_time = time_add(free_time, time_sub(time_now(), start));
	}
	alloc_time = time_divide(alloc_time, i);
	free_time = time_divide(free_time, i);
	printf("Malloc time:             %lluns\n", time_to_nsec(alloc_time));
	printf("Free time:               %lluns\n", time_to_nsec(free_time));

after_malloc:
	if (!run_talloc)
		goto after_talloc;

	alloc_time.tv_sec = alloc_time.tv_nsec = 0;
	free_time.tv_sec = free_time.tv_nsec = 0;
	for (i = 0; i < LOOPS; i++) {
		start = time_now();
		do_tallocs(root);
		alloc_time = time_add(alloc_time, time_sub(time_now(), start));

		start = time_now();
		free_tallocs(root);
		free_time = time_add(free_time, time_sub(time_now(), start));
	}
	alloc_time = time_divide(alloc_time, i);
	free_time = time_divide(free_time, i);
	printf("Talloc time:             %lluns\n", time_to_nsec(alloc_time));
	printf("talloc_free time:        %lluns\n", time_to_nsec(free_time));

	free_time.tv_sec = free_time.tv_nsec = 0;
	for (i = 0; i < LOOPS; i++) {
		do_tallocs(root);

		start = time_now();
		talloc_free(root->n);
		free_time = time_add(free_time, time_sub(time_now(), start));
	}
	free_time = time_divide(free_time, i);
	printf("Single talloc_free time: %lluns\n", time_to_nsec(free_time));

after_talloc:
	if (!run_tal)
		goto after_tal;

	alloc_time.tv_sec = alloc_time.tv_nsec = 0;
	free_time.tv_sec = free_time.tv_nsec = 0;
	for (i = 0; i < LOOPS; i++) {
		start = time_now();
		do_tals(root);
		alloc_time = time_add(alloc_time, time_sub(time_now(), start));

		start = time_now();
		free_tals(root);
		free_time = time_add(free_time, time_sub(time_now(), start));
	}
	alloc_time = time_divide(alloc_time, i);
	free_time = time_divide(free_time, i);
	printf("Tal time:                %lluns\n", time_to_nsec(alloc_time));
	printf("Tal_free time:           %lluns\n", time_to_nsec(free_time));

	free_time.tv_sec = free_time.tv_nsec = 0;
	for (i = 0; i < LOOPS; i++) {
		do_tals(root);

		start = time_now();
		tal_free(root->n);
		free_time = time_add(free_time, time_sub(time_now(), start));
	}
	free_time = time_divide(free_time, i);
	printf("Single tal_free time:    %lluns\n", time_to_nsec(free_time));
after_tal:

	return 0;
}