Diff for /freem/src/global_bltin.c between versions 1.11 and 1.13

version 1.11, 2025/04/08 21:41:13 version 1.13, 2025/04/09 14:34:30
Line 24 Line 24
  *   along with FreeM.  If not, see <https://www.gnu.org/licenses/>.   *   along with FreeM.  If not, see <https://www.gnu.org/licenses/>.
  *   *
  *   $Log$   *   $Log$
    *   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   *   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   *   Make insert, update, and splitp global handler functions take a ptr to a global_handle instead of a file descriptor
  *   *
Line 66 Line 72
 #include "global_bltin.h"  #include "global_bltin.h"
   
 global_handle *global_handles_head;  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 (short filedes, unsigned long blknbr);
 static void splitp (global_handle *g, char *block, long *addr, long *offs, unsigned long *blknbr);  static void splitp (global_handle *g, char *block, long *addr, long *offs, unsigned long *blknbr);
Line 209  nextpath: Line 217  nextpath:
     return i;      return i;
 } /* gbl_path() */  } /* 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)  int gbl_lock(global_handle *g, int type)
 {  {
     if (g->locked == TRUE || lonelyflag == TRUE) {      if (g->locked == TRUE || lonelyflag == TRUE) {
Line 294  int gbl_write_header(global_handle *g, g Line 315  int gbl_write_header(global_handle *g, g
     if (g->opened == FALSE) {      if (g->opened == FALSE) {
         return FALSE;          return FALSE;
     }      }
       
     gbl_lock (g, 1);      gbl_lock (g, 1);
     old_position = lseek (g->fd, 0, SEEK_CUR);      old_position = lseek (g->fd, 0, SEEK_CUR);
     lseek (g->fd, 0, SEEK_SET);      lseek (g->fd, 0, SEEK_SET);
Line 306  int gbl_write_header(global_handle *g, g Line 327  int gbl_write_header(global_handle *g, g
   
     lseek (g->fd, old_position, SEEK_SET);      lseek (g->fd, old_position, SEEK_SET);
     gbl_unlock (g);      gbl_unlock (g);
   
       gbl_read_header (g, &g->header);
           
     return TRUE;          return TRUE;    
 } /* gbl_write_header() */  } /* gbl_write_header() */
Line 374  int gbl_create(global_handle *g) Line 397  int gbl_create(global_handle *g)
     g->age = time (0L);      g->age = time (0L);
     g->last_block = 0;      g->last_block = 0;
     g->use_count = 1;      g->use_count = 1;
       g->fast_path = 0;
       
     gbl_write_initial_header (g);      gbl_write_initial_header (g);
           
     return OK;      return OK;
Line 383  int gbl_create(global_handle *g) Line 407  int gbl_create(global_handle *g)
 short gbl_open(global_handle *g, short action)  short gbl_open(global_handle *g, short action)
 {  {
     int result;      int result;
     global_header h;  
           
     if (g->opened == FALSE) {      if (g->opened == FALSE) {
           gbl_cache_miss (g);
         while (1) {          while (1) {
             errno = 0;              errno = 0;
             g->fd = open (g->global_path, 2);              g->fd = open (g->global_path, 2);
Line 414  short gbl_open(global_handle *g, short a Line 438  short gbl_open(global_handle *g, short a
         }          }
         else {          else {
             g->opened = TRUE;              g->opened = TRUE;
             result = gbl_read_header (g, &h);              result = gbl_read_header (g, &g->header);
               
             if (result == GBL_HDR_OK) {              if (result == GBL_HDR_OK) {
                 g->opened = TRUE;                  g->opened = TRUE;
             }              }
             else {              else {                
                 gbl_close (g);                                  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;                  return FALSE;
             }              }
         }          }
Line 477  global_handle *gbl_handle(char *key) Line 517  global_handle *gbl_handle(char *key)
     g->opened = FALSE;      g->opened = FALSE;
     g->fd = 0;      g->fd = 0;
     g->fast_path = -1;      g->fast_path = -1;
       g->cache_misses = 0;
       g->cache_hits = 0;
           
     strcpy (g->global_name, global_name);          strcpy (g->global_name, global_name);    
     gbl_path (key, g->global_path);      gbl_path (key, g->global_path);
Line 1028  lock: Line 1070  lock:
   
 tfast0:  tfast0:
         gbl_lock (g, 3);          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 */          blknbr = g->last_block = ROOT;          /* start with ROOT block */
                   
Line 1059  tfast2: Line 1102  tfast2:
                 j = UNSIGN (block[i++]);        /* length of key - offset */                  j = UNSIGN (block[i++]);        /* length of key - offset */
                 k = UNSIGN (block[i++]);        /* offset into previous entry */                  k = UNSIGN (block[i++]);        /* offset into previous entry */
   
 #ifdef VERSNEW  
   
                 stcpy0 (&key0[k], &block[i], j);  
                 i += j;  
                 j += k;  
   
 #else  
   
                 j += k;                  j += k;
                           
                 while (k < j) key1[k++] = block[i++];           /* get key */                  while (k < j) key1[k++] = block[i++];           /* get key */
   
 #endif /* VERSNEW */  
   
                 if (j != ch) {  /* keys have different length */                  if (j != ch) {  /* keys have different length */
                                           
                     i += UNSIGN (block[i]);                      i += UNSIGN (block[i]);
Line 1106  tfast2: Line 1139  tfast2:
   
             /* fast access failed. try normal path */              /* fast access failed. try normal path */
             if (tryfast) {              if (tryfast) {
                 tryfast = FALSE;                  gbl_cache_miss (g);
                 goto tfast0;                  goto tfast0;
             }              }
   
Line 1117  tfast2: Line 1150  tfast2:
         }           } 
         else {          else {
                   
             if (tryfast) {              if (g->fast_path > 0) {
                 tryfast = FALSE;                  gbl_cache_miss (g);
                 goto tfast0;                  goto tfast0;
             }              }
   
Line 1148  tfast2: Line 1181  tfast2:
   
         addr += PLEN;           /* skip data */          addr += PLEN;           /* skip data */
         g->last_block = blknbr;          g->last_block = blknbr;
           g->fast_path = 1;
                   
         if (merr () == INRPT) goto quit;          if (merr () == INRPT) goto quit;
   
Line 1226  k_again:    /* entry point for repeated Line 1260  k_again:    /* entry point for repeated
   
         addr += PLEN;                   /* skip data */          addr += PLEN;                   /* skip data */
         g->last_block = blknbr;          g->last_block = blknbr;
           g->fast_path = 1;
     }      }
   
     traceadr[trx] = addr;      traceadr[trx] = addr;
Line 3709  static void panic (void) Line 3744  static void panic (void)
 void gbl_dump_stat(void)  void gbl_dump_stat(void)
 {  {
     global_handle *g;      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%-12s%-20s%-10s%s\r\n", "GLOBAL", "USECT", "SLOW PTHCT", "AGE", "LAST BLK", "FILE");
     printf ("%-20s%-10s%-20s%-10s%s\r\n", "======", "=====", "===", "========", "====");      printf ("%-20s%-10s%-12s%-20s%-10s%s\r\n", "======", "=====", "==========", "===", "========", "====");
       
       access_total = 0;
       ct = 0;
     for (g = global_handles_head; g != NULL; g = g->next) {      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 ("<no globals opened in this pid>\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);    
 }  }

Removed from v.1.11  
changed lines
  Added in v.1.13


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>