forked from mpollice/AmdMsrTweaker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWorker.cpp
289 lines (241 loc) · 6.63 KB
/
Worker.cpp
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
* Copyright (c) Martin Kinkelin
*
* See the "License.txt" file in the root directory for infos
* about permitted and prohibited uses of this code.
*/
#include <algorithm>
#include <iostream>
#include <locale>
#include "Worker.h"
#include "StringUtils.h"
#include "WinRing0.h"
using std::cerr;
using std::endl;
using std::min;
using std::max;
using std::string;
using std::tolower;
using std::vector;
static void SplitPair(string& left, string& right, const string& str, char delimiter)
{
const size_t i = str.find(delimiter);
left = str.substr(0, i);
if (i == string::npos)
right.clear();
else
right = str.substr(i + 1);
}
bool Worker::ParseParams(int argc, const char* argv[])
{
const Info& info = *_info;
PStateInfo psi;
psi.Multi = psi.VID = psi.NBVID = -1;
psi.NBPState = -1;
NBPStateInfo nbpsi;
nbpsi.Multi = 1.0;
nbpsi.VID = -1;
for (int i = 0; i < info.NumPStates; i++)
{
_pStates.push_back(psi);
_pStates.back().Index = i;
}
for (int i = 0; i < info.NumNBPStates; i++)
{
_nbPStates.push_back(nbpsi);
_nbPStates.back().Index = i;
}
for (int i = 1; i < argc; i++)
{
const string param(argv[i]);
string key, value;
SplitPair(key, value, param, '=');
if (value.empty())
{
if (param.length() >= 2 && tolower(param[0]) == 'p')
{
const int index = atoi(param.c_str() + 1);
if (index >= 0 && index < info.NumPStates)
{
_pState = index;
continue;
}
}
}
else
{
if (key.length() >= 2 && tolower(key[0]) == 'p')
{
const int index = atoi(key.c_str() + 1);
if (index >= 0 && index < info.NumPStates)
{
string multi, vid;
SplitPair(multi, vid, value, '@');
if (!multi.empty())
_pStates[index].Multi = info.multiScaleFactor * atof(multi.c_str());
if (!vid.empty())
_pStates[index].VID = info.EncodeVID(atof(vid.c_str()));
continue;
}
}
if (key.length() >= 5 && _strnicmp(key.c_str(), "NB_P", 4) == 0)
{
const int index = atoi(key.c_str() + 4);
if (index >= 0 && index < info.NumNBPStates)
{
string multi, vid;
SplitPair(multi, vid, value, '@');
if (!multi.empty())
_nbPStates[index].Multi = atof(multi.c_str());
if (!vid.empty())
_nbPStates[index].VID = info.EncodeVID(atof(vid.c_str()));
continue;
}
}
if (_stricmp(key.c_str(), "NB_low") == 0)
{
const int index = atoi(value.c_str());
int j = 0;
for (; j < min(index, info.NumPStates); j++)
_pStates[j].NBPState = 0;
for (; j < info.NumPStates; j++)
_pStates[j].NBPState = 1;
continue;
}
if (_stricmp(key.c_str(), "Turbo") == 0)
{
const int flag = atoi(value.c_str());
if (flag == 0 || flag == 1)
{
_turbo = flag;
continue;
}
}
if( _stricmp( key.c_str(), "BoostEnAllCores" ) == 0 )
{
const int flag = atoi( value.c_str() );
if( flag == 0 || flag == 1 )
{
_boostEnAllCores = flag;
continue;
}
}
if( _stricmp( key.c_str(), "IgnoreBoostThresh" ) == 0 )
{
const int flag = atoi( value.c_str() );
if( flag == 0 || flag == 1 )
{
_ignoreBoostThresh = flag;
continue;
}
}
if (_stricmp(key.c_str(), "APM") == 0)
{
const int flag = atoi(value.c_str());
if (flag == 0 || flag == 1)
{
_apm = flag;
continue;
}
}
if (_stricmp(key.c_str(), "NbPsi0Vid") == 0)
{
if (!value.empty())
_NbPsi0Vid_VID = info.EncodeVID(atof(value.c_str()));
continue;
}
}
cerr << "ERROR: invalid parameter " << param.c_str() << endl;
return false;
}
return true;
}
static bool ContainsChanges(const PStateInfo& info)
{
return (info.Multi >= 0 || info.VID >= 0 || info.NBVID >= 0 || info.NBPState >= 0);
}
static bool ContainsChanges(const NBPStateInfo& info)
{
return (info.Multi >= 0 || info.VID >= 0);
}
static void SwitchTo(int logicalCPUIndex)
{
const HANDLE hThread = GetCurrentThread();
SetThreadAffinityMask(hThread, (DWORD_PTR)1 << logicalCPUIndex);
}
void Worker::ApplyChanges()
{
const Info& info = *_info;
if (info.Family == 0x15)
{
for (int i = 0; i < _nbPStates.size(); i++)
{
const NBPStateInfo& nbpsi = _nbPStates[i];
if (ContainsChanges(nbpsi))
info.WriteNBPState(nbpsi);
}
}
else if (info.Family == 0x10 && (_nbPStates[0].VID >= 0 || _nbPStates[1].VID >= 0))
{
for (int i = 0; i < _pStates.size(); i++)
{
PStateInfo& psi = _pStates[i];
const int nbPState = (psi.NBPState >= 0 ? psi.NBPState :
info.ReadPState(i).NBPState);
const NBPStateInfo& nbpsi = _nbPStates[nbPState];
if (nbpsi.VID >= 0)
psi.NBVID = nbpsi.VID;
}
}
if (_turbo >= 0 && info.IsBoostSupported)
info.SetBoostSource(_turbo == 1);
if( _boostEnAllCores >= 0 && info.BoostEnAllCores != -1 )
info.SetBoostEnAllCores( _boostEnAllCores );
if( _ignoreBoostThresh >= 0 && info.IgnoreBoostThresh != -1 )
info.SetIgnoreBoostThresh( _ignoreBoostThresh );
if (_apm >= 0 && info.Family == 0x15)
info.SetAPM(_apm == 1);
if (_NbPsi0Vid_VID >= 0 && info.Family == 0x15)
info.WriteNbPsi0Vid(_NbPsi0Vid_VID);
SYSTEM_INFO sysInfo;
GetSystemInfo(&sysInfo);
const int numLogicalCPUs = sysInfo.dwNumberOfProcessors;
// switch to the highest thread priority (we do not want to get interrupted often)
const HANDLE hProcess = GetCurrentProcess();
const HANDLE hThread = GetCurrentThread();
SetPriorityClass(hProcess, REALTIME_PRIORITY_CLASS);
SetThreadPriority(hThread, THREAD_PRIORITY_HIGHEST);
// perform one iteration in each logical core
for (int j = 0; j < numLogicalCPUs; j++)
{
SwitchTo(j);
for (int i = 0; i < _pStates.size(); i++)
{
const PStateInfo& psi = _pStates[i];
if (ContainsChanges(psi))
info.WritePState(psi);
}
if (_turbo >= 0 && info.IsBoostSupported)
info.SetCPBDis(_turbo == 1);
}
for (int j = 0; j < numLogicalCPUs; j++)
{
SwitchTo(j);
const int currentPState = info.GetCurrentPState();
const int newPState = (_pState >= 0 ? _pState : currentPState);
if (newPState != currentPState)
info.SetCurrentPState(newPState);
else
{
if (ContainsChanges(_pStates[currentPState]))
{
const int tempPState = (currentPState == info.NumPStates - 1 ? 0 : info.NumPStates - 1);
info.SetCurrentPState(tempPState);
Sleep(1);
info.SetCurrentPState(currentPState);
}
}
}
SetThreadPriority(hThread, THREAD_PRIORITY_NORMAL);
SetPriorityClass(hProcess, NORMAL_PRIORITY_CLASS);
}