00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <sys/types.h>
00011
00012 #include <errno.h>
00013 #include <stdlib.h>
00014 #include <string.h>
00015 #include <time.h>
00016
00017 #ifdef _WIN32
00018 extern int getopt(int, char * const *, const char *);
00019 #else
00020 #include <unistd.h>
00021 #endif
00022
00023 #include <db.h>
00024
00025 int init __P((const char *, int, int, const char *));
00026 int run __P((int, int, int, int, const char *));
00027 int run_mpool __P((int, int, int, int, const char *));
00028 int main __P((int, char *[]));
00029 int usage __P((const char *));
00030 #define MPOOL "mpool"
00031
00032 int
00033 main(argc, argv)
00034 int argc;
00035 char *argv[];
00036 {
00037 extern char *optarg;
00038 extern int optind;
00039 int cachesize, ch, hits, npages, pagesize;
00040 char *progname;
00041
00042 cachesize = 20 * 1024;
00043 hits = 1000;
00044 npages = 50;
00045 pagesize = 1024;
00046 progname = argv[0];
00047 while ((ch = getopt(argc, argv, "c:h:n:p:")) != EOF)
00048 switch (ch) {
00049 case 'c':
00050 if ((cachesize = atoi(optarg)) < 20 * 1024)
00051 return (usage(progname));
00052 break;
00053 case 'h':
00054 if ((hits = atoi(optarg)) <= 0)
00055 return (usage(progname));
00056 break;
00057 case 'n':
00058 if ((npages = atoi(optarg)) <= 0)
00059 return (usage(progname));
00060 break;
00061 case 'p':
00062 if ((pagesize = atoi(optarg)) <= 0)
00063 return (usage(progname));
00064 break;
00065 case '?':
00066 default:
00067 return (usage(progname));
00068 }
00069 argc -= optind;
00070 argv += optind;
00071
00072 return (run_mpool(pagesize, cachesize,
00073 hits, npages, progname) == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
00074 }
00075
00076 int
00077 usage(progname)
00078 const char *progname;
00079 {
00080 (void)fprintf(stderr,
00081 "usage: %s [-c cachesize] [-h hits] [-n npages] [-p pagesize]\n",
00082 progname);
00083 return (EXIT_FAILURE);
00084 }
00085
00086 int
00087 run_mpool(pagesize, cachesize, hits, npages, progname)
00088 int pagesize, cachesize, hits, npages;
00089 const char *progname;
00090 {
00091 int ret;
00092
00093
00094 if ((ret = init(MPOOL, pagesize, npages, progname)) != 0)
00095 return (ret);
00096
00097
00098 if ((ret = run(hits, cachesize, pagesize, npages, progname)) != 0)
00099 return (ret);
00100
00101 return (0);
00102 }
00103
00104
00105
00106
00107
00108 int
00109 init(file, pagesize, npages, progname)
00110 const char *file, *progname;
00111 int pagesize, npages;
00112 {
00113 FILE *fp;
00114 int cnt;
00115 char *p;
00116
00117
00118
00119
00120
00121 (void)remove(file);
00122 if ((fp = fopen(file, "wb")) == NULL) {
00123 fprintf(stderr,
00124 "%s: %s: %s\n", progname, file, strerror(errno));
00125 return (1);
00126 }
00127 if ((p = (char *)malloc(pagesize)) == NULL) {
00128 fprintf(stderr, "%s: %s\n", progname, strerror(ENOMEM));
00129 return (1);
00130 }
00131
00132
00133
00134
00135
00136
00137
00138 for (cnt = 0; cnt < npages; ++cnt) {
00139 *(db_pgno_t *)p = cnt;
00140 if (fwrite(p, pagesize, 1, fp) != 1) {
00141 fprintf(stderr,
00142 "%s: %s: %s\n", progname, file, strerror(errno));
00143 return (1);
00144 }
00145 }
00146
00147 (void)fclose(fp);
00148 free(p);
00149 return (0);
00150 }
00151
00152
00153
00154
00155
00156 int
00157 run(hits, cachesize, pagesize, npages, progname)
00158 int hits, cachesize, pagesize, npages;
00159 const char *progname;
00160 {
00161 DB_ENV *dbenv;
00162 DB_MPOOLFILE *mfp;
00163 db_pgno_t pageno;
00164 int cnt, ret;
00165 void *p;
00166
00167 dbenv = NULL;
00168 mfp = NULL;
00169
00170 printf("%s: cachesize: %d; pagesize: %d; N pages: %d\n",
00171 progname, cachesize, pagesize, npages);
00172
00173
00174
00175
00176
00177 if ((ret = db_env_create(&dbenv, 0)) != 0) {
00178 fprintf(stderr,
00179 "%s: db_env_create: %s\n", progname, db_strerror(ret));
00180 return (1);
00181 }
00182 dbenv->set_errfile(dbenv, stderr);
00183 dbenv->set_errpfx(dbenv, progname);
00184 #ifdef HAVE_VXWORKS
00185 if ((ret = dbenv->set_shm_key(dbenv, VXSHM_KEY)) != 0) {
00186 dbenv->err(dbenv, ret, "set_shm_key");
00187 return (1);
00188 }
00189 #endif
00190
00191
00192 if ((ret = dbenv->set_cachesize(dbenv, 0, cachesize, 0)) != 0) {
00193 dbenv->err(dbenv, ret, "set_cachesize");
00194 goto err;
00195 }
00196
00197
00198 if ((ret = dbenv->open(
00199 dbenv, NULL, DB_CREATE | DB_INIT_MPOOL, 0)) != 0) {
00200 dbenv->err(dbenv, ret, "DB_ENV->open");
00201 goto err;
00202 }
00203
00204
00205 if ((ret = dbenv->memp_fcreate(dbenv, &mfp, 0)) != 0) {
00206 dbenv->err(dbenv, ret, "DB_ENV->memp_fcreate: %s", MPOOL);
00207 goto err;
00208 }
00209 if ((ret = mfp->open(mfp, MPOOL, 0, 0, pagesize)) != 0) {
00210 dbenv->err(dbenv, ret, "DB_MPOOLFILE->open: %s", MPOOL);
00211 goto err;
00212 }
00213
00214 printf("retrieve %d random pages... ", hits);
00215
00216 srand((u_int)time(NULL));
00217 for (cnt = 0; cnt < hits; ++cnt) {
00218 pageno = rand() % npages;
00219 if ((ret = mfp->get(mfp, &pageno, 0, &p)) != 0) {
00220 dbenv->err(dbenv, ret,
00221 "unable to retrieve page %lu", (u_long)pageno);
00222 goto err;
00223 }
00224
00225 if (*(db_pgno_t *)p != pageno) {
00226 dbenv->errx(dbenv,
00227 "wrong page retrieved (%lu != %d)",
00228 (u_long)pageno, *(int *)p);
00229 goto err;
00230 }
00231 if ((ret = mfp->put(mfp, p, 0)) != 0) {
00232 dbenv->err(dbenv, ret,
00233 "unable to return page %lu", (u_long)pageno);
00234 goto err;
00235 }
00236 }
00237
00238 printf("successful.\n");
00239
00240
00241 if ((ret = mfp->close(mfp, 0)) != 0) {
00242 dbenv->err(dbenv, ret, "DB_MPOOLFILE->close");
00243 goto err;
00244 }
00245
00246
00247 if ((ret = dbenv->close(dbenv, 0)) != 0) {
00248 fprintf(stderr,
00249 "%s: db_env_create: %s\n", progname, db_strerror(ret));
00250 return (1);
00251 }
00252 return (0);
00253
00254 err: if (mfp != NULL)
00255 (void)mfp->close(mfp, 0);
00256 if (dbenv != NULL)
00257 (void)dbenv->close(dbenv, 0);
00258 return (1);
00259 }