LCOV - code coverage report
Current view: top level - monetdb5/modules/mal - profiler.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 32 60 53.3 %
Date: 2021-01-13 20:07:21 Functions: 9 19 47.4 %

          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             : /*
      10             :  * Martin Kersten
      11             :  * Performance profiler
      12             :  * A key issue in developing fast programs using the Monet database
      13             :  * back-end requires a keen eye on where performance is lost.
      14             :  * Although performance tracking and measurements are highly
      15             :  * application dependent, a simple to use tool makes life
      16             :  * a lot easier.
      17             :  *
      18             :  * Activation of the performance monitor has a global effect,
      19             :  * i.e. all concurrent actions on the kernel are traced,
      20             :  * but the events are only sent to the client initiated
      21             :  * the profiler thread.
      22             :  *
      23             :  * The profiler event can be handled in several ways.
      24             :  * The default strategy is to ship the event record immediately over a stream
      25             :  * to a performance monitor.
      26             :  * An alternative strategy is preparation of off-line performance analysis.
      27             :  *
      28             :  * To reduce the  interference of performance measurement with
      29             :  * the experiments, the user can use an event cache, which is
      30             :  * emptied explicitly upon need.
      31             :  */
      32             : /*
      33             :  * Using the Monet Performance Profiler is constrained by the mal_profiler.
      34             :  */
      35             : #include "monetdb_config.h"
      36             : #include "profiler.h"
      37             : 
      38             : str
      39           0 : CMDopenProfilerStream(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pc)
      40             : {
      41             :         (void) cntxt;
      42             :         (void) mb;
      43             :         (void) stk;
      44             :         (void) pc;
      45           0 :         return openProfilerStream(cntxt);
      46             : }
      47             : 
      48             : str
      49           0 : CMDcloseProfilerStream(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pc)
      50             : {
      51             :         (void) mb;
      52             :         (void) stk;
      53             :         (void) pc;
      54           0 :         return closeProfilerStream(cntxt);
      55             : }
      56             : 
      57             : // initialize SQL tracing
      58             : str
      59           2 : CMDstartProfiler(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pc)
      60             : {
      61             :         (void)mb;
      62             :         (void) stk;
      63             :         (void) pc;
      64             :         (void) cntxt;
      65           2 :         return startProfiler(cntxt);
      66             : }
      67             : 
      68             : str
      69           2 : CMDstopProfiler(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
      70             : {
      71             :         (void) mb;
      72             :         (void) stk;
      73             :         (void) pci;
      74             : 
      75           2 :         return stopProfiler(cntxt);
      76             : }
      77             : 
      78             : // called by the SQL front end optional a directory to keep the traces.
      79             : str
      80           1 : CMDstartTrace(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
      81             : {
      82             :         (void) mb;
      83             :         (void) stk;
      84             :         (void) pci;
      85           1 :         return startTrace(cntxt);
      86             : }
      87             : 
      88             : str
      89           3 : CMDstopTrace(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
      90             : {
      91             :         (void) mb;
      92             :         (void) stk;
      93             :         (void) pci;
      94           3 :         return stopTrace(cntxt);
      95             : }
      96             : 
      97             : str
      98           0 : CMDnoopProfiler(void *res)
      99             : {
     100             :         (void) res;             /* fool compiler */
     101           0 :         return MAL_SUCCEED;
     102             : }
     103             : 
     104             : str
     105           0 : CMDcleanupTraces(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     106             : {
     107             :         (void) mb;
     108             :         (void) stk;
     109             :         (void) pci;
     110           0 :         cleanupTraces(cntxt);
     111           0 :         return MAL_SUCCEED;
     112             : }
     113             : 
     114             : str
     115           4 : CMDgetTrace(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
     116             : {
     117           4 :         str path = *getArgReference_str(stk,pci,1);
     118           4 :         bat *res =  getArgReference_bat(stk,pci,0);
     119             :         BAT *bn;
     120             : 
     121             :         (void) cntxt;           /* fool compiler */
     122             :         (void) mb;
     123           4 :         bn = getTrace(cntxt, path);
     124           4 :         if (bn) {
     125           4 :                 BBPkeepref(*res = bn->batCacheid);
     126           4 :                 return MAL_SUCCEED;
     127             :         }
     128           0 :         throw(MAL, "getTrace", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING  "%s", path);
     129             : }
     130             : 
     131             : str
     132           2 : CMDgetprofilerlimit(int *res)
     133             : {
     134           2 :         *res = getprofilerlimit();
     135           2 :         return MAL_SUCCEED;
     136             : }
     137             : 
     138             : str
     139           0 : CMDsetprofilerlimit(void *res, int *lim)
     140             : {
     141             :         (void) res;
     142           0 :         setprofilerlimit(*lim);
     143           0 :         return MAL_SUCCEED;
     144             : }
     145             : 
     146             : /*
     147             :  * Tracing an active system.
     148             :  */
     149             : 
     150             : str
     151           0 : CMDsetHeartbeat(void *res, int *ev)
     152             : {
     153             :         (void) res;
     154           0 :         setHeartbeat(*ev);
     155           0 :         return MAL_SUCCEED;
     156             : }
     157             : 
     158             : str
     159           0 : CMDgetDiskReads(lng *ret)
     160             : {
     161           0 :         *ret= getDiskReads();
     162           0 :         return MAL_SUCCEED;
     163             : }
     164             : str
     165           0 : CMDgetDiskWrites(lng *ret)
     166             : {
     167           0 :         *ret= getDiskWrites();
     168           0 :         return MAL_SUCCEED;
     169             : }
     170             : str
     171           0 : CMDgetUserTime(lng *ret)
     172             : {
     173           0 :         *ret= getUserTime();
     174           0 :         return MAL_SUCCEED;
     175             : }
     176             : str
     177           0 : CMDgetSystemTime(lng *ret)
     178             : {
     179           0 :         *ret= getUserTime();
     180           0 :         return MAL_SUCCEED;
     181             : }
     182             : 
     183             : str
     184           5 : CMDcpustats(lng *user, lng *nice, lng *sys, lng *idle, lng *iowait)
     185             : {
     186           5 :         profilerGetCPUStat(user,nice,sys,idle,iowait);
     187           5 :         return MAL_SUCCEED;
     188             : }
     189             : 
     190             : str
     191           5 : CMDcpuloadPercentage(int *cycles, int *io, lng *user, lng *nice, lng *sys, lng *idle, lng *iowait)
     192             : {
     193             :         lng userN, niceN, sysN, idleN, iowaitN, N;
     194           5 :         *cycles = 0;
     195           5 :         *io = 0;
     196           5 :         profilerGetCPUStat(&userN,&niceN,&sysN,&idleN,&iowaitN);
     197           5 :         N = (userN - *user + niceN - *nice + sysN - *sys);
     198           5 :         if ( N){
     199           1 :                 *cycles = (int) ( ((double) N) / (N + idleN - *idle + iowaitN - *iowait) *100);
     200           1 :                 *io = (int) ( ((double) iowaitN- *iowait) / (N + idleN - *idle + iowaitN - *iowait) *100);
     201             :         }
     202           5 :         return MAL_SUCCEED;
     203             : }
     204             : 
     205             : #include "mel.h"
     206             : mel_func profiler_init_funcs[] = {
     207             :  pattern("profiler", "start", CMDstartProfiler, false, "Start offline performance profiling", noargs),
     208             :  pattern("profiler", "stop", CMDstopProfiler, false, "Stop offline performance profiling", args(1,1, arg("",void))),
     209             :  pattern("profiler", "starttrace", CMDstartTrace, false, "Start collecting trace information", noargs),
     210             :  pattern("profiler", "stoptrace", CMDstopTrace, false, "Stop collecting trace information", args(1,1, arg("",void))),
     211             :  command("profiler", "setheartbeat", CMDsetHeartbeat, false, "Set heart beat performance tracing", args(1,2, arg("",void),arg("b",int))),
     212             :  command("profiler", "getlimit", CMDgetprofilerlimit, false, "Set profiler limit", args(1,1, arg("",int))),
     213             :  command("profiler", "setlimit", CMDsetprofilerlimit, false, "Get profiler limit", args(1,2, arg("",void),arg("l",int))),
     214             :  pattern("profiler", "openstream", CMDopenProfilerStream, false, "Start profiling the events, send to output stream", args(1,1, arg("",void))),
     215             :  pattern("profiler", "closestream", CMDcloseProfilerStream, false, "Stop offline proviling", args(1,1, arg("",void))),
     216             :  command("profiler", "noop", CMDnoopProfiler, false, "Fetch any pending performance events", args(1,1, arg("",void))),
     217             :  pattern("profiler", "getTrace", CMDgetTrace, false, "Get the trace details of a specific event", args(1,2, batargany("",1),arg("e",str))),
     218             :  pattern("profiler", "cleanup", CMDcleanupTraces, false, "Remove the temporary tables for profiling", args(1,1, arg("",void))),
     219             :  command("profiler", "getDiskReads", CMDgetDiskReads, false, "Obtain the number of physical reads", args(1,1, arg("",lng))),
     220             :  command("profiler", "getDiskWrites", CMDgetDiskWrites, false, "Obtain the number of physical reads", args(1,1, arg("",lng))),
     221             :  command("profiler", "getUserTime", CMDgetUserTime, false, "Obtain the user timing information.", args(1,1, arg("",lng))),
     222             :  command("profiler", "getSystemTime", CMDgetSystemTime, false, "Obtain the user timing information.", args(1,1, arg("",lng))),
     223             :  command("profiler", "cpustats", CMDcpustats, false, "Extract cpu statistics from the kernel", args(5,5, arg("user",lng),arg("nice",lng),arg("sys",lng),arg("idle",lng),arg("iowait",lng))),
     224             :  command("profiler", "cpuload", CMDcpuloadPercentage, false, "Calculate the average cpu load percentage and io waiting times", args(2,7, arg("cycles",int),arg("io",int),arg("user",lng),arg("nice",lng),arg("sys",lng),arg("idle",lng),arg("iowait",lng))),
     225             :  { .imp=NULL }
     226             : };
     227             : #include "mal_import.h"
     228             : #ifdef _MSC_VER
     229             : #undef read
     230             : #pragma section(".CRT$XCU",read)
     231             : #endif
     232         255 : LIB_STARTUP_FUNC(init_profiler_mal)
     233         255 : { mal_module("profiler", NULL, profiler_init_funcs); }

Generated by: LCOV version 1.14