aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/fossil/bwatch.c
blob: 8cd0956ee391ddd43fcbfd0ed395d76f4be681fa (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#include "stdinc.h"
#include "dat.h"
#include "fns.h"
#include "error.h"

/*
 * Lock watcher.  Check that locking of blocks is always down.
 *
 * This is REALLY slow, and it won't work when the blocks aren't
 * arranged in a tree (e.g., after the first snapshot).  But it's great
 * for debugging.
 */
enum
{
	MaxLock = 16,
	HashSize = 1009,
};

/*
 * Thread-specific watch state.
 */
typedef struct WThread WThread;
struct WThread
{
	Block *b[MaxLock];	/* blocks currently held */
	uint nb;
	uint pid;
};

typedef struct WMap WMap;
typedef struct WEntry WEntry;

struct WEntry
{
	uchar c[VtScoreSize];
	uchar p[VtScoreSize];
	int off;

	WEntry *cprev;
	WEntry *cnext;
	WEntry *pprev;
	WEntry *pnext;
};

struct WMap
{
	QLock lk;

	WEntry *hchild[HashSize];
	WEntry *hparent[HashSize];
};

static WMap map;
static void **wp;
static uint blockSize;
static WEntry *pool;
uint bwatchDisabled;

static uint
hash(uchar score[VtScoreSize])
{
	uint i, h;

	h = 0;
	for(i=0; i<VtScoreSize; i++)
		h = h*37 + score[i];
	return h%HashSize;
}

#include <pool.h>
static void
freeWEntry(WEntry *e)
{
	memset(e, 0, sizeof(WEntry));
	e->pnext = pool;
	pool = e;
}

static WEntry*
allocWEntry(void)
{
	int i;
	WEntry *w;

	w = pool;
	if(w == nil){
		w = vtmallocz(1024*sizeof(WEntry));
		for(i=0; i<1024; i++)
			freeWEntry(&w[i]);
		w = pool;
	}
	pool = w->pnext;
	memset(w, 0, sizeof(WEntry));
	return w;
}

/*
 * remove all dependencies with score as a parent
 */
static void
_bwatchResetParent(uchar *score)
{
	WEntry *w, *next;
	uint h;

	h = hash(score);
	for(w=map.hparent[h]; w; w=next){
		next = w->pnext;
		if(memcmp(w->p, score, VtScoreSize) == 0){
			if(w->pnext)
				w->pnext->pprev = w->pprev;
			if(w->pprev)
				w->pprev->pnext = w->pnext;
			else
				map.hparent[h] = w->pnext;
			if(w->cnext)
				w->cnext->cprev = w->cprev;
			if(w->cprev)
				w->cprev->cnext = w->cnext;
			else
				map.hchild[hash(w->c)] = w->cnext;
			freeWEntry(w);
		}
	}
}
/*
 * and child 
 */
static void
_bwatchResetChild(uchar *score)
{
	WEntry *w, *next;
	uint h;

	h = hash(score);
	for(w=map.hchild[h]; w; w=next){
		next = w->cnext;
		if(memcmp(w->c, score, VtScoreSize) == 0){
			if(w->pnext)
				w->pnext->pprev = w->pprev;
			if(w->pprev)
				w->pprev->pnext = w->pnext;
			else
				map.hparent[hash(w->p)] = w->pnext;
			if(w->cnext)
				w->cnext->cprev = w->cprev;
			if(w->cprev)
				w->cprev->cnext = w->cnext;
			else
				map.hchild[h] = w->cnext;
			freeWEntry(w);
		}
	}
}

static uchar*
parent(uchar c[VtScoreSize], int *off)
{
	WEntry *w;
	uint h;

	h = hash(c);
	for(w=map.hchild[h]; w; w=w->cnext)
		if(memcmp(w->c, c, VtScoreSize) == 0){
			*off = w->off;
			return w->p;
		}
	return nil;
}

static void
addChild(uchar p[VtEntrySize], uchar c[VtEntrySize], int off)
{
	uint h;
	WEntry *w;

	w = allocWEntry();
	memmove(w->p, p, VtScoreSize);
	memmove(w->c, c, VtScoreSize);
	w->off = off;

	h = hash(p);
	w->pnext = map.hparent[h];
	if(w->pnext)
		w->pnext->pprev = w;
	map.hparent[h] = w;

	h = hash(c);
	w->cnext = map.hchild[h];
	if(w->cnext)
		w->cnext->cprev = w;
	map.hchild[h] = w;
}

void
bwatchReset(uchar score[VtScoreSize])
{
	qlock(&map.lk);
	_bwatchResetParent(score);
	_bwatchResetChild(score);
	qunlock(&map.lk);
}

void
bwatchInit(void)
{
	wp = privalloc();
	*wp = nil;
}

void
bwatchSetBlockSize(uint bs)
{
	blockSize = bs;
}

static WThread*
getWThread(void)
{
	WThread *w;

	w = *wp;
	if(w == nil || w->pid != getpid()){
		w = vtmallocz(sizeof(WThread));
		*wp = w;
		w->pid = getpid();
	}
	return w;
}

/*
 * Derive dependencies from the contents of b.
 */
void
bwatchDependency(Block *b)
{
	int i, epb, ppb;
	Entry e;

	if(bwatchDisabled)
		return;

	qlock(&map.lk);
	_bwatchResetParent(b->score);

	switch(b->l.type){
	case BtData:
		break;

	case BtDir:
		epb = blockSize / VtEntrySize;
		for(i=0; i<epb; i++){
			entryUnpack(&e, b->data, i);
			if(!(e.flags & VtEntryActive))
				continue;
			addChild(b->score, e.score, i);
		}
		break;

	default:
		ppb = blockSize / VtScoreSize;
		for(i=0; i<ppb; i++)
			addChild(b->score, b->data+i*VtScoreSize, i);
		break;
	}
	qunlock(&map.lk);
}

static int
depth(uchar *s)
{
	int d, x;

	d = -1;
	while(s){
		d++;
		s = parent(s, &x);
	}
	return d;
}

static int
lockConflicts(uchar xhave[VtScoreSize], uchar xwant[VtScoreSize])
{
	uchar *have, *want;
	int havedepth, wantdepth, havepos, wantpos;

	have = xhave;
	want = xwant;

	havedepth = depth(have);
	wantdepth = depth(want);

	/*
	 * walk one or the other up until they're both
 	 * at the same level.
	 */
	havepos = -1;
	wantpos = -1;
	have = xhave;
	want = xwant;
	while(wantdepth > havedepth){
		wantdepth--;
		want = parent(want, &wantpos);
	}
	while(havedepth > wantdepth){
		havedepth--;
		have = parent(have, &havepos);
	}

	/*
	 * walk them up simultaneously until we reach
	 * a common ancestor.
	 */
	while(have && want && memcmp(have, want, VtScoreSize) != 0){
		have = parent(have, &havepos);
		want = parent(want, &wantpos);
	}

	/*
	 * not part of same tree.  happens mainly with
	 * newly allocated blocks.
	 */
	if(!have || !want)
		return 0;

	/*
	 * never walked want: means we want to lock
	 * an ancestor of have.  no no.
	 */
	if(wantpos == -1)
		return 1;

	/*
	 * never walked have: means we want to lock a
	 * child of have.  that's okay.
	 */
	if(havepos == -1)
		return 0;

	/*
	 * walked both: they're from different places in the tree.
	 * require that the left one be locked before the right one.
	 * (this is questionable, but it puts a total order on the block tree).
	 */
	return havepos < wantpos;
}

static void
stop(void)
{
	int fd;
	char buf[32];

	snprint(buf, sizeof buf, "#p/%d/ctl", getpid());
	fd = open(buf, OWRITE);
	write(fd, "stop", 4);
	close(fd);
}

/*
 * Check whether the calling thread can validly lock b.
 * That is, check that the calling thread doesn't hold
 * locks for any of b's children.
 */
void
bwatchLock(Block *b)
{
	int i;
	WThread *w;

	if(bwatchDisabled)
		return;

	if(b->part != PartData)
		return;

	qlock(&map.lk);
	w = getWThread();
	for(i=0; i<w->nb; i++){
		if(lockConflicts(w->b[i]->score, b->score)){
			fprint(2, "%d: have block %V; shouldn't lock %V\n",
				w->pid, w->b[i]->score, b->score);
			stop();
		}
	}
	qunlock(&map.lk);
	if(w->nb >= MaxLock){
		fprint(2, "%d: too many blocks held\n", w->pid);
		stop();
	}else
		w->b[w->nb++] = b;
}

/*
 * Note that the calling thread is about to unlock b.
 */
void
bwatchUnlock(Block *b)
{
	int i;
	WThread *w;

	if(bwatchDisabled)
		return;

	if(b->part != PartData)
		return;

	w = getWThread();
	for(i=0; i<w->nb; i++)
		if(w->b[i] == b)
			break;
	if(i>=w->nb){
		fprint(2, "%d: unlock of unlocked block %V\n", w->pid, b->score);
		stop();
	}else
		w->b[i] = w->b[--w->nb];
}