configure: Add <linux/blkzoned.h> test
[fio.git] / t / lfsr-test.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <string.h>
5
6 #include "../lib/lfsr.h"
7 #include "../gettime.h"
8 #include "../fio_time.h"
9
10 static void usage(void)
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;
26         struct timespec start, end;
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
37         arch_init(argv);
38
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));
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);
71         printf("Spin is         %u\n", fl->spin);
72         printf("Cycle length is %lu\n", (unsigned long) fl->cycle_length);
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);
79                 printf("\nVerification table is %lf KiB\n", (double)(v_size) / 1024);
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... ");
89         fio_gettime(&start, NULL);
90         while (!lfsr_next(fl, &i)) {
91                 if (verify)
92                         *(uint8_t *)(v + i) += 1;
93         }
94         fio_gettime(&end, NULL);
95         fprintf(stderr, "finished.\n");
96
97
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++) {
103                         if (*(uint8_t *)(v + i) != 1) {
104                                 fprintf(stderr, "failed (%lu = %d).\n",
105                                                 (unsigned long) i,
106                                                 *(uint8_t *)(v + i));
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 */
116         total = utime_since(&start, &end);
117         mean = total / fl->num_vals;
118
119         printf("\nTime results ");
120         if (verify)
121                 printf("(slower due to verification)");
122         printf("\n==============================\n");
123         printf("Elapsed: %lf s\n", total / pow(10,6));
124         printf("Mean:    %lf us\n", mean);
125
126         free(v_start);
127         free(fl);
128         return r;
129 }