summaryrefslogtreecommitdiff
path: root/t/verify-state.c
blob: 734c1e4c77fa81c791f62d4e784f060d4256c6a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 * Dump the contents of a verify state file in plain text
 */
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include "../log.h"
#include "../os/os.h"
#include "../verify-state.h"
#include "../crc/crc32c.h"
#include "debug.h"

static void show_s(struct thread_io_list *s, unsigned int no_s)
{
	int i;

	printf("Thread:\t\t%u\n", no_s);
	printf("Name:\t\t%s\n", s->name);
	printf("Completions:\t%llu\n", (unsigned long long) s->no_comps);
	printf("Depth:\t\t%llu\n", (unsigned long long) s->depth);
	printf("Number IOs:\t%llu\n", (unsigned long long) s->numberio);
	printf("Index:\t\t%llu\n", (unsigned long long) s->index);

	printf("Completions:\n");
	if (!s->no_comps)
		return;
	for (i = s->no_comps - 1; i >= 0; i--) {
		printf("\t(file=%2llu) %llu\n",
				(unsigned long long) s->comps[i].fileno,
				(unsigned long long) s->comps[i].offset);
	}
}

static void show(struct thread_io_list *s, size_t size)
{
	int no_s;

	no_s = 0;
	do {
		int i;

		s->no_comps = le64_to_cpu(s->no_comps);
		s->depth = le32_to_cpu(s->depth);
		s->nofiles = le32_to_cpu(s->nofiles);
		s->numberio = le64_to_cpu(s->numberio);
		s->index = le64_to_cpu(s->index);

		for (i = 0; i < s->no_comps; i++) {
			s->comps[i].fileno = le64_to_cpu(s->comps[i].fileno);
			s->comps[i].offset = le64_to_cpu(s->comps[i].offset);
		}

		show_s(s, no_s);
		no_s++;
		size -= __thread_io_list_sz(s->depth, s->nofiles);
		s = (struct thread_io_list *)((char *) s +
			__thread_io_list_sz(s->depth, s->nofiles));
	} while (size != 0);
}

static void show_verify_state(void *buf, size_t size)
{
	struct verify_state_hdr *hdr = buf;
	struct thread_io_list *s;
	uint32_t crc;

	hdr->version = le64_to_cpu(hdr->version);
	hdr->size = le64_to_cpu(hdr->size);
	hdr->crc = le64_to_cpu(hdr->crc);

	printf("Version:\t0x%x\n", (unsigned int) hdr->version);
	printf("Size:\t\t%u\n", (unsigned int) hdr->size);
	printf("CRC:\t\t0x%x\n", (unsigned int) hdr->crc);

	size -= sizeof(*hdr);
	if (hdr->size != size) {
		log_err("Size mismatch\n");
		return;
	}

	s = buf + sizeof(*hdr);
	crc = fio_crc32c((unsigned char *) s, hdr->size);
	if (crc != hdr->crc) {
		log_err("crc mismatch %x != %x\n", crc, (unsigned int) hdr->crc);
		return;
	}

	if (hdr->version == 0x03)
		show(s, size);
	else
		log_err("Unsupported version %d\n", (int) hdr->version);
}

static int show_file(const char *file)
{
	struct stat sb;
	void *buf;
	int ret, fd;

	fd = open(file, O_RDONLY);
	if (fd < 0) {
		log_err("open %s: %s\n", file, strerror(errno));
		return 1;
	}

	if (fstat(fd, &sb) < 0) {
		log_err("stat: %s\n", strerror(errno));
		close(fd);
		return 1;
	}

	buf = malloc(sb.st_size);
	ret = read(fd, buf, sb.st_size);
	if (ret < 0) {
		log_err("read: %s\n", strerror(errno));
		close(fd);
		free(buf);
		return 1;
	} else if (ret != sb.st_size) {
		log_err("Short read\n");
		close(fd);
		free(buf);
		return 1;
	}

	close(fd);
	show_verify_state(buf, sb.st_size);

	free(buf);
	return 0;
}

int main(int argc, char *argv[])
{
	int i, ret;

	debug_init();

	if (argc < 2) {
		log_err("Usage: %s <state file>\n", argv[0]);
		return 1;
	}

	ret = 0;
	for (i = 1; i < argc; i++) {
		ret = show_file(argv[i]);
		if (ret)
			break;
	}

	return ret;
}