aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/venti/srv/checkarenas.c
blob: 525a634cb2a345fc26cbed8c62471b9cabe858c9 (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
#include "stdinc.h"
#include "dat.h"
#include "fns.h"

static int	verbose;

static void
checkarena(Arena *arena, int scan, int fix)
{
	ATailStats old;
	int err, e;

	if(verbose && arena->memstats.clumps)
		printarena(2, arena);

	old = arena->memstats;

	if(scan){
		arena->memstats.used = 0;
		arena->memstats.clumps = 0;
		arena->memstats.cclumps = 0;
		arena->memstats.uncsize = 0;
	}

	err = 0;
	for(;;){
		e = syncarena(arena, 0, 1000, 0, fix);
		err |= e;
		if(!(e & SyncHeader))
			break;
		if(verbose && arena->memstats.clumps)
			fprint(2, ".");
	}
	if(verbose && arena->memstats.clumps)
		fprint(2, "\n");

	err &= ~SyncHeader;
	if(arena->memstats.used != old.used
	|| arena->memstats.clumps != old.clumps
	|| arena->memstats.cclumps != old.cclumps
	|| arena->memstats.uncsize != old.uncsize){
		fprint(2, "%s: incorrect arena header fields\n", arena->name);
		printarena(2, arena);
		err |= SyncHeader;
	}

	if(!err || !fix)
		return;

	fprint(2, "%s: writing fixed arena header fields\n", arena->name);
	arena->diskstats = arena->memstats;
	if(wbarena(arena) < 0)
		fprint(2, "arena header write failed: %r\n");
	flushdcache();
}

void
usage(void)
{
	fprint(2, "usage: checkarenas [-afv] file [arenaname...]\n");
	threadexitsall(0);
}

int
should(char *name, int argc, char **argv)
{
	int i;

	if(argc == 0)
		return 1;
	for(i=0; i<argc; i++)
		if(strcmp(name, argv[i]) == 0)
			return 1;
	return 0;
}

void
threadmain(int argc, char *argv[])
{
	ArenaPart *ap;
	Part *part;
	char *file;
	int i, fix, scan;

	ventifmtinstall();
	statsinit();

	fix = 0;
	scan = 0;
	ARGBEGIN{
	case 'f':
		fix++;
		break;
	case 'a':
		scan = 1;
		break;
	case 'v':
		verbose++;
		break;
	default:
		usage();
		break;
	}ARGEND

	if(!fix)
		readonly = 1;

	if(argc < 1)
		usage();

	file = argv[0];

	part = initpart(file, ORDWR|ODIRECT);
	if(part == nil)
		sysfatal("can't open partition %s: %r", file);

	ap = initarenapart(part);
	if(ap == nil)
		sysfatal("can't initialize arena partition in %s: %r", file);

	if(verbose > 1){
		printarenapart(2, ap);
		fprint(2, "\n");
	}

	initdcache(8 * MaxDiskBlock);

	for(i = 0; i < ap->narenas; i++)
		if(should(ap->arenas[i]->name, argc, argv))
			checkarena(ap->arenas[i], scan, fix);

	if(verbose > 1)
		printstats();
	threadexitsall(0);
}