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.

191 lines
3.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #ifndef PMELIB_H
  9. #define PMELIB_H
  10. #include "Windows.h"
  11. #include "tier0/platform.h"
  12. // Get rid of a bunch of STL warnings!
  13. #pragma warning( push, 3 )
  14. #pragma warning( disable : 4018 )
  15. #define VERSION "1.0.2"
  16. // uncomment this list to add some runtime checks
  17. //#define PME_DEBUG
  18. #include "tier0/valve_off.h"
  19. #include <string>
  20. #include "tier0/valve_on.h"
  21. using namespace std;
  22. // RDTSC Instruction macro
  23. #define RDTSC(var) var = __rdtsc()
  24. // RDPMC Instruction macro
  25. #define RDPMC(counter, var) \
  26. _asm mov ecx, counter \
  27. _asm RDPMC \
  28. _asm mov DWORD PTR var,eax \
  29. _asm mov DWORD PTR var+4,edx
  30. // RDPMC Instruction macro, for performance counter 1 (ecx = 1)
  31. #define RDPMC0(var) \
  32. _asm mov ecx, 0 \
  33. _asm RDPMC \
  34. _asm mov DWORD PTR var,eax \
  35. _asm mov DWORD PTR var+4,edx
  36. #define RDPMC1(var) \
  37. _asm mov ecx, 1 \
  38. _asm RDPMC \
  39. _asm mov DWORD PTR var,eax \
  40. _asm mov DWORD PTR var+4,edx
  41. #define EVENT_TYPE(mode) EventType##mode
  42. #define EVENT_MASK(mode) EventMask##mode
  43. #include "ia32detect.h"
  44. enum ProcessPriority
  45. {
  46. ProcessPriorityNormal,
  47. ProcessPriorityHigh,
  48. };
  49. enum PrivilegeCapture
  50. {
  51. OS_Only, // ring 0, kernel level
  52. USR_Only, // app level
  53. OS_and_USR, // all levels
  54. };
  55. enum CompareMethod
  56. {
  57. CompareGreater, //
  58. CompareLessEqual, //
  59. };
  60. enum EdgeState
  61. {
  62. RisingEdgeDisabled, //
  63. RisingEdgeEnabled, //
  64. };
  65. enum CompareState
  66. {
  67. CompareDisable, //
  68. CompareEnable, //
  69. };
  70. // Singletion Class
  71. class PME : public ia32detect
  72. {
  73. public:
  74. //private:
  75. static PME* _singleton;
  76. HANDLE hFile;
  77. bool bDriverOpen;
  78. double m_CPUClockSpeed;
  79. //ia32detect detect;
  80. HRESULT Init();
  81. HRESULT Close();
  82. protected:
  83. PME()
  84. {
  85. hFile = NULL;
  86. bDriverOpen = FALSE;
  87. m_CPUClockSpeed = 0;
  88. Init();
  89. }
  90. public:
  91. static PME* Instance(); // gives back a real object
  92. ~PME()
  93. {
  94. Close();
  95. }
  96. double GetCPUClockSpeedSlow( void );
  97. double GetCPUClockSpeedFast( void );
  98. HRESULT SelectP5P6PerformanceEvent( uint32 dw_event, uint32 dw_counter, bool b_user, bool b_kernel );
  99. HRESULT ReadMSR( uint32 dw_reg, int64 * pi64_value );
  100. HRESULT ReadMSR( uint32 dw_reg, uint64 * pi64_value );
  101. HRESULT WriteMSR( uint32 dw_reg, const int64 & i64_value );
  102. HRESULT WriteMSR( uint32 dw_reg, const uint64 & i64_value );
  103. void SetProcessPriority( ProcessPriority priority )
  104. {
  105. switch( priority )
  106. {
  107. case ProcessPriorityNormal:
  108. {
  109. SetPriorityClass (GetCurrentProcess(),NORMAL_PRIORITY_CLASS);
  110. SetThreadPriority (GetCurrentThread(),THREAD_PRIORITY_NORMAL);
  111. break;
  112. }
  113. case ProcessPriorityHigh:
  114. {
  115. SetPriorityClass (GetCurrentProcess(),REALTIME_PRIORITY_CLASS);
  116. SetThreadPriority (GetCurrentThread(),THREAD_PRIORITY_HIGHEST);
  117. break;
  118. }
  119. }
  120. }
  121. //---------------------------------------------------------------------------
  122. // Return the family of the processor
  123. //---------------------------------------------------------------------------
  124. CPUVendor GetVendor(void)
  125. {
  126. return vendor;
  127. }
  128. int GetProcessorFamily(void)
  129. {
  130. return version.Family;
  131. }
  132. #ifdef DBGFLAG_VALIDATE
  133. void Validate( CValidator &validator, tchar *pchName ); // Validate our internal structures
  134. #endif // DBGFLAG_VALIDATE
  135. };
  136. #include "P5P6PerformanceCounters.h"
  137. #include "P4PerformanceCounters.h"
  138. #include "K8PerformanceCounters.h"
  139. enum PerfErrors
  140. {
  141. E_UNKNOWN_CPU_VENDOR = -1,
  142. E_BAD_COUNTER = -2,
  143. E_UNKNOWN_CPU = -3,
  144. E_CANT_OPEN_DRIVER = -4,
  145. E_DRIVER_ALREADY_OPEN = -5,
  146. E_DRIVER_NOT_OPEN = -6,
  147. E_DISABLED = -7,
  148. E_BAD_DATA = -8,
  149. E_CANT_CLOSE = -9,
  150. E_ILLEGAL_OPERATION = -10,
  151. };
  152. #pragma warning( pop )
  153. #endif // PMELIB_H