processclock.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //////////////////////////////////////////////////////////////////////////////////
  2. //////////////////////////////////////////////////////////////////////////////////
  3. #include <errno.h>
  4. #include <string.h>
  5. #include "processclock.h"
  6. #include "strutil.h"
  7. //////////////////////////////////////////////////////////////////////////////////
  8. #define _NSEC_PER_USEC _PC_NS_PER_US
  9. #define _NSEC_PER_MSEC (_NSEC_PER_USEC * _PC_US_PER_MS)
  10. #define _NSEC_PER_SEC (_NSEC_PER_MSEC * _PC_MS_PER_S)
  11. #define _NSEC_PER_MIN (_NSEC_PER_SEC * 60LL)
  12. #define _NSEC_PER_HOUR (_NSEC_PER_MIN * 60LL)
  13. //////////////////////////////////////////////////////////////////////////////////
  14. CProcessClock::CProcessClock(bool bInitAtConstr)
  15. {
  16. if(bInitAtConstr)
  17. {
  18. clock_gettime(CLOCK_MONOTONIC, &m_tsDueTime);
  19. m_bIsInit = true;
  20. }
  21. else
  22. {
  23. memset(&m_tsDueTime, 0, sizeof(m_tsDueTime));
  24. m_bIsInit = false;
  25. }
  26. for(int i = 0; i < _PC_NUM_STOPCLOCKS; i++)
  27. {
  28. StopclockReset(i);
  29. }
  30. }
  31. CProcessClock::~CProcessClock(void)
  32. {
  33. }
  34. //////////////////////////////////////////////////////////////////////////////////
  35. pc_time64_t CProcessClock::GetNanoTick(struct timespec *pts)
  36. {
  37. struct timespec ts;
  38. if(!pts)
  39. pts = &ts;
  40. clock_gettime(CLOCK_MONOTONIC, pts);
  41. return Timespec2NanoSec(pts);
  42. }
  43. pc_time64_t CProcessClock::ClockGetElapsed(unsigned int nClockIndex) const
  44. {
  45. if(nClockIndex >= _PC_NUM_STOPCLOCKS)
  46. return -1;
  47. struct timespec tsCur;
  48. clock_gettime(CLOCK_MONOTONIC, &tsCur);
  49. pc_time64_t n1 = Timespec2NanoSec(&m_sc[nClockIndex].tsStopclock);
  50. pc_time64_t n2 = Timespec2NanoSec(&tsCur);
  51. pc_time64_t nElapsed = n2 - n1;
  52. return nElapsed;
  53. }
  54. CProcessClock::SleepResult CProcessClock::NSleep(pc_time64_t nInterval, bool bResumeOnIntr)
  55. {
  56. int nRet;
  57. pc_time64_t dt;
  58. if(!m_bIsInit)
  59. {
  60. clock_gettime(CLOCK_MONOTONIC, &m_tsDueTime);
  61. m_bIsInit = true;
  62. }
  63. dt = IncTime(&m_tsDueTime, nInterval);
  64. if(dt < GetNanoTick())
  65. {
  66. clock_gettime(CLOCK_MONOTONIC, &m_tsDueTime);
  67. return SR_TimerUnderrun;
  68. }
  69. if((nRet = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &m_tsDueTime, NULL)))
  70. {
  71. if(bResumeOnIntr)
  72. {
  73. while(nRet == EINTR)
  74. {
  75. nRet = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &m_tsDueTime, NULL);
  76. }
  77. }
  78. }
  79. return !nRet ? SR_Ok : ((nRet == EINTR) ? SR_Interrupt : SR_Error);
  80. }
  81. std::string CProcessClock::Interval2String(pc_time64_t nInterval)
  82. {
  83. if(nInterval < 0)
  84. return "(error negative value)";
  85. if(nInterval < _NSEC_PER_USEC)
  86. return formatString("%lld ns", nInterval);
  87. else if(nInterval < _NSEC_PER_MSEC)
  88. return formatString("%.1f us", (double)nInterval / (double)_NSEC_PER_USEC);
  89. else if(nInterval < _NSEC_PER_SEC)
  90. return formatString("%.2f ms", (double)nInterval / (double)_NSEC_PER_MSEC);
  91. else if(nInterval < _NSEC_PER_MIN)
  92. return formatString("%.2f sec", (double)nInterval / (double)_NSEC_PER_SEC);
  93. else if(nInterval < _NSEC_PER_HOUR)
  94. return formatString("%.2f min", (double)nInterval / (double)_NSEC_PER_MIN);
  95. else
  96. return formatString("%.2f h", (double)nInterval / (double)_NSEC_PER_HOUR);
  97. }