aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/venti/venti.c
blob: 4c533cc626c782d719e4792aff43b28e218eee7d (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include "stdinc.h"
#include "dat.h"
#include "fns.h"

#include "whack.h"

int debug;
int mainstacksize = 256*1024;

static void	ventiserver(char *vaddr);

void
threadmain(int argc, char *argv[])
{
	char *config, *haddr, *vaddr;
	u32int mem, icmem, bcmem, minbcmem;

	vaddr = "tcp!*!venti";
	haddr = nil;
	config = nil;
	mem = 0xffffffffUL;
	icmem = 0;
	bcmem = 0;
	ARGBEGIN{
	case 'a':
		vaddr = ARGF();
		if(vaddr == nil)
			goto usage;
		break;
	case 'B':
		bcmem = unittoull(ARGF());
		break;
	case 'c':
		config = ARGF();
		if(config == nil)
			goto usage;
		break;
	case 'C':
		mem = unittoull(ARGF());
		break;
	case 'd':
		debug = 1;
		break;
	case 'h':
		haddr = ARGF();
		if(haddr == nil)
			goto usage;
		break;
	case 'I':
		icmem = unittoull(ARGF());
		break;
	case 'w':
		queuewrites = 1;
		break;
	default:
		goto usage;
	}ARGEND

print("whack %d\n", sizeof(Whack));

	if(argc){
  usage:
		fprint(2, "usage: venti [-dw] [-a ventiaddress] [-h httpaddress] [-c config] [-C cachesize] [-I icachesize] [-B blockcachesize]\n");
		threadexitsall("usage");
	}

	fmtinstall('V', vtscorefmt);
	fmtinstall('H', encodefmt);
	fmtinstall('F', vtfcallfmt);

	if(config == nil)
		config = "venti.conf";


	if(initarenasum() < 0)
		fprint(2, "warning: can't initialize arena summing process: %r");

	if(initventi(config) < 0)
		sysfatal("can't init server: %r");

	if(mem == 0xffffffffUL)
		mem = 1 * 1024 * 1024;
	fprint(2, "initialize %d bytes of lump cache for %d lumps\n",
		mem, mem / (8 * 1024));
	initlumpcache(mem, mem / (8 * 1024));

	icmem = u64log2(icmem / (sizeof(IEntry)+sizeof(IEntry*)) / ICacheDepth);
	if(icmem < 4)
		icmem = 4;
	fprint(2, "initialize %d bytes of index cache for %d index entries\n",
		(sizeof(IEntry)+sizeof(IEntry*)) * (1 << icmem) * ICacheDepth,
		(1 << icmem) * ICacheDepth);
	initicache(icmem, ICacheDepth);

	/*
	 * need a block for every arena and every process
	 */
	minbcmem = maxblocksize * 
		(mainindex->narenas + mainindex->nsects*4 + 16);
	if(bcmem < minbcmem)
		bcmem = minbcmem;

	fprint(2, "initialize %d bytes of disk block cache\n", bcmem);
	initdcache(bcmem);

	fprint(2, "sync arenas and index...\n");
	if(syncindex(mainindex, 1) < 0)
		sysfatal("can't sync server: %r");

	if(queuewrites){
		fprint(2, "initialize write queue...\n");
		if(initlumpqueues(mainindex->nsects) < 0){
			fprint(2, "can't initialize lump queues,"
				" disabling write queueing: %r");
			queuewrites = 0;
		}
	}

	if(haddr){
		fprint(2, "starting http server at %s\n", haddr);
		if(httpdinit(haddr) < 0)
			fprint(2, "warning: can't start http server: %r");
	}

	ventiserver(vaddr);
	threadexitsall(0);
}

static void
vtrerror(VtReq *r, char *error)
{
	r->rx.type = VtRerror;
	r->rx.error = estrdup(error);
}

static void
ventiserver(char *addr)
{
	Packet *p;
	VtReq *r;
	VtSrv *s;
	char err[ERRMAX];

	s = vtlisten(addr);
	if(s == nil)
		sysfatal("can't announce %s: %r", addr);

	while((r = vtgetreq(s)) != nil){
		r->rx.type = r->tx.type+1;
	//	print("req (arenas[0]=%p sects[0]=%p) %F\n",
	//		mainindex->arenas[0], mainindex->sects[0], &r->tx);
		switch(r->tx.type){
		default:
			vtrerror(r, "unknown request");
			break;
		case VtTread:
			if((r->rx.data = readlump(r->tx.score, r->tx.dtype, r->tx.count)) == nil){
				rerrstr(err, sizeof err);
				vtrerror(r, err);
			}
			break;
		case VtTwrite:
			p = r->tx.data;
			r->tx.data = nil;
			if(writelump(p, r->rx.score, r->tx.dtype, 0) < 0){
				rerrstr(err, sizeof err);
				vtrerror(r, err);
			}
			break;
		case VtTsync:
			flushqueue();
			flushdcache();
			break;
		}
		vtrespond(r);
	}
}