LCOV - code coverage report
Current view: top level - monetdb5/optimizer - opt_querylog.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 105 134 78.4 %
Date: 2021-10-13 02:24:04 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /*
       2             :  * This Source Code Form is subject to the terms of the Mozilla Public
       3             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       4             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       5             :  *
       6             :  * Copyright 1997 - July 2008 CWI, August 2008 - 2021 MonetDB B.V.
       7             :  */
       8             : 
       9             : #include "monetdb_config.h"
      10             : #include "opt_querylog.h"
      11             : #include "gdk_time.h"
      12             : #include "querylog.h"
      13             : 
      14             : str
      15      353096 : OPTquerylogImplementation(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
      16             : {
      17             :         int i, limit, slimit, actions = 0;
      18      353096 :         InstrPtr p = 0, *old= mb->stmt, q,r;
      19             :         int argc, io, user,nice,sys,idle,iowait,load, arg, start,finish, name;
      20             :         int xtime=0, rtime = 0, tuples=0;
      21             :         InstrPtr defineQuery = NULL;
      22             :         str msg = MAL_SUCCEED;
      23             : 
      24             : 
      25             :         // query log needed?
      26      353096 :         if ( !QLOGisset() )
      27             :                 return MAL_SUCCEED;
      28             : 
      29             :         (void) stk;             /* to fool compilers */
      30             :         (void) cntxt;
      31             :         /* gather information */
      32         207 :         for (i = 1; i < mb->stop; i++) {
      33         202 :                 p = getInstrPtr(mb,i);
      34         202 :                 if ( getModuleId(p) && idcmp(getModuleId(p), "querylog") == 0 && idcmp(getFunctionId(p),"define")==0){
      35             :                         defineQuery= p;
      36           5 :                         getVarConstant(mb,getArg(p,3)).val.lval = GDKusec()-getVarConstant(mb,getArg(p,3)).val.lval ;
      37             :                 }
      38             :         }
      39           5 :         if ( defineQuery == NULL)
      40             :                 /* nothing to do */
      41             :                 return MAL_SUCCEED;
      42             : 
      43             :         actions++;
      44             :         limit= mb->stop;
      45           5 :         slimit= mb->ssize;
      46           5 :         if ( newMalBlkStmt(mb, mb->ssize) < 0)
      47           0 :                 throw(MAL,"optimizer.querylog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
      48             : 
      49           5 :         pushInstruction(mb, old[0]);
      50             :         /* run the querylog.define operation */
      51           5 :         defineQuery = copyInstruction(defineQuery);
      52           5 :         if( defineQuery == NULL)
      53           0 :                 throw(MAL,"optimizer.querylog", SQLSTATE(HY013) MAL_MALLOC_FAIL);
      54             : 
      55           5 :         defineQuery->argc--;  // remove MAL instruction count
      56           5 :         setFunctionId(defineQuery, appendRef);
      57           5 :         getArg(defineQuery,0) = newTmpVariable(mb,TYPE_any);
      58           5 :         defineQuery->token = ASSIGNsymbol;
      59           5 :         setModuleId(defineQuery,querylogRef);
      60             : 
      61             :         /* collect the initial statistics */
      62           5 :         q = newStmt(mb, "clients", "getUsername");
      63           5 :         name= getArg(q,0)= newVariable(mb,"name",4,TYPE_str);
      64           5 :         defineQuery = addArgument(mb,defineQuery,name);
      65           5 :         q = newStmt(mb, "mtime", "current_timestamp");
      66           5 :         start= getArg(q,0)= newVariable(mb,"start",5,TYPE_timestamp);
      67           5 :         defineQuery = addArgument(mb,defineQuery,start);
      68           5 :         pushInstruction(mb, defineQuery);
      69             : 
      70           5 :         q = newStmtArgs(mb, sqlRef, "argRecord", old[0]->argc);
      71           5 :         for ( argc=1; argc < old[0]->argc; argc++)
      72           0 :                 q = pushArgument(mb, q, getArg(old[0],argc));
      73             : 
      74           5 :         arg= getArg(q,0)= newVariable(mb,"args",4,TYPE_str);
      75             : 
      76             : 
      77           5 :         q = newStmt(mb, "alarm", "usec");
      78           5 :         xtime = getArg(q,0)= newVariable(mb,"xtime",5,TYPE_lng);
      79           5 :         user = newVariable(mb,"user",4,TYPE_lng);
      80           5 :         nice = newVariable(mb,"nice",4,TYPE_lng);
      81           5 :         sys = newVariable(mb,"sys",3,TYPE_lng);
      82           5 :         idle = newVariable(mb,"idle",4,TYPE_lng);
      83           5 :         iowait = newVariable(mb,"iowait",6,TYPE_lng);
      84           5 :         q = newStmt(mb, "profiler", "cpustats");
      85           5 :         q->retc= q->argc =0;
      86           5 :         q = pushReturn(mb,q,user);
      87           5 :         q = pushReturn(mb,q,nice);
      88           5 :         q = pushReturn(mb,q,sys);
      89           5 :         q = pushReturn(mb,q,idle);
      90           5 :         q = pushReturn(mb,q,iowait);
      91           5 :         q = newAssignment(mb);
      92           5 :         tuples= getArg(q,0) = newVariable(mb,"tuples",6,TYPE_lng);
      93           5 :         (void) pushLng(mb,q,1);
      94             : 
      95         207 :         for (i = 1; i < limit; i++) {
      96         202 :                 p = old[i];
      97             : 
      98         202 :                 if (getModuleId(p)==sqlRef &&
      99          11 :                         (idcmp(getFunctionId(p),"exportValue")==0 ||
     100          11 :                          idcmp(getFunctionId(p),"exportResult")==0  ) ) {
     101             : 
     102           0 :                         q = newStmt(mb, "alarm", "usec");
     103           0 :                         r = newStmt(mb, calcRef, "-");
     104           0 :                         r = pushArgument(mb, r, getArg(q,0));
     105           0 :                         r = pushArgument(mb, r, xtime);
     106           0 :                         getArg(r,0)=xtime;
     107             : 
     108           0 :                         q = newStmt(mb, "alarm", "usec");
     109           0 :                         rtime= getArg(q,0)= newVariable(mb,"rtime",5,TYPE_lng);
     110           0 :                         pushInstruction(mb,p);
     111           0 :                         continue;
     112             :                 }
     113         202 :                 if ( getModuleId(p) == sqlRef && getFunctionId(p) == resultSetRef && isaBatType(getVarType(mb,getArg(p,3)))){
     114           3 :                         q = newStmt(mb, "aggr", "count");
     115           3 :                         getArg(q,0) = tuples;
     116           3 :                         (void) pushArgument(mb,q, getArg(p,3));
     117           3 :                         pushInstruction(mb,p);
     118           3 :                         continue;
     119             :                 }
     120         199 :                 if ( p->token== ENDsymbol || p->barrier == RETURNsymbol || p->barrier == YIELDsymbol){
     121           5 :                         if ( rtime == 0){
     122           5 :                                 q = newStmt(mb, "alarm", "usec");
     123           5 :                                 r = newStmt(mb, calcRef, "-");
     124           5 :                                 r = pushArgument(mb, r, getArg(q,0));
     125           5 :                                 r = pushArgument(mb, r, xtime);
     126           5 :                                 getArg(r,0)=xtime;
     127           5 :                                 q = newStmt(mb, "alarm", "usec");
     128           5 :                                 rtime= getArg(q,0)= newVariable(mb,"rtime",5,TYPE_lng);
     129             :                         }
     130           5 :                         q = newStmt(mb, "alarm", "usec");
     131           5 :                         r = newStmt(mb, calcRef, "-");
     132           5 :                         r = pushArgument(mb, r, getArg(q,0));
     133           5 :                         r = pushArgument(mb, r, rtime);
     134           5 :                         getArg(r,0)=rtime;
     135             :                         /*
     136             :                          * Post execution statistics gathering
     137             :                          */
     138           5 :                         q = newStmt(mb, "mtime", "current_timestamp");
     139           5 :                         finish= getArg(q,0)= newVariable(mb,"finish",6,TYPE_any);
     140             : 
     141           5 :                         q = newStmt(mb, "profiler", "cpuload");
     142           5 :                         load = newVariable(mb,"load",4,TYPE_int);
     143           5 :                         getArg(q,0)= load;
     144           5 :                         io = newVariable(mb,"io",2,TYPE_int);
     145           5 :                         q= pushReturn(mb,q,io);
     146           5 :                         q = pushArgument(mb,q,user);
     147           5 :                         q = pushArgument(mb,q,nice);
     148           5 :                         q = pushArgument(mb,q,sys);
     149           5 :                         q = pushArgument(mb,q,idle);
     150           5 :                         q = pushArgument(mb,q,iowait);
     151             : 
     152           5 :                         q = newStmtArgs(mb, querylogRef, "call", 9);
     153           5 :                         q = pushArgument(mb, q, start);
     154           5 :                         q = pushArgument(mb, q, finish);
     155           5 :                         q = pushArgument(mb, q, arg);
     156           5 :                         q = pushArgument(mb, q, tuples);
     157           5 :                         q = pushArgument(mb, q, xtime);
     158           5 :                         q = pushArgument(mb, q, rtime);
     159           5 :                         q = pushArgument(mb, q, load);
     160           5 :                         q = pushArgument(mb, q, io);
     161           5 :                         pushInstruction(mb,p);
     162           5 :                         continue;
     163             :                 }
     164             : 
     165         194 :                 pushInstruction(mb,p);
     166         194 :                 if (p->barrier == YIELDsymbol){
     167             :                         /* the factory yield may return */
     168           0 :                         q = newStmt(mb, "mtime", "current_timestamp");
     169           0 :                         start= getArg(q,0)= newVariable(mb,"start",5,TYPE_any);
     170           0 :                         q = newStmtArgs(mb, sqlRef, "argRecord", old[0]->argc);
     171           0 :                         for ( argc=1; argc < old[0]->argc; argc++)
     172           0 :                                 q = pushArgument(mb, q, getArg(old[0],argc));
     173           0 :                         arg= getArg(q,0)= newVariable(mb,"args",4,TYPE_str);
     174           0 :                         q = newAssignment(mb);
     175           0 :                         q = pushLng(mb,q,0);
     176           0 :                         q = newAssignment(mb);
     177           0 :                         q = pushLng(mb,q,0);
     178           0 :                         tuples= getArg(q,0)= newVariable(mb,"tuples",6,TYPE_lng);
     179           0 :                         newFcnCall(mb,"profiler","setMemoryFlag");
     180           0 :                         q->argc--;
     181           0 :                         pushLng(mb,q,1);
     182           0 :                         q = newStmt(mb, "alarm", "usec");
     183           0 :                         xtime = getArg(q,0)= newVariable(mb,"xtime",5,TYPE_lng);
     184             :                 }
     185             :         }
     186             : 
     187        1078 :         for( ; i<slimit; i++)
     188        1073 :                 if(old[i])
     189           0 :                         pushInstruction(mb, old[i]);
     190           5 :         GDKfree(old);
     191             :         /* Defense line against incorrect plans */
     192           5 :         msg = chkTypes(cntxt->usermodule, mb, FALSE);
     193           5 :         if (!msg)
     194           5 :                 msg = chkFlow(mb);
     195           5 :         if (!msg)
     196           5 :                 msg = chkDeclarations(mb);
     197             :         /* keep actions taken as a fake argument*/
     198           5 :         (void) pushInt(mb, pci, actions);
     199           5 :         return msg;
     200             : }

Generated by: LCOV version 1.14