1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3 #ident "$Id$"
4 /*======
5 This file is part of PerconaFT.
6 
7 
8 Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9 
10     PerconaFT is free software: you can redistribute it and/or modify
11     it under the terms of the GNU General Public License, version 2,
12     as published by the Free Software Foundation.
13 
14     PerconaFT is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18 
19     You should have received a copy of the GNU General Public License
20     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
21 
22 ----------------------------------------
23 
24     PerconaFT is free software: you can redistribute it and/or modify
25     it under the terms of the GNU Affero General Public License, version 3,
26     as published by the Free Software Foundation.
27 
28     PerconaFT is distributed in the hope that it will be useful,
29     but WITHOUT ANY WARRANTY; without even the implied warranty of
30     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31     GNU Affero General Public License for more details.
32 
33     You should have received a copy of the GNU Affero General Public License
34     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
35 ======= */
36 
37 #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
38 
39 #include "test.h"
40 
41 #include <stdio.h>
42 #include <stdlib.h>
43 
44 #include <unistd.h>
45 #include <memory.h>
46 #include <inttypes.h>
47 #include <sys/stat.h>
48 #include <db.h>
49 
50 
51 static uint64_t
size_from(uint32_t gbytes,uint32_t bytes)52 size_from (uint32_t gbytes, uint32_t bytes) {
53     return ((uint64_t)gbytes << 30) + bytes;
54 }
55 
56 static inline void
size_to(uint64_t s,uint32_t * gbytes,uint32_t * bytes)57 size_to (uint64_t s, uint32_t *gbytes, uint32_t *bytes) {
58     *gbytes = s >> 30;
59     *bytes = s & ((1<<30) - 1);
60 }
61 
62 static inline void
expect_le(uint64_t a,uint32_t gbytes,uint32_t bytes)63 expect_le (uint64_t a, uint32_t gbytes, uint32_t bytes) {
64     uint64_t b = size_from(gbytes, bytes);
65     if (a != b && verbose)
66         printf("WARNING: expect %" PRIu64 " got %" PRIu64 "\n", a, b);
67     assert(a <= b);
68 }
69 
70 
71 static void
test_cachesize(void)72 test_cachesize (void) {
73 #if DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 3
74     int r;
75     DB_ENV *env;
76     uint32_t gbytes, bytes; int ncache;
77 
78     r = db_env_create(&env, 0); assert(r == 0);
79     r = env->get_cachesize(env, &gbytes, &bytes, &ncache); assert(r == 0);
80     if (verbose) printf("default %u %u %d\n", gbytes, bytes, ncache);
81 
82     r = env->set_cachesize(env, 0, 0, 1); assert(r == 0);
83     r = env->get_cachesize(env, &gbytes, &bytes, &ncache); assert(r == 0);
84     if (verbose) printf("minimum %u %u %d\n", gbytes, bytes, ncache);
85     uint64_t minsize = size_from(gbytes, bytes);
86 
87     uint64_t s = 1; size_to(s, &gbytes, &bytes);
88     while (gbytes <= 32) {
89         r = env->set_cachesize(env, gbytes, bytes, ncache);
90         if (r != 0) {
91             if (verbose) printf("max %u %u\n", gbytes, bytes);
92             break;
93         }
94         assert(r == 0);
95         r = env->get_cachesize(env, &gbytes, &bytes, &ncache); assert(r == 0);
96         assert(ncache == 1);
97         if (s <= minsize)
98             expect_le(minsize, gbytes, bytes);
99         else
100             expect_le(s, gbytes, bytes);
101         s *= 2; size_to(s, &gbytes, &bytes);
102     }
103     r = env->close(env, 0); assert(r == 0);
104 #endif
105 }
106 
107 
108 int
test_main(int argc,char * const argv[])109 test_main(int argc, char *const argv[]) {
110     parse_args(argc, argv);
111     test_cachesize();
112 
113     return 0;
114 }
115