t/fio-verify-state: allow multiple input files
[fio.git] / t / verify-state.c
1 /*
2  * Dump the contents of a verify state file in plain text
3  */
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <string.h>
12 #include "../log.h"
13 #include "../os/os.h"
14 #include "../verify-state.h"
15 #include "../crc/crc32c.h"
16 #include "debug.h"
17
18 static void show_s(struct thread_io_list *s, unsigned int no_s)
19 {
20         int i;
21
22         printf("Thread:\t\t%u\n", no_s);
23         printf("Name:\t\t%s\n", s->name);
24         printf("Completions:\t%llu\n", (unsigned long long) s->no_comps);
25         printf("Depth:\t\t%llu\n", (unsigned long long) s->depth);
26         printf("Number IOs:\t%llu\n", (unsigned long long) s->numberio);
27         printf("Index:\t\t%llu\n", (unsigned long long) s->index);
28
29         printf("Completions:\n");
30         for (i = 0; i < s->no_comps; i++) {
31                 printf("\t(file=%2llu) %llu\n",
32                                 (unsigned long long) s->comps[i].fileno,
33                                 (unsigned long long) s->comps[i].offset);
34         }
35 }
36
37 static void show(struct thread_io_list *s, size_t size)
38 {
39         int no_s;
40
41         no_s = 0;
42         do {
43                 int i;
44
45                 s->no_comps = le64_to_cpu(s->no_comps);
46                 s->depth = le32_to_cpu(s->depth);
47                 s->nofiles = le32_to_cpu(s->nofiles);
48                 s->numberio = le64_to_cpu(s->numberio);
49                 s->index = le64_to_cpu(s->index);
50
51                 for (i = 0; i < s->no_comps; i++) {
52                         s->comps[i].fileno = le64_to_cpu(s->comps[i].fileno);
53                         s->comps[i].offset = le64_to_cpu(s->comps[i].offset);
54                 }
55
56                 show_s(s, no_s);
57                 no_s++;
58                 size -= __thread_io_list_sz(s->depth, s->nofiles);
59                 s = (void *) s + __thread_io_list_sz(s->depth, s->nofiles);
60         } while (size != 0);
61 }
62
63 static void show_verify_state(void *buf, size_t size)
64 {
65         struct verify_state_hdr *hdr = buf;
66         struct thread_io_list *s;
67         uint32_t crc;
68
69         hdr->version = le64_to_cpu(hdr->version);
70         hdr->size = le64_to_cpu(hdr->size);
71         hdr->crc = le64_to_cpu(hdr->crc);
72
73         printf("Version:\t0x%x\n", (unsigned int) hdr->version);
74         printf("Size:\t\t%u\n", (unsigned int) hdr->size);
75         printf("CRC:\t\t0x%x\n", (unsigned int) hdr->crc);
76
77         size -= sizeof(*hdr);
78         if (hdr->size != size) {
79                 log_err("Size mismatch\n");
80                 return;
81         }
82
83         s = buf + sizeof(*hdr);
84         crc = fio_crc32c((unsigned char *) s, hdr->size);
85         if (crc != hdr->crc) {
86                 log_err("crc mismatch %x != %x\n", crc, (unsigned int) hdr->crc);
87                 return;
88         }
89
90         if (hdr->version == 0x03)
91                 show(s, size);
92         else
93                 log_err("Unsupported version %d\n", (int) hdr->version);
94 }
95
96 static int show_file(const char *file)
97 {
98         struct stat sb;
99         void *buf;
100         int ret, fd;
101
102         fd = open(file, O_RDONLY);
103         if (fd < 0) {
104                 log_err("open %s: %s\n", file, strerror(errno));
105                 return 1;
106         }
107
108         if (fstat(fd, &sb) < 0) {
109                 log_err("stat: %s\n", strerror(errno));
110                 close(fd);
111                 return 1;
112         }
113
114         buf = malloc(sb.st_size);
115         ret = read(fd, buf, sb.st_size);
116         if (ret < 0) {
117                 log_err("read: %s\n", strerror(errno));
118                 close(fd);
119                 return 1;
120         } else if (ret != sb.st_size) {
121                 log_err("Short read\n");
122                 close(fd);
123                 return 1;
124         }
125
126         close(fd);
127         show_verify_state(buf, sb.st_size);
128
129         free(buf);
130         return 0;
131 }
132
133 int main(int argc, char *argv[])
134 {
135         int i, ret;
136
137         debug_init();
138
139         if (argc < 2) {
140                 log_err("Usage: %s <state file>\n", argv[0]);
141                 return 1;
142         }
143
144         ret = 0;
145         for (i = 1; i < argc; i++) {
146                 ret = show_file(argv[i]);
147                 if (ret)
148                         break;
149         }
150
151         return ret;
152 }