Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "tier1/reliabletimer.h"
  7. int64 CReliableTimer::sm_nPerformanceFrequency = 0;
  8. bool CReliableTimer::sm_bUseQPC = false;
  9. #ifdef _WIN32
  10. #include "winlite.h"
  11. #endif
  12. //-----------------------------------------------------------------------------
  13. // Purpose: Constructor
  14. //-----------------------------------------------------------------------------
  15. CReliableTimer::CReliableTimer()
  16. {
  17. m_nPerformanceCounterStart = 0;
  18. m_nPerformanceCounterEnd = 0;
  19. m_nPerformanceCounterLimit = 0;
  20. #ifdef _WIN32
  21. // calculate performance frequency the first time we use a timer
  22. if ( 0 == sm_nPerformanceFrequency )
  23. {
  24. // Are we on a bad CPU?
  25. sm_bUseQPC = false; // todo
  26. const CPUInformation &cpu = *GetCPUInformation();
  27. sm_bUseQPC = ( ( 0 == Q_stricmp( cpu.m_szProcessorID, "AuthenticAMD" ) )
  28. && ( cpu.m_nPhysicalProcessors > 1 )
  29. && !cpu.m_bSSE41 );
  30. if ( sm_bUseQPC )
  31. {
  32. LARGE_INTEGER li;
  33. QueryPerformanceFrequency( &li );
  34. sm_nPerformanceFrequency = li.QuadPart;
  35. }
  36. else
  37. {
  38. sm_nPerformanceFrequency = g_ClockSpeed;
  39. }
  40. }
  41. #elif defined(_PS3)
  42. // On PowerPC, the time base register increment frequency is implementation dependent, and doesn't have to be constant.
  43. // On PS3, measured it to be just shy of 80Mhz on the PPU and doesn't seem to change
  44. if ( sm_nPerformanceFrequency == 0 )
  45. sm_nPerformanceFrequency = sys_time_get_timebase_frequency();
  46. #else
  47. // calculate performance frequency the first time we use a timer
  48. if ( 0 == sm_nPerformanceFrequency )
  49. {
  50. sm_nPerformanceFrequency = g_ClockSpeed;
  51. }
  52. #endif
  53. }
  54. //-----------------------------------------------------------------------------
  55. // Purpose: Returns current QueryPerformanceCounter value
  56. //-----------------------------------------------------------------------------
  57. int64 CReliableTimer::GetPerformanceCountNow()
  58. {
  59. //VPROF_BUDGET( "CReliableTimer::GetPerformanceCountNow", VPROF_BUDGETGROUP_OTHER_UNACCOUNTED );
  60. #ifdef _WIN32
  61. if ( sm_bUseQPC )
  62. {
  63. LARGE_INTEGER li = {0};
  64. QueryPerformanceCounter( &li );
  65. return li.QuadPart;
  66. }
  67. else
  68. {
  69. CCycleCount CycleCount;
  70. CycleCount.Sample();
  71. return CycleCount.GetLongCycles();
  72. }
  73. #elif defined( _PS3 )
  74. // use handy macro to grab tb
  75. uint64 ulNow;
  76. SYS_TIMEBASE_GET( ulNow );
  77. return ulNow;
  78. #else
  79. uint64 un64;
  80. __asm__ __volatile__ (
  81. "rdtsc\n\t"
  82. : "=A" (un64) );
  83. return (int64)un64;
  84. #endif
  85. }