Changeset: 03397ca1309c for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=03397ca1309c
Added Files:
        monetdb5/mal/mal_runtime.c
        monetdb5/mal/mal_runtime.h
Branch: default
Log Message:

Fork off the performance profile wrappers
Turn it into a little helper class


diffs (252 lines):

diff --git a/monetdb5/mal/mal_runtime.c b/monetdb5/mal/mal_runtime.c
new file mode 100644
--- /dev/null
+++ b/monetdb5/mal/mal_runtime.c
@@ -0,0 +1,198 @@
+/*
+ * The contents of this file are subject to the MonetDB Public License
+ * Version 1.1 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ * http://www.monetdb.org/Legal/MonetDBLicense
+ * 
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific language governing rights and limitations
+ * under the License.
+ * 
+ * The Original Code is the MonetDB Database System.
+ * 
+ * The Initial Developer of the Original Code is CWI.
+ * Portions created by CWI are Copyright (C) 1997-July 2008 CWI.
+ * Copyright August 2008-2011 MonetDB B.V.
+ * All Rights Reserved.
+*/
+
+/* Author(s) M.L. Kersten
+ * The MAL Runtime Profiler
+ * This little helper module is used to perform instruction based profiling.
+*/
+
+#include "monetdb_config.h"
+#include "mal_utils.h"
+#include "mal_runtime.h"
+#include "mal_function.h"
+#include "mal_profiler.h"
+#include "mal_listing.h"
+
+/*
+ * Manage the runtime profiling information
+*/
+void
+runtimeProfileInit(MalBlkPtr mb, RuntimeProfile prof)
+{
+       prof->newclk=0;
+       prof->ppc= -2;
+       prof->tcs = 0;
+       if (malProfileMode ) {
+               setFilterOnBlock(mb, 0, 0);
+               prof->ppc = -1;
+       }
+}
+
+inline void 
+runtimeProfileBegin(Client cntxt, MalBlkPtr mb, MalStkPtr stk, int stkpc, 
RuntimeProfile prof, int start)
+{
+       if( stk && mb->profiler != NULL ){
+               stk->clk= GDKusec();
+               if ( mb->profiler[stkpc].trace) {
+                       mal_set_lock(mal_contextLock, "DFLOWdelay");
+                       gettimeofday(&stk->clock, NULL);
+                       prof->ppc= stkpc;
+                       mb->profiler[stkpc].clk= 0;
+                       mb->profiler[stkpc].ticks= 0;
+                       mb->profiler[stkpc].clock= stk->clock;
+                       /* emit the instruction upon start as well */
+                       if ( start )
+                               profilerEvent(cntxt->idx,mb,stk,stkpc,1);
+#ifdef HAVE_TIMES
+                       times(&stk->timer);
+                       mb->profiler[stkpc].timer= stk->timer;
+#endif
+                       mb->profiler[stkpc].clk= stk->clk;
+                       mal_unset_lock(mal_contextLock, "DFLOWdelay");
+               }
+       }
+}
+
+
+inline void 
+runtimeProfileExit(Client cntxt, MalBlkPtr mb, MalStkPtr stk, int stkpc, 
RuntimeProfile prof)
+{
+       if( malProfileMode == 0)
+               /* mostly true */;
+       else
+       if(stk != NULL && prof->ppc>= 0 && mb->profiler != NULL && 
mb->profiler[stkpc].trace  && mb->profiler[stkpc].clk)
+       {
+               prof->newclk= GDKusec();
+               mb->profiler[stkpc].counter++;
+               mb->profiler[stkpc].ticks = prof->newclk - stk->clk;
+               mb->profiler[stkpc].clk += mb->profiler[stkpc].clk;
+               if( stkpc){
+                       mb->profiler[stkpc].rbytes= 
getVolume(stk,getInstrPtr(mb,stkpc),0);
+                       mb->profiler[stkpc].wbytes= 
getVolume(stk,getInstrPtr(mb,stkpc),1);
+               }
+               profilerEvent(cntxt->idx,mb,stk,stkpc,0);
+               prof->ppc = -1;
+       }
+}
+
+void
+runtimeTiming(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci, int tid 
, MT_Lock *lock, RuntimeProfile prof)
+{
+       str line;
+       if (lock)
+               MT_lock_set(&*lock, "timing");
+       if( cntxt->flags && stk->cmd != 't' && stk->cmd != 'C'){
+               mnstr_printf(cntxt->fdout,"= ");        /* single column 
rendering */
+               if( cntxt->flags & timerFlag){
+                       char buf[32];
+                       snprintf(buf,sizeof(buf),LLFMT,GDKusec()-cntxt->timer);
+                       mnstr_printf(cntxt->fdout, "%8s usec ",buf);
+               }
+               if( cntxt->flags & threadFlag)
+                       mnstr_printf(cntxt->fdout,"@%d ",tid);
+#ifdef HAVE_SYS_RESOURCE_H
+               if( cntxt-> flags & ioFlag){
+                       struct  rusage resource;
+                       getrusage(RUSAGE_SELF, &resource);
+                       mnstr_printf(cntxt->fdout," %3ld R",
+                               resource.ru_inblock);
+                       mnstr_printf(cntxt->fdout," %3ld W ",
+                               resource.ru_oublock);
+               }
+#endif
+               if( cntxt->flags & memoryFlag){
+                       struct Mallinfo memory;
+                       memory= MT_mallinfo();
+                       if( memory.arena)
+                               mnstr_printf(cntxt->fdout," " SZFMT " bytes ",
+                                       (size_t) (memory.arena - 
prof->memory.arena) );
+                       prof->memory.arena = memory.arena;
+               }
+               if( cntxt->flags & flowFlag){
+                       /* calculate the read/write byte flow */
+                       displayVolume(cntxt, getVolume(stk,pci,0));
+                       mnstr_printf(cntxt->fdout,"/");
+                       displayVolume(cntxt, getVolume(stk,pci,1));
+                       mnstr_printf(cntxt->fdout," ");
+               }
+               if( cntxt->flags & bigfootFlag){
+                       displayVolume(cntxt,cntxt->vmfoot);
+                       mnstr_printf(cntxt->fdout,":");
+                       displayVolume(cntxt,cntxt->bigfoot);
+                       mnstr_printf(cntxt->fdout," ");
+               }
+               if (cntxt->flags & cntFlag){
+                       char buf[32];
+                       snprintf(buf, sizeof(buf), OIDFMT , cntxt->cnt);
+                       mnstr_printf(cntxt->fdout,":%6s ", buf);
+               }
+               line= instruction2str(mb, stk, pci, LIST_MAL_DEBUG);
+               if ( line) {
+                       mnstr_printf(cntxt->fdout, " %s\n", line);
+                       GDKfree(line);
+               }
+               if( cntxt->flags & timerFlag)
+                       cntxt->timer = GDKusec();
+       }
+       if (lock)
+                       MT_lock_unset(&*lock, "timing");
+}
+/*
+ * For performance evaluation it is handy to know the
+ * maximal amount of bytes read/written. The actual
+ * amount is harder to guess, because it too much
+ * depends on the operation.
+*/
+lng getVolume(MalStkPtr stk, InstrPtr pci, int rd)
+{
+       int i, limit;
+       lng vol=0;
+       BAT *b;
+       int isview = 0;
+
+       limit = rd == 0?pci->retc:pci->argc;
+       i = rd?pci->retc:0;
+
+       if( stk->stk[getArg(pci,0)].vtype == TYPE_bat){
+               b = BBPquickdesc(stk->stk[getArg(pci,0)].val.bval, TRUE);
+               if (b)
+                       isview = isVIEW(b);
+       }
+       for(; i<limit; i++) {
+               if (stk->stk[getArg(pci,i)].vtype == TYPE_bat){
+                       oid cnt = 0;
+
+                       b = BBPquickdesc(stk->stk[getArg(pci,i)].val.bval, 
TRUE);
+                       if (b==NULL)
+                               continue;
+                       cnt = BATcount(b);
+                       /* Usually reading views cost as much as full bats.
+                          But when we output a slice that is not the case. */
+                       vol += ((rd && !isview) || !VIEWhparent(b)) ? 
headsize(b, cnt) : 0;
+            vol += ((rd && !isview) || !VIEWtparent(b)) ? tailsize(b, cnt) : 0;
+               }
+       }
+       return vol;
+}
+
+void displayVolume(Client cntxt, lng vol){
+       char buf[32];
+       formatVolume(buf,(int)sizeof(buf),vol);
+       mnstr_printf(cntxt->fdout,"%s",buf);
+}
diff --git a/monetdb5/mal/mal_runtime.h b/monetdb5/mal/mal_runtime.h
new file mode 100644
--- /dev/null
+++ b/monetdb5/mal/mal_runtime.h
@@ -0,0 +1,44 @@
+/*
+ * The contents of this file are subject to the MonetDB Public License
+ * Version 1.1 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ * http://www.monetdb.org/Legal/MonetDBLicense
+ * 
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific language governing rights and limitations
+ * under the License.
+ * 
+ * The Original Code is the MonetDB Database System.
+ * 
+ * The Initial Developer of the Original Code is CWI.
+ * Portions created by CWI are Copyright (C) 1997-July 2008 CWI.
+ * Copyright August 2008-2011 MonetDB B.V.
+ * All Rights Reserved.
+*/
+
+#ifndef _MAL_RUNTIME_H
+#define _MAL_RUNTIME_H
+
+#include "mal.h"
+#include "mal_client.h"
+#include "mal_instruction.h"
+
+/* During MAL interpretation we collect performance event data.
+ * Their management is orchestrated from here.
+*/
+typedef struct{
+       lng newclk;
+       int ppc;
+       lng tcs;
+       lng oublock, inblock;
+       struct Mallinfo memory;
+} *RuntimeProfile, RuntimeProfileRecord;
+
+mal_export void runtimeProfileInit(MalBlkPtr mb, RuntimeProfile prof);
+mal_export void runtimeProfileBegin(Client cntxt, MalBlkPtr mb, MalStkPtr stk, 
int stkpc, RuntimeProfile prof, int start);
+mal_export void runtimeProfileExit(Client cntxt, MalBlkPtr mb, MalStkPtr stk, 
int stkpc, RuntimeProfile prof);
+mal_export void runtimeTiming(Client cntxt, MalBlkPtr mb, MalStkPtr stk, 
InstrPtr pci, int tid, MT_Lock *lock, RuntimeProfile prof);
+mal_export lng getVolume(MalStkPtr stk, InstrPtr pci, int rd);
+mal_export void displayVolume(Client cntxt, lng vol);
+#endif
_______________________________________________
Checkin-list mailing list
[email protected]
http://mail.monetdb.org/mailman/listinfo/checkin-list

Reply via email to