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
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 1997-2002
* Sleepycat Software. All rights reserved.
*
* $Id: ex_mpool.c,v 1.1.1.1 2003/11/20 22:13:28 toshok Exp $
*/
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifdef _WIN32
extern int getopt(int, char * const *, const char *);
#else
#include <unistd.h>
#endif
#include <db.h>
int init __P((const char *, int, int, const char *));
int run __P((int, int, int, int, const char *));
int run_mpool __P((int, int, int, int, const char *));
int main __P((int, char *[]));
int usage __P((const char *));
#define MPOOL "mpool" /* File. */
int
main(argc, argv)
int argc;
char *argv[];
{
extern char *optarg;
extern int optind;
int cachesize, ch, hits, npages, pagesize;
char *progname;
cachesize = 20 * 1024;
hits = 1000;
npages = 50;
pagesize = 1024;
progname = argv[0];
while ((ch = getopt(argc, argv, "c:h:n:p:")) != EOF)
switch (ch) {
case 'c':
if ((cachesize = atoi(optarg)) < 20 * 1024)
return (usage(progname));
break;
case 'h':
if ((hits = atoi(optarg)) <= 0)
return (usage(progname));
break;
case 'n':
if ((npages = atoi(optarg)) <= 0)
return (usage(progname));
break;
case 'p':
if ((pagesize = atoi(optarg)) <= 0)
return (usage(progname));
break;
case '?':
default:
return (usage(progname));
}
argc -= optind;
argv += optind;
return (run_mpool(pagesize, cachesize,
hits, npages, progname) == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}
int
usage(progname)
const char *progname;
{
(void)fprintf(stderr,
"usage: %s [-c cachesize] [-h hits] [-n npages] [-p pagesize]\n",
progname);
return (EXIT_FAILURE);
}
int
run_mpool(pagesize, cachesize, hits, npages, progname)
int pagesize, cachesize, hits, npages;
const char *progname;
{
int ret;
/* Initialize the file. */
if ((ret = init(MPOOL, pagesize, npages, progname)) != 0)
return (ret);
/* Get the pages. */
if ((ret = run(hits, cachesize, pagesize, npages, progname)) != 0)
return (ret);
return (0);
}
/*
* init --
* Create a backing file.
*/
int
init(file, pagesize, npages, progname)
const char *file, *progname;
int pagesize, npages;
{
FILE *fp;
int cnt;
char *p;
/*
* Create a file with the right number of pages, and store a page
* number on each page.
*/
if ((fp = fopen(file, "wb")) == NULL) {
fprintf(stderr,
"%s: %s: %s\n", progname, file, strerror(errno));
return (1);
}
if ((p = (char *)malloc(pagesize)) == NULL) {
fprintf(stderr, "%s: %s\n", progname, strerror(ENOMEM));
return (1);
}
/* The pages are numbered from 0. */
for (cnt = 0; cnt <= npages; ++cnt) {
*(int *)p = cnt;
if (fwrite(p, pagesize, 1, fp) != 1) {
fprintf(stderr,
"%s: %s: %s\n", progname, file, strerror(errno));
return (1);
}
}
(void)fclose(fp);
free(p);
return (0);
}
/*
* run --
* Get a set of pages.
*/
int
run(hits, cachesize, pagesize, npages, progname)
int hits, cachesize, pagesize, npages;
const char *progname;
{
DB_ENV *dbenv;
DB_MPOOLFILE *mfp;
db_pgno_t pageno;
int cnt, ret;
void *p;
dbenv = NULL;
mfp = NULL;
printf("%s: cachesize: %d; pagesize: %d; N pages: %d\n",
progname, cachesize, pagesize, npages);
/*
* Open a memory pool, specify a cachesize, output error messages
* to stderr.
*/
if ((ret = db_env_create(&dbenv, 0)) != 0) {
fprintf(stderr,
"%s: db_env_create: %s\n", progname, db_strerror(ret));
return (1);
}
dbenv->set_errfile(dbenv, stderr);
dbenv->set_errpfx(dbenv, progname);
#ifdef HAVE_VXWORKS
if ((ret = dbenv->set_shm_key(dbenv, VXSHM_KEY)) != 0) {
dbenv->err(dbenv, ret, "set_shm_key");
return (1);
}
#endif
/* Set the cachesize. */
if ((ret = dbenv->set_cachesize(dbenv, 0, cachesize, 0)) != 0) {
dbenv->err(dbenv, ret, "set_cachesize");
goto err;
}
/* Open the environment. */
if ((ret = dbenv->open(
dbenv, NULL, DB_CREATE | DB_INIT_MPOOL, 0)) != 0) {
dbenv->err(dbenv, ret, "DB_ENV->open");
goto err;
}
/* Open the file in the environment. */
if ((ret = dbenv->memp_fcreate(dbenv, &mfp, 0)) != 0) {
dbenv->err(dbenv, ret, "DB_ENV->memp_fcreate: %s", MPOOL);
goto err;
}
if ((ret = mfp->open(mfp, MPOOL, 0, 0, pagesize)) != 0) {
dbenv->err(dbenv, ret, "DB_MPOOLFILE->open: %s", MPOOL);
goto err;
}
printf("retrieve %d random pages... ", hits);
srand((u_int)time(NULL));
for (cnt = 0; cnt < hits; ++cnt) {
pageno = (rand() % npages) + 1;
if ((ret = mfp->get(mfp, &pageno, 0, &p)) != 0) {
dbenv->err(dbenv, ret,
"unable to retrieve page %lu", (u_long)pageno);
goto err;
}
if (*(db_pgno_t *)p != pageno) {
dbenv->errx(dbenv,
"wrong page retrieved (%lu != %d)",
(u_long)pageno, *(int *)p);
goto err;
}
if ((ret = mfp->put(mfp, p, 0)) != 0) {
dbenv->err(dbenv, ret,
"unable to return page %lu", (u_long)pageno);
goto err;
}
}
printf("successful.\n");
/* Close the file. */
if ((ret = mfp->close(mfp, 0)) != 0) {
dbenv->err(dbenv, ret, "DB_MPOOLFILE->close");
goto err;
}
/* Close the pool. */
if ((ret = dbenv->close(dbenv, 0)) != 0) {
fprintf(stderr,
"%s: db_env_create: %s\n", progname, db_strerror(ret));
return (1);
}
return (0);
err: if (mfp != NULL)
(void)mfp->close(mfp, 0);
if (dbenv != NULL)
(void)dbenv->close(dbenv, 0);
return (1);
}
|