1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 1997,2008 Oracle.  All rights reserved.
5 *
6 * $Id: TpcbExample.cpp,v 12.8 2008/01/08 20:58:26 bostic Exp $
7 */
8
9#include <sys/types.h>
10
11#include <errno.h>
12#include <stdlib.h>
13#include <string.h>
14#include <time.h>
15
16#include <iostream>
17#include <iomanip>
18#include <db_cxx.h>
19
20using std::cout;
21using std::cerr;
22
23typedef enum { ACCOUNT, BRANCH, TELLER } FTYPE;
24
25static int	  invarg(int, char *);
26u_int32_t random_id(FTYPE, u_int32_t, u_int32_t, u_int32_t);
27u_int32_t random_int(u_int32_t, u_int32_t);
28static int	  usage(void);
29
30int verbose;
31const char *progname = "TpcbExample";                       // Program name.
32
33class TpcbExample : public DbEnv
34{
35public:
36	void populate(int, int, int, int);
37	void run(int, int, int, int);
38	int txn(Db *, Db *, Db *, Db *,
39		int, int, int);
40	void populateHistory(Db *, int, u_int32_t, u_int32_t, u_int32_t);
41	void populateTable(Db *, u_int32_t, u_int32_t, int, const char *);
42
43	// Note: the constructor creates a DbEnv(), which is
44	// not fully initialized until the DbEnv::open() method
45	// is called.
46	//
47	TpcbExample(const char *home, int cachesize, int flags);
48
49private:
50	static const char FileName[];
51
52	// no need for copy and assignment
53	TpcbExample(const TpcbExample &);
54	void operator = (const TpcbExample &);
55};
56
57//
58// This program implements a basic TPC/B driver program.  To create the
59// TPC/B database, run with the -i (init) flag.  The number of records
60// with which to populate the account, history, branch, and teller tables
61// is specified by the a, s, b, and t flags respectively.  To run a TPC/B
62// test, use the n flag to indicate a number of transactions to run (note
63// that you can run many of these processes in parallel to simulate a
64// multiuser test run).
65//
66#define	TELLERS_PER_BRANCH      100
67#define	ACCOUNTS_PER_TELLER     1000
68#define	HISTORY_PER_BRANCH	2592000
69
70/*
71 * The default configuration that adheres to TPCB scaling rules requires
72 * nearly 3 GB of space.  To avoid requiring that much space for testing,
73 * we set the parameters much lower.  If you want to run a valid 10 TPS
74 * configuration, define VALID_SCALING.
75 */
76#ifdef	VALID_SCALING
77#define	ACCOUNTS	 1000000
78#define	BRANCHES	      10
79#define	TELLERS		     100
80#define	HISTORY		25920000
81#endif
82
83#ifdef	TINY
84#define	ACCOUNTS	    1000
85#define	BRANCHES	      10
86#define	TELLERS		     100
87#define	HISTORY		   10000
88#endif
89
90#if !defined(VALID_SCALING) && !defined(TINY)
91#define	ACCOUNTS	  100000
92#define	BRANCHES	      10
93#define	TELLERS		     100
94#define	HISTORY		  259200
95#endif
96
97#define	HISTORY_LEN	    100
98#define	RECLEN		    100
99#define	BEGID		1000000
100
101struct Defrec {
102	u_int32_t   id;
103	u_int32_t   balance;
104	u_int8_t    pad[RECLEN - sizeof(u_int32_t) - sizeof(u_int32_t)];
105};
106
107struct Histrec {
108	u_int32_t   aid;
109	u_int32_t   bid;
110	u_int32_t   tid;
111	u_int32_t   amount;
112	u_int8_t    pad[RECLEN - 4 * sizeof(u_int32_t)];
113};
114
115int
116main(int argc, char *argv[])
117{
118	unsigned long seed;
119	int accounts, branches, tellers, history;
120	int iflag, mpool, ntxns, txn_no_sync;
121	const char *home;
122	char *endarg;
123
124	home = "TESTDIR";
125	accounts = branches = history = tellers = 0;
126	txn_no_sync = 0;
127	mpool = ntxns = 0;
128	verbose = 0;
129	iflag = 0;
130	seed = (unsigned long)time(NULL);
131
132	for (int i = 1; i < argc; ++i) {
133
134		if (strcmp(argv[i], "-a") == 0) {
135			// Number of account records
136			if ((accounts = atoi(argv[++i])) <= 0)
137				return (invarg('a', argv[i]));
138		}
139		else if (strcmp(argv[i], "-b") == 0) {
140			// Number of branch records
141			if ((branches = atoi(argv[++i])) <= 0)
142				return (invarg('b', argv[i]));
143		}
144		else if (strcmp(argv[i], "-c") == 0) {
145			// Cachesize in bytes
146			if ((mpool = atoi(argv[++i])) <= 0)
147				return (invarg('c', argv[i]));
148		}
149		else if (strcmp(argv[i], "-f") == 0) {
150			// Fast mode: no txn sync.
151			txn_no_sync = 1;
152		}
153		else if (strcmp(argv[i], "-h") == 0) {
154			// DB  home.
155			home = argv[++i];
156		}
157		else if (strcmp(argv[i], "-i") == 0) {
158			// Initialize the test.
159			iflag = 1;
160		}
161		else if (strcmp(argv[i], "-n") == 0) {
162			// Number of transactions
163			if ((ntxns = atoi(argv[++i])) <= 0)
164				return (invarg('n', argv[i]));
165		}
166		else if (strcmp(argv[i], "-S") == 0) {
167			// Random number seed.
168			seed = strtoul(argv[++i], &endarg, 0);
169			if (*endarg != '\0')
170				return (invarg('S', argv[i]));
171		}
172		else if (strcmp(argv[i], "-s") == 0) {
173			// Number of history records
174			if ((history = atoi(argv[++i])) <= 0)
175				return (invarg('s', argv[i]));
176		}
177		else if (strcmp(argv[i], "-t") == 0) {
178			// Number of teller records
179			if ((tellers = atoi(argv[++i])) <= 0)
180				return (invarg('t', argv[i]));
181		}
182		else if (strcmp(argv[i], "-v") == 0) {
183			// Verbose option.
184			verbose = 1;
185		}
186		else {
187			return (usage());
188		}
189	}
190
191	srand((unsigned int)seed);
192
193	accounts = accounts == 0 ? ACCOUNTS : accounts;
194	branches = branches == 0 ? BRANCHES : branches;
195	tellers = tellers == 0 ? TELLERS : tellers;
196	history = history == 0 ? HISTORY : history;
197
198	if (verbose)
199		cout << (long)accounts << " Accounts, "
200		     << (long)branches << " Branches, "
201		     << (long)tellers << " Tellers, "
202		     << (long)history << " History\n";
203
204	try {
205		// Initialize the database environment.
206		// Must be done in within a try block, unless you
207		// change the error model in the environment options.
208		//
209		TpcbExample app(home, mpool, txn_no_sync ? DB_TXN_NOSYNC : 0);
210
211		if (iflag) {
212			if (ntxns != 0)
213				return (usage());
214			app.populate(accounts, branches, history, tellers);
215		}
216		else {
217			if (ntxns == 0)
218				return (usage());
219			app.run(ntxns, accounts, branches, tellers);
220		}
221
222		app.close(0);
223		return (EXIT_SUCCESS);
224	}
225	catch (DbException &dbe) {
226		cerr << "TpcbExample: " << dbe.what() << "\n";
227		return (EXIT_FAILURE);
228	}
229}
230
231static int
232invarg(int arg, char *str)
233{
234	cerr << "TpcbExample: invalid argument for -"
235	     << (char)arg << ": " << str << "\n";
236	return (EXIT_FAILURE);
237}
238
239static int
240usage()
241{
242	cerr << "usage: TpcbExample [-fiv] [-a accounts] [-b branches]\n"
243	     << "                   [-c cachesize] [-h home] [-n transactions ]\n"
244	     << "                   [-S seed] [-s history] [-t tellers]\n";
245	return (EXIT_FAILURE);
246}
247
248TpcbExample::TpcbExample(const char *home, int cachesize, int flags)
249:	DbEnv(0)
250{
251	u_int32_t local_flags;
252
253	set_error_stream(&cerr);
254	set_errpfx("TpcbExample");
255	(void)set_cachesize(0, cachesize == 0 ?
256			    4 * 1024 * 1024 : (u_int32_t)cachesize, 0);
257
258	if (flags & (DB_TXN_NOSYNC))
259		set_flags(DB_TXN_NOSYNC, 1);
260	flags &= ~(DB_TXN_NOSYNC);
261
262	local_flags = flags | DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG |
263	    DB_INIT_MPOOL | DB_INIT_TXN;
264	open(home, local_flags, 0);
265}
266
267//
268// Initialize the database to the specified number of accounts, branches,
269// history records, and tellers.
270//
271void
272TpcbExample::populate(int accounts, int branches, int history, int tellers)
273{
274	Db *dbp;
275
276	int err;
277	u_int32_t balance, idnum;
278	u_int32_t end_anum, end_bnum, end_tnum;
279	u_int32_t start_anum, start_bnum, start_tnum;
280
281	idnum = BEGID;
282	balance = 500000;
283
284	dbp = new Db(this, 0);
285	dbp->set_h_nelem((unsigned int)accounts);
286
287	if ((err = dbp->open(NULL, "account", NULL, DB_HASH,
288			     DB_CREATE, 0644)) != 0) {
289		DbException except("Account file create failed", err);
290		throw except;
291	}
292
293	start_anum = idnum;
294	populateTable(dbp, idnum, balance, accounts, "account");
295	idnum += accounts;
296	end_anum = idnum - 1;
297	if ((err = dbp->close(0)) != 0) {
298		DbException except("Account file close failed", err);
299		throw except;
300	}
301	delete dbp;
302	if (verbose)
303		cout << "Populated accounts: "
304		     << (long)start_anum << " - " << (long)end_anum << "\n";
305
306	dbp = new Db(this, 0);
307	//
308	// Since the number of branches is very small, we want to use very
309	// small pages and only 1 key per page.  This is the poor-man's way
310	// of getting key locking instead of page locking.
311	//
312	dbp->set_h_ffactor(1);
313	dbp->set_h_nelem((unsigned int)branches);
314	dbp->set_pagesize(512);
315
316	if ((err = dbp->open(NULL, "branch", NULL, DB_HASH,
317			     DB_CREATE, 0644)) != 0) {
318		DbException except("Branch file create failed", err);
319		throw except;
320	}
321	start_bnum = idnum;
322	populateTable(dbp, idnum, balance, branches, "branch");
323	idnum += branches;
324	end_bnum = idnum - 1;
325	if ((err = dbp->close(0)) != 0) {
326		DbException except("Close of branch file failed", err);
327		throw except;
328	}
329	delete dbp;
330
331	if (verbose)
332		cout << "Populated branches: "
333		     << (long)start_bnum << " - " << (long)end_bnum << "\n";
334
335	dbp = new Db(this, 0);
336	//
337	// In the case of tellers, we also want small pages, but we'll let
338	// the fill factor dynamically adjust itself.
339	//
340	dbp->set_h_ffactor(0);
341	dbp->set_h_nelem((unsigned int)tellers);
342	dbp->set_pagesize(512);
343
344	if ((err = dbp->open(NULL, "teller", NULL, DB_HASH,
345			     DB_CREATE, 0644)) != 0) {
346		DbException except("Teller file create failed", err);
347		throw except;
348	}
349
350	start_tnum = idnum;
351	populateTable(dbp, idnum, balance, tellers, "teller");
352	idnum += tellers;
353	end_tnum = idnum - 1;
354	if ((err = dbp->close(0)) != 0) {
355		DbException except("Close of teller file failed", err);
356		throw except;
357	}
358	delete dbp;
359	if (verbose)
360		cout << "Populated tellers: "
361		     << (long)start_tnum << " - " << (long)end_tnum << "\n";
362
363	dbp = new Db(this, 0);
364	dbp->set_re_len(HISTORY_LEN);
365	if ((err = dbp->open(NULL, "history", NULL, DB_RECNO,
366			     DB_CREATE, 0644)) != 0) {
367		DbException except("Create of history file failed", err);
368		throw except;
369	}
370
371	populateHistory(dbp, history, accounts, branches, tellers);
372	if ((err = dbp->close(0)) != 0) {
373		DbException except("Close of history file failed", err);
374		throw except;
375	}
376	delete dbp;
377}
378
379void
380TpcbExample::populateTable(Db *dbp,
381			   u_int32_t start_id, u_int32_t balance,
382			   int nrecs, const char *msg)
383{
384	Defrec drec;
385	memset(&drec.pad[0], 1, sizeof(drec.pad));
386
387	Dbt kdbt(&drec.id, sizeof(u_int32_t));
388	Dbt ddbt(&drec, sizeof(drec));
389
390	for (int i = 0; i < nrecs; i++) {
391		drec.id = start_id + (u_int32_t)i;
392		drec.balance = balance;
393		int err;
394		if ((err =
395		     dbp->put(NULL, &kdbt, &ddbt, DB_NOOVERWRITE)) != 0) {
396			cerr << "Failure initializing " << msg << " file: "
397			     << strerror(err) << "\n";
398			DbException except("failure initializing file", err);
399			throw except;
400		}
401	}
402}
403
404void
405TpcbExample::populateHistory(Db *dbp, int nrecs, u_int32_t accounts,
406			     u_int32_t branches, u_int32_t tellers)
407{
408	Histrec hrec;
409	memset(&hrec.pad[0], 1, sizeof(hrec.pad));
410	hrec.amount = 10;
411	db_recno_t key;
412
413	Dbt kdbt(&key, sizeof(u_int32_t));
414	Dbt ddbt(&hrec, sizeof(hrec));
415
416	for (int i = 1; i <= nrecs; i++) {
417		hrec.aid = random_id(ACCOUNT, accounts, branches, tellers);
418		hrec.bid = random_id(BRANCH, accounts, branches, tellers);
419		hrec.tid = random_id(TELLER, accounts, branches, tellers);
420
421		int err;
422		key = (db_recno_t)i;
423		if ((err = dbp->put(NULL, &kdbt, &ddbt, DB_APPEND)) != 0) {
424			DbException except("failure initializing history file",
425					   err);
426			throw except;
427		}
428	}
429}
430
431u_int32_t
432random_int(u_int32_t lo, u_int32_t hi)
433{
434	u_int32_t ret;
435	int t;
436
437	t = rand();
438	ret = (u_int32_t)(((double)t / ((double)(RAND_MAX) + 1)) *
439			  (hi - lo + 1));
440	ret += lo;
441	return (ret);
442}
443
444u_int32_t
445random_id(FTYPE type, u_int32_t accounts, u_int32_t branches, u_int32_t tellers)
446{
447	u_int32_t min, max, num;
448
449	max = min = BEGID;
450	num = accounts;
451	switch (type) {
452	case TELLER:
453		min += branches;
454		num = tellers;
455		// Fallthrough
456	case BRANCH:
457		if (type == BRANCH)
458			num = branches;
459		min += accounts;
460		// Fallthrough
461	case ACCOUNT:
462		max = min + num - 1;
463	}
464	return (random_int(min, max));
465}
466
467void
468TpcbExample::run(int n, int accounts, int branches, int tellers)
469{
470	Db *adb, *bdb, *hdb, *tdb;
471	int failed, ret, txns;
472	time_t start_time, end_time;
473
474	//
475	// Open the database files.
476	//
477
478	int err;
479	adb = new Db(this, 0);
480	if ((err = adb->open(NULL, "account", NULL, DB_UNKNOWN,
481			     DB_AUTO_COMMIT, 0)) != 0) {
482		DbException except("Open of account file failed", err);
483		throw except;
484	}
485
486	bdb = new Db(this, 0);
487	if ((err = bdb->open(NULL, "branch", NULL, DB_UNKNOWN,
488			     DB_AUTO_COMMIT, 0)) != 0) {
489		DbException except("Open of branch file failed", err);
490		throw except;
491	}
492
493	tdb = new Db(this, 0);
494	if ((err = tdb->open(NULL, "teller", NULL, DB_UNKNOWN,
495			     DB_AUTO_COMMIT, 0)) != 0) {
496		DbException except("Open of teller file failed", err);
497		throw except;
498	}
499
500	hdb = new Db(this, 0);
501	if ((err = hdb->open(NULL, "history", NULL, DB_UNKNOWN,
502			     DB_AUTO_COMMIT, 0)) != 0) {
503		DbException except("Open of history file failed", err);
504		throw except;
505	}
506
507	(void)time(&start_time);
508	for (txns = n, failed = 0; n-- > 0;)
509		if ((ret = txn(adb, bdb, tdb, hdb,
510		    accounts, branches, tellers)) != 0)
511			++failed;
512	(void)time(&end_time);
513	if (end_time == start_time)
514		++end_time;
515	// We use printf because it provides much simpler
516	// formatting than iostreams.
517	//
518	printf("%s: %d txns: %d failed, %.2f TPS\n", progname, txns, failed,
519	    (txns - failed) / (double)(end_time - start_time));
520
521	(void)adb->close(0);
522	(void)bdb->close(0);
523	(void)tdb->close(0);
524	(void)hdb->close(0);
525}
526
527//
528// XXX Figure out the appropriate way to pick out IDs.
529//
530int
531TpcbExample::txn(Db *adb, Db *bdb, Db *tdb, Db *hdb,
532		 int accounts, int branches, int tellers)
533{
534	Dbc *acurs = NULL;
535	Dbc *bcurs = NULL;
536	Dbc *tcurs = NULL;
537	DbTxn *t = NULL;
538
539	db_recno_t key;
540	Defrec rec;
541	Histrec hrec;
542	int account, branch, teller, ret;
543
544	Dbt d_dbt;
545	Dbt d_histdbt;
546	Dbt k_dbt;
547	Dbt k_histdbt(&key, sizeof(key));
548
549	// !!!
550	// This is sample code -- we could move a lot of this into the driver
551	// to make it faster.
552	//
553	account = random_id(ACCOUNT, accounts, branches, tellers);
554	branch = random_id(BRANCH, accounts, branches, tellers);
555	teller = random_id(TELLER, accounts, branches, tellers);
556
557	k_dbt.set_size(sizeof(int));
558
559	d_dbt.set_flags(DB_DBT_USERMEM);
560	d_dbt.set_data(&rec);
561	d_dbt.set_ulen(sizeof(rec));
562
563	hrec.aid = account;
564	hrec.bid = branch;
565	hrec.tid = teller;
566	hrec.amount = 10;
567	// Request 0 bytes since we're just positioning.
568	d_histdbt.set_flags(DB_DBT_PARTIAL);
569
570	// START PER-TRANSACTION TIMING.
571	//
572	// Technically, TPCB requires a limit on response time, you only get
573	// to count transactions that complete within 2 seconds.  That's not
574	// an issue for this sample application -- regardless, here's where
575	// the transaction begins.
576	if (txn_begin(NULL, &t, 0) != 0)
577		goto err;
578
579	if (adb->cursor(t, &acurs, 0) != 0 ||
580	    bdb->cursor(t, &bcurs, 0) != 0 ||
581	    tdb->cursor(t, &tcurs, 0) != 0)
582		goto err;
583
584	// Account record
585	k_dbt.set_data(&account);
586	if (acurs->get(&k_dbt, &d_dbt, DB_SET) != 0)
587		goto err;
588	rec.balance += 10;
589	if (acurs->put(&k_dbt, &d_dbt, DB_CURRENT) != 0)
590		goto err;
591
592	// Branch record
593	k_dbt.set_data(&branch);
594	if (bcurs->get(&k_dbt, &d_dbt, DB_SET) != 0)
595		goto err;
596	rec.balance += 10;
597	if (bcurs->put(&k_dbt, &d_dbt, DB_CURRENT) != 0)
598		goto err;
599
600	// Teller record
601	k_dbt.set_data(&teller);
602	if (tcurs->get(&k_dbt, &d_dbt, DB_SET) != 0)
603		goto err;
604	rec.balance += 10;
605	if (tcurs->put(&k_dbt, &d_dbt, DB_CURRENT) != 0)
606		goto err;
607
608	// History record
609	d_histdbt.set_flags(0);
610	d_histdbt.set_data(&hrec);
611	d_histdbt.set_ulen(sizeof(hrec));
612	if (hdb->put(t, &k_histdbt, &d_histdbt, DB_APPEND) != 0)
613		goto err;
614
615	if (acurs->close() != 0 || bcurs->close() != 0 || tcurs->close() != 0)
616		goto err;
617
618	ret = t->commit(0);
619	t = NULL;
620	if (ret != 0)
621		goto err;
622
623	// END PER-TRANSACTION TIMING.
624	return (0);
625
626err:
627	if (acurs != NULL)
628		(void)acurs->close();
629	if (bcurs != NULL)
630		(void)bcurs->close();
631	if (tcurs != NULL)
632		(void)tcurs->close();
633	if (t != NULL)
634		(void)t->abort();
635
636	if (verbose)
637		cout << "Transaction A=" << (long)account
638		     << " B=" << (long)branch
639		     << " T=" << (long)teller << " failed\n";
640	return (-1);
641}
642