random-test.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <signal.h>
  4. #include "kerncompat.h"
  5. #include "radix-tree.h"
  6. #include "ctree.h"
  7. #include "disk-io.h"
  8. #include "print-tree.h"
  9. int keep_running = 1;
  10. static int setup_key(struct radix_tree_root *root, struct key *key, int exists)
  11. {
  12. int num = rand();
  13. unsigned long res[2];
  14. int ret;
  15. key->flags = 0;
  16. key->offset = 0;
  17. again:
  18. ret = radix_tree_gang_lookup(root, (void **)res, num, 2);
  19. if (exists) {
  20. if (ret == 0)
  21. return -1;
  22. num = res[0];
  23. } else if (ret != 0 && num == res[0]) {
  24. num++;
  25. if (ret > 1 && num == res[1]) {
  26. num++;
  27. goto again;
  28. }
  29. }
  30. key->objectid = num;
  31. return 0;
  32. }
  33. static int ins_one(struct ctree_root *root, struct radix_tree_root *radix)
  34. {
  35. struct ctree_path path;
  36. struct key key;
  37. int ret;
  38. char buf[128];
  39. unsigned long oid;
  40. init_path(&path);
  41. ret = setup_key(radix, &key, 0);
  42. sprintf(buf, "str-%Lu\n", key.objectid);
  43. ret = insert_item(root, &key, buf, strlen(buf));
  44. if (ret)
  45. goto error;
  46. oid = (unsigned long)key.objectid;
  47. radix_tree_preload(GFP_KERNEL);
  48. ret = radix_tree_insert(radix, oid, (void *)oid);
  49. radix_tree_preload_end();
  50. if (ret)
  51. goto error;
  52. return ret;
  53. error:
  54. printf("failed to insert %Lu\n", key.objectid);
  55. return -1;
  56. }
  57. static int run_commit(struct ctree_root *root, struct radix_tree_root *radix)
  58. {
  59. return commit_transaction(root);
  60. }
  61. static int insert_dup(struct ctree_root *root, struct radix_tree_root *radix)
  62. {
  63. struct ctree_path path;
  64. struct key key;
  65. int ret;
  66. char buf[128];
  67. init_path(&path);
  68. ret = setup_key(radix, &key, 1);
  69. if (ret < 0)
  70. return 0;
  71. sprintf(buf, "str-%Lu\n", key.objectid);
  72. ret = insert_item(root, &key, buf, strlen(buf));
  73. if (ret != -EEXIST) {
  74. printf("insert on %Lu gave us %d\n", key.objectid, ret);
  75. return 1;
  76. }
  77. return 0;
  78. }
  79. static int del_one(struct ctree_root *root, struct radix_tree_root *radix)
  80. {
  81. struct ctree_path path;
  82. struct key key;
  83. int ret;
  84. unsigned long *ptr;
  85. init_path(&path);
  86. ret = setup_key(radix, &key, 1);
  87. if (ret < 0)
  88. return 0;
  89. ret = search_slot(root, &key, &path, -1);
  90. if (ret)
  91. goto error;
  92. ret = del_item(root, &path);
  93. release_path(root, &path);
  94. if (ret != 0)
  95. goto error;
  96. ptr = radix_tree_delete(radix, key.objectid);
  97. if (!ptr)
  98. goto error;
  99. return 0;
  100. error:
  101. printf("failed to delete %Lu\n", key.objectid);
  102. return -1;
  103. }
  104. static int lookup_item(struct ctree_root *root, struct radix_tree_root *radix)
  105. {
  106. struct ctree_path path;
  107. struct key key;
  108. int ret;
  109. init_path(&path);
  110. ret = setup_key(radix, &key, 1);
  111. if (ret < 0)
  112. return 0;
  113. ret = search_slot(root, &key, &path, 0);
  114. release_path(root, &path);
  115. if (ret)
  116. goto error;
  117. return 0;
  118. error:
  119. printf("unable to find key %Lu\n", key.objectid);
  120. return -1;
  121. }
  122. static int lookup_enoent(struct ctree_root *root, struct radix_tree_root *radix)
  123. {
  124. struct ctree_path path;
  125. struct key key;
  126. int ret;
  127. init_path(&path);
  128. ret = setup_key(radix, &key, 0);
  129. if (ret < 0)
  130. return ret;
  131. ret = search_slot(root, &key, &path, 0);
  132. release_path(root, &path);
  133. if (ret <= 0)
  134. goto error;
  135. return 0;
  136. error:
  137. printf("able to find key that should not exist %Lu\n", key.objectid);
  138. return -1;
  139. }
  140. static int empty_tree(struct ctree_root *root, struct radix_tree_root *radix,
  141. int nr)
  142. {
  143. struct ctree_path path;
  144. struct key key;
  145. unsigned long found = 0;
  146. int ret;
  147. int slot;
  148. int *ptr;
  149. int count = 0;
  150. key.offset = 0;
  151. key.flags = 0;
  152. key.objectid = (unsigned long)-1;
  153. while(nr-- >= 0) {
  154. init_path(&path);
  155. ret = search_slot(root, &key, &path, -1);
  156. if (ret < 0) {
  157. release_path(root, &path);
  158. return ret;
  159. }
  160. if (ret != 0) {
  161. if (path.slots[0] == 0) {
  162. release_path(root, &path);
  163. break;
  164. }
  165. path.slots[0] -= 1;
  166. }
  167. slot = path.slots[0];
  168. found = path.nodes[0]->leaf.items[slot].key.objectid;
  169. ret = del_item(root, &path);
  170. count++;
  171. if (ret) {
  172. fprintf(stderr,
  173. "failed to remove %lu from tree\n",
  174. found);
  175. return -1;
  176. }
  177. release_path(root, &path);
  178. ptr = radix_tree_delete(radix, found);
  179. if (!ptr)
  180. goto error;
  181. if (!keep_running)
  182. break;
  183. }
  184. return 0;
  185. error:
  186. fprintf(stderr, "failed to delete from the radix %lu\n", found);
  187. return -1;
  188. }
  189. static int fill_tree(struct ctree_root *root, struct radix_tree_root *radix,
  190. int count)
  191. {
  192. int i;
  193. int ret = 0;
  194. for (i = 0; i < count; i++) {
  195. ret = ins_one(root, radix);
  196. if (ret) {
  197. fprintf(stderr, "fill failed\n");
  198. goto out;
  199. }
  200. if (i % 1000 == 0) {
  201. ret = commit_transaction(root);
  202. if (ret) {
  203. fprintf(stderr, "fill commit failed\n");
  204. return ret;
  205. }
  206. }
  207. if (i % 10000 == 0) {
  208. printf("bigfill %d\n", i);
  209. }
  210. if (!keep_running)
  211. break;
  212. }
  213. out:
  214. return ret;
  215. }
  216. static int bulk_op(struct ctree_root *root, struct radix_tree_root *radix)
  217. {
  218. int ret;
  219. int nr = rand() % 20000;
  220. static int run_nr = 0;
  221. /* do the bulk op much less frequently */
  222. if (run_nr++ % 100)
  223. return 0;
  224. ret = empty_tree(root, radix, nr);
  225. if (ret)
  226. return ret;
  227. ret = fill_tree(root, radix, nr);
  228. if (ret)
  229. return ret;
  230. return 0;
  231. }
  232. int (*ops[])(struct ctree_root *root, struct radix_tree_root *radix) =
  233. { ins_one, insert_dup, del_one, lookup_item,
  234. lookup_enoent, bulk_op, run_commit };
  235. static int fill_radix(struct ctree_root *root, struct radix_tree_root *radix)
  236. {
  237. struct ctree_path path;
  238. struct key key;
  239. unsigned long found;
  240. int ret;
  241. int slot;
  242. int i;
  243. key.offset = 0;
  244. key.flags = 0;
  245. key.objectid = (unsigned long)-1;
  246. while(1) {
  247. init_path(&path);
  248. ret = search_slot(root, &key, &path, 0);
  249. if (ret < 0) {
  250. release_path(root, &path);
  251. return ret;
  252. }
  253. slot = path.slots[0];
  254. if (ret != 0) {
  255. if (slot == 0) {
  256. release_path(root, &path);
  257. break;
  258. }
  259. slot -= 1;
  260. }
  261. for (i = slot; i >= 0; i--) {
  262. found = path.nodes[0]->leaf.items[i].key.objectid;
  263. radix_tree_preload(GFP_KERNEL);
  264. ret = radix_tree_insert(radix, found, (void *)found);
  265. if (ret) {
  266. fprintf(stderr,
  267. "failed to insert %lu into radix\n",
  268. found);
  269. exit(1);
  270. }
  271. radix_tree_preload_end();
  272. }
  273. release_path(root, &path);
  274. key.objectid = found - 1;
  275. if (key.objectid > found)
  276. break;
  277. }
  278. return 0;
  279. }
  280. void sigstopper(int ignored)
  281. {
  282. keep_running = 0;
  283. fprintf(stderr, "caught exit signal, stopping\n");
  284. }
  285. int print_usage(void)
  286. {
  287. printf("usage: tester [-ih] [-c count] [-f count]\n");
  288. printf("\t -c count -- iteration count after filling\n");
  289. printf("\t -f count -- run this many random inserts before starting\n");
  290. printf("\t -i -- only do initial fill\n");
  291. printf("\t -h -- this help text\n");
  292. exit(1);
  293. }
  294. int main(int ac, char **av)
  295. {
  296. RADIX_TREE(radix, GFP_KERNEL);
  297. struct ctree_super_block super;
  298. struct ctree_root *root;
  299. int i;
  300. int ret;
  301. int count;
  302. int op;
  303. int iterations = 20000;
  304. int init_fill_count = 800000;
  305. int err = 0;
  306. int initial_only = 0;
  307. radix_tree_init();
  308. root = open_ctree("dbfile", &super);
  309. fill_radix(root, &radix);
  310. signal(SIGTERM, sigstopper);
  311. signal(SIGINT, sigstopper);
  312. for (i = 1 ; i < ac ; i++) {
  313. if (strcmp(av[i], "-i") == 0) {
  314. initial_only = 1;
  315. } else if (strcmp(av[i], "-c") == 0) {
  316. iterations = atoi(av[i+1]);
  317. i++;
  318. } else if (strcmp(av[i], "-f") == 0) {
  319. init_fill_count = atoi(av[i+1]);
  320. i++;
  321. } else {
  322. print_usage();
  323. }
  324. }
  325. printf("initial fill\n");
  326. ret = fill_tree(root, &radix, init_fill_count);
  327. printf("starting run\n");
  328. if (ret) {
  329. err = ret;
  330. goto out;
  331. }
  332. if (initial_only == 1) {
  333. goto out;
  334. }
  335. for (i = 0; i < iterations; i++) {
  336. op = rand() % ARRAY_SIZE(ops);
  337. count = rand() % 128;
  338. if (i % 2000 == 0) {
  339. printf("%d\n", i);
  340. fflush(stdout);
  341. }
  342. if (i && i % 5000 == 0) {
  343. printf("open & close, root level %d nritems %d\n",
  344. node_level(root->node->node.header.flags),
  345. root->node->node.header.nritems);
  346. write_ctree_super(root, &super);
  347. close_ctree(root);
  348. root = open_ctree("dbfile", &super);
  349. }
  350. while(count--) {
  351. ret = ops[op](root, &radix);
  352. if (ret) {
  353. fprintf(stderr, "op %d failed %d:%d\n",
  354. op, i, iterations);
  355. print_tree(root, root->node);
  356. fprintf(stderr, "op %d failed %d:%d\n",
  357. op, i, iterations);
  358. err = ret;
  359. goto out;
  360. }
  361. if (ops[op] == bulk_op || ops[op] == run_commit)
  362. break;
  363. if (keep_running == 0) {
  364. err = 0;
  365. goto out;
  366. }
  367. }
  368. }
  369. out:
  370. write_ctree_super(root, &super);
  371. close_ctree(root);
  372. return err;
  373. }