1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "powerhal-libperfmgr"
18 #define ATRACE_TAG (ATRACE_TAG_POWER | ATRACE_TAG_HAL)
19
20 #include <array>
21 #include <memory>
22
23 #include <fcntl.h>
24 #include <poll.h>
25 #include <sys/eventfd.h>
26 #include <time.h>
27 #include <unistd.h>
28
29 #include <android-base/properties.h>
30 #include <utils/Log.h>
31 #include <utils/Trace.h>
32
33 #include "InteractionHandler.h"
34
35 #define MAX_LENGTH 64
36
37 #define MSINSEC 1000L
38 #define NSINMS 1000000L
39
40 namespace aidl {
41 namespace google {
42 namespace hardware {
43 namespace power {
44 namespace impl {
45 namespace pixel {
46
47 namespace {
48
49 static const bool kDisplayIdleSupport =
50 ::android::base::GetBoolProperty("vendor.powerhal.disp.idle_support", true);
51 static const std::array<const char *, 2> kDispIdlePath = {"/sys/class/drm/card0/device/idle_state",
52 "/sys/class/graphics/fb0/idle_state"};
53 static const uint32_t kWaitMs =
54 ::android::base::GetUintProperty("vendor.powerhal.disp.idle_wait", /*default*/ 100U);
55 static const uint32_t kMinDurationMs =
56 ::android::base::GetUintProperty("vendor.powerhal.interaction.min", /*default*/ 1400U);
57 static const uint32_t kMaxDurationMs =
58 ::android::base::GetUintProperty("vendor.powerhal.interaction.max", /*default*/ 5650U);
59 static const uint32_t kDurationOffsetMs =
60 ::android::base::GetUintProperty("vendor.powerhal.interaction.offset", /*default*/ 650U);
61
CalcTimespecDiffMs(struct timespec start,struct timespec end)62 static size_t CalcTimespecDiffMs(struct timespec start, struct timespec end) {
63 size_t diff_in_ms = 0;
64 diff_in_ms += (end.tv_sec - start.tv_sec) * MSINSEC;
65 diff_in_ms += (end.tv_nsec - start.tv_nsec) / NSINMS;
66 return diff_in_ms;
67 }
68
FbIdleOpen(void)69 static int FbIdleOpen(void) {
70 int fd;
71 for (const auto &path : kDispIdlePath) {
72 fd = open(path, O_RDONLY);
73 if (fd >= 0)
74 return fd;
75 }
76 ALOGE("Unable to open fb idle state path (%d)", errno);
77 return -1;
78 }
79
80 } // namespace
81
InteractionHandler(std::shared_ptr<HintManager> const & hint_manager)82 InteractionHandler::InteractionHandler(std::shared_ptr<HintManager> const &hint_manager)
83 : mState(INTERACTION_STATE_UNINITIALIZED),
84 mDurationMs(0),
85 mHintManager(hint_manager) {}
86
~InteractionHandler()87 InteractionHandler::~InteractionHandler() {
88 Exit();
89 }
90
Init()91 bool InteractionHandler::Init() {
92 std::lock_guard<std::mutex> lk(mLock);
93
94 if (mState != INTERACTION_STATE_UNINITIALIZED)
95 return true;
96
97 int fd = FbIdleOpen();
98 if (fd < 0)
99 return false;
100 mIdleFd = fd;
101
102 mEventFd = eventfd(0, EFD_NONBLOCK);
103 if (mEventFd < 0) {
104 ALOGE("Unable to create event fd (%d)", errno);
105 close(mIdleFd);
106 return false;
107 }
108
109 mState = INTERACTION_STATE_IDLE;
110 mThread = std::unique_ptr<std::thread>(new std::thread(&InteractionHandler::Routine, this));
111
112 return true;
113 }
114
Exit()115 void InteractionHandler::Exit() {
116 std::unique_lock<std::mutex> lk(mLock);
117 if (mState == INTERACTION_STATE_UNINITIALIZED)
118 return;
119
120 AbortWaitLocked();
121 mState = INTERACTION_STATE_UNINITIALIZED;
122 lk.unlock();
123
124 mCond.notify_all();
125 mThread->join();
126
127 close(mEventFd);
128 close(mIdleFd);
129 }
130
PerfLock()131 void InteractionHandler::PerfLock() {
132 ALOGV("%s: acquiring perf lock", __func__);
133 if (!mHintManager->DoHint("INTERACTION")) {
134 ALOGE("%s: do hint INTERACTION failed", __func__);
135 }
136 }
137
PerfRel()138 void InteractionHandler::PerfRel() {
139 ALOGV("%s: releasing perf lock", __func__);
140 if (!mHintManager->EndHint("INTERACTION")) {
141 ALOGE("%s: end hint INTERACTION failed", __func__);
142 }
143 }
144
Acquire(int32_t duration)145 void InteractionHandler::Acquire(int32_t duration) {
146 ATRACE_CALL();
147
148 std::lock_guard<std::mutex> lk(mLock);
149
150 int inputDuration = duration + kDurationOffsetMs;
151 int finalDuration;
152 if (inputDuration > kMaxDurationMs)
153 finalDuration = kMaxDurationMs;
154 else if (inputDuration > kMinDurationMs)
155 finalDuration = inputDuration;
156 else
157 finalDuration = kMinDurationMs;
158
159 // Fallback to do boost directly
160 // 1) override property is set OR
161 // 2) InteractionHandler not initialized
162 if (!kDisplayIdleSupport || mState == INTERACTION_STATE_UNINITIALIZED) {
163 mHintManager->DoHint("INTERACTION", std::chrono::milliseconds(finalDuration));
164 return;
165 }
166
167 struct timespec cur_timespec;
168 clock_gettime(CLOCK_MONOTONIC, &cur_timespec);
169 if (mState != INTERACTION_STATE_IDLE && finalDuration <= mDurationMs) {
170 size_t elapsed_time = CalcTimespecDiffMs(mLastTimespec, cur_timespec);
171 // don't hint if previous hint's duration covers this hint's duration
172 if (elapsed_time <= (mDurationMs - finalDuration)) {
173 ALOGV("%s: Previous duration (%d) cover this (%d) elapsed: %lld", __func__,
174 static_cast<int>(mDurationMs), static_cast<int>(finalDuration),
175 static_cast<long long>(elapsed_time));
176 return;
177 }
178 }
179 mLastTimespec = cur_timespec;
180 mDurationMs = finalDuration;
181
182 ALOGV("%s: input: %d final duration: %d", __func__, duration, finalDuration);
183
184 if (mState == INTERACTION_STATE_WAITING)
185 AbortWaitLocked();
186 else if (mState == INTERACTION_STATE_IDLE)
187 PerfLock();
188
189 mState = INTERACTION_STATE_INTERACTION;
190 mCond.notify_one();
191 }
192
Release()193 void InteractionHandler::Release() {
194 std::lock_guard<std::mutex> lk(mLock);
195 if (mState == INTERACTION_STATE_WAITING) {
196 ATRACE_CALL();
197 PerfRel();
198 mState = INTERACTION_STATE_IDLE;
199 } else {
200 // clear any wait aborts pending in event fd
201 uint64_t val;
202 ssize_t ret = read(mEventFd, &val, sizeof(val));
203
204 ALOGW_IF(ret < 0, "%s: failed to clear eventfd (%zd, %d)", __func__, ret, errno);
205 }
206 }
207
208 // should be called while locked
AbortWaitLocked()209 void InteractionHandler::AbortWaitLocked() {
210 uint64_t val = 1;
211 ssize_t ret = write(mEventFd, &val, sizeof(val));
212 if (ret != sizeof(val))
213 ALOGW("Unable to write to event fd (%zd)", ret);
214 }
215
WaitForIdle(int32_t wait_ms,int32_t timeout_ms)216 void InteractionHandler::WaitForIdle(int32_t wait_ms, int32_t timeout_ms) {
217 char data[MAX_LENGTH];
218 ssize_t ret;
219 struct pollfd pfd[2];
220
221 ATRACE_CALL();
222
223 ALOGV("%s: wait:%d timeout:%d", __func__, wait_ms, timeout_ms);
224
225 pfd[0].fd = mEventFd;
226 pfd[0].events = POLLIN;
227 pfd[1].fd = mIdleFd;
228 pfd[1].events = POLLPRI | POLLERR;
229
230 ret = poll(pfd, 1, wait_ms);
231 if (ret > 0) {
232 ALOGV("%s: wait aborted", __func__);
233 return;
234 } else if (ret < 0) {
235 ALOGE("%s: error in poll while waiting", __func__);
236 return;
237 }
238
239 ret = pread(mIdleFd, data, sizeof(data), 0);
240 if (!ret) {
241 ALOGE("%s: Unexpected EOF!", __func__);
242 return;
243 }
244
245 if (!strncmp(data, "idle", 4)) {
246 ALOGV("%s: already idle", __func__);
247 return;
248 }
249
250 ret = poll(pfd, 2, timeout_ms);
251 if (ret < 0)
252 ALOGE("%s: Error on waiting for idle (%zd)", __func__, ret);
253 else if (ret == 0)
254 ALOGV("%s: timed out waiting for idle", __func__);
255 else if (pfd[0].revents)
256 ALOGV("%s: wait for idle aborted", __func__);
257 else if (pfd[1].revents)
258 ALOGV("%s: idle detected", __func__);
259 }
260
Routine()261 void InteractionHandler::Routine() {
262 pthread_setname_np(pthread_self(), "DispIdle");
263 std::unique_lock<std::mutex> lk(mLock, std::defer_lock);
264
265 while (true) {
266 lk.lock();
267 mCond.wait(lk, [&] { return mState != INTERACTION_STATE_IDLE; });
268 if (mState == INTERACTION_STATE_UNINITIALIZED)
269 return;
270 mState = INTERACTION_STATE_WAITING;
271 lk.unlock();
272
273 WaitForIdle(kWaitMs, mDurationMs);
274 Release();
275 }
276 }
277
278 } // namespace pixel
279 } // namespace impl
280 } // namespace power
281 } // namespace hardware
282 } // namespace google
283 } // namespace aidl
284