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
|
/*
NAME
gtcacheopt - test GDBM_GETCACHESIZE and GDBM_SETCACHESIZE options.
SYNOPSIS
gtcacheopt [-v]
DESCRIPTION
Reducing the cache size should retain most recently used elements
and ensure correct rehashing.
Operation:
1) Create new database.
2) Generate at least 10 full buckets,
3) Check GDBM_GETCACHESIZE.
4) Get cache statistics for the first 8 most recently
used cache elements.
5) Set cache size to 8.
6) Retrieve each of the bucket pointed to by stats obtained in 4.
7) Verify that (6) retrieved buckets from cache.
OPTIONS
-v Verbosely print what's being done.
EXIT CODE
0 success
1 failure
2 usage error
LICENSE
This file is part of GDBM test suite.
Copyright (C) 2021-2024 Free Software Foundation, Inc.
GDBM is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GDBM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GDBM. If not, see <http://www.gnu.org/licenses/>.
*/
#include "autoconf.h"
#include "gdbmdefs.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <unistd.h>
char dbname[] = "a.db";
int verbose = 0;
#define NBUCKETS 10
#define CACHE_SIZE 8
#define DATASIZE (4*IGNORE_SIZE)
static void
test_getcachesize (GDBM_FILE dbf, size_t expected_size,
struct gdbm_cache_stat *pstat, size_t *pnstat)
{
size_t size;
int cache_auto;
if (gdbm_setopt (dbf, GDBM_GETCACHESIZE, &size, sizeof (size)))
{
fprintf (stderr, "GDBM_GETCACHESIZE: %s\n", gdbm_strerror (gdbm_errno));
exit (1);
}
if (verbose)
printf ("size = %zu\n", size);
if (expected_size && expected_size != size)
{
fprintf (stderr, "expected_size != size (%zu != %zu)\n",
expected_size, size);
exit (1);
}
if (gdbm_setopt (dbf, GDBM_GETCACHEAUTO, &cache_auto, sizeof (cache_auto)))
{
fprintf (stderr, "GDBM_GETCACHESIZE: %s\n", gdbm_strerror (gdbm_errno));
exit (1);
}
if (verbose)
printf ("cache_auto = %d\n", cache_auto);
if (expected_size && cache_auto != 0)
{
fprintf (stderr, "cache_auto != 0\n");
exit (1);
}
if (pstat)
{
gdbm_get_cache_stats (dbf, NULL, NULL, pnstat, pstat, CACHE_SIZE);
}
}
static int
dir_index (GDBM_FILE dbf, off_t adr)
{
int i;
for (i = 0; i < dbf->header->dir_size; i++)
if (dbf->dir[i] == adr)
return i;
fprintf (stderr, "%lu: can't find bucket in directory\n", adr);
exit (1);
}
int
main (int argc, char **argv)
{
GDBM_FILE dbf;
datum key, content;
int nkeys;
char data[DATASIZE];
int i;
struct gdbm_cache_stat stat[2][CACHE_SIZE];
size_t nstat[2];
while ((i = getopt (argc, argv, "v")) != EOF)
{
switch (i)
{
case 'v':
verbose++;
break;
default:
return 2;
}
}
/*
* 1) Create new database.
*/
if (verbose)
printf ("creating database\n");
dbf = gdbm_open (dbname, GDBM_MIN_BLOCK_SIZE, GDBM_NEWDB, 0644, NULL);
if (!dbf)
{
fprintf (stderr, "gdbm_open: %s\n", gdbm_strerror (gdbm_errno));
return 1;
}
/*
* 2) Generate 10 full buckets or key/value pairs are created.
*/
/* Initialize keys. */
nkeys = NBUCKETS * dbf->header->bucket_elems;
/* Initialize content */
for (i = 0; i < DATASIZE; i++)
data[i] = i+1;
content.dsize = DATASIZE;
content.dptr = data;
/* Populate the database. */
if (verbose)
printf ("populating database (%d keys)\n", nkeys);
key.dsize = sizeof (i);
key.dptr = (char*) &i;
for (i = 0; i < nkeys; i++)
{
if (gdbm_store (dbf, key, content, 0) != 0)
{
fprintf (stderr, "%d: item not inserted: %s\n",
i, gdbm_db_strerror (dbf));
gdbm_close (dbf);
return 1;
}
}
/*
* 3) Check if the value retrieved by GDBM_GETCACHESIZE matches the
* expected one and
* 4) save cache statistics for the first CACHE_SIZE most recently used
* cache elements.
*/
test_getcachesize (dbf, 0, stat[0], &nstat[0]);
if (verbose)
printf ("setting new cache size\n");
/*
* 5) Set new cache size.
*/
i = CACHE_SIZE;
if (gdbm_setopt (dbf, GDBM_SETCACHESIZE, &i, sizeof (i)))
{
fprintf (stderr, "GDBM_SETCACHESIZE: %s\n", gdbm_strerror (gdbm_errno));
return 1;
}
if (verbose)
printf ("verifying cache (pass 1)\n");
/*
* 6) Retrieve each of the bucket pointed to by stats obtained in 4.
*
* To retrieve a bucket, the corresponding directory index must be known.
* That index is obtained using linear search in the database file directory.
*
* Buckets must be retrieved in reverse order, so that the LRU cache
* remains in the same order after the operation (each retrieval cyclically
* shifts elements in the queue).
*/
test_getcachesize (dbf, CACHE_SIZE, stat[1], &nstat[1]);
for (i = CACHE_SIZE - 1; i >= 0; i--)
{
if (stat[0][i].adr != stat[1][i].adr)
{
fprintf (stderr, "%d: address mismatch\n", i);
return 1;
}
if (_gdbm_get_bucket (dbf, dir_index (dbf, stat[0][i].adr)))
{
fprintf (stderr, "%d: _gdbm_get_bucket: %s\n", i,
gdbm_db_strerror (dbf));
return 1;
}
}
if (verbose)
printf ("getting cache statistics\n");
test_getcachesize (dbf, CACHE_SIZE, stat[0], &nstat[0]);
gdbm_close (dbf);
/*
* 7) Verify that the buckets were retrieved from cache.
*
* To do so, compare addresses and hit counts in statistic buffers
* stat[0] and stat[1]. Each pair of elements must have the same
* bucket address. Hit counts must differ by 1.
*/
if (verbose)
printf ("verifying cache (pass 2)\n");
for (i = 0; i < CACHE_SIZE; i++)
{
if (stat[0][i].adr != stat[1][i].adr)
{
fprintf (stderr, "%d: address mismatch\n", i);
return 1;
}
if (stat[0][i].hits != stat[1][i].hits + 1)
{
fprintf (stderr, "%d: hit count mismatch: %zu != %zu\n", i,
stat[0][i].hits, stat[1][i].hits);
return 1;
}
}
return 0;
}
|