--- freem/src/global_bltin.c 2025/04/09 19:52:02 1.14
+++ freem/src/global_bltin.c 2025/04/11 16:23:18 1.18
@@ -1,5 +1,5 @@
/*
- * $Id: global_bltin.c,v 1.14 2025/04/09 19:52:02 snw Exp $
+ * $Id: global_bltin.c,v 1.18 2025/04/11 16:23:18 snw Exp $
* freem database engine
*
*
@@ -24,6 +24,18 @@
* along with FreeM. If not, see .
*
* $Log: global_bltin.c,v $
+ * Revision 1.18 2025/04/11 16:23:18 snw
+ * Avoid re-reading the same block consecutively when possible
+ *
+ * Revision 1.17 2025/04/11 14:21:03 snw
+ * Make all but one of the read/write calls in global_bltin use gbl_read_block or gbl_write_block
+ *
+ * Revision 1.16 2025/04/11 00:52:40 snw
+ * Replace all lseek/read calls in global handler to use gbl_read_block function
+ *
+ * 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
*
@@ -78,15 +90,14 @@ 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 b_free (global_handle *g, 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 void getnewblk (global_handle *g, 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);
@@ -314,7 +325,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);
@@ -437,9 +448,13 @@ short gbl_open(global_handle *g, short a
}
else {
g->opened = TRUE;
- result = gbl_read_header (g, &g->header);
+ result = gbl_read_header (g, &g->header);
if (result == GBL_HDR_OK) {
+ /* initialize last_block_accessed cache */
+ g->last_block_accessed = (char *) calloc (g->header.block_size, sizeof (char));
+ NULLPTRCHK(g->last_block_accessed,"gbl_open");
+
g->opened = TRUE;
}
else {
@@ -469,6 +484,87 @@ short gbl_open(global_handle *g, short a
} /* gbl_open() */
+int gbl_read_block(global_handle *g, unsigned long blocknum, char *block)
+{
+ struct stat gstat;
+ unsigned long hdr_offset;
+ hdr_offset = sizeof (global_header);
+
+ if (g->opened == FALSE) {
+ return FALSE;
+ }
+
+ g->use_count++;
+
+ fstat (g->fd, &gstat);
+ if (!g->locked) gbl_lock (g, 1);
+
+
+ if ((g->last_block == blocknum) &&
+ (g->have_cached_block) &&
+ (g->cached_block_num == blocknum) &&
+ (gstat.st_mtime < g->last_read_time)) {
+ /* the global has not been modified since the last read; grab from memory */
+ g->memory_reads++;
+ g->last_read_time = time (0L);
+ memcpy (block, g->last_block_accessed, g->header.block_size);
+ }
+ else {
+ /* have to go out to disk */
+ lseek (g->fd, hdr_offset + ((long) blocknum * (long) (g->header.block_size)), SEEK_SET);
+ read (g->fd, block, g->header.block_size);
+
+ /* update cache */
+ memcpy (g->last_block_accessed, block, g->header.block_size);
+ g->have_cached_block = TRUE;
+ g->last_read_time = time (0L);
+ g->cached_block_num = blocknum;
+
+ g->last_block = blocknum;
+ g->use_count++;
+ g->read_ops++;
+ }
+
+ if (g->locked) gbl_unlock (g);
+
+ return TRUE;
+} /* gbl_read_block() */
+
+int gbl_write_block(global_handle *g, unsigned long blocknum, char *block)
+{
+ int errsav;
+ unsigned long hdr_offset;
+ hdr_offset = sizeof (global_header);
+
+ if (g->opened == FALSE) {
+ return FALSE;
+ }
+
+ gbl_lock (g, 1);
+
+ for (;;) {
+
+ errno = 0;
+
+ lseek (g->fd, hdr_offset + (blocknum * g->header.block_size), SEEK_SET);
+ write (g->fd, block, BLOCKLEN);
+ errsav = errno;
+ g->last_block = blocknum;
+ g->use_count++;
+ g->write_ops++;
+
+ if (errsav == 0) break;
+
+ panic ();
+
+ }
+
+ gbl_update_tid (g);
+ gbl_unlock (g);
+
+ return TRUE;
+}
+
global_handle *gbl_handle(char *key)
{
global_handle *g;
@@ -510,12 +606,18 @@ global_handle *gbl_handle(char *key)
g->use_count = 1;
g->locked = FALSE;
g->age = time (0L);
+ g->last_read_time = 0;
g->last_block = 0;
g->opened = FALSE;
g->fd = 0;
g->fast_path = -1;
g->cache_misses = 0;
g->cache_hits = 0;
+ g->read_ops = 0;
+ g->write_ops = 0;
+ g->memory_reads = 0;
+ g->have_cached_block = FALSE;
+ g->last_block_accessed = NULL;
strcpy (g->global_name, global_name);
gbl_path (key, g->global_path);
@@ -643,6 +745,8 @@ void global_bltin (short action, char *k
register long int k;
register long int ch;
+ j = 0;
+
hdr_offset = sizeof (global_header);
/* process optional limitations */
@@ -1007,19 +1111,9 @@ reopen:
merr_raise (iresult);
return;
}
-
- for (;;) {
-
- errno = 0;
-
- lseek (g->fd, hdr_offset + (ROOT * BLOCKLEN), SEEK_SET);
- write (g->fd, block, BLOCKLEN);
-
- if (errno == 0) break;
-
- panic ();
- }
+ gbl_write_block (g, ROOT, block);
+
block[NRBLK] = 0;
block[NRBLK + 1] = 0;
block[NRBLK + 2] = ROOT; /* clear */
@@ -1056,12 +1150,12 @@ reopen:
/* even ones read/write access (set_sym,kill_sym) 1 */
/* temporarily disabled
-lock:
+ lock:
*/
if (action == get_sym) {
-tfast0:
+ tfast0:
gbl_lock (g, 3);
if (g->fast_path > 0) goto tfast1; /* try again last block */
@@ -1071,103 +1165,100 @@ tfast0:
for (;;) {
-tfast1:
-
- lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);
- read (g->fd, block, BLOCKLEN);
+ tfast1:
+ gbl_read_block (g, blknbr, block);
/* temporarily disabled
-tfast2:
+ tfast2:
*/
- if ((typ = block[BTYP]) == DATA) { /* scan data block: here we test for equality only */
-
- offset = UNSIGN (block[OFFS]) * 256 +
- UNSIGN (block[OFFS + 1]);
- j = UNSIGN (block[0]);
- i = 0;
+ if ((typ = block[BTYP]) == DATA) { /* scan data block: here we test for equality only */
- stcpy0 (key1, &block[2], j); /* get first key */
-
- ch = keyl; /* ch is a register! */
-
- while (i < offset) {
-
- j = UNSIGN (block[i++]); /* length of key - offset */
- k = UNSIGN (block[i++]); /* offset into previous entry */
+ offset = UNSIGN (block[OFFS]) * 256 +
+ UNSIGN (block[OFFS + 1]);
+ j = UNSIGN (block[0]);
+ i = 0;
- j += k;
+ stcpy0 (key1, &block[2], j); /* get first key */
- while (k < j) key1[k++] = block[i++]; /* get key */
-
- if (j != ch) { /* keys have different length */
+ ch = keyl; /* ch is a register! */
+
+ while (i < offset) {
+
+ j = UNSIGN (block[i++]); /* length of key - offset */
+ k = UNSIGN (block[i++]); /* offset into previous entry */
+
+ j += k;
+
+ while (k < j) key1[k++] = block[i++]; /* get key */
+
+ if (j != ch) { /* keys have different length */
+
+ i += UNSIGN (block[i]);
+ i++;
+
+ continue;
+
+ }
+
+ j = ch;
+
+ do {
+ j--;
+ } while (compactkey[j] == key1[j]); /* compare keys */
+
+
+ if (j < 0) {
+
+ k = UNSIGN (block[i++]);
+ stcpy0 (data, &block[i], k); /* get value */
+ data[k] = EOL1; /* append EOL */
+
+ goto quit;
+
+ }
i += UNSIGN (block[i]);
- i++;
-
- continue;
-
+ i++; /* skip data */
+
+ }
+
+ /* fast access failed. try normal path */
+ if (tryfast) {
+ gbl_cache_miss (g);
+ goto tfast0;
}
- j = ch;
+ merr_raise (M7);
+ data[0] = EOL;
+
+ goto quit; /* variable not found */
+ }
+ else {
- do {
- j--;
- } while (compactkey[j] == key1[j]); /* compare keys */
+ if (g->fast_path > 0) {
+ gbl_cache_miss (g);
+ goto tfast0;
+ }
-
- if (j < 0) {
+ if (typ == EMPTY) {
- k = UNSIGN (block[i++]);
- stcpy0 (data, &block[i], k); /* get value */
- data[k] = EOL1; /* append EOL */
-
+ if (blknbr == ROOT) {
+ gbl_close (g);
+ goto reopen;
+ }
+
+ merr_raise (DBDGD);
goto quit;
-
+
}
-
- i += UNSIGN (block[i]);
- i++; /* skip data */
-
- }
-
- /* fast access failed. try normal path */
- if (tryfast) {
- gbl_cache_miss (g);
- goto tfast0;
- }
-
- merr_raise (M7);
- data[0] = EOL;
-
- goto quit; /* variable not found */
- }
- else {
-
- if (g->fast_path > 0) {
- gbl_cache_miss (g);
- goto tfast0;
- }
-
- if (typ == EMPTY) {
- if (blknbr == ROOT) {
- //close (filedes);
- gbl_close (g);
- goto reopen;
- }
-
- merr_raise (DBDGD);
- goto quit;
-
}
-
- }
-
- scanpblk (block, &addr, &found);
-
- addr += UNSIGN (block[addr]) + 2; /* skip key */
-
- if ((blknbr = UNSIGN (block[addr]) * 65536 + UNSIGN (block[addr + 1]) * 256 + UNSIGN (block[addr + 2])) == g->last_block) {
+
+ scanpblk (block, &addr, &found);
+
+ addr += UNSIGN (block[addr]) + 2; /* skip key */
+
+ if ((blknbr = UNSIGN (block[addr]) * 65536 + UNSIGN (block[addr + 1]) * 256 + UNSIGN (block[addr + 2])) == g->last_block) {
merr_raise (DBDGD);
goto quit;
}
@@ -1186,14 +1277,12 @@ tfast2:
/* a KILL on an unsubscripted global deletes the entire file */
if (action == kill_sym && compactkey[0] == g_EOL) {
- lseek (g->fd, hdr_offset + ROOT, SEEK_SET);
-
- /* note : UNIX does not tell other */
- block[BTYP] = EMPTY; /* jobs that a file has been unlinked */
-
+ /* note : UNIX does not tell other jobs that a file has been unlinked */
/* as long as they keep it open. */
/* so we mark this global as EMPTY */
- write (g->fd, block, BLOCKLEN);
+ block[BTYP] = EMPTY;
+
+ gbl_write_block (g, ROOT, block);
gbl_unlock (g);
gbl_close (g);
@@ -1218,10 +1307,8 @@ k_again: /* entry point for repeated
traceblk[trx] = blknbr;
traceadr[trx] = 0;
-
- lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);
- read (g->fd, block, BLOCKLEN);
-
+
+ gbl_read_block (g, blknbr, block);
typ = block[BTYP];
if (typ == DATA) {
@@ -1388,8 +1475,7 @@ s10: {
}
- lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);
- write (g->fd, block, BLOCKLEN);
+ gbl_write_block (g, blknbr, block);
if (traceadr[trx] == 0) update (g, compactkey, keyl);
@@ -1475,10 +1561,8 @@ s20:
}
stcpy0 (&block[++addr], data, (long) datal);
- lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);
- write (g->fd, block, BLOCKLEN);
- gbl_update_tid (g);
+ gbl_write_block (g, blknbr, block);
break;
@@ -1509,9 +1593,8 @@ s20:
if (addr >= offset) {
if ((blknbr = UNSIGN (block[RLPTR]) * 65536 + UNSIGN (block[RLPTR + 1]) * 256 + UNSIGN (block[RLPTR + 2]))) {
-
- lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);
- read (g->fd, block, BLOCKLEN);
+
+ gbl_read_block (g, blknbr, block);
j1 = UNSIGN (block[0]);
i = 0;
@@ -1592,9 +1675,8 @@ s20:
goto quit;
} /* no next block */
-
- lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);
- read (g->fd, block, BLOCKLEN);
+
+ gbl_read_block (g, blknbr, block);
scandblk (block, &addr, &found);
}
@@ -1750,8 +1832,7 @@ s20:
goto quit; /* no next block */
}
- lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);
- read (g->fd, block, BLOCKLEN);
+ gbl_read_block (g, blknbr, block);
addr = 0;
offset = UNSIGN (block[OFFS]) * 256 +
@@ -1774,8 +1855,7 @@ s20:
goto quit; /* no next block */
}
- lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);
- read (g->fd, block, BLOCKLEN);
+ gbl_read_block (g, blknbr, block);
addr = 0;
}
@@ -1984,9 +2064,8 @@ killo: /* entry from killone section
other = traceblk[--trx];
addr = traceadr[trx];
-
- lseek (g->fd, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET);
- read (g->fd, block, BLOCKLEN);
+
+ gbl_read_block (g, other, block);
addr += UNSIGN (block[addr]);
addr += (2 + PLEN); /* skip previous entry */
@@ -2005,8 +2084,7 @@ killo: /* entry from killone section
trx = trxsav;
- lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);
- read (g->fd, block, BLOCKLEN);
+ gbl_read_block (g, blknbr, block);
offset = UNSIGN (block[OFFS]) * 256 +
UNSIGN (block[OFFS + 1]);
@@ -2112,33 +2190,29 @@ p_empty: /* entry if pointer block goes
if (left) {
- lseek (g->fd, hdr_offset + ((long) left * (long) (BLOCKLEN)), SEEK_SET);
- read (g->fd, block0, BLOCKLEN);
+ gbl_read_block (g, left, block0);
block0[RLPTR] = block[RLPTR];
block0[RLPTR + 1] = block[RLPTR + 1];
block0[RLPTR + 2] = block[RLPTR + 2];
-
- lseek (g->fd, hdr_offset + ((long) left * (long) (BLOCKLEN)), SEEK_SET);
- write (g->fd, block0, BLOCKLEN);
+
+ gbl_write_block (g, left, block0);
}
if (right) {
-
- lseek (g->fd, hdr_offset + ((long) right * (long) (BLOCKLEN)), SEEK_SET);
- read (g->fd, block0, BLOCKLEN);
+
+ gbl_read_block (g, right, block0);
block0[LLPTR] = block[LLPTR];
block0[LLPTR + 1] = block[LLPTR + 1];
block0[LLPTR + 2] = block[LLPTR + 2];
-
- lseek (g->fd, hdr_offset + ((long) right * (long) (BLOCKLEN)), SEEK_SET);
- write (g->fd, block0, BLOCKLEN);
+
+ gbl_write_block (g, right, block0);
}
- b_free (g->fd, blknbr); /* modify free list */
+ b_free (g, blknbr); /* modify free list */
/* delete pointer */
/**************************/
@@ -2151,8 +2225,7 @@ p_empty: /* entry if pointer block goes
blknbr = traceblk[--trx];
addr = traceadr[trx];
- lseek (g->fd, hdr_offset + ((long) (blknbr) * (long) (BLOCKLEN)), SEEK_SET);
- read (g->fd, block, BLOCKLEN);
+ gbl_read_block (g, blknbr, block);
offset = UNSIGN (block[OFFS]) * 256 +
UNSIGN (block[OFFS + 1]);
freecnt = UNSIGN (block[addr]) + 2 + PLEN;
@@ -2164,14 +2237,15 @@ p_empty: /* entry if pointer block goes
if (blknbr == ROOT) { /* global went empty */
- lseek (g->fd, hdr_offset + 0L, SEEK_SET);
+
/* note : UNIX does not tell other */
block[BTYP] = EMPTY; /* jobs that a file has been unlinked */
/* as long as they keep it open. */
/* so we mark this global as EMPTY */
- write (g->fd, block, BLOCKLEN);
+ gbl_write_block (g, 0L, block);
+
gbl_close (g);
unlink (filnam);
@@ -2196,8 +2270,7 @@ p_empty: /* entry if pointer block goes
for (i = offset; i < offset + freecnt; block[i++] = 0);
- lseek (g->fd, hdr_offset + ((long) (blknbr) * (long) (BLOCKLEN)), SEEK_SET);
- write (g->fd, block, BLOCKLEN);
+ gbl_write_block (g, blknbr, block);
if (addr == 0) { /* update of pointer */
traceadr[trx] = 0;
@@ -2244,11 +2317,12 @@ p_empty: /* entry if pointer block goes
}
block[OFFS] = offset / 256;
block[OFFS + 1] = offset % 256;
- lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);
- write (g->fd, block, BLOCKLEN);
+
+ gbl_write_block (g, blknbr, block);
+
if (addr < 3) { /* update of pointer */
- traceadr[trx] = 0;
- update (g, &block[2], (long) UNSIGN (block[0]));
+ traceadr[trx] = 0;
+ update (g, &block[2], (long) UNSIGN (block[0]));
}
}
@@ -2270,9 +2344,8 @@ zinv:
data[0] = EOL1;
goto quit;
} /* no previous block */
-
- lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);
- read (g->fd, block, BLOCKLEN);
+
+ gbl_read_block (g, blknbr, block);
addr = UNSIGN (block[OFFS]) * 256 +
UNSIGN (block[OFFS + 1]);
@@ -2451,8 +2524,7 @@ zinv:
break; /* no next block */
}
- lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);
- read (g->fd, block, BLOCKLEN);
+ gbl_read_block (g, blknbr, block);
addr = 0;
offset = UNSIGN (block[OFFS]) * 256 +
@@ -2580,7 +2652,7 @@ splitd: /* split data block in two se
/* 'addr' there I would like to insert, if possible (which is not) */
/* 'offset' filled up to this limit */
- getnewblk (g->fd, &newblk); /* get a new block */
+ getnewblk (g, &newblk); /* get a new block */
/* if we have to insert at the begin or end of a block */
/* we don't split - we just start a new block */
@@ -2598,8 +2670,7 @@ splitd: /* split data block in two se
block[RLPTR + 1] = newblk % 65536 / 256;
block[RLPTR + 2] = newblk % 256;
- lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);
- write (g->fd, block, BLOCKLEN);
+ gbl_write_block (g, blknbr, block);
block[RLPTR] = right;
block[RLPTR + 1] = right1;
@@ -2618,16 +2689,14 @@ splitd: /* split data block in two se
char block0[BLOCKLEN];
- lseek (g->fd, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET);
- read (g->fd, block0, BLOCKLEN);
+ gbl_read_block (g, other, block0);
block0[LLPTR] = blknbr / 65536;
block0[LLPTR + 1] = blknbr % 65536 / 256;
block0[LLPTR + 2] = blknbr % 256;
- lseek (g->fd, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET);
- write (g->fd, block0, BLOCKLEN);
-
+ gbl_write_block (g, other, block0);
+
}
goto spltex;
@@ -2644,9 +2713,8 @@ splitd: /* split data block in two se
block[LLPTR + 1] = newblk % 65536 / 256;
block[LLPTR + 2] = newblk % 256;
- lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);
- write (g->fd, block, BLOCKLEN);
-
+ gbl_write_block (g, blknbr, block);
+
block[LLPTR] = left;
block[LLPTR + 1] = left1;
block[LLPTR + 2] = left2;
@@ -2770,8 +2838,7 @@ splitd: /* split data block in two se
block0[LLPTR + 1] = blknbr % 65536 / 256;
block0[LLPTR + 2] = blknbr % 256;
- lseek (g->fd, hdr_offset + ((long) (newblk) * (long) (BLOCKLEN)), SEEK_SET);
- write (g->fd, block0, BLOCKLEN);
+ gbl_write_block (g, newblk, block0);
offset = limit;
/* insert new block in pointer structure */
@@ -2781,15 +2848,13 @@ splitd: /* split data block in two se
/* up-date LL-PTR of RL-block */
if (other != 0) {
- lseek (g->fd, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET);
- read (g->fd, block0, BLOCKLEN);
+ gbl_read_block (g, other, block0);
block0[LLPTR] = newblk / 65536;
block0[LLPTR + 1] = newblk % 65536 / 256;
block0[LLPTR + 2] = newblk % 256;
-
- lseek (g->fd, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET);
- write (g->fd, block0, BLOCKLEN);
+
+ gbl_write_block (g, other, block0);
}
@@ -2810,8 +2875,8 @@ splitd: /* split data block in two se
block0[LLPTR + 1] = blknbr % 65536 / 256;
block0[LLPTR + 2] = blknbr % 256;
- lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);
- write (g->fd, block, BLOCKLEN);
+ gbl_write_block (g, blknbr, block);
+
stcpy0 (block, block0, (long) BLOCKLEN);
traceadr[trx] = (addr -= limit);
@@ -2823,15 +2888,13 @@ splitd: /* split data block in two se
/* up-date LL-PTR of RL-block */
if (other != 0) {
- lseek (g->fd, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET);
- read (g->fd, block0, BLOCKLEN);
+ gbl_read_block (g, other, block0);
block0[LLPTR] = newblk / 65536;
block0[LLPTR + 1] = newblk % 65536 / 256;
block0[LLPTR + 2] = newblk % 256;
-
- lseek (g->fd, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET);
- write (g->fd, block0, BLOCKLEN);
+
+ gbl_write_block (g, other, block0);
}
@@ -2866,12 +2929,8 @@ static void splitp (global_handle *g, ch
unsigned long newblk;
unsigned long other;
register int i, j;
-
- long hdr_offset;
-
- hdr_offset = sizeof (global_header);
- getnewblk (g->fd, &newblk); /* get a new block */
+ getnewblk (g, &newblk); /* get a new block */
if (*blknbr == ROOT) { /* ROOT overflow is special */
@@ -2906,8 +2965,7 @@ static void splitp (global_handle *g, ch
block0[NRBLK + 2] = i % 256;
block0[BTYP] = POINTER;
- lseek (g->fd, hdr_offset + ROOT, SEEK_SET);
- write (g->fd, block0, BLOCKLEN);
+ gbl_write_block (g, ROOT, block0);
/* shift trace_stack */
j = trx++;
@@ -2924,7 +2982,7 @@ static void splitp (global_handle *g, ch
traceblk[1] = newblk;
*blknbr = newblk;
- getnewblk (g->fd, &newblk); /* get a new block */
+ getnewblk (g, &newblk); /* get a new block */
}
@@ -2978,8 +3036,7 @@ static void splitp (global_handle *g, ch
block0[LLPTR + 1] = (*blknbr) % 65536 / 256;
block0[LLPTR + 2] = (*blknbr) % 256;
- lseek (g->fd, hdr_offset + ((long) (newblk) * (long) (BLOCKLEN)), SEEK_SET);
- write (g->fd, block0, BLOCKLEN);
+ gbl_write_block (g, newblk, block0);
(*offs) = limit;
@@ -2988,15 +3045,13 @@ static void splitp (global_handle *g, ch
/* up-date LL-PTR of RL-block */
if (other != 0) {
- lseek (g->fd, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET);
- read (g->fd, block0, BLOCKLEN);
+ gbl_read_block (g, other, block0);
block0[LLPTR] = newblk / 65536;
block0[LLPTR + 1] = newblk % 65536 / 256;
block0[LLPTR + 2] = newblk % 256;
-
- lseek (g->fd, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET);
- write (g->fd, block0, BLOCKLEN);
+
+ gbl_write_block (g, other, block0);
}
@@ -3020,9 +3075,9 @@ static void splitp (global_handle *g, ch
(*addr) -= limit;
(*offs) -= limit;
+
+ gbl_write_block (g, *blknbr, block);
- lseek (g->fd, hdr_offset + ((long) (*blknbr) * (long) (BLOCKLEN)), SEEK_SET);
- write (g->fd, block, BLOCKLEN);
stcpy0 (block, block0, (long) BLOCKLEN);
(*blknbr) = newblk;
@@ -3032,15 +3087,13 @@ static void splitp (global_handle *g, ch
/* up-date LL-PTR of RL-block */
if (other != 0) {
- lseek (g->fd, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET);
- read (g->fd, block0, BLOCKLEN);
+ gbl_read_block (g, other, block0);
block0[LLPTR] = newblk / 65536;
block0[LLPTR + 1] = newblk % 65536 / 256;
block0[LLPTR + 2] = newblk % 256;
-
- lseek (g->fd, hdr_offset + ((long) other * (long) (BLOCKLEN)), SEEK_SET);
- write (g->fd, block0, BLOCKLEN);
+
+ gbl_write_block (g, other, block0);
}
@@ -3062,9 +3115,6 @@ static void update (global_handle *g, ch
unsigned long blknbr;
char block[BLOCKLEN];
long i, j, k;
- long hdr_offset;
-
- hdr_offset = sizeof (global_header);
while (traceadr[trx] == 0) { /* update of pointer blocks necessary */
@@ -3072,9 +3122,8 @@ static void update (global_handle *g, ch
blknbr = traceblk[trx];
addr = traceadr[trx];
-
- lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);
- read (g->fd, block, BLOCKLEN);
+
+ gbl_read_block (g, blknbr, block);
{
long oldkeyl;
@@ -3124,13 +3173,10 @@ static void update (global_handle *g, ch
while (i < keyl) block[j++] = ins_key[i++];
/* block pointed to remains the same */
- lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);
- write (g->fd, block, BLOCKLEN);
-
+ gbl_write_block (g, blknbr, block);
}
-
- lseek (g->fd, hdr_offset + ((long) blknbr * (long) (BLOCKLEN)), SEEK_SET);
- read (g->fd, block, BLOCKLEN);
+
+ gbl_read_block (g, blknbr, block);
}
@@ -3154,16 +3200,12 @@ static void insert (global_handle *g, ch
long needed;
long addr;
register int i, k;
- long hdr_offset;
-
- hdr_offset = sizeof (global_header);
trxsav = trx--;
blk = traceblk[trx];
addr = traceadr[trx];
- lseek (g->fd, hdr_offset + ((long) (blk) * (long) (BLOCKLEN)), SEEK_SET);
- read (g->fd, block, BLOCKLEN);
+ gbl_read_block (g, blk, block);
offset = UNSIGN (block[OFFS]) * 256 +
UNSIGN (block[OFFS + 1]);
@@ -3199,8 +3241,7 @@ static void insert (global_handle *g, ch
block[i++] = blknbr % 65536 / 256;
block[i] = blknbr % 256;
- lseek (g->fd, hdr_offset + ((long) (blk) * (long) (BLOCKLEN)), SEEK_SET);
- write (g->fd, block, BLOCKLEN);
+ gbl_write_block (g, blk, block);
trx = trxsav;
@@ -3212,36 +3253,29 @@ static void insert (global_handle *g, ch
* filedes: global file descriptor
* blknbr: block to be freed
*/
-static void b_free (short filedes, unsigned long blknbr)
+static void b_free (global_handle *g, unsigned long blknbr)
{
char block0[BLOCKLEN];
unsigned long free;
unsigned long other;
long i;
long offset;
- long hdr_offset;
-
- hdr_offset = sizeof (global_header);
/* mark block as empty */
- lseek (filedes, hdr_offset + ((long) (blknbr) * BLOCKLEN), SEEK_SET);
- read (filedes, block0, BLOCKLEN);
-
- block0[BTYP] = EMPTY;
+ gbl_read_block (g, blknbr, block0);
- lseek (filedes, hdr_offset + ((long) (blknbr) * BLOCKLEN), SEEK_SET);
- write (filedes, block0, BLOCKLEN);
+ block0[BTYP] = EMPTY;
+
+ gbl_write_block (g, blknbr, block0);
/* do we have a list of free blocks? */
- lseek (filedes, hdr_offset + ROOT, SEEK_SET);
- read (filedes, block0, BLOCKLEN);
+ gbl_read_block (g, ROOT, block0);
if ((free = UNSIGN (block0[FREE]) * 65536 + UNSIGN (block0[FREE + 1]) * 256 + UNSIGN (block0[FREE + 2]))) {
for (;;) {
-
- lseek (filedes, hdr_offset + ((long) free * (long) BLOCKLEN), SEEK_SET);
- read (filedes, block0, BLOCKLEN);
+
+ gbl_read_block (g, free, block0);
other = UNSIGN (block0[RLPTR]) * 65536 +
UNSIGN (block0[RLPTR + 1]) * 256 +
@@ -3272,9 +3306,8 @@ static void b_free (short filedes, unsig
block0[RLPTR] = other / 65536;
block0[RLPTR + 1] = other % 65536 / 256;
block0[RLPTR + 2] = other % 256;
-
- lseek (filedes, hdr_offset + ((long) free * (long) BLOCKLEN), SEEK_SET);
- write (filedes, block0, BLOCKLEN);
+
+ gbl_write_block (g, free, block0);
for (i = 0; i < BLOCKLEN; block0[i++] = 0); /* clear block */
@@ -3290,19 +3323,16 @@ static void b_free (short filedes, unsig
}
else {
-
- getnewblk (filedes, &free);
+ getnewblk (g, &free);
/* set FBLK free blocks pointer */
- lseek (filedes, hdr_offset + ROOT, SEEK_SET);
- read (filedes, block0, BLOCKLEN);
+ gbl_read_block (g, ROOT, block0);
block0[FREE] = free / 65536;
block0[FREE + 1] = free % 65536 / 256;
block0[FREE + 2] = free % 256;
-
- lseek (filedes, hdr_offset + ROOT, SEEK_SET);
- write (filedes, block0, BLOCKLEN);
+
+ gbl_write_block (g, ROOT, block0);
for (i = 0; i < BLOCKLEN; block0[i++] = 0); /* clear block */
@@ -3317,8 +3347,7 @@ static void b_free (short filedes, unsig
block0[OFFS] = offset / 256;
block0[OFFS + 1] = offset % 256;
- lseek (filedes, hdr_offset + ((long) free * (long) BLOCKLEN), SEEK_SET);
- write (filedes, block0, BLOCKLEN);
+ gbl_write_block (g, free, block0);
return;
} /* end of b_free() */
@@ -3438,26 +3467,21 @@ static void scandblk (char *block, long
* filedes: global file descriptor
* blknbr: number of new block
*/
-static void getnewblk (int filedes, unsigned long *blknbr)
+static void getnewblk (global_handle *g, unsigned long *blknbr)
{
char nblock[BLOCKLEN];
unsigned long freeblks, no_of_blks;
long other;
long offset;
- long hdr_offset;
- hdr_offset = sizeof (global_header);
-
- lseek (filedes, hdr_offset + ROOT, SEEK_SET);
- read (filedes, nblock, BLOCKLEN);
+ gbl_read_block (g, ROOT, nblock);
freeblks = UNSIGN (nblock[FREE]) * 65536 + UNSIGN (nblock[FREE + 1]) * 256 + UNSIGN (nblock[FREE + 2]);
no_of_blks = UNSIGN (nblock[NRBLK]) * 65536 + UNSIGN (nblock[NRBLK + 1]) * 256 + UNSIGN (nblock[NRBLK + 2]);
if (freeblks) {
-
- lseek (filedes, hdr_offset + ((long) (freeblks) * BLOCKLEN), SEEK_SET);
- read (filedes, nblock, BLOCKLEN);
+
+ gbl_read_block (g, freeblks, nblock);
offset = UNSIGN (nblock[OFFS]) * 256 + UNSIGN (nblock[OFFS + 1]);
@@ -3469,28 +3493,24 @@ static void getnewblk (int filedes, unsi
/* update RL-block, if any */
if (other) {
- lseek (filedes, hdr_offset + ((long) (other) * BLOCKLEN), SEEK_SET);
- read (filedes, nblock, BLOCKLEN);
+ gbl_read_block (g, other, nblock);
nblock[LLPTR] = 0;
nblock[LLPTR + 1] = 0;
nblock[LLPTR + 2] = 0;
-
- lseek (filedes, hdr_offset + ((long) (other) * BLOCKLEN), SEEK_SET);
- write (filedes, nblock, BLOCKLEN);
+
+ gbl_write_block (g, other, nblock);
}
/* update ROOT block */
- lseek (filedes, hdr_offset + ROOT, SEEK_SET);
- read (filedes, nblock, BLOCKLEN);
+ gbl_read_block (g, ROOT, nblock);
nblock[FREE] = other / 65536;
nblock[FREE + 1] = other % 65536 / 256;
nblock[FREE + 2] = other % 256;
-
- lseek (filedes, hdr_offset + ROOT, SEEK_SET);
- write (filedes, nblock, BLOCKLEN);
+
+ gbl_write_block (g, ROOT, nblock);
return;
@@ -3502,9 +3522,8 @@ static void getnewblk (int filedes, unsi
nblock[offset + 1] = 0;
nblock[OFFS] = offset / 256;
nblock[OFFS + 1] = offset % 256;
-
- lseek (filedes, hdr_offset + ((long) (freeblks) * BLOCKLEN), SEEK_SET);
- write (filedes, nblock, BLOCKLEN);
+
+ gbl_write_block (g, freeblks, nblock);
return;
@@ -3515,24 +3534,12 @@ static void getnewblk (int filedes, unsi
nblock[NRBLK] = no_of_blks / 65536;
nblock[NRBLK + 1] = no_of_blks % 65536 / 256;
nblock[NRBLK + 2] = no_of_blks % 256;
-
- lseek (filedes, hdr_offset + ROOT, SEEK_SET);
- write (filedes, nblock, BLOCKLEN);
+
+ gbl_write_block (g, ROOT, nblock);
*blknbr = no_of_blks;
-
- for (;;) {
-
- errno = 0;
-
- lseek (filedes, hdr_offset + ((long) (no_of_blks) * BLOCKLEN), SEEK_SET);
- write (filedes, nblock, BLOCKLEN);
-
- if (errno == 0) break;
-
- panic ();
- }
+ gbl_write_block (g, no_of_blks, nblock);
return;
@@ -3661,7 +3668,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;
@@ -3742,23 +3748,29 @@ void gbl_dump_stat(void)
int hit_pct;
unsigned long access_total;
unsigned long hit_total;
-
+ unsigned long mem_reads_total;
+
+ mem_reads_total = 0;
printf ("\r\nFreeM Global Statistics [PID %d]\r\n\r\n", pid);
- 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", "======", "=====", "==========", "===", "========", "====");
+ printf ("%-20s%-10s%-10s%-10s%-10s%-12s%-20s%-10s%s\r\n", "GLOBAL", "USECT", "READS", "WRITES", "MEMREADS", "SLOW PTHCT", "AGE", "LAST BLK", "FILE");
+ printf ("%-20s%-10s%-10s%-10s%-10s%-12s%-20s%-10s%s\r\n", "======", "=====", "=====", "======", "========", "==========", "===", "========", "====");
access_total = 0;
ct = 0;
for (g = global_handles_head; g != NULL; g = g->next) {
- printf ("%-20s%-10ld%-12ld%-20ld%-10ld%s\r\n",
+ printf ("%-20s%-10ld%-10ld%-10ld%-10ld%-12ld%-20ld%-10ld%s\r\n",
g->global_name,
g->use_count,
+ g->read_ops,
+ g->write_ops,
+ g->memory_reads,
g->cache_misses,
g->age,
g->last_block,
g->global_path);
ct++;
+ mem_reads_total += g->memory_reads;
access_total += g->use_count;
}
if (!ct) printf ("\r\n");
@@ -3769,5 +3781,7 @@ void gbl_dump_stat(void)
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);
+ printf ("Fast path misses: %ld\t(%d%%)\r\n", gbl_cache_misses, miss_pct);
+ printf ("Disk reads avoided: %ld\r\n", mem_reads_total);
+
}