Counter Strike : Global Offensive Source Code
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.

211 lines
4.3 KiB

  1. //===== Copyright � 1996-2005, 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. #ifdef COMPILER_MSVC64
  24. #define RDTSC(var) (var = __rdtsc())
  25. #else
  26. #define RDTSC(var) \
  27. _asm RDTSC \
  28. _asm mov DWORD PTR var,eax \
  29. _asm mov DWORD PTR var+4,edx
  30. #endif
  31. // RDPMC Instruction macro
  32. #ifdef COMPILER_MSVC64
  33. #define RDPMC(counter, var) (var = __readpmc(counter))
  34. #else
  35. #define RDPMC(counter, var) \
  36. _asm mov ecx, counter \
  37. _asm RDPMC \
  38. _asm mov DWORD PTR var,eax \
  39. _asm mov DWORD PTR var+4,edx
  40. #endif
  41. // RDPMC Instruction macro, for performance counter 1 (ecx = 1)
  42. #ifdef COMPILER_MSVC64
  43. #define RDPMC0(var) (var = __readpmc(0))
  44. #else
  45. #define RDPMC0(var) \
  46. _asm mov ecx, 0 \
  47. _asm RDPMC \
  48. _asm mov DWORD PTR var,eax \
  49. _asm mov DWORD PTR var+4,edx
  50. #endif
  51. #ifdef COMPILER_MSVC64
  52. #define RDPMC1(var) (var = __readpmc(1))
  53. #else
  54. #define RDPMC1(var) \
  55. _asm mov ecx, 1 \
  56. _asm RDPMC \
  57. _asm mov DWORD PTR var,eax \
  58. _asm mov DWORD PTR var+4,edx
  59. #endif
  60. #define EVENT_TYPE(mode) EventType##mode
  61. #define EVENT_MASK(mode) EventMask##mode
  62. #include "ia32detect.h"
  63. enum ProcessPriority
  64. {
  65. ProcessPriorityNormal,
  66. ProcessPriorityHigh,
  67. };
  68. enum PrivilegeCapture
  69. {
  70. OS_Only, // ring 0, kernel level
  71. USR_Only, // app level
  72. OS_and_USR, // all levels
  73. };
  74. enum CompareMethod
  75. {
  76. CompareGreater, //
  77. CompareLessEqual, //
  78. };
  79. enum EdgeState
  80. {
  81. RisingEdgeDisabled, //
  82. RisingEdgeEnabled, //
  83. };
  84. enum CompareState
  85. {
  86. CompareDisable, //
  87. CompareEnable, //
  88. };
  89. // Singletion Class
  90. class PME : public ia32detect
  91. {
  92. public:
  93. //private:
  94. static PME* _singleton;
  95. HANDLE hFile;
  96. bool bDriverOpen;
  97. double m_CPUClockSpeed;
  98. //ia32detect detect;
  99. HRESULT Init();
  100. HRESULT Close();
  101. protected:
  102. PME()
  103. {
  104. hFile = NULL;
  105. bDriverOpen = FALSE;
  106. m_CPUClockSpeed = 0;
  107. Init();
  108. }
  109. public:
  110. static PME* Instance(); // gives back a real object
  111. ~PME()
  112. {
  113. Close();
  114. }
  115. double GetCPUClockSpeedSlow( void );
  116. double GetCPUClockSpeedFast( void );
  117. HRESULT SelectP5P6PerformanceEvent( uint32 dw_event, uint32 dw_counter, bool b_user, bool b_kernel );
  118. HRESULT ReadMSR( uint32 dw_reg, int64 * pi64_value );
  119. HRESULT ReadMSR( uint32 dw_reg, uint64 * pi64_value );
  120. HRESULT WriteMSR( uint32 dw_reg, const int64 & i64_value );
  121. HRESULT WriteMSR( uint32 dw_reg, const uint64 & i64_value );
  122. void SetProcessPriority( ProcessPriority priority )
  123. {
  124. switch( priority )
  125. {
  126. case ProcessPriorityNormal:
  127. {
  128. SetPriorityClass (GetCurrentProcess(),NORMAL_PRIORITY_CLASS);
  129. SetThreadPriority (GetCurrentThread(),THREAD_PRIORITY_NORMAL);
  130. break;
  131. }
  132. case ProcessPriorityHigh:
  133. {
  134. SetPriorityClass (GetCurrentProcess(),REALTIME_PRIORITY_CLASS);
  135. SetThreadPriority (GetCurrentThread(),THREAD_PRIORITY_HIGHEST);
  136. break;
  137. }
  138. }
  139. }
  140. //---------------------------------------------------------------------------
  141. // Return the family of the processor
  142. //---------------------------------------------------------------------------
  143. CPUVendor GetVendor(void)
  144. {
  145. return vendor;
  146. }
  147. int GetProcessorFamily(void)
  148. {
  149. return version.Family;
  150. }
  151. #ifdef DBGFLAG_VALIDATE
  152. void Validate( CValidator &validator, tchar *pchName ); // Validate our internal structures
  153. #endif // DBGFLAG_VALIDATE
  154. };
  155. #include "p5p6performancecounters.h"
  156. #include "p4performancecounters.h"
  157. #include "k8performancecounters.h"
  158. enum PerfErrors
  159. {
  160. E_UNKNOWN_CPU_VENDOR = -1,
  161. E_BAD_COUNTER = -2,
  162. E_UNKNOWN_CPU = -3,
  163. E_CANT_OPEN_DRIVER = -4,
  164. E_DRIVER_ALREADY_OPEN = -5,
  165. E_DRIVER_NOT_OPEN = -6,
  166. E_DISABLED = -7,
  167. E_BAD_DATA = -8,
  168. E_CANT_CLOSE = -9,
  169. E_ILLEGAL_OPERATION = -10,
  170. };
  171. #pragma warning( pop )
  172. #endif // PMELIB_H