2 * Copyright (C) 2005-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
22 #include "PerformanceSample.h"
25 #include "linux/PlatformInclude.h"
28 #include "Application.h"
30 #include "TimeUtils.h"
34 int64_t CPerformanceSample::m_tmFreq;
36 CPerformanceSample::CPerformanceSample(const string &statName, bool bCheckWhenDone)
38 m_statName = statName;
39 m_bCheckWhenDone = bCheckWhenDone;
41 m_tmFreq = CurrentHostFrequency();
46 CPerformanceSample::~CPerformanceSample()
52 void CPerformanceSample::Reset()
54 m_tmStart = CurrentHostCounter();
56 if (getrusage(RUSAGE_SELF, &m_usage) == -1)
57 CLog::Log(LOGERROR,"error %d in getrusage", errno);
61 void CPerformanceSample::CheckPoint()
63 #ifdef HAS_PERFORMANCE_SAMPLE
65 tmNow = CurrentHostCounter();
66 double elapsed = (double)(tmNow - m_tmStart) / (double)m_tmFreq.QuadPart;
68 double dUser=0.0, dSys=0.0;
71 if (getrusage(RUSAGE_SELF, &usage) == -1)
72 CLog::Log(LOGERROR,"error %d in getrusage", errno);
75 dUser = ( ((double)usage.ru_utime.tv_sec + (double)usage.ru_utime.tv_usec / 1000000.0) -
76 ((double)m_usage.ru_utime.tv_sec + (double)m_usage.ru_utime.tv_usec / 1000000.0) );
77 dSys = ( ((double)usage.ru_stime.tv_sec + (double)usage.ru_stime.tv_usec / 1000000.0) -
78 ((double)m_usage.ru_stime.tv_sec + (double)m_usage.ru_stime.tv_usec / 1000000.0) );
82 g_application.GetPerformanceStats().AddSample(m_statName, PerformanceCounter(elapsed,dUser,dSys));
88 double CPerformanceSample::GetEstimatedError()
91 m_tmFreq = CurrentHostFrequency();
93 int64_t tmStart, tmEnd;
94 tmStart = CurrentHostCounter();
96 for (int i=0; i<100000;i++)
98 DECLARE_UNUSED(int64_t,tmDummy);
99 tmDummy = CurrentHostCounter();
102 tmEnd = CurrentHostCounter();
103 double elapsed = (double)(tmEnd - tmStart) / (double)m_tmFreq;
105 return (elapsed / 100000.0) * 2.0; // one measure at start time and another when done.