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
|
/*
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2005, 2013 Oracle and/or its affiliates. All rights reserved.
*
* $Id$
*/
#include "bench.h"
#if DB_VERSION_MAJOR > 4 || DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 0
/*
* The in-memory tests don't run on early releases of Berkeley DB.
*/
#undef MEGABYTE
#define MEGABYTE (1024 * 1024)
u_int32_t bulkbufsize = 4 * MEGABYTE;
u_int32_t cachesize = 32 * MEGABYTE;
u_int32_t datasize = 32;
u_int32_t keysize = 8;
u_int32_t logbufsize = 8 * MEGABYTE;
u_int32_t numitems;
u_int32_t pagesize = 32 * 1024;
FILE *fp;
static void op_ds __P((u_int, int));
static void op_ds_bulk __P((u_int, u_int *));
static void op_tds __P((u_int, int, u_int32_t, u_int32_t));
static int usage __P((void));
static void
op_ds(u_int ops, int update)
{
DB_ENV *dbenv;
char *letters = "abcdefghijklmnopqrstuvwxuz";
DB *dbp;
DBT key, data;
char *keybuf, *databuf;
DB_MPOOL_STAT *gsp;
DB_BENCH_ASSERT((keybuf = malloc(keysize)) != NULL);
DB_BENCH_ASSERT((databuf = malloc(datasize)) != NULL);
memset(&key, 0, sizeof(key));
memset(&data, 0, sizeof(data));
key.data = keybuf;
key.size = keysize;
memset(keybuf, 'a', keysize);
data.data = databuf;
data.size = datasize;
memset(databuf, 'b', datasize);
DB_BENCH_ASSERT(db_create(&dbp, NULL, 0) == 0);
dbenv = dbp->dbenv;
dbp->set_errfile(dbp, stderr);
DB_BENCH_ASSERT(dbp->set_pagesize(dbp, pagesize) == 0);
DB_BENCH_ASSERT(dbp->open(
dbp, NULL, NULL, NULL, DB_BTREE, DB_CREATE, 0666) == 0);
(void)dbenv->memp_stat(dbenv, &gsp, NULL, DB_STAT_CLEAR);
if (update) {
TIMER_START;
for (; ops > 0; --ops) {
keybuf[(ops % keysize)] = letters[(ops % 26)];
DB_BENCH_ASSERT(
dbp->put(dbp, NULL, &key, &data, 0) == 0);
}
TIMER_STOP;
} else {
DB_BENCH_ASSERT(dbp->put(dbp, NULL, &key, &data, 0) == 0);
TIMER_START;
for (; ops > 0; --ops)
DB_BENCH_ASSERT(
dbp->get(dbp, NULL, &key, &data, 0) == 0);
TIMER_STOP;
}
if (dbenv->memp_stat(dbenv, &gsp, NULL, 0) == 0)
DB_BENCH_ASSERT(gsp->st_cache_miss == 0);
DB_BENCH_ASSERT(dbp->close(dbp, 0) == 0);
}
static void
op_ds_bulk(u_int ops, u_int *totalp)
{
DB_ENV *dbenv;
DB *dbp;
DBC *dbc;
DBT key, data;
u_int32_t len, klen;
u_int i, total;
char *keybuf, *databuf;
void *pointer, *dp, *kp;
DB_MPOOL_STAT *gsp;
DB_BENCH_ASSERT((keybuf = malloc(keysize)) != NULL);
DB_BENCH_ASSERT((databuf = malloc(bulkbufsize)) != NULL);
memset(&key, 0, sizeof(key));
memset(&data, 0, sizeof(data));
key.data = keybuf;
key.size = keysize;
data.data = databuf;
data.size = datasize;
memset(databuf, 'b', datasize);
DB_BENCH_ASSERT(db_create(&dbp, NULL, 0) == 0);
dbenv = dbp->dbenv;
dbp->set_errfile(dbp, stderr);
DB_BENCH_ASSERT(dbp->set_pagesize(dbp, pagesize) == 0);
DB_BENCH_ASSERT(dbp->set_cachesize(dbp, 0, cachesize, 1) == 0);
DB_BENCH_ASSERT(
dbp->open(dbp, NULL, NULL, NULL, DB_BTREE, DB_CREATE, 0666) == 0);
for (i = 1; i <= numitems; ++i) {
(void)snprintf(keybuf, keysize, "%7d", i);
DB_BENCH_ASSERT(dbp->put(dbp, NULL, &key, &data, 0) == 0);
}
#if 0
fp = fopen("before", "w");
dbp->set_msgfile(dbp, fp);
DB_BENCH_ASSERT (dbp->stat_print(dbp, DB_STAT_ALL) == 0);
#endif
DB_BENCH_ASSERT(dbp->cursor(dbp, NULL, &dbc, 0) == 0);
data.ulen = bulkbufsize;
data.flags = DB_DBT_USERMEM;
(void)dbenv->memp_stat(dbenv, &gsp, NULL, DB_STAT_CLEAR);
TIMER_START;
for (total = 0; ops > 0; --ops) {
DB_BENCH_ASSERT(dbc->c_get(
dbc, &key, &data, DB_FIRST | DB_MULTIPLE_KEY) == 0);
DB_MULTIPLE_INIT(pointer, &data);
while (pointer != NULL) {
DB_MULTIPLE_KEY_NEXT(pointer, &data, kp, klen, dp, len);
if (kp != NULL)
++total;
}
}
TIMER_STOP;
*totalp = total;
if (dbenv->memp_stat(dbenv, &gsp, NULL, 0) == 0)
DB_BENCH_ASSERT(gsp->st_cache_miss == 0);
#if 0
fp = fopen("before", "w");
dbp->set_msgfile(dbp, fp);
DB_BENCH_ASSERT (dbp->stat_print(dbp, DB_STAT_ALL) == 0);
#endif
DB_BENCH_ASSERT(dbp->close(dbp, 0) == 0);
COMPQUIET(dp, NULL);
COMPQUIET(klen, 0);
COMPQUIET(len, 0);
}
static void
op_tds(u_int ops, int update, u_int32_t env_flags, u_int32_t log_flags)
{
DB *dbp;
DBT key, data;
DB_ENV *dbenv;
DB_MPOOL_STAT *gsp;
DB_TXN *txn;
char *keybuf, *databuf;
DB_BENCH_ASSERT((keybuf = malloc(keysize)) != NULL);
DB_BENCH_ASSERT((databuf = malloc(datasize)) != NULL);
memset(&key, 0, sizeof(key));
memset(&data, 0, sizeof(data));
key.data = keybuf;
key.size = keysize;
memset(keybuf, 'a', keysize);
data.data = databuf;
data.size = datasize;
memset(databuf, 'b', datasize);
DB_BENCH_ASSERT(db_env_create(&dbenv, 0) == 0);
dbenv->set_errfile(dbenv, stderr);
/* General environment configuration. */
#ifdef DB_AUTO_COMMIT
DB_BENCH_ASSERT(dbenv->set_flags(dbenv, DB_AUTO_COMMIT, 1) == 0);
#endif
if (env_flags != 0)
DB_BENCH_ASSERT(dbenv->set_flags(dbenv, env_flags, 1) == 0);
/* Logging configuration. */
if (log_flags != 0)
#if DB_VERSION_MINOR >= 7 || DB_VERSION_MAJOR > 4
DB_BENCH_ASSERT(
dbenv->log_set_config(dbenv, log_flags, 1) == 0);
#else
DB_BENCH_ASSERT(dbenv->set_flags(dbenv, log_flags, 1) == 0);
#endif
#ifdef DB_LOG_INMEMORY
if (!(log_flags & DB_LOG_INMEMORY))
#endif
#ifdef DB_LOG_IN_MEMORY
if (!(log_flags & DB_LOG_IN_MEMORY))
#endif
DB_BENCH_ASSERT(dbenv->set_lg_max(dbenv, logbufsize * 10) == 0);
DB_BENCH_ASSERT(dbenv->set_lg_bsize(dbenv, logbufsize) == 0);
DB_BENCH_ASSERT(dbenv->open(dbenv, "TESTDIR",
DB_CREATE | DB_PRIVATE | DB_INIT_LOCK |
DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN, 0666) == 0);
DB_BENCH_ASSERT(db_create(&dbp, dbenv, 0) == 0);
DB_BENCH_ASSERT(dbp->set_pagesize(dbp, pagesize) == 0);
DB_BENCH_ASSERT(dbp->open(
dbp, NULL, TESTFILE, NULL, DB_BTREE, DB_CREATE, 0666) == 0);
if (update) {
(void)dbenv->memp_stat(dbenv, &gsp, NULL, DB_STAT_CLEAR);
TIMER_START;
for (; ops > 0; --ops)
DB_BENCH_ASSERT(
dbp->put(dbp, NULL, &key, &data, 0) == 0);
TIMER_STOP;
if (dbenv->memp_stat(dbenv, &gsp, NULL, 0) == 0)
DB_BENCH_ASSERT(gsp->st_page_out == 0);
} else {
DB_BENCH_ASSERT(dbp->put(dbp, NULL, &key, &data, 0) == 0);
(void)dbenv->memp_stat(dbenv, &gsp, NULL, DB_STAT_CLEAR);
TIMER_START;
for (; ops > 0; --ops) {
DB_BENCH_ASSERT(
dbenv->txn_begin(dbenv, NULL, &txn, 0) == 0);
DB_BENCH_ASSERT(
dbp->get(dbp, NULL, &key, &data, 0) == 0);
DB_BENCH_ASSERT(txn->commit(txn, 0) == 0);
}
TIMER_STOP;
if (dbenv->memp_stat(dbenv, &gsp, NULL, 0) == 0)
DB_BENCH_ASSERT(gsp->st_cache_miss == 0);
}
DB_BENCH_ASSERT(dbp->close(dbp, 0) == 0);
DB_BENCH_ASSERT(dbenv->close(dbenv, 0) == 0);
}
#define DEFAULT_OPS 1000000
int
b_inmem(int argc, char *argv[])
{
extern char *optarg;
extern int optind;
u_int ops, total;
int ch;
if ((progname = strrchr(argv[0], '/')) == NULL)
progname = argv[0];
else
++progname;
ops = 0;
while ((ch = getopt(argc, argv, "b:C:d:k:l:o:P:")) != EOF)
switch (ch) {
case 'b':
bulkbufsize = (u_int32_t)atoi(optarg);
break;
case 'C':
cachesize = (u_int32_t)atoi(optarg);
break;
case 'd':
datasize = (u_int)atoi(optarg);
break;
case 'k':
keysize = (u_int)atoi(optarg);
break;
case 'l':
logbufsize = (u_int32_t)atoi(optarg);
break;
case 'o':
ops = (u_int)atoi(optarg);
break;
case 'P':
pagesize = (u_int32_t)atoi(optarg);
break;
case '?':
default:
return (usage());
}
argc -= optind;
argv += optind;
if (argc != 1)
return (usage());
numitems = (cachesize / (keysize + datasize - 1)) / 2;
if (strcasecmp(argv[0], "read") == 0) {
if (ops == 0)
ops = DEFAULT_OPS;
op_ds(ops, 0);
printf(
"# %u in-memory Btree database reads of %u/%u byte key/data pairs\n",
ops, keysize, datasize);
} else if (strcasecmp(argv[0], "bulk") == 0) {
if (keysize < 8) {
fprintf(stderr,
"%s: bulk read requires a key size >= 10\n", progname);
return (EXIT_FAILURE);
}
/*
* The ops value is the number of bulk operations, not key get
* operations. Reduce the value so the test doesn't take so
* long, and use the returned number of retrievals as the ops
* value for timing purposes.
*/
if (ops == 0)
ops = 100000;
op_ds_bulk(ops, &total);
ops = total;
printf(
"# %u bulk in-memory Btree database reads of %u/%u byte key/data pairs\n",
ops, keysize, datasize);
} else if (strcasecmp(argv[0], "write") == 0) {
if (ops == 0)
ops = DEFAULT_OPS;
op_ds(ops, 1);
printf(
"# %u in-memory Btree database writes of %u/%u byte key/data pairs\n",
ops, keysize, datasize);
} else if (strcasecmp(argv[0], "txn-read") == 0) {
if (ops == 0)
ops = DEFAULT_OPS;
op_tds(ops, 0, 0, 0);
printf(
"# %u transactional in-memory Btree database reads of %u/%u %s",
ops, keysize, datasize, "byte key/data pairs\n");
} else if (strcasecmp(argv[0], "txn-write") == 0) {
if (ops == 0)
ops = DEFAULT_OPS;
#if defined(DB_LOG_INMEMORY) || defined(DB_LOG_IN_MEMORY)
#if defined(DB_LOG_INMEMORY)
op_tds(ops, 1, 0, DB_LOG_INMEMORY);
#else
op_tds(ops, 1, 0, DB_LOG_IN_MEMORY);
#endif
printf(
"# %u transactional in-memory logging Btree database writes of %u/%u%s",
ops, keysize, datasize, " byte key/data pairs\n");
#else
return (EXIT_SUCCESS);
#endif
} else if (strcasecmp(argv[0], "txn-nosync") == 0) {
if (ops == 0)
ops = DEFAULT_OPS;
op_tds(ops, 1, DB_TXN_NOSYNC, 0);
printf(
"# %u transactional nosync logging Btree database writes of %u/%u %s",
ops, keysize, datasize, "byte key/data pairs\n");
} else if (strcasecmp(argv[0], "txn-write-nosync") == 0) {
if (ops == 0)
ops = DEFAULT_OPS;
#ifdef DB_TXN_WRITE_NOSYNC
op_tds(ops, 1, DB_TXN_WRITE_NOSYNC, 0);
printf(
"# %u transactional OS-write/nosync logging Btree database writes of %u/%u%s",
ops, keysize, datasize, " byte key/data pairs\n");
#else
return (EXIT_SUCCESS);
#endif
} else if (strcasecmp(argv[0], "txn-sync") == 0) {
/*
* Flushing to disk takes a long time, reduce the number of
* default ops.
*/
if (ops == 0)
ops = 100000;
op_tds(ops, 1, 0, 0);
printf(
"# %u transactional logging Btree database writes of %u/%u %s",
ops, keysize, datasize, "byte key/data pairs\n");
} else {
fprintf(stderr, "%s: unknown keyword %s\n", progname, argv[0]);
return (EXIT_FAILURE);
}
TIMER_DISPLAY(ops);
return (EXIT_SUCCESS);
}
static int
usage()
{
fprintf(stderr, "usage: %s %s%s%s%s",
progname, "[-b bulkbufsz] [-C cachesz]\n\t",
"[-d datasize] [-k keysize] [-l logbufsz] [-o ops] [-P pagesz]\n\t",
"[read | bulk | write | txn-read |\n\t",
"txn-write | txn-nosync | txn-write-nosync | txn-sync]\n");
return (EXIT_FAILURE);
}
#else
int
b_inmem(int argc, char *argv[])
{
COMPQUIET(argc, 0);
COMPQUIET(argv, NULL);
return (0);
}
#endif
|