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
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 1997,2007 Oracle. All rights reserved.
*
* $Id: TpcbExample.cpp,v 12.7 2007/05/17 15:15:31 bostic Exp $
*/
#include <sys/types.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <iostream>
#include <iomanip>
#include <db_cxx.h>
using std::cout;
using std::cerr;
typedef enum { ACCOUNT, BRANCH, TELLER } FTYPE;
static int invarg(int, char *);
u_int32_t random_id(FTYPE, u_int32_t, u_int32_t, u_int32_t);
u_int32_t random_int(u_int32_t, u_int32_t);
static int usage(void);
int verbose;
const char *progname = "TpcbExample"; // Program name.
class TpcbExample : public DbEnv
{
public:
void populate(int, int, int, int);
void run(int, int, int, int);
int txn(Db *, Db *, Db *, Db *,
int, int, int);
void populateHistory(Db *, int, u_int32_t, u_int32_t, u_int32_t);
void populateTable(Db *, u_int32_t, u_int32_t, int, const char *);
// Note: the constructor creates a DbEnv(), which is
// not fully initialized until the DbEnv::open() method
// is called.
//
TpcbExample(const char *home, int cachesize, int flags);
private:
static const char FileName[];
// no need for copy and assignment
TpcbExample(const TpcbExample &);
void operator = (const TpcbExample &);
};
//
// This program implements a basic TPC/B driver program. To create the
// TPC/B database, run with the -i (init) flag. The number of records
// with which to populate the account, history, branch, and teller tables
// is specified by the a, s, b, and t flags respectively. To run a TPC/B
// test, use the n flag to indicate a number of transactions to run (note
// that you can run many of these processes in parallel to simulate a
// multiuser test run).
//
#define TELLERS_PER_BRANCH 100
#define ACCOUNTS_PER_TELLER 1000
#define HISTORY_PER_BRANCH 2592000
/*
* The default configuration that adheres to TPCB scaling rules requires
* nearly 3 GB of space. To avoid requiring that much space for testing,
* we set the parameters much lower. If you want to run a valid 10 TPS
* configuration, define VALID_SCALING.
*/
#ifdef VALID_SCALING
#define ACCOUNTS 1000000
#define BRANCHES 10
#define TELLERS 100
#define HISTORY 25920000
#endif
#ifdef TINY
#define ACCOUNTS 1000
#define BRANCHES 10
#define TELLERS 100
#define HISTORY 10000
#endif
#if !defined(VALID_SCALING) && !defined(TINY)
#define ACCOUNTS 100000
#define BRANCHES 10
#define TELLERS 100
#define HISTORY 259200
#endif
#define HISTORY_LEN 100
#define RECLEN 100
#define BEGID 1000000
struct Defrec {
u_int32_t id;
u_int32_t balance;
u_int8_t pad[RECLEN - sizeof(u_int32_t) - sizeof(u_int32_t)];
};
struct Histrec {
u_int32_t aid;
u_int32_t bid;
u_int32_t tid;
u_int32_t amount;
u_int8_t pad[RECLEN - 4 * sizeof(u_int32_t)];
};
int
main(int argc, char *argv[])
{
unsigned long seed;
int accounts, branches, tellers, history;
int iflag, mpool, ntxns, txn_no_sync;
const char *home;
char *endarg;
home = "TESTDIR";
accounts = branches = history = tellers = 0;
txn_no_sync = 0;
mpool = ntxns = 0;
verbose = 0;
iflag = 0;
seed = (unsigned long)time(NULL);
for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i], "-a") == 0) {
// Number of account records
if ((accounts = atoi(argv[++i])) <= 0)
return (invarg('a', argv[i]));
}
else if (strcmp(argv[i], "-b") == 0) {
// Number of branch records
if ((branches = atoi(argv[++i])) <= 0)
return (invarg('b', argv[i]));
}
else if (strcmp(argv[i], "-c") == 0) {
// Cachesize in bytes
if ((mpool = atoi(argv[++i])) <= 0)
return (invarg('c', argv[i]));
}
else if (strcmp(argv[i], "-f") == 0) {
// Fast mode: no txn sync.
txn_no_sync = 1;
}
else if (strcmp(argv[i], "-h") == 0) {
// DB home.
home = argv[++i];
}
else if (strcmp(argv[i], "-i") == 0) {
// Initialize the test.
iflag = 1;
}
else if (strcmp(argv[i], "-n") == 0) {
// Number of transactions
if ((ntxns = atoi(argv[++i])) <= 0)
return (invarg('n', argv[i]));
}
else if (strcmp(argv[i], "-S") == 0) {
// Random number seed.
seed = strtoul(argv[++i], &endarg, 0);
if (*endarg != '\0')
return (invarg('S', argv[i]));
}
else if (strcmp(argv[i], "-s") == 0) {
// Number of history records
if ((history = atoi(argv[++i])) <= 0)
return (invarg('s', argv[i]));
}
else if (strcmp(argv[i], "-t") == 0) {
// Number of teller records
if ((tellers = atoi(argv[++i])) <= 0)
return (invarg('t', argv[i]));
}
else if (strcmp(argv[i], "-v") == 0) {
// Verbose option.
verbose = 1;
}
else {
return (usage());
}
}
srand((unsigned int)seed);
accounts = accounts == 0 ? ACCOUNTS : accounts;
branches = branches == 0 ? BRANCHES : branches;
tellers = tellers == 0 ? TELLERS : tellers;
history = history == 0 ? HISTORY : history;
if (verbose)
cout << (long)accounts << " Accounts, "
<< (long)branches << " Branches, "
<< (long)tellers << " Tellers, "
<< (long)history << " History\n";
try {
// Initialize the database environment.
// Must be done in within a try block, unless you
// change the error model in the environment options.
//
TpcbExample app(home, mpool, txn_no_sync ? DB_TXN_NOSYNC : 0);
if (iflag) {
if (ntxns != 0)
return (usage());
app.populate(accounts, branches, history, tellers);
}
else {
if (ntxns == 0)
return (usage());
app.run(ntxns, accounts, branches, tellers);
}
app.close(0);
return (EXIT_SUCCESS);
}
catch (DbException &dbe) {
cerr << "TpcbExample: " << dbe.what() << "\n";
return (EXIT_FAILURE);
}
}
static int
invarg(int arg, char *str)
{
cerr << "TpcbExample: invalid argument for -"
<< (char)arg << ": " << str << "\n";
return (EXIT_FAILURE);
}
static int
usage()
{
cerr << "usage: TpcbExample [-fiv] [-a accounts] [-b branches]\n"
<< " [-c cachesize] [-h home] [-n transactions ]\n"
<< " [-S seed] [-s history] [-t tellers]\n";
return (EXIT_FAILURE);
}
TpcbExample::TpcbExample(const char *home, int cachesize, int flags)
: DbEnv(0)
{
u_int32_t local_flags;
set_error_stream(&cerr);
set_errpfx("TpcbExample");
(void)set_cachesize(0, cachesize == 0 ?
4 * 1024 * 1024 : (u_int32_t)cachesize, 0);
if (flags & (DB_TXN_NOSYNC))
set_flags(DB_TXN_NOSYNC, 1);
flags &= ~(DB_TXN_NOSYNC);
local_flags = flags | DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG |
DB_INIT_MPOOL | DB_INIT_TXN;
open(home, local_flags, 0);
}
//
// Initialize the database to the specified number of accounts, branches,
// history records, and tellers.
//
void
TpcbExample::populate(int accounts, int branches, int history, int tellers)
{
Db *dbp;
int err;
u_int32_t balance, idnum;
u_int32_t end_anum, end_bnum, end_tnum;
u_int32_t start_anum, start_bnum, start_tnum;
idnum = BEGID;
balance = 500000;
dbp = new Db(this, 0);
dbp->set_h_nelem((unsigned int)accounts);
if ((err = dbp->open(NULL, "account", NULL, DB_HASH,
DB_CREATE, 0644)) != 0) {
DbException except("Account file create failed", err);
throw except;
}
start_anum = idnum;
populateTable(dbp, idnum, balance, accounts, "account");
idnum += accounts;
end_anum = idnum - 1;
if ((err = dbp->close(0)) != 0) {
DbException except("Account file close failed", err);
throw except;
}
delete dbp;
if (verbose)
cout << "Populated accounts: "
<< (long)start_anum << " - " << (long)end_anum << "\n";
dbp = new Db(this, 0);
//
// Since the number of branches is very small, we want to use very
// small pages and only 1 key per page. This is the poor-man's way
// of getting key locking instead of page locking.
//
dbp->set_h_ffactor(1);
dbp->set_h_nelem((unsigned int)branches);
dbp->set_pagesize(512);
if ((err = dbp->open(NULL, "branch", NULL, DB_HASH,
DB_CREATE, 0644)) != 0) {
DbException except("Branch file create failed", err);
throw except;
}
start_bnum = idnum;
populateTable(dbp, idnum, balance, branches, "branch");
idnum += branches;
end_bnum = idnum - 1;
if ((err = dbp->close(0)) != 0) {
DbException except("Close of branch file failed", err);
throw except;
}
delete dbp;
if (verbose)
cout << "Populated branches: "
<< (long)start_bnum << " - " << (long)end_bnum << "\n";
dbp = new Db(this, 0);
//
// In the case of tellers, we also want small pages, but we'll let
// the fill factor dynamically adjust itself.
//
dbp->set_h_ffactor(0);
dbp->set_h_nelem((unsigned int)tellers);
dbp->set_pagesize(512);
if ((err = dbp->open(NULL, "teller", NULL, DB_HASH,
DB_CREATE, 0644)) != 0) {
DbException except("Teller file create failed", err);
throw except;
}
start_tnum = idnum;
populateTable(dbp, idnum, balance, tellers, "teller");
idnum += tellers;
end_tnum = idnum - 1;
if ((err = dbp->close(0)) != 0) {
DbException except("Close of teller file failed", err);
throw except;
}
delete dbp;
if (verbose)
cout << "Populated tellers: "
<< (long)start_tnum << " - " << (long)end_tnum << "\n";
dbp = new Db(this, 0);
dbp->set_re_len(HISTORY_LEN);
if ((err = dbp->open(NULL, "history", NULL, DB_RECNO,
DB_CREATE, 0644)) != 0) {
DbException except("Create of history file failed", err);
throw except;
}
populateHistory(dbp, history, accounts, branches, tellers);
if ((err = dbp->close(0)) != 0) {
DbException except("Close of history file failed", err);
throw except;
}
delete dbp;
}
void
TpcbExample::populateTable(Db *dbp,
u_int32_t start_id, u_int32_t balance,
int nrecs, const char *msg)
{
Defrec drec;
memset(&drec.pad[0], 1, sizeof(drec.pad));
Dbt kdbt(&drec.id, sizeof(u_int32_t));
Dbt ddbt(&drec, sizeof(drec));
for (int i = 0; i < nrecs; i++) {
drec.id = start_id + (u_int32_t)i;
drec.balance = balance;
int err;
if ((err =
dbp->put(NULL, &kdbt, &ddbt, DB_NOOVERWRITE)) != 0) {
cerr << "Failure initializing " << msg << " file: "
<< strerror(err) << "\n";
DbException except("failure initializing file", err);
throw except;
}
}
}
void
TpcbExample::populateHistory(Db *dbp, int nrecs, u_int32_t accounts,
u_int32_t branches, u_int32_t tellers)
{
Histrec hrec;
memset(&hrec.pad[0], 1, sizeof(hrec.pad));
hrec.amount = 10;
db_recno_t key;
Dbt kdbt(&key, sizeof(u_int32_t));
Dbt ddbt(&hrec, sizeof(hrec));
for (int i = 1; i <= nrecs; i++) {
hrec.aid = random_id(ACCOUNT, accounts, branches, tellers);
hrec.bid = random_id(BRANCH, accounts, branches, tellers);
hrec.tid = random_id(TELLER, accounts, branches, tellers);
int err;
key = (db_recno_t)i;
if ((err = dbp->put(NULL, &kdbt, &ddbt, DB_APPEND)) != 0) {
DbException except("failure initializing history file",
err);
throw except;
}
}
}
u_int32_t
random_int(u_int32_t lo, u_int32_t hi)
{
u_int32_t ret;
int t;
t = rand();
ret = (u_int32_t)(((double)t / ((double)(RAND_MAX) + 1)) *
(hi - lo + 1));
ret += lo;
return (ret);
}
u_int32_t
random_id(FTYPE type, u_int32_t accounts, u_int32_t branches, u_int32_t tellers)
{
u_int32_t min, max, num;
max = min = BEGID;
num = accounts;
switch (type) {
case TELLER:
min += branches;
num = tellers;
// Fallthrough
case BRANCH:
if (type == BRANCH)
num = branches;
min += accounts;
// Fallthrough
case ACCOUNT:
max = min + num - 1;
}
return (random_int(min, max));
}
void
TpcbExample::run(int n, int accounts, int branches, int tellers)
{
Db *adb, *bdb, *hdb, *tdb;
int failed, ret, txns;
time_t start_time, end_time;
//
// Open the database files.
//
int err;
adb = new Db(this, 0);
if ((err = adb->open(NULL, "account", NULL, DB_UNKNOWN,
DB_AUTO_COMMIT, 0)) != 0) {
DbException except("Open of account file failed", err);
throw except;
}
bdb = new Db(this, 0);
if ((err = bdb->open(NULL, "branch", NULL, DB_UNKNOWN,
DB_AUTO_COMMIT, 0)) != 0) {
DbException except("Open of branch file failed", err);
throw except;
}
tdb = new Db(this, 0);
if ((err = tdb->open(NULL, "teller", NULL, DB_UNKNOWN,
DB_AUTO_COMMIT, 0)) != 0) {
DbException except("Open of teller file failed", err);
throw except;
}
hdb = new Db(this, 0);
if ((err = hdb->open(NULL, "history", NULL, DB_UNKNOWN,
DB_AUTO_COMMIT, 0)) != 0) {
DbException except("Open of history file failed", err);
throw except;
}
(void)time(&start_time);
for (txns = n, failed = 0; n-- > 0;)
if ((ret = txn(adb, bdb, tdb, hdb,
accounts, branches, tellers)) != 0)
++failed;
(void)time(&end_time);
if (end_time == start_time)
++end_time;
// We use printf because it provides much simpler
// formatting than iostreams.
//
printf("%s: %d txns: %d failed, %.2f TPS\n", progname, txns, failed,
(txns - failed) / (double)(end_time - start_time));
(void)adb->close(0);
(void)bdb->close(0);
(void)tdb->close(0);
(void)hdb->close(0);
}
//
// XXX Figure out the appropriate way to pick out IDs.
//
int
TpcbExample::txn(Db *adb, Db *bdb, Db *tdb, Db *hdb,
int accounts, int branches, int tellers)
{
Dbc *acurs = NULL;
Dbc *bcurs = NULL;
Dbc *tcurs = NULL;
DbTxn *t = NULL;
db_recno_t key;
Defrec rec;
Histrec hrec;
int account, branch, teller, ret;
Dbt d_dbt;
Dbt d_histdbt;
Dbt k_dbt;
Dbt k_histdbt(&key, sizeof(key));
// !!!
// This is sample code -- we could move a lot of this into the driver
// to make it faster.
//
account = random_id(ACCOUNT, accounts, branches, tellers);
branch = random_id(BRANCH, accounts, branches, tellers);
teller = random_id(TELLER, accounts, branches, tellers);
k_dbt.set_size(sizeof(int));
d_dbt.set_flags(DB_DBT_USERMEM);
d_dbt.set_data(&rec);
d_dbt.set_ulen(sizeof(rec));
hrec.aid = account;
hrec.bid = branch;
hrec.tid = teller;
hrec.amount = 10;
// Request 0 bytes since we're just positioning.
d_histdbt.set_flags(DB_DBT_PARTIAL);
// START PER-TRANSACTION TIMING.
//
// Technically, TPCB requires a limit on response time, you only get
// to count transactions that complete within 2 seconds. That's not
// an issue for this sample application -- regardless, here's where
// the transaction begins.
if (txn_begin(NULL, &t, 0) != 0)
goto err;
if (adb->cursor(t, &acurs, 0) != 0 ||
bdb->cursor(t, &bcurs, 0) != 0 ||
tdb->cursor(t, &tcurs, 0) != 0)
goto err;
// Account record
k_dbt.set_data(&account);
if (acurs->get(&k_dbt, &d_dbt, DB_SET) != 0)
goto err;
rec.balance += 10;
if (acurs->put(&k_dbt, &d_dbt, DB_CURRENT) != 0)
goto err;
// Branch record
k_dbt.set_data(&branch);
if (bcurs->get(&k_dbt, &d_dbt, DB_SET) != 0)
goto err;
rec.balance += 10;
if (bcurs->put(&k_dbt, &d_dbt, DB_CURRENT) != 0)
goto err;
// Teller record
k_dbt.set_data(&teller);
if (tcurs->get(&k_dbt, &d_dbt, DB_SET) != 0)
goto err;
rec.balance += 10;
if (tcurs->put(&k_dbt, &d_dbt, DB_CURRENT) != 0)
goto err;
// History record
d_histdbt.set_flags(0);
d_histdbt.set_data(&hrec);
d_histdbt.set_ulen(sizeof(hrec));
if (hdb->put(t, &k_histdbt, &d_histdbt, DB_APPEND) != 0)
goto err;
if (acurs->close() != 0 || bcurs->close() != 0 || tcurs->close() != 0)
goto err;
ret = t->commit(0);
t = NULL;
if (ret != 0)
goto err;
// END PER-TRANSACTION TIMING.
return (0);
err:
if (acurs != NULL)
(void)acurs->close();
if (bcurs != NULL)
(void)bcurs->close();
if (tcurs != NULL)
(void)tcurs->close();
if (t != NULL)
(void)t->abort();
if (verbose)
cout << "Transaction A=" << (long)account
<< " B=" << (long)branch
<< " T=" << (long)teller << " failed\n";
return (-1);
}
|