-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerHintSession.cpp
More file actions
435 lines (386 loc) · 14.3 KB
/
PowerHintSession.cpp
File metadata and controls
435 lines (386 loc) · 14.3 KB
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
* Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause-Clear
*/
#include "PowerHintSession.h"
#include "hint-data.h"
#include "performance.h"
#include "utils.h"
#include <cutils/properties.h>
#include <dlfcn.h>
#include <cmath>
#define PERF_EXT_LIB_PROP "ro.vendor.extension_library"
#define ADPF_DEBUG_PROP "vendor.debug.enable.adpf"
#define PROP_VAL_LENGTH 92
#define CPU_BOOST_HINT 0x0000104E
#define MAX_THREADS 16
#define MAX_BOOST 200
#define MIN_BOOST -200
#define SCHED_TASK_LOAD_BOOST 0x43C04000
#define MAX_TLB_THREADS 5
#define MAX_TLB_PCT 80
#define MIN_TLB_PCT -80
#define INC_TLB_PCT 10
#define DEC_TLB_PCT -10
#include <android-base/logging.h>
#include "android/binder_auto_utils.h"
#define LOG_TAG "QTI PowerHAL"
std::unordered_map<PowerHintSessionImpl*, int32_t> mPowerHintSessions;
std::mutex mSessionLock;
static int validateBoost(int boostVal, int boostSum) {
boostSum += boostVal;
if (boostSum > MAX_BOOST)
return MAX_BOOST;
else if (boostSum < MIN_BOOST)
return MIN_BOOST;
return boostSum;
}
void PowerHintSessionImpl::resetBoost() {
if (mHandle > 0) {
release_request(mHandle);
if (mDebug) LOG(INFO) << "Handle " << mHandle << " released";
}
mHandle = -1;
mLastAction = LOAD_RESET;
}
static std::string printThreads(const std::vector<int32_t>& threadIds, int numThreads) {
std::string str;
for (int i = 0; i < numThreads; i++) {
str = str + std::to_string(threadIds[i]) + " ";
}
return str;
}
bool PowerHintSessionImpl::perfBoost(int boostVal, int hintType) {
int tBoostSum = mBoostSum;
int mHandlePerfHint = -1;
if (hintType == LOAD_RESET) {
resetBoost();
return true;
}
if (hintType == LOAD_RESUME && mLastAction != LOAD_RESET) {
tBoostSum = 0;
}
tBoostSum = validateBoost(boostVal, tBoostSum);
if (tBoostSum != 0) {
mHandlePerfHint = perf_hint_enable(CPU_BOOST_HINT, tBoostSum);
if (mHandlePerfHint < 0) {
LOG(ERROR) << "Unable to acquire Perf hint for" << CPU_BOOST_HINT;
return false;
}
}
if (mHandle > 0) {
release_request(mHandle);
if (mDebug) LOG(INFO) << "Handle " << mHandle << " released";
}
mBoostSum = tBoostSum;
mHandle = mHandlePerfHint;
mLastAction = hintType;
return true;
}
bool PowerHintSessionImpl::taskLoadBoost(int loadType) {
int boostSum = mTLBoostSum;
int handle = -1;
if (loadType == LOAD_RESET) {
if (mTLBHandle > 0) {
release_request(mTLBHandle);
if (mDebug) LOG(INFO) << "Handle " << mTLBHandle << " released";
}
mTLBHandle = -1;
mTLBoostSum = 0;
return true;
}
// Calculate boost sum
if (loadType == LOAD_UP) {
boostSum += INC_TLB_PCT;
if (boostSum > MAX_TLB_PCT) {
boostSum = MAX_TLB_PCT;
}
} else if (loadType == LOAD_DOWN) {
boostSum += DEC_TLB_PCT;
if (boostSum < MIN_TLB_PCT) {
boostSum = MIN_TLB_PCT;
}
}
if (mTLBHandle > 0) {
// If no change in boost, keep handle
if (boostSum == mTLBoostSum) {
return true;
}
// Release old boost
release_request(mTLBHandle);
if (mDebug) LOG(INFO) << "Handle " << mTLBHandle << " released";
}
mTLBHandle = -1;
// Acquire new boost
if (boostSum != 0) {
int numThreads =
(mThreadIds.size() > MAX_TLB_THREADS) ? MAX_TLB_THREADS : mThreadIds.size();
int size = numThreads * 2;
int list[size];
for (int i = 0; i < size; i += 2) {
list[i] = SCHED_TASK_LOAD_BOOST;
list[i + 1] = (std::abs(boostSum) & 0x7F) | ((boostSum < 0) ? 0x80 : 0x00) |
(mThreadIds[i / 2] << 8);
}
handle = interaction_with_handle(handle, 0, size, list);
if (handle < 0) {
LOG(ERROR) << "Unable to apply boost";
// Do not update boost sum if handle is not acquired
return false;
}
if (mDebug)
LOG(INFO) << "Handle " << handle << " for threads "
<< printThreads(mThreadIds, numThreads);
}
mTLBHandle = handle;
mTLBoostSum = boostSum;
return true;
}
bool isSessionAlive(PowerHintSessionImpl* session) {
if (mPowerHintSessions.find(session) != mPowerHintSessions.end()) return true;
return false;
}
bool isSessionActive(PowerHintSessionImpl* session) {
if (!isSessionAlive(session)) return false;
if (mPowerHintSessions[session] == 1) return true;
return false;
}
std::shared_ptr<aidl::android::hardware::power::IPowerHintSession> setPowerHintSession(
int32_t tgid, int32_t uid, const std::vector<int32_t>& threadIds, int64_t durationNanos) {
LOG(INFO) << "setPowerHintSession ";
std::shared_ptr<aidl::android::hardware::power::IPowerHintSession> mPowerSession =
ndk::SharedRefBase::make<PowerHintSessionImpl>(tgid, uid, threadIds, durationNanos);
if (mPowerSession == nullptr) {
return nullptr;
}
return mPowerSession;
}
int64_t getSessionPreferredRate() {
return 16666666L;
}
void setSessionActivity(PowerHintSessionImpl* session, bool flag) {
std::lock_guard<std::mutex> mLockGuard(mSessionLock);
if (flag)
mPowerHintSessions[session] = 1;
else
mPowerHintSessions[session] = 0;
}
void PowerHintSessionImpl::getPerfProperties() {
char perfClientLib[PROPERTY_VALUE_MAX] = {0};
if (!property_get(PERF_EXT_LIB_PROP, perfClientLib, nullptr)) {
LOG(ERROR) << "Failed to get property " << PERF_EXT_LIB_PROP;
return;
}
void* perfClientLibHandle = dlopen(perfClientLib, RTLD_NOW);
if (!perfClientLibHandle) {
LOG(ERROR) << "Unable to open " << perfClientLib << ": " << dlerror();
return;
}
int (*perfGetPropExtn)(const char*, char*, size_t, const char*) = nullptr;
perfGetPropExtn = (int (*)(const char*, char*, size_t, const char*))dlsym(perfClientLibHandle,
"perf_get_prop_extn");
if (!perfGetPropExtn) {
LOG(ERROR) << "Unable to find perf_get_prop_extn function in " << perfClientLib << ": "
<< dlerror();
} else {
char debugEnable[PROP_VAL_LENGTH] = {0};
perfGetPropExtn(ADPF_DEBUG_PROP, debugEnable, PROP_VAL_LENGTH, "0");
mDebug = atoi(debugEnable) == 1;
}
dlclose(perfClientLibHandle);
}
PowerHintSessionImpl::PowerHintSessionImpl(int32_t tgid, int32_t uid,
const std::vector<int32_t>& threadIds,
int64_t durationNanos) {
mUid = uid;
mTgid = tgid;
mHandle = -1;
mBoostSum = 0;
mLastAction = -1;
mThreadIds = threadIds;
mNumPowerEfficiencyThreads = 0;
mPowerEfficiencyMode = false;
mDebug = false;
getPerfProperties();
mTargetWorkDurationNanos = -1;
mThresholdNanos = -1;
mConsecutiveDownCount = 0;
mTLBHandle = -1;
mTLBoostSum = 0;
mIsTopAppGame = false;
updateTargetWorkDuration(durationNanos); // mTargetWorkDurationNanos, mThresholdNanos
setSessionActivity(this, true);
}
PowerHintSessionImpl::~PowerHintSessionImpl() {
close();
}
void PowerHintSessionImpl::releaseLowCpuUtil() {
for (int i = 0; i < mNumPowerEfficiencyThreads; i++) {
if (mThreadIds[i] == 0) continue;
// TODO: Relegate(EngineHints::EH_HIGH_CPUUTIL, mTopAppName, mThreadIds[i], 0);
if (mDebug) LOG(INFO) << "Released low cpu util hint for thread " << mThreadIds[i];
}
mNumPowerEfficiencyThreads = 0;
}
void PowerHintSessionImpl::hintLowCpuUtil() {
int count = 0;
for (auto it = mThreadIds.begin(); it != mThreadIds.end() && count < MAX_THREADS; ++it) {
if (*it == 0) continue;
// TODO: Relegate(EngineHints::EH_HIGH_CPUUTIL, mTopAppName, *it, 2);
if (mDebug) LOG(INFO) << "Set thread " << *it << " to prefer power efficiency";
++count;
}
mNumPowerEfficiencyThreads = count;
}
double PowerHintSessionImpl::nextSupportedFPS(double fps) {
const std::vector<double> supportedFPS = {30.0, 60.0, 90.0, 120.0, 144.0};
auto it = std::lower_bound(supportedFPS.begin(), supportedFPS.end(), fps);
if (it == supportedFPS.end()) {
it = supportedFPS.end() - 1;
}
if (mDebug) LOG(INFO) << "Supported FPS: " << *it << " for requested FPS: " << fps;
return *it;
}
ndk::ScopedAStatus PowerHintSessionImpl::updateTargetWorkDuration(int64_t in_targetDurationNanos) {
// TODO: top app is game check
LOG(INFO) << "PowerHintSessionImpl::updateTargetWorkDuration: " << in_targetDurationNanos;
if (in_targetDurationNanos <= 0) {
LOG(ERROR) << "Invalid target work duration";
return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
}
mTargetWorkDurationNanos = in_targetDurationNanos;
mThresholdNanos = mTargetWorkDurationNanos - (mTargetWorkDurationNanos / 8);
double durationInSeconds = static_cast<double>(mTargetWorkDurationNanos) / 1'000'000'000.0;
double calculatedFps = 1.0 / durationInSeconds;
double supportedFps = nextSupportedFPS(calculatedFps);
// TODO: Relegate(EngineHints::EH_RENDER_RATE, mTopAppName, supportedFps, 1);
// TODO: TFPS
return ndk::ScopedAStatus::ok();
}
ndk::ScopedAStatus PowerHintSessionImpl::reportActualWorkDuration(
const std::vector<::aidl::android::hardware::power::WorkDuration>& in_durations) {
// TODO: top app is game check
LOG(INFO) << "PowerHintSessionImpl::reportActualWorkDuration: ";
int64_t targetWorkDurationNanos = mTargetWorkDurationNanos;
if (targetWorkDurationNanos == -1 || in_durations.empty()) {
return ndk::ScopedAStatus::ok();
}
int64_t actualWorkDurationNanos = in_durations[0].durationNanos;
for (const auto& duration : in_durations) {
if (duration.durationNanos > actualWorkDurationNanos) {
actualWorkDurationNanos = duration.durationNanos;
}
}
if (mDebug)
LOG(INFO) << "actual = " << actualWorkDurationNanos
<< " ns, target = " << targetWorkDurationNanos
<< " ns, last_boost = " << mTLBoostSum;
if (actualWorkDurationNanos >= mThresholdNanos) {
taskLoadBoost(LOAD_UP);
mConsecutiveDownCount = 0;
} else {
if (mConsecutiveDownCount < 3) {
mConsecutiveDownCount++;
}
if (mConsecutiveDownCount >= 3) {
taskLoadBoost(LOAD_DOWN);
}
}
return ndk::ScopedAStatus::ok();
}
ndk::ScopedAStatus PowerHintSessionImpl::pause() {
LOG(INFO) << "PowerHintSessionImpl::pause ";
if (isSessionAlive(this)) {
sendHint(aidl::android::hardware::power::SessionHint::CPU_LOAD_RESET);
setSessionActivity(this, false);
}
return ndk::ScopedAStatus::ok();
}
ndk::ScopedAStatus PowerHintSessionImpl::resume() {
LOG(INFO) << "PowerHintSessionImpl::resume ";
if (isSessionAlive(this)) {
sendHint(aidl::android::hardware::power::SessionHint::CPU_LOAD_RESUME);
setSessionActivity(this, true);
}
return ndk::ScopedAStatus::ok();
}
ndk::ScopedAStatus PowerHintSessionImpl::close() {
LOG(INFO) << "PowerHintSessionImpl::close ";
if (isSessionAlive(this)) {
sendHint(aidl::android::hardware::power::SessionHint::CPU_LOAD_RESET);
taskLoadBoost(LOAD_RESET);
releaseLowCpuUtil();
mThreadIds.clear();
mSessionLock.lock();
mPowerHintSessions.erase(this);
mSessionLock.unlock();
}
return ndk::ScopedAStatus::ok();
}
ndk::ScopedAStatus PowerHintSessionImpl::sendHint(
aidl::android::hardware::power::SessionHint hint) {
LOG(INFO) << "PowerHintSessionImpl::sendHint ";
if (!isSessionActive(this)) return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
switch (hint) {
case aidl::android::hardware::power::SessionHint::CPU_LOAD_UP:
perfBoost(20, LOAD_UP);
break;
case aidl::android::hardware::power::SessionHint::CPU_LOAD_DOWN:
perfBoost(-20, LOAD_DOWN);
break;
case aidl::android::hardware::power::SessionHint::CPU_LOAD_RESET:
perfBoost(0, LOAD_RESET);
break;
case aidl::android::hardware::power::SessionHint::CPU_LOAD_RESUME:
perfBoost(0, LOAD_RESUME);
break;
case aidl::android::hardware::power::SessionHint::POWER_EFFICIENCY:
perfBoost(-20, LOAD_DOWN);
break;
default:
break;
}
return ndk::ScopedAStatus::ok();
}
ndk::ScopedAStatus PowerHintSessionImpl::setThreads(const std::vector<int32_t>& threadIds) {
LOG(INFO) << "PowerHintSessionImpl::setThreads "
<< printThreads(threadIds, static_cast<int>(threadIds.size()));
if (threadIds.size() == 0) {
LOG(ERROR) << "Threads list is empty";
return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
}
mThreadIds = threadIds;
// reset task load boost
mConsecutiveDownCount = 0;
taskLoadBoost(LOAD_RESET);
// reset session hint
if (mPowerEfficiencyMode)
setMode(aidl::android::hardware::power::SessionMode::POWER_EFFICIENCY,
mPowerEfficiencyMode);
return ndk::ScopedAStatus::ok();
}
ndk::ScopedAStatus PowerHintSessionImpl::setMode(aidl::android::hardware::power::SessionMode mode,
bool enabled) {
// TODO: top app is game check
switch (mode) {
case aidl::android::hardware::power::SessionMode::POWER_EFFICIENCY:
LOG(INFO) << "PowerHintSessionImpl::setMode: mode = POWER_EFFICIENCY, enabled = "
<< enabled;
releaseLowCpuUtil();
if (enabled) {
hintLowCpuUtil();
}
mPowerEfficiencyMode = enabled;
return ndk::ScopedAStatus::ok();
default:
LOG(ERROR) << "PowerHintSessionImpl::setMode: Unsupported mode = "
<< static_cast<int>(mode);
return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
}
return ndk::ScopedAStatus::ok();
}
ndk::ScopedAStatus PowerHintSessionImpl::getSessionConfig(
aidl::android::hardware::power::SessionConfig* _aidl_return) {
_aidl_return->id = 1;
return ndk::ScopedAStatus::ok();
}