head 1.2; branch ; access ; symbols Version_2_1:1.2; locks ; strict; comment @ * @; 1.2 date 90.08.18.16.10.48; author sp; state Exp; branches ; next 1.1; 1.1 date 90.08.14.19.57.29; author sp; state Exp; branches ; next ; desc @Routines that can be used to time postgres (or non postgres!) code... @ 1.2 log @added some debugginh coed + bug fixes. @ text @/*==================================================================== * * FILE: * testtimer.c * * IDENTIFICATION: * $Header: RCS/testtimer.c,v 1.1 90/08/14 19:57:29 sp Exp $ * * These are some routines that can be used to time postgres. * * initTimer(): * Initialize the timer. The timer starts running... * (can be called more than once...) * * double getTimer(): * get the number of ellapsed seconds since `initTimer()' * * stopTimer(): * stop temporarily the timer * * restartTimer(): * restart the timer after a 'stopTimer()' call. * * * *==================================================================== */ #include #include #include /* #define DEBUG */ #define MILLION 1000000L static void zeroTimeVal(); static void copyTimeVal(); static void addTimeVal(); static void subTimeVal(); static void getTimeVal(); static double timeValToDouble(); static void printTimeVal(); static int TimerIsRunning = 0; static struct timeval StartTimeVal; static struct timeval ElapsedTimeSinceStop; /*---------------------------------------------------- * * zeroTimeVal * */ static void zeroTimeVal(x) struct timeval *x; { x->tv_sec = 0L; x->tv_usec = 0L; } /*---------------------------------------------------- * * copyTimeVal * */ static void copyTimeVal(src, dest) struct timeval src; struct timeval *dest; { dest->tv_sec = src.tv_sec; dest->tv_usec = src.tv_usec; } /*---------------------------------------------------- * * addTimeVal * * add 2 timeval structs. */ static void addTimeVal(v1, v2, res) struct timeval v1; struct timeval v2; struct timeval *res; { long usecs; #ifdef DEBUG printf("addTimeVal: "); printTimeVal(v1); printf("+"); printTimeVal(v2); printf("="); #endif DEBUG usecs = v1.tv_usec + v2.tv_usec; res->tv_usec = usecs % MILLION; res->tv_sec = v1.tv_sec + v2.tv_sec + usecs/MILLION; #ifdef DEBUG printTimeVal(*res); printf("\n"); #endif DEBUG } /*---------------------------------------------------- * * subTimeVal * * subtract 2 timeval structs. */ static void subTimeVal(v1, v2, res) struct timeval v1; struct timeval v2; struct timeval *res; { long borrow; #ifdef DEBUG printf("subTimeVal: "); printTimeVal(v1); printf("-"); printTimeVal(v2); printf("="); #endif DEBUG if (v1.tv_usec >= v2.tv_usec) { res->tv_usec = v1.tv_usec - v2.tv_usec; borrow = 0L; } else { res->tv_usec = v1.tv_usec - v2.tv_usec + MILLION; borrow = 1L; } res->tv_sec = v1.tv_sec - v2.tv_sec - borrow; #ifdef DEBUG printTimeVal(*res); printf("\n"); #endif DEBUG } /*------------------------------------------------------ * * getTimeVal * */ static void getTimeVal(res) struct timeval *res; { struct rusage x, y; struct timeval self, children; #ifdef DEBUG printf("getTimeVal----in\n"); #endif DEBUG getrusage(RUSAGE_SELF, &x); getrusage(RUSAGE_CHILDREN, &y); addTimeVal(x.ru_utime, x.ru_stime, &self); addTimeVal(y.ru_utime, y.ru_stime, &children); addTimeVal(self, children, res); #ifdef DEBUG printf("getTimeVal----out, res:"); printTimeVal(*res); printf("\n"); #endif DEBUG } /*------------------------------------------------------ * * timeValToDouble */ static double timeValToDouble(t) struct timeval t; { double res; res = (double) t.tv_sec + ((double) t.tv_usec) / ((double)MILLION); #ifdef DEBUG printf("timeValToDouble: struct="); printTimeVal(t); printf(", double=%.6f\n", res); #endif DEBUG return(res); } /*------------------------------------------------------ * * printTimeVal */ static void printTimeVal(t) struct timeval t; { printf("[%ld,%ld]", t.tv_sec, t.tv_usec); } /*------------------------------------------------------ * * initTimer * */ initTimer() { #ifdef DEBUG printf("initTimer--------in\n"); #endif DEBUG getTimeVal(&StartTimeVal); zeroTimeVal(&ElapsedTimeSinceStop); TimerIsRunning = 1; #ifdef DEBUG printf("initTimer--------out\n"); #endif DEBUG } /*------------------------------------------------------ * * getTimer * */ double getTimer() { struct timeval now, res; double d; #ifdef DEBUG printf("getTimer--------in\n"); #endif DEBUG if (TimerIsRunning) { getTimeVal(&now); subTimeVal(now, StartTimeVal, &res); addTimeVal(ElapsedTimeSinceStop, res, &res); } else { copyTimeVal(ElapsedTimeSinceStop, &res); } d = timeValToDouble(res); #ifdef DEBUG printf("getTimer--------out, ret=%.6f\n", d); #endif DEBUG return(d); } /*------------------------------------------------------ * * stopTimer * */ stopTimer() { struct timeval now, interval; getTimeVal(&now); TimerIsRunning = 0; subTimeVal(now, StartTimeVal, &interval); addTimeVal(ElapsedTimeSinceStop, interval, &ElapsedTimeSinceStop); } /*------------------------------------------------------ * * restartTimer * */ restartTimer() { getTimeVal(&StartTimeVal); TimerIsRunning = 1; } @ 1.1 log @Initial revision @ text @d7 1 a7 1 * $Header: $ d33 1 d42 1 d90 5 d98 4 d119 5 d131 5 a135 1 res->tv_sec = v1.tv_sec - v2.tv_sec + borrow; d151 4 d161 5 d180 5 d190 12 d207 3 d213 3 d229 4 d242 5 @