summaryrefslogtreecommitdiffstats
path: root/tools/regression/geom/geom_kernsim.c
blob: 8b05154b65b101e1906c5859b0f56a6e2761daa2 (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
/*-
 * Copyright (c) 2002 Poul-Henning Kamp
 * Copyright (c) 2002 Networks Associates Technology, Inc.
 * All rights reserved.
 *
 * This software was developed for the FreeBSD Project by Poul-Henning Kamp
 * and NAI Labs, the Security Research Division of Network Associates, Inc.
 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
 * DARPA CHATS research program.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The names of the authors may not be used to endorse or promote
 *    products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */


#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <sched.h>
#include <err.h>
#include <sys/errno.h>
#include <sys/time.h>
#include <geom/geom.h>
#include <geom/geom_int.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <machine/atomic.h>

#include <pthread.h>

#define NTHREAD 30
static struct thread thr[NTHREAD];

static int weredone;
int g_debugflags;
int bootverbose = 1;

void
done(void)
{
	
	rattle();
	weredone = 1;
	exit(0);
}

void
secrethandshake()
{
	int i;

	if (weredone)
		exit(0);
	for (i = 0; i < NTHREAD; i++) {
		if (thr[i].pid == 0)
			continue;
		if (kill(thr[i].pid, 0)) {
			printf("\n\nMissing process for thread %d (\"%s\")\n",
				i, thr[i].name);
			goto scram;
		}
	}
	return;

scram:
	for (i = 0; i < NTHREAD; i++) {
		if (thr[i].pid == 0)
			continue;
		if (thr[i].pid == getpid())
			continue;
		kill(thr[i].pid, 9);
	}
	exit(9);
}


void
biodone(struct bio *bp)
{
	bp->bio_flags |= BIO_DONE;
	if (bp->bio_done != NULL)
		bp->bio_done(bp);
}

int
biowait(struct bio *bp, const char *wchan __unused)
{

	while ((bp->bio_flags & BIO_DONE) == 0)
		usleep(10000);
	if (!(bp->bio_flags & BIO_ERROR))
		return (0);
	if (bp->bio_error)
		return (bp->bio_error);
	return (EIO);
}

void
wakeup(void *chan)
{
	
	if (chan == &g_wait_event)
		pthread_cond_signal(&ptc_event);
	else if (chan == &g_wait_up)
		pthread_cond_signal(&ptc_up);
	else if (chan == &g_wait_down)
		pthread_cond_signal(&ptc_down);
	else
	return;
}

int hz;

int
tsleep(void *chan __unused, int pri __unused, const char *wmesg __unused, int timo)
{

	usleep(timo);
	return (0);
}

void
rattle()
{
	int i;

	pthread_yield();

	for (;;) {
		i = pthread_mutex_trylock(&ptm_up);
		if (i != EBUSY) {
			i = pthread_mutex_trylock(&ptm_down);
			if (i != EBUSY) {
				i = pthread_mutex_trylock(&ptm_event);
				if (i != EBUSY)
					break;
				pthread_mutex_unlock(&ptm_down);
			}
			pthread_mutex_unlock(&ptm_up);
		}
		usleep(100000);
	}
	pthread_mutex_unlock(&ptm_event);
	pthread_mutex_unlock(&ptm_down);
	pthread_mutex_unlock(&ptm_up);
	return;
}


void
new_thread(void *(*func)(void *arg), const char *name)
{
	struct thread *tp;
	static int nextt;
	int error;

	tp = thr + nextt++;
	error = pthread_create(&tp->tid, NULL, func, tp);
	if (error)
		err(1, "pthread_create(%s)", name);
	tp->name = strdup(name);
	printf("New Thread %d %s %p %d\n", tp - thr, name, tp, tp->pid);
}

#define FASCIST 0

void *
g_malloc(int size, int flags)
{
	void *p;

#if FASCIST
	p = malloc(4096);
	printf("Malloc %p \n", p);
#else
	p = malloc(size);
#endif
	if (flags & M_ZERO)
		memset(p, 0, size);
	else
		memset(p, 0xd0, size);
	return (p);
}

void
g_free(void *ptr)
{

	secrethandshake();
#if FASCIST
	printf("Free %p \n", ptr);
	munmap(ptr, 4096);
#else
	free(ptr);
#endif
}

static pthread_mutex_t pt_topology;

void
g_init(void)
{

	pthread_mutex_init(&pt_topology, NULL);
	g_io_init();
	g_event_init();
}

void
g_topology_lock()
{

	pthread_mutex_lock(&pt_topology);
}

void
g_topology_unlock()
{

	pthread_mutex_unlock(&pt_topology);
}

void
g_topology_assert()
{

#if 0
	if (topology_lock == getpid())
		return;
	KASSERT(1 == 0, ("Lacking topology_lock"));
#endif
}

void
mtx_init(struct mtx *mp, const char *bla __unused, const char *yak __unused, int foo __unused)
{

	pthread_mutex_init((pthread_mutex_t *)&mp->mtx_object, NULL);
}

void
mtx_destroy(struct mtx *mp)
{

	pthread_mutex_destroy((pthread_mutex_t *)&mp->mtx_object);
}

void
mtx_lock(struct mtx *mp)
{

	pthread_mutex_lock((pthread_mutex_t *)&mp->mtx_object);
}

void
mtx_unlock(struct mtx *mp)
{

	pthread_mutex_unlock((pthread_mutex_t *)&mp->mtx_object);
}

struct mtx Giant;

OpenPOWER on IntegriCloud