• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     ATRACE_INT("interaction_lock", 1);
137 }
138 
PerfRel()139 void InteractionHandler::PerfRel() {
140     ALOGV("%s: releasing perf lock", __func__);
141     if (!mHintManager->EndHint("INTERACTION")) {
142         ALOGE("%s: end hint INTERACTION failed", __func__);
143     }
144     ATRACE_INT("interaction_lock", 0);
145 }
146 
Acquire(int32_t duration)147 void InteractionHandler::Acquire(int32_t duration) {
148     ATRACE_CALL();
149 
150     std::lock_guard<std::mutex> lk(mLock);
151 
152     int inputDuration = duration + kDurationOffsetMs;
153     int finalDuration;
154     if (inputDuration > kMaxDurationMs)
155         finalDuration = kMaxDurationMs;
156     else if (inputDuration > kMinDurationMs)
157         finalDuration = inputDuration;
158     else
159         finalDuration = kMinDurationMs;
160 
161     // Fallback to do boost directly
162     // 1) override property is set OR
163     // 2) InteractionHandler not initialized
164     if (!kDisplayIdleSupport || mState == INTERACTION_STATE_UNINITIALIZED) {
165         mHintManager->DoHint("INTERACTION", std::chrono::milliseconds(finalDuration));
166         return;
167     }
168 
169     struct timespec cur_timespec;
170     clock_gettime(CLOCK_MONOTONIC, &cur_timespec);
171     if (mState != INTERACTION_STATE_IDLE && finalDuration <= mDurationMs) {
172         size_t elapsed_time = CalcTimespecDiffMs(mLastTimespec, cur_timespec);
173         // don't hint if previous hint's duration covers this hint's duration
174         if (elapsed_time <= (mDurationMs - finalDuration)) {
175             ALOGV("%s: Previous duration (%d) cover this (%d) elapsed: %lld", __func__,
176                   static_cast<int>(mDurationMs), static_cast<int>(finalDuration),
177                   static_cast<long long>(elapsed_time));
178             return;
179         }
180     }
181     mLastTimespec = cur_timespec;
182     mDurationMs = finalDuration;
183 
184     ALOGV("%s: input: %d final duration: %d", __func__, duration, finalDuration);
185 
186     if (mState == INTERACTION_STATE_WAITING)
187         AbortWaitLocked();
188     else if (mState == INTERACTION_STATE_IDLE)
189         PerfLock();
190 
191     mState = INTERACTION_STATE_INTERACTION;
192     mCond.notify_one();
193 }
194 
Release()195 void InteractionHandler::Release() {
196     std::lock_guard<std::mutex> lk(mLock);
197     if (mState == INTERACTION_STATE_WAITING) {
198         ATRACE_CALL();
199         PerfRel();
200         mState = INTERACTION_STATE_IDLE;
201     } else {
202         // clear any wait aborts pending in event fd
203         uint64_t val;
204         ssize_t ret = read(mEventFd, &val, sizeof(val));
205 
206         ALOGW_IF(ret < 0, "%s: failed to clear eventfd (%zd, %d)", __func__, ret, errno);
207     }
208 }
209 
210 // should be called while locked
AbortWaitLocked()211 void InteractionHandler::AbortWaitLocked() {
212     uint64_t val = 1;
213     ssize_t ret = write(mEventFd, &val, sizeof(val));
214     if (ret != sizeof(val))
215         ALOGW("Unable to write to event fd (%zd)", ret);
216 }
217 
WaitForIdle(int32_t wait_ms,int32_t timeout_ms)218 void InteractionHandler::WaitForIdle(int32_t wait_ms, int32_t timeout_ms) {
219     char data[MAX_LENGTH];
220     ssize_t ret;
221     struct pollfd pfd[2];
222 
223     ATRACE_CALL();
224 
225     ALOGV("%s: wait:%d timeout:%d", __func__, wait_ms, timeout_ms);
226 
227     pfd[0].fd = mEventFd;
228     pfd[0].events = POLLIN;
229     pfd[1].fd = mIdleFd;
230     pfd[1].events = POLLPRI | POLLERR;
231 
232     ret = poll(pfd, 1, wait_ms);
233     if (ret > 0) {
234         ALOGV("%s: wait aborted", __func__);
235         return;
236     } else if (ret < 0) {
237         ALOGE("%s: error in poll while waiting", __func__);
238         return;
239     }
240 
241     ret = pread(mIdleFd, data, sizeof(data), 0);
242     if (!ret) {
243         ALOGE("%s: Unexpected EOF!", __func__);
244         return;
245     }
246 
247     if (!strncmp(data, "idle", 4)) {
248         ALOGV("%s: already idle", __func__);
249         return;
250     }
251 
252     ret = poll(pfd, 2, timeout_ms);
253     if (ret < 0)
254         ALOGE("%s: Error on waiting for idle (%zd)", __func__, ret);
255     else if (ret == 0)
256         ALOGV("%s: timed out waiting for idle", __func__);
257     else if (pfd[0].revents)
258         ALOGV("%s: wait for idle aborted", __func__);
259     else if (pfd[1].revents)
260         ALOGV("%s: idle detected", __func__);
261 }
262 
Routine()263 void InteractionHandler::Routine() {
264     pthread_setname_np(pthread_self(), "DispIdle");
265     std::unique_lock<std::mutex> lk(mLock, std::defer_lock);
266 
267     while (true) {
268         lk.lock();
269         mCond.wait(lk, [&] { return mState != INTERACTION_STATE_IDLE; });
270         if (mState == INTERACTION_STATE_UNINITIALIZED)
271             return;
272         mState = INTERACTION_STATE_WAITING;
273         lk.unlock();
274 
275         WaitForIdle(kWaitMs, mDurationMs);
276         Release();
277     }
278 }
279 
280 }  // namespace pixel
281 }  // namespace impl
282 }  // namespace power
283 }  // namespace hardware
284 }  // namespace google
285 }  // namespace aidl
286