/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. #include "system.h"
  21. #include "PerformanceSample.h"
  22. #include "TimeUtils.h"
  23. #ifdef TARGET_POSIX
  24. #include "linux/PlatformInclude.h"
  25. #include "log.h"
  26. #endif
  27. #ifdef HAS_PERFORMANCE_SAMPLE
  28. #include "Application.h"
  29. #include "PerformanceStats.h"
  30. #endif
  31. int64_t CPerformanceSample::m_tmFreq;
  32. CPerformanceSample::CPerformanceSample(const std::string &statName, bool bCheckWhenDone) : m_statName(statName)
  33. {
  34. m_bCheckWhenDone = bCheckWhenDone;
  35. if (m_tmFreq == 0LL)
  36. m_tmFreq = CurrentHostFrequency();
  37. Reset();
  38. }
  39. CPerformanceSample::~CPerformanceSample()
  40. {
  41. if (m_bCheckWhenDone)
  42. CheckPoint();
  43. }
  44. void CPerformanceSample::Reset()
  45. {
  46. m_tmStart = CurrentHostCounter();
  47. #ifdef TARGET_POSIX
  48. if (getrusage(RUSAGE_SELF, &m_usage) == -1)
  49. CLog::Log(LOGERROR,"error %d in getrusage", errno);
  50. #endif
  51. }
  52. void CPerformanceSample::CheckPoint()
  53. {
  54. #ifdef HAS_PERFORMANCE_SAMPLE
  55. int64_t tmNow;
  56. tmNow = CurrentHostCounter();
  57. double elapsed = (double)(tmNow - m_tmStart) / (double)m_tmFreq;
  58. double dUser=0.0, dSys=0.0;
  59. #ifdef TARGET_POSIX
  60. struct rusage usage;
  61. if (getrusage(RUSAGE_SELF, &usage) == -1)
  62. CLog::Log(LOGERROR,"error %d in getrusage", errno);
  63. else
  64. {
  65. dUser = ( ((double)usage.ru_utime.tv_sec + (double)usage.ru_utime.tv_usec / 1000000.0) -
  66. ((double)m_usage.ru_utime.tv_sec + (double)m_usage.ru_utime.tv_usec / 1000000.0) );
  67. dSys = ( ((double)usage.ru_stime.tv_sec + (double)usage.ru_stime.tv_usec / 1000000.0) -
  68. ((double)m_usage.ru_stime.tv_sec + (double)m_usage.ru_stime.tv_usec / 1000000.0) );
  69. }
  70. #endif
  71. g_application.GetPerformanceStats().AddSample(m_statName, PerformanceCounter(elapsed,dUser,dSys));
  72. #endif
  73. Reset();
  74. }
  75. double CPerformanceSample::GetEstimatedError()
  76. {
  77. if (m_tmFreq == 0LL)
  78. m_tmFreq = CurrentHostFrequency();
  79. int64_t tmStart, tmEnd;
  80. tmStart = CurrentHostCounter();
  81. for (int i=0; i<100000;i++)
  82. {
  83. DECLARE_UNUSED(int64_t,tmDummy);
  84. tmDummy = CurrentHostCounter();
  85. }
  86. tmEnd = CurrentHostCounter();
  87. double elapsed = (double)(tmEnd - tmStart) / (double)m_tmFreq;
  88. return (elapsed / 100000.0) * 2.0; // one measure at start time and another when done.
  89. }