tlbie_test.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2019, Nick Piggin, Gautham R. Shenoy, Aneesh Kumar K.V, IBM Corp.
  4. */
  5. /*
  6. *
  7. * Test tlbie/mtpidr race. We have 4 threads doing flush/load/compare/store
  8. * sequence in a loop. The same threads also rung a context switch task
  9. * that does sched_yield() in loop.
  10. *
  11. * The snapshot thread mark the mmap area PROT_READ in between, make a copy
  12. * and copy it back to the original area. This helps us to detect if any
  13. * store continued to happen after we marked the memory PROT_READ.
  14. */
  15. #define _GNU_SOURCE
  16. #include <stdio.h>
  17. #include <sys/mman.h>
  18. #include <sys/types.h>
  19. #include <sys/wait.h>
  20. #include <sys/ipc.h>
  21. #include <sys/shm.h>
  22. #include <sys/stat.h>
  23. #include <sys/time.h>
  24. #include <linux/futex.h>
  25. #include <unistd.h>
  26. #include <asm/unistd.h>
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #include <fcntl.h>
  30. #include <sched.h>
  31. #include <time.h>
  32. #include <stdarg.h>
  33. #include <pthread.h>
  34. #include <signal.h>
  35. #include <sys/prctl.h>
  36. static inline void dcbf(volatile unsigned int *addr)
  37. {
  38. __asm__ __volatile__ ("dcbf %y0; sync" : : "Z"(*(unsigned char *)addr) : "memory");
  39. }
  40. static void err_msg(char *msg)
  41. {
  42. time_t now;
  43. time(&now);
  44. printf("=================================\n");
  45. printf(" Error: %s\n", msg);
  46. printf(" %s", ctime(&now));
  47. printf("=================================\n");
  48. exit(1);
  49. }
  50. static char *map1;
  51. static char *map2;
  52. static pid_t rim_process_pid;
  53. /*
  54. * A "rim-sequence" is defined to be the sequence of the following
  55. * operations performed on a memory word:
  56. * 1) FLUSH the contents of that word.
  57. * 2) LOAD the contents of that word.
  58. * 3) COMPARE the contents of that word with the content that was
  59. * previously stored at that word
  60. * 4) STORE new content into that word.
  61. *
  62. * The threads in this test that perform the rim-sequence are termed
  63. * as rim_threads.
  64. */
  65. /*
  66. * A "corruption" is defined to be the failed COMPARE operation in a
  67. * rim-sequence.
  68. *
  69. * A rim_thread that detects a corruption informs about it to all the
  70. * other rim_threads, and the mem_snapshot thread.
  71. */
  72. static volatile unsigned int corruption_found;
  73. /*
  74. * This defines the maximum number of rim_threads in this test.
  75. *
  76. * The THREAD_ID_BITS denote the number of bits required
  77. * to represent the thread_ids [0..MAX_THREADS - 1].
  78. * We are being a bit paranoid here and set it to 8 bits,
  79. * though 6 bits suffice.
  80. *
  81. */
  82. #define MAX_THREADS 64
  83. #define THREAD_ID_BITS 8
  84. #define THREAD_ID_MASK ((1 << THREAD_ID_BITS) - 1)
  85. static unsigned int rim_thread_ids[MAX_THREADS];
  86. static pthread_t rim_threads[MAX_THREADS];
  87. /*
  88. * Each rim_thread works on an exclusive "chunk" of size
  89. * RIM_CHUNK_SIZE.
  90. *
  91. * The ith rim_thread works on the ith chunk.
  92. *
  93. * The ith chunk begins at
  94. * map1 + (i * RIM_CHUNK_SIZE)
  95. */
  96. #define RIM_CHUNK_SIZE 1024
  97. #define BITS_PER_BYTE 8
  98. #define WORD_SIZE (sizeof(unsigned int))
  99. #define WORD_BITS (WORD_SIZE * BITS_PER_BYTE)
  100. #define WORDS_PER_CHUNK (RIM_CHUNK_SIZE/WORD_SIZE)
  101. static inline char *compute_chunk_start_addr(unsigned int thread_id)
  102. {
  103. char *chunk_start;
  104. chunk_start = (char *)((unsigned long)map1 +
  105. (thread_id * RIM_CHUNK_SIZE));
  106. return chunk_start;
  107. }
  108. /*
  109. * The "word-offset" of a word-aligned address inside a chunk, is
  110. * defined to be the number of words that precede the address in that
  111. * chunk.
  112. *
  113. * WORD_OFFSET_BITS denote the number of bits required to represent
  114. * the word-offsets of all the word-aligned addresses of a chunk.
  115. */
  116. #define WORD_OFFSET_BITS (__builtin_ctz(WORDS_PER_CHUNK))
  117. #define WORD_OFFSET_MASK ((1 << WORD_OFFSET_BITS) - 1)
  118. static inline unsigned int compute_word_offset(char *start, unsigned int *addr)
  119. {
  120. unsigned int delta_bytes, ret;
  121. delta_bytes = (unsigned long)addr - (unsigned long)start;
  122. ret = delta_bytes/WORD_SIZE;
  123. return ret;
  124. }
  125. /*
  126. * A "sweep" is defined to be the sequential execution of the
  127. * rim-sequence by a rim_thread on its chunk one word at a time,
  128. * starting from the first word of its chunk and ending with the last
  129. * word of its chunk.
  130. *
  131. * Each sweep of a rim_thread is uniquely identified by a sweep_id.
  132. * SWEEP_ID_BITS denote the number of bits required to represent
  133. * the sweep_ids of rim_threads.
  134. *
  135. * As to why SWEEP_ID_BITS are computed as a function of THREAD_ID_BITS,
  136. * WORD_OFFSET_BITS, and WORD_BITS, see the "store-pattern" below.
  137. */
  138. #define SWEEP_ID_BITS (WORD_BITS - (THREAD_ID_BITS + WORD_OFFSET_BITS))
  139. #define SWEEP_ID_MASK ((1 << SWEEP_ID_BITS) - 1)
  140. /*
  141. * A "store-pattern" is the word-pattern that is stored into a word
  142. * location in the 4)STORE step of the rim-sequence.
  143. *
  144. * In the store-pattern, we shall encode:
  145. *
  146. * - The thread-id of the rim_thread performing the store
  147. * (The most significant THREAD_ID_BITS)
  148. *
  149. * - The word-offset of the address into which the store is being
  150. * performed (The next WORD_OFFSET_BITS)
  151. *
  152. * - The sweep_id of the current sweep in which the store is
  153. * being performed. (The lower SWEEP_ID_BITS)
  154. *
  155. * Store Pattern: 32 bits
  156. * |------------------|--------------------|---------------------------------|
  157. * | Thread id | Word offset | sweep_id |
  158. * |------------------|--------------------|---------------------------------|
  159. * THREAD_ID_BITS WORD_OFFSET_BITS SWEEP_ID_BITS
  160. *
  161. * In the store pattern, the (Thread-id + Word-offset) uniquely identify the
  162. * address to which the store is being performed i.e,
  163. * address == map1 +
  164. * (Thread-id * RIM_CHUNK_SIZE) + (Word-offset * WORD_SIZE)
  165. *
  166. * And the sweep_id in the store pattern identifies the time when the
  167. * store was performed by the rim_thread.
  168. *
  169. * We shall use this property in the 3)COMPARE step of the
  170. * rim-sequence.
  171. */
  172. #define SWEEP_ID_SHIFT 0
  173. #define WORD_OFFSET_SHIFT (SWEEP_ID_BITS)
  174. #define THREAD_ID_SHIFT (WORD_OFFSET_BITS + SWEEP_ID_BITS)
  175. /*
  176. * Compute the store pattern for a given thread with id @tid, at
  177. * location @addr in the sweep identified by @sweep_id
  178. */
  179. static inline unsigned int compute_store_pattern(unsigned int tid,
  180. unsigned int *addr,
  181. unsigned int sweep_id)
  182. {
  183. unsigned int ret = 0;
  184. char *start = compute_chunk_start_addr(tid);
  185. unsigned int word_offset = compute_word_offset(start, addr);
  186. ret += (tid & THREAD_ID_MASK) << THREAD_ID_SHIFT;
  187. ret += (word_offset & WORD_OFFSET_MASK) << WORD_OFFSET_SHIFT;
  188. ret += (sweep_id & SWEEP_ID_MASK) << SWEEP_ID_SHIFT;
  189. return ret;
  190. }
  191. /* Extract the thread-id from the given store-pattern */
  192. static inline unsigned int extract_tid(unsigned int pattern)
  193. {
  194. unsigned int ret;
  195. ret = (pattern >> THREAD_ID_SHIFT) & THREAD_ID_MASK;
  196. return ret;
  197. }
  198. /* Extract the word-offset from the given store-pattern */
  199. static inline unsigned int extract_word_offset(unsigned int pattern)
  200. {
  201. unsigned int ret;
  202. ret = (pattern >> WORD_OFFSET_SHIFT) & WORD_OFFSET_MASK;
  203. return ret;
  204. }
  205. /* Extract the sweep-id from the given store-pattern */
  206. static inline unsigned int extract_sweep_id(unsigned int pattern)
  207. {
  208. unsigned int ret;
  209. ret = (pattern >> SWEEP_ID_SHIFT) & SWEEP_ID_MASK;
  210. return ret;
  211. }
  212. /************************************************************
  213. * *
  214. * Logging the output of the verification *
  215. * *
  216. ************************************************************/
  217. #define LOGDIR_NAME_SIZE 100
  218. static char logdir[LOGDIR_NAME_SIZE];
  219. static FILE *fp[MAX_THREADS];
  220. static const char logfilename[] ="Thread-%02d-Chunk";
  221. static inline void start_verification_log(unsigned int tid,
  222. unsigned int *addr,
  223. unsigned int cur_sweep_id,
  224. unsigned int prev_sweep_id)
  225. {
  226. FILE *f;
  227. char logfile[30];
  228. char path[LOGDIR_NAME_SIZE + 30];
  229. char separator[2] = "/";
  230. char *chunk_start = compute_chunk_start_addr(tid);
  231. unsigned int size = RIM_CHUNK_SIZE;
  232. sprintf(logfile, logfilename, tid);
  233. strcpy(path, logdir);
  234. strcat(path, separator);
  235. strcat(path, logfile);
  236. f = fopen(path, "w");
  237. if (!f) {
  238. err_msg("Unable to create logfile\n");
  239. }
  240. fp[tid] = f;
  241. fprintf(f, "----------------------------------------------------------\n");
  242. fprintf(f, "PID = %d\n", rim_process_pid);
  243. fprintf(f, "Thread id = %02d\n", tid);
  244. fprintf(f, "Chunk Start Addr = 0x%016lx\n", (unsigned long)chunk_start);
  245. fprintf(f, "Chunk Size = %d\n", size);
  246. fprintf(f, "Next Store Addr = 0x%016lx\n", (unsigned long)addr);
  247. fprintf(f, "Current sweep-id = 0x%08x\n", cur_sweep_id);
  248. fprintf(f, "Previous sweep-id = 0x%08x\n", prev_sweep_id);
  249. fprintf(f, "----------------------------------------------------------\n");
  250. }
  251. static inline void log_anamoly(unsigned int tid, unsigned int *addr,
  252. unsigned int expected, unsigned int observed)
  253. {
  254. FILE *f = fp[tid];
  255. fprintf(f, "Thread %02d: Addr 0x%lx: Expected 0x%x, Observed 0x%x\n",
  256. tid, (unsigned long)addr, expected, observed);
  257. fprintf(f, "Thread %02d: Expected Thread id = %02d\n", tid, extract_tid(expected));
  258. fprintf(f, "Thread %02d: Observed Thread id = %02d\n", tid, extract_tid(observed));
  259. fprintf(f, "Thread %02d: Expected Word offset = %03d\n", tid, extract_word_offset(expected));
  260. fprintf(f, "Thread %02d: Observed Word offset = %03d\n", tid, extract_word_offset(observed));
  261. fprintf(f, "Thread %02d: Expected sweep-id = 0x%x\n", tid, extract_sweep_id(expected));
  262. fprintf(f, "Thread %02d: Observed sweep-id = 0x%x\n", tid, extract_sweep_id(observed));
  263. fprintf(f, "----------------------------------------------------------\n");
  264. }
  265. static inline void end_verification_log(unsigned int tid, unsigned nr_anamolies)
  266. {
  267. FILE *f = fp[tid];
  268. char logfile[30];
  269. char path[LOGDIR_NAME_SIZE + 30];
  270. char separator[] = "/";
  271. fclose(f);
  272. if (nr_anamolies == 0) {
  273. remove(path);
  274. return;
  275. }
  276. sprintf(logfile, logfilename, tid);
  277. strcpy(path, logdir);
  278. strcat(path, separator);
  279. strcat(path, logfile);
  280. printf("Thread %02d chunk has %d corrupted words. For details check %s\n",
  281. tid, nr_anamolies, path);
  282. }
  283. /*
  284. * When a COMPARE step of a rim-sequence fails, the rim_thread informs
  285. * everyone else via the shared_memory pointed to by
  286. * corruption_found variable. On seeing this, every thread verifies the
  287. * content of its chunk as follows.
  288. *
  289. * Suppose a thread identified with @tid was about to store (but not
  290. * yet stored) to @next_store_addr in its current sweep identified
  291. * @cur_sweep_id. Let @prev_sweep_id indicate the previous sweep_id.
  292. *
  293. * This implies that for all the addresses @addr < @next_store_addr,
  294. * Thread @tid has already performed a store as part of its current
  295. * sweep. Hence we expect the content of such @addr to be:
  296. * |-------------------------------------------------|
  297. * | tid | word_offset(addr) | cur_sweep_id |
  298. * |-------------------------------------------------|
  299. *
  300. * Since Thread @tid is yet to perform stores on address
  301. * @next_store_addr and above, we expect the content of such an
  302. * address @addr to be:
  303. * |-------------------------------------------------|
  304. * | tid | word_offset(addr) | prev_sweep_id |
  305. * |-------------------------------------------------|
  306. *
  307. * The verifier function @verify_chunk does this verification and logs
  308. * any anamolies that it finds.
  309. */
  310. static void verify_chunk(unsigned int tid, unsigned int *next_store_addr,
  311. unsigned int cur_sweep_id,
  312. unsigned int prev_sweep_id)
  313. {
  314. unsigned int *iter_ptr;
  315. unsigned int size = RIM_CHUNK_SIZE;
  316. unsigned int expected;
  317. unsigned int observed;
  318. char *chunk_start = compute_chunk_start_addr(tid);
  319. int nr_anamolies = 0;
  320. start_verification_log(tid, next_store_addr,
  321. cur_sweep_id, prev_sweep_id);
  322. for (iter_ptr = (unsigned int *)chunk_start;
  323. (unsigned long)iter_ptr < (unsigned long)chunk_start + size;
  324. iter_ptr++) {
  325. unsigned int expected_sweep_id;
  326. if (iter_ptr < next_store_addr) {
  327. expected_sweep_id = cur_sweep_id;
  328. } else {
  329. expected_sweep_id = prev_sweep_id;
  330. }
  331. expected = compute_store_pattern(tid, iter_ptr, expected_sweep_id);
  332. dcbf((volatile unsigned int*)iter_ptr); //Flush before reading
  333. observed = *iter_ptr;
  334. if (observed != expected) {
  335. nr_anamolies++;
  336. log_anamoly(tid, iter_ptr, expected, observed);
  337. }
  338. }
  339. end_verification_log(tid, nr_anamolies);
  340. }
  341. static void set_pthread_cpu(pthread_t th, int cpu)
  342. {
  343. cpu_set_t run_cpu_mask;
  344. struct sched_param param;
  345. CPU_ZERO(&run_cpu_mask);
  346. CPU_SET(cpu, &run_cpu_mask);
  347. pthread_setaffinity_np(th, sizeof(cpu_set_t), &run_cpu_mask);
  348. param.sched_priority = 1;
  349. if (0 && sched_setscheduler(0, SCHED_FIFO, &param) == -1) {
  350. /* haven't reproduced with this setting, it kills random preemption which may be a factor */
  351. fprintf(stderr, "could not set SCHED_FIFO, run as root?\n");
  352. }
  353. }
  354. static void set_mycpu(int cpu)
  355. {
  356. cpu_set_t run_cpu_mask;
  357. struct sched_param param;
  358. CPU_ZERO(&run_cpu_mask);
  359. CPU_SET(cpu, &run_cpu_mask);
  360. sched_setaffinity(0, sizeof(cpu_set_t), &run_cpu_mask);
  361. param.sched_priority = 1;
  362. if (0 && sched_setscheduler(0, SCHED_FIFO, &param) == -1) {
  363. fprintf(stderr, "could not set SCHED_FIFO, run as root?\n");
  364. }
  365. }
  366. static volatile int segv_wait;
  367. static void segv_handler(int signo, siginfo_t *info, void *extra)
  368. {
  369. while (segv_wait) {
  370. sched_yield();
  371. }
  372. }
  373. static void set_segv_handler(void)
  374. {
  375. struct sigaction sa;
  376. sa.sa_flags = SA_SIGINFO;
  377. sa.sa_sigaction = segv_handler;
  378. if (sigaction(SIGSEGV, &sa, NULL) == -1) {
  379. perror("sigaction");
  380. exit(EXIT_FAILURE);
  381. }
  382. }
  383. int timeout = 0;
  384. /*
  385. * This function is executed by every rim_thread.
  386. *
  387. * This function performs sweeps over the exclusive chunks of the
  388. * rim_threads executing the rim-sequence one word at a time.
  389. */
  390. static void *rim_fn(void *arg)
  391. {
  392. unsigned int tid = *((unsigned int *)arg);
  393. int size = RIM_CHUNK_SIZE;
  394. char *chunk_start = compute_chunk_start_addr(tid);
  395. unsigned int prev_sweep_id;
  396. unsigned int cur_sweep_id = 0;
  397. /* word access */
  398. unsigned int pattern = cur_sweep_id;
  399. unsigned int *pattern_ptr = &pattern;
  400. unsigned int *w_ptr, read_data;
  401. set_segv_handler();
  402. /*
  403. * Let us initialize the chunk:
  404. *
  405. * Each word-aligned address addr in the chunk,
  406. * is initialized to :
  407. * |-------------------------------------------------|
  408. * | tid | word_offset(addr) | 0 |
  409. * |-------------------------------------------------|
  410. */
  411. for (w_ptr = (unsigned int *)chunk_start;
  412. (unsigned long)w_ptr < (unsigned long)(chunk_start) + size;
  413. w_ptr++) {
  414. *pattern_ptr = compute_store_pattern(tid, w_ptr, cur_sweep_id);
  415. *w_ptr = *pattern_ptr;
  416. }
  417. while (!corruption_found && !timeout) {
  418. prev_sweep_id = cur_sweep_id;
  419. cur_sweep_id = cur_sweep_id + 1;
  420. for (w_ptr = (unsigned int *)chunk_start;
  421. (unsigned long)w_ptr < (unsigned long)(chunk_start) + size;
  422. w_ptr++) {
  423. unsigned int old_pattern;
  424. /*
  425. * Compute the pattern that we would have
  426. * stored at this location in the previous
  427. * sweep.
  428. */
  429. old_pattern = compute_store_pattern(tid, w_ptr, prev_sweep_id);
  430. /*
  431. * FLUSH:Ensure that we flush the contents of
  432. * the cache before loading
  433. */
  434. dcbf((volatile unsigned int*)w_ptr); //Flush
  435. /* LOAD: Read the value */
  436. read_data = *w_ptr; //Load
  437. /*
  438. * COMPARE: Is it the same as what we had stored
  439. * in the previous sweep ? It better be!
  440. */
  441. if (read_data != old_pattern) {
  442. /* No it isn't! Tell everyone */
  443. corruption_found = 1;
  444. }
  445. /*
  446. * Before performing a store, let us check if
  447. * any rim_thread has found a corruption.
  448. */
  449. if (corruption_found || timeout) {
  450. /*
  451. * Yes. Someone (including us!) has found
  452. * a corruption :(
  453. *
  454. * Let us verify that our chunk is
  455. * correct.
  456. */
  457. /* But first, let us allow the dust to settle down! */
  458. verify_chunk(tid, w_ptr, cur_sweep_id, prev_sweep_id);
  459. return 0;
  460. }
  461. /*
  462. * Compute the new pattern that we are going
  463. * to write to this location
  464. */
  465. *pattern_ptr = compute_store_pattern(tid, w_ptr, cur_sweep_id);
  466. /*
  467. * STORE: Now let us write this pattern into
  468. * the location
  469. */
  470. *w_ptr = *pattern_ptr;
  471. }
  472. }
  473. return NULL;
  474. }
  475. static unsigned long start_cpu = 0;
  476. static unsigned long nrthreads = 4;
  477. static pthread_t mem_snapshot_thread;
  478. static void *mem_snapshot_fn(void *arg)
  479. {
  480. int page_size = getpagesize();
  481. size_t size = page_size;
  482. void *tmp = malloc(size);
  483. while (!corruption_found && !timeout) {
  484. /* Stop memory migration once corruption is found */
  485. segv_wait = 1;
  486. mprotect(map1, size, PROT_READ);
  487. /*
  488. * Load from the working alias (map1). Loading from map2
  489. * also fails.
  490. */
  491. memcpy(tmp, map1, size);
  492. /*
  493. * Stores must go via map2 which has write permissions, but
  494. * the corrupted data tends to be seen in the snapshot buffer,
  495. * so corruption does not appear to be introduced at the
  496. * copy-back via map2 alias here.
  497. */
  498. memcpy(map2, tmp, size);
  499. /*
  500. * Before releasing other threads, must ensure the copy
  501. * back to
  502. */
  503. asm volatile("sync" ::: "memory");
  504. mprotect(map1, size, PROT_READ|PROT_WRITE);
  505. asm volatile("sync" ::: "memory");
  506. segv_wait = 0;
  507. usleep(1); /* This value makes a big difference */
  508. }
  509. return 0;
  510. }
  511. void alrm_sighandler(int sig)
  512. {
  513. timeout = 1;
  514. }
  515. int main(int argc, char *argv[])
  516. {
  517. int c;
  518. int page_size = getpagesize();
  519. time_t now;
  520. int i, dir_error;
  521. pthread_attr_t attr;
  522. key_t shm_key = (key_t) getpid();
  523. int shmid, run_time = 20 * 60;
  524. struct sigaction sa_alrm;
  525. snprintf(logdir, LOGDIR_NAME_SIZE,
  526. "/tmp/logdir-%u", (unsigned int)getpid());
  527. while ((c = getopt(argc, argv, "r:hn:l:t:")) != -1) {
  528. switch(c) {
  529. case 'r':
  530. start_cpu = strtoul(optarg, NULL, 10);
  531. break;
  532. case 'h':
  533. printf("%s [-r <start_cpu>] [-n <nrthreads>] [-l <logdir>] [-t <timeout>]\n", argv[0]);
  534. exit(0);
  535. break;
  536. case 'n':
  537. nrthreads = strtoul(optarg, NULL, 10);
  538. break;
  539. case 'l':
  540. strncpy(logdir, optarg, LOGDIR_NAME_SIZE - 1);
  541. break;
  542. case 't':
  543. run_time = strtoul(optarg, NULL, 10);
  544. break;
  545. default:
  546. printf("invalid option\n");
  547. exit(0);
  548. break;
  549. }
  550. }
  551. if (nrthreads > MAX_THREADS)
  552. nrthreads = MAX_THREADS;
  553. shmid = shmget(shm_key, page_size, IPC_CREAT|0666);
  554. if (shmid < 0) {
  555. err_msg("Failed shmget\n");
  556. }
  557. map1 = shmat(shmid, NULL, 0);
  558. if (map1 == (void *) -1) {
  559. err_msg("Failed shmat");
  560. }
  561. map2 = shmat(shmid, NULL, 0);
  562. if (map2 == (void *) -1) {
  563. err_msg("Failed shmat");
  564. }
  565. dir_error = mkdir(logdir, 0755);
  566. if (dir_error) {
  567. err_msg("Failed mkdir");
  568. }
  569. printf("start_cpu list:%lu\n", start_cpu);
  570. printf("number of worker threads:%lu + 1 snapshot thread\n", nrthreads);
  571. printf("Allocated address:0x%016lx + secondary map:0x%016lx\n", (unsigned long)map1, (unsigned long)map2);
  572. printf("logdir at : %s\n", logdir);
  573. printf("Timeout: %d seconds\n", run_time);
  574. time(&now);
  575. printf("=================================\n");
  576. printf(" Starting Test\n");
  577. printf(" %s", ctime(&now));
  578. printf("=================================\n");
  579. for (i = 0; i < nrthreads; i++) {
  580. if (1 && !fork()) {
  581. prctl(PR_SET_PDEATHSIG, SIGKILL);
  582. set_mycpu(start_cpu + i);
  583. for (;;)
  584. sched_yield();
  585. exit(0);
  586. }
  587. }
  588. sa_alrm.sa_handler = &alrm_sighandler;
  589. sigemptyset(&sa_alrm.sa_mask);
  590. sa_alrm.sa_flags = 0;
  591. if (sigaction(SIGALRM, &sa_alrm, 0) == -1) {
  592. err_msg("Failed signal handler registration\n");
  593. }
  594. alarm(run_time);
  595. pthread_attr_init(&attr);
  596. for (i = 0; i < nrthreads; i++) {
  597. rim_thread_ids[i] = i;
  598. pthread_create(&rim_threads[i], &attr, rim_fn, &rim_thread_ids[i]);
  599. set_pthread_cpu(rim_threads[i], start_cpu + i);
  600. }
  601. pthread_create(&mem_snapshot_thread, &attr, mem_snapshot_fn, map1);
  602. set_pthread_cpu(mem_snapshot_thread, start_cpu + i);
  603. pthread_join(mem_snapshot_thread, NULL);
  604. for (i = 0; i < nrthreads; i++) {
  605. pthread_join(rim_threads[i], NULL);
  606. }
  607. if (!timeout) {
  608. time(&now);
  609. printf("=================================\n");
  610. printf(" Data Corruption Detected\n");
  611. printf(" %s", ctime(&now));
  612. printf(" See logfiles in %s\n", logdir);
  613. printf("=================================\n");
  614. return 1;
  615. }
  616. return 0;
  617. }