Changeset: 4db9e30b90ff for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/4db9e30b90ff
Modified Files:
        gdk/gdk_aggr.c
        gdk/gdk_align.c
        gdk/gdk_imprints.c
        gdk/gdk_private.h
        gdk/gdk_select.c
        gdk/gdk_tracer.c
        sql/test/BugTracker-2016/Tests/storagemodel.test
Branch: Jul2021
Log Message:

Reenable use of imprints on parent bats.  Protect them using reference counting.


diffs (truncated from 696 to 300 lines):

diff --git a/gdk/gdk_aggr.c b/gdk/gdk_aggr.c
--- a/gdk/gdk_aggr.c
+++ b/gdk/gdk_aggr.c
@@ -3693,27 +3693,49 @@ BATmin_skipnil(BAT *b, void *aggr, bit s
                                pos = ords[r];
                        }
                        HEAPdecref(oidxh, false);
-               } else if ((VIEWtparent(b) == 0 ||
-                           (/* DISABLES CODE */ (0) &&
-                            BATcount(b) == 
BATcount(BBP_cache(VIEWtparent(b))))) &&
-                          BATcheckimprints(b)) {
-                       Imprints *imprints = VIEWtparent(b) ? 
BBP_cache(VIEWtparent(b))->timprints : b->timprints;
-                       int i;
-
-                       MT_thread_setalgorithm(VIEWtparent(b) ? "using parent 
imprints" : "using imprints");
-                       pos = oid_nil;
-                       /* find first non-empty bin */
-                       for (i = 0; i < imprints->bits; i++) {
-                               if (imprints->stats[i + 128]) {
-                                       pos = imprints->stats[i] + b->hseqbase;
-                                       break;
+               } else {
+                       Imprints *imprints = NULL;
+                       if ((VIEWtparent(b) == 0 ||
+                            BATcount(b) == 
BATcount(BBP_cache(VIEWtparent(b)))) &&
+                           BATcheckimprints(b)) {
+                               if (VIEWtparent(b)) {
+                                       BAT *pb = BBP_cache(VIEWtparent(b));
+                                       MT_lock_set(&pb->batIdxLock);
+                                       imprints = pb->timprints;
+                                       if (imprints != NULL)
+                                               IMPSincref(imprints);
+                                       else
+                                               imprints = NULL;
+                                       MT_lock_unset(&pb->batIdxLock);
+                               } else {
+                                       MT_lock_set(&b->batIdxLock);
+                                       imprints = b->timprints;
+                                       if (imprints != NULL)
+                                               IMPSincref(imprints);
+                                       else
+                                               imprints = NULL;
+                                       MT_lock_unset(&b->batIdxLock);
                                }
                        }
-               } else {
-                       struct canditer ci;
-                       BUN ncand = canditer_init(&ci, b, NULL);
-                       (void) do_groupmin(&pos, b, NULL, 1, 0, 0, &ci, ncand,
-                                          skipnil, false);
+                       if (imprints) {
+                               int i;
+
+                               MT_thread_setalgorithm(VIEWtparent(b) ? "using 
parent imprints" : "using imprints");
+                               pos = oid_nil;
+                               /* find first non-empty bin */
+                               for (i = 0; i < imprints->bits; i++) {
+                                       if (imprints->stats[i + 128]) {
+                                               pos = imprints->stats[i] + 
b->hseqbase;
+                                               break;
+                                       }
+                               }
+                               IMPSdecref(imprints, false);
+                       } else {
+                               struct canditer ci;
+                               BUN ncand = canditer_init(&ci, b, NULL);
+                               (void) do_groupmin(&pos, b, NULL, 1, 0, 0, &ci, 
ncand,
+                                                  skipnil, false);
+                       }
                }
                if (is_oid_nil(pos)) {
                        res = ATOMnilptr(b->ttype);
@@ -3826,27 +3848,49 @@ BATmax_skipnil(BAT *b, void *aggr, bit s
                                bat_iterator_end(&bi);
                        }
                        HEAPdecref(oidxh, false);
-               } else if ((VIEWtparent(b) == 0 ||
-                           (/* DISABLES CODE */ (0) &&
-                            BATcount(b) == 
BATcount(BBP_cache(VIEWtparent(b))))) &&
-                          BATcheckimprints(b)) {
-                       Imprints *imprints = VIEWtparent(b) ? 
BBP_cache(VIEWtparent(b))->timprints : b->timprints;
-                       int i;
-
-                       MT_thread_setalgorithm(VIEWtparent(b) ? "using parent 
imprints" : "using imprints");
-                       pos = oid_nil;
-                       /* find last non-empty bin */
-                       for (i = imprints->bits - 1; i >= 0; i--) {
-                               if (imprints->stats[i + 128]) {
-                                       pos = imprints->stats[i + 64] + 
b->hseqbase;
-                                       break;
+               } else {
+                       Imprints *imprints = NULL;
+                       if ((VIEWtparent(b) == 0 ||
+                            BATcount(b) == 
BATcount(BBP_cache(VIEWtparent(b)))) &&
+                           BATcheckimprints(b)) {
+                               if (VIEWtparent(b)) {
+                                       BAT *pb = BBP_cache(VIEWtparent(b));
+                                       MT_lock_set(&pb->batIdxLock);
+                                       imprints = pb->timprints;
+                                       if (imprints != NULL)
+                                               IMPSincref(imprints);
+                                       else
+                                               imprints = NULL;
+                                       MT_lock_unset(&pb->batIdxLock);
+                               } else {
+                                       MT_lock_set(&b->batIdxLock);
+                                       imprints = b->timprints;
+                                       if (imprints != NULL)
+                                               IMPSincref(imprints);
+                                       else
+                                               imprints = NULL;
+                                       MT_lock_unset(&b->batIdxLock);
                                }
                        }
-               } else {
-                       struct canditer ci;
-                       BUN ncand = canditer_init(&ci, b, NULL);
-                       (void) do_groupmax(&pos, b, NULL, 1, 0, 0, &ci, ncand,
-                                          skipnil, false);
+                       if (imprints) {
+                               int i;
+
+                               MT_thread_setalgorithm(VIEWtparent(b) ? "using 
parent imprints" : "using imprints");
+                               pos = oid_nil;
+                               /* find last non-empty bin */
+                               for (i = imprints->bits - 1; i >= 0; i--) {
+                                       if (imprints->stats[i + 128]) {
+                                               pos = imprints->stats[i + 64] + 
b->hseqbase;
+                                               break;
+                                       }
+                               }
+                               IMPSdecref(imprints, false);
+                       } else {
+                               struct canditer ci;
+                               BUN ncand = canditer_init(&ci, b, NULL);
+                               (void) do_groupmax(&pos, b, NULL, 1, 0, 0, &ci, 
ncand,
+                                                  skipnil, false);
+                       }
                }
                if (is_oid_nil(pos)) {
                        res = ATOMnilptr(b->ttype);
diff --git a/gdk/gdk_align.c b/gdk/gdk_align.c
--- a/gdk/gdk_align.c
+++ b/gdk/gdk_align.c
@@ -302,15 +302,22 @@ VIEWunlink(BAT *b)
                        HEAPdecref(b->tvheap, false);
                        b->tvheap = NULL;
                }
-               MT_lock_unset(&b->theaplock);
 
                /* unlink properties shared with parent */
                if (tpb && b->tprops && b->tprops == tpb->tprops)
                        b->tprops = NULL;
 
+               MT_lock_unset(&b->theaplock);
+
+               MT_lock_set(&b->batIdxLock);
                /* unlink imprints shared with parent */
-               if (tpb && b->timprints && b->timprints == tpb->timprints)
+               if (b->timprints &&
+                   b->timprints != (Imprints *) 1 &&
+                   b->timprints->imprints.parentid != b->batCacheid) {
+                       IMPSdecref(b->timprints, false);
                        b->timprints = NULL;
+               }
+               MT_lock_unset(&b->batIdxLock);
        }
 }
 
diff --git a/gdk/gdk_imprints.c b/gdk/gdk_imprints.c
--- a/gdk/gdk_imprints.c
+++ b/gdk/gdk_imprints.c
@@ -301,7 +301,7 @@ BATcheckimprints(BAT *b)
        bool ret;
        BATiter bi = bat_iterator(b);
 
-       if (/* DISABLES CODE */ (0) && VIEWtparent(b)) {
+       if (VIEWtparent(b)) {
                assert(b->timprints == NULL);
                b = BBP_cache(VIEWtparent(b));
        }
@@ -353,6 +353,7 @@ BATcheckimprints(BAT *b)
                                                imprints->dict = (void *) 
((uintptr_t) ((char *) imprints->imps + pages * (imprints->bits / 8) + 
sizeof(uint64_t)) & ~(sizeof(uint64_t) - 1));
                                                close(fd);
                                                imprints->imprints.parentid = 
b->batCacheid;
+                                               
ATOMIC_INIT(&imprints->imprints.refs, 1);
                                                b->timprints = imprints;
                                                TRC_DEBUG(ACCELERATOR, 
"BATcheckimprints(" ALGOBATFMT "): reusing persisted imprints\n", 
ALGOBATPAR(b));
                                                MT_lock_unset(&b->batIdxLock);
@@ -468,7 +469,7 @@ BATimprints(BAT *b)
        if (BATcheckimprints(b))
                return GDK_SUCCEED;
 
-       if (/* DISABLES CODE */ (0) && VIEWtparent(b)) {
+       if (VIEWtparent(b)) {
                /* views always keep null pointer and need to obtain
                 * the latest imprint from the parent at query time */
                s2 = b;         /* remember for ACCELDEBUG print */
@@ -480,7 +481,6 @@ BATimprints(BAT *b)
        bi = bat_iterator(b);
        MT_lock_set(&b->batIdxLock);
 
-
        if (b->timprints == NULL) {
                BUN cnt;
                const char *nme = GDKinmemory(bi.h->farmid) ? ":memory:" : 
BBP_physical(b->batCacheid);
@@ -511,7 +511,7 @@ BATimprints(BAT *b)
                              nme, ".timprints", NULL);
                pages = (((size_t) bi.count * bi.width) + IMPS_PAGE - 1) / 
IMPS_PAGE;
                imprints->imprints.farmid = BBPselectfarm(b->batRole, b->ttype,
-                                                          imprintsheap);
+                                                         imprintsheap);
 
 #define SMP_SIZE 2048
                s1 = BATsample_with_seed(b, SMP_SIZE, (uint64_t) GDKusec() * 
(uint64_t) b->batCacheid);
@@ -640,10 +640,12 @@ BATimprints(BAT *b)
                ((size_t *) imprints->imprints.base)[2] = (size_t) 
imprints->dictcnt;
                ((size_t *) imprints->imprints.base)[3] = (size_t) bi.count;
                imprints->imprints.parentid = b->batCacheid;
+               ATOMIC_INIT(&imprints->imprints.refs, 1);
                b->timprints = imprints;
                if (BBP_status(b->batCacheid) & BBPEXISTING &&
                    !b->theap->dirty &&
-                   !GDKinmemory(bi.h->farmid)) {
+                   !GDKinmemory(bi.h->farmid) &&
+                   !GDKinmemory(imprints->imprints.farmid)) {
                        MT_Id tid;
                        BBPfix(b->batCacheid);
                        char name[MT_NAME_LEN];
@@ -730,35 +732,14 @@ lng
 IMPSimprintsize(BAT *b)
 {
        lng sz = 0;
+       MT_lock_set(&b->batIdxLock);
        if (b->timprints && b->timprints != (Imprints *) 1) {
                sz = (lng) b->timprints->imprints.free;
        }
+       MT_lock_unset(&b->batIdxLock);
        return sz;
 }
 
-static void
-IMPSremove(BAT *b)
-{
-       Imprints *imprints;
-
-       assert(b->timprints != NULL);
-       assert(/* DISABLES CODE */ (1) || !VIEWtparent(b));
-
-       if ((imprints = b->timprints) != NULL) {
-               b->timprints = NULL;
-
-               TRC_DEBUG_IF(ACCELERATOR) {
-                       if (* (size_t *) imprints->imprints.base & (1 << 16))
-                               TRC_DEBUG_ENDIF(ACCELERATOR, "Removing 
persisted imprints\n");
-               }
-               if (HEAPdelete(&imprints->imprints, BBP_physical(b->batCacheid),
-                              "timprints") != GDK_SUCCEED)
-                       TRC_DEBUG(IO_, "IMPSremove(%s): imprints heap\n", 
BATgetId(b));
-
-               GDKfree(imprints);
-       }
-}
-
 void
 IMPSdestroy(BAT *b)
 {
@@ -770,9 +751,10 @@ IMPSdestroy(BAT *b)
                                  BATDIR,
                                  BBP_physical(b->batCacheid),
                                  "timprints");
-               } else if (b->timprints != NULL &&
-                          (/* DISABLES CODE */ (1) || !VIEWtparent(b)))
-                       IMPSremove(b);
+               } else if (b->timprints != NULL) {
+                       IMPSdecref(b->timprints, 
b->timprints->imprints.parentid == b->batCacheid);
+                       b->timprints = NULL;
+               }
                MT_lock_unset(&b->batIdxLock);
        }
 }
@@ -790,24 +772,40 @@ IMPSfree(BAT *b)
                MT_lock_set(&b->batIdxLock);
                imprints = b->timprints;
                if (imprints != NULL && imprints != (Imprints *) 1) {
-                       if (GDKinmemory(b->theap->farmid)) {
+                       if (GDKinmemory(imprints->imprints.farmid)) {
                                b->timprints = NULL;
-                               if (/* DISABLES CODE */ (1) || !VIEWtparent(b)) 
{
-                                       HEAPfree(&imprints->imprints, true);
-                                       GDKfree(imprints);
-                               }
+                               IMPSdecref(imprints, 
imprints->imprints.parentid == b->batCacheid);
                        } else {
-                               b->timprints = (Imprints *) 1;
-                               if (/* DISABLES CODE */ (1) || !VIEWtparent(b)) 
{
-                                       HEAPfree(&imprints->imprints, false);
-                                       GDKfree(imprints);
-                               }
+                               if (imprints->imprints.parentid == 
b->batCacheid)
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to