--- freem/src/global_bltin.c 2025/04/08 20:00:56 1.10
+++ freem/src/global_bltin.c 2025/04/10 01:24:38 1.15
@@ -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.15 2025/04/10 01:24:38 snw Exp $
* freem database engine
*
*
@@ -24,6 +24,21 @@
* along with FreeM. If not, see .
*
* $Log: global_bltin.c,v $
+ * Revision 1.15 2025/04/10 01:24:38 snw
+ * Remove C++ style comments
+ *
+ * Revision 1.14 2025/04/09 19:52:02 snw
+ * Eliminate as many warnings as possible while building with -Wall
+ *
+ * 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,16 +78,17 @@
#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);
static short int g_collate (char *t);
-//static short int g_numeric (char *str);
short g_numeric (char *str);
void close_all_globals(void);
static void panic (void);
@@ -123,19 +139,14 @@ static void panic (void);
inline long gbl_path(char *key, char *buf)
{
long savj;
- long savch; /* saved j and ch for multiple pathes */
register long int i;
register long int j;
register long int k;
register long int ch;
- long pathscan; /* flag for repeated scan of pathlist setting an undef global */
/* construct full UNIX filename */
savj = 0;
- savch = ch = EOL;
- pathscan = TRUE;
-nextpath:
k = 0;
j = savj;
@@ -159,8 +170,6 @@ nextpath:
}
}
-
- if (savj == 0 && ch == EOL) pathscan = FALSE; /* one path only: inhibit search */
if (k > 0) {
@@ -173,7 +182,6 @@ nextpath:
}
- savch = ch;
savj = j;
i = 0;
j = 0;
@@ -206,6 +214,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) {
@@ -214,6 +235,8 @@ int gbl_lock(global_handle *g, int type)
locking (g->fd, type, 0L);
g->locked = TRUE;
+
+ return TRUE;
} /* gbl_lock() */
int gbl_unlock(global_handle *g)
@@ -224,6 +247,8 @@ int gbl_unlock(global_handle *g)
locking (g->fd, 0, 0L);
g->locked = FALSE;
+
+ return TRUE;
} /* gbl_unlock() */
void gbl_close(global_handle *g)
@@ -291,7 +316,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 +328,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 +398,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 +408,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 +439,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;
}
}
@@ -432,8 +476,6 @@ global_handle *gbl_handle(char *key)
global_handle *g;
char global_name[256];
int i;
- long path_len;
- char block[BLOCKLEN];
struct stat dinf;
i = 0;
@@ -474,6 +516,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);
@@ -568,7 +612,6 @@ void global_bltin (short action, char *k
unsigned long hdr_offset;
/* these must be static variables */
- static short filedes; /* filedescr for global access */
static char filnam[256]; /* name of global/unix file */
/* the following vars may be */
@@ -597,16 +640,13 @@ void global_bltin (short action, char *k
int iresult;
- struct stat dinf; /* get modification date */
-
- long savj;
- long savch; /* saved j and ch for multiple pathes */
register long int i;
register long int j;
register long int k;
register long int ch;
- long pathscan; /* flag for repeated scan of pathlist setting an undef global */
+ j = 0;
+
hdr_offset = sizeof (global_header);
/* process optional limitations */
@@ -1019,13 +1059,16 @@ reopen:
/* odd numbered actions get read access (get_sym,data,fra_order) 3 */
/* even ones read/write access (set_sym,kill_sym) 1 */
+/* temporarily disabled
lock:
+*/
if (action == get_sym) {
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 */
@@ -1037,9 +1080,9 @@ tfast1:
lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);
read (g->fd, block, BLOCKLEN);
-
+/* temporarily disabled
tfast2:
-
+*/
if ((typ = block[BTYP]) == DATA) { /* scan data block: here we test for equality only */
offset = UNSIGN (block[OFFS]) * 256 +
@@ -1056,20 +1099,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 +1136,7 @@ tfast2:
/* fast access failed. try normal path */
if (tryfast) {
- tryfast = FALSE;
+ gbl_cache_miss (g);
goto tfast0;
}
@@ -1114,15 +1147,14 @@ tfast2:
}
else {
- if (tryfast) {
- tryfast = FALSE;
+ if (g->fast_path > 0) {
+ gbl_cache_miss (g);
goto tfast0;
}
if (typ == EMPTY) {
if (blknbr == ROOT) {
- //close (filedes);
gbl_close (g);
goto reopen;
}
@@ -1145,6 +1177,7 @@ tfast2:
addr += PLEN; /* skip data */
g->last_block = blknbr;
+ g->fast_path = 1;
if (merr () == INRPT) goto quit;
@@ -1223,6 +1256,7 @@ k_again: /* entry point for repeated
addr += PLEN; /* skip data */
g->last_block = blknbr;
+ g->fast_path = 1;
}
traceadr[trx] = addr;
@@ -1360,7 +1394,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 +2205,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 +2251,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 +2614,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 +2660,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 +2779,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 +2821,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 +2861,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 +2874,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 +2909,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 +2927,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 +2981,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 +3024,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 +3058,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 +3076,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 +3104,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 +3127,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 +3148,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 +3165,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 +3178,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 +3202,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;
@@ -3630,7 +3664,6 @@ static short int g_collate (char *t)
/*
* test whether 'str' is canonical
*/
-//static short int g_numeric (char *str)
short g_numeric (char *str)
{
register int ptr = 0, ch;
@@ -3706,14 +3739,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%-10ld%-12ld%-20ld%-10ld%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);
}