summaryrefslogtreecommitdiff
path: root/src/third_party/wiredtiger/test/thread/rw.c
blob: c6107a06c491f12f2e2e0d5c480fb900fd9ef3d2 (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
/*-
 * Public Domain 2014-2016 MongoDB, Inc.
 * Public Domain 2008-2014 WiredTiger, Inc.
 *
 * This is free and unencumbered software released into the public domain.
 *
 * Anyone is free to copy, modify, publish, use, compile, sell, or
 * distribute this software, either in source code form or as a compiled
 * binary, for any purpose, commercial or non-commercial, and by any
 * means.
 *
 * In jurisdictions that recognize copyright laws, the author or authors
 * of this software dedicate any and all copyright interest in the
 * software to the public domain. We make this dedication for the benefit
 * of the public at large and to the detriment of our heirs and
 * successors. We intend this dedication to be an overt act of
 * relinquishment in perpetuity of all present and future rights to this
 * software under copyright law.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#include "thread.h"

static void  print_stats(u_int);
static void *reader(void *);
static void *writer(void *);

typedef struct {
	char *name;				/* object name */
	u_int nops;				/* Thread op count */

	WT_RAND_STATE rnd;			/* RNG */

	int remove;				/* cursor.remove */
	int update;				/* cursor.update */
	int reads;				/* cursor.search */
} INFO;

static INFO *run_info;

int
rw_start(u_int readers, u_int writers)
{
	struct timeval start, stop;
	double seconds;
	pthread_t *tids;
	u_int i, name_index, offset, total_nops;
	int ret;
	void *thread_ret;

	tids = NULL;	/* Keep GCC 4.1 happy. */
	total_nops = 0;

	/* Create per-thread structures. */
	run_info = dcalloc((size_t)(readers + writers), sizeof(*run_info));
	tids = dcalloc((size_t)(readers + writers), sizeof(*tids));

	/* Create the files and load the initial records. */
	for (i = 0; i < writers; ++i) {
		if (i == 0 || multiple_files) {
			run_info[i].name = dmalloc(64);
			snprintf(run_info[i].name, 64, FNAME, i);

			/* Vary by orders of magnitude */
			if (vary_nops)
				run_info[i].nops = WT_MAX(1000, max_nops >> i);
			load(run_info[i].name);
		} else
			run_info[i].name = run_info[0].name;

		/* Setup op count if not varying ops. */
		if (run_info[i].nops == 0)
			run_info[i].nops = max_nops;
		total_nops += run_info[i].nops;
	}

	/* Setup the reader configurations */
	for (i = 0; i < readers; ++i) {
		offset = i + writers;
		if (multiple_files) {
			run_info[offset].name = dmalloc(64);
			/* Have readers read from tables with writes. */
			name_index = i % writers;
			snprintf(
			    run_info[offset].name, 64, FNAME, name_index);

			/* Vary by orders of magnitude */
			if (vary_nops)
				run_info[offset].nops =
				    WT_MAX(1000, max_nops >> name_index);
		} else
			run_info[offset].name = run_info[0].name;

		/* Setup op count if not varying ops. */
		if (run_info[offset].nops == 0)
			run_info[offset].nops = max_nops;
		total_nops += run_info[offset].nops;
	}

	(void)gettimeofday(&start, NULL);

	/* Create threads. */
	for (i = 0; i < readers; ++i)
		if ((ret = pthread_create(
		    &tids[i], NULL, reader, (void *)(uintptr_t)i)) != 0)
			testutil_die(ret, "pthread_create");
	for (; i < readers + writers; ++i) {
		if ((ret = pthread_create(
		    &tids[i], NULL, writer, (void *)(uintptr_t)i)) != 0)
			testutil_die(ret, "pthread_create");
	}

	/* Wait for the threads. */
	for (i = 0; i < readers + writers; ++i)
		(void)pthread_join(tids[i], &thread_ret);

	(void)gettimeofday(&stop, NULL);
	seconds = (stop.tv_sec - start.tv_sec) +
	    (stop.tv_usec - start.tv_usec) * 1e-6;
	fprintf(stderr, "timer: %.2lf seconds (%d ops/second)\n",
	    seconds, (int)(((readers + writers) * total_nops) / seconds));

	/* Verify the files. */
	for (i = 0; i < readers + writers; ++i) {
		verify(run_info[i].name);
		if (!multiple_files)
			break;
	}

	/* Output run statistics. */
	print_stats(readers + writers);

	/* Free allocated memory. */
	for (i = 0; i < readers + writers; ++i) {
		free(run_info[i].name);
		if (!multiple_files)
			break;
	}

	free(run_info);
	free(tids);

	return (0);
}

/*
 * reader_op --
 *	Read operation.
 */
static inline void
reader_op(WT_SESSION *session, WT_CURSOR *cursor, INFO *s)
{
	WT_ITEM *key, _key;
	u_int keyno;
	int ret;
	char keybuf[64];

	key = &_key;

	keyno = __wt_random(&s->rnd) % nkeys + 1;
	if (ftype == ROW) {
		key->data = keybuf;
		key->size = (uint32_t)
		    snprintf(keybuf, sizeof(keybuf), "%017u", keyno);
		cursor->set_key(cursor, key);
	} else
		cursor->set_key(cursor, (uint32_t)keyno);
	if ((ret = cursor->search(cursor)) != 0 && ret != WT_NOTFOUND)
		testutil_die(ret, "cursor.search");
	if (log_print)
		testutil_check(session->log_printf(session,
		    "Reader Thread %p key %017u", pthread_self(), keyno));
}

/*
 * reader --
 *	Reader thread start function.
 */
static void *
reader(void *arg)
{
	INFO *s;
	WT_CURSOR *cursor;
	WT_SESSION *session;
	u_int i;
	int id, ret;
	char tid[128];

	id = (int)(uintptr_t)arg;
	s = &run_info[id];
	__wt_thread_id(tid, sizeof(tid));
	__wt_random_init(&s->rnd);

	printf(" read thread %2d starting: tid: %s, file: %s\n",
	    id, tid, s->name);

	__wt_yield();		/* Get all the threads created. */

	if (session_per_op) {
		for (i = 0; i < s->nops; ++i, ++s->reads, __wt_yield()) {
			if ((ret = conn->open_session(
			    conn, NULL, NULL, &session)) != 0)
				testutil_die(ret, "conn.open_session");
			if ((ret = session->open_cursor(
			    session, s->name, NULL, NULL, &cursor)) != 0)
				testutil_die(ret, "session.open_cursor");
			reader_op(session, cursor, s);
			if ((ret = session->close(session, NULL)) != 0)
				testutil_die(ret, "session.close");
		}
	} else {
		if ((ret = conn->open_session(
		    conn, NULL, NULL, &session)) != 0)
			testutil_die(ret, "conn.open_session");
		if ((ret = session->open_cursor(
		    session, s->name, NULL, NULL, &cursor)) != 0)
			testutil_die(ret, "session.open_cursor");
		for (i = 0; i < s->nops; ++i, ++s->reads, __wt_yield())
			reader_op(session, cursor, s);
		if ((ret = session->close(session, NULL)) != 0)
			testutil_die(ret, "session.close");
	}

	printf(" read thread %2d stopping: tid: %s, file: %s\n",
	    id, tid, s->name);

	return (NULL);
}

/*
 * writer_op --
 *	Write operation.
 */
static inline void
writer_op(WT_SESSION *session, WT_CURSOR *cursor, INFO *s)
{
	WT_ITEM *key, _key, *value, _value;
	u_int keyno;
	int ret;
	char keybuf[64], valuebuf[64];

	key = &_key;
	value = &_value;

	keyno = __wt_random(&s->rnd) % nkeys + 1;
	if (ftype == ROW) {
		key->data = keybuf;
		key->size = (uint32_t)
		    snprintf(keybuf, sizeof(keybuf), "%017u", keyno);
		cursor->set_key(cursor, key);
	} else
		cursor->set_key(cursor, (uint32_t)keyno);
	if (keyno % 5 == 0) {
		++s->remove;
		if ((ret =
		    cursor->remove(cursor)) != 0 && ret != WT_NOTFOUND)
			testutil_die(ret, "cursor.remove");
	} else {
		++s->update;
		value->data = valuebuf;
		if (ftype == FIX)
			cursor->set_value(cursor, 0x10);
		else {
			value->size = (uint32_t)snprintf(
			    valuebuf, sizeof(valuebuf), "XXX %37u", keyno);
			cursor->set_value(cursor, value);
		}
		if ((ret = cursor->update(cursor)) != 0)
			testutil_die(ret, "cursor.update");
	}
	if (log_print)
		testutil_check(session->log_printf(session,
		    "Writer Thread %p key %017u", pthread_self(), keyno));
}

/*
 * writer --
 *	Writer thread start function.
 */
static void *
writer(void *arg)
{
	INFO *s;
	WT_CURSOR *cursor;
	WT_SESSION *session;
	u_int i;
	int id, ret;
	char tid[128];

	id = (int)(uintptr_t)arg;
	s = &run_info[id];
	__wt_thread_id(tid, sizeof(tid));
	__wt_random_init(&s->rnd);

	printf("write thread %2d starting: tid: %s, file: %s\n",
	    id, tid, s->name);

	__wt_yield();		/* Get all the threads created. */

	if (session_per_op) {
		for (i = 0; i < s->nops; ++i, __wt_yield()) {
			if ((ret = conn->open_session(
			    conn, NULL, NULL, &session)) != 0)
				testutil_die(ret, "conn.open_session");
			if ((ret = session->open_cursor(
			    session, s->name, NULL, NULL, &cursor)) != 0)
				testutil_die(ret, "session.open_cursor");
			writer_op(session, cursor, s);
			if ((ret = session->close(session, NULL)) != 0)
				testutil_die(ret, "session.close");
		}
	} else {
		if ((ret = conn->open_session(
		    conn, NULL, NULL, &session)) != 0)
			testutil_die(ret, "conn.open_session");
		if ((ret = session->open_cursor(
		    session, s->name, NULL, NULL, &cursor)) != 0)
			testutil_die(ret, "session.open_cursor");
		for (i = 0; i < s->nops; ++i, __wt_yield())
			writer_op(session, cursor, s);
		if ((ret = session->close(session, NULL)) != 0)
			testutil_die(ret, "session.close");
	}

	printf("write thread %2d stopping: tid: %s, file: %s\n",
	    id, tid, s->name);

	return (NULL);
}

/*
 * print_stats --
 *	Display reader/writer thread stats.
 */
static void
print_stats(u_int nthreads)
{
	INFO *s;
	u_int id;

	s = run_info;
	for (id = 0; id < nthreads; ++id, ++s)
		printf("%3d: read %6d, remove %6d, update %6d\n",
		    id, s->reads, s->remove, s->update);
}