PageRenderTime 17ms CodeModel.GetById 10ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/xbmc/utils/PerformanceSample.cpp

http://github.com/xbmc/xbmc
C++ | 110 lines | 70 code | 21 blank | 19 comment | 10 complexity | 83e0d55bc83d7ae4082a450fd2edb682 MD5 | raw file
  1/*
  2 *      Copyright (C) 2005-2013 Team XBMC
  3 *      http://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#include "TimeUtils.h"
 24
 25#ifdef TARGET_POSIX
 26#include "linux/PlatformInclude.h"
 27#include "log.h"
 28#endif
 29
 30#ifdef HAS_PERFORMANCE_SAMPLE
 31#include "Application.h"
 32#include "PerformanceStats.h"
 33#endif
 34
 35int64_t CPerformanceSample::m_tmFreq;
 36
 37CPerformanceSample::CPerformanceSample(const std::string &statName, bool bCheckWhenDone) : m_statName(statName)
 38{
 39  m_bCheckWhenDone = bCheckWhenDone;
 40  if (m_tmFreq == 0LL)
 41    m_tmFreq = CurrentHostFrequency();
 42
 43  Reset();
 44}
 45
 46CPerformanceSample::~CPerformanceSample()
 47{
 48  if (m_bCheckWhenDone)
 49    CheckPoint();
 50}
 51
 52void CPerformanceSample::Reset()
 53{
 54  m_tmStart = CurrentHostCounter();
 55#ifdef TARGET_POSIX
 56  if (getrusage(RUSAGE_SELF, &m_usage) == -1)
 57    CLog::Log(LOGERROR,"error %d in getrusage", errno);
 58#endif
 59}
 60
 61void 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;
 67
 68  double dUser=0.0, dSys=0.0;
 69#ifdef TARGET_POSIX
 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
 88double 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