pf_performance_timer.hpp

Go to the documentation of this file.
00001 /* $Id: pf_performance_timer.hpp 21594 2010-12-22 11:24:38Z alberth $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD 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, version 2.
00006  * OpenTTD 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.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #ifndef PF_PERFORMANCE_TIMER_HPP
00013 #define PF_PERFORMANCE_TIMER_HPP
00014 
00015 extern uint64 ottd_rdtsc();
00016 
00017 struct CPerformanceTimer
00018 {
00019   int64    m_start;
00020   int64    m_acc;
00021 
00022   CPerformanceTimer() : m_start(0), m_acc(0) {}
00023 
00024   FORCEINLINE void Start()
00025   {
00026     m_start = QueryTime();
00027   }
00028 
00029   FORCEINLINE void Stop()
00030   {
00031     m_acc += QueryTime() - m_start;
00032   }
00033 
00034   FORCEINLINE int Get(int64 coef)
00035   {
00036     return (int)(m_acc * coef / QueryFrequency());
00037   }
00038 
00039   FORCEINLINE int64 QueryTime()
00040   {
00041     return ottd_rdtsc();
00042   }
00043 
00044   FORCEINLINE int64 QueryFrequency()
00045   {
00046     return ((int64)2200 * 1000000);
00047   }
00048 };
00049 
00050 struct CPerfStartReal
00051 {
00052   CPerformanceTimer *m_pperf;
00053 
00054   FORCEINLINE CPerfStartReal(CPerformanceTimer& perf) : m_pperf(&perf)
00055   {
00056     if (m_pperf != NULL) m_pperf->Start();
00057   }
00058 
00059   FORCEINLINE ~CPerfStartReal()
00060   {
00061     Stop();
00062   }
00063 
00064   FORCEINLINE void Stop()
00065   {
00066     if (m_pperf != NULL) {
00067       m_pperf->Stop();
00068       m_pperf = NULL;
00069     }
00070   }
00071 };
00072 
00073 struct CPerfStartFake
00074 {
00075   FORCEINLINE CPerfStartFake(CPerformanceTimer& perf) {}
00076   FORCEINLINE ~CPerfStartFake() {}
00077   FORCEINLINE void Stop() {}
00078 };
00079 
00080 typedef CPerfStartFake CPerfStart;
00081 
00082 #endif /* PF_PERFORMANCE_TIMER_HPP */