1 /*
2 * Copyright 2019 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 // TODO(b/129481165): remove the #pragma below and fix conversion issues
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wextra"
20
21 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
22 //#define LOG_NDEBUG 0
23 #include "VSyncPredictor.h"
24 #include <android-base/logging.h>
25 #include <android-base/stringprintf.h>
26 #include <cutils/compiler.h>
27 #include <cutils/properties.h>
28 #include <utils/Log.h>
29 #include <utils/Trace.h>
30 #include <algorithm>
31 #include <chrono>
32 #include <sstream>
33 #include "RefreshRateConfigs.h"
34
35 #undef LOG_TAG
36 #define LOG_TAG "VSyncPredictor"
37
38 namespace android::scheduler {
39 using base::StringAppendF;
40
41 static auto constexpr kMaxPercent = 100u;
42
43 VSyncPredictor::~VSyncPredictor() = default;
44
VSyncPredictor(nsecs_t idealPeriod,size_t historySize,size_t minimumSamplesForPrediction,uint32_t outlierTolerancePercent)45 VSyncPredictor::VSyncPredictor(nsecs_t idealPeriod, size_t historySize,
46 size_t minimumSamplesForPrediction, uint32_t outlierTolerancePercent)
47 : mTraceOn(property_get_bool("debug.sf.vsp_trace", true)),
48 kHistorySize(historySize),
49 kMinimumSamplesForPrediction(minimumSamplesForPrediction),
50 kOutlierTolerancePercent(std::min(outlierTolerancePercent, kMaxPercent)),
51 mIdealPeriod(idealPeriod) {
52 resetModel();
53 }
54
traceInt64If(const char * name,int64_t value) const55 inline void VSyncPredictor::traceInt64If(const char* name, int64_t value) const {
56 if (CC_UNLIKELY(mTraceOn)) {
57 ATRACE_INT64(name, value);
58 }
59 }
60
next(size_t i) const61 inline size_t VSyncPredictor::next(size_t i) const {
62 return (i + 1) % mTimestamps.size();
63 }
64
validate(nsecs_t timestamp) const65 bool VSyncPredictor::validate(nsecs_t timestamp) const {
66 if (mLastTimestampIndex < 0 || mTimestamps.empty()) {
67 return true;
68 }
69
70 auto const aValidTimestamp = mTimestamps[mLastTimestampIndex];
71 auto const percent = (timestamp - aValidTimestamp) % mIdealPeriod * kMaxPercent / mIdealPeriod;
72 if (percent >= kOutlierTolerancePercent &&
73 percent <= (kMaxPercent - kOutlierTolerancePercent)) {
74 return false;
75 }
76
77 const auto iter = std::min_element(mTimestamps.begin(), mTimestamps.end(),
78 [timestamp](nsecs_t a, nsecs_t b) {
79 return std::abs(timestamp - a) < std::abs(timestamp - b);
80 });
81 const auto distancePercent = std::abs(*iter - timestamp) * kMaxPercent / mIdealPeriod;
82 if (distancePercent < kOutlierTolerancePercent) {
83 // duplicate timestamp
84 return false;
85 }
86 return true;
87 }
88
currentPeriod() const89 nsecs_t VSyncPredictor::currentPeriod() const {
90 std::lock_guard lock(mMutex);
91 return mRateMap.find(mIdealPeriod)->second.slope;
92 }
93
addVsyncTimestamp(nsecs_t timestamp)94 bool VSyncPredictor::addVsyncTimestamp(nsecs_t timestamp) {
95 std::lock_guard lock(mMutex);
96
97 if (!validate(timestamp)) {
98 // VSR could elect to ignore the incongruent timestamp or resetModel(). If ts is ignored,
99 // don't insert this ts into mTimestamps ringbuffer. If we are still
100 // in the learning phase we should just clear all timestamps and start
101 // over.
102 if (mTimestamps.size() < kMinimumSamplesForPrediction) {
103 // Add the timestamp to mTimestamps before clearing it so we could
104 // update mKnownTimestamp based on the new timestamp.
105 mTimestamps.push_back(timestamp);
106 clearTimestamps();
107 } else if (!mTimestamps.empty()) {
108 mKnownTimestamp =
109 std::max(timestamp, *std::max_element(mTimestamps.begin(), mTimestamps.end()));
110 } else {
111 mKnownTimestamp = timestamp;
112 }
113 return false;
114 }
115
116 if (mTimestamps.size() != kHistorySize) {
117 mTimestamps.push_back(timestamp);
118 mLastTimestampIndex = next(mLastTimestampIndex);
119 } else {
120 mLastTimestampIndex = next(mLastTimestampIndex);
121 mTimestamps[mLastTimestampIndex] = timestamp;
122 }
123
124 if (mTimestamps.size() < kMinimumSamplesForPrediction) {
125 mRateMap[mIdealPeriod] = {mIdealPeriod, 0};
126 return true;
127 }
128
129 // This is a 'simple linear regression' calculation of Y over X, with Y being the
130 // vsync timestamps, and X being the ordinal of vsync count.
131 // The calculated slope is the vsync period.
132 // Formula for reference:
133 // Sigma_i: means sum over all timestamps.
134 // mean(variable): statistical mean of variable.
135 // X: snapped ordinal of the timestamp
136 // Y: vsync timestamp
137 //
138 // Sigma_i( (X_i - mean(X)) * (Y_i - mean(Y) )
139 // slope = -------------------------------------------
140 // Sigma_i ( X_i - mean(X) ) ^ 2
141 //
142 // intercept = mean(Y) - slope * mean(X)
143 //
144 std::vector<nsecs_t> vsyncTS(mTimestamps.size());
145 std::vector<nsecs_t> ordinals(mTimestamps.size());
146
147 // normalizing to the oldest timestamp cuts down on error in calculating the intercept.
148 auto const oldest_ts = *std::min_element(mTimestamps.begin(), mTimestamps.end());
149 auto it = mRateMap.find(mIdealPeriod);
150 auto const currentPeriod = it->second.slope;
151 // TODO (b/144707443): its important that there's some precision in the mean of the ordinals
152 // for the intercept calculation, so scale the ordinals by 1000 to continue
153 // fixed point calculation. Explore expanding
154 // scheduler::utils::calculate_mean to have a fixed point fractional part.
155 static constexpr int64_t kScalingFactor = 1000;
156
157 for (auto i = 0u; i < mTimestamps.size(); i++) {
158 traceInt64If("VSP-ts", mTimestamps[i]);
159
160 vsyncTS[i] = mTimestamps[i] - oldest_ts;
161 ordinals[i] = ((vsyncTS[i] + (currentPeriod / 2)) / currentPeriod) * kScalingFactor;
162 }
163
164 auto meanTS = scheduler::calculate_mean(vsyncTS);
165 auto meanOrdinal = scheduler::calculate_mean(ordinals);
166 for (size_t i = 0; i < vsyncTS.size(); i++) {
167 vsyncTS[i] -= meanTS;
168 ordinals[i] -= meanOrdinal;
169 }
170
171 auto top = 0ll;
172 auto bottom = 0ll;
173 for (size_t i = 0; i < vsyncTS.size(); i++) {
174 top += vsyncTS[i] * ordinals[i];
175 bottom += ordinals[i] * ordinals[i];
176 }
177
178 if (CC_UNLIKELY(bottom == 0)) {
179 it->second = {mIdealPeriod, 0};
180 clearTimestamps();
181 return false;
182 }
183
184 nsecs_t const anticipatedPeriod = top * kScalingFactor / bottom;
185 nsecs_t const intercept = meanTS - (anticipatedPeriod * meanOrdinal / kScalingFactor);
186
187 auto const percent = std::abs(anticipatedPeriod - mIdealPeriod) * kMaxPercent / mIdealPeriod;
188 if (percent >= kOutlierTolerancePercent) {
189 it->second = {mIdealPeriod, 0};
190 clearTimestamps();
191 return false;
192 }
193
194 traceInt64If("VSP-period", anticipatedPeriod);
195 traceInt64If("VSP-intercept", intercept);
196
197 it->second = {anticipatedPeriod, intercept};
198
199 ALOGV("model update ts: %" PRId64 " slope: %" PRId64 " intercept: %" PRId64, timestamp,
200 anticipatedPeriod, intercept);
201 return true;
202 }
203
nextAnticipatedVSyncTimeFromLocked(nsecs_t timePoint) const204 nsecs_t VSyncPredictor::nextAnticipatedVSyncTimeFromLocked(nsecs_t timePoint) const {
205 auto const [slope, intercept] = getVSyncPredictionModelLocked();
206
207 if (mTimestamps.empty()) {
208 traceInt64If("VSP-mode", 1);
209 auto const knownTimestamp = mKnownTimestamp ? *mKnownTimestamp : timePoint;
210 auto const numPeriodsOut = ((timePoint - knownTimestamp) / mIdealPeriod) + 1;
211 return knownTimestamp + numPeriodsOut * mIdealPeriod;
212 }
213
214 auto const oldest = *std::min_element(mTimestamps.begin(), mTimestamps.end());
215
216 // See b/145667109, the ordinal calculation must take into account the intercept.
217 auto const zeroPoint = oldest + intercept;
218 auto const ordinalRequest = (timePoint - zeroPoint + slope) / slope;
219 auto const prediction = (ordinalRequest * slope) + intercept + oldest;
220
221 traceInt64If("VSP-mode", 0);
222 traceInt64If("VSP-timePoint", timePoint);
223 traceInt64If("VSP-prediction", prediction);
224
225 auto const printer = [&, slope = slope, intercept = intercept] {
226 std::stringstream str;
227 str << "prediction made from: " << timePoint << "prediction: " << prediction << " (+"
228 << prediction - timePoint << ") slope: " << slope << " intercept: " << intercept
229 << "oldestTS: " << oldest << " ordinal: " << ordinalRequest;
230 return str.str();
231 };
232
233 ALOGV("%s", printer().c_str());
234 LOG_ALWAYS_FATAL_IF(prediction < timePoint, "VSyncPredictor: model miscalculation: %s",
235 printer().c_str());
236
237 return prediction;
238 }
239
nextAnticipatedVSyncTimeFrom(nsecs_t timePoint) const240 nsecs_t VSyncPredictor::nextAnticipatedVSyncTimeFrom(nsecs_t timePoint) const {
241 std::lock_guard lock(mMutex);
242 return nextAnticipatedVSyncTimeFromLocked(timePoint);
243 }
244
245 /*
246 * Returns whether a given vsync timestamp is in phase with a frame rate.
247 * If the frame rate is not a divider of the refresh rate, it is always considered in phase.
248 * For example, if the vsync timestamps are (16.6,33.3,50.0,66.6):
249 * isVSyncInPhase(16.6, 30) = true
250 * isVSyncInPhase(33.3, 30) = false
251 * isVSyncInPhase(50.0, 30) = true
252 */
isVSyncInPhase(nsecs_t timePoint,Fps frameRate) const253 bool VSyncPredictor::isVSyncInPhase(nsecs_t timePoint, Fps frameRate) const {
254 struct VsyncError {
255 nsecs_t vsyncTimestamp;
256 float error;
257
258 bool operator<(const VsyncError& other) const { return error < other.error; }
259 };
260
261 std::lock_guard lock(mMutex);
262 const auto divider =
263 RefreshRateConfigs::getFrameRateDivider(Fps::fromPeriodNsecs(mIdealPeriod), frameRate);
264 if (divider <= 1 || timePoint == 0) {
265 return true;
266 }
267
268 const nsecs_t period = mRateMap[mIdealPeriod].slope;
269 const nsecs_t justBeforeTimePoint = timePoint - period / 2;
270 const nsecs_t dividedPeriod = mIdealPeriod / divider;
271
272 // If this is the first time we have asked about this divider with the
273 // current vsync period, it is considered in phase and we store the closest
274 // vsync timestamp
275 const auto knownTimestampIter = mRateDividerKnownTimestampMap.find(dividedPeriod);
276 if (knownTimestampIter == mRateDividerKnownTimestampMap.end()) {
277 const auto vsync = nextAnticipatedVSyncTimeFromLocked(justBeforeTimePoint);
278 mRateDividerKnownTimestampMap[dividedPeriod] = vsync;
279 return true;
280 }
281
282 // Find the next N vsync timestamp where N is the divider.
283 // One of these vsyncs will be in phase. We return the one which is
284 // the most aligned with the last known in phase vsync
285 std::vector<VsyncError> vsyncs(static_cast<size_t>(divider));
286 const nsecs_t knownVsync = knownTimestampIter->second;
287 nsecs_t point = justBeforeTimePoint;
288 for (size_t i = 0; i < divider; i++) {
289 const nsecs_t vsync = nextAnticipatedVSyncTimeFromLocked(point);
290 const auto numPeriods = static_cast<float>(vsync - knownVsync) / (period * divider);
291 const auto error = std::abs(std::round(numPeriods) - numPeriods);
292 vsyncs[i] = {vsync, error};
293 point = vsync + 1;
294 }
295
296 const auto minVsyncError = std::min_element(vsyncs.begin(), vsyncs.end());
297 mRateDividerKnownTimestampMap[dividedPeriod] = minVsyncError->vsyncTimestamp;
298 return std::abs(minVsyncError->vsyncTimestamp - timePoint) < period / 2;
299 }
300
getVSyncPredictionModel() const301 VSyncPredictor::Model VSyncPredictor::getVSyncPredictionModel() const {
302 std::lock_guard lock(mMutex);
303 const auto model = VSyncPredictor::getVSyncPredictionModelLocked();
304 return {model.slope, model.intercept};
305 }
306
getVSyncPredictionModelLocked() const307 VSyncPredictor::Model VSyncPredictor::getVSyncPredictionModelLocked() const {
308 return mRateMap.find(mIdealPeriod)->second;
309 }
310
setPeriod(nsecs_t period)311 void VSyncPredictor::setPeriod(nsecs_t period) {
312 ATRACE_CALL();
313
314 std::lock_guard lock(mMutex);
315 static constexpr size_t kSizeLimit = 30;
316 if (CC_UNLIKELY(mRateMap.size() == kSizeLimit)) {
317 mRateMap.erase(mRateMap.begin());
318 }
319
320 mIdealPeriod = period;
321 if (mRateMap.find(period) == mRateMap.end()) {
322 mRateMap[mIdealPeriod] = {period, 0};
323 }
324
325 clearTimestamps();
326 }
327
clearTimestamps()328 void VSyncPredictor::clearTimestamps() {
329 if (!mTimestamps.empty()) {
330 auto const maxRb = *std::max_element(mTimestamps.begin(), mTimestamps.end());
331 if (mKnownTimestamp) {
332 mKnownTimestamp = std::max(*mKnownTimestamp, maxRb);
333 } else {
334 mKnownTimestamp = maxRb;
335 }
336
337 mTimestamps.clear();
338 mLastTimestampIndex = 0;
339 }
340 }
341
needsMoreSamples() const342 bool VSyncPredictor::needsMoreSamples() const {
343 std::lock_guard lock(mMutex);
344 return mTimestamps.size() < kMinimumSamplesForPrediction;
345 }
346
resetModel()347 void VSyncPredictor::resetModel() {
348 std::lock_guard lock(mMutex);
349 mRateMap[mIdealPeriod] = {mIdealPeriod, 0};
350 clearTimestamps();
351 }
352
dump(std::string & result) const353 void VSyncPredictor::dump(std::string& result) const {
354 std::lock_guard lock(mMutex);
355 StringAppendF(&result, "\tmIdealPeriod=%.2f\n", mIdealPeriod / 1e6f);
356 StringAppendF(&result, "\tRefresh Rate Map:\n");
357 for (const auto& [idealPeriod, periodInterceptTuple] : mRateMap) {
358 StringAppendF(&result,
359 "\t\tFor ideal period %.2fms: period = %.2fms, intercept = %" PRId64 "\n",
360 idealPeriod / 1e6f, periodInterceptTuple.slope / 1e6f,
361 periodInterceptTuple.intercept);
362 }
363 }
364
365 } // namespace android::scheduler
366
367 // TODO(b/129481165): remove the #pragma below and fix conversion issues
368 #pragma clang diagnostic pop // ignored "-Wextra"