Commit | Line | Data |
---|---|---|
7a887ffe AP |
1 | #include <stdio.h> |
2 | #include <stdlib.h> | |
7a887ffe AP |
3 | #include <math.h> |
4 | #include <string.h> | |
7a887ffe AP |
5 | |
6 | #include "../lib/lfsr.h" | |
48b944c4 JA |
7 | #include "../gettime.h" |
8 | #include "../fio_time.h" | |
7a887ffe | 9 | |
ca0a0b52 | 10 | static void usage(void) |
7a887ffe AP |
11 | { |
12 | printf("Usage: lfsr-test 0x<numbers> [seed] [spin] [verify]\n"); | |
13 | printf("-------------------------------------------------------------\n"); | |
14 | printf("*numbers: how many random numbers to produce (in hex)\n" | |
15 | "seed: initial value\n" | |
16 | "spin: how many iterations before we produce a number\n" | |
17 | "verify: check if LFSR has iterated correctly\n\n" | |
18 | "Only <numbers> is required. The rest are evaluated to 0 or false\n" | |
19 | "Elapsed/mean time and verification results are printed at the" | |
20 | "end of the test\n"); | |
21 | } | |
22 | ||
23 | int main(int argc, char *argv[]) | |
24 | { | |
25 | int r; | |
8b6a404c | 26 | struct timespec start, end; |
7a887ffe AP |
27 | struct fio_lfsr *fl; |
28 | int verify = 0; | |
29 | unsigned int spin = 0; | |
30 | uint64_t seed = 0; | |
31 | uint64_t numbers; | |
32 | uint64_t v_size; | |
33 | uint64_t i; | |
34 | void *v = NULL, *v_start; | |
35 | double total, mean; | |
36 | ||
71471cb1 JA |
37 | arch_init(argv); |
38 | ||
7a887ffe AP |
39 | /* Read arguments */ |
40 | switch (argc) { | |
41 | case 5: if (strncmp(argv[4], "verify", 7) == 0) | |
42 | verify = 1; | |
43 | case 4: spin = atoi(argv[3]); | |
44 | case 3: seed = atol(argv[2]); | |
45 | case 2: numbers = strtol(argv[1], NULL, 16); | |
46 | break; | |
47 | default: usage(); | |
48 | return 1; | |
49 | } | |
50 | ||
51 | /* Initialize LFSR */ | |
52 | fl = malloc(sizeof(struct fio_lfsr)); | |
53 | if (!fl) { | |
54 | perror("malloc"); | |
55 | return 1; | |
56 | } | |
57 | ||
58 | r = lfsr_init(fl, numbers, seed, spin); | |
59 | if (r) { | |
60 | printf("Initialization failed.\n"); | |
61 | return r; | |
62 | } | |
63 | ||
64 | /* Print specs */ | |
65 | printf("LFSR specs\n"); | |
66 | printf("==========================\n"); | |
67 | printf("Size is %u\n", 64 - __builtin_clzl(fl->cached_bit)); | |
761c2729 JA |
68 | printf("Max val is %lu\n", (unsigned long) fl->max_val); |
69 | printf("XOR-mask is 0x%lX\n", (unsigned long) fl->xormask); | |
70 | printf("Seed is %lu\n", (unsigned long) fl->last_val); | |
7a887ffe | 71 | printf("Spin is %u\n", fl->spin); |
761c2729 | 72 | printf("Cycle length is %lu\n", (unsigned long) fl->cycle_length); |
7a887ffe AP |
73 | |
74 | /* Create verification table */ | |
75 | if (verify) { | |
76 | v_size = numbers * sizeof(uint8_t); | |
77 | v = malloc(v_size); | |
78 | memset(v, 0, v_size); | |
4870138d | 79 | printf("\nVerification table is %lf KiB\n", (double)(v_size) / 1024); |
7a887ffe AP |
80 | } |
81 | v_start = v; | |
82 | ||
83 | /* | |
84 | * Iterate over a tight loop until we have produced all the requested | |
85 | * numbers. Verifying the results should introduce some small yet not | |
86 | * negligible overhead. | |
87 | */ | |
88 | fprintf(stderr, "\nTest initiated... "); | |
9077ee4a | 89 | fio_gettime(&start, NULL); |
dd9bd2b2 | 90 | while (!lfsr_next(fl, &i)) { |
7a887ffe AP |
91 | if (verify) |
92 | *(uint8_t *)(v + i) += 1; | |
93 | } | |
9077ee4a | 94 | fio_gettime(&end, NULL); |
7a887ffe AP |
95 | fprintf(stderr, "finished.\n"); |
96 | ||
d0f85362 | 97 | |
7a887ffe AP |
98 | /* Check if all expected numbers within range have been calculated */ |
99 | r = 0; | |
100 | if (verify) { | |
101 | fprintf(stderr, "Verifying results... "); | |
102 | for (i = 0; i < numbers; i++) { | |
d0f85362 AP |
103 | if (*(uint8_t *)(v + i) != 1) { |
104 | fprintf(stderr, "failed (%lu = %d).\n", | |
761c2729 JA |
105 | (unsigned long) i, |
106 | *(uint8_t *)(v + i)); | |
7a887ffe AP |
107 | r = 1; |
108 | break; | |
109 | } | |
110 | } | |
111 | if (!r) | |
112 | fprintf(stderr, "OK!\n"); | |
113 | } | |
114 | ||
115 | /* Calculate elapsed time and mean time per number */ | |
48b944c4 | 116 | total = utime_since(&start, &end); |
7a887ffe AP |
117 | mean = total / fl->num_vals; |
118 | ||
119 | printf("\nTime results "); | |
120 | if (verify) | |
121 | printf("(slower due to verification)"); | |
122 | printf("\n==============================\n"); | |
036c8019 | 123 | printf("Elapsed: %lf s\n", total / pow(10,6)); |
48b944c4 | 124 | printf("Mean: %lf us\n", mean); |
7a887ffe AP |
125 | |
126 | free(v_start); | |
127 | free(fl); | |
128 | return r; | |
129 | } |