• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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_NDEBUG 0
18 
19 #define LOG_TAG "PowerInteractionHandler"
20 #define ATRACE_TAG (ATRACE_TAG_POWER | ATRACE_TAG_HAL)
21 
22 #include <fcntl.h>
23 #include <poll.h>
24 #include <sys/eventfd.h>
25 #include <time.h>
26 #include <unistd.h>
27 #include <utils/Log.h>
28 #include <utils/Trace.h>
29 
30 #include "InteractionHandler.h"
31 #include "power-common.h"
32 #include "power-helper.h"
33 #include "powerhintparser.h"
34 #include "hint-data.h"
35 #include "utils.h"
36 
37 #define FB_IDLE_PATH "/sys/class/graphics/fb0/idle_state"
38 #define MAX_LENGTH 64
39 
40 #define MSINSEC 1000L
41 #define USINMS 1000000L
42 
InteractionHandler()43 InteractionHandler::InteractionHandler()
44     : mState(INTERACTION_STATE_UNINITIALIZED),
45       mWaitMs(100),
46       mMinDurationMs(1400),
47       mMaxDurationMs(5650),
48       mDurationMs(0) {
49 }
50 
~InteractionHandler()51 InteractionHandler::~InteractionHandler() {
52     Exit();
53 }
54 
Init()55 bool InteractionHandler::Init() {
56     std::lock_guard<std::mutex> lk(mLock);
57 
58     if (mState != INTERACTION_STATE_UNINITIALIZED)
59         return true;
60 
61     mIdleFd = open(FB_IDLE_PATH, O_RDONLY);
62     if (mIdleFd < 0) {
63         ALOGE("Unable to open idle state path (%d)", errno);
64         return false;
65     }
66 
67     mEventFd = eventfd(0, EFD_NONBLOCK);
68     if (mEventFd < 0) {
69         ALOGE("Unable to create event fd (%d)", errno);
70         close(mIdleFd);
71         return false;
72     }
73 
74     mState = INTERACTION_STATE_IDLE;
75     mThread = std::unique_ptr<std::thread>(
76         new std::thread(&InteractionHandler::Routine, this));
77 
78     return true;
79 }
80 
Exit()81 void InteractionHandler::Exit() {
82     std::unique_lock<std::mutex> lk(mLock);
83     if (mState == INTERACTION_STATE_UNINITIALIZED)
84         return;
85 
86     AbortWaitLocked();
87     mState = INTERACTION_STATE_UNINITIALIZED;
88     lk.unlock();
89 
90     mCond.notify_all();
91     mThread->join();
92 
93     close(mEventFd);
94     close(mIdleFd);
95 }
96 
PerfLock()97 void InteractionHandler::PerfLock() {
98     int *resource_values;
99     int num_resources;
100 
101     resource_values = getPowerhint(INTERACTION_HINT_ID, &num_resources);
102     if (resource_values != NULL) {
103         ALOGV("%s: acquiring perf lock", __func__);
104         perform_hint_action(INTERACTION_HINT_ID,
105                             resource_values, num_resources);
106 
107         ATRACE_INT("interaction_lock", 1);
108     }
109 }
110 
PerfRel()111 void InteractionHandler::PerfRel() {
112     ALOGV("%s: releasing perf lock", __func__);
113     undo_hint_action(INTERACTION_HINT_ID);
114     ATRACE_INT("interaction_lock", 0);
115 }
116 
CalcTimespecDiffMs(struct timespec start,struct timespec end)117 long long InteractionHandler::CalcTimespecDiffMs(struct timespec start,
118                                                struct timespec end) {
119     long long diff_in_us = 0;
120     diff_in_us += (end.tv_sec - start.tv_sec) * MSINSEC;
121     diff_in_us += (end.tv_nsec - start.tv_nsec) / USINMS;
122     return diff_in_us;
123 }
124 
Acquire(int32_t duration)125 void InteractionHandler::Acquire(int32_t duration) {
126     if (is_perf_hint_active(SUSTAINED_PERF_HINT_ID) ||
127         is_perf_hint_active(VR_MODE_HINT_ID)) {
128         ALOGV("%s: ignoring due to other active perf hints", __func__);
129         return;
130     }
131 
132     ATRACE_CALL();
133 
134     std::lock_guard<std::mutex> lk(mLock);
135     if (mState == INTERACTION_STATE_UNINITIALIZED) {
136         ALOGW("%s: called while uninitialized", __func__);
137         return;
138     }
139 
140     int inputDuration = duration + 650;
141     int finalDuration;
142     if (inputDuration > mMaxDurationMs)
143         finalDuration = mMaxDurationMs;
144     else if (inputDuration > mMinDurationMs)
145         finalDuration = inputDuration;
146     else
147         finalDuration = mMinDurationMs;
148 
149     struct timespec cur_timespec;
150     clock_gettime(CLOCK_MONOTONIC, &cur_timespec);
151     if (mState != INTERACTION_STATE_IDLE && finalDuration <= mDurationMs) {
152         long long elapsed_time = CalcTimespecDiffMs(mLastTimespec, cur_timespec);
153         // don't hint if previous hint's duration covers this hint's duration
154         if (elapsed_time <= (mDurationMs - finalDuration)) {
155             ALOGV("%s: Previous duration (%d) cover this (%d) elapsed: %lld",
156                   __func__, mDurationMs, finalDuration, elapsed_time);
157             return;
158         }
159     }
160     mLastTimespec = cur_timespec;
161     mDurationMs = finalDuration;
162 
163     ALOGV("%s: input: %d final duration: %d", __func__,
164           duration, finalDuration);
165 
166     if (mState == INTERACTION_STATE_WAITING)
167         AbortWaitLocked();
168     else if (mState == INTERACTION_STATE_IDLE)
169         PerfLock();
170 
171     mState = INTERACTION_STATE_INTERACTION;
172     mCond.notify_one();
173 }
174 
Release()175 void InteractionHandler::Release() {
176     std::lock_guard<std::mutex> lk(mLock);
177     if (mState == INTERACTION_STATE_WAITING) {
178         ATRACE_CALL();
179         PerfRel();
180         mState = INTERACTION_STATE_IDLE;
181     } else {
182         // clear any wait aborts pending in event fd
183         uint64_t val;
184         ssize_t ret = read(mEventFd, &val, sizeof(val));
185 
186         ALOGW_IF(ret < 0, "%s: failed to clear eventfd (%zd, %d)",
187                  __func__, ret, errno);
188     }
189 }
190 
191 // should be called while locked
AbortWaitLocked()192 void InteractionHandler::AbortWaitLocked() {
193     uint64_t val = 1;
194     ssize_t ret = write(mEventFd, &val, sizeof(val));
195     if (ret != sizeof(val))
196         ALOGW("Unable to write to event fd (%zd)", ret);
197 }
198 
WaitForIdle(int32_t wait_ms,int32_t timeout_ms)199 void InteractionHandler::WaitForIdle(int32_t wait_ms, int32_t timeout_ms) {
200     char data[MAX_LENGTH];
201     ssize_t ret;
202     struct pollfd pfd[2];
203 
204     ATRACE_CALL();
205 
206     ALOGV("%s: wait:%d timeout:%d", __func__, wait_ms, timeout_ms);
207 
208     pfd[0].fd = mEventFd;
209     pfd[0].events = POLLIN;
210     pfd[1].fd = mIdleFd;
211     pfd[1].events = POLLPRI | POLLERR;
212 
213     ret = poll(pfd, 1, wait_ms);
214     if (ret > 0) {
215         ALOGV("%s: wait aborted", __func__);
216         return;
217     } else if (ret < 0) {
218         ALOGE("%s: error in poll while waiting", __func__);
219         return;
220     }
221 
222     ret = pread(mIdleFd, data, sizeof(data), 0);
223     if (!ret) {
224         ALOGE("%s: Unexpected EOF!", __func__);
225         return;
226     }
227 
228     if (!strncmp(data, "idle", 4)) {
229         ALOGV("%s: already idle", __func__);
230         return;
231     }
232 
233     ret = poll(pfd, 2, timeout_ms);
234     if (ret < 0)
235         ALOGE("%s: Error on waiting for idle (%zd)", __func__, ret);
236     else if (ret == 0)
237         ALOGV("%s: timed out waiting for idle", __func__);
238     else if (pfd[0].revents)
239         ALOGV("%s: wait for idle aborted", __func__);
240     else if (pfd[1].revents)
241         ALOGV("%s: idle detected", __func__);
242 }
243 
Routine()244 void InteractionHandler::Routine() {
245     std::unique_lock<std::mutex> lk(mLock, std::defer_lock);
246 
247     while (true) {
248         lk.lock();
249         mCond.wait(lk, [&] { return mState != INTERACTION_STATE_IDLE; });
250         if (mState == INTERACTION_STATE_UNINITIALIZED)
251             return;
252         mState = INTERACTION_STATE_WAITING;
253         lk.unlock();
254 
255         WaitForIdle(mWaitMs, mDurationMs);
256         Release();
257     }
258 }
259