Changeset: a1d56d661de7 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=a1d56d661de7
Modified Files:
        sql/backends/monet5/sql_execute.c
        sql/backends/monet5/sql_scenario.c
Branch: default
Log Message:

Move trace code to just before execution
Injection of the trace code is now performed in the SQLrun command,
the sole place where a plan is taken in execution.


diffs (truncated from 426 to 300 lines):

diff --git a/sql/backends/monet5/sql_execute.c 
b/sql/backends/monet5/sql_execute.c
--- a/sql/backends/monet5/sql_execute.c
+++ b/sql/backends/monet5/sql_execute.c
@@ -62,6 +62,154 @@
 * The tricky part for this statement is to ensure that the SQL statement
 * is executed within the client context specified. This leads to context 
juggling.
 */
+
+/*
+ * The trace operation collects the events in the BATs
+ * and creates a secondary result set upon termination
+ * of the query. 
+ */
+static void
+SQLsetTrace(Client cntxt)
+{
+       InstrPtr q, resultset;
+       InstrPtr tbls, cols, types, clen, scale;
+       MalBlkPtr mb = cntxt->curprg->def;
+       int k;
+
+       startTrace("sql_traces");
+       initTrace();
+
+       for(k= mb->stop-1; k>0; k--)
+               if( getInstrPtr(mb,k)->token ==ENDsymbol)
+                       break;
+       mb->stop=k;
+
+       q= newStmt(mb, "profiler", "stoptrace");
+       q= pushStr(mb,q,"sql_traces");
+       /* cook a new resultSet instruction */
+       resultset = newInstruction(mb,ASSIGNsymbol);
+       setModuleId(resultset, sqlRef);
+       setFunctionId(resultset, resultSetRef);
+       getArg(resultset,0)= newTmpVariable(mb,TYPE_int);
+
+       /* build table defs */
+       tbls = newStmt(mb,batRef, newRef);
+       setVarType(mb, getArg(tbls,0), newBatType(TYPE_oid, TYPE_str));
+       tbls = pushType(mb, tbls, TYPE_oid);
+       tbls = pushType(mb, tbls, TYPE_str);
+       resultset= pushArgument(mb,resultset, getArg(tbls,0));
+
+       q= newStmt(mb,batRef,appendRef);
+       q= pushArgument(mb,q,getArg(tbls,0));
+       q= pushStr(mb,q,".trace");
+       k= getArg(q,0);
+
+       q= newStmt(mb,batRef,appendRef);
+       q= pushArgument(mb,q,k);
+       q= pushStr(mb,q,".trace");
+
+       /* build colum defs */
+       cols = newStmt(mb,batRef, newRef);
+       setVarType(mb, getArg(cols,0), newBatType(TYPE_oid, TYPE_str));
+       cols = pushType(mb, cols, TYPE_oid);
+       cols = pushType(mb, cols, TYPE_str);
+       resultset= pushArgument(mb,resultset, getArg(cols,0));
+
+       q= newStmt(mb,batRef,appendRef);
+       q= pushArgument(mb,q,getArg(cols,0));
+       q= pushStr(mb,q,"usec");
+       k= getArg(q,0);
+
+       q= newStmt(mb,batRef,appendRef);
+       q= pushArgument(mb,q, getArg(cols,0));
+       q= pushStr(mb,q,"statement");
+
+       /* build type defs */
+       types = newStmt(mb,batRef, newRef);
+       setVarType(mb, getArg(types,0), newBatType(TYPE_oid, TYPE_str));
+       types = pushType(mb, types, TYPE_oid);
+       types = pushType(mb, types, TYPE_str);
+       resultset= pushArgument(mb,resultset, getArg(types,0));
+
+       q= newStmt(mb,batRef,appendRef);
+       q= pushArgument(mb,q, getArg(types,0));
+       q= pushStr(mb,q,"bigint");
+       k= getArg(q,0);
+
+       q= newStmt(mb,batRef,appendRef);
+       q= pushArgument(mb,q, k);
+       q= pushStr(mb,q,"clob");
+
+       /* build scale defs */
+       clen = newStmt(mb,batRef, newRef);
+       setVarType(mb, getArg(clen,0), newBatType(TYPE_oid, TYPE_int));
+       clen = pushType(mb, clen, TYPE_oid);
+       clen = pushType(mb, clen, TYPE_int);
+       resultset= pushArgument(mb,resultset, getArg(clen,0));
+
+       q= newStmt(mb,batRef,appendRef);
+       q= pushArgument(mb,q, getArg(clen,0));
+       q= pushInt(mb,q,64);
+       k= getArg(q,0);
+
+       q= newStmt(mb,batRef,appendRef);
+       q= pushArgument(mb,q, k);
+       q= pushInt(mb,q,0);
+
+       /* build scale defs */
+       scale = newStmt(mb,batRef, newRef);
+       setVarType(mb, getArg(scale,0), newBatType(TYPE_oid, TYPE_int));
+       scale = pushType(mb, scale, TYPE_oid);
+       scale = pushType(mb, scale, TYPE_int);
+       resultset= pushArgument(mb,resultset, getArg(scale,0));
+
+       q= newStmt(mb,batRef,appendRef);
+       q= pushArgument(mb,q, getArg(scale,0));
+       q= pushInt(mb,q,0);
+       k= getArg(q,0);
+
+       q= newStmt(mb,batRef,appendRef);
+       q= pushArgument(mb, q, k);
+       q= pushInt(mb,q,0);
+
+       /* add the ticks column */
+
+       q = newStmt(mb, profilerRef, "getTrace");
+       q = pushStr(mb, q, putName("usec",4));
+       resultset= pushArgument(mb,resultset, getArg(q,0));
+
+       /* add the stmt column */
+       q = newStmt(mb, profilerRef, "getTrace");
+       q = pushStr(mb, q, putName("stmt",4));
+       resultset= pushArgument(mb,resultset, getArg(q,0));
+
+       pushInstruction(mb,resultset);
+       pushEndInstruction(mb);
+       chkTypes(cntxt->fdout, cntxt->nspace, mb, TRUE);
+}
+
+static str
+SQLrun(Client c, mvc *m){
+       str msg= MAL_SUCCEED;
+       MalBlkPtr old;
+                       
+       // first consider running in debug mode
+       if( m->emod & mod_debug)
+               msg = runMALDebugger(c, c->curprg->def);
+        else{
+               if( m->emod & mod_trace){
+                       c->curprg->def = copyMalBlk(old = c->curprg->def);
+                       SQLsetTrace(c);
+                       msg = runMAL(c, c->curprg->def, 0, 0);
+                       stopTrace(0);
+                       freeMalBlk(c->curprg->def);
+                       c->curprg->def = old;
+               } else
+                       msg = runMAL(c, c->curprg->def, 0, 0);
+       }
+       return msg;
+}
+
 str
 SQLstatementIntern(Client c, str *expr, str nme, bit execute, bit output, 
res_table **result)
 {
@@ -220,13 +368,8 @@ SQLstatementIntern(Client c, str *expr, 
 
                if (!output)
                        sql->out = NULL;        /* no output stream */
-               if (execute){
-                       // first consider running in debug mode
-                       if( m->emod & mod_debug)
-                               msg = runMALDebugger(c, c->curprg->def);
-                        else
-                               msg = runMAL(c, c->curprg->def, 0, 0);
-               }
+               if (execute)
+                       msg = SQLrun(c,m);
 
                MSresetInstructions(c->curprg->def, oldstop);
                freeVariables(c, c->curprg->def, NULL, oldvtop);
@@ -444,15 +587,10 @@ SQLengineIntern(Client c, backend *be)
         * in the context of a user global environment. We have a private
         * environment.
         */
-       if (MALcommentsOnly(c->curprg->def)) {
+       if (MALcommentsOnly(c->curprg->def)) 
                msg = MAL_SUCCEED;
-       } else {
-               // first consider running in debug mode
-               if( m->emod & mod_debug)
-                       msg = runMALDebugger(c, c->curprg->def);
-                else
-                       msg = runMAL(c, c->curprg->def, 0, 0);
-       }
+       else 
+               msg = SQLrun(c,m);
 
 cleanup_engine:
        if (m->type == Q_SCHEMA)
@@ -492,7 +630,7 @@ cleanup_engine:
 
 /* a hook is provided to execute relational algebra expressions */
 str
-RAstatement(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
+RAstatement(Client c, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
 {
        int pos = 0;
        str *expr = getArgReference_str(stk, pci, 1);
@@ -503,46 +641,41 @@ RAstatement(Client cntxt, MalBlkPtr mb, 
        sql_rel *rel;
        list *refs;
 
-       if ((msg = getSQLContext(cntxt, mb, &m, &b)) != NULL)
+       if ((msg = getSQLContext(c, mb, &m, &b)) != NULL)
                return msg;
-       if ((msg = checkSQLContext(cntxt)) != NULL)
+       if ((msg = checkSQLContext(c)) != NULL)
                return msg;
        if (!m->sa)
                m->sa = sa_create();
        refs = sa_list(m->sa);
        rel = rel_read(m, *expr, &pos, refs);
        if (rel) {
-               int oldvtop = cntxt->curprg->def->vtop;
-               int oldstop = cntxt->curprg->def->stop;
+               int oldvtop = c->curprg->def->vtop;
+               int oldstop = c->curprg->def->stop;
                stmt *s;
-               MalStkPtr oldglb = cntxt->glb;
+               MalStkPtr oldglb = c->glb;
 
                if (*opt)
                        rel = rel_optimizer(m, rel);
                s = output_rel_bin(m, rel);
                rel_destroy(rel);
 
-               MSinitClientPrg(cntxt, "user", "test");
+               MSinitClientPrg(c, "user", "test");
 
                /* generate MAL code, ignoring any code generation error */
-               if(  backend_callinline(b, cntxt) < 0 ||
-                        backend_dumpstmt(b, cntxt->curprg->def, s, 1, 1) < 0)
+               if(  backend_callinline(b, c) < 0 ||
+                        backend_dumpstmt(b, c->curprg->def, s, 1, 1) < 0)
                        msg = createException(SQL,"RAstatement","Program 
contains errors");
-               else {
-                       addQueryToCache(cntxt);
-                       // first consider running in debug mode
-                       if( m->emod & mod_debug)
-                               msg = runMALDebugger(cntxt, cntxt->curprg->def);
-                        else
-                               msg = runMAL(cntxt, cntxt->curprg->def, 0, 0);
-                       }
+               else 
+                       addQueryToCache(c);
+                       SQLrun(c,m);
                if (!msg) {
-                       resetMalBlk(cntxt->curprg->def, oldstop);
-                       freeVariables(cntxt, cntxt->curprg->def, NULL, oldvtop);
-                       if( !(cntxt->glb == 0 || cntxt->glb == oldglb))
+                       resetMalBlk(c->curprg->def, oldstop);
+                       freeVariables(c, c->curprg->def, NULL, oldvtop);
+                       if( !(c->glb == 0 || c->glb == oldglb))
                                msg= createException(MAL,"sql","global stack 
leakage"); /* detect leak */
                }
-               cntxt->glb = oldglb;
+               c->glb = oldglb;
        }
        return msg;
 }
diff --git a/sql/backends/monet5/sql_scenario.c 
b/sql/backends/monet5/sql_scenario.c
--- a/sql/backends/monet5/sql_scenario.c
+++ b/sql/backends/monet5/sql_scenario.c
@@ -866,131 +866,6 @@ SQLreader(Client c)
  * A future version may analyze the parameter settings in more detail.
  */
 
-/*
- * The trace operation collects the events in the BATs
- * and creates a secondary result set upon termination
- * of the query. 
- */
-static void
-SQLsetTrace(Client cntxt, mvc *m, bit onoff)
-{
-       InstrPtr q, resultset;
-       InstrPtr tbls, cols, types, clen, scale;
-       MalBlkPtr mb = cntxt->curprg->def;
-       int k;
-
-       if ( m == 0 || !(m->emod & mod_trace))
-               return;
-
-       if (onoff) {
-               q= newStmt(mb, "profiler", "starttrace");
-               q= pushStr(mb,q,"sql_traces");
-               initTrace();
-       } else {
-               q= newStmt(mb, "profiler", "stoptrace");
-               q= pushStr(mb,q,"sql_traces");
-               /* cook a new resultSet instruction */
-               resultset = newInstruction(mb,ASSIGNsymbol);
-               setModuleId(resultset, sqlRef);
-               setFunctionId(resultset, resultSetRef);
-           getArg(resultset,0)= newTmpVariable(mb,TYPE_int);
-
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to