ntdbtorture.c 11 KB

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