samba-allocs.c 8.5 KB

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