0872e29de7a2af3f720915405eb54e4b0f1e1851
[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 %u, %s\n", no_s, s->name);
23         printf("Completions: %llu\n", (unsigned long long) s->no_comps);
24         printf("Depth: %llu\n", (unsigned long long) s->depth);
25         printf("Number IOs: %llu\n", (unsigned long long) s->numberio);
26         printf("Index: %llu\n", (unsigned long long) s->index);
27
28         printf("Completions:\n");
29         for (i = 0; i < s->no_comps; i++) {
30                 printf("\t(file=%2llu) %llu\n",
31                                 (unsigned long long) s->comps[i].fileno,
32                                 (unsigned long long) s->comps[i].offset);
33         }
34 }
35
36 static void show(struct thread_io_list *s, size_t size)
37 {
38         int no_s;
39
40         no_s = 0;
41         do {
42                 int i;
43
44                 s->no_comps = le64_to_cpu(s->no_comps);
45                 s->depth = le32_to_cpu(s->depth);
46                 s->nofiles = le32_to_cpu(s->nofiles);
47                 s->numberio = le64_to_cpu(s->numberio);
48                 s->index = le64_to_cpu(s->index);
49
50                 for (i = 0; i < s->no_comps; i++) {
51                         s->comps[i].fileno = le64_to_cpu(s->comps[i].fileno);
52                         s->comps[i].offset = le64_to_cpu(s->comps[i].offset);
53                 }
54
55                 show_s(s, no_s);
56                 no_s++;
57                 size -= __thread_io_list_sz(s->depth, s->nofiles);
58                 s = (void *) s + __thread_io_list_sz(s->depth, s->nofiles);
59         } while (size != 0);
60 }
61
62 static void show_verify_state(void *buf, size_t size)
63 {
64         struct verify_state_hdr *hdr = buf;
65         struct thread_io_list *s;
66         uint32_t crc;
67
68         hdr->version = le64_to_cpu(hdr->version);
69         hdr->size = le64_to_cpu(hdr->size);
70         hdr->crc = le64_to_cpu(hdr->crc);
71
72         printf("Version: %x, Size %u, crc %x\n", (unsigned int) hdr->version,
73                                                 (unsigned int) hdr->size,
74                                                 (unsigned int) hdr->crc);
75
76         size -= sizeof(*hdr);
77         if (hdr->size != size) {
78                 log_err("Size mismatch\n");
79                 return;
80         }
81
82         s = buf + sizeof(*hdr);
83         crc = fio_crc32c((unsigned char *) s, hdr->size);
84         if (crc != hdr->crc) {
85                 log_err("crc mismatch %x != %x\n", crc, (unsigned int) hdr->crc);
86                 return;
87         }
88
89         if (hdr->version == 0x03)
90                 show(s, size);
91         else
92                 log_err("Unsupported version %d\n", (int) hdr->version);
93 }
94
95 int main(int argc, char *argv[])
96 {
97         struct stat sb;
98         void *buf;
99         int ret, fd;
100
101         debug_init();
102
103         if (argc < 2) {
104                 log_err("Usage: %s <state file>\n", argv[0]);
105                 return 1;
106         }
107
108         fd = open(argv[1], O_RDONLY);
109         if (fd < 0) {
110                 log_err("open %s: %s\n", argv[1], strerror(errno));
111                 return 1;
112         }
113
114         if (fstat(fd, &sb) < 0) {
115                 log_err("stat: %s\n", strerror(errno));
116                 close(fd);
117                 return 1;
118         }
119
120         buf = malloc(sb.st_size);
121         ret = read(fd, buf, sb.st_size);
122         if (ret < 0) {
123                 log_err("read: %s\n", strerror(errno));
124                 close(fd);
125                 return 1;
126         } else if (ret != sb.st_size) {
127                 log_err("Short read\n");
128                 close(fd);
129                 return 1;
130         }
131
132         close(fd);
133         show_verify_state(buf, sb.st_size);
134
135         free(buf);
136         return 0;
137 }