tdbtorture.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /* this tests tdb by doing lots of ops from several simultaneous
  2. writers - that stresses the locking code.
  3. */
  4. #include <ccan/tdb2/tdb2.h>
  5. #include <stdlib.h>
  6. #include <err.h>
  7. #include <getopt.h>
  8. #include <stdarg.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <errno.h>
  12. #include <unistd.h>
  13. #include <sys/types.h>
  14. #include <fcntl.h>
  15. #include <time.h>
  16. #include <sys/wait.h>
  17. //#define REOPEN_PROB 30
  18. #define DELETE_PROB 8
  19. #define STORE_PROB 4
  20. #define APPEND_PROB 6
  21. #define TRANSACTION_PROB 10
  22. #define TRANSACTION_PREPARE_PROB 2
  23. #define LOCKSTORE_PROB 5
  24. #define TRAVERSE_PROB 20
  25. #define TRAVERSE_MOD_PROB 100
  26. #define TRAVERSE_ABORT_PROB 500
  27. #define CULL_PROB 100
  28. #define KEYLEN 3
  29. #define DATALEN 100
  30. static struct tdb_context *db;
  31. static int in_transaction;
  32. static int in_traverse;
  33. static int error_count;
  34. #if TRANSACTION_PROB
  35. static int always_transaction = 0;
  36. #endif
  37. static int loopnum;
  38. static int count_pipe;
  39. static union tdb_attribute log_attr;
  40. static union tdb_attribute seed_attr;
  41. static void tdb_log(struct tdb_context *tdb, enum tdb_log_level level,
  42. void *private, const char *message)
  43. {
  44. fputs(message, stdout);
  45. fflush(stdout);
  46. #if 0
  47. {
  48. char *ptr;
  49. signal(SIGUSR1, SIG_IGN);
  50. asprintf(&ptr,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid());
  51. system(ptr);
  52. free(ptr);
  53. }
  54. #endif
  55. }
  56. #include "../private.h"
  57. static void segv_handler(int signal, siginfo_t *info, void *p)
  58. {
  59. char string[100];
  60. sprintf(string, "%u: death at %p (map_ptr %p, map_size %zu)\n",
  61. getpid(), info->si_addr, db->file->map_ptr,
  62. (size_t)db->file->map_size);
  63. if (write(2, string, strlen(string)) > 0)
  64. sleep(60);
  65. _exit(11);
  66. }
  67. static void fatal(const char *why)
  68. {
  69. perror(why);
  70. error_count++;
  71. }
  72. static char *randbuf(int len)
  73. {
  74. char *buf;
  75. int i;
  76. buf = (char *)malloc(len+1);
  77. for (i=0;i<len;i++) {
  78. buf[i] = 'a' + (rand() % 26);
  79. }
  80. buf[i] = 0;
  81. return buf;
  82. }
  83. static void addrec_db(void);
  84. static int modify_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
  85. void *state)
  86. {
  87. #if CULL_PROB
  88. if (random() % CULL_PROB == 0) {
  89. tdb_delete(tdb, key);
  90. }
  91. #endif
  92. #if TRAVERSE_MOD_PROB
  93. if (random() % TRAVERSE_MOD_PROB == 0) {
  94. addrec_db();
  95. }
  96. #endif
  97. #if TRAVERSE_ABORT_PROB
  98. if (random() % TRAVERSE_ABORT_PROB == 0)
  99. return 1;
  100. #endif
  101. return 0;
  102. }
  103. static void addrec_db(void)
  104. {
  105. int klen, dlen;
  106. char *k, *d;
  107. TDB_DATA key, data;
  108. klen = 1 + (rand() % KEYLEN);
  109. dlen = 1 + (rand() % DATALEN);
  110. k = randbuf(klen);
  111. d = randbuf(dlen);
  112. key.dptr = (unsigned char *)k;
  113. key.dsize = klen+1;
  114. data.dptr = (unsigned char *)d;
  115. data.dsize = dlen+1;
  116. #if REOPEN_PROB
  117. if (in_traverse == 0 && in_transaction == 0 && random() % REOPEN_PROB == 0) {
  118. tdb_reopen_all(0);
  119. goto next;
  120. }
  121. #endif
  122. #if TRANSACTION_PROB
  123. if (in_traverse == 0 && in_transaction == 0 && (always_transaction || random() % TRANSACTION_PROB == 0)) {
  124. if (tdb_transaction_start(db) != 0) {
  125. fatal("tdb_transaction_start failed");
  126. }
  127. in_transaction++;
  128. goto next;
  129. }
  130. if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
  131. if (random() % TRANSACTION_PREPARE_PROB == 0) {
  132. if (tdb_transaction_prepare_commit(db) != 0) {
  133. fatal("tdb_transaction_prepare_commit failed");
  134. }
  135. }
  136. if (tdb_transaction_commit(db) != 0) {
  137. fatal("tdb_transaction_commit failed");
  138. }
  139. in_transaction--;
  140. goto next;
  141. }
  142. if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
  143. tdb_transaction_cancel(db);
  144. in_transaction--;
  145. goto next;
  146. }
  147. #endif
  148. #if DELETE_PROB
  149. if (random() % DELETE_PROB == 0) {
  150. tdb_delete(db, key);
  151. goto next;
  152. }
  153. #endif
  154. #if STORE_PROB
  155. if (random() % STORE_PROB == 0) {
  156. if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
  157. fatal("tdb_store failed");
  158. }
  159. goto next;
  160. }
  161. #endif
  162. #if APPEND_PROB
  163. if (random() % APPEND_PROB == 0) {
  164. if (tdb_append(db, key, data) != 0) {
  165. fatal("tdb_append failed");
  166. }
  167. goto next;
  168. }
  169. #endif
  170. #if LOCKSTORE_PROB
  171. if (random() % LOCKSTORE_PROB == 0) {
  172. tdb_chainlock(db, key);
  173. tdb_fetch(db, key, &data);
  174. if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
  175. fatal("tdb_store failed");
  176. }
  177. if (data.dptr) free(data.dptr);
  178. tdb_chainunlock(db, key);
  179. goto next;
  180. }
  181. #endif
  182. #if TRAVERSE_PROB
  183. /* FIXME: recursive traverses break transactions? */
  184. if (in_traverse == 0 && random() % TRAVERSE_PROB == 0) {
  185. in_traverse++;
  186. tdb_traverse(db, modify_traverse, NULL);
  187. in_traverse--;
  188. goto next;
  189. }
  190. #endif
  191. if (tdb_fetch(db, key, &data) == TDB_SUCCESS)
  192. free(data.dptr);
  193. next:
  194. free(k);
  195. free(d);
  196. }
  197. static int traverse_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
  198. void *state)
  199. {
  200. tdb_delete(tdb, key);
  201. return 0;
  202. }
  203. static void usage(void)
  204. {
  205. printf("Usage: tdbtorture"
  206. #if TRANSACTION_PROB
  207. " [-t]"
  208. #endif
  209. " [-k] [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED]\n");
  210. exit(0);
  211. }
  212. static void send_count_and_suicide(int sig)
  213. {
  214. /* This ensures our successor can continue where we left off. */
  215. if (write(count_pipe, &loopnum, sizeof(loopnum)) != sizeof(loopnum))
  216. exit(2);
  217. /* This gives a unique signature. */
  218. kill(getpid(), SIGUSR2);
  219. }
  220. static int run_child(int i, int seed, unsigned num_loops, unsigned start)
  221. {
  222. struct sigaction act = { .sa_sigaction = segv_handler,
  223. .sa_flags = SA_SIGINFO };
  224. sigaction(11, &act, NULL);
  225. db = tdb_open("torture.tdb", TDB_DEFAULT, O_RDWR | O_CREAT, 0600,
  226. &log_attr);
  227. if (!db) {
  228. fatal("db open failed");
  229. }
  230. #if 0
  231. if (i == 0) {
  232. printf("pid %i\n", getpid());
  233. sleep(9);
  234. } else
  235. sleep(10);
  236. #endif
  237. srand(seed + i);
  238. srandom(seed + i);
  239. /* Set global, then we're ready to handle being killed. */
  240. loopnum = start;
  241. signal(SIGUSR1, send_count_and_suicide);
  242. for (;loopnum<num_loops && error_count == 0;loopnum++) {
  243. addrec_db();
  244. }
  245. if (error_count == 0) {
  246. tdb_traverse(db, NULL, NULL);
  247. #if TRANSACTION_PROB
  248. if (always_transaction) {
  249. while (in_transaction) {
  250. tdb_transaction_cancel(db);
  251. in_transaction--;
  252. }
  253. if (tdb_transaction_start(db) != 0)
  254. fatal("tdb_transaction_start failed");
  255. }
  256. #endif
  257. tdb_traverse(db, traverse_fn, NULL);
  258. tdb_traverse(db, traverse_fn, NULL);
  259. #if TRANSACTION_PROB
  260. if (always_transaction) {
  261. if (tdb_transaction_commit(db) != 0)
  262. fatal("tdb_transaction_commit failed");
  263. }
  264. #endif
  265. }
  266. tdb_close(db);
  267. return (error_count < 100 ? error_count : 100);
  268. }
  269. int main(int argc, char * const *argv)
  270. {
  271. int i, seed = -1;
  272. int num_loops = 5000;
  273. int num_procs = 3;
  274. int c, pfds[2];
  275. extern char *optarg;
  276. pid_t *pids;
  277. int kill_random = 0;
  278. int *done;
  279. log_attr.base.attr = TDB_ATTRIBUTE_LOG;
  280. log_attr.base.next = &seed_attr;
  281. log_attr.log.log_fn = tdb_log;
  282. seed_attr.base.attr = TDB_ATTRIBUTE_SEED;
  283. while ((c = getopt(argc, argv, "n:l:s:thk")) != -1) {
  284. switch (c) {
  285. case 'n':
  286. num_procs = strtol(optarg, NULL, 0);
  287. break;
  288. case 'l':
  289. num_loops = strtol(optarg, NULL, 0);
  290. break;
  291. case 's':
  292. seed = strtol(optarg, NULL, 0);
  293. break;
  294. case 't':
  295. #if TRANSACTION_PROB
  296. always_transaction = 1;
  297. #else
  298. fprintf(stderr, "Transactions not supported\n");
  299. usage();
  300. #endif
  301. break;
  302. case 'k':
  303. kill_random = 1;
  304. break;
  305. default:
  306. usage();
  307. }
  308. }
  309. unlink("torture.tdb");
  310. if (seed == -1) {
  311. seed = (getpid() + time(NULL)) & 0x7FFFFFFF;
  312. }
  313. seed_attr.seed.seed = (((uint64_t)seed) << 32) | seed;
  314. if (num_procs == 1 && !kill_random) {
  315. /* Don't fork for this case, makes debugging easier. */
  316. error_count = run_child(0, seed, num_loops, 0);
  317. goto done;
  318. }
  319. pids = (pid_t *)calloc(sizeof(pid_t), num_procs);
  320. done = (int *)calloc(sizeof(int), num_procs);
  321. if (pipe(pfds) != 0) {
  322. perror("Creating pipe");
  323. exit(1);
  324. }
  325. count_pipe = pfds[1];
  326. for (i=0;i<num_procs;i++) {
  327. if ((pids[i]=fork()) == 0) {
  328. close(pfds[0]);
  329. if (i == 0) {
  330. printf("testing with %d processes, %d loops, seed=%d%s\n",
  331. num_procs, num_loops, seed,
  332. #if TRANSACTION_PROB
  333. always_transaction ? " (all within transactions)" : ""
  334. #else
  335. ""
  336. #endif
  337. );
  338. }
  339. exit(run_child(i, seed, num_loops, 0));
  340. }
  341. }
  342. while (num_procs) {
  343. int status, j;
  344. pid_t pid;
  345. if (error_count != 0) {
  346. /* try and stop the test on any failure */
  347. for (j=0;j<num_procs;j++) {
  348. if (pids[j] != 0) {
  349. kill(pids[j], SIGTERM);
  350. }
  351. }
  352. }
  353. pid = waitpid(-1, &status, kill_random ? WNOHANG : 0);
  354. if (pid == 0) {
  355. struct timespec ts;
  356. /* Sleep for 1/10 second. */
  357. ts.tv_sec = 0;
  358. ts.tv_nsec = 100000000;
  359. nanosleep(&ts, NULL);
  360. /* Kill someone. */
  361. kill(pids[random() % num_procs], SIGUSR1);
  362. continue;
  363. }
  364. if (pid == -1) {
  365. perror("failed to wait for child\n");
  366. exit(1);
  367. }
  368. for (j=0;j<num_procs;j++) {
  369. if (pids[j] == pid) break;
  370. }
  371. if (j == num_procs) {
  372. printf("unknown child %d exited!?\n", (int)pid);
  373. exit(1);
  374. }
  375. if (WIFSIGNALED(status)) {
  376. if (WTERMSIG(status) == SIGUSR2
  377. || WTERMSIG(status) == SIGUSR1) {
  378. /* SIGUSR2 means they wrote to pipe. */
  379. if (WTERMSIG(status) == SIGUSR2) {
  380. if (read(pfds[0], &done[j],
  381. sizeof(done[j]))
  382. != sizeof(done[j]))
  383. err(1,
  384. "Short read from child?");
  385. }
  386. pids[j] = fork();
  387. if (pids[j] == 0)
  388. exit(run_child(j, seed, num_loops,
  389. done[j]));
  390. printf("Restarting child %i for %u-%u\n",
  391. j, done[j], num_loops);
  392. continue;
  393. }
  394. printf("child %d exited with signal %d\n",
  395. (int)pid, WTERMSIG(status));
  396. error_count++;
  397. } else {
  398. if (WEXITSTATUS(status) != 0) {
  399. printf("child %d exited with status %d\n",
  400. (int)pid, WEXITSTATUS(status));
  401. error_count++;
  402. }
  403. }
  404. memmove(&pids[j], &pids[j+1],
  405. (num_procs - j - 1)*sizeof(pids[0]));
  406. num_procs--;
  407. }
  408. free(pids);
  409. done:
  410. if (error_count == 0) {
  411. db = tdb_open("torture.tdb", TDB_DEFAULT, O_RDWR | O_CREAT,
  412. 0600, &log_attr);
  413. if (!db) {
  414. fatal("db open failed");
  415. }
  416. if (tdb_check(db, NULL, NULL) == -1) {
  417. printf("db check failed");
  418. exit(1);
  419. }
  420. tdb_close(db);
  421. printf("OK\n");
  422. }
  423. return error_count;
  424. }