forked from MWisBest/AmdMsrTweaker
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Worker.cpp
352 lines (304 loc) · 8.37 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
* 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 <chrono>
#include <iostream>
#include <locale>
#include <thread>
#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;
#ifdef _DEBUG
unsigned short sleepDelay = 0;
string sleepText = ", waiting for " + std::to_string(sleepDelay) + "seconds.";
#endif
// Apply NB P-states
#ifdef _DEBUG
cerr << "Applying NB P-states" << sleepText << endl;
std::this_thread::sleep_for(std::chrono::seconds(sleepDelay));
#endif
//Changing NB P-states causes system hang on Carrizo (Model == 0x60), disable for now
if (info.Family == 0x15 && info.Model != 0x60)
{
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;
}
}
#ifdef _DEBUG
if (_nbPStates.size() > 0 && (info.Family == 0x15 && info.Model != 0x60))
{
cerr << "NB P-states successfully applied" << endl;
}
else if (info.Family == 0x15 && info.Model == 0x60)
{
cerr << "Modifying P-states on Carrizo is disabled for now (causes system hang)" << endl;
}
else
{
cerr << "No P-states applied (non were specified)" << endl;
}
#endif
// Applying turbo
#ifdef _DEBUG
cerr << "Configuring turbo and APM (if supported)" << sleepText << endl;
std::this_thread::sleep_for(std::chrono::seconds(sleepDelay));
#endif
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)
{
#ifdef _DEBUG
cerr << "Writing NbPsi0Vid" << sleepText << endl;
std::this_thread::sleep_for(std::chrono::seconds(sleepDelay));
#endif
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);
// Write P-states, perform one iteration in each logical core
#ifdef _DEBUG
if (_pStates.size() > 0)
{
cerr << "Writing P-states" << sleepText << endl;
std::this_thread::sleep_for(std::chrono::seconds(sleepDelay));
}
#endif
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);
}
// Set P-states, perform one iteration in each logical core
#ifdef _DEBUG
if (ContainsChanges(_pStates[info.GetCurrentPState()]))
{
cerr << "Settings P-states" << sleepText << endl;
std::this_thread::sleep_for(std::chrono::seconds(sleepDelay));
}
#endif
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);
#ifdef _DEBUG
cerr << "Successfully executed all steps, exiting in 5 seconds" << endl;
std::this_thread::sleep_for(std::chrono::seconds(5));
#endif
}