Changeset: 61018a4425ee for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/61018a4425ee
Modified Files:
        gdk/gdk_hash.c
        gdk/gdk_join.c
        gdk/gdk_private.h
        gdk/gdk_string.c
Branch: ustr
Log Message:

Fixed some deadlock conditions; added some error checks; added some tracer msgs.


diffs (truncated from 389 to 300 lines):

diff --git a/gdk/gdk_hash.c b/gdk/gdk_hash.c
--- a/gdk/gdk_hash.c
+++ b/gdk/gdk_hash.c
@@ -372,7 +372,7 @@ HASHgrowbucket(BAT *b)
                h->heapbckt.free += h->width;
                BUN lold, lnew, hb;
                lold = lnew = BUN_NONE;
-               BATiter bi = bat_iterator(b);
+               BATiter bi = bat_iterator_nolock(b);
                if ((hb = HASHget(h, old)) != BUN_NONE) {
                        h->nheads--;
                        var_t off = 0;
@@ -405,7 +405,6 @@ HASHgrowbucket(BAT *b)
                                hb = HASHgetlink(h, hb);
                        } while (hb != BUN_NONE);
                }
-               bat_iterator_end(&bi);
                if (lnew == BUN_NONE)
                        HASHput(h, new, BUN_NONE);
                else
@@ -671,7 +670,7 @@ BAThashsave(BAT *b, bool dosync)
 
 #define starthash(TYPE)                                                        
\
        do {                                                            \
-               const TYPE *restrict v = (const TYPE *) BUNtloc(&bi, 0);        
\
+               const TYPE *restrict v = (const TYPE *) BUNtloc(bi, 0); \
                TIMEOUT_LOOP_IDX(p, cnt1, qry_ctx) {                    \
                        c = hash_##TYPE(h, v + o - b->hseqbase);        \
                        hget = HASHget(h, c);                           \
@@ -698,7 +697,7 @@ BAThashsave(BAT *b, bool dosync)
        } while (0)
 #define finishhash(TYPE)                                               \
        do {                                                            \
-               const TYPE *restrict v = (const TYPE *) BUNtloc(&bi, 0);        
\
+               const TYPE *restrict v = (const TYPE *) BUNtloc(bi, 0); \
                TIMEOUT_LOOP(ci->ncand - p, qry_ctx) {                  \
                        c = hash_##TYPE(h, v + o - b->hseqbase);        \
                        hget = HASHget(h, c);                           \
@@ -731,9 +730,10 @@ BAThashsave(BAT *b, bool dosync)
  * If offsets is set, the hash is built on the theap values and not the
  * values in the tvheap that they point to. */
 Hash *
-BAThash_impl(BAT *restrict b, struct canditer *restrict ci,
+BAThash_impl(BATiter *restrict bi, struct canditer *restrict ci,
             bool offsets, const char *restrict ext)
 {
+       BAT *b = bi->b;
        lng t0 = 0;
        BUN cnt1;
        BUN mask, maxmask = 0;
@@ -742,16 +742,15 @@ BAThash_impl(BAT *restrict b, struct can
        BUN hget, hb;
        Hash *h = NULL;
        const char *nme = GDKinmemory(b->theap->farmid) ? ":memory:" : 
BBP_physical(b->batCacheid);
-       BATiter bi = bat_iterator(b);
-       unsigned int tpe = ATOMbasetype(bi.type);
-       bool hascand = ci->tpe != cand_dense || ci->ncand != bi.count;
+       unsigned int tpe = ATOMbasetype(bi->type);
+       bool hascand = ci->tpe != cand_dense || ci->ncand != bi->count;
 
        QryCtx *qry_ctx = MT_thread_get_qry_ctx();
 
        assert(!offsets || ATOMvarsized(b->ttype));
        assert(strcmp(ext, "thash") != 0 || !hascand);
-       assert(bi.type != TYPE_msk);
-       assert(bi.type != TYPE_void);
+       assert(bi->type != TYPE_msk);
+       assert(bi->type != TYPE_void);
 
        MT_thread_setalgorithm(hascand ? "create hash with candidates" : 
"create hash");
        TRC_DEBUG_IF(ACCELERATOR) t0 = GDKusec();
@@ -759,10 +758,9 @@ BAThash_impl(BAT *restrict b, struct can
                  ALGOBATFMT ": create hash;\n", ALGOBATPAR(b));
 
        if ((h = GDKzalloc(sizeof(*h))) == NULL ||
-           (h->heaplink.farmid = BBPselectfarm(hascand ? TRANSIENT : 
b->batRole, bi.type, hashheap)) < 0 ||
-           (h->heapbckt.farmid = BBPselectfarm(hascand ? TRANSIENT : 
b->batRole, bi.type, hashheap)) < 0) {
+           (h->heaplink.farmid = BBPselectfarm(hascand ? TRANSIENT : 
b->batRole, bi->type, hashheap)) < 0 ||
+           (h->heapbckt.farmid = BBPselectfarm(hascand ? TRANSIENT : 
b->batRole, bi->type, hashheap)) < 0) {
                GDKfree(h);
-               bat_iterator_end(&bi);
                return NULL;
        }
        h->width = HASHwidth(BATcapacity(b));
@@ -778,7 +776,6 @@ BAThash_impl(BAT *restrict b, struct can
        if (HEAPalloc(&h->heaplink, hascand ? ci->ncand : BATcapacity(b),
                      h->width) != GDK_SUCCEED) {
                GDKfree(h);
-               bat_iterator_end(&bi);
                return NULL;
        }
        h->heaplink.free = ci->ncand * h->width;
@@ -798,13 +795,13 @@ BAThash_impl(BAT *restrict b, struct can
        } else if (ATOMsize(tpe) == 2) {
                /* perfect hash for two-byte sized atoms */
                mask = (1 << 16);
-       } else if (bi.key || ci->ncand <= 4096) {
+       } else if (bi->key || ci->ncand <= 4096) {
                /* if key, or if small, don't bother dynamically
                 * adjusting the hash mask */
                mask = HASHmask(ci->ncand);
-       } else if (!hascand && bi.unique_est != 0) {
+       } else if (!hascand && bi->unique_est != 0) {
                maxmask = HASHmask(ci->ncand);
-               mask = HASHmask((BUN) bi.unique_est);
+               mask = HASHmask((BUN) bi->unique_est);
                if (mask < maxmask) {
                        /* it's only an estimate: try out on first 25%
                         * of b */
@@ -843,7 +840,6 @@ BAThash_impl(BAT *restrict b, struct can
                            true) != GDK_SUCCEED) {
                        HEAPfree(&h->heaplink, true);
                        GDKfree(h);
-                       bat_iterator_end(&bi);
                        return NULL;
                }
 
@@ -883,7 +879,7 @@ BAThash_impl(BAT *restrict b, struct can
                default: {
                        if (offsets) {
                                TIMEOUT_LOOP_IDX(p, cnt1, qry_ctx) {
-                                       var_t off = VarHeapVal(bi.base, o - 
b->hseqbase, bi.width);
+                                       var_t off = VarHeapVal(bi->base, o - 
b->hseqbase, bi->width);
                                        c = HASHbucket(h, (BUN) mix_oid(off));
                                        hget = HASHget(h, c);
                                        if (hget == BUN_NONE) {
@@ -895,7 +891,7 @@ BAThash_impl(BAT *restrict b, struct can
                                                for (hb = hget;
                                                     hb != BUN_NONE;
                                                     hb = HASHgetlink(h, hb)) {
-                                                       if (off == 
VarHeapVal(bi.base, hb, bi.width))
+                                                       if (off == 
VarHeapVal(bi->base, hb, bi->width))
                                                                break;
                                                }
                                                h->nunique += hb == BUN_NONE;
@@ -911,7 +907,7 @@ BAThash_impl(BAT *restrict b, struct can
 
                        bool (*atomeq)(const void *, const void *) = 
ATOMequal(tpe);
                        TIMEOUT_LOOP_IDX(p, cnt1, qry_ctx) {
-                               const void *restrict v = BUNtail(&bi, o - 
b->hseqbase);
+                               const void *restrict v = BUNtail(bi, o - 
b->hseqbase);
                                c = hash_any(h, v);
                                hget = HASHget(h, c);
                                if (hget == BUN_NONE) {
@@ -924,7 +920,7 @@ BAThash_impl(BAT *restrict b, struct can
                                             hb != BUN_NONE;
                                             hb = HASHgetlink(h, hb)) {
                                                if (atomeq(v,
-                                                          BUNtail(&bi, hb)))
+                                                          BUNtail(bi, hb)))
                                                        break;
                                        }
                                        h->nunique += hb == BUN_NONE;
@@ -994,7 +990,7 @@ BAThash_impl(BAT *restrict b, struct can
        default: {
                if (offsets) {
                        TIMEOUT_LOOP(ci->ncand - p, qry_ctx) {
-                               var_t off = VarHeapVal(bi.base, o - 
b->hseqbase, bi.width);
+                               var_t off = VarHeapVal(bi->base, o - 
b->hseqbase, bi->width);
                                c = HASHbucket(h, (BUN) mix_oid(off));
                                hget = HASHget(h, c);
                                h->nheads += hget == BUN_NONE;
@@ -1002,7 +998,7 @@ BAThash_impl(BAT *restrict b, struct can
                                        for (hb = hget;
                                             hb != BUN_NONE;
                                             hb = HASHgetlink(h, hb)) {
-                                               if (off == VarHeapVal(bi.base, 
hb, bi.width))
+                                               if (off == VarHeapVal(bi->base, 
hb, bi->width))
                                                        break;
                                        }
                                        h->nunique += hb == BUN_NONE;
@@ -1020,7 +1016,7 @@ BAThash_impl(BAT *restrict b, struct can
                }
                bool (*atomeq)(const void *, const void *) = ATOMequal(tpe);
                TIMEOUT_LOOP(ci->ncand - p, qry_ctx) {
-                       const void *restrict v = BUNtail(&bi, o - b->hseqbase);
+                       const void *restrict v = BUNtail(bi, o - b->hseqbase);
                        c = hash_any(h, v);
                        hget = HASHget(h, c);
                        h->nheads += hget == BUN_NONE;
@@ -1028,7 +1024,7 @@ BAThash_impl(BAT *restrict b, struct can
                                for (hb = hget;
                                     hb != BUN_NONE;
                                     hb = HASHgetlink(h, hb)) {
-                                       if (atomeq(v, BUNtail(&bi, hb)))
+                                       if (atomeq(v, BUNtail(bi, hb)))
                                                break;
                                }
                                h->nunique += hb == BUN_NONE;
@@ -1043,16 +1039,6 @@ BAThash_impl(BAT *restrict b, struct can
                break;
        }
        }
-       bat_iterator_end(&bi);
-       /* if the number of unique values is equal to the bat count,
-        * all values are necessarily distinct */
-       MT_lock_set(&b->theaplock);
-       if (h->nunique == BATcount(b) && !b->tkey) {
-               b->tkey = true;
-       }
-       if (ci->ncand == BATcount(b))
-               b->tunique_est = (double) h->nunique;
-       MT_lock_unset(&b->theaplock);
        TRC_DEBUG_IF(ACCELERATOR) {
                TRC_DEBUG_ENDIF(ACCELERATOR,
                                "hash construction " LLFMT " usec\n", GDKusec() 
- t0);
@@ -1061,7 +1047,6 @@ BAThash_impl(BAT *restrict b, struct can
        return h;
 
   bailout:
-       bat_iterator_end(&bi);
        HEAPfree(&h->heaplink, true);
        HEAPfree(&h->heapbckt, true);
        GDKfree(h);
@@ -1111,11 +1096,23 @@ BAThash(BAT *b)
        /* we have the write lock */
        if (b->thash == NULL) {
                struct canditer ci;
+               BATiter bi = bat_iterator(b);;
                canditer_init(&ci, b, NULL);
-               if ((b->thash = BAThash_impl(b, &ci, b->ustr != 0, "thash")) == 
NULL) {
+               b->thash = BAThash_impl(&bi, &ci, b->ustr != 0, "thash");
+               bat_iterator_end(&bi);
+               if (b->thash == NULL) {
                        MT_rwlock_wrunlock(&b->thashlock);
                        return GDK_FAIL;
                }
+               /* if the number of unique values is equal to the bat
+                * count, all values are necessarily distinct */
+               MT_lock_set(&b->theaplock);
+               if (b->thash->nunique == BATcount(b) && !b->tkey) {
+                       b->tkey = true;
+               }
+               if (ci.ncand == BATcount(b))
+                       b->tunique_est = (double) b->thash->nunique;
+               MT_lock_unset(&b->theaplock);
        }
        MT_rwlock_wrunlock(&b->thashlock);
        return GDK_SUCCEED;
diff --git a/gdk/gdk_join.c b/gdk/gdk_join.c
--- a/gdk/gdk_join.c
+++ b/gdk/gdk_join.c
@@ -2815,8 +2815,12 @@ vkeyjoin(BAT **r1p, BAT **r2p, BAT **r3p
                          r->thash ? " ignoring existing hash" : "",
                          swapped ? " (swapped)" : "");
                if (snprintf(ext, sizeof(ext), "thshjn%x",
-                            (unsigned) MT_getpid()) >= (int) sizeof(ext) ||
-                   (hsh = BAThash_impl(r, rci, true, ext)) == NULL)
+                            (unsigned) MT_getpid()) >= (int) sizeof(ext))
+                       return GDK_FAIL;
+               BATiter ri = bat_iterator(r);
+               hsh = BAThash_impl(&ri, rci, true, ext);
+               bat_iterator_end(&ri);
+               if (hsh == NULL)
                        return GDK_FAIL;
        }
 
@@ -3247,9 +3251,11 @@ hashjoin(BAT **r1p, BAT **r2p, BAT **r3p
                if (snprintf(ext, sizeof(ext), "thshjn%x",
                             (unsigned) MT_getpid()) >= (int) sizeof(ext))
                        goto bailout;
-               if ((hsh = BAThash_impl(r, rci, false, ext)) == NULL) {
+               BATiter ri = bat_iterator(r);
+               hsh = BAThash_impl(&ri, rci, false, ext);
+               bat_iterator_end(&ri);
+               if (hsh == NULL)
                        goto bailout;
-               }
        } else if (r->ustr) {
                /* we use a hash on r */
                MT_thread_setalgorithm(swapped ? "hashjoin using ustr hash 
(swapped)" : "hashjoin using ustr hash");
diff --git a/gdk/gdk_private.h b/gdk/gdk_private.h
--- a/gdk/gdk_private.h
+++ b/gdk/gdk_private.h
@@ -59,7 +59,7 @@ void BATfree(BAT *b)
 gdk_return BATgroup_internal(BAT **groups, BAT **extents, BAT **histo, BAT *b, 
BAT *s, BAT *g, BAT *e, BAT *h, bool subsorted)
        __attribute__((__warn_unused_result__))
        __attribute__((__visibility__("hidden")));
-Hash *BAThash_impl(BAT *restrict b, struct canditer *restrict ci, bool 
offsets, const char *restrict ext)
+Hash *BAThash_impl(BATiter *restrict bi, struct canditer *restrict ci, bool 
offsets, const char *restrict ext)
        __attribute__((__visibility__("hidden")));
 void BAThashsave(BAT *b, bool dosync)
        __attribute__((__visibility__("hidden")));
@@ -337,12 +337,12 @@ ilog2(BUN x)
        b->hseqbase,                                                    \
        ATOMname(b->ttype),                                             \
        
b->ttype==TYPE_str?b->twidth==1?"1":b->twidth==2?"2":b->twidth==4?"4":"8":"", \
-       !b->batTransient ? "P" : b->theap && b->theap->parentid != 
b->batCacheid ? "V" : b->tvheap && b->tvheap->parentid != b->batCacheid ? "v" : 
"T", \
+       !b->batTransient ? b->ustr ? "U" : "P" : b->theap && b->theap->parentid 
!= b->batCacheid ? "V" : b->tvheap && b->tvheap->parentid != b->batCacheid ? 
b->ustr ? "u" : "v" : "T", \
        BATtdense(b) ? "D" : b->ttype == TYPE_void && b->tvheap ? "X" : 
ATOMstorage(b->ttype) == TYPE_str && b->tvheap && GDK_ELIMDOUBLES(b->tvheap) ? 
"E" : "", \
        b->tsorted ? "S" : b->tnosorted ? "!s" : "",                    \
        b->trevsorted ? "R" : b->tnorevsorted ? "!r" : "",              \
        b->tkey ? "K" : b->tnokey[1] ? "!k" : "",                       \
-       b->tvkey ? "U" : "",                                            \
+       b->tvkey ? "k" : "",                                            \
        b->tnonil ? "N" : "",                                           \
        b->thash ? "H" : "",                                            \
        b->torderidx ? "O" : "",                                        \
@@ -359,12 +359,12 @@ ilog2(BUN x)
        b ? ATOMname(b->ttype) : "",                                    \
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to