summaryrefslogtreecommitdiff
path: root/bench/wtperf/wtperf.c
blob: ebe0f4fa1447d8fc0a813d56a5854f20bd17627c (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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
/*-
 * Public Domain 2008-2013 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.
 *
 * wtperf.c
 *	This is an application that executes parallel random read workload.
 */

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <math.h>
#include <stdlib.h>
#include <sys/time.h>
#include <pthread.h>
#include <inttypes.h>
#include <unistd.h>
#include <stddef.h>
#include <ctype.h>
#include <limits.h>

#include <wiredtiger.h>
#include <wiredtiger_ext.h>

#define	ATOMIC_ADD(v, val)						\
	__sync_add_and_fetch(&(v), val)
#ifndef F_CLR
#define	F_CLR(p, mask)		((p)->flags &= ~((uint32_t)(mask)))
#endif
#ifndef F_ISSET
#define	F_ISSET(p, mask)	((p)->flags & ((uint32_t)(mask)))
#endif
#ifndef F_SET
#define	F_SET(p, mask)		((p)->flags |= ((uint32_t)(mask)))
#endif

typedef struct {
	const char *home;
	WT_CONNECTION *conn;
	FILE *logf;
#define	WT_PERF_INIT		0x00
#define	WT_PERF_POP		0x01
#define	WT_PERF_READ		0x02
	uint32_t phase;
#define PERF_INSERT_RMW		0x01
#define PERF_RAND_PARETO	0x02 /* Use the Pareto random distribution. */
#define PERF_RAND_WORKLOAD	0x04
	uint32_t flags;
	struct timeval phase_start_time;
	uint32_t rand_range; /* The range to use if doing random inserts. */
	uint32_t elapsed_time;

	/* Fields changeable on command line are listed in wtperf_opt.i */
#define OPT_DECLARE_STRUCT
#include "wtperf_opt.i"
#undef OPT_DECLARE_STRUCT

} CONFIG;

typedef enum {
	UINT32_TYPE, STRING_TYPE, BOOL_TYPE, FLAG_TYPE
} CONFIG_OPT_TYPE;

typedef struct {
	const char *name;
	CONFIG_OPT_TYPE type;
	size_t offset;
	uint32_t flagmask;
} CONFIG_OPT;

/* Al options changeable on command line using -o or -O are listed here. */
CONFIG_OPT config_opts[] = {

#define OPT_DEFINE_DESC
#include "wtperf_opt.i"
#undef OPT_DEFINE_DESC

};

/* Forward function definitions. */
int execute_populate(CONFIG *);
int execute_workload(CONFIG *);
int get_next_op(uint64_t *);
int lprintf(CONFIG *cfg, int err, uint32_t level, const char *fmt, ...)
#ifdef __GNUC__
    __attribute__((format (printf, 4, 5)))
#endif
;
void *checkpoint_worker(void *);
int find_table_count(CONFIG *);
void *insert_thread(void *);
int connection_reconfigure(WT_CONNECTION *, const char *);
void config_assign(CONFIG *, const CONFIG *);
void config_free(CONFIG *);
int config_opt(CONFIG *, WT_CONFIG_ITEM *, WT_CONFIG_ITEM *);
int config_opt_str(CONFIG *, WT_SESSION *, const char *, const char *);
int config_opt_int(CONFIG *, WT_SESSION *, const char *, const char *);
int config_opt_file(CONFIG *, WT_SESSION *, const char *);
int config_opt_line(CONFIG *, WT_SESSION *, const char *);
void *populate_thread(void *);
void print_config(CONFIG *);
void *read_thread(void *);
int setup_log_file(CONFIG *);
int start_threads(CONFIG *, u_int, pthread_t **, void *(*func)(void *));
void *stat_worker(void *);
int stop_threads(CONFIG *, u_int, pthread_t *);
char *strstr_right(const char *str, const char *match, const char **rightp);
void *update_thread(void *);
void usage(void);
void worker(CONFIG *, uint32_t);
void wtperf_srand(CONFIG *);
uint64_t wtperf_rand(CONFIG *);
uint64_t wtperf_value_range(CONFIG *);

#define	DEFAULT_LSM_CONFIG						\
	"key_format=S,value_format=S,exclusive,"			\
	"leaf_page_max=4kb,internal_page_max=64kb,allocation_size=4kb,"

/* Worker thread types. */
#define WORKER_READ		0x01
#define WORKER_INSERT		0x02
#define WORKER_INSERT_RMW	0x03
#define WORKER_UPDATE		0x04

/* Default values. */
CONFIG default_cfg = {
	"WT_TEST",	/* home */
	NULL,		/* conn */
	NULL,		/* logf */
	WT_PERF_INIT, /* phase */
	0,		/* flags */
	{0, 0},		/* phase_start_time */
	0,		/* rand_range */
	0,		/* elapsed_time */

#define OPT_DEFINE_DEFAULT
#include "wtperf_opt.i"
#undef OPT_DEFINE_DEFAULT

};

const char *small_config_str =
    "conn_config=\"create,cache_size=500MB\","
    "table_config=\"" DEFAULT_LSM_CONFIG "lsm_chunk_size=5MB,\","
    "icount=500000,"
    "data_sz=100,"
    "key_sz=20,"
    "report_interval=5,"
    "run_time=20,"
    "populate_threads=1,"
    "read_threads=8,";

const char *med_config_str =
    "conn_config=\"create,cache_size=1GB\","
    "table_config=\"" DEFAULT_LSM_CONFIG "lsm_chunk_size=20MB,\","
    "icount=50000000,"
    "data_sz=100,"
    "key_sz=20,"
    "report_interval=5,"
    "run_time=100,"
    "populate_threads=1,"
    "read_threads=16,";

const char *large_config_str =
    "conn_config=\"create,cache_size=2GB\","
    "table_config=\"" DEFAULT_LSM_CONFIG "lsm_chunk_size=50MB,\","
    "icount=500000000,"
    "data_sz=100,"
    "key_sz=20,"
    "report_interval=5,"
    "run_time=600,"
    "populate_threads=1,"
    "read_threads=16,";


const char *debug_cconfig = "verbose=[lsm]";
const char *debug_tconfig = "";

/* Global values shared by threads. */
/*
 * g_nins_ops is used to track both insert count and assign keys, so use this
 * to track insert failures.
 */
uint64_t g_nfailedins_ops; 
uint64_t g_nins_ops;
uint64_t g_npop_ops;
uint64_t g_nread_ops;
uint64_t g_nupdate_ops;
uint64_t g_nworker_ops;
int g_running;
int g_util_running;
uint32_t g_threads_quit; /* For tracking threads that exit early. */

/* End global values shared by threads. */

void *
read_thread(void *arg)
{
	worker((CONFIG *)arg, WORKER_READ);
	return (NULL);
}

void *
insert_thread(void *arg)
{
	CONFIG *config;

	config = (CONFIG *)arg;
	worker(config, F_ISSET(config, PERF_INSERT_RMW) ?
	    WORKER_INSERT_RMW : WORKER_INSERT);
	return (NULL);
}

void *
update_thread(void *arg)
{
	worker((CONFIG *)arg, WORKER_UPDATE);
	return (NULL);
}

void
worker(CONFIG *cfg, uint32_t worker_type)
{
	WT_CONNECTION *conn;
	WT_SESSION *session;
	WT_CURSOR *cursor;
	const char *op_name = "search";
	char *data_buf, *key_buf, *value;
	int ret, op_ret;
	uint64_t next_incr, next_val;

	session = NULL;
	data_buf = key_buf = NULL;
	op_ret = 0;

	conn = cfg->conn;
	key_buf = calloc(cfg->key_sz + 1, 1);
	if (key_buf == NULL) {
		lprintf(cfg, ret = ENOMEM, 0, "Populate key buffer");
		goto err;
	}
	if (worker_type == WORKER_INSERT || worker_type == WORKER_UPDATE) {
		data_buf = calloc(cfg->data_sz, 1);
		if (data_buf == NULL) {
			lprintf(cfg, ret = ENOMEM, 0, "Populate data buffer");
			goto err;
		}
		memset(data_buf, 'a', cfg->data_sz - 1);
	}

	if ((ret = conn->open_session(conn, NULL, NULL, &session)) != 0) {
		lprintf(cfg, ret, 0,
		    "open_session failed in read thread");
		goto err;
	}
	if ((ret = session->open_cursor(session, cfg->uri,
	    NULL, NULL, &cursor)) != 0) {
		lprintf(cfg, ret, 0,
		    "open_cursor failed in read thread");
		goto err;
	}

	while (g_running) {
		/* Get a value in range, avoid zero. */
		if (worker_type == WORKER_INSERT)
			next_incr = ATOMIC_ADD(g_nins_ops, 1);

		if (!F_ISSET(cfg, PERF_RAND_WORKLOAD) &&
		    worker_type == WORKER_INSERT)
			next_val = cfg->icount + next_incr;
		else
			next_val = wtperf_rand(cfg);
		/*
		 * If the workload is started without a populate phase we
		 * rely on at least one insert to get a valid item id.
		 */
		if (worker_type != WORKER_INSERT &&
		    wtperf_value_range(cfg) < next_val)
			continue;
		sprintf(key_buf, "%0*" PRIu64, cfg->key_sz, next_val);
		cursor->set_key(cursor, key_buf);
		switch(worker_type) {
		case WORKER_READ:
			op_name = "read";
			op_ret = cursor->search(cursor);
			if (F_ISSET(cfg, PERF_RAND_WORKLOAD) &&
			    op_ret == WT_NOTFOUND)
				op_ret = 0;
			if (op_ret == 0)
				++g_nread_ops;
			break;
		case WORKER_INSERT_RMW:
			op_name="insert_rmw";
			op_ret = cursor->search(cursor);
			if (op_ret != WT_NOTFOUND)
				break;
			/* Fall through */
		case WORKER_INSERT:
			op_name = "insert";
			cursor->set_value(cursor, data_buf);
			op_ret = cursor->insert(cursor);
			if (F_ISSET(cfg, PERF_RAND_WORKLOAD) &&
			    op_ret == WT_DUPLICATE_KEY)
				op_ret = 0;
			if (op_ret != 0)
				++g_nfailedins_ops;
			break;
		case WORKER_UPDATE:
			op_name = "update";
			op_ret = cursor->search(cursor);
			if (op_ret == 0) {
				cursor->get_value(cursor, &value);
				memcpy(data_buf, value, cfg->data_sz);
				if (data_buf[0] == 'a')
					data_buf[0] = 'b';
				else
					data_buf[0] = 'a';
				cursor->set_value(cursor, data_buf);
				op_ret = cursor->update(cursor);
			}
			if (F_ISSET(cfg, PERF_RAND_WORKLOAD) &&
			    op_ret == WT_NOTFOUND)
				op_ret = 0;
			if (op_ret == 0)
				++g_nupdate_ops;
			break;
		default:
			lprintf(cfg, EINVAL, 0, "Invalid worker type");
			goto err;
		}

		/* Report errors and continue. */
		if (op_ret != 0)
			lprintf(cfg, op_ret, 0,
			    "%s failed for: %s", op_name, key_buf);
		else
			++g_nworker_ops;
	}

err:	if (ret != 0)
		++g_threads_quit;
	if (session != NULL)
		session->close(session, NULL);
	if (data_buf != NULL)
		free(data_buf);
	if (key_buf != NULL)
		free(key_buf);
}

/* Retrieve an ID for the next insert operation. */
int get_next_op(uint64_t *op)
{
	*op = ATOMIC_ADD(g_npop_ops, 1);
	return (0);
}

void *
populate_thread(void *arg)
{
	CONFIG *cfg;
	WT_CONNECTION *conn;
	WT_CURSOR *cursor;
	WT_SESSION *session;
	char *data_buf, *key_buf;
	int ret;
	uint64_t op;

	cfg = (CONFIG *)arg;
	conn = cfg->conn;
	session = NULL;
	data_buf = key_buf = NULL;
	ret = 0;

	cfg->phase = WT_PERF_POP;

	data_buf = calloc(cfg->data_sz, 1);
	if (data_buf == NULL) {
		lprintf(cfg, ENOMEM, 0, "Populate data buffer");
		goto err;
	}
	key_buf = calloc(cfg->key_sz + 1, 1);
	if (key_buf == NULL) {
		lprintf(cfg, ENOMEM, 0, "Populate key buffer");
		goto err;
	}

	/* Open a session for the current thread's work. */
	if ((ret = conn->open_session(conn, NULL, NULL, &session)) != 0) {
		lprintf(cfg, ret, 0,
		    "Error opening a session on %s", cfg->home);
		goto err;
	}

	/* Do a bulk load if populate is single-threaded. */
	if ((ret = session->open_cursor(
	    session, cfg->uri, NULL,
	    cfg->populate_threads == 1 ? "bulk" : NULL,
	    &cursor)) != 0) {
		lprintf(cfg, ret, 0, "Error opening cursor %s", cfg->uri);
		goto err;
	}

	memset(data_buf, 'a', cfg->data_sz - 1);
	cursor->set_value(cursor, data_buf);
	/* Populate the database. */
	while (1) {
		get_next_op(&op);
		if (op > cfg->icount)
			break;
		sprintf(key_buf, "%0*" PRIu64, cfg->key_sz, op);
		cursor->set_key(cursor, key_buf);
		if ((ret = cursor->insert(cursor)) != 0) {
			lprintf(cfg, ret, 0, "Failed inserting");
			goto err;
		}
	}
	/* To ensure managing thread knows if we exited early. */
err:	if (ret != 0)
		++g_threads_quit;
	if (session != NULL)
		session->close(session, NULL);
	if (data_buf)
		free(data_buf);
	if (key_buf)
		free(key_buf);
	return (arg);
}

void *
stat_worker(void *arg)
{
	CONFIG *cfg;
	WT_CONNECTION *conn;
	WT_CURSOR *cursor;
	WT_SESSION *session;
	const char *desc, *pvalue;
	char *stat_uri;
	double secs;
	int ret;
	size_t uri_len;
	struct timeval e;
	uint32_t i;
	uint64_t value;

	session = NULL;
	cfg = (CONFIG *)arg;
	conn = cfg->conn;
	stat_uri = NULL;

	if ((ret = conn->open_session(conn, NULL, NULL, &session)) != 0) {
		lprintf(cfg, ret, 0,
		    "open_session failed in statistics thread.");
		goto err;
	}

	uri_len = strlen("statistics:") + strlen(cfg->uri) + 1;
	if ((stat_uri = malloc(uri_len)) == NULL) {
		lprintf(cfg, ENOMEM, 0, "Statistics thread uri create.");
		goto err;
	}
	(void)snprintf(stat_uri, uri_len, "statistics:%s", cfg->uri);

	while (g_util_running) {
		/* Break the sleep up, so we notice interrupts faster. */
		for (i = 0; i < cfg->stat_interval; i++) {
			sleep(cfg->report_interval);
			if (!g_util_running)
				break;
		}
		/* Generic header. */
		lprintf(cfg, 0, cfg->verbose,
		    "=======================================");
		gettimeofday(&e, NULL);
		secs = e.tv_sec + e.tv_usec / 1000000.0;
		secs -= (cfg->phase_start_time.tv_sec +
		    cfg->phase_start_time.tv_usec / 1000000.0);
		if (secs == 0)
			++secs;
		if (cfg->phase == WT_PERF_POP)
			lprintf(cfg, 0, cfg->verbose,
			    "inserts: %" PRIu64 ", elapsed time: %.2f",
			    g_npop_ops, secs);
		else
			lprintf(cfg, 0, cfg->verbose,
			    "reads: %" PRIu64 " inserts: %" PRIu64
			    " updates: %" PRIu64 ", elapsed time: %.2f",
			    g_nread_ops, g_nins_ops, g_nupdate_ops, secs);

		/* Report data source stats. */
		if ((ret = session->open_cursor(session, stat_uri,
		    NULL, "statistics_fast", &cursor)) != 0) {
			lprintf(cfg, ret, 0,
			    "open_cursor failed for data source statistics");
			goto err;
		}
		while ((ret = cursor->next(cursor)) == 0 && (ret =
		    cursor->get_value(cursor, &desc, &pvalue, &value)) == 0 &&
		    value != 0)
			lprintf(cfg, 0, cfg->verbose,
			    "stat:lsm: %s=%s", desc, pvalue);
		cursor->close(cursor);
		lprintf(cfg, 0, cfg->verbose, "-----------------");

		/* Dump the connection statistics since last time. */
		if ((ret = session->open_cursor(session, "statistics:",
		    NULL, "statistics_clear", &cursor)) != 0) {
			lprintf(cfg, ret, 0,
			    "open_cursor failed in statistics");
			goto err;
		}
		while ((ret = cursor->next(cursor)) == 0 && (ret =
		    cursor->get_value(cursor, &desc, &pvalue, &value)) == 0 &&
		    value != 0)
			lprintf(cfg, 0, cfg->verbose,
			    "stat:conn: %s=%s", desc, pvalue);
		cursor->close(cursor);
	}
err:	if (session != NULL)
		session->close(session, NULL);
	if (stat_uri != NULL)
		free(stat_uri);
	return (arg);
}

void *
checkpoint_worker(void *arg)
{
	CONFIG *cfg;
	WT_CONNECTION *conn;
	WT_SESSION *session;
	int ret;
	struct timeval e, s;
	uint32_t i;
	uint64_t ms;

	session = NULL;
	cfg = (CONFIG *)arg;
	conn = cfg->conn;

	if ((ret = conn->open_session(conn, NULL, NULL, &session)) != 0) {
		lprintf(cfg, ret, 0,
		    "open_session failed in checkpoint thread.");
		goto err;
	}

	while (g_util_running) {
		/*
		 * TODO: do we care how long the checkpoint takes?
		 */
		/* Break the sleep up, so we notice interrupts faster. */
		for (i = 0; i < cfg->checkpoint_interval; i++) {
			sleep(cfg->report_interval);
			if (!g_util_running)
				break;
		}

		gettimeofday(&s, NULL);
		if ((ret = session->checkpoint(session, NULL)) != 0)
			/* Report errors and continue. */
			lprintf(cfg, ret, 0, "Checkpoint failed.");
		gettimeofday(&e, NULL);
		ms = (e.tv_sec * 1000) + (e.tv_usec / 1000.0);
		ms -= (s.tv_sec * 1000) + (s.tv_usec / 1000.0);
		lprintf(cfg, 0, 1,
		    "Finished checkpoint in %" PRIu64 " ms.", ms);
	}
err:	if (session != NULL)
		session->close(session, NULL);
	return (arg);
}

int execute_populate(CONFIG *cfg)
{
	WT_CONNECTION *conn;
	WT_SESSION *session;
	pthread_t *threads;
	double secs;
	int ret;
	uint64_t elapsed, last_ops;
	struct timeval e;

	conn = cfg->conn;
	cfg->phase = WT_PERF_POP;
	lprintf(cfg, 0, 1, "Starting populate threads");

	/* First create the table. */
	if ((ret = conn->open_session(conn, NULL, NULL, &session)) != 0) {
		lprintf(cfg, ret, 0,
		    "Error opening a session on %s", cfg->home);
		return (ret);
	}

	if ((ret = session->create(
	    session, cfg->uri, cfg->table_config)) != 0) {
		lprintf(cfg, ret, 0, "Error creating table %s", cfg->uri);
		session->close(session, NULL);
		return (ret);
	}
	session->close(session, NULL);

	if ((ret = start_threads(
	    cfg, cfg->populate_threads, &threads, populate_thread)) != 0)
		return (ret);

	gettimeofday(&cfg->phase_start_time, NULL);
	for (cfg->elapsed_time = 0, elapsed = last_ops = 0;
	    g_npop_ops < cfg->icount &&
	    g_threads_quit < cfg->populate_threads;) {
		/*
		 * Sleep for 100th of a second, report_interval is in second
		 * granularity, so adjust accordingly.
		 */
		usleep(10000);
		elapsed += 1;
		if (elapsed % 100 == 0 &&
		    (elapsed / 100) % cfg->report_interval == 0) {
			lprintf(cfg, 0, 1, "%" PRIu64 " ops in %d secs",
			    g_npop_ops - last_ops, cfg->report_interval);
			last_ops = g_npop_ops;
		}
	}
	if (g_threads_quit == cfg->populate_threads) {
		lprintf(cfg, WT_ERROR, 0,
		    "Populate threads exited without finishing.");
		return (WT_ERROR);
	}
	gettimeofday(&e, NULL);

	if ((ret = stop_threads(cfg, cfg->populate_threads, threads)) != 0)
		return (ret);

	lprintf(cfg, 0, 1,
	    "Finished load of %d items", cfg->icount);
	secs = e.tv_sec + e.tv_usec / 1000000.0;
	secs -= (cfg->phase_start_time.tv_sec +
	    cfg->phase_start_time.tv_usec / 1000000.0);
	if (secs == 0)
		++secs;
	lprintf(cfg, 0, 1,
	    "Load time: %.2f\n" "load ops/sec: %.2f",
	    secs, cfg->icount / secs);

	return (0);
}

int execute_workload(CONFIG *cfg)
{
	pthread_t *ithreads, *rthreads, *uthreads;
	int ret;
	uint64_t last_inserts, last_reads, last_updates;

	cfg->phase = WT_PERF_READ;
	last_inserts = last_reads = last_updates = 0;
	lprintf(cfg, 0, 1, "Starting read threads");

	if (cfg->read_threads != 0 && (ret = start_threads(
	    cfg, cfg->read_threads, &rthreads, read_thread)) != 0)
		return (ret);

	if (cfg->insert_threads != 0 && (ret = start_threads(
	    cfg, cfg->insert_threads, &ithreads, insert_thread)) != 0)
		return (ret);

	if (cfg->update_threads != 0 && (ret = start_threads(
	    cfg, cfg->update_threads, &uthreads, update_thread)) != 0)
		return (ret);

	/* Sanity check reporting interval. */
	if (cfg->report_interval > cfg->run_time)
		cfg->report_interval = cfg->run_time;

	gettimeofday(&cfg->phase_start_time, NULL);
	for (cfg->elapsed_time = 0;
	    cfg->elapsed_time < cfg->run_time &&
	    g_threads_quit < cfg->read_threads;
	    cfg->elapsed_time += cfg->report_interval) {
		sleep(cfg->report_interval);
		lprintf(cfg, 0, 1,
		    "%" PRIu64 " reads, %" PRIu64 " inserts, %" PRIu64
		    " updates in %d secs",
		    g_nread_ops - last_reads,
		    g_nins_ops - last_inserts,
		    g_nupdate_ops - last_updates,
		    cfg->report_interval);
		last_reads = g_nread_ops;
		last_inserts = g_nins_ops;
		last_updates = g_nupdate_ops;
	}
	/* Report if any worker threads didn't finish. */
	if (g_threads_quit != 0)
		lprintf(cfg, WT_ERROR, 0,
		    "Worker thread(s) exited without finishing.");

	if (cfg->read_threads != 0 &&
	    (ret = stop_threads(cfg, cfg->read_threads, rthreads)) != 0)
		return (ret);

	if (cfg->insert_threads != 0 &&
	    (ret = stop_threads(cfg, cfg->insert_threads, ithreads)) != 0)
		return (ret);

	if (cfg->update_threads != 0 &&
	    (ret = stop_threads(cfg, cfg->update_threads, uthreads)) != 0)
		return (ret);

	return (0);
}

/*
 * Ensure that icount matches the number of records in the 
 * existing table.
 */
int find_table_count(CONFIG *cfg)
{
	WT_CONNECTION *conn;
	WT_CURSOR *cursor;
	WT_SESSION *session;
	char *key;
	int ret;

	conn = cfg->conn;

	if ((ret = conn->open_session(conn, NULL, NULL, &session)) != 0) {
		lprintf(cfg, ret, 0,
		    "open_session failed finding existing table count");
		goto err;
	}
	if ((ret = session->open_cursor(session, cfg->uri,
	    NULL, NULL, &cursor)) != 0) {
		lprintf(cfg, ret, 0,
		    "open_cursor failed finding existing table count");
		goto err;
	}
	if ((ret = cursor->prev(cursor)) != 0) {
		lprintf(cfg, ret, 0,
		    "cursor prev failed finding existing table count");
		goto err;
	}
	cursor->get_key(cursor, &key);
	cfg->icount = (uint32_t)atoi(key);

err:	session->close(session, NULL);
	return (ret);
}

/* Same as strstr, but also returns the right boundary of the match if found */
char *strstr_right(const char *str, const char *match, const char **rightp)
{
	char *result;

	if ((result = strstr(str, match)) != NULL)
		*rightp = result + strlen(match);
	else
		*rightp = NULL;
	return result;
}

/* Strip out any create parameter before reconfiguring */
int connection_reconfigure(WT_CONNECTION *conn, const char *orig)
{
	char *alloced;
	const char *config;
	const char *left;
	const char *right;
	int ret;

	alloced = NULL;
	if ((left = strstr_right(orig, ",create,", &right)) != NULL ||
	    (left = strstr_right(orig, "create,", &right)) == orig ||
	    ((left = strstr_right(orig, ",create", &right)) != NULL &&
		right == &orig[strlen(orig)])) {
		size_t alloclen;
		size_t leftlen;

		leftlen = left - orig;
		alloclen = leftlen + strlen(right) + 1;
		alloced = malloc(alloclen);
		strncpy(alloced, orig, leftlen);
		strncpy(&alloced[leftlen], right, alloclen - leftlen);
		config = alloced;
	}
	else
		config = orig;

	ret = conn->reconfigure(conn, config);
	if (alloced != NULL)
		free(alloced);
	return (ret);
}


int main(int argc, char **argv)
{
	CONFIG cfg;
	WT_CONNECTION *conn;
	WT_SESSION *parse_session;
	const char *user_cconfig, *user_tconfig;
	const char *opts = "C:I:O:P:R:U:T:c:d:eh:i:jk:l:m:o:r:ps:t:u:v:SML";
	char *cc_buf, *tc_buf;
	int ch, checkpoint_created, ret, stat_created;
	pthread_t checkpoint, stat;
	uint64_t req_len;

	/* Setup the default configuration values. */
	memset(&cfg, 0, sizeof(cfg));
	config_assign(&cfg, &default_cfg);
	cc_buf = tc_buf = NULL;
	user_cconfig = user_tconfig = NULL;
	conn = NULL;
	checkpoint_created = stat_created = 0;
	parse_session = NULL;

	/*
	 * First do a basic validation of options,
	 * and home is needed before open.
	 */
	while ((ch = getopt(argc, argv, opts)) != EOF)
		switch (ch) {
		case 'h':
			cfg.home = optarg;
			break;
		case '?':
			fprintf(stderr, "Invalid option\n");
			usage();
			return (EINVAL);
		}

	/*
	 * We do the open now, since we'll need a connection and
	 * session to use the extension config parser.  We will
	 * reconfigure later as needed.
	 */
	if ((ret = wiredtiger_open(
	    cfg.home, NULL, "create,cache_size=1M", &conn)) != 0) {
		lprintf(&cfg, ret, 0, "Error connecting to %s", cfg.home);
		goto err;
	}

	if ((ret = conn->open_session(conn, NULL, NULL, &parse_session)) != 0) {
		lprintf(&cfg, ret, 0, "Error creating session");
		goto err;
	}

	/*
	 * Then parse different config structures - other options override
	 * fields within the structure.
	 */
	optind = 1;
	while ((ch = getopt(argc, argv, opts)) != EOF)
		switch (ch) {
		case 'S':
			if (config_opt_line(&cfg,
				parse_session, small_config_str) != 0)
				return (EINVAL);
			break;
		case 'M':
			if (config_opt_line(&cfg,
				parse_session, med_config_str) != 0)
				return (EINVAL);
			break;
		case 'L':
			if (config_opt_line(&cfg,
				parse_session, large_config_str) != 0)
				return (EINVAL);
			break;
		case 'O':
			if (config_opt_file(&cfg,
				parse_session, optarg) != 0)
				return (EINVAL);
			break;
		default:
			/* Validation is provided on the next parse. */
			break;
		}

	/* Parse other options */
	optind = 1;
	while ((ch = getopt(argc, argv, opts)) != EOF)
		switch (ch) {
		case 'd':
			config_opt_int(&cfg, parse_session, "data_sz", optarg);
			break;
		case 'c':
			config_opt_int(&cfg, parse_session,
			    "checkpoint_interval", optarg);
			break;
		case 'e':
			cfg.create = 0;
			break;
		case 'h':
			/* handled above */
			break;
		case 'i':
			config_opt_int(&cfg, parse_session, "icount", optarg);
			break;
		case 'j':
			F_SET(&cfg, PERF_INSERT_RMW);
			break;
		case 'k':
			config_opt_int(&cfg, parse_session, "key_sz", optarg);
			break;
		case 'l':
			config_opt_int(&cfg, parse_session,
			    "stat_interval", optarg);
			break;
		case 'm':
			F_SET(&cfg, PERF_RAND_WORKLOAD);
			config_opt_int(&cfg, parse_session,
			    "rand_range", optarg);
			if (cfg.rand_range == 0) {
				fprintf(stderr, "Invalid random range.\n");
				usage();
				return (EINVAL);
			}
				
			break;
		case 'o':
			/* Allow -o key=value */
			if (config_opt_line(&cfg,
				parse_session, optarg) != 0)
				return (EINVAL);
			break;
		case 'p':
			F_SET(&cfg, PERF_RAND_PARETO);
			break;
		case 'r':
			config_opt_int(&cfg, parse_session, "run_time", optarg);
			break;
		case 's':
			config_opt_int(&cfg, parse_session,
			    "rand_seed", optarg);
			break;
		case 't':
			config_opt_int(&cfg, parse_session,
			    "report_interval", optarg);
			break;
		case 'u':
			config_opt_str(&cfg, parse_session, "uri", optarg);
			break;
		case 'v':
			config_opt_int(&cfg, parse_session, "verbose", optarg);
			break;
		case 'C':
			user_cconfig = optarg;
			break;
		case 'I':
			config_opt_int(&cfg, parse_session,
			    "insert_threads", optarg);
			break;
		case 'P':
			config_opt_int(&cfg, parse_session,
			    "populate_threads", optarg);
			break;
		case 'R':
			config_opt_int(&cfg, parse_session,
			    "read_threads", optarg);
			break;
		case 'U':
			config_opt_int(&cfg, parse_session,
			    "update_threads", optarg);
			break;
		case 'T':
			user_tconfig = optarg;
			break;
		case 'O':
		case 'L':
		case 'M':
		case 'S':
			break;
		case '?':
		default:
			fprintf(stderr, "Invalid option\n");
			usage();
			return (EINVAL);
		}

	if (cfg.rand_range > 0 && !F_ISSET(&cfg, PERF_RAND_WORKLOAD)) {
		fprintf(stderr, "rand_range cannot be set without "
		    "random workload\n");
		return (EINVAL);
	}

	if ((ret = setup_log_file(&cfg)) != 0)
		goto err;

	/* Make stdout line buffered, so verbose output appears quickly. */
	(void)setvbuf(stdout, NULL, _IOLBF, 0);

	/* Concatenate non-default configuration strings. */
	if (cfg.verbose > 1 || user_cconfig != NULL) {
		req_len = strlen(cfg.conn_config) + strlen(debug_cconfig) + 3;
		if (user_cconfig != NULL)
			req_len += strlen(user_cconfig);
		cc_buf = calloc(req_len, 1);
		if (cc_buf == NULL) {
			ret = ENOMEM;
			goto err;
		}
		snprintf(cc_buf, req_len, "%s%s%s%s%s",
		    cfg.conn_config,
		    cfg.verbose > 1 ? "," : "",
		    cfg.verbose > 1 ? debug_cconfig : "",
		    user_cconfig ? "," : "", user_cconfig ? user_cconfig : "");
		config_opt_str(&cfg, parse_session,
		    "conn_config", cc_buf);
	}
	if (cfg.verbose > 1 || user_tconfig != NULL) {
		req_len = strlen(cfg.table_config) + strlen(debug_tconfig) + 3;
		if (user_tconfig != NULL)
			req_len += strlen(user_tconfig);
		tc_buf = calloc(req_len, 1);
		if (tc_buf == NULL) {
			ret = ENOMEM;
			goto err;
		}
		snprintf(tc_buf, req_len, "%s%s%s%s%s",
		    cfg.table_config,
		    cfg.verbose > 1 ? "," : "",
		    cfg.verbose > 1 ? debug_tconfig : "",
		    user_tconfig ? "," : "", user_tconfig ? user_tconfig : "");
		config_opt_str(&cfg, parse_session,
		    "table_config", tc_buf);
	}

	wtperf_srand(&cfg);

	parse_session->close(parse_session, NULL);
	parse_session = NULL;

	if (cfg.verbose > 1)
		print_config(&cfg);

	/* Reconfigure our connection to the database. */
	if ((ret = connection_reconfigure(conn, cfg.conn_config)) != 0) {
		lprintf(&cfg, ret, 0, "Error configuring using %s",
		    cfg.conn_config);
		goto err;
	}

	cfg.conn = conn;

	g_util_running = 1;
	if (cfg.stat_interval != 0) {
		if ((ret = pthread_create(
		    &stat, NULL, stat_worker, &cfg)) != 0) {
			lprintf(&cfg, ret, 0,
			    "Error creating statistics thread.");
			goto err;
		}
		stat_created = 1;
	}
	if (cfg.checkpoint_interval != 0) {
		if ((ret = pthread_create(
		    &checkpoint, NULL, checkpoint_worker, &cfg)) != 0) {
			lprintf(&cfg, ret, 0,
			    "Error creating checkpoint thread.");
			goto err;
		}
		checkpoint_created = 1;
	}
	if (cfg.create != 0 && execute_populate(&cfg) != 0)
		goto err;
	/* If we aren't populating, set the insert count. */
	if (cfg.create == 0 && find_table_count(&cfg) != 0)
		goto err;

	if (cfg.run_time != 0 &&
	    cfg.read_threads + cfg.insert_threads + cfg.update_threads != 0 &&
	    (ret = execute_workload(&cfg)) != 0)
			goto err;

	lprintf(&cfg, 0, 1,
	    "Ran performance test example with %d read threads, %d insert"
	    " threads and %d update threads for %d seconds.",
	    cfg.read_threads, cfg.insert_threads,
	    cfg.update_threads, cfg.run_time);

	if (cfg.read_threads != 0)
		lprintf(&cfg, 0, 1,
		    "Executed %" PRIu64 " read operations", g_nread_ops);
	if (cfg.insert_threads != 0)
		lprintf(&cfg, 0, 1,
		    "Executed %" PRIu64 " insert operations", g_nins_ops);
	if (cfg.update_threads != 0)
		lprintf(&cfg, 0, 1,
		    "Executed %" PRIu64 " update operations", g_nupdate_ops);

err:	g_util_running = 0;

	if (parse_session != NULL)
		parse_session->close(parse_session, NULL);
	if (checkpoint_created != 0 &&
	    (ret = pthread_join(checkpoint, NULL)) != 0)
		lprintf(&cfg, ret, 0, "Error joining checkpoint thread.");
	if (stat_created != 0 && (ret = pthread_join(stat, NULL)) != 0)
		lprintf(&cfg, ret, 0, "Error joining stat thread.");
	if (conn != NULL && (ret = conn->close(conn, NULL)) != 0)
		lprintf(&cfg, ret, 0,
		    "Error closing connection to %s", cfg.home);
	if (cc_buf != NULL)
		free(cc_buf);
	if (tc_buf != NULL)
		free(tc_buf);
	if (cfg.logf != NULL) {
		fflush(cfg.logf);
		fclose(cfg.logf);
	}
	config_free(&cfg);

	return (ret);
}

/*
 * Following are utility functions.
 */

/* Assign the src config to the dest.
 * Any storage allocated in dest is freed as a result.
 */
void config_assign(CONFIG *dest, const CONFIG *src)
{
	int i;
	char **pstr;
	char *newstr;
	size_t len;
	const char *saved_home;

	saved_home = dest->home;
	config_free(dest);
	memcpy(dest, src, sizeof(CONFIG));

	for (i = 0; i < sizeof(config_opts)/sizeof(config_opts[0]); i++) {
		if (config_opts[i].type == STRING_TYPE) {
			pstr = (char **)
			    ((unsigned char *)dest + config_opts[i].offset);
			if (*pstr != NULL) {
				len = strlen(*pstr) + 1;
				newstr = malloc(len);
				strncpy(newstr, *pstr, len);
				*pstr = newstr;
			}
		}
	}
	dest->home = saved_home;
}

/* Free any storage allocated in the config struct.
 */
void
config_free(CONFIG *cfg)
{
	int i;
	char **pstr;

	for (i = 0; i < sizeof(config_opts)/sizeof(config_opts[0]); i++) {
		if (config_opts[i].type == STRING_TYPE) {
			pstr = (char **)
			    ((unsigned char *)cfg + config_opts[i].offset);
			if (*pstr != NULL) {
				free(*pstr);
				*pstr = NULL;
			}
		}
	}
}

/*
 * Check a single key=value returned by the config parser
 * against our table of valid keys, along with the expected type.
 * If everything is okay, set the value.
 */
int
config_opt(CONFIG *cfg, WT_CONFIG_ITEM *k, WT_CONFIG_ITEM *v)
{
	int i;
	size_t nopt;
	CONFIG_OPT *popt;
	void *valueloc;
	char *newstr;
	char **strp;

	popt = NULL;
	nopt = sizeof(config_opts)/sizeof(config_opts[0]);
	for (i = 0; i < nopt; i++) {
		if (strlen(config_opts[i].name) == k->len &&
		    strncmp(config_opts[i].name, k->str, k->len) == 0) {
			popt = &config_opts[i];
			break;
		}
	}
	if (popt == NULL) {
		fprintf(stderr, "wtperf: Error: "
		    "unknown option \'%.*s\'\n", (int)k->len, k->str);
		fprintf(stderr, "Options:\n");
		for (i = 0; i < nopt; i++) {
			fprintf(stderr, "\t%s\n", config_opts[i].name);
		}
		return (EINVAL);
	}
	valueloc = ((unsigned char *)cfg + popt->offset);
	if (popt->type == UINT32_TYPE) {
		if (v->type != WT_CONFIG_ITEM_NUM) {
			fprintf(stderr, "wtperf: Error: "
			    "bad int value for \'%.*s=%.*s\'\n",
			    (int)k->len, k->str, (int)v->len, v->str);
			return (EINVAL);
		}
		else if (v->val < 0 || v->val > UINT_MAX) {
			fprintf(stderr, "wtperf: Error: "
			    "uint32 value out of range for \'%.*s=%.*s\'\n",
			    (int)k->len, k->str, (int)v->len, v->str);
			return (EINVAL);
		}
		*(uint32_t *)valueloc = (uint32_t)v->val;
	}
	else if (popt->type == STRING_TYPE) {
		if (v->type != WT_CONFIG_ITEM_STRING) {
			fprintf(stderr, "wtperf: Error: "
			    "bad string value for \'%.*s=%.*s\'\n",
			    (int)k->len, k->str, (int)v->len, v->str);
			return (EINVAL);
		}
		strp = (char **)valueloc;
		if (*strp != NULL) {
			free(*strp);
		}
		newstr = malloc(v->len + 1);
		strncpy(newstr, v->str, v->len);
		newstr[v->len] = '\0';
		*strp = newstr;
	}
	else if (popt->type == BOOL_TYPE || popt->type == FLAG_TYPE) {
		uint32_t *pconfigval;

		if (v->type != WT_CONFIG_ITEM_BOOL) {
			fprintf(stderr, "wtperf: Error: "
			    "bad bool value for \'%.*s=%.*s\'\n",
			    (int)k->len, k->str, (int)v->len, v->str);
			return (EINVAL);
		}
		pconfigval = (uint32_t *)valueloc;
		if (popt->type == BOOL_TYPE) {
			*pconfigval = v->val;
		}
		else if (v->val != 0) {
			*pconfigval |= popt->flagmask;
		}
		else {
			*pconfigval &= ~popt->flagmask;
		}
	}
	return (0);
}

/* Parse a configuration file.
 * We recognize comments '#' and continuation via lines ending in '\'.
 */
int
config_opt_file(CONFIG *cfg, WT_SESSION *parse_session, const char *filename)
{
	FILE *fp;
	char option[1024];
	char line[256];
	char *ltrim;
	char *rtrim;
	char *comment;
	int ret;
	int contline;
	int optionpos;
	size_t linelen;
	int linenum;

	if ((fp = fopen(filename, "r")) == NULL) {
		fprintf(stderr, "wtperf: %s: %s\n", filename, strerror(errno));
		return errno;
	}

	ret = 0;
	optionpos = 0;
	linenum = 0;
	while (fgets(line, sizeof(line), fp) != NULL) {
		linenum++;
		/* trim the line */
		for (ltrim = line; *ltrim && isspace(*ltrim); ltrim++)
			;
		rtrim = &ltrim[strlen(ltrim)];
		if (rtrim > ltrim && rtrim[-1] == '\n')
			rtrim--;

		contline = (rtrim > ltrim && rtrim[-1] == '\\');
		if (contline)
			rtrim--;

		comment = strchr(ltrim, '#');
		if (comment != NULL && comment < rtrim)
			rtrim = comment;
		while (rtrim > ltrim && isspace(rtrim[-1]))
			rtrim--;

		linelen = rtrim - ltrim;
		if (linelen == 0)
			continue;

		if (linelen + optionpos + 1 > sizeof(option)) {
			fprintf(stderr, "wtperf: %s: %d: line overflow\n",
			    filename, linenum);
			ret = EINVAL;
			break;
		}
		*rtrim = '\0';
		strncpy(&option[optionpos], ltrim, linelen);
		option[optionpos + linelen] = '\0';
		if (contline) {
			optionpos += linelen;
		}
		else {
			if ((ret = config_opt_line(cfg,
				    parse_session, option)) != 0) {
				fprintf(stderr, "wtperf: %s: %d: parse error\n",
				    filename, linenum);
				break;
			}
			optionpos = 0;
		}
	}
	if (ret == 0 && optionpos > 0) {
		fprintf(stderr, "wtperf: %s: %d: last line continues\n",
		    filename, linenum);
		ret = EINVAL;
	}

	(void)fclose(fp);
	return (ret);
}

/* Parse a single line of config options.
 * Continued lines have already been joined.
 */
int
config_opt_line(CONFIG *cfg, WT_SESSION *parse_session, const char *optstr)
{
	WT_CONFIG_ITEM k, v;
	WT_CONFIG_SCAN *scan;
	int ret;
	int t_ret;
	WT_EXTENSION_API *wt_api;
	WT_CONNECTION *conn;

	conn = parse_session->connection;
	wt_api = conn->get_extension_api(conn);

	if ((ret = wt_api->config_scan_begin(wt_api, parse_session, optstr,
		    strlen(optstr), &scan)) != 0) {
		lprintf(cfg, ret, 0, "Error in config_scan_begin");
		return (ret);
	}

	while (ret == 0) {
		if ((ret = wt_api->config_scan_next(wt_api, scan, &k, &v))
		    != 0) {
			/* Any parse error has already been reported. */
			if (ret == WT_NOTFOUND)
				ret = 0;
			break;
		}
		ret = config_opt(cfg, &k, &v);
	}
	if ((t_ret = wt_api->config_scan_end(wt_api, scan)) != 0) {
		lprintf(cfg, ret, 0,
		    "Error in config_scan_end");
		if (ret == 0)
			ret = t_ret;
	}

	return (ret);
}

/* Set a single string config option */
int
config_opt_str(CONFIG *cfg, WT_SESSION *parse_session,
    const char *name, const char *value)
{
	char *optstr;
	int ret;

	optstr = malloc(strlen(name) + strlen(value) + 4);  /* name="value" */
	sprintf(optstr, "%s=\"%s\"", name, value);
	ret = config_opt_line(cfg, parse_session, optstr);
	free(optstr);
	return (ret);
}

/* Set a single int config option */
int
config_opt_int(CONFIG *cfg, WT_SESSION *parse_session,
    const char *name, const char *value)
{
	char *optstr;
	int ret;

	optstr = malloc(strlen(name) + strlen(value) + 2);  /* name=value */
	sprintf(optstr, "%s=%s", name, value);
	ret = config_opt_line(cfg, parse_session, optstr);
	free(optstr);
	return (ret);
}

int
start_threads(
    CONFIG *cfg, u_int num, pthread_t **threadsp, void *(*func)(void *))
{
	pthread_t *threads;
	u_int i;
	int ret;

	g_running = 1;
	g_npop_ops = g_nread_ops = g_nupdate_ops = 0;
	g_threads_quit = 0;
	threads = calloc(num, sizeof(pthread_t *));
	if (threads == NULL)
		return (ENOMEM);
	for (i = 0; i < num; i++) {
		if ((ret = pthread_create(
		    &threads[i], NULL, func, cfg)) != 0) {
			g_running = 0;
			lprintf(cfg, ret, 0, "Error creating thread: %d", i);
			return (ret);
		}
	}
	*threadsp = threads;
	return (0);
}

int
stop_threads(CONFIG *cfg, u_int num, pthread_t *threads)
{
	u_int i;
	int ret;

	g_running = 0;

	for (i = 0; i < num; i++) {
		if ((ret = pthread_join(threads[i], NULL)) != 0) {
			lprintf(cfg, ret, 0, "Error joining thread %d", i);
			return (ret);
		}
	}

	free(threads);
	return (0);
}

/*
 * Log printf - output a log message.
 */
int
lprintf(CONFIG *cfg, int err, uint32_t level, const char *fmt, ...)
{
	va_list ap;

	if (err == 0 && level <= cfg->verbose) {
		va_start(ap, fmt);
		vfprintf(cfg->logf, fmt, ap);
		va_end(ap);
		fprintf(cfg->logf, "\n");

		if (level < cfg->verbose) {
			va_start(ap, fmt);
			vprintf(fmt, ap);
			va_end(ap);
			printf("\n");
		}
	}
	if (err == 0)
		return (0);

	/* We are dealing with an error. */
	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	va_end(ap);
	fprintf(stderr, " Error: %s\n", wiredtiger_strerror(err));
	if (cfg->logf != NULL) {
		va_start(ap, fmt);
		vfprintf(cfg->logf, fmt, ap);
		va_end(ap);
		fprintf(cfg->logf, " Error: %s\n", wiredtiger_strerror(err));
	}

	return (0);
}

/* Setup the logging output mechanism. */
int setup_log_file(CONFIG *cfg)
{
	char *fname;
	int offset;

	if (cfg->verbose < 1 && cfg->stat_interval == 0)
		return (0);

	if ((fname = calloc(strlen(cfg->home) +
	    strlen(cfg->uri) + strlen(".stat") + 1, 1)) == NULL) {
		fprintf(stderr, "No memory in stat thread\n");
		return (ENOMEM);
	}
	for (offset = 0;
	    cfg->uri[offset] != 0 && cfg->uri[offset] != ':';
	    offset++) {}
	if (cfg->uri[offset] == 0)
		offset = 0;
	else
		++offset;
	sprintf(fname, "%s/%s.stat", cfg->home, cfg->uri + offset);
	if ((cfg->logf = fopen(fname, "w")) == NULL) {
		fprintf(stderr, "Statistics failed to open log file.\n");
		return (EINVAL);
	}
	/* Use line buffering for the log file. */
	(void)setvbuf(cfg->logf, NULL, _IOLBF, 0);
	if (fname != NULL)
		free(fname);
	return (0);
}

void wtperf_srand(CONFIG *cfg) {
	srand(cfg->rand_seed);
}

uint64_t wtperf_value_range(CONFIG *cfg) {
	if (F_ISSET(cfg, PERF_RAND_WORKLOAD))
		return (cfg->icount + cfg->rand_range);
	else 
		return (cfg->icount + g_nins_ops - (cfg->insert_threads + 1));
}

uint64_t wtperf_rand(CONFIG *cfg) {
	double S1, S2, U;
	uint64_t rval = (uint64_t)rand();
	/* Use Pareto distribution to give 80/20 hot/cold values. */
	if (F_ISSET(cfg, PERF_RAND_PARETO)) {
#define	PARETO_SHAPE	1.5
		S1 = (-1 / PARETO_SHAPE);
		S2 = wtperf_value_range(cfg) * 0.2 * (PARETO_SHAPE - 1);
		U = 1 - (double)rval / (double)RAND_MAX;
		rval = (pow(U, S1) - 1) * S2;
		/*
		 * This Pareto calculation chooses out of range values about
		 * about 2% of the time, from my testing. That will lead to the
		 * last item in the table being "hot".
		 */
		if (rval > wtperf_value_range(cfg))
			rval = wtperf_value_range(cfg);
	}
	/* Avoid zero - LSM doesn't like it. */
	rval = (rval % wtperf_value_range(cfg)) + 1;
	return rval;
}

void print_config(CONFIG *cfg)
{
	printf("Workload configuration:\n");
	printf("\t home: %s\n", cfg->home);
	printf("\t uri: %s\n", cfg->uri);
	printf("\t Connection configuration: %s\n", cfg->conn_config);
	printf("\t Table configuration: %s\n", cfg->table_config);
	printf("\t %s\n", cfg->create ? "Creating" : "Using existing");
	printf("\t Checkpoint interval: %d\n", cfg->checkpoint_interval);
	printf("\t Random seed: %d\n", cfg->rand_seed);
	if (cfg->create) {
		printf("\t Insert count: %d\n", cfg->icount);
		printf("\t Number populate threads: %d\n",
		    cfg->populate_threads);
	}
	printf("\t key size: %d data size: %d\n", cfg->key_sz, cfg->data_sz);
	printf("\t Reporting interval: %d\n", cfg->report_interval);
	printf("\t Workload period: %d\n", cfg->run_time);
	printf("\t Number read threads: %d\n", cfg->read_threads);
	printf("\t Number insert threads: %d\n", cfg->insert_threads);
	if (F_ISSET(cfg, PERF_INSERT_RMW))
		printf("\t Insert operations are RMW.\n");
	printf("\t Number update threads: %d\n", cfg->update_threads);
	printf("\t Verbosity: %d\n", cfg->verbose);
}

void usage(void)
{
	printf("wtperf [-CLMPRSTdehikrsuv]\n");
	printf("\t-S Use a small default configuration\n");
	printf("\t-M Use a medium default configuration\n");
	printf("\t-L Use a large default configuration\n");
	printf("\t-C <string> additional connection configuration\n");
	printf("\t-I <int> number of insert worker threads\n");
	printf("\t-P <int> number of populate threads\n");
	printf("\t-R <int> number of read threads\n");
	printf("\t-U <int> number of update threads\n");
	printf("\t-T <string> additional table configuration\n");
	printf("\t-c <int> checkpoint every <int> report intervals."
	    "Default disabled,\n");
	printf("\t-d <int> data item size\n");
	printf("\t-e use existing database (skip population phase)\n");
	printf("\t-h <string> Wired Tiger home must exist, default WT_TEST \n");
	printf("\t-i <int> number of records to insert\n");
	printf("\t-j Execute a read prior to each insert in populate\n");
	printf("\t-k <int> key item size\n");
	printf("\t-l <int> log statistics every <int> report intervals."
	    "Default disabled.\n");
	printf("\t-m <range> use random inserts in workload. Means reads"
	    " and updates will ignore WT_NOTFOUND\n");
	printf("\t-p use pareto 80/20 distribution for random numbers\n");
	printf("\t-r <int> number of seconds to run workload phase\n");
	printf("\t-s <int> seed for random number generator\n");
	printf("\t-t <int> How often to output throughput information\n");
	printf("\t-u <string> table uri, default lsm:test\n");
	printf("\t-v <int> verbosity\n");
}