Debian Codegraph @

build_vxworks/util/db_tuner.c

raw
1 /*
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2011, 2013 Oracle and/or its affiliates. All rights reserved.
5 *
6 * $Id$
7 *
8 * This utility choses a reasonable pagesize for a BTREE database.
9 *
10 * Here we assume that:
11 * 1) This set of records are already in a BTREE database, which may been
12 * configured with a unreasonable page size.
13 * 2) Treat the database as if it were compacted.
14 * 3) The goal is to optimize the database for the current content,
15 * rather than for ongoing insertions.
16 *
17 * The page size of a BTREE can be 512, 1024, 2048, 4096, 8192, 16384,
18 * 32768, 65536, totally 8 different cases. So we have 8 possible BTREE,
19 * each with different pagesize to contain them. Without actually creating
20 * those 8 databases, this utility tries to simulate situations, that is,
21 * for each pagesize, how many leaf pages, over flow pages and duplicate
22 * pages are needed, and what's the distribution of each kind of pages
23 * based on their fill factor.
24 *
25 * db_tuner contains 2 parts:
26 *
27 * I) Simulation of 8 different pagesized databases.
28 * This includes, the number of leaf pages, overflow pages caused by
29 * big key/data in leaf layers, and duplicate pages, the distribution
30 * of each kind of pages in different fill factor ranges.
31 *
32 * This is achieved by retrieving those records from existing btree and
33 * inserting them into different kind of pages. Since the records from
34 * the btree are sorted, they are inserted into the end of each page.
35 * If this page become full, that is no enough space, only this new
36 * record will be put into next new page.
37 *
38 * II) Recommend the best page size.
39 * From our simulation results, this utility choose a page size based on
40 * the number of overflow pages and storage (on-disk space).
41 * If there is no overflow pages, then choose the one resulting in
42 * the smallest storage as the recommended page size. Otherwise,
43 * choose the one that results in a reasonable small number of overflow pages.
44 */
45
46 #include "db_config.h"
47
48 #include <assert.h>
49 #include <math.h>
50
51 #include "db_int.h"
52 #include "dbinc/db_page.h"
53 #include "dbinc/btree.h"
54
55 #ifndef lint
56 static const char copyright[] =
57 "Copyright (c) 2011, 2013 Oracle and/or its affiliates. All rights reserved.\n";
58 #endif
59
60 /*
61 * Fill factor distribution division,
62 * e.g., [0.000 - 0.099], ..., [0.900 - 0.999], [1.000- 1.000]
63 */
64 #define DIST_DIVISION 11
65
66 /* Error return code in db_tuner, different with those in src\dbinc\db.in. */
67 /* Dist >= DIST_DIVISION. */
68 #define EXIT_DIST_OUTRANGE (-31000)
69 /* "Insert" zero needed. */
70 #define EXIT_INSERT_ZERO_NEED (-31001)
71 /* On-page duplicate set = 0. */
72 #define EXIT_INSERT_ZERO_ONPGDUP (-31002)
73
74 /* Follows are some special "insert" types based on the data. */
75 /* Insert as normal case. */
76 #define INSERT_NORMAL 0x0001
77 /* Nothing is inserted. */
78 #define INSERT_NOTHING 0x0002
79 /* No key but a slot is inserted. */
80 #define INSERT_SLOT 0x0003
81 /* A B_DUPLICATE point is inserted. */
82 #define INSERT_BDUPLICATE 0x0004
83
84 /*
85 * Page size of BTREE can be from DB_MIN_PGSIZE (512) to DB_MAX_PGSIZE(64K),
86 * and all the pagesize is the power of 2, so we have 8 possible cases.
87 *
88 * 8 is from ((int)(log(DB_MAX_PGSIZE) - log(DB_MIN_PGSIZE) + 1)).
89 */
90 #define NUM_PGSIZES 8
91
92 /* Structure used to store statistics of the assessment. */
93 typedef struct __tuner_ff_stat {
94 uintmax_t pgsize_leaf_dist[NUM_PGSIZES][DIST_DIVISION];
95 uintmax_t pgsize_ovfl_dist[NUM_PGSIZES][DIST_DIVISION];
96 uintmax_t pgsize_dup_dist[NUM_PGSIZES][DIST_DIVISION];
97
98 /* Info used to track stats across page in a traverse. */
99 u_int32_t pg_leaf_offset[NUM_PGSIZES];
100 u_int32_t pg_dup_offset[NUM_PGSIZES];
101 }TUNER_FF_STAT;
102
103 static int db_tuner___tuner_analyze_btree __P((DB_ENV *, DB *, u_int32_t));
104 static int db_tuner___tuner_ff_stat_callback __P((DBC *, PAGE *, void *, int *));
105 static int db_tuner___tuner_generate_fillfactor_stats __P((DB_ENV *, DB *,
106 TUNER_FF_STAT *));
107 static int db_tuner___tuner_insert_dupdata __P((DB *, u_int32_t, int, TUNER_FF_STAT *));
108 static int db_tuner___tuner_insert_kvpair __P((DB *, u_int32_t, u_int32_t, int, int,
109 int, TUNER_FF_STAT *));
110 static int db_tuner___tuner_leaf_page __P((DBC *, PAGE *, TUNER_FF_STAT *));
111 static int db_tuner___tuner_leaf_dupdata __P((DBC*, PAGE *, int, int, u_int32_t,
112 TUNER_FF_STAT *));
113 static int db_tuner___tuner_leaf_dupdata_entries __P((DBC *, PAGE *, int, int, int, int,
114 TUNER_FF_STAT *));
115 static int db_tuner___tuner_opd_data_entries __P((DBC *, PAGE *, int, int,
116 TUNER_FF_STAT *));
117 static int db_tuner___tuner_opd_data __P((DBC *, PAGE *, int, int, TUNER_FF_STAT *));
118 static int db_tuner___tuner_opd_page __P((DBC *, PAGE *, TUNER_FF_STAT *));
119 static int db_tuner___tuner_print_btree_fillfactor __P((u_int32_t, TUNER_FF_STAT *));
120 static int db_tuner___tuner_record_dup_pg __P((int, TUNER_FF_STAT *));
121 static int db_tuner___tuner_record_last_opd __P((int, TUNER_FF_STAT *));
122 static int db_tuner___tuner_record_leaf_pg __P((int, TUNER_FF_STAT *));
123 static int db_tuner___tuner_record_ovfl_pg __P((u_int32_t, int, TUNER_FF_STAT *));
124
125 static int db_tuner_get_opd_size __P((DBC*, PAGE*, u_int32_t*));
126 static int db_tuner_item_size __P((DB *, PAGE *, db_indx_t));
127 static int db_tuner_item_space __P((DB *, PAGE *, db_indx_t));
128 int db_tuner_main __P((int, char *[]));
129 static int db_tuner_open_db __P((DB **, DB_ENV *, char *, char *));
130 static int db_tuner_sum_opd_page_data_entries __P((DB *, PAGE *));
131 static int db_tuner_usage __P((void));
132 static int db_tuner_version_check __P((void));
133
134 const char *progname = "db_tuner";
135
136 int
137 db_tuner(args)
138 char *args;
139 {
140 int argc;
141 char **argv;
142
143 __db_util_arg("db_tuner", args, &argc, &argv);
144 return (db_tuner_main(argc, argv) ? EXIT_FAILURE : EXIT_SUCCESS);
145 }
146
147 #include <stdio.h>
148 #define ERROR_RETURN ERROR
149
150 int
151 db_tuner_main(argc, argv)
152 int argc;
153 char *argv[];
154 {
155 extern char *optarg;
156 DB *dbp;
157 DB_ENV *dbenv;
158 DBTYPE dbtype;
159 char *dbname, *home, *subdb;
160 int ch, is_set_dbfile, ret;
161 u_int32_t cachesize, verbose;
162
163 if ((ret = db_tuner_version_check()) != 0)
164 return (ret);
165
166 dbenv = NULL;
167 dbp = NULL;
168 cachesize = 0;
169 dbname = home = subdb = NULL;
170 is_set_dbfile = verbose = 0;
171 dbtype = DB_UNKNOWN;
172
173 __db_getopt_reset = 1;
174 while ((ch = getopt(argc, argv, "c:d:h:vs:")) != EOF)
175 switch (ch) {
176 case 'c':
177 cachesize = atoi(optarg);
178 break;
179 case 'd':
180 dbname = optarg;
181 is_set_dbfile = 1;
182 break;
183 case 'h':
184 home = optarg;
185 break;
186 case 's':
187 subdb = optarg;
188 break;
189 case 'v':
190 verbose = 1;
191 break;
192 default:
193 db_tuner_usage();
194 }
195
196 /* Handle possible interruptions. */
197 __db_util_siginit();
198
199 if (!is_set_dbfile)
200 db_tuner_usage();
201
202 if ((ret = db_env_create(&dbenv, 0)) != 0) {
203 fprintf(stderr, "%s: db_env_create: %s\n",
204 progname, db_strerror(ret));
205 goto err;
206 }
207
208 dbenv->set_errfile(dbenv, stderr);
209 dbenv->set_errpfx(dbenv, progname);
210
211 if ((cachesize != 0) && (ret =
212 dbenv->set_cachesize(dbenv, (u_int32_t)0, cachesize, 1)) != 0) {
213 dbenv->err(dbenv, ret, "DB_ENV->set_cachesize:");
214 goto err;
215 }
216
217 /*
218 * If attaching to a pre-existing environment fails, create a
219 * private one and try again.
220 */
221 if ((ret = dbenv->open(dbenv, home, DB_USE_ENVIRON, 0)) != 0 &&
222 (ret == DB_VERSION_MISMATCH || ret == DB_REP_LOCKOUT ||
223 (ret = dbenv->open(dbenv, home,
224 DB_CREATE | DB_INIT_MPOOL | DB_USE_ENVIRON | DB_PRIVATE,
225 0)) != 0)) {
226 dbenv->err(dbenv, ret, "DB_ENV->open:");
227 goto err;
228 }
229
230 if ((ret = db_tuner_open_db(&dbp, dbenv, dbname, subdb)) != 0) {
231 dbenv->err(dbenv, ret, "open_db:");
232 goto err;
233 }
234
235 if ((ret = dbp->get_type(dbp, &dbtype)) != 0) {
236 dbenv->err(dbenv, ret, "DB->get_type:");
237 goto err;
238 }
239
240 switch (dbtype) {
241 case DB_BTREE:
242 if ((ret = db_tuner___tuner_analyze_btree(dbenv, dbp, verbose)) != 0)
243 dbenv->err(dbenv, ret, "__tuner_analyze_btree fails.");
244 break;
245 default:
246 dbenv->errx(dbenv, DB_STR("5001",
247 "%s: Unsupported database type"), progname);
248 }
249
250 err:
251 if (dbp != NULL && (ret = dbp->close(dbp, 0)) != 0)
252 dbenv->err(dbenv, ret, "DB->close: %s", dbname);
253
254 if (dbenv != NULL && (ret = dbenv->close(dbenv, 0)) != 0)
255 fprintf(stderr, "%s: dbenv->close: %s", progname,
256 db_strerror(ret));
257
258 /* Resend any caught signal. */
259 __db_util_sigresend();
260
261 return (ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
262 }
263
264 /*
265 * Generate the simulated statistics for each different btree pagesize,
266 * then print out this information if verbose enabled, finally make our
267 * recommendation of our best pagesize based on our simulated results.
268 */
269 static int
270 db_tuner___tuner_analyze_btree(dbenv, dbp, verbose)
271 DB_ENV *dbenv;
272 DB *dbp;
273 u_int32_t verbose;
274 {
275 TUNER_FF_STAT stats;
276 int ret;
277
278 memset(&stats, 0, sizeof(TUNER_FF_STAT));
279
280 if ((ret = db_tuner___tuner_generate_fillfactor_stats(dbenv, dbp,
281 &stats)) != 0) {
282 dbenv->err(dbenv, ret,
283 "__tuner_generate_fillfactor_stats fails.");
284 return (ret);
285 }
286
287 (void)db_tuner___tuner_print_btree_fillfactor(verbose, &stats);
288
289 return (EXIT_SUCCESS);
290 }
291
292 /* Traverse the database to gather simulated statistics for each pagesize.*/
293 static int
294 db_tuner___tuner_generate_fillfactor_stats(dbenv, dbp, stats)
295 DB_ENV *dbenv;
296 DB *dbp;
297 TUNER_FF_STAT *stats;
298 {
299 DBC *dbc;
300 int i, ret, t_ret;
301
302 ret = t_ret = 0;
303
304 if ((ret = dbp->cursor(dbp, NULL, &dbc, 0)) != 0) {
305 dbenv->err(dbenv, ret, "DB_ENV->cursor:");
306 return (ret);
307 }
308
309 /*
310 * Call the internal Berkeley DB function, that triggers a callback
311 * for each page in a btree database.
312 */
313 if ((ret = __bam_traverse(dbc, DB_LOCK_READ, PGNO_INVALID,
314 db_tuner___tuner_ff_stat_callback, (void *)stats)) != 0) {
315 dbenv->err(dbenv, ret, "__bam_traverse:");
316 goto err;
317 }
318
319 /*
320 * Record the last simulated page for leaf and dup page,
321 * which ensure at least one page is used.
322 */
323 for (i = 0; i < NUM_PGSIZES; ++i) {
324 if (stats->pg_leaf_offset[i] > 0 &&
325 (ret = db_tuner___tuner_record_leaf_pg(i, stats)) != 0) {
326 dbenv->err(dbenv, ret, "__tuner_record_leaf");
327 break;
328 }
329
330 if (stats->pg_dup_offset[i] > 0 &&
331 (ret = db_tuner___tuner_record_dup_pg(i, stats)) != 0) {
332 dbenv->err(dbenv, ret, "__tuner_record_dup_pg");
333 break;
334 }
335 }
336
337 err:
338 if(dbc != NULL && (t_ret = dbc->close(dbc)) != 0)
339 dbenv->err(dbenv, t_ret, "DBC->close:");
340
341 if (ret == 0 && t_ret != 0)
342 ret = t_ret;
343
344 return (ret);
345 }
346
347 /*
348 * This callback is used in __bam_traverse. When traversing each page in
349 * the BTREE, it retrieves each record for simulation.
350 */
351 static int
352 db_tuner___tuner_ff_stat_callback(dbc, h, cookie, putp)
353 DBC *dbc;
354 PAGE *h;
355 void *cookie;
356 int *putp;
357 {
358 DB_ENV *dbenv;
359 int ret;
360
361 dbenv = dbc->dbenv;
362 *putp = 0;
363
364 switch (TYPE(h)) {
365 case P_LBTREE:
366 if ((ret = db_tuner___tuner_leaf_page(dbc, h, cookie)) != 0) {
367 dbenv->err(dbenv, ret, "__tuner_leaf_page");
368 return (ret);
369 }
370 break;
371 case P_LDUP:
372 case P_LRECNO:
373 /* Coming a new off-page duplicate set.*/
374 if (h->prev_pgno == PGNO_INVALID &&
375 (ret = db_tuner___tuner_opd_page(dbc, h, cookie)) != 0) {
376 dbenv->err(dbenv, ret, "__tuner_opd_page:");
377 return (ret);
378 }
379 break;
380 case P_IBTREE:
381 case P_IRECNO:
382 case P_OVERFLOW:
383 break;
384 default:
385 return (EXIT_FAILURE);
386 }
387
388 return (EXIT_SUCCESS);
389 }
390
391 /*
392 * Deal with the leaf page of existing database. This includes:
393 * 1: determine the on-page duplicate set, and calculate its total size
394 * 2: decise where should this set go (on-page or off-page) in the later
395 * simulation stage and do some "movement".
396 * 3: "move" the unique key data pairs to the simulated leaf pages.
397 */
398 static int
399 db_tuner___tuner_leaf_page(dbc, h, cookie)
400 DBC *dbc;
401 PAGE *h;
402 TUNER_FF_STAT *cookie;
403 {
404 DB *dbp;
405 DB_ENV *dbenv;
406 db_indx_t findx, lindx, indx, *inp, top;
407 u_int32_t data_sz, key_sz, onpd_sz;
408 int i, ret, in_data_type;
409
410 dbp = dbc->dbp;
411 dbenv = dbp->dbenv;
412 /*
413 * Use some macros from db_page.h to retrieve information from the
414 * page. P_INP retrieves the offset to the start of page index array.
415 * NUM_ENT retrieves the number of items on the page.
416 */
417 inp = P_INP(dbp, h);
418 top = NUM_ENT(h);
419 ret = 0;
420
421 for (indx = 0; indx < top;) {
422 /*
423 * If on-page duplicate, first calculate the total size,
424 * including one key and all data.
425 */
426 onpd_sz = 0;
427 if ((indx + P_INDX) < top && inp[indx] == inp[indx + P_INDX]) {
428
429 /* Ignore deleted items. */
430 if (B_DISSET(GET_BKEYDATA(dbp, h, indx)->type))
431 continue;
432
433 /* Count the key once. */
434 onpd_sz += db_tuner_item_space(dbp, h, indx);
435
436 for (findx = indx;
437 indx < top && inp[findx] == inp[indx];
438 indx += P_INDX) {
439 /* Ignore deleted items. */
440 if (B_DISSET(GET_BKEYDATA(dbp, h,
441 indx + O_INDX)->type))
442 continue;
443 /* Count all the data items. */
444 onpd_sz += db_tuner_item_space(dbp, h, indx + O_INDX);
445 }
446
447 /*Indx range of on-page duplicate set: [findx, lindx)*/
448 lindx = indx;
449
450 if (onpd_sz == 0)
451 return (EXIT_INSERT_ZERO_ONPGDUP);
452
453 /* "Move" on-page duplicate set to simualted pages.*/
454 if ((ret = db_tuner___tuner_leaf_dupdata(dbc, h, findx, lindx,
455 onpd_sz, cookie)) != 0) {
456 dbenv->err(dbenv, ret, "__tuner_leaf_dupdata");
457 return (ret);
458 }
459 } else {
460 in_data_type = INSERT_NORMAL;
461 /* Ignore deleted items. */
462 if (B_DISSET(GET_BKEYDATA(dbp, h, indx)->type))
463 continue;
464
465 /* First consider key. */
466 key_sz = db_tuner_item_size(dbp, h, indx);
467
468 /* Ignore deleted items. */
469 if (B_DISSET(GET_BKEYDATA(dbp, h, indx + O_INDX)->type))
470 continue;
471
472 /* next consider data.*/
473 if (B_TYPE(GET_BKEYDATA(dbp, h,
474 indx + O_INDX)->type) == B_DUPLICATE) {
475 /*
476 * Off-page duplicate set is not handled here
477 * but on the duplicate pages.
478 * Here the key is inserted into "simulated"
479 * leaf_page.
480 */
481 in_data_type = INSERT_NOTHING;
482 data_sz = 0;
483 } else
484 data_sz = db_tuner_item_size(dbp, h, indx + O_INDX);
485
486 for (i = 0; i < NUM_PGSIZES; ++i) {
487 if ((ret = db_tuner___tuner_insert_kvpair(dbp, key_sz,
488 data_sz, i, INSERT_NORMAL, in_data_type,
489 cookie)) != 0) {
490 dbenv->err(dbenv, ret,
491 "__tuner_insert_kvpair");
492 break;
493 }
494 }
495
496 indx += P_INDX;
497 }
498 }
499
500 return (ret);
501 }
502
503 /*
504 * "Move" the on-page duplicate data set from the specific page to our
505 * simulated databases (Indx range of this on-page duplicate set:
506 * [findx, lindx)), it includes following steps:
507 *
508 * First check where should this set go (on-page or off-page duplicate tree).
509 *
510 * This is determined as "If total size of duplicate data set is more than 25%
511 * of a specific page size, then this set go to off-page duplicate tree.
512 * Otherwise, it goes to on-page duplicate. "
513 *
514 * Then "move" this duplicate set to our simulated pages in each simulated
515 * database.
516 */
517 static int
518 db_tuner___tuner_leaf_dupdata(dbc, h, findx, lindx, dup_sz, cookie)
519 DBC *dbc;
520 PAGE *h;
521 int findx, lindx;
522 u_int32_t dup_sz;
523 TUNER_FF_STAT *cookie;
524 {
525 DB_ENV *dbenv;
526 int i, is_opd, ret;
527 u_int32_t pgsize;
528
529 dbenv = dbc->dbenv;
530
531 for (i = 0; i < NUM_PGSIZES; ++i) {
532 pgsize = (1 << i) * DB_MIN_PGSIZE;
533
534 /* Check whether this duplicate set go to opd? */
535 is_opd = (dup_sz < (pgsize / 4)) ? 0 : 1;
536
537 /* "Move" this on-page duplicate to our simulated pages. */
538 if ((ret = db_tuner___tuner_leaf_dupdata_entries(dbc, h, findx,
539 lindx, i, is_opd, cookie)) != 0) {
540 dbenv->err(dbenv, ret, "__tuner_leaf_dupdata_entries");
541 return (ret);
542 }
543
544 /*
545 * Record the last simulated duplicate pages for a finished
546 * off-page duplicate set then reset the offset to zero
547 * for next opd set.
548 */
549 if (is_opd &&
550 (ret = db_tuner___tuner_record_last_opd(i, cookie)) != 0) {
551 dbenv->err(dbenv, ret, "__tuner_record_last_opd");
552 return (ret);
553 }
554 }
555
556 return (EXIT_SUCCESS);
557 }
558
559 /*
560 * "Move" the on-page duplicate set [findx, lindx) on the specific page to
561 * simulated database with pagesize = (1 << indx_pgsz) * DB_MIN_PGSIZE;
562 */
563 static int
564 db_tuner___tuner_leaf_dupdata_entries(dbc, h, findx, lindx, indx_pgsz, is_opd, cookie)
565 DBC *dbc;
566 PAGE *h;
567 int findx, lindx, indx_pgsz, is_opd;
568 TUNER_FF_STAT *cookie;
569 {
570 DB *dbp;
571 DB_ENV *dbenv;
572 db_indx_t indx;
573 u_int32_t data_sz, key_sz;
574 int ret, in_key_type;
575
576 dbp = dbc->dbp;
577 dbenv = dbp->dbenv;
578
579 for (indx = findx; indx < lindx; indx += P_INDX) {
580
581 key_sz = 0;
582 in_key_type = INSERT_SLOT;
583 /*
584 * For on-page duplicate data, the key is inserted once,
585 * then its corresponding data.
586 */
587 if (indx == findx) {
588 /* Ignore deleted items. */
589 if (B_DISSET(GET_BKEYDATA(dbp, h, indx)->type))
590 continue;
591
592 key_sz = db_tuner_item_size(dbp, h, indx);
593 in_key_type = INSERT_NORMAL;
594
595 /*
596 * If is_opd, then insert a key + B_DUPLICATE pair for
597 * this on-page duplicate to simulated leaf page.
598 * INSERT_BDUPLICATE: B_DUPLICATE point.
599 */
600 if (is_opd && (ret =
601 db_tuner___tuner_insert_kvpair(dbp, key_sz, 0, indx_pgsz,
602 INSERT_NORMAL, INSERT_BDUPLICATE, cookie)) != 0) {
603 dbenv->err(dbenv, ret, "__tuner_insert_kvpair");
604 return (ret);
605 }
606 }
607
608 /* Ignore deleted items. */
609 if (B_DISSET(GET_BKEYDATA(dbp, h, indx + O_INDX)->type))
610 continue;
611
612 data_sz = db_tuner_item_size(dbp, h, indx + O_INDX);
613
614 if (is_opd) {
615 ret = db_tuner___tuner_insert_dupdata(dbp, data_sz,
616 indx_pgsz, cookie);
617 if (ret != 0) {
618 dbenv->err(dbenv, ret,
619 "__tuner_insert_dupdata");
620 return (ret);
621 }
622 } else {
623 ret = db_tuner___tuner_insert_kvpair(dbp, key_sz, data_sz,
624 indx_pgsz, in_key_type, INSERT_NORMAL,
625 cookie);
626
627 if (ret != 0) {
628 dbenv->err(dbenv, ret, "__tuner_insert_kvpair");
629 return (ret);
630 }
631 }
632 }
633 return (EXIT_SUCCESS);
634 }
635
636 /* Tuner the off-page duplicate pages from existing database. */
637 static int
638 db_tuner___tuner_opd_page(dbc, h, cookie)
639 DBC *dbc;
640 PAGE *h;
641 TUNER_FF_STAT *cookie;
642 {
643 DB_ENV *dbenv;
644 u_int32_t opd_sz, pgsize;
645 int i, is_opd, ret;
646
647 dbenv = dbc->dbenv;
648 ret = opd_sz = 0;
649
650 /* 1st calculate the total size of the duplicate set. */
651 if ((ret = db_tuner_get_opd_size(dbc, h, &opd_sz)) != 0) {
652 dbenv->err(dbenv, ret, "get_opd_size:");
653 return (ret);
654 }
655
656 /* 2nd insert this set into "simulated" pages for each page size.*/
657 for (i = 0; i < NUM_PGSIZES; ++i) {
658 pgsize = (1 << i) * DB_MIN_PGSIZE;
659
660 /* Check whether this duplicate set go to opd? */
661 is_opd = (opd_sz < (pgsize / 4)) ? 0 : 1;
662
663 if ((ret = db_tuner___tuner_opd_data(dbc, h, i, is_opd, cookie)) != 0) {
664 dbenv->err(dbenv, ret, "__tuner_opd_data:");
665 break;
666 }
667 }
668
669 return (ret);
670 }
671
672 /* "Move" all the off-page duplicate data into simulated on-page or off-page.*/
673 static int
674 db_tuner___tuner_opd_data(dbc, h, indx_pgsz, is_opd, cookie)
675 DBC *dbc;
676 PAGE *h;
677 int indx_pgsz, is_opd;
678 TUNER_FF_STAT *cookie;
679 {
680 DB *dbp;
681 DB_ENV *dbenv;
682 DB_MPOOLFILE *mpf;
683 PAGE *p;
684 db_pgno_t next_pgno;
685 u_int32_t pgsize;
686 int ret;
687
688 dbp = dbc->dbp;
689 dbenv = dbp->dbenv;
690 mpf = dbp->mpf;
691
692 p = h;
693 pgsize = (1 << indx_pgsz) * DB_MIN_PGSIZE;
694
695 /*
696 * __tuner_leaf_page has inserted one key for each opd already,
697 * so here only a B_DUPLICATE point is inserted into simulate
698 * leaf page if this duplicate set goes to on-page.
699 */
700 if (is_opd) {
701 ret = db_tuner___tuner_insert_kvpair(dbp, 0, 0, indx_pgsz,
702 INSERT_NOTHING, INSERT_BDUPLICATE, cookie);
703
704 if (ret!= 0) {
705 dbenv->err(dbenv, ret, "__tuner_insert_kvpair");
706 return (ret);
707 }
708 }
709
710 /* Next insert all the data of this duplicate set. */
711 while (1) {
712 ret = db_tuner___tuner_opd_data_entries(dbc, h, indx_pgsz, is_opd,
713 cookie);
714 if (ret != 0) {
715 dbenv->err(dbenv, ret, "__tuner_opd_data_entries");
716 return (ret);
717 }
718
719 next_pgno = p->next_pgno;
720
721 if (p != h && (ret = mpf->put(mpf, p, dbc->priority, 0)) != 0) {
722 dbenv->err(dbenv, ret, "DB_MPOOLFILE->put:");
723 return (ret);
724 }
725
726 if (next_pgno == PGNO_INVALID)
727 break;
728
729 if ((ret = mpf->get(mpf, &next_pgno, dbc->txn, 0, &p)) != 0) {
730 dbenv->err(dbenv, ret, "DB_MPOOLFILE->get:");
731 return (ret);
732 }
733 }
734
735 /* Record the last simulate duplicate page if goto off-page duplicate*/
736 if (is_opd && (ret = db_tuner___tuner_record_last_opd(indx_pgsz, cookie)) != 0) {
737 dbenv->err(dbenv, ret, "__tuner_record_last_opd");
738 return (ret);
739 }
740
741 return (EXIT_SUCCESS);
742 }
743
744 /*
745 * "Move" the off-page duplicate data set to our simulated on-page or
746 * off-page.
747 */
748 static int
749 db_tuner___tuner_opd_data_entries(dbc, h, indx_pgsz, is_opd, cookie)
750 DBC *dbc;
751 PAGE *h;
752 int indx_pgsz, is_opd;
753 TUNER_FF_STAT *cookie;
754 {
755 DB *dbp;
756 DB_ENV *dbenv;
757 db_indx_t indx;
758 u_int32_t data_sz;
759 int ret;
760
761 dbp = dbc->dbp;
762 dbenv = dbp->dbenv;
763
764 for (indx = 0; indx < NUM_ENT(h); indx += O_INDX) {
765 /* Ignore deleted items. */
766 if (B_DISSET(GET_BKEYDATA(dbp, h, indx)->type))
767 continue;
768
769 data_sz = db_tuner_item_size(dbp, h, indx);
770
771 if (is_opd) {
772 ret = db_tuner___tuner_insert_dupdata(dbp, data_sz,
773 indx_pgsz, cookie);
774
775 if (ret != 0) {
776 dbenv->err(dbenv, ret,
777 "__tuner_insert_dupdata");
778 return (ret);
779 }
780 } else {
781 /*
782 * __tuner_leaf_page has inserted one key for each
783 * opd already (this will insert moment later),
784 * so only data items and key slots are inserted.
785 */
786 ret = db_tuner___tuner_insert_kvpair(dbp, 0, data_sz,
787 indx_pgsz, INSERT_SLOT, INSERT_NORMAL,
788 cookie);
789
790 if (ret != 0) {
791 dbenv->err(dbenv, ret,
792 "__tuner_insert_kvpair");
793 return (ret);
794 }
795 }
796 }
797 return (EXIT_SUCCESS);
798 }
799
800 /*
801 * Try to insert a key and data pair into simulated leaf pages.
802 * Key and data pairs are always stored (or referenced) on the same leaf page.
803 */
804 static int
805 db_tuner___tuner_insert_kvpair(dbp, key_sz, data_sz, indx_pgsz, in_key, in_data, stats)
806 DB *dbp;
807 u_int32_t key_sz, data_sz;
808 int indx_pgsz, in_key, in_data;
809 TUNER_FF_STAT *stats;
810 {
811 DB_ENV *dbenv;
812 int is_big_data, is_big_key, ret;
813 u_int32_t needed, pgsize;
814
815 dbenv = dbp->dbenv;
816 is_big_data = is_big_key = 0;
817 needed = 0;
818 pgsize = (1 << indx_pgsz) * DB_MIN_PGSIZE;
819
820 if (key_sz > B_MINKEY_TO_OVFLSIZE(dbp, 2, pgsize))
821 is_big_key = 1;
822
823 if (data_sz > B_MINKEY_TO_OVFLSIZE(dbp, 2, pgsize))
824 is_big_data = 1;
825
826 if (is_big_key) {
827 needed += BOVERFLOW_PSIZE;
828
829 /* Add big key into ovfl pages. */
830 if ((ret =
831 db_tuner___tuner_record_ovfl_pg(key_sz, indx_pgsz, stats)) != 0) {
832 dbenv->err(dbenv, ret, "__tuner_record_ovfl_pg:key_sz");
833 return (ret);
834 }
835 } else {
836 /*
837 * key_sz = INSERT_SLOT indicates no key is inserted
838 * but a slot in the inp array, e.g., on-page duplicate.
839 * key_sz = INSERT_NOTHING indicates no key no slot is
840 * inserted.
841 */
842 if (in_key == INSERT_NOTHING)
843 needed += 0;
844 else if (in_key == INSERT_SLOT)
845 needed += sizeof(db_indx_t);
846 else if (in_key == INSERT_NORMAL)
847 needed += BKEYDATA_PSIZE(key_sz);
848 }
849
850 if (is_big_data) {
851 needed += BOVERFLOW_PSIZE;
852
853 /* Add big data into ovfl pages. */
854 if ((ret =
855 db_tuner___tuner_record_ovfl_pg(data_sz, indx_pgsz, stats)) != 0) {
856 dbenv->err(dbenv, ret, "__tuner_record_ovfl_pg");
857 return (ret);
858 }
859 } else {
860 /*
861 * in_data = INSERT_BDUPLICATE indicates a B_DUPLICATE is
862 * inserted, e.g., off-page duplicate case.
863 * in_data = INSERT_NOTHING indicates nothing is inserted,
864 * happens when there is a key + B_DUPLICATE pair in
865 * db_tuner___tuner_leaf_page, in which case, only the key is inserted
866 * but no data because the data will considered in
867 * __tuner_opd_page when an off-page
868 * duplicate set is coming.
869 */
870 if (in_data == INSERT_NOTHING)
871 needed += 0;
872 else if (in_data == INSERT_BDUPLICATE)
873 needed += BOVERFLOW_PSIZE;
874 else if (in_data == INSERT_NORMAL)
875 needed += BKEYDATA_PSIZE(data_sz);
876 }
877
878 if (needed == 0)
879 return (EXIT_INSERT_ZERO_NEED);
880
881 /* 1st leaf page, add overhead size. */
882 if (stats->pg_leaf_offset[indx_pgsz] == 0)
883 stats->pg_leaf_offset[indx_pgsz] = SIZEOF_PAGE;
884
885 if ((stats->pg_leaf_offset[indx_pgsz] + needed) > pgsize) {
886 /* No enough space, then record current page info. */
887 if ((ret = db_tuner___tuner_record_leaf_pg(indx_pgsz, stats)) != 0) {
888 dbenv->err(dbenv, ret, "__tuner_record_leaf_pg");
889 return (ret);
890 }
891
892 /* Insert pair into new page. */
893 stats->pg_leaf_offset[indx_pgsz] = needed + SIZEOF_PAGE;
894 } else
895 stats->pg_leaf_offset[indx_pgsz] += needed;
896
897 return (EXIT_SUCCESS);
898 }
899
900 /* Try to insert a duplicate data into simulated off duplicate pages. */
901 static int
902 db_tuner___tuner_insert_dupdata(dbp, data_sz, indx_pgsz, stats)
903 DB *dbp;
904 u_int32_t data_sz;
905 int indx_pgsz;
906 TUNER_FF_STAT *stats;
907 {
908 DB_ENV *dbenv;
909 int is_big_data, ret;
910 u_int32_t needed, pgsize;
911
912 dbenv = dbp->dbenv;
913 is_big_data = 0;
914 needed = 0;
915 pgsize = (1 << indx_pgsz) * DB_MIN_PGSIZE;
916
917 if (data_sz > B_MINKEY_TO_OVFLSIZE(dbp, 2, pgsize))
918 is_big_data = 1;
919
920 if (is_big_data) {
921 needed = BOVERFLOW_PSIZE;
922
923 /* Add big data into ovfl pages. */
924 if ((ret =
925 db_tuner___tuner_record_ovfl_pg(data_sz, indx_pgsz, stats)) != 0) {
926 dbenv->err(dbenv, ret, "__tuner_record_ovfl_pg");
927 return (ret);
928 }
929 } else
930 needed += BKEYDATA_PSIZE(data_sz);
931
932 if (needed == 0)
933 return (EXIT_INSERT_ZERO_NEED);
934
935 /* 1st opd page, add overhead size. */
936 if (stats->pg_dup_offset[indx_pgsz] == 0)
937 stats->pg_dup_offset[indx_pgsz] = SIZEOF_PAGE;
938
939 if ((stats->pg_dup_offset[indx_pgsz] + needed) > pgsize) {
940 /* no enough space then record current page info. */
941 if ((ret = db_tuner___tuner_record_dup_pg(indx_pgsz, stats)) != 0) {
942 dbenv->err(dbenv, ret, "__tuner_record_dup_pg");
943 return (ret);
944 }
945
946 /* insert new item into new page. */
947 stats->pg_dup_offset[indx_pgsz] = needed + SIZEOF_PAGE;
948 } else
949 stats->pg_dup_offset[indx_pgsz] += needed;
950
951 return (EXIT_SUCCESS);
952 }
953
954 /* Insert big item into simulated over flow pages. */
955 static int
956 db_tuner___tuner_record_ovfl_pg(size, indx_pgsz, stats)
957 u_int32_t size;
958 int indx_pgsz;
959 TUNER_FF_STAT *stats;
960 {
961 u_int32_t pgsize = (1 << indx_pgsz) * DB_MIN_PGSIZE;
962 int dist;
963
964 /* Update OVFLPAGE list: 1. Add to "full" ovfl pages.*/
965 stats->pgsize_ovfl_dist[indx_pgsz][DIST_DIVISION - 1] +=
966 (size / (pgsize - SIZEOF_PAGE));
967 /* Update OVFLPAGE list: 2. Add the remainder.*/
968 size = size % (pgsize - SIZEOF_PAGE);
969 dist = (int)(((double)(size + SIZEOF_PAGE) *
970 (DIST_DIVISION - 1)) / pgsize);
971
972 /* assert(dist < DIST_DIVISION); */
973 if (dist >= DIST_DIVISION)
974 return (EXIT_DIST_OUTRANGE);
975
976 ++stats->pgsize_ovfl_dist[indx_pgsz][dist];
977
978 return (EXIT_SUCCESS);
979 }
980
981 /* Record simulated leaf page if it has no space to contain new record. */
982 static int
983 db_tuner___tuner_record_leaf_pg(indx_pgsz, stats)
984 int indx_pgsz;
985 TUNER_FF_STAT *stats;
986 {
987 int dist;
988 u_int32_t pgsize;
989
990 pgsize = (1 << indx_pgsz) * DB_MIN_PGSIZE;
991
992 /* First calculate its fill factor. */
993 dist = (int)(((double)stats->pg_leaf_offset[indx_pgsz] *
994 (DIST_DIVISION - 1)) / pgsize);
995
996 /* assert(dist < DIST_DIVISION); */
997 if (dist >= DIST_DIVISION)
998 return (EXIT_DIST_OUTRANGE);
999
1000 /* Then add one page to its corresponding distribution. */
1001 ++stats->pgsize_leaf_dist[indx_pgsz][dist];
1002
1003 return (EXIT_SUCCESS);
1004 }
1005
1006 /* Record simulated duplicate page if it has no enough space for new record. */
1007 static int
1008 db_tuner___tuner_record_dup_pg(indx_pgsz, stats)
1009 int indx_pgsz;
1010 TUNER_FF_STAT *stats;
1011 {
1012 int dist;
1013 u_int32_t pgsize;
1014
1015 pgsize = (1 << indx_pgsz) * DB_MIN_PGSIZE;
1016
1017 /* First calculate its fill factor. */
1018 dist = (int)(((double)stats->pg_dup_offset[indx_pgsz] *
1019 (DIST_DIVISION - 1)) / pgsize);
1020
1021 /* assert(dist < DIST_DIVISION); */
1022 if (dist >= DIST_DIVISION)
1023 return (EXIT_DIST_OUTRANGE);
1024
1025 /* Then add one page to its corresponding distribution. */
1026 ++stats->pgsize_dup_dist[indx_pgsz][dist];
1027
1028 return (EXIT_SUCCESS);
1029 }
1030
1031 /*
1032 * Record the last simulated duplicate page when an off-page duplicate set
1033 * is finished, also reset its offset to be zero for next set.
1034 */
1035 static int
1036 db_tuner___tuner_record_last_opd(indx_pgsz, stats)
1037 int indx_pgsz;
1038 TUNER_FF_STAT *stats;
1039 {
1040 int ret;
1041 if (stats->pg_dup_offset[indx_pgsz] != 0 &&
1042 (ret = db_tuner___tuner_record_dup_pg(indx_pgsz, stats)) != 0)
1043 return (ret);
1044
1045 /* Reset offset to zero for new opd set. */
1046 stats->pg_dup_offset[indx_pgsz] = 0;
1047
1048 return (EXIT_SUCCESS);
1049 }
1050
1051 /*
1052 * When a new off-page duplicate set is coming, we first calculate its total
1053 * size, which will be used to determine whether this set should go to on-page
1054 * or off-page duplicate tree in our simulation part.
1055 *
1056 * As a off-page duplicate set is in a linked pages, we simply traverse this
1057 * link and sum up all the size of each data in each page.
1058 */
1059 static int
1060 db_tuner_get_opd_size(dbc, h, opd_sz)
1061 DBC *dbc;
1062 PAGE *h;
1063 u_int32_t *opd_sz;
1064 {
1065 DB *dbp;
1066 DB_ENV *dbenv;
1067 DB_MPOOLFILE *mpf;
1068 PAGE *p;
1069 db_pgno_t next_pgno;
1070 int ret;
1071 u_int32_t dup_sz;
1072
1073 dbp = dbc->dbp;
1074 dbenv = dbp->dbenv;
1075 mpf = dbp->mpf;
1076 dup_sz = 0;
1077 ret = 0;
1078 p = h;
1079
1080 while (1) {
1081 dup_sz += db_tuner_sum_opd_page_data_entries(dbp, p);
1082
1083 next_pgno = p->next_pgno;
1084 if (p != h && (ret =
1085 mpf->put(mpf, p, dbc->priority, 0)) != 0) {
1086 dbenv->err(dbenv, ret, "DB_MPOOLFILE->put:");
1087 return (ret);
1088 }
1089
1090 if (next_pgno == PGNO_INVALID)
1091 break;
1092
1093 if ((ret =
1094 mpf->get(mpf, &next_pgno, dbc->txn, 0, &p)) != 0) {
1095 dbenv->err(dbenv, ret, "DB_MPOOLFILE->get:");
1096 return (ret);
1097 }
1098 }
1099
1100 *opd_sz = dup_sz;
1101
1102 return (EXIT_SUCCESS);
1103 }
1104
1105 /* Sum up the space used to contain all the data in a specific page.*/
1106 static int
1107 db_tuner_sum_opd_page_data_entries(dbp, h)
1108 DB *dbp;
1109 PAGE *h;
1110 {
1111 db_indx_t i;
1112 u_int32_t sz;
1113 sz = 0;
1114
1115 for (i = 0; i < NUM_ENT(h); i += O_INDX) {
1116 /* Ignore deleted items. */
1117 if (B_DISSET(GET_BKEYDATA(dbp, h, i)->type))
1118 continue;
1119 sz += db_tuner_item_space(dbp, h, i);
1120 }
1121
1122 return sz;
1123 }
1124
1125 /* The space used by one item in a page. */
1126 static int
1127 db_tuner_item_space(dbp, h, indx)
1128 DB *dbp;
1129 PAGE *h;
1130 db_indx_t indx;
1131 {
1132 return (B_TYPE(GET_BKEYDATA(dbp, h, indx)->type) == B_KEYDATA ?
1133 BKEYDATA_PSIZE(GET_BKEYDATA(dbp, h, indx)->len) :
1134 BKEYDATA_PSIZE(GET_BOVERFLOW(dbp, h, indx)->tlen));
1135 }
1136
1137 /* The actual length of a item. */
1138 static int
1139 db_tuner_item_size(dbp, h, indx)
1140 DB *dbp;
1141 PAGE *h;
1142 db_indx_t indx;
1143 {
1144 return (B_TYPE(GET_BKEYDATA(dbp, h, indx)->type) == B_KEYDATA ?
1145 GET_BKEYDATA(dbp, h, indx)->len : GET_BOVERFLOW(dbp, h,
1146 indx)->tlen);
1147 }
1148
1149 /* Print out the information according to user's options. */
1150 static int
1151 db_tuner___tuner_print_btree_fillfactor(verbose, stats)
1152 u_int32_t verbose;
1153 TUNER_FF_STAT *stats;
1154 {
1155 const char * DIVIDE_LINE1 = "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=";
1156 const char * DIVIDE_LINE2 = "-----------|";
1157 const char * DIVIDE_LINE3 = "---------------------------------------";
1158 double shift_point;
1159 int best_indx, i, j;
1160 u_int32_t pgsize;
1161 u_int64_t minispace, ttpgcnt[NUM_PGSIZES], ttspace[NUM_PGSIZES];
1162 uintmax_t dup_cnt[NUM_PGSIZES], leaf_cnt[NUM_PGSIZES],
1163 ovfl_cnt[NUM_PGSIZES];
1164
1165 shift_point = 0.099;
1166 best_indx = 0;
1167 minispace = UINT64_MAX;
1168
1169 for (i = 0; i < NUM_PGSIZES; ++i) {
1170 pgsize = (1 << i) * DB_MIN_PGSIZE;
1171 ovfl_cnt[i] = leaf_cnt[i] = dup_cnt[i] = ttpgcnt[i] = 0;
1172
1173 for (j = 0; j < DIST_DIVISION; ++j) {
1174 ovfl_cnt[i] += stats->pgsize_ovfl_dist[i][j];
1175 leaf_cnt[i] += stats->pgsize_leaf_dist[i][j];
1176 dup_cnt[i] += stats->pgsize_dup_dist[i][j];
1177 }
1178
1179 ttpgcnt[i] = ovfl_cnt[i] + leaf_cnt[i] + dup_cnt[i];
1180 ttspace[i] = pgsize * ttpgcnt[i];
1181 }
1182
1183 if (verbose == 1) {
1184 printf("\n %50s \n",
1185 "===========Simulation Results===========");
1186 printf("\n %s\n %s\n %s\n",
1187 "leaf_pg:\t percentage of leaf page in that range",
1188 "dup_pg:\t percentage of duplicate page in that range",
1189 "ovfl_pg:\t percentage of over flow page in that range");
1190
1191 for (i = 0; i < NUM_PGSIZES; ++i) {
1192 printf("\n\n%s%s\n", DIVIDE_LINE1, DIVIDE_LINE1);
1193 printf("page size = %d\n", (1 << i) * DB_MIN_PGSIZE);
1194 printf("%s%s\n", DIVIDE_LINE1, DIVIDE_LINE1);
1195 printf("%s\n", DIVIDE_LINE3);
1196 printf("%s %s %s %s\n", "fill factor",
1197 "leaf_pg", "dup_pg", "ovfl_pg");
1198
1199 for (j = 0; j < DIST_DIVISION; ++j) {
1200 if (j == (DIST_DIVISION - 1))
1201 shift_point = 0.000;
1202 else
1203 shift_point = 0.099;
1204
1205 printf("\n[%2.1f-%4.3f]\t",
1206 (double)j/(DIST_DIVISION - 1),
1207 ((double)j/(DIST_DIVISION - 1) +
1208 shift_point));
1209
1210 if (leaf_cnt[i] == 0 ||
1211 stats->pgsize_leaf_dist[i][j] == 0)
1212 printf("%3.2f\t", (double)0);
1213 else
1214 printf("%3.2f%%\t", (double)
1215 (stats->pgsize_leaf_dist[i][j] *
1216 100) / leaf_cnt[i]);
1217
1218 if (dup_cnt[i] == 0 ||
1219 stats->pgsize_dup_dist[i][j] == 0)
1220 printf("%3.2f\t", (double)0);
1221 else
1222 printf("%3.2f%%\t", (double)
1223 (stats->pgsize_dup_dist[i][j] *
1224 100) / dup_cnt[i]);
1225
1226 if (ovfl_cnt[i] == 0 ||
1227 stats->pgsize_ovfl_dist[i][j] == 0)
1228 printf("%3.2f\t", (double)0);
1229 else
1230 printf("%3.2f%%\t", (double)
1231 (stats->pgsize_ovfl_dist[i][j] *
1232 100) / ovfl_cnt[i]);
1233 }
1234 }
1235
1236 printf("\n\n\n\n %55s\n\n",
1237 "=====Summary of simulated statistic=====");
1238 printf(" %s\n %s\n %s\n %s\n %s\n %s\n\n",
1239 "pgsize: \tpage size", "storage: \ton-disk space",
1240 "pgcnt: \ttotal number of all pages "
1241 "(e.g, sum of ovflcnt, leafcnt, dupcnt)",
1242 "ovflcnt: \tnumber of over flow pages",
1243 "leafcnt: \tnumber of leaf pages",
1244 "dupcnt: \tnumber of duplicate pages");
1245 printf("%s%s%s%s%s%s\n", DIVIDE_LINE2, DIVIDE_LINE2,
1246 DIVIDE_LINE2, DIVIDE_LINE2, DIVIDE_LINE2, DIVIDE_LINE2);
1247 printf(" %10s| %10s| %10s| %10s| %10s| %10s|\n", "pgsize",
1248 "storage", "pgcnt", "ovflcnt", "leafcnt", "dupcnt");
1249 printf("%s%s%s%s%s%s\n", DIVIDE_LINE2, DIVIDE_LINE2,
1250 DIVIDE_LINE2, DIVIDE_LINE2, DIVIDE_LINE2, DIVIDE_LINE2);
1251 for (i = 0; i < NUM_PGSIZES; ++i) {
1252 printf(" %10d|", (1 << i) * DB_MIN_PGSIZE);
1253 printf(" %10u|", (u_int32_t)ttspace[i]);
1254 if (ttspace[i] != (u_int32_t)ttspace[i])
1255 printf("(truncated value reported)");
1256 printf(" %10u|", (u_int32_t)ttpgcnt[i]);
1257 if (ttpgcnt[i] != (u_int32_t)ttpgcnt[i])
1258 printf("(truncated value reported)");
1259 printf(" %10u|", (u_int32_t)ovfl_cnt[i]);
1260 if (ovfl_cnt[i] != (u_int32_t)ovfl_cnt[i])
1261 printf("(truncated value reported)");
1262 printf(" %10u|", (u_int32_t)leaf_cnt[i]);
1263 if (leaf_cnt[i] != (u_int32_t)leaf_cnt[i])
1264 printf("(truncated value reported)");
1265 printf(" %10u|", (u_int32_t)dup_cnt[i]);
1266 if (dup_cnt[i] != (u_int32_t)dup_cnt[i])
1267 printf("(truncated value reported)");
1268 printf("\n");
1269 }
1270 printf("%s%s%s%s%s%s\n", DIVIDE_LINE2, DIVIDE_LINE2,
1271 DIVIDE_LINE2, DIVIDE_LINE2, DIVIDE_LINE2, DIVIDE_LINE2);
1272 }
1273
1274 /*
1275 * Choose a page size based on the overflow calculation. If there
1276 * is no overflow consideration, then use the smallest on-disk
1277 * space as a recommended page size.
1278 */
1279 if (ovfl_cnt[0] == 0) {
1280 minispace = ttspace[0];
1281 for (i = 1; i < NUM_PGSIZES; ++i)
1282 if ((ttspace[i] != 0) && (minispace > ttspace[i])) {
1283 minispace = ttspace[i];
1284 best_indx = i;
1285 }
1286 } else
1287 for (i = 1; i < NUM_PGSIZES; ++i)
1288 if ((ovfl_cnt[i - 1] - ovfl_cnt[i]) > 0.02 * ttpgcnt[i])
1289 best_indx = i;
1290
1291 printf("\n\nFor your input database, we recommend page size = %d \n \t"
1292 "out of 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536 for you.\n",
1293 (1 << best_indx) * DB_MIN_PGSIZE);
1294
1295 return (EXIT_SUCCESS);
1296 }
1297
1298 /* Open the specific existing database. */
1299 static int
1300 db_tuner_open_db(dbpp, dbenv, dbname, subdb)
1301 DB **dbpp;
1302 DB_ENV *dbenv;
1303 char *dbname;
1304 char *subdb;
1305 {
1306 DB *dbp;
1307 int ret = 0;
1308
1309 if ((ret = db_create(&dbp, dbenv, 0)) != 0) {
1310 dbenv->err(dbenv, ret, "db_create fails.\n");
1311 return (ret);
1312 }
1313
1314 *dbpp = dbp;
1315
1316 /* Open a database for read-only.*/
1317 if ((ret =
1318 dbp->open(dbp, NULL, dbname, subdb, DB_UNKNOWN, DB_RDONLY, 0)) != 0)
1319 dbenv->err(dbenv, ret, "DB->open");
1320
1321 return (ret);
1322 }
1323
1324 /* Usage flag information to indicate what can user query for given database.*/
1325 static int
1326 db_tuner_usage()
1327 {
1328 fprintf(stderr, "usage: %s %s\n", progname,
1329 "[-c cachesize] -d file [-h home] [-s database] [-v verbose]");
1330 exit(EXIT_FAILURE);
1331 }
1332
1333 /*Check the verion of Berkeley DB libaray, make sure it is the right version.*/
1334 static int
1335 db_tuner_version_check()
1336 {
1337 int v_major, v_minor, v_patch;
1338
1339 /* Make sure we're loaded with the right version of the DB library. */
1340 (void)db_version(&v_major, &v_minor, &v_patch);
1341 if (v_major != DB_VERSION_MAJOR || v_minor != DB_VERSION_MINOR) {
1342 fprintf(stderr, DB_STR_A("5002",
1343 "%s: version %d.%d doesn't match library version %d.%d\n",
1344 "%s %d %d %d %d"), progname, DB_VERSION_MAJOR,
1345 DB_VERSION_MINOR, v_major, v_minor);
1346
1347 return (EXIT_FAILURE);
1348 }
1349
1350 return (EXIT_SUCCESS);
1351 }