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) | |
24010223 JA |
42 | verify = 1; |
43 | /* fall through */ | |
7a887ffe | 44 | case 4: spin = atoi(argv[3]); |
24010223 | 45 | /* fall through */ |
7a887ffe | 46 | case 3: seed = atol(argv[2]); |
24010223 | 47 | /* fall through */ |
7a887ffe AP |
48 | case 2: numbers = strtol(argv[1], NULL, 16); |
49 | break; | |
50 | default: usage(); | |
51 | return 1; | |
52 | } | |
53 | ||
54 | /* Initialize LFSR */ | |
55 | fl = malloc(sizeof(struct fio_lfsr)); | |
56 | if (!fl) { | |
57 | perror("malloc"); | |
58 | return 1; | |
59 | } | |
60 | ||
61 | r = lfsr_init(fl, numbers, seed, spin); | |
62 | if (r) { | |
63 | printf("Initialization failed.\n"); | |
64 | return r; | |
65 | } | |
66 | ||
67 | /* Print specs */ | |
68 | printf("LFSR specs\n"); | |
69 | printf("==========================\n"); | |
70 | printf("Size is %u\n", 64 - __builtin_clzl(fl->cached_bit)); | |
761c2729 JA |
71 | printf("Max val is %lu\n", (unsigned long) fl->max_val); |
72 | printf("XOR-mask is 0x%lX\n", (unsigned long) fl->xormask); | |
73 | printf("Seed is %lu\n", (unsigned long) fl->last_val); | |
7a887ffe | 74 | printf("Spin is %u\n", fl->spin); |
761c2729 | 75 | printf("Cycle length is %lu\n", (unsigned long) fl->cycle_length); |
7a887ffe AP |
76 | |
77 | /* Create verification table */ | |
78 | if (verify) { | |
79 | v_size = numbers * sizeof(uint8_t); | |
80 | v = malloc(v_size); | |
81 | memset(v, 0, v_size); | |
4870138d | 82 | printf("\nVerification table is %lf KiB\n", (double)(v_size) / 1024); |
7a887ffe AP |
83 | } |
84 | v_start = v; | |
85 | ||
86 | /* | |
87 | * Iterate over a tight loop until we have produced all the requested | |
88 | * numbers. Verifying the results should introduce some small yet not | |
89 | * negligible overhead. | |
90 | */ | |
91 | fprintf(stderr, "\nTest initiated... "); | |
9077ee4a | 92 | fio_gettime(&start, NULL); |
dd9bd2b2 | 93 | while (!lfsr_next(fl, &i)) { |
7a887ffe AP |
94 | if (verify) |
95 | *(uint8_t *)(v + i) += 1; | |
96 | } | |
9077ee4a | 97 | fio_gettime(&end, NULL); |
7a887ffe AP |
98 | fprintf(stderr, "finished.\n"); |
99 | ||
d0f85362 | 100 | |
7a887ffe AP |
101 | /* Check if all expected numbers within range have been calculated */ |
102 | r = 0; | |
103 | if (verify) { | |
104 | fprintf(stderr, "Verifying results... "); | |
105 | for (i = 0; i < numbers; i++) { | |
d0f85362 AP |
106 | if (*(uint8_t *)(v + i) != 1) { |
107 | fprintf(stderr, "failed (%lu = %d).\n", | |
761c2729 JA |
108 | (unsigned long) i, |
109 | *(uint8_t *)(v + i)); | |
7a887ffe AP |
110 | r = 1; |
111 | break; | |
112 | } | |
113 | } | |
114 | if (!r) | |
115 | fprintf(stderr, "OK!\n"); | |
116 | } | |
117 | ||
118 | /* Calculate elapsed time and mean time per number */ | |
48b944c4 | 119 | total = utime_since(&start, &end); |
7a887ffe AP |
120 | mean = total / fl->num_vals; |
121 | ||
122 | printf("\nTime results "); | |
123 | if (verify) | |
124 | printf("(slower due to verification)"); | |
125 | printf("\n==============================\n"); | |
036c8019 | 126 | printf("Elapsed: %lf s\n", total / pow(10,6)); |
48b944c4 | 127 | printf("Mean: %lf us\n", mean); |
7a887ffe AP |
128 | |
129 | free(v_start); | |
130 | free(fl); | |
131 | return r; | |
132 | } |