[cosmetics] update date in GPL header
[vuplus_xbmc] / xbmc / utils / PerformanceSample.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://www.xbmc.org
4  *
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)
8  *  any later version.
9  *
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.
14  *
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/>.
18  *
19  */
20
21 #include "system.h"
22 #include "PerformanceSample.h"
23
24 #ifdef _LINUX
25 #include "linux/PlatformInclude.h"
26 #endif
27
28 #include "Application.h"
29 #include "log.h"
30 #include "TimeUtils.h"
31
32 using namespace std;
33
34 int64_t CPerformanceSample::m_tmFreq;
35
36 CPerformanceSample::CPerformanceSample(const string &statName, bool bCheckWhenDone)
37 {
38   m_statName = statName;
39   m_bCheckWhenDone = bCheckWhenDone;
40   if (m_tmFreq == 0LL)
41     m_tmFreq = CurrentHostFrequency();
42
43   Reset();
44 }
45
46 CPerformanceSample::~CPerformanceSample()
47 {
48   if (m_bCheckWhenDone)
49     CheckPoint();
50 }
51
52 void CPerformanceSample::Reset()
53 {
54   m_tmStart = CurrentHostCounter();
55 #ifdef _LINUX
56   if (getrusage(RUSAGE_SELF, &m_usage) == -1)
57     CLog::Log(LOGERROR,"error %d in getrusage", errno);
58 #endif
59 }
60
61 void CPerformanceSample::CheckPoint()
62 {
63 #ifdef HAS_PERFORMANCE_SAMPLE
64   int64_t tmNow;
65   tmNow = CurrentHostCounter();
66   double elapsed = (double)(tmNow - m_tmStart) / (double)m_tmFreq.QuadPart;
67
68   double dUser=0.0, dSys=0.0;
69 #ifdef _LINUX
70   struct rusage usage;
71   if (getrusage(RUSAGE_SELF, &usage) == -1)
72     CLog::Log(LOGERROR,"error %d in getrusage", errno);
73   else
74   {
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) );
79   }
80 #endif
81
82   g_application.GetPerformanceStats().AddSample(m_statName, PerformanceCounter(elapsed,dUser,dSys));
83 #endif
84
85   Reset();
86 }
87
88 double CPerformanceSample::GetEstimatedError()
89 {
90   if (m_tmFreq == 0LL)
91     m_tmFreq = CurrentHostFrequency();
92
93   int64_t tmStart, tmEnd;
94   tmStart = CurrentHostCounter();
95
96   for (int i=0; i<100000;i++)
97   {
98     DECLARE_UNUSED(int64_t,tmDummy);
99     tmDummy = CurrentHostCounter();
100   }
101
102   tmEnd = CurrentHostCounter();
103   double elapsed = (double)(tmEnd - tmStart) / (double)m_tmFreq;
104
105   return (elapsed / 100000.0) * 2.0; // one measure at start time and another when done.
106 }
107
108
109
110