Changeset: 21de4e475709 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=21de4e475709
Modified Files:
        monetdb5/mal/mal_dataflow.c
        monetdb5/mal/mal_interpreter.c
        monetdb5/mal/mal_runtime.c
Branch: default
Log Message:

Factor out timing
Proper timing of function calls reported now for MAL function signatures.
Timing and profiling modes have been factored out.
Proper administration when exceptions are obtained
avoiding possible leakage.


diffs (truncated from 450 to 300 lines):

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
@@ -476,7 +476,6 @@ DFLOWstep(FlowTask *t, FlowStatus fs)
                        }
                        FREE_EXCEPTION(ret);
                        ret = 0;
-                       runtimeTiming(cntxt, mb, stk, pci, tid, lock, 
&runtimeProfile);
                        break;
                case PATcall:
                        /* patterncall(FAST,fs->pc = -fs->pc; GDKfree(backup); 
GDKfree(garbage); return ret,t) */
@@ -486,12 +485,10 @@ DFLOWstep(FlowTask *t, FlowStatus fs)
                        } else {
                                ret = (str)(*pci->fcn)(cntxt, mb, stk, pci);
                        }
-                       runtimeTiming(cntxt, mb, stk, pci, tid, lock, 
&runtimeProfile);
                        break;
                case CMDcall:
                        /* commandcall(FAST,fs->pc = -fs->pc; GDKfree(backup); 
GDKfree(garbage); return ret,t) */
                        ret =malCommandCall(stk, pci);
-                       runtimeTiming(cntxt, mb, stk, pci, tid, lock, 
&runtimeProfile);
                        break;
                case FACcall:
 /*
@@ -525,7 +522,6 @@ DFLOWstep(FlowTask *t, FlowStatus fs)
                                }
                                ret = runFactory(cntxt, pci->blk, mb, stk, pci);
                        }
-                       runtimeTiming(cntxt, mb, stk, pci, tid, lock, 
&runtimeProfile);
                        break;
                case FCNcall:
 /*
@@ -540,18 +536,24 @@ DFLOWstep(FlowTask *t, FlowStatus fs)
 
                                stk->pcup = stkpc;
                                nstk = prepareMALstack(pci->blk, 
pci->blk->vsize);
-                               if (nstk == 0)
-                                       
throw(MAL,"mal.interpreter",MAL_STACK_FAIL);
+                               if (nstk == 0){
+                                       ret= 
createException(MAL,"mal.interpreter",MAL_STACK_FAIL);
+                                       break;
+                               }
 
                                /*safeguardStack*/
                                nstk->stkdepth = nstk->stksize + stk->stkdepth;
                                nstk->calldepth = stk->calldepth + 1;
                                nstk->up = stk;
-                               if (nstk->calldepth > 256)
-                                       throw(MAL, "mal.interpreter", 
MAL_CALLDEPTH_FAIL);
-                               if ((unsigned)nstk->stkdepth > 
THREAD_STACK_SIZE / sizeof(mb->var[0]) / 4 && THRhighwater())
+                               if (nstk->calldepth > 256){
+                                       ret=createException(MAL, 
"mal.interpreter", MAL_CALLDEPTH_FAIL);
+                                       break;
+                               }
+                               if ((unsigned)nstk->stkdepth > 
THREAD_STACK_SIZE / sizeof(mb->var[0]) / 4 && THRhighwater()){
                                        /* we are running low on stack space */
-                                       throw(MAL, "mal.interpreter", 
MAL_STACK_FAIL);
+                                       ret= createException(MAL, 
"mal.interpreter", MAL_STACK_FAIL);
+                                       break;
+                               }
 
                                /* copy arguments onto destination stack */
                                q= getInstrPtr(pci->blk,0);
@@ -563,10 +565,8 @@ DFLOWstep(FlowTask *t, FlowStatus fs)
                                        if (lhs->vtype == TYPE_bat)
                                                BBPincref(lhs->val.bval, TRUE);
                                }
-                               runtimeProfileBegin(cntxt, pci->blk, nstk, 0, 
&runtimeProfile, 1);
                                ret = runMALsequence(cntxt, pci->blk, 1, 
pci->blk->stop, nstk, stk, pci);
                                GDKfree(nstk);
-                               runtimeTiming(cntxt, pci->blk, nstk, 0, tid, 
lock, &runtimeProfile);
                        }
                        break;
                case NOOPsymbol:
@@ -668,13 +668,15 @@ DFLOWstep(FlowTask *t, FlowStatus fs)
                                mdbStep(cntxt, mb, stk, stkpc);
                                if (stk->cmd == 'x' || stk->cmd == 'q' || 
cntxt->mode == FINISHING) {
                                        stkpc = mb->stop;
-                                       fs->pc = -fs->pc; GDKfree(backup); 
GDKfree(garbage); return ret;
+                                       fs->pc = -fs->pc;
+                                       goto finalize;
                                }
                                if (stk->cmd == 'r') {
                                        stk->cmd = 'n';
                                        stkpc = startpc;
                                        exceptionVar = -1;
-                                       fs->pc = -fs->pc; GDKfree(backup); 
GDKfree(garbage); return ret;
+                                       fs->pc = -fs->pc;
+                                       goto finalize;
                                }
                        }
                        /* Detect any exception received from the 
implementation. */
@@ -683,7 +685,8 @@ DFLOWstep(FlowTask *t, FlowStatus fs)
                                GDKfree(ret);       /* no need to check for 
M5OutOfMemory */
                                ret = MAL_SUCCEED;
                                stkpc = mb->stop;
-                               fs->pc = -fs->pc; GDKfree(backup); 
GDKfree(garbage); return ret;
+                               fs->pc = -fs->pc;
+                               goto finalize;
                        }
                        /*
                         * Exceptions are caught based on their name, which is 
part of the
@@ -703,9 +706,10 @@ DFLOWstep(FlowTask *t, FlowStatus fs)
                        if (exceptionVar == -1) {
                                runtimeProfileExit(cntxt, mb, stk, 
&runtimeProfile);
                                if (cntxt->qtimeout && time(NULL) - 
stk->clock.tv_usec > cntxt->qtimeout)
-                                       throw(MAL, "mal.interpreter", 
RUNTIME_QRY_TIMEOUT);
+                                       ret= createException(MAL, 
"mal.interpreter", RUNTIME_QRY_TIMEOUT);
                                stkpc = mb->stop;
-                               fs->pc = -fs->pc; GDKfree(backup); 
GDKfree(garbage); return ret;
+                               fs->pc = -fs->pc;
+                               goto finalize;
                        }
                        /* assure correct variable type */
                        if (getVarType(mb, exceptionVar) == TYPE_str) {
@@ -733,7 +737,8 @@ DFLOWstep(FlowTask *t, FlowStatus fs)
                                mdbStep(cntxt, mb, stk, stkpc);
                                if (stk->cmd == 'x' || cntxt->mode == 
FINISHING) {
                                        stkpc = mb->stop;
-                                       fs->pc = -fs->pc; GDKfree(backup); 
GDKfree(garbage); return ret;
+                                       fs->pc = -fs->pc; 
+                                       goto finalize;
                                }
                        }
                        /* skip to catch block or end */
@@ -755,20 +760,22 @@ DFLOWstep(FlowTask *t, FlowStatus fs)
                                runtimeProfileExit(cntxt, mb, stk, 
&runtimeProfile);
                                runtimeProfile.ppc = 0; /* also finalize 
function call event */
                                runtimeProfileExit(cntxt, mb, stk, 
&runtimeProfile);
-                               if (cntxt->qtimeout && time(NULL) - 
stk->clock.tv_usec > cntxt->qtimeout)
-                                       throw(MAL, "mal.interpreter", 
RUNTIME_QRY_TIMEOUT);
-                               fs->pc = -fs->pc; GDKfree(backup); 
GDKfree(garbage); return ret;
+                               fs->pc = -fs->pc; 
+                               stkpc = mb->stop;
+                               goto finalize;
                        }
                        pci = getInstrPtr(mb, stkpc);
                }
                runtimeProfileExit(cntxt, mb, stk, &runtimeProfile);
+               runtimeTiming(cntxt, mb, stk, pci, tid, lock, &runtimeProfile);
        }
+finalize:
+       GDKfree(backup);
+       GDKfree(garbage);
        if (cntxt->qtimeout && time(NULL) - stk->clock.tv_usec > 
cntxt->qtimeout)
                throw(MAL, "mal.interpreter", RUNTIME_QRY_TIMEOUT);
-       if (ret)
+       if (ret && fs->pc > 0)
                fs->pc = -fs->pc;
-       GDKfree(backup);
-       GDKfree(garbage);
        return ret;
 }
 
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
@@ -458,10 +458,10 @@ str runMAL(Client cntxt, MalBlkPtr mb, i
                env->cmd = stk->cmd;
        if (!stk->keepAlive && garbageControl(getInstrPtr(mb, 0)))
                garbageCollector(cntxt, mb, stk, env != stk);
+       if (stk && stk != env)
+               GDKfree(stk);
        if (cntxt->qtimeout && time(NULL) - stk->clock.tv_usec > 
cntxt->qtimeout)
                throw(MAL, "mal.interpreter", RUNTIME_QRY_TIMEOUT);
-       if (stk && stk != env)
-               GDKfree(stk);
        return ret;
 }
 
@@ -574,7 +574,7 @@ str runMALsequence(Client cntxt, MalBlkP
        int stkpc = 0;
        MT_Lock *lock = NULL;
        int tid = 0;
-       RuntimeProfileRecord runtimeProfile;
+       RuntimeProfileRecord runtimeProfile, runtimeProfileFunction;
 
        runtimeProfileInit(mb, &runtimeProfile, cntxt->flags & memoryFlag);
        if (stk == NULL)
@@ -583,8 +583,10 @@ str runMALsequence(Client cntxt, MalBlkP
                oldtimer = cntxt->timer = GDKusec();
 
        /* also produce event record for start of function */
-       if ( startpc == 1 ) 
-               runtimeProfileBegin(cntxt, mb, stk, 0, &runtimeProfile, 1);
+       if ( startpc == 1 )  {
+               runtimeProfileInit(mb, &runtimeProfileFunction, cntxt->flags & 
memoryFlag);
+               runtimeProfileBegin(cntxt, mb, stk, 0, &runtimeProfileFunction, 
1);
+       }
        stkpc = startpc;
        exceptionVar = -1;
 
@@ -674,7 +676,6 @@ str runMALsequence(Client cntxt, MalBlkP
  * their. They seem superflous and the way they are used will
  * cause errors in multi-assignment statements.
  */
-                               /* assignStmt(@1,continue,stk)*/
                                for (k = 0, i = pci->retc; k < pci->retc && i < 
pci->argc; i++, k++) {
                                        lhs = &stk->stk[pci->argv[k]];
                                        rhs = &stk->stk[pci->argv[i]];
@@ -684,22 +685,16 @@ str runMALsequence(Client cntxt, MalBlkP
                                }
                                FREE_EXCEPTION(ret);
                                ret = 0;
-                               runtimeTiming(cntxt, mb, stk, pci, tid, lock, 
&runtimeProfile);
                                break;
                        case PATcall:
-                               /* patterncall(@1,continue,stk) */
                                if (pci->fcn == NULL) {
                                        ret = createScriptException(mb, stkpc, 
MAL, NULL,
                                                "address of pattern %s.%s 
missing", pci->modname, pci->fcnname);
-                               } else {
+                               } else
                                        ret = (str)(*pci->fcn)(cntxt, mb, stk, 
pci);
-                               }
-                               runtimeTiming(cntxt, mb, stk, pci, tid, lock, 
&runtimeProfile);
                                break;
                        case CMDcall:
-                               /* commandcall(@1,continue,stk) */
                                ret =malCommandCall(stk, pci);
-                               runtimeTiming(cntxt, mb, stk, pci, tid, lock, 
&runtimeProfile);
                                break;
                        case FACcall:
 /*
@@ -707,7 +702,6 @@ str runMALsequence(Client cntxt, MalBlkP
  * call to the factory manager.
  * Factory calls should deal with the reference counting.
  */
-                               /* factorycall(@1,continue,stk) */
                                if (pci->blk == NULL)
                                        ret = createScriptException(mb, stkpc, 
MAL, NULL,
                                                "reference to MAL function 
missing");
@@ -733,7 +727,6 @@ str runMALsequence(Client cntxt, MalBlkP
                                        }
                                        ret = runFactory(cntxt, pci->blk, mb, 
stk, pci);
                                }
-                               runtimeTiming(cntxt, mb, stk, pci, tid, lock, 
&runtimeProfile);
                                break;
                        case FCNcall:
 /*
@@ -741,25 +734,30 @@ str runMALsequence(Client cntxt, MalBlkP
  * a new stack frame and do housekeeping, such as garbagecollection of all
  * non-returned values.
  */
-                               /* functioncall(@1,continue,stk) */
                                {       MalStkPtr nstk;
                                        InstrPtr q;
                                        int ii, arg;
 
                                        stk->pcup = stkpc;
                                        nstk = prepareMALstack(pci->blk, 
pci->blk->vsize);
-                                       if (nstk == 0)
-                                               
throw(MAL,"mal.interpreter",MAL_STACK_FAIL);
+                                       if (nstk == 0){
+                                               ret= 
createException(MAL,"mal.interpreter",MAL_STACK_FAIL);
+                                               break;
+                                       }
 
                                        /*safeguardStack*/
                                        nstk->stkdepth = nstk->stksize + 
stk->stkdepth;
                                        nstk->calldepth = stk->calldepth + 1;
                                        nstk->up = stk;
-                                       if (nstk->calldepth > 256)
-                                               throw(MAL, "mal.interpreter", 
MAL_CALLDEPTH_FAIL);
-                                       if ((unsigned)nstk->stkdepth > 
THREAD_STACK_SIZE / sizeof(mb->var[0]) / 4 && THRhighwater())
+                                       if (nstk->calldepth > 256) {
+                                               ret= createException(MAL, 
"mal.interpreter", MAL_CALLDEPTH_FAIL);
+                                               break;
+                                       }
+                                       if ((unsigned)nstk->stkdepth > 
THREAD_STACK_SIZE / sizeof(mb->var[0]) / 4 && THRhighwater()){
                                                /* we are running low on stack 
space */
-                                               throw(MAL, "mal.interpreter", 
MAL_STACK_FAIL);
+                                               ret= createException(MAL, 
"mal.interpreter", MAL_STACK_FAIL);
+                                               break;
+                                       }
 
                                        /* copy arguments onto destination 
stack */
                                        q= getInstrPtr(pci->blk,0);
@@ -771,10 +769,8 @@ str runMALsequence(Client cntxt, MalBlkP
                                                if (lhs->vtype == TYPE_bat)
                                                        
BBPincref(lhs->val.bval, TRUE);
                                        }
-                                       runtimeProfileBegin(cntxt, pci->blk, 
nstk, 0, &runtimeProfile, 1);
                                        ret = runMALsequence(cntxt, pci->blk, 
1, pci->blk->stop, nstk, stk, pci);
                                        GDKfree(nstk);
-                                       runtimeTiming(cntxt, pci->blk, nstk, 0, 
tid, lock, &runtimeProfile);
                                }
                                break;
                        case NOOPsymbol:
@@ -789,9 +785,10 @@ str runMALsequence(Client cntxt, MalBlkP
                                        garbageCollector(cntxt, mb, stk, TRUE);
                                runtimeProfileExit(cntxt, mb, stk, 
&runtimeProfile);
                                runtimeProfile.ppc = 0; /* also finalize 
function call event */
-                               runtimeProfileExit(cntxt, mb, stk, 
&runtimeProfile);
-                               if (cntxt->qtimeout && time(NULL) - 
stk->clock.tv_usec > cntxt->qtimeout)
-                                       throw(MAL, "mal.interpreter", 
RUNTIME_QRY_TIMEOUT);
+                               if (cntxt->qtimeout && time(NULL) - 
stk->clock.tv_usec > cntxt->qtimeout){
+                                       ret= createException(MAL, 
"mal.interpreter", RUNTIME_QRY_TIMEOUT);
+                                       break;
+                               }
_______________________________________________
Checkin-list mailing list
[email protected]
http://mail.monetdb.org/mailman/listinfo/checkin-list

Reply via email to