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