]> git.ozlabs.org Git - ccan/blob - ccan/tal/benchmark/samba-allocs.c
tal: add benchmark based on Samba4's talloc usage.
[ccan] / ccan / tal / benchmark / samba-allocs.c
1 /* Grab dump of Samba4 talloc tree to do benchmarks on it. */
2 #include <ccan/talloc/talloc.h>
3 #include <ccan/tal/tal.h>
4 #include <ccan/time/time.h>
5 #include <ccan/err/err.h>
6 #include <ccan/str/str.h>
7 #include <string.h>
8 #include <assert.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <unistd.h>
12 #include <fcntl.h>
13
14 struct node {
15         void *n;
16         struct node *parent;
17         char *name;
18         bool destructor;
19         size_t len;
20         unsigned int num_children;
21         struct node *children[0];
22 };
23
24 static int node_count;
25
26 static struct node *new_node(void)
27 {
28         node_count++;
29         return calloc(sizeof(struct node), 1);
30 }
31
32 /* struct db_context              contains    282 bytes in   5 blocks (ref 0) d=(nil) 0x1f64e70 */
33 static struct node *parse(const char *line)
34 {
35         struct node *n = new_node();
36         const char *p;
37
38         p = strstr(line, " contains ");
39         p += strlen(" contains ");
40         p += strspn(line, " ");
41         n->len = strtol(p, NULL, 0);
42         p = strstr(p, "d=");
43         if (p[2] != '(')
44                 n->destructor = true;
45         return n;
46 }
47
48 static void add_child(struct node *parent, struct node *child)
49 {
50         unsigned int i;
51         struct node *oldp = parent;
52
53         parent = realloc(parent, sizeof(*parent)
54                          + sizeof(parent->children[0]) * (parent->num_children+1));
55         parent->children[parent->num_children++] = child;
56         child->parent = parent;
57
58         if (parent == oldp)
59                 return;
60
61         /* Fix up children's parent pointers. */
62         for (i = 0; i < parent->num_children-1; i++) {
63                 assert(parent->children[i]->parent == oldp);
64                 parent->children[i]->parent = parent;
65         }
66
67         /* Fix up parent's child pointer. */
68         if (parent->parent) {
69                 assert(parent->parent->children[parent->parent->num_children-1]
70                        == oldp);
71                 parent->parent->children[parent->parent->num_children-1]
72                         = parent;
73         }
74 }
75
76 /* Random string of required length */
77 static char *namelen(int len)
78 {
79         char *p = malloc(len);
80         memset(p, 'x', len-1);
81         p[len-1] = '\0';
82         return p;
83 }
84
85 static struct node *read_nodes(FILE *f)
86 {
87         char line[4096];
88         unsigned int curr_indent = 0, indent;
89         struct node *n, *curr = new_node();
90
91         /* Ignore first line */
92         fgets(line, 4096, f);
93
94         while (fgets(line, 4096, f)) {
95                 bool is_name;
96
97                 indent = strspn(line, " ");
98
99                 /* Ignore references for now. */
100                 if (strstarts(line + indent, "reference to: "))
101                         continue;
102
103                 /* Blank name?  Use offset of 'contains' to guess indent! */
104                 if (strstarts(line + indent, "contains "))
105                         indent -= 31;
106
107                 is_name = strstarts(line + indent, ".name ");
108
109                 n = parse(line + indent);
110                 if (is_name) {
111                         curr->name = namelen(n->len);
112                         free(n);
113                 } else {
114                         if (indent > curr_indent) {
115                                 assert(indent == curr_indent + 4);
116                                 curr_indent += 4;
117                         } else {
118                                 /* Go back up to parent. */
119                                 for (curr_indent += 4;
120                                      curr_indent != indent;
121                                      curr_indent -= 4)
122                                         curr = curr->parent;
123                         }
124                         add_child(curr, n);
125                         curr = n;
126                 }
127         }
128         while (curr->parent) {
129                 curr = curr->parent;
130                 curr_indent -= 4;
131         }
132         assert(curr_indent == 0);
133         return curr;
134 }
135
136 static int unused_talloc_destructor(void *p)
137 {
138         return 0;
139 }
140
141 static void do_tallocs(struct node *node)
142 {
143         unsigned int i;
144
145         node->n = talloc_size(node->parent ? node->parent->n : NULL, node->len);
146         if (node->destructor)
147                 talloc_set_destructor(node->n, unused_talloc_destructor);
148         if (node->name)
149                 talloc_set_name(node->n, "%s", node->name);
150
151         for (i = 0; i < node->num_children; i++)
152                 do_tallocs(node->children[i]);
153 }
154
155 static void free_tallocs(struct node *node)
156 {
157         unsigned int i;
158
159         for (i = 0; i < node->num_children; i++)
160                 free_tallocs(node->children[i]);
161
162         talloc_free(node->n);
163 }
164
165 static void unused_tal_destructor(void *p)
166 {
167 }
168
169 static void do_tals(struct node *node)
170 {
171         unsigned int i;
172
173         node->n = tal_arr(node->parent ? node->parent->n : NULL,
174                           char, node->len);
175         if (node->destructor)
176                 tal_add_destructor(node->n, unused_tal_destructor);
177         if (node->name)
178                 tal_set_name(node->n, node->name);
179
180         for (i = 0; i < node->num_children; i++)
181                 do_tals(node->children[i]);
182 }
183
184 static void free_tals(struct node *node)
185 {
186         unsigned int i;
187
188         for (i = 0; i < node->num_children; i++)
189                 free_tals(node->children[i]);
190
191         tal_free(node->n);
192 }
193
194 static void do_mallocs(struct node *node)
195 {
196         unsigned int i;
197
198         node->n = malloc(node->len + (node->name ? strlen(node->name) + 1 : 1));
199
200         for (i = 0; i < node->num_children; i++)
201                 do_mallocs(node->children[i]);
202 }
203
204 static void free_mallocs(struct node *node)
205 {
206         unsigned int i;
207
208         for (i = 0; i < node->num_children; i++)
209                 free_mallocs(node->children[i]);
210
211         free(node->n);
212 }
213
214 /* See proc(5): field 23 is vsize, 24 is rss (in pages) */
215 static void dump_vsize(void)
216 {
217         int fd, i;
218         char buf[1000], *p = buf;
219
220         sprintf(buf, "/proc/%u/stat", getpid());
221         fd = open(buf, O_RDONLY);
222         read(fd, buf, sizeof(buf));
223         close(fd);
224
225         for (i = 0; i < 22; i++) {
226                 p += strcspn(p, " ");
227                 p += strspn(p, " ");
228         }
229         i = atoi(p);
230         printf("Virtual size = %i, ", i);
231         p += strcspn(p, " ");
232         p += strspn(p, " ");
233         i = atoi(p);
234         printf("RSS = %i\n", i * getpagesize());
235 }
236
237 #define LOOPS 1000
238
239 int main(int argc, char *argv[])
240 {
241         struct timespec start, alloc_time, free_time;
242         struct node *root;
243         unsigned int i;
244         FILE *f;
245         bool run_talloc = true, run_tal = true, run_malloc = true;
246
247         f = argv[1] ? fopen(argv[1], "r") : stdin;
248         root = read_nodes(f);
249         fclose(f);
250         printf("Read %u nodes\n", node_count);
251
252         if (argc > 2) {
253                 if (streq(argv[2], "--talloc-size")) {
254                         do_tallocs(root);
255                         dump_vsize();
256                         exit(0);
257                 }
258                 if (streq(argv[2], "--tal-size")) {
259                         do_tals(root);
260                         dump_vsize();
261                         exit(0);
262                 }
263                 if (strcmp(argv[2], "--talloc") == 0)
264                         run_tal = run_malloc = false;
265                 else if (strcmp(argv[2], "--tal") == 0)
266                         run_talloc = run_malloc = false;
267                 else if (strcmp(argv[2], "--malloc") == 0)
268                         run_talloc = run_tal = false;
269                 else
270                         errx(1, "Bad flag %s", argv[2]);
271         }
272
273         if (!run_malloc)
274                 goto after_malloc;
275
276         alloc_time.tv_sec = alloc_time.tv_nsec = 0;
277         free_time.tv_sec = free_time.tv_nsec = 0;
278         for (i = 0; i < LOOPS; i++) {
279                 start = time_now();
280                 do_mallocs(root);
281                 alloc_time = time_add(alloc_time, time_sub(time_now(), start));
282
283                 start = time_now();
284                 free_mallocs(root);
285                 free_time = time_add(free_time, time_sub(time_now(), start));
286         }
287         alloc_time = time_divide(alloc_time, i);
288         free_time = time_divide(free_time, i);
289         printf("Malloc time:             %lluns\n", time_to_nsec(alloc_time));
290         printf("Free time:               %lluns\n", time_to_nsec(free_time));
291
292 after_malloc:
293         if (!run_talloc)
294                 goto after_talloc;
295
296         alloc_time.tv_sec = alloc_time.tv_nsec = 0;
297         free_time.tv_sec = free_time.tv_nsec = 0;
298         for (i = 0; i < LOOPS; i++) {
299                 start = time_now();
300                 do_tallocs(root);
301                 alloc_time = time_add(alloc_time, time_sub(time_now(), start));
302
303                 start = time_now();
304                 free_tallocs(root);
305                 free_time = time_add(free_time, time_sub(time_now(), start));
306         }
307         alloc_time = time_divide(alloc_time, i);
308         free_time = time_divide(free_time, i);
309         printf("Talloc time:             %lluns\n", time_to_nsec(alloc_time));
310         printf("talloc_free time:        %lluns\n", time_to_nsec(free_time));
311
312         free_time.tv_sec = free_time.tv_nsec = 0;
313         for (i = 0; i < LOOPS; i++) {
314                 do_tallocs(root);
315
316                 start = time_now();
317                 talloc_free(root->n);
318                 free_time = time_add(free_time, time_sub(time_now(), start));
319         }
320         free_time = time_divide(free_time, i);
321         printf("Single talloc_free time: %lluns\n", time_to_nsec(free_time));
322
323 after_talloc:
324         if (!run_tal)
325                 goto after_tal;
326
327         alloc_time.tv_sec = alloc_time.tv_nsec = 0;
328         free_time.tv_sec = free_time.tv_nsec = 0;
329         for (i = 0; i < LOOPS; i++) {
330                 start = time_now();
331                 do_tals(root);
332                 alloc_time = time_add(alloc_time, time_sub(time_now(), start));
333
334                 start = time_now();
335                 free_tals(root);
336                 free_time = time_add(free_time, time_sub(time_now(), start));
337         }
338         alloc_time = time_divide(alloc_time, i);
339         free_time = time_divide(free_time, i);
340         printf("Tal time:                %lluns\n", time_to_nsec(alloc_time));
341         printf("Tal_free time:           %lluns\n", time_to_nsec(free_time));
342
343 after_tal:
344         free_time.tv_sec = free_time.tv_nsec = 0;
345         for (i = 0; i < LOOPS; i++) {
346                 do_tals(root);
347
348                 start = time_now();
349                 tal_free(root->n);
350                 free_time = time_add(free_time, time_sub(time_now(), start));
351         }
352         free_time = time_divide(free_time, i);
353         printf("Single tal_free time:    %lluns\n", time_to_nsec(free_time));
354
355         return 0;
356 }