Changeset: 7def90fc96dc for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/7def90fc96dc
Modified Files:
        gdk/gdk_logger.c
Branch: Jul2021
Log Message:

merged


diffs (truncated from 348 to 300 lines):

diff --git a/gdk/gdk.h b/gdk/gdk.h
--- a/gdk/gdk.h
+++ b/gdk/gdk.h
@@ -1528,7 +1528,7 @@ typedef struct {
        int refs;               /* in-memory references on which the loaded 
status of a BAT relies */
        int lrefs;              /* logical references on which the existence of 
a BAT relies */
        ATOMIC_TYPE status;     /* status mask used for spin locking */
-       /* MT_Id pid;           non-zero thread-id if this BAT is private */
+       MT_Id pid;              /* creator of this bat while "private" */
 } BBPrec;
 
 gdk_export bat BBPlimit;
@@ -1919,6 +1919,7 @@ BBPcheck(bat x)
                if (x < 0 || x >= getBBPsize() || BBP_logical(x) == NULL) {
                        TRC_DEBUG(CHECK_, "range error %d\n", (int) x);
                } else {
+                       assert(BBP_pid(x) == 0 || BBP_pid(x) == MT_getpid());
                        return x;
                }
        }
diff --git a/gdk/gdk_bbp.c b/gdk/gdk_bbp.c
--- a/gdk/gdk_bbp.c
+++ b/gdk/gdk_bbp.c
@@ -365,8 +365,10 @@ BBPextend(int idx, bool buildhash)
                        GDKerror("failed to extend BAT pool\n");
                        return GDK_FAIL;
                }
-               for (BUN i = 0; i < BBPINIT; i++)
+               for (BUN i = 0; i < BBPINIT; i++) {
                        ATOMIC_INIT(&BBP[limit][i].status, 0);
+                       BBP[limit][i].pid = ~(MT_Id)0;
+               }
                BBPlimit += BBPINIT;
        }
 
@@ -721,6 +723,7 @@ BBPreadEntries(FILE *fp, unsigned bbpver
                BBP_refs(bid) = 0;
                BBP_lrefs(bid) = 1;     /* any BAT we encounter here is 
persistent, so has a logical reference */
                BBP_desc(bid) = bn;
+               BBP_pid(bid) = 0;
                BBP_status_set(bid, BBPEXISTING);       /* do we need other 
status bits? */
        }
        return GDK_SUCCEED;
@@ -1314,6 +1317,7 @@ BBPexit(void)
                                                BATfree(b);
                                        }
                                }
+                               BBP_pid(i) = 0;
                                BBPuncacheit(i, true);
                                if (BBP_logical(i) != BBP_bak(i))
                                        GDKfree(BBP_logical(i));
@@ -1921,6 +1925,7 @@ BBPinsert(BAT *bn)
        BBP_desc(i) = NULL;
        BBP_refs(i) = 1;        /* new bats have 1 pin */
        BBP_lrefs(i) = 0;       /* ie. no logical refs */
+       BBP_pid(i) = MT_getpid();
 
 #ifdef HAVE_HGE
        if (bn->ttype == TYPE_hge)
@@ -2051,6 +2056,7 @@ bbpclear(bat i, int idx, bool lock)
        BBP_logical(i) = NULL;
        BBP_next(i) = BBP_free(idx);
        BBP_free(idx) = i;
+       BBP_pid(i) = ~(MT_Id)0; /* not zero, not a valid thread id */
        if (lock)
                MT_lock_unset(&GDKcacheLock(idx));
 }
@@ -2213,7 +2219,7 @@ static inline int
 incref(bat i, bool logical, bool lock)
 {
        int refs;
-       bat tp, tvp;
+       bat tp = i, tvp = i;
        BAT *b, *pb = NULL, *pvb = NULL;
        bool load = false;
 
@@ -2227,13 +2233,17 @@ incref(bat i, bool logical, bool lock)
         * reference, getting the parent BAT descriptor is
         * superfluous, but not too expensive, so we do it anyway. */
        if (!logical && (b = BBP_desc(i)) != NULL) {
-               if (b->theap && b->theap->parentid != i) {
-                       pb = BATdescriptor(b->theap->parentid);
+               MT_lock_set(&b->theaplock);
+               tp = b->theap ? b->theap->parentid : i;
+               tvp = b->tvheap ? b->tvheap->parentid : i;
+               MT_lock_unset(&b->theaplock);
+               if (tp != i) {
+                       pb = BATdescriptor(tp);
                        if (pb == NULL)
                                return 0;
                }
-               if (b->tvheap && b->tvheap->parentid != i) {
-                       pvb = BATdescriptor(b->tvheap->parentid);
+               if (tvp != i) {
+                       pvb = BATdescriptor(tvp);
                        if (pvb == NULL) {
                                if (pb)
                                        BBPunfix(pb->batCacheid);
@@ -2266,15 +2276,13 @@ incref(bat i, bool logical, bool lock)
               BBP_status(i) & (BBPDELETED | BBPSWAPPED));
        if (logical) {
                /* parent BATs are not relevant for logical refs */
-               tp = tvp = 0;
                refs = ++BBP_lrefs(i);
+               BBP_pid(i) = 0;
        } else {
-               tp = b->theap == NULL || b->theap->parentid == i ? 0 : 
b->theap->parentid;
                assert(tp >= 0);
-               tvp = b->tvheap == 0 || b->tvheap->parentid == i ? 0 : 
b->tvheap->parentid;
                refs = ++BBP_refs(i);
                unsigned flag = BBPHOT;
-               if (refs == 1 && (tp || tvp)) {
+               if (refs == 1 && (tp != i || tvp != i)) {
                        /* If this is a view, we must load the parent
                         * BATs, but we must do that outside of the
                         * lock.  Set the BBPLOADING flag so that
@@ -2291,13 +2299,18 @@ incref(bat i, bool logical, bool lock)
        if (load) {
                /* load the parent BATs */
                assert(!logical);
-               if (tp) {
+               if (tp != i) {
                        assert(pb != NULL);
+                       /* load being set implies there is no other
+                        * thread that has access to this bat, but the
+                        * parent is a different matter */
+                       MT_lock_set(&pb->theaplock);
                        if (b->theap != pb->theap) {
                                HEAPincref(pb->theap);
                                HEAPdecref(b->theap, false);
                                b->theap = pb->theap;
                        }
+                       MT_lock_unset(&pb->theaplock);
                }
                /* done loading, release descriptor */
                BBP_status_off(i, BBPLOADING);
@@ -2351,9 +2364,15 @@ decref(bat i, bool logical, bool release
        int refs = 0, lrefs;
        bool swap = false;
        bat tp = 0, tvp = 0;
+       int farmid = 0;
        BAT *b;
 
+       if (is_bat_nil(i))
+               return -1;
        assert(i > 0);
+       if (BBPcheck(i) == 0)
+               return -1;
+
        if (lock)
                MT_lock_set(&GDKswapLock(i));
        if (releaseShare) {
@@ -2402,16 +2421,22 @@ decref(bat i, bool logical, bool release
                        }
                }
        }
-       if (b && b->batCount > b->batInserted && !isVIEW(b)) {
-               /* if batCount is larger than batInserted and the dirty
-                * bits are off, it may be that a (sub)commit happened
-                * in parallel to an update; we must undo the turning
-                * off of the dirty bits */
-               b->batDirtydesc = true;
-               if (b->theap)
-                       b->theap->dirty = true;
-               if (b->tvheap)
-                       b->tvheap->dirty = true;
+       if (b) {
+               MT_lock_set(&b->theaplock);
+               if (b->batCount > b->batInserted && !isVIEW(b)) {
+                       /* if batCount is larger than batInserted and
+                        * the dirty bits are off, it may be that a
+                        * (sub)commit happened in parallel to an
+                        * update; we must undo the turning off of the
+                        * dirty bits */
+                       b->batDirtydesc = true;
+                       if (b->theap)
+                               b->theap->dirty = true;
+                       if (b->tvheap)
+                               b->tvheap->dirty = true;
+               }
+               farmid = b->theap->farmid;
+               MT_lock_unset(&b->theaplock);
        }
 
        /* we destroy transients asap and unload persistent bats only
@@ -2422,7 +2447,7 @@ decref(bat i, bool logical, bool release
              BATdirty(b) ||
              (BBP_status(i) & (BBPHOT | BBPSYNCING)) ||
              !(BBP_status(i) & BBPPERSISTENT) ||
-             GDKinmemory(b->theap->farmid)))) {
+             GDKinmemory(farmid)))) {
                /* bat cannot be swapped out */
        } else if (b ? b->batSharecnt == 0 : (BBP_status(i) & BBPTMP)) {
                /* bat will be unloaded now. set the UNLOADING bit
@@ -2462,18 +2487,12 @@ decref(bat i, bool logical, bool release
 int
 BBPunfix(bat i)
 {
-       if (BBPcheck(i) == 0) {
-               return -1;
-       }
        return decref(i, false, false, true, "BBPunfix");
 }
 
 int
 BBPrelease(bat i)
 {
-       if (BBPcheck(i) == 0) {
-               return -1;
-       }
        return decref(i, true, false, true, "BBPrelease");
 }
 
diff --git a/gdk/gdk_logger.c b/gdk/gdk_logger.c
--- a/gdk/gdk_logger.c
+++ b/gdk/gdk_logger.c
@@ -211,6 +211,7 @@ logbat_new(int tt, BUN size, role_t role
        BAT *nb = COLnew(0, tt, size, role);
 
        if (nb) {
+               BBP_pid(nb->batCacheid) = 0;
                if (role == PERSISTENT)
                        BATmode(nb, false);
        } else {
diff --git a/gdk/gdk_logger_old.c b/gdk/gdk_logger_old.c
--- a/gdk/gdk_logger_old.c
+++ b/gdk/gdk_logger_old.c
@@ -1489,10 +1489,6 @@ logger_load(const char *fn, char filenam
                GDKerror("Logger_new: failed to create freed bat");
                goto error;
        }
-       strconcat_len(bak, sizeof(bak), fn, "_freed", NULL);
-       if (BBPrename(lg->freed->batCacheid, bak) < 0) {
-               goto error;
-       }
        snapshots_bid = old_logger_find_bat(lg, "snapshots_bid", 0, 0);
        if (snapshots_bid == 0) {
                lg->snapshots_bid = logbat_new(TYPE_int, 1, TRANSIENT);
diff --git a/gdk/gdk_utils.c b/gdk/gdk_utils.c
--- a/gdk/gdk_utils.c
+++ b/gdk/gdk_utils.c
@@ -1055,6 +1055,8 @@ GDKinit(opt *set, int setlen, bool embed
                TRC_CRITICAL(GDK, "BBPrename of environment BATs failed");
                return GDK_FAIL;
        }
+       BBP_pid(GDKkey->batCacheid) = 0;
+       BBP_pid(GDKval->batCacheid) = 0;
 
        /* store options into environment BATs */
        for (i = 0; i < nlen; i++)
diff --git a/monetdb5/mal/mal_resource.c b/monetdb5/mal/mal_resource.c
--- a/monetdb5/mal/mal_resource.c
+++ b/monetdb5/mal/mal_resource.c
@@ -67,7 +67,9 @@ getMemoryClaim(MalBlkPtr mb, MalStkPtr s
                b = BATdescriptor( stk->stk[getArg(pci, i)].val.bval);
                if (b == NULL)
                        return 0;
+               MT_lock_set(&b->theaplock);
                if (flag && isVIEW(b)) {
+                       MT_lock_unset(&b->theaplock);
                        BBPunfix(b->batCacheid);
                        return 0;
                }
@@ -75,6 +77,7 @@ getMemoryClaim(MalBlkPtr mb, MalStkPtr s
                /* calculate the basic scan size */
                total += BATcount(b) << b->tshift;
                total += heapinfo(b->tvheap, b->batCacheid);
+               MT_lock_unset(&b->theaplock);
 
                /* indices should help, find their maximum footprint */
                MT_rwlock_rdlock(&b->thashlock);
diff --git a/monetdb5/modules/atoms/str.c b/monetdb5/modules/atoms/str.c
--- a/monetdb5/modules/atoms/str.c
+++ b/monetdb5/modules/atoms/str.c
@@ -2937,6 +2937,10 @@ STRprelude(void *ret)
                        BBPrename(UTF8_toLowerTo->batCacheid, 
"monet_unicode_lower_to") != 0) {
                        goto bailout;
                }
+               BBP_pid(UTF8_toUpperFrom->batCacheid) = 0;
+               BBP_pid(UTF8_toUpperTo->batCacheid) = 0;
+               BBP_pid(UTF8_toLowerFrom->batCacheid) = 0;
+               BBP_pid(UTF8_toLowerTo->batCacheid) = 0;
        }
        return MAL_SUCCEED;
 
diff --git a/sql/storage/bat/bat_utils.c b/sql/storage/bat/bat_utils.c
--- a/sql/storage/bat/bat_utils.c
+++ b/sql/storage/bat/bat_utils.c
@@ -19,7 +19,10 @@ bat_destroy(BAT *b)
 BAT *
 bat_new(int tt, BUN size, role_t role)
 {
-       return COLnew(0, tt, size, role);
+       BAT *bn = COLnew(0, tt, size, role);
+       if (bn)
+               BBP_pid(bn->batCacheid) = 0;
+       return bn;
 }
 
 void
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to