--- freem/src/global_bltin.c 2025/04/08 20:00:56 1.10
+++ freem/src/global_bltin.c 2025/04/09 14:34:30 1.13
@@ -1,5 +1,5 @@
/*
- * $Id: global_bltin.c,v 1.10 2025/04/08 20:00:56 snw Exp $
+ * $Id: global_bltin.c,v 1.13 2025/04/09 14:34:30 snw Exp $
* freem database engine
*
*
@@ -24,6 +24,15 @@
* along with FreeM. If not, see .
*
* $Log: global_bltin.c,v $
+ * Revision 1.13 2025/04/09 14:34:30 snw
+ * Further work on global_bltin.c refactor
+ *
+ * Revision 1.12 2025/04/09 00:43:07 snw
+ * Exit with fatal error if a header mismatch found
+ *
+ * Revision 1.11 2025/04/08 21:41:13 snw
+ * Make insert, update, and splitp global handler functions take a ptr to a global_handle instead of a file descriptor
+ *
* Revision 1.10 2025/04/08 20:00:56 snw
* Global handler now uses a header file and maintains the last journaling transaction ID
*
@@ -63,11 +72,13 @@
#include "global_bltin.h"
global_handle *global_handles_head;
+unsigned long gbl_cache_misses = 0;
+unsigned long gbl_cache_hits = 0;
static void b_free (short filedes, unsigned long blknbr);
-static void splitp (short filedes, char *block, long *addr, long *offs, unsigned long *blknbr);
-static void update (short filedes, char *ins_key, long keyl);
-static void insert (int filedes, char *ins_key, long key_len, unsigned long blknbr);
+static void splitp (global_handle *g, char *block, long *addr, long *offs, unsigned long *blknbr);
+static void update (global_handle *g, char *ins_key, long keyl);
+static void insert (global_handle *g, char *ins_key, long key_len, unsigned long blknbr);
static void scanpblk (char *block, long *adr, long *fnd);
static void scandblk (char *block, long *adr, long *fnd);
static void getnewblk (int filedes, unsigned long *blknbr);
@@ -206,6 +217,19 @@ nextpath:
return i;
} /* gbl_path() */
+void gbl_cache_hit(global_handle *g)
+{
+ g->cache_hits++;
+ gbl_cache_hits++;
+} /* gbl_cache_hit() */
+
+void gbl_cache_miss(global_handle *g)
+{
+ g->fast_path = 0;
+ g->cache_misses++;
+ gbl_cache_misses++;
+} /* gbl_cache_miss() */
+
int gbl_lock(global_handle *g, int type)
{
if (g->locked == TRUE || lonelyflag == TRUE) {
@@ -291,7 +315,7 @@ int gbl_write_header(global_handle *g, g
if (g->opened == FALSE) {
return FALSE;
}
-
+
gbl_lock (g, 1);
old_position = lseek (g->fd, 0, SEEK_CUR);
lseek (g->fd, 0, SEEK_SET);
@@ -303,6 +327,8 @@ int gbl_write_header(global_handle *g, g
lseek (g->fd, old_position, SEEK_SET);
gbl_unlock (g);
+
+ gbl_read_header (g, &g->header);
return TRUE;
} /* gbl_write_header() */
@@ -371,7 +397,8 @@ int gbl_create(global_handle *g)
g->age = time (0L);
g->last_block = 0;
g->use_count = 1;
-
+ g->fast_path = 0;
+
gbl_write_initial_header (g);
return OK;
@@ -380,9 +407,9 @@ int gbl_create(global_handle *g)
short gbl_open(global_handle *g, short action)
{
int result;
- global_header h;
if (g->opened == FALSE) {
+ gbl_cache_miss (g);
while (1) {
errno = 0;
g->fd = open (g->global_path, 2);
@@ -411,13 +438,29 @@ short gbl_open(global_handle *g, short a
}
else {
g->opened = TRUE;
- result = gbl_read_header (g, &h);
-
+ result = gbl_read_header (g, &g->header);
+
if (result == GBL_HDR_OK) {
g->opened = TRUE;
}
- else {
- gbl_close (g);
+ else {
+ gbl_close (g);
+ set_io (UNIX);
+
+ switch (result) {
+
+ case GBL_HDR_BADMAGIC:
+ fprintf (stderr, "gbl_open: bad magic value in %s [FATAL]\n", g->global_name);
+ exit (1);
+ break;
+
+ case GBL_HDR_BADVERSION:
+ fprintf (stderr, "gbl_open: global version is %d in %s (must be %d) [FATAL]\n", g->header.format_version, g->global_name, GBL_FORMAT_VERSION);
+ exit (1);
+ break;
+
+ }
+
return FALSE;
}
}
@@ -474,6 +517,8 @@ global_handle *gbl_handle(char *key)
g->opened = FALSE;
g->fd = 0;
g->fast_path = -1;
+ g->cache_misses = 0;
+ g->cache_hits = 0;
strcpy (g->global_name, global_name);
gbl_path (key, g->global_path);
@@ -1025,7 +1070,8 @@ lock:
tfast0:
gbl_lock (g, 3);
- if (tryfast) goto tfast1; /* try again last block */
+
+ if (g->fast_path > 0) goto tfast1; /* try again last block */
blknbr = g->last_block = ROOT; /* start with ROOT block */
@@ -1056,20 +1102,10 @@ tfast2:
j = UNSIGN (block[i++]); /* length of key - offset */
k = UNSIGN (block[i++]); /* offset into previous entry */
-#ifdef VERSNEW
-
- stcpy0 (&key0[k], &block[i], j);
- i += j;
- j += k;
-
-#else
-
j += k;
while (k < j) key1[k++] = block[i++]; /* get key */
-#endif /* VERSNEW */
-
if (j != ch) { /* keys have different length */
i += UNSIGN (block[i]);
@@ -1103,7 +1139,7 @@ tfast2:
/* fast access failed. try normal path */
if (tryfast) {
- tryfast = FALSE;
+ gbl_cache_miss (g);
goto tfast0;
}
@@ -1114,8 +1150,8 @@ tfast2:
}
else {
- if (tryfast) {
- tryfast = FALSE;
+ if (g->fast_path > 0) {
+ gbl_cache_miss (g);
goto tfast0;
}
@@ -1145,6 +1181,7 @@ tfast2:
addr += PLEN; /* skip data */
g->last_block = blknbr;
+ g->fast_path = 1;
if (merr () == INRPT) goto quit;
@@ -1223,6 +1260,7 @@ k_again: /* entry point for repeated
addr += PLEN; /* skip data */
g->last_block = blknbr;
+ g->fast_path = 1;
}
traceadr[trx] = addr;
@@ -1360,7 +1398,7 @@ s10: {
lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);
write (g->fd, block, BLOCKLEN);
- if (traceadr[trx] == 0) update (g->fd, compactkey, keyl);
+ if (traceadr[trx] == 0) update (g, compactkey, keyl);
break;
}
@@ -2171,7 +2209,7 @@ p_empty: /* entry if pointer block goes
if (addr == 0) { /* update of pointer */
traceadr[trx] = 0;
- update (g->fd, &block[2], (long) UNSIGN (block[0]));
+ update (g, &block[2], (long) UNSIGN (block[0]));
}
trx = trxsav;
@@ -2217,7 +2255,7 @@ p_empty: /* entry if pointer block goes
write (g->fd, block, BLOCKLEN);
if (addr < 3) { /* update of pointer */
traceadr[trx] = 0;
- update (g->fd, &block[2], (long) UNSIGN (block[0]));
+ update (g, &block[2], (long) UNSIGN (block[0]));
}
}
@@ -2580,7 +2618,7 @@ splitd: /* split data block in two se
addr = 0;
blknbr = newblk;
- insert (g->fd, compactkey, keyl, newblk);
+ insert (g, compactkey, keyl, newblk);
/* up-date LL-PTR of RL-block */
if ((other = right * 65536 + right1 * 256 + right2)) {
@@ -2626,12 +2664,12 @@ splitd: /* split data block in two se
blknbr = newblk;
traceadr[trx] = (-1); /* inhibit second update of pointers */
- insert (g->fd, compactkey, keyl, newblk);
+ insert (g, compactkey, keyl, newblk);
if (addr < 3) { /* update of pointer */
traceadr[trx] = 0;
- update (g->fd, compactkey, keyl);
+ update (g, compactkey, keyl);
}
/* other is ***always*** zero !!!
@@ -2745,7 +2783,7 @@ splitd: /* split data block in two se
offset = limit;
/* insert new block in pointer structure */
- insert (g->fd, &block0[2], (long) UNSIGN (block0[0]), newblk);
+ insert (g, &block0[2], (long) UNSIGN (block0[0]), newblk);
/* up-date LL-PTR of RL-block */
if (other != 0) {
@@ -2787,7 +2825,7 @@ splitd: /* split data block in two se
traceblk[trx] = (blknbr = newblk);
/* insert new block in pointer structure */
- insert (g->fd, &block0[2], (long) UNSIGN (block0[0]), newblk);
+ insert (g, &block0[2], (long) UNSIGN (block0[0]), newblk);
/* up-date LL-PTR of RL-block */
if (other != 0) {
@@ -2827,7 +2865,7 @@ spltex:
* 'addr' there I would like to insert, if possible (which is not)
* 'offset' filled up to this limit
*/
-static void splitp (short filedes, char *block, long *addr, long *offs, unsigned long *blknbr)
+static void splitp (global_handle *g, char *block, long *addr, long *offs, unsigned long *blknbr)
{
char block0[BLOCKLEN];
@@ -2840,7 +2878,7 @@ static void splitp (short filedes, char
hdr_offset = sizeof (global_header);
- getnewblk (filedes, &newblk); /* get a new block */
+ getnewblk (g->fd, &newblk); /* get a new block */
if (*blknbr == ROOT) { /* ROOT overflow is special */
@@ -2875,8 +2913,8 @@ static void splitp (short filedes, char
block0[NRBLK + 2] = i % 256;
block0[BTYP] = POINTER;
- lseek (filedes, hdr_offset + ROOT, SEEK_SET);
- write (filedes, block0, BLOCKLEN);
+ lseek (g->fd, hdr_offset + ROOT, SEEK_SET);
+ write (g->fd, block0, BLOCKLEN);
/* shift trace_stack */
j = trx++;
@@ -2893,7 +2931,7 @@ static void splitp (short filedes, char
traceblk[1] = newblk;
*blknbr = newblk;
- getnewblk (filedes, &newblk); /* get a new block */
+ getnewblk (g->fd, &newblk); /* get a new block */
}
@@ -2947,25 +2985,25 @@ static void splitp (short filedes, char
block0[LLPTR + 1] = (*blknbr) % 65536 / 256;
block0[LLPTR + 2] = (*blknbr) % 256;
- lseek (filedes, hdr_offset + ((long) (newblk) * (long) (BLOCKLEN)), SEEK_SET);
- write (filedes, block0, BLOCKLEN);
+ lseek (g->fd, hdr_offset + ((long) (newblk) * (long) (BLOCKLEN)), SEEK_SET);
+ write (g->fd, block0, BLOCKLEN);
(*offs) = limit;
- insert (filedes, &block0[2], (long) UNSIGN (block0[0]), newblk);
+ insert (g, &block0[2], (long) UNSIGN (block0[0]), newblk);
/* up-date LL-PTR of RL-block */
if (other != 0) {
- lseek (filedes, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET);
- read (filedes, block0, BLOCKLEN);
+ lseek (g->fd, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET);
+ read (g->fd, block0, BLOCKLEN);
block0[LLPTR] = newblk / 65536;
block0[LLPTR + 1] = newblk % 65536 / 256;
block0[LLPTR + 2] = newblk % 256;
- lseek (filedes, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET);
- write (filedes, block0, BLOCKLEN);
+ lseek (g->fd, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET);
+ write (g->fd, block0, BLOCKLEN);
}
@@ -2990,26 +3028,26 @@ static void splitp (short filedes, char
(*addr) -= limit;
(*offs) -= limit;
- lseek (filedes, hdr_offset + ((long) (*blknbr) * (long) (BLOCKLEN)), SEEK_SET);
- write (filedes, block, BLOCKLEN);
+ lseek (g->fd, hdr_offset + ((long) (*blknbr) * (long) (BLOCKLEN)), SEEK_SET);
+ write (g->fd, block, BLOCKLEN);
stcpy0 (block, block0, (long) BLOCKLEN);
(*blknbr) = newblk;
- insert (filedes, &block0[2], (long) UNSIGN (block0[0]), newblk);
+ insert (g, &block0[2], (long) UNSIGN (block0[0]), newblk);
/* up-date LL-PTR of RL-block */
if (other != 0) {
- lseek (filedes, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET);
- read (filedes, block0, BLOCKLEN);
+ lseek (g->fd, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET);
+ read (g->fd, block0, BLOCKLEN);
block0[LLPTR] = newblk / 65536;
block0[LLPTR + 1] = newblk % 65536 / 256;
block0[LLPTR + 2] = newblk % 256;
- lseek (filedes, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET);
- write (filedes, block0, BLOCKLEN);
+ lseek (g->fd, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET);
+ write (g->fd, block0, BLOCKLEN);
}
@@ -3024,7 +3062,7 @@ static void splitp (short filedes, char
* ins_key: key to be inserted
* keyl: length of that key
*/
-static void update (short filedes, char *ins_key, long keyl)
+static void update (global_handle *g, char *ins_key, long keyl)
{
long offset;
long addr;
@@ -3042,8 +3080,8 @@ static void update (short filedes, char
blknbr = traceblk[trx];
addr = traceadr[trx];
- lseek (filedes, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);
- read (filedes, block, BLOCKLEN);
+ lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);
+ read (g->fd, block, BLOCKLEN);
{
long oldkeyl;
@@ -3070,7 +3108,7 @@ static void update (short filedes, char
else if (j < 0) { /* we need more space */
/* block too small */
- if ((offset - j) > DATALIM) splitp (filedes, block, &addr, &offset, &blknbr);
+ if ((offset - j) > DATALIM) splitp (g, block, &addr, &offset, &blknbr);
i = offset;
offset -= j;
@@ -3093,13 +3131,13 @@ static void update (short filedes, char
while (i < keyl) block[j++] = ins_key[i++];
/* block pointed to remains the same */
- lseek (filedes, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);
- write (filedes, block, BLOCKLEN);
+ lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);
+ write (g->fd, block, BLOCKLEN);
}
- lseek (filedes, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);
- read (filedes, block, BLOCKLEN);
+ lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);
+ read (g->fd, block, BLOCKLEN);
}
@@ -3114,7 +3152,7 @@ static void update (short filedes, char
* key_len: length of that key
* blknbr: key points to this block
*/
-static void insert (int filedes, char *ins_key, long key_len, unsigned long blknbr) /* insert pointer */
+static void insert (global_handle *g, char *ins_key, long key_len, unsigned long blknbr) /* insert pointer */
{
unsigned long blk;
char block[BLOCKLEN];
@@ -3131,8 +3169,8 @@ static void insert (int filedes, char *i
blk = traceblk[trx];
addr = traceadr[trx];
- lseek (filedes, hdr_offset + ((long) (blk) * (long) (BLOCKLEN)), SEEK_SET);
- read (filedes, block, BLOCKLEN);
+ lseek (g->fd, hdr_offset + ((long) (blk) * (long) (BLOCKLEN)), SEEK_SET);
+ read (g->fd, block, BLOCKLEN);
offset = UNSIGN (block[OFFS]) * 256 +
UNSIGN (block[OFFS + 1]);
@@ -3144,7 +3182,7 @@ static void insert (int filedes, char *i
needed = key_len + 2 + PLEN;
- if ((offset + needed) > DATALIM) splitp (filedes, block, &addr, &offset, &blk);
+ if ((offset + needed) > DATALIM) splitp (g, block, &addr, &offset, &blk);
/* insert key */
i = (offset += needed);
@@ -3168,8 +3206,8 @@ static void insert (int filedes, char *i
block[i++] = blknbr % 65536 / 256;
block[i] = blknbr % 256;
- lseek (filedes, hdr_offset + ((long) (blk) * (long) (BLOCKLEN)), SEEK_SET);
- write (filedes, block, BLOCKLEN);
+ lseek (g->fd, hdr_offset + ((long) (blk) * (long) (BLOCKLEN)), SEEK_SET);
+ write (g->fd, block, BLOCKLEN);
trx = trxsav;
@@ -3706,14 +3744,37 @@ static void panic (void)
void gbl_dump_stat(void)
{
global_handle *g;
+ int ct;
+ int miss_pct;
+ int hit_pct;
+ unsigned long access_total;
+ unsigned long hit_total;
- printf ("FreeM Global Statistics [PID %d]\r\n", pid);
+ printf ("\r\nFreeM Global Statistics [PID %d]\r\n\r\n", pid);
- printf ("%-20s%-10s%-20s%-10s%s\r\n", "GLOBAL", "USECT", "AGE", "LAST BLK", "FILE");
- printf ("%-20s%-10s%-20s%-10s%s\r\n", "======", "=====", "===", "========", "====");
-
+ printf ("%-20s%-10s%-12s%-20s%-10s%s\r\n", "GLOBAL", "USECT", "SLOW PTHCT", "AGE", "LAST BLK", "FILE");
+ printf ("%-20s%-10s%-12s%-20s%-10s%s\r\n", "======", "=====", "==========", "===", "========", "====");
+
+ access_total = 0;
+ ct = 0;
for (g = global_handles_head; g != NULL; g = g->next) {
- printf ("%-20s%-10d%-20d%-10d%s\r\n", g->global_name, g->use_count, g->age, g->last_block, g->global_path);
- }
-
+ printf ("%-20s%-10d%-12d%-20d%-10d%s\r\n",
+ g->global_name,
+ g->use_count,
+ g->cache_misses,
+ g->age,
+ g->last_block,
+ g->global_path);
+ ct++;
+ access_total += g->use_count;
+ }
+ if (!ct) printf ("\r\n");
+
+ hit_total = access_total - gbl_cache_misses;
+ miss_pct = (gbl_cache_misses * 100) / access_total;
+ hit_pct = (hit_total * 100) / access_total;
+
+ printf ("\r\nTotal accesses: %ld\r\n", access_total);
+ printf ("Fast path hits %ld\t(%d%%)\r\n", hit_total, hit_pct);
+ printf ("Fast path misses: %ld\t(%d%%)\r\n", gbl_cache_misses, miss_pct);
}