1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2001 Silicon Graphics, Inc.
9 #ifdef HAVE_GDBM_NDBM_H_
10 #include <gdbm/ndbm.h>
11 #elif HAVE_GDBM_NDBM_H
12 #include <gdbm-ndbm.h>
20 /* #define WorkDir "/xfs" */
21 #define DBNAME "DBtest"
25 typedef struct _myDB {
27 unsigned short checksum;
31 int InitDbmLookup(int);
32 int DoDbmLookup(void);
33 void CleanupDbmLookup(void);
34 void DoSysError(char *, int);
35 int creatDbRec(myDB *);
36 unsigned short calccksum(char *, int);
37 void CkSumError(char *, unsigned short, unsigned short);
38 int InsertKey(unsigned short *, int, unsigned short);
39 void DumpKeys(int, unsigned short);
42 int numDbmEntries, keyIdx, Dups = 0, Errors = 0;
43 unsigned short *KeyArray, *DupsArray;
44 int debugflg = 0; /* are we in debugging mode? */
45 int errflg = 0; /* usage option errors */
46 int ignoreflg = 1; /* ignore errors in lookup; default = on */
51 main(int argc, char *argv[])
53 int numrecs = DBRECS, c, l;
55 while ((c = getopt(argc, argv, "idn:l:s:")) != EOF) {
62 numrecs = atoi(optarg);
63 numrecs = (numrecs < 1) ? DBRECS : numrecs;
67 loops = (loops < 1) ? LOOPS : loops;
70 randseed = atoi(optarg);
71 randseed = (randseed < 0) ? 0 : randseed;
79 printf("Usage: dbtest [-di] [-n number] [-l loop] [-s randseed]\n");
83 printf("Extra arguments ignored\n");
84 for ( ; optind<argc; optind++)
85 printf("\t%s\n", argv[optind]);
88 printf("dbtest v1.0\n\n");
89 printf("Creating database containing %d records...\n", numrecs);
90 printf("\tperforming lookups for %d iterations...\n", loops);
92 printf("\tusing %d as seed for srandom()...\n\n", randseed);
94 InitDbmLookup(numrecs);
95 printf("\t\nThere were %d duplicate checksums generated\n", Dups);
96 for (l=0; l<loops; l++) {
97 printf("\nPerforming lookups on database...\n");
98 if (DoDbmLookup() != numrecs)
99 printf("\nError performing lookups!\n");
101 printf("\nLookups succeeded...\n");
103 printf("\nCleaning up database...\n");
104 printf("\t\nThere were %d duplicate checksums generated\n", Dups);
108 for (l=0; l<Dups; l++) {
111 printf("0x%x\t", DupsArray[l]);
118 int InitDbmLookup(int howmany)
125 sprintf(filename, "%s-%d", DBNAME, (int)getpid());
127 printf("dbm_open(%s, O_RDWR|O_CREAT, 0644)\n", filename);
130 dbm = dbm_open(filename, O_RDWR|O_CREAT, 0644);
132 DoSysError("\ndbm_open", -1);
134 if ((KeyArray = (unsigned short *)calloc( howmany,
135 sizeof(unsigned short))) == NULL)
136 DoSysError("\ncalloc:KeyArray", -1);
137 if ((DupsArray = (unsigned short *)calloc( 100,
138 sizeof(unsigned short))) == NULL)
139 DoSysError("\ncalloc:DupsArray", -1);
142 for(i=0; i<howmany; i++) {
144 if ( creatDbRec(&dbRec) )
145 DoSysError("\ncreatDbRec", -1);
147 printf("created rec #%-7d\t%x\r", i+1, dbRec.checksum);
151 if (InsertKey(KeyArray, keyIdx, dbRec.checksum))
154 key.dptr = (char *)&(dbRec.checksum);
155 key.dsize = sizeof(dbRec.checksum);
156 content.dptr = (char *)&dbRec;
157 content.dsize = sizeof(dbRec);
162 printf("dbm_store(dbm, key, content, DBM_INSERT)\n");
164 rc = dbm_store(dbm, key, content, DBM_INSERT);
166 DoSysError("\ndbm_store", rc);
168 keyIdx--; /* key is already in database */
169 DupsArray[Dups++] = dbRec.checksum;
174 printf("dbm_close(dbm)\n");
177 dbm_close(dbm); /* close to eliminate chance of in-memory caching */
179 printf("dbm_open(%s, O_RDONLY, 0)\n", filename);
182 dbm = dbm_open(filename, O_RDONLY, 0);
184 DoSysError("\ndbm_open", -1);
188 int DoDbmLookup(void)
195 printf("\n\tSequential lookups...\n");
197 for(i=0, j=0; i<numDbmEntries; i++) {
198 key.dptr = (char *)(KeyArray+j);
199 key.dsize = sizeof(KeyArray[0]);
203 printf("dbm_fetch(dbm, key = %d)\n", j);
206 content = dbm_fetch(dbm, key);
207 dbp = (myDB *)content.dptr;
208 if ( !content.dptr ) {
210 if (n) printf("\n\ndbm_error: %d\n", n);
211 printf("\n%d: Sequential Lookup of key %4x failed!\n",
213 DumpKeys(i, KeyArray[j]);
222 if (debugflg && dbp) {
223 printf("Seq rec #%-6d: checksum %4x (%4x)\r", i,
224 dbp->checksum, KeyArray[j]);
228 if (content.dsize == 0) {
229 printf("\nrec #%-8d: key = %4x (%d)\n", i, KeyArray[j], j);
231 DoSysError("\ndbm_fetch", content.dsize);
233 if (dbp->checksum != KeyArray[j])
234 CkSumError("KeySequential", dbp->checksum, KeyArray[j]);
235 if ((tmpck = calccksum(dbp->data, sizeof(dbp->data))) != KeyArray[j])
236 CkSumError("DataSequential", tmpck, KeyArray[j]);
240 printf("\n\n\tRandom lookups...\n");
242 for(i=0; i<numDbmEntries; i++) {
243 n = random() % keyIdx;
244 key.dptr = (char *)(KeyArray+n);
245 key.dsize = sizeof(KeyArray[0]);
247 printf("dbm_fetch(dbm, key = %d)\n", n);
250 content = dbm_fetch(dbm, key);
251 dbp = (myDB *)content.dptr;
252 if ( !content.dptr ) {
254 if (n) printf("\n\ndbm_error: %d\n", n);
255 printf("\n%d: Random Lookup of key %4x failed!\n",
257 DumpKeys(n, KeyArray[n]);
265 if (debugflg && dbp) {
266 printf("Rnd rec #%-6d: checksum %4x (%4x)\r", i,
267 dbp->checksum, KeyArray[n]);
271 if (content.dsize == 0)
272 DoSysError("\ndbm_fetch", content.dsize);
273 if (dbp->checksum != KeyArray[n])
274 CkSumError("KeyRand", dbp->checksum, KeyArray[n]);
275 if ((tmpck = calccksum(dbp->data, sizeof(dbp->data))) != KeyArray[n])
276 CkSumError("DataRand", tmpck, KeyArray[n]);
281 void CleanupDbmLookup(void)
287 printf("dbm_close(dbm)\n");
291 sprintf(filename, "%s-%d.dir", DBNAME, (int)getpid());
292 rc = unlink(filename);
293 if (rc) DoSysError("\nunlink", rc);
294 sprintf(filename, "%s-%d.pag", DBNAME, (int)getpid());
295 rc = unlink(filename);
296 if (rc) DoSysError("\nunlink", rc);
299 void DoSysError(char *msg, int rc)
302 fprintf(stderr, "Bailing out! status = %d\n", rc);
306 int creatDbRec(myDB *dbp)
317 printf("\tusing %ld as seed for srandom()...\n\n",
323 ptr = (long *)&(dbp->data);
324 j = sizeof(dbp->data) / sizeof(long);
325 for (i=0; i < j; i++, ptr++) {
328 for (i=(j*sizeof(long)); i<sizeof(dbp->data); i++)
329 dbp->data[i] = (char)time(0);
331 dbp->checksum = calccksum(dbp->data, sizeof(dbp->data));
335 unsigned short calccksum(char *ptr, int size)
341 n = ((size > 100) ? 100 : size);
342 for (i=0; i<n && ptr; i++, ptr++) {
344 sum = (sum >> 1) + 0x8000;
347 sum += (unsigned short)*ptr;
354 void CkSumError(char *msg, unsigned short ck1, unsigned short ck2)
357 printf("%s\n\tChecksum error, %u != %u, Total errors = %d\n",
358 msg, ck1, ck2, Errors);
362 int InsertKey(unsigned short *k, int idx, unsigned short val)
368 return( (k[idx] = val) );
371 for (i=0; i<idx; i++) {
385 void DumpKeys(int n, unsigned short key)
393 if ((f = fopen("keys", "a")) == NULL) {
394 perror("open(keys)");
398 for (i=0, p=arr; i<keyIdx && p; i++, p++)
400 fprintf(f, "%d: 0x%x\n", n, key);