Changeset: b826f98606d7 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/b826f98606d7
Modified Files:
        clients/Tests/exports.stable.out
        gdk/gdk_system.c
        monetdb5/mal/mal_dataflow.c
        monetdb5/mal/mal_interpreter.c
        monetdb5/mal/mal_interpreter.h
        monetdb5/modules/mal/orderidx.c
        sql/backends/monet5/sql.c
        tools/monetdbe/monetdbe.c
        tools/mserver/mserver5.c
Branch: resource_management
Log Message:

Better use and reset of thread-local allocator.
This fixes the out-of-memory issue on 32 bit Windows during testing.


diffs (truncated from 307 to 300 lines):

diff --git a/clients/Tests/exports.stable.out b/clients/Tests/exports.stable.out
--- a/clients/Tests/exports.stable.out
+++ b/clients/Tests/exports.stable.out
@@ -1220,7 +1220,7 @@ const char rpcRef[];
 const char rsColumnRef[];
 const char rtreeRef[];
 str runMAL(Client c, MalBlkPtr mb, MalBlkPtr mbcaller, MalStkPtr env);
-str runMALsequence(allocator *, Client cntxt, MalBlkPtr mb, int startpc, int 
stoppc, MalStkPtr stk, MalStkPtr env, InstrPtr pcicaller);
+str runMALsequence(Client cntxt, MalBlkPtr mb, int startpc, int stoppc, 
MalStkPtr stk, MalStkPtr env, InstrPtr pcicaller);
 oid runtimeProfileSetTag(Client cntxt);
 const char sampleRef[];
 const char selectNotNilRef[];
diff --git a/gdk/gdk_system.c b/gdk/gdk_system.c
--- a/gdk/gdk_system.c
+++ b/gdk/gdk_system.c
@@ -418,6 +418,11 @@ MT_thread_init(void)
        }
        InitializeCriticalSection(&winthread_cs);
 #endif
+       mainthread.ma = create_allocator(NULL, mainthread.threadname, false);
+       if (mainthread.ma == NULL) {
+               GDKerror("Creating thread-local allocator failed");
+               return false;
+       }
        thread_initialized = true;
        return true;
 }
diff --git a/monetdb5/mal/mal_dataflow.c b/monetdb5/mal/mal_dataflow.c
--- a/monetdb5/mal/mal_dataflow.c
+++ b/monetdb5/mal/mal_dataflow.c
@@ -337,7 +337,7 @@ DFLOWworker(void *T)
                                        break;
                        }
 
-                       error = runMALsequence(ma, flow->cntxt, flow->mb, 
fe->pc, fe->pc + 1,
+                       error = runMALsequence(flow->cntxt, flow->mb, fe->pc, 
fe->pc + 1,
                                                                   flow->stk, 
0, 0);
                        if (error)
                                error = MA_STRDUP(flow->mb->ma, error);
diff --git a/monetdb5/mal/mal_interpreter.c b/monetdb5/mal/mal_interpreter.c
--- a/monetdb5/mal/mal_interpreter.c
+++ b/monetdb5/mal/mal_interpreter.c
@@ -370,9 +370,7 @@ runMAL(Client cntxt, MalBlkPtr mb, MalBl
                 * been observed due the small size of the function).
                 */
        }
-       allocator_state ta_state = ma_open(ta);
-       ret = copyException(mb->ma, runMALsequence(ta, cntxt, mb, 1, 0, stk, 
env, 0));
-       ma_close(ta, &ta_state);
+       ret = copyException(mb->ma, runMALsequence(cntxt, mb, 1, 0, stk, env, 
0));
 
        if (!stk->keepAlive && garbageControl(getInstrPtr(mb, 0)))
                garbageCollector(cntxt, mb, stk, env != stk);
@@ -409,10 +407,7 @@ reenterMAL(Client cntxt, MalBlkPtr mb, i
        if (stk == NULL)
                throw(MAL, "mal.interpreter", MAL_STACK_FAIL);
        keepAlive = stk->keepAlive;
-       allocator *ta = MT_thread_getallocator();
-       allocator_state ta_state = ma_open(ta);
-       ret = copyException(mb->ma, runMALsequence(ta, cntxt, mb, startpc, 
stoppc, stk, 0, 0));
-       ma_close(ta, &ta_state);
+       ret = copyException(mb->ma, runMALsequence(cntxt, mb, startpc, stoppc, 
stk, 0, 0));
 
        if (keepAlive == 0 && garbageControl(getInstrPtr(mb, 0)))
                garbageCollector(cntxt, mb, stk, stk != 0);
@@ -468,10 +463,7 @@ callMAL(Client cntxt, MalBlkPtr mb, MalS
                        if (lhs->bat)
                                BBPretain(lhs->val.bval);
                }
-               allocator *ta = MT_thread_getallocator();
-               allocator_state ta_state = ma_open(ta);
-               ret = copyException(mb->ma, runMALsequence(ta, cntxt, mb, 1, 0, 
stk, 0, 0));
-               ma_close(ta, &ta_state);
+               ret = copyException(mb->ma, runMALsequence(cntxt, mb, 1, 0, 
stk, 0, 0));
                break;
        case PATcall:
        case CMDcall:
@@ -501,7 +493,7 @@ callMAL(Client cntxt, MalBlkPtr mb, MalS
  * instruction, we take the wall-clock time for resource management.
  */
 str
-runMALsequence(allocator *ma, Client cntxt, MalBlkPtr mb, int startpc,
+runMALsequence(Client cntxt, MalBlkPtr mb, int startpc,
                           int stoppc, MalStkPtr stk, MalStkPtr env, InstrPtr 
pcicaller)
 {
        ValPtr lhs, rhs, v;
@@ -521,6 +513,9 @@ runMALsequence(allocator *ma, Client cnt
        if (stk == NULL)
                throw(MAL, "mal.interpreter", MAL_STACK_FAIL);
 
+       allocator *ma = MT_thread_getallocator();
+       allocator_state ma_state = ma_open(ma);
+
        /* prepare extended backup and garbage structures */
        if (startpc + 1 == stoppc) {
                pci = getInstrPtr(mb, startpc);
@@ -528,8 +523,7 @@ runMALsequence(allocator *ma, Client cnt
                        backup = ma_alloc(ma, pci->retc * sizeof(ValRecord));
                        garbage = (int *) ma_zalloc(ma, pci->argc * 
sizeof(int));
                        if (backup == NULL || garbage == NULL) {
-                               //GDKfree(backup);
-                               //GDKfree(garbage);
+                               ma_close(ma, &ma_state);
                                throw(MAL, "mal.interpreter", SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
                        }
                } else {
@@ -541,8 +535,7 @@ runMALsequence(allocator *ma, Client cnt
                backup = ma_alloc(ma, mb->maxarg * sizeof(ValRecord));
                garbage = (int *) ma_zalloc(ma, mb->maxarg * sizeof(int));
                if (backup == NULL || garbage == NULL) {
-                       //GDKfree(backup);
-                       //GDKfree(garbage);
+                       ma_close(ma, &ma_state);
                        throw(MAL, "mal.interpreter", SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
                }
        } else {
@@ -561,10 +554,7 @@ runMALsequence(allocator *ma, Client cnt
                        && cntxt->qryctx.starttime - cntxt->session >
                        cntxt->sessiontimeout) {
                        runtimeProfileFinish(cntxt, mb, stk);
-                       //if (backup != backups)
-                       //      GDKfree(backup);
-                       //if (garbage != garbages)
-                       //      GDKfree(garbage);
+                       ma_close(ma, &ma_state);
                        throw(MAL, "mal.interpreter",
                                  SQLSTATE(HYT00) RUNTIME_SESSION_TIMEOUT);
                }
@@ -782,6 +772,8 @@ runMALsequence(allocator *ma, Client cnt
                        InstrPtr q;
                        int ii, arg;
 
+                       allocator_state ma_state2 = ma_open(ma);
+
                        nstk = prepareMALstack(ma, pci->blk, pci->blk->vsize);
                        if (nstk == 0) {
                                ret = createException(MAL, "mal.interpreter", 
MAL_STACK_FAIL);
@@ -796,14 +788,14 @@ runMALsequence(allocator *ma, Client cnt
                        if (nstk->calldepth > 256) {
                                ret = createException(MAL, "mal.interpreter",
                                                                          
MAL_CALLDEPTH_FAIL);
-                               // GDKfree(nstk);
+                               ma_close(ma, &ma_state2);
                                break;
                        }
                        if ((unsigned) nstk->stkdepth >
                                THREAD_STACK_SIZE / sizeof(mb->var[0]) / 4 && 
THRhighwater()) {
                                /* we are running low on stack space */
                                ret = createException(MAL, "mal.interpreter", 
MAL_STACK_FAIL);
-                               // GDKfree(nstk);
+                               ma_close(ma, &ma_state2);
                                break;
                        }
 
@@ -814,7 +806,7 @@ runMALsequence(allocator *ma, Client cnt
                                lhs = &nstk->stk[q->argv[arg]];
                                rhs = &stk->stk[pci->argv[ii]];
                                if (VALcopy(ma, lhs, rhs) == NULL) {
-                                       //GDKfree(nstk);
+                                       ma_close(ma, &ma_state2);
                                        ret = createException(MAL, 
"mal.interpreter",
                                                                                
  SQLSTATE(HY013) MAL_MALLOC_FAIL);
                                        break;
@@ -822,7 +814,7 @@ runMALsequence(allocator *ma, Client cnt
                                        BBPretain(lhs->val.bval);
                        }
                        if (ret == MAL_SUCCEED && ii == pci->argc) {
-                               ret = runMALsequence(ma, cntxt, pci->blk, 1, 
pci->blk->stop, nstk,
+                               ret = runMALsequence(cntxt, pci->blk, 1, 
pci->blk->stop, nstk,
                                                                         stk, 
pci);
                                garbageCollector(cntxt, pci->blk, nstk, 0);
                                arg = q->retc;
@@ -831,8 +823,8 @@ runMALsequence(allocator *ma, Client cnt
                                        if (lhs->bat)
                                                BBPrelease(lhs->val.bval);
                                }
-                               //GDKfree(nstk);
                        }
+                       ma_close(ma, &ma_state2);
                        break;
                }
                case REMsymbol:
@@ -1289,10 +1281,7 @@ runMALsequence(allocator *ma, Client cnt
        }
        if (startedProfileQueue)
                runtimeProfileFinish(cntxt, mb, stk);
-       //if (backup != backups)
-       //      GDKfree(backup);
-       //if (garbage != garbages)
-       //      GDKfree(garbage);
+       ma_close(ma, &ma_state);
        return ret;
 }
 
diff --git a/monetdb5/mal/mal_interpreter.h b/monetdb5/mal/mal_interpreter.h
--- a/monetdb5/mal/mal_interpreter.h
+++ b/monetdb5/mal/mal_interpreter.h
@@ -29,7 +29,7 @@
 mal_export MalStkPtr prepareMALstack(allocator *pa, MalBlkPtr mb, int size);
 mal_export str runMAL(Client c, MalBlkPtr mb, MalBlkPtr mbcaller,
                                          MalStkPtr env);
-mal_export str runMALsequence(allocator *, Client cntxt, MalBlkPtr mb, int 
startpc,
+mal_export str runMALsequence(Client cntxt, MalBlkPtr mb, int startpc,
                                                          int stoppc, MalStkPtr 
stk, MalStkPtr env,
                                                          InstrPtr pcicaller);
 mal_export str reenterMAL(Client cntxt, MalBlkPtr mb, int startpc, int stoppc,
diff --git a/monetdb5/modules/mal/orderidx.c b/monetdb5/modules/mal/orderidx.c
--- a/monetdb5/modules/mal/orderidx.c
+++ b/monetdb5/modules/mal/orderidx.c
@@ -231,7 +231,7 @@ OIDXcreateImplementation(Client cntxt, i
                .val.bval = b->batCacheid,
        };
        BBPretain(newstk->stk[arg].val.bval);
-       msg = runMALsequence(smb->ma, cntxt, smb, 1, 0, newstk, 0, 0);
+       msg = runMALsequence(cntxt, smb, 1, 0, newstk, 0, 0);
        freeStack(newstk);
        /* get rid of temporary MAL block */
   bailout:
diff --git a/sql/backends/monet5/sql.c b/sql/backends/monet5/sql.c
--- a/sql/backends/monet5/sql.c
+++ b/sql/backends/monet5/sql.c
@@ -5081,7 +5081,7 @@ SQLunionfunc(Client cntxt, MalBlkPtr mb,
                                        if (!omb && npci->fcn && npci->token == 
PATcall) /* pattern */
                                                ret = (*(str (*)(Client, 
MalBlkPtr, MalStkPtr, InstrPtr))npci->fcn)(cntxt, nmb, nstk, npci);
                                        else
-                                               ret = runMALsequence(nmb->ma, 
cntxt, nmb, start, nmb->stop, nstk, env /* copy result in nstk first 
instruction*/, q);
+                                               ret = runMALsequence(cntxt, 
nmb, start, nmb->stop, nstk, env /* copy result in nstk first instruction*/, q);
 
                                        if (!ret) {
                                                /* insert into result */
diff --git a/tools/monetdbe/monetdbe.c b/tools/monetdbe/monetdbe.c
--- a/tools/monetdbe/monetdbe.c
+++ b/tools/monetdbe/monetdbe.c
@@ -881,7 +881,7 @@ monetdbe_open_remote(monetdbe_database_i
        stk->keepAlive = TRUE;
        c->qryctx.starttime = GDKusec();
        c->qryctx.endtime = c->querytimeout ? c->qryctx.starttime + 
c->querytimeout : 0;
-       if ( (mdbe->msg = runMALsequence(mb->ma, c, mb, 1, 0, stk, 0, 0)) != 
MAL_SUCCEED ) {
+       if ( (mdbe->msg = runMALsequence(c, mb, 1, 0, stk, 0, 0)) != 
MAL_SUCCEED ) {
                freeStack(stk);
                freeSymbol(c->curprg);
                c->curprg = curprg;
@@ -943,13 +943,6 @@ monetdbe_open(monetdbe_database *dbhdl, 
 
        if (res == 0 && is_remote && !mdbe->msg)
                res = monetdbe_open_remote(mdbe, opts);
-       allocator *ta = MT_thread_getallocator();
-       if (!ta) {
-               allocator *ma = create_allocator(NULL, MT_thread_getname(), 
false);
-               assert(ma);
-               if (ma)
-                       MT_thread_setallocator(ma);
-       }
 
        MT_lock_unset(&embedded_lock);
        if (mdbe->msg)
diff --git a/tools/mserver/mserver5.c b/tools/mserver/mserver5.c
--- a/tools/mserver/mserver5.c
+++ b/tools/mserver/mserver5.c
@@ -924,30 +924,20 @@ main(int argc, char **av)
                        *secretp = '\0';
                }
        }
-       allocator *ma = NULL;
-       if ((ma = create_allocator(NULL, "MA_tls_main_mserver5", false)) == 
NULL) {
-                       fprintf(stderr, "!ERROR: failed to create allocator\n");
-                       exit(1);
-       }
-       MT_thread_setallocator(ma);
        assert(MT_thread_getallocator() != NULL);
        modules[mods++] = 0;
        if (mal_init(modules, false, readpwdxit ? secret : NULL, 
mercurial_revision())) {
                /* don't show this as a crash */
                if (!GDKinmemory(0))
                        msab_registerStop();
-               if (ma) {
-                       ma_destroy(ma);
-                       MT_thread_setallocator(NULL);
-               }
+               ma_destroy(MT_thread_getallocator());
+               MT_thread_setallocator(NULL);
                return 1;
        }
        if (readpwdxit) {
                msab_registerStop();
-               if (ma) {
-                       ma_destroy(ma);
-                       MT_thread_setallocator(NULL);
-               }
+               ma_destroy(MT_thread_getallocator());
+               MT_thread_setallocator(NULL);
                return 0;
        }
 
@@ -991,10 +981,8 @@ main(int argc, char **av)
                }
                MT_sleep_ms(100);               /* pause(), except for 
sys.shutdown() */
        }
-       if (ma) {
-               ma_destroy(ma);
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to