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