[Kde-cygwin-cvs] CVS: tools/profiler profiler.cpp,1.26,1.27
Status: Inactive
Brought to you by:
habacker
From: Ralf H. <hab...@us...> - 2005-10-14 12:59:39
|
Update of /cvsroot/kde-cygwin/tools/profiler In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26942 Modified Files: profiler.cpp Log Message: dos to unix Index: profiler.cpp =================================================================== RCS file: /cvsroot/kde-cygwin/tools/profiler/profiler.cpp,v retrieving revision 1.26 retrieving revision 1.27 diff -u -r1.26 -r1.27 --- profiler.cpp 28 May 2002 06:49:32 -0000 1.26 +++ profiler.cpp 14 Oct 2005 12:59:30 -0000 1.27 @@ -1,327 +1,327 @@ -/* profiler implementation - - Copyright 2002 - Ralf Habacker <Ral...@fr...> - Chris January <ch...@at...> - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - -$Id$ - -*/ - -#if defined(__CYGWIN__) || defined (__MINGW32__) || defined (__WIN32__) -#define USE_WIN32_TIMER -#endif - -#ifdef USE_WIN32_TIMER -#include <windows.h> -#else -#include <sys/time.h> -#endif - - -#include <stdio.h> -#include <string.h> -#include <stdlib.h> -#include <limits.h> -#include <process.h> - -#include "profiler.h" - -// enable this for nanosec printing -//#define PRINT_NSEC - -#ifdef HIGH_RESOLUTION_TIMER - -#ifdef PRINT_NSEC -#define pfactor 1000000000 -#define ptype "llu" -#define punit "[ns]" -#else -#define pfactor 1000000 -#define ptype "llu" -#define punit "[us]" -#endif - -#define MAX_VAL ULONG_LONG_MAX -#else -#define ptype "llu" -#define punit "[ms]" -#define MAX_VAL ULONG_MAX; -#endif - - -namespace Prof { - -// -// basic class for providing a timer -// - -class myTimer { - public: - // constructor - myTimer(void); - - // get the current time of timer in an internal format - inline TimeType getValue(void); - - // result a printable version of timer value <value> - inline TimeType getPrintableValue(TimeType value); - - protected: - TimeType frequency; - TimeType currentValue; -}; - -myTimer::myTimer(void) -{ -#ifdef HIGH_RESOLUTION_TIMER -#ifdef USE_WIN32_TIMER - LARGE_INTEGER liFrequency; - QueryPerformanceFrequency((LARGE_INTEGER *)&liFrequency); - frequency = liFrequency.QuadPart; -#else -#endif -#endif -} - -TimeType myTimer::getValue(void) { -#ifdef HIGH_RESOLUTION_TIMER -#ifdef USE_WIN32_TIMER - QueryPerformanceCounter((LARGE_INTEGER *)¤tValue); -#else - struct timeval val; - gettimeofday(&val, (struct timezone*)0); - currentValue = (TimeType )val.tv_sec * 1000000 + val.tv_usec; -#endif -#else - currentValue = GetTickCount(); -#endif - return currentValue; -} - -TimeType myTimer::getPrintableValue(TimeType value) -{ -#ifdef HIGH_RESOLUTION_TIMER -#ifdef USE_WIN32_TIMER - return value * pfactor / frequency; -#else - return value; -#endif -#else - return value; -#endif -} - -static myTimer timer; - - -ProfilerList::ProfilerList() -{ - // init array - ProfilerItem *p = &ItemList[0]; - for (int i = 0; i < ProfilerItem_Count; i++) { - p->scope = 0; - p++; - } - - // handle environment - env = ::getenv("PROFILER"); - - // set default pointer is no value - if (!env) - env = ""; - - // save initial time stamp if enabled - if (strstr(env,"startup") != 0) - StartupTime = new Profiler("AppStartup"); - -// char *s; - - // set output stream to file if set - if (char *s = strstr(env,"out=")) { - out = fopen(s+4,"at+"); -// fprintf(out,"'%s' started\n",env); - } - if (!out) - out = stderr; -} - -void ProfilerList::SetProfilerReady(void) -{ - static bool lock = false; - if (!StartupTime || lock) - return; - if (!lock) { - StartupTime->Stop(); - lock = true; - } -} - -bool ProfilerList::Add(const char *scope, TimeType difftime, int _count) -{ - ProfilerItem *p = &ItemList[0]; - for (int i = 0; i < ProfilerItem_Count; i++) { - // new entry - if (!p->scope) { - p->scope = scope; - p->mintime = MAX_VAL; - p->maxtime = 0; - p->count = 0; - p->sumtime = 0; - UpdateTime(p, difftime, _count); - return true; - } - // entry found - else if (p->scope == scope) { - UpdateTime(p, difftime, _count); - return true; - } - p++; - } - return false; -} - -void ProfilerList::UpdateTime(ProfilerItem *p, TimeType difftime, int count) -{ - if (p->mintime > difftime) - p->mintime = difftime; - if (p->maxtime < difftime) - p->maxtime = difftime; - p->count+= count; - p->sumtime += difftime; -} - -void ProfilerList::Print(void) -{ - ProfilerItem *p = &ItemList[0]; - - for (int i = 0; i < ProfilerItem_Count; i++) { - if (p->scope) { -#ifdef HIGH_RESOLUTION_TIMER - fprintf(out,"[%4d] %-30s n:%6d (min) %10llu (max) %10llu (avg) %10llu (sum) %10llu %s\n", -#else - fprintf(out,"[%4d] %-30s n:%6d (min) %10d (max) %10d (avg) %10d (sum) %10d %s\n", -#endif - ::getpid(), - p->scope, p->count, - timer.getPrintableValue(p->mintime), - timer.getPrintableValue(p->maxtime), - timer.getPrintableValue(p->sumtime)/p->count, - timer.getPrintableValue(p->sumtime), - punit); - } - p++; - } -} - -void ProfilerList::Reset(void) -{ - ProfilerItem *p = &ItemList[0]; - for (int i = 0; i < ProfilerItem_Count; i++) { - p->scope = NULL; - p->mintime = MAX_VAL; - p->maxtime = 0; - p->count = 0; - p->sumtime = 0; - p++; - } -} - -ProfilerList::~ProfilerList() -{ - if (env || (out && out != stderr)) - Print(); - - // close output - fclose(out); -} - -// global object -ProfilerList theProfiler; - -#ifdef NO_COUNT -Profiler::Profiler(const char *s,const char *akey) -{ - Init(s,1,akey); -} -#else -Profiler::Profiler(const char *s, const int _count, const char *akey) -{ - Init(s,_count,akey); -} -#endif - -inline void Profiler::Init(const char *s, const int _count, const char *akey) -{ - // notify ProfilerList, that we has been called - theProfiler.SetProfilerReady(); - - key = akey; - - if (strstr(theProfiler.getenv(),"all") != 0) - printSingleLine = true; - else - printSingleLine = false; - starttime = 0; - if (_count) - Start(s,_count); -} - -void Profiler::Restart(const char *s, const int _count) -{ - if (starttime) - Stop(); - Start(s,_count); -} - -void Profiler::Start(const char *s, const int _count) -{ - if (starttime) - Stop(); - count = _count; - if (s) - scope = s; - if (scope) - starttime = timer.getValue(); -} - -void Profiler::Stop(void) -{ - TimeType endtime = timer.getValue(); - TimeType difftime = endtime-starttime; - - if (starttime) { - if (printSingleLine) { -#ifdef HIGH_RESOLUTION_TIMER - fprintf(theProfiler.getout(),"[%4d] %-30s time: %10llu %s\n", -#else - fprintf(theProfiler.getout(),"[%4d] %-30s time: %10d %s\n", -#endif - ::getpid(),scope,timer.getPrintableValue(difftime/count),punit); - - } - theProfiler.Add(scope,difftime/count,count); - starttime = 0; - } -} - -Profiler::~Profiler() -{ - Stop(); -} - -} +/* profiler implementation + + Copyright 2002 + Ralf Habacker <Ral...@fr...> + Chris January <ch...@at...> + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +$Id$ + +*/ + +#if defined(__CYGWIN__) || defined (__MINGW32__) || defined (__WIN32__) +#define USE_WIN32_TIMER +#endif + +#ifdef USE_WIN32_TIMER +#include <windows.h> +#else +#include <sys/time.h> +#endif + + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <limits.h> +#include <process.h> + +#include "profiler.h" + +// enable this for nanosec printing +//#define PRINT_NSEC + +#ifdef HIGH_RESOLUTION_TIMER + +#ifdef PRINT_NSEC +#define pfactor 1000000000 +#define ptype "llu" +#define punit "[ns]" +#else +#define pfactor 1000000 +#define ptype "llu" +#define punit "[us]" +#endif + +#define MAX_VAL ULONG_LONG_MAX +#else +#define ptype "llu" +#define punit "[ms]" +#define MAX_VAL ULONG_MAX; +#endif + + +namespace Prof { + +// +// basic class for providing a timer +// + +class myTimer { + public: + // constructor + myTimer(void); + + // get the current time of timer in an internal format + inline TimeType getValue(void); + + // result a printable version of timer value <value> + inline TimeType getPrintableValue(TimeType value); + + protected: + TimeType frequency; + TimeType currentValue; +}; + +myTimer::myTimer(void) +{ +#ifdef HIGH_RESOLUTION_TIMER +#ifdef USE_WIN32_TIMER + LARGE_INTEGER liFrequency; + QueryPerformanceFrequency((LARGE_INTEGER *)&liFrequency); + frequency = liFrequency.QuadPart; +#else +#endif +#endif +} + +TimeType myTimer::getValue(void) { +#ifdef HIGH_RESOLUTION_TIMER +#ifdef USE_WIN32_TIMER + QueryPerformanceCounter((LARGE_INTEGER *)¤tValue); +#else + struct timeval val; + gettimeofday(&val, (struct timezone*)0); + currentValue = (TimeType )val.tv_sec * 1000000 + val.tv_usec; +#endif +#else + currentValue = GetTickCount(); +#endif + return currentValue; +} + +TimeType myTimer::getPrintableValue(TimeType value) +{ +#ifdef HIGH_RESOLUTION_TIMER +#ifdef USE_WIN32_TIMER + return value * pfactor / frequency; +#else + return value; +#endif +#else + return value; +#endif +} + +static myTimer timer; + + +ProfilerList::ProfilerList() +{ + // init array + ProfilerItem *p = &ItemList[0]; + for (int i = 0; i < ProfilerItem_Count; i++) { + p->scope = 0; + p++; + } + + // handle environment + env = ::getenv("PROFILER"); + + // set default pointer is no value + if (!env) + env = ""; + + // save initial time stamp if enabled + if (strstr(env,"startup") != 0) + StartupTime = new Profiler("AppStartup"); + +// char *s; + + // set output stream to file if set + if (char *s = strstr(env,"out=")) { + out = fopen(s+4,"at+"); +// fprintf(out,"'%s' started\n",env); + } + if (!out) + out = stderr; +} + +void ProfilerList::SetProfilerReady(void) +{ + static bool lock = false; + if (!StartupTime || lock) + return; + if (!lock) { + StartupTime->Stop(); + lock = true; + } +} + +bool ProfilerList::Add(const char *scope, TimeType difftime, int _count) +{ + ProfilerItem *p = &ItemList[0]; + for (int i = 0; i < ProfilerItem_Count; i++) { + // new entry + if (!p->scope) { + p->scope = scope; + p->mintime = MAX_VAL; + p->maxtime = 0; + p->count = 0; + p->sumtime = 0; + UpdateTime(p, difftime, _count); + return true; + } + // entry found + else if (p->scope == scope) { + UpdateTime(p, difftime, _count); + return true; + } + p++; + } + return false; +} + +void ProfilerList::UpdateTime(ProfilerItem *p, TimeType difftime, int count) +{ + if (p->mintime > difftime) + p->mintime = difftime; + if (p->maxtime < difftime) + p->maxtime = difftime; + p->count+= count; + p->sumtime += difftime; +} + +void ProfilerList::Print(void) +{ + ProfilerItem *p = &ItemList[0]; + + for (int i = 0; i < ProfilerItem_Count; i++) { + if (p->scope) { +#ifdef HIGH_RESOLUTION_TIMER + fprintf(out,"[%4d] %-30s n:%6d (min) %10llu (max) %10llu (avg) %10llu (sum) %10llu %s\n", +#else + fprintf(out,"[%4d] %-30s n:%6d (min) %10d (max) %10d (avg) %10d (sum) %10d %s\n", +#endif + ::getpid(), + p->scope, p->count, + timer.getPrintableValue(p->mintime), + timer.getPrintableValue(p->maxtime), + timer.getPrintableValue(p->sumtime)/p->count, + timer.getPrintableValue(p->sumtime), + punit); + } + p++; + } +} + +void ProfilerList::Reset(void) +{ + ProfilerItem *p = &ItemList[0]; + for (int i = 0; i < ProfilerItem_Count; i++) { + p->scope = NULL; + p->mintime = MAX_VAL; + p->maxtime = 0; + p->count = 0; + p->sumtime = 0; + p++; + } +} + +ProfilerList::~ProfilerList() +{ + if (env || (out && out != stderr)) + Print(); + + // close output + fclose(out); +} + +// global object +ProfilerList theProfiler; + +#ifdef NO_COUNT +Profiler::Profiler(const char *s,const char *akey) +{ + Init(s,1,akey); +} +#else +Profiler::Profiler(const char *s, const int _count, const char *akey) +{ + Init(s,_count,akey); +} +#endif + +inline void Profiler::Init(const char *s, const int _count, const char *akey) +{ + // notify ProfilerList, that we has been called + theProfiler.SetProfilerReady(); + + key = akey; + + if (strstr(theProfiler.getenv(),"all") != 0) + printSingleLine = true; + else + printSingleLine = false; + starttime = 0; + if (_count) + Start(s,_count); +} + +void Profiler::Restart(const char *s, const int _count) +{ + if (starttime) + Stop(); + Start(s,_count); +} + +void Profiler::Start(const char *s, const int _count) +{ + if (starttime) + Stop(); + count = _count; + if (s) + scope = s; + if (scope) + starttime = timer.getValue(); +} + +void Profiler::Stop(void) +{ + TimeType endtime = timer.getValue(); + TimeType difftime = endtime-starttime; + + if (starttime) { + if (printSingleLine) { +#ifdef HIGH_RESOLUTION_TIMER + fprintf(theProfiler.getout(),"[%4d] %-30s time: %10llu %s\n", +#else + fprintf(theProfiler.getout(),"[%4d] %-30s time: %10d %s\n", +#endif + ::getpid(),scope,timer.getPrintableValue(difftime/count),punit); + + } + theProfiler.Add(scope,difftime/count,count); + starttime = 0; + } +} + +Profiler::~Profiler() +{ + Stop(); +} + +} |