Changeset: d83cad648c36 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/d83cad648c36
Modified Files:
        gdk/gdk_bbp.c
Branch: default
Log Message:

Make the hash table for BAT names a static size.
This means no reallocs and complete reinitializations anymore whenever
the total number of bats in the system increases.


diffs (204 lines):

diff --git a/gdk/gdk_bbp.c b/gdk/gdk_bbp.c
--- a/gdk/gdk_bbp.c
+++ b/gdk/gdk_bbp.c
@@ -113,8 +113,8 @@ struct BBPfarm_t BBPfarms[MAXFARMS];
  * 'next' field in the BBPrec records.
  */
 static MT_Lock BBPnameLock = MT_LOCK_INITIALIZER(BBPnameLock);
-static bat *BBP_hash = NULL;           /* BBP logical name hash buckets */
-static bat BBP_mask = 0;               /* number of buckets = & mask */
+#define BBP_mask       1023            /* number of buckets = & mask */
+static bat BBP_hash[BBP_mask+1];       /* BBP logical name hash buckets */
 static MT_Lock GDKcacheLock = MT_LOCK_INITIALIZER(GDKcacheLock);
 static bat BBP_free;
 
@@ -155,11 +155,10 @@ BBP_insert(bat i)
 static void
 BBP_delete(bat i)
 {
-       bat *h = BBP_hash;
        const char *s = BBP_logical(i);
        bat idx = (bat) (strHash(s) & BBP_mask);
 
-       for (h += idx; (i = *h) != 0; h = &BBP_next(i)) {
+       for (bat *h = &BBP_hash[idx]; (i = *h) != 0; h = &BBP_next(i)) {
                if (strcmp(BBP_logical(i), s) == 0) {
                        *h = BBP_next(i);
                        break;
@@ -335,32 +334,6 @@ BBPunlock(void)
        BBPtmunlock();
 }
 
-static gdk_return
-BBPinithash(bat size)
-{
-       for (BBP_mask = 1; (BBP_mask << 1) <= BBPlimit; BBP_mask <<= 1)
-               ;
-       BBP_hash = (bat *) GDKzalloc(BBP_mask * sizeof(bat));
-       if (BBP_hash == NULL) {
-               return GDK_FAIL;
-       }
-       BBP_mask--;
-
-       while (--size > 0) {
-               const char *s = BBP_logical(size);
-
-               if (s) {
-                       if (*s != '.' && !BBPtmpcheck(s)) {
-                               BBP_insert(size);
-                       }
-               } else {
-                       BBP_next(size) = BBP_free;
-                       BBP_free = size;
-               }
-       }
-       return GDK_SUCCEED;
-}
-
 int
 BBPselectfarm(role_t role, int type, enum heaptype hptype)
 {
@@ -389,7 +362,7 @@ BBPselectfarm(role_t role, int type, enu
 }
 
 static gdk_return
-BBPextend(bool buildhash, bat newsize)
+BBPextend(bat newsize)
 {
        if (newsize >= N_BBPINIT * BBPINIT) {
                GDKerror("trying to extend BAT pool beyond the "
@@ -413,13 +386,6 @@ BBPextend(bool buildhash, bat newsize)
                BBPlimit += BBPINIT;
        }
 
-       if (buildhash) {
-               GDKfree(BBP_hash);
-               BBP_hash = NULL;
-               BBP_free = 0;
-               if (BBPinithash(newsize) != GDK_SUCCEED)
-                       return GDK_FAIL;
-       }
        return GDK_SUCCEED;
 }
 
@@ -779,6 +745,7 @@ BBPreadEntries(FILE *fp, unsigned bbpver
 
        /* read the BBP.dir and insert the BATs into the BBP */
        return_options = true;
+       MT_lock_set(&BBPnameLock);
        for (;;) {
                BAT b;
                Heap h;
@@ -810,6 +777,7 @@ BBPreadEntries(FILE *fp, unsigned bbpver
                        *nhashbats = nhbats;
 #endif
                        return_options = false;
+                       MT_lock_unset(&BBPnameLock);
                        return GDK_SUCCEED;
                case 1:
                        /* successfully read an entry */
@@ -827,7 +795,7 @@ BBPreadEntries(FILE *fp, unsigned bbpver
 
                if (b.batCacheid >= (bat) ATOMIC_GET(&BBPsize)) {
                        if ((bat) ATOMIC_GET(&BBPsize) + 1 >= BBPlimit &&
-                           BBPextend(false, b.batCacheid + 1) != GDK_SUCCEED) {
+                           BBPextend(b.batCacheid + 1) != GDK_SUCCEED) {
                                GDKfree(options);
                                goto bailout;
                        }
@@ -927,9 +895,12 @@ BBPreadEntries(FILE *fp, unsigned bbpver
                BBP_desc(b.batCacheid) = bn;
                BBP_pid(b.batCacheid) = 0;
                BBP_status_set(b.batCacheid, BBPEXISTING);      /* do we need 
other status bits? */
+               if (BBPnamecheck(BBP_logical(b.batCacheid)) == 0)
+                       BBP_insert(b.batCacheid);
        }
 
   bailout:
+       MT_lock_unset(&BBPnameLock);
        return_options = false;
 #ifdef GDKLIBRARY_HASHASH
        GDKfree(hbats);
@@ -1736,7 +1707,7 @@ BBPinit(void)
        }
 
        /* allocate BBP records */
-       if (BBPextend(false, bbpsize) != GDK_SUCCEED) {
+       if (BBPextend(bbpsize) != GDK_SUCCEED) {
                GDKdebug = dbg;
                return GDK_FAIL;
        }
@@ -1754,18 +1725,6 @@ BBPinit(void)
                fclose(fp);
        }
 
-       MT_lock_set(&BBPnameLock);
-       if (BBPinithash((bat) ATOMIC_GET(&BBPsize)) != GDK_SUCCEED) {
-               TRC_CRITICAL(GDK, "BBPinithash failed");
-               MT_lock_unset(&BBPnameLock);
-#ifdef GDKLIBRARY_HASHASH
-               GDKfree(hashbats);
-#endif
-               GDKdebug = dbg;
-               return GDK_FAIL;
-       }
-       MT_lock_unset(&BBPnameLock);
-
        /* will call BBPrecover if needed */
        if (!GDKinmemory(0)) {
                BBPtmlock();
@@ -1952,9 +1911,8 @@ BBPexit(void)
                        }
                }
        } while (skipped);
-       GDKfree(BBP_hash);
-       BBP_hash = NULL;
        /* these need to be NULL, otherwise no new ones get created */
+       memset(BBP_hash, 0, sizeof(BBP_hash));
        backup_files = 0;
        backup_dir = 0;
        backup_subdir = 0;
@@ -2434,15 +2392,15 @@ BBPgetsubdir(str s, bat i)
  * increases BBPlimit).
  *
  * Note that this is the only place in normal, multi-threaded operation
- * where BBPsize is assigned a value (never decreasing), that the
+ * where BBPsize is assigned a value (never decreasing) and that the
  * assignment happens after any necessary memory was allocated and
- * initialized, and that this happens when the BBPnameLock is held. */
+ * initialized. */
 static gdk_return
 maybeextend(void)
 {
        bat size = (bat) ATOMIC_GET(&BBPsize);
        if (size >= BBPlimit &&
-           BBPextend(true, size + 1) != GDK_SUCCEED) {
+           BBPextend(size + 1) != GDK_SUCCEED) {
                /* nothing available */
                return GDK_FAIL;
        } else {
@@ -2471,13 +2429,11 @@ BBPinsert(BAT *bn)
        if (BBP_free <= 0) {
                /* we need to extend the BBP */
                gdk_return r = GDK_SUCCEED;
-               MT_lock_set(&BBPnameLock);
                /* check again in case some other thread extended
                 * while we were waiting */
                if (BBP_free <= 0) {
                        r = maybeextend();
                }
-               MT_lock_unset(&BBPnameLock);
                if (r != GDK_SUCCEED) {
                        if (lock) {
                                MT_lock_unset(&GDKcacheLock);
@@ -4365,9 +4321,7 @@ gdk_bbp_reset(void)
        for (i = 0; i < MAXFARMS; i++)
                GDKfree((void *) BBPfarms[i].dirname); /* loose "const" */
        memset(BBPfarms, 0, sizeof(BBPfarms));
-       GDKfree(BBP_hash);
-       BBP_hash = NULL;
-       BBP_mask = 0;
+       memset(BBP_hash, 0, sizeof(BBP_hash));
 
        locked_by = 0;
        BBPunloadCnt = 0;
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to