| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 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 |  | 
| Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 17 | // TODO(b/129481165): remove the #pragma below and fix conversion issues | 
|  | 18 | #pragma clang diagnostic push | 
|  | 19 | #pragma clang diagnostic ignored "-Wconversion" | 
|  | 20 |  | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 21 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS | 
|  | 22 | //#define LOG_NDEBUG 0 | 
|  | 23 | #include "VSyncPredictor.h" | 
|  | 24 | #include <android-base/logging.h> | 
|  | 25 | #include <cutils/compiler.h> | 
| Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 26 | #include <cutils/properties.h> | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 27 | #include <utils/Log.h> | 
|  | 28 | #include <utils/Trace.h> | 
|  | 29 | #include <algorithm> | 
|  | 30 | #include <chrono> | 
| Kevin DuBois | 127a2d9 | 2019-12-04 13:52:52 -0800 | [diff] [blame] | 31 | #include <sstream> | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 32 |  | 
|  | 33 | namespace android::scheduler { | 
| Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 34 |  | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 35 | static auto constexpr kMaxPercent = 100u; | 
|  | 36 |  | 
|  | 37 | VSyncPredictor::~VSyncPredictor() = default; | 
|  | 38 |  | 
|  | 39 | VSyncPredictor::VSyncPredictor(nsecs_t idealPeriod, size_t historySize, | 
|  | 40 | size_t minimumSamplesForPrediction, uint32_t outlierTolerancePercent) | 
| Kevin DuBois | c57f2c3 | 2019-12-20 16:32:29 -0800 | [diff] [blame] | 41 | : mTraceOn(property_get_bool("debug.sf.vsp_trace", true)), | 
| Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 42 | kHistorySize(historySize), | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 43 | kMinimumSamplesForPrediction(minimumSamplesForPrediction), | 
|  | 44 | kOutlierTolerancePercent(std::min(outlierTolerancePercent, kMaxPercent)), | 
|  | 45 | mIdealPeriod(idealPeriod) { | 
| Kevin DuBois | c3e9e8e | 2020-01-07 09:06:52 -0800 | [diff] [blame] | 46 | resetModel(); | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 47 | } | 
|  | 48 |  | 
| Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 49 | inline void VSyncPredictor::traceInt64If(const char* name, int64_t value) const { | 
|  | 50 | if (CC_UNLIKELY(mTraceOn)) { | 
|  | 51 | ATRACE_INT64(name, value); | 
|  | 52 | } | 
|  | 53 | } | 
|  | 54 |  | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 55 | inline size_t VSyncPredictor::next(int i) const { | 
| Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 56 | return (i + 1) % mTimestamps.size(); | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 57 | } | 
|  | 58 |  | 
|  | 59 | bool VSyncPredictor::validate(nsecs_t timestamp) const { | 
| Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 60 | if (mLastTimestampIndex < 0 || mTimestamps.empty()) { | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 61 | return true; | 
|  | 62 | } | 
|  | 63 |  | 
| Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 64 | auto const aValidTimestamp = mTimestamps[mLastTimestampIndex]; | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 65 | auto const percent = (timestamp - aValidTimestamp) % mIdealPeriod * kMaxPercent / mIdealPeriod; | 
|  | 66 | return percent < kOutlierTolerancePercent || percent > (kMaxPercent - kOutlierTolerancePercent); | 
|  | 67 | } | 
|  | 68 |  | 
| Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 69 | nsecs_t VSyncPredictor::currentPeriod() const { | 
|  | 70 | std::lock_guard<std::mutex> lk(mMutex); | 
|  | 71 | return std::get<0>(mRateMap.find(mIdealPeriod)->second); | 
|  | 72 | } | 
|  | 73 |  | 
| Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 74 | bool VSyncPredictor::addVsyncTimestamp(nsecs_t timestamp) { | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 75 | std::lock_guard<std::mutex> lk(mMutex); | 
|  | 76 |  | 
|  | 77 | if (!validate(timestamp)) { | 
| Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 78 | ALOGV("timestamp was too far off the last known timestamp"); | 
| Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 79 | return false; | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 80 | } | 
|  | 81 |  | 
| Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 82 | if (mTimestamps.size() != kHistorySize) { | 
|  | 83 | mTimestamps.push_back(timestamp); | 
|  | 84 | mLastTimestampIndex = next(mLastTimestampIndex); | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 85 | } else { | 
| Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 86 | mLastTimestampIndex = next(mLastTimestampIndex); | 
|  | 87 | mTimestamps[mLastTimestampIndex] = timestamp; | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 88 | } | 
|  | 89 |  | 
| Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 90 | if (mTimestamps.size() < kMinimumSamplesForPrediction) { | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 91 | mRateMap[mIdealPeriod] = {mIdealPeriod, 0}; | 
| Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 92 | return true; | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 93 | } | 
|  | 94 |  | 
|  | 95 | // This is a 'simple linear regression' calculation of Y over X, with Y being the | 
|  | 96 | // vsync timestamps, and X being the ordinal of vsync count. | 
|  | 97 | // The calculated slope is the vsync period. | 
|  | 98 | // Formula for reference: | 
|  | 99 | // Sigma_i: means sum over all timestamps. | 
|  | 100 | // mean(variable): statistical mean of variable. | 
|  | 101 | // X: snapped ordinal of the timestamp | 
|  | 102 | // Y: vsync timestamp | 
|  | 103 | // | 
|  | 104 | //         Sigma_i( (X_i - mean(X)) * (Y_i - mean(Y) ) | 
|  | 105 | // slope = ------------------------------------------- | 
|  | 106 | //         Sigma_i ( X_i - mean(X) ) ^ 2 | 
|  | 107 | // | 
|  | 108 | // intercept = mean(Y) - slope * mean(X) | 
|  | 109 | // | 
| Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 110 | std::vector<nsecs_t> vsyncTS(mTimestamps.size()); | 
|  | 111 | std::vector<nsecs_t> ordinals(mTimestamps.size()); | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 112 |  | 
|  | 113 | // normalizing to the oldest timestamp cuts down on error in calculating the intercept. | 
| Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 114 | auto const oldest_ts = *std::min_element(mTimestamps.begin(), mTimestamps.end()); | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 115 | auto it = mRateMap.find(mIdealPeriod); | 
|  | 116 | auto const currentPeriod = std::get<0>(it->second); | 
|  | 117 | // TODO (b/144707443): its important that there's some precision in the mean of the ordinals | 
| Kevin DuBois | 0049f8b | 2020-03-11 10:30:11 -0700 | [diff] [blame] | 118 | //                     for the intercept calculation, so scale the ordinals by 1000 to continue | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 119 | //                     fixed point calculation. Explore expanding | 
|  | 120 | //                     scheduler::utils::calculate_mean to have a fixed point fractional part. | 
| Kevin DuBois | 0049f8b | 2020-03-11 10:30:11 -0700 | [diff] [blame] | 121 | static constexpr int64_t kScalingFactor = 1000; | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 122 |  | 
| Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 123 | for (auto i = 0u; i < mTimestamps.size(); i++) { | 
|  | 124 | traceInt64If("VSP-ts", mTimestamps[i]); | 
| Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 125 |  | 
| Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 126 | vsyncTS[i] = mTimestamps[i] - oldest_ts; | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 127 | ordinals[i] = ((vsyncTS[i] + (currentPeriod / 2)) / currentPeriod) * kScalingFactor; | 
|  | 128 | } | 
|  | 129 |  | 
|  | 130 | auto meanTS = scheduler::calculate_mean(vsyncTS); | 
|  | 131 | auto meanOrdinal = scheduler::calculate_mean(ordinals); | 
|  | 132 | for (auto i = 0; i < vsyncTS.size(); i++) { | 
|  | 133 | vsyncTS[i] -= meanTS; | 
|  | 134 | ordinals[i] -= meanOrdinal; | 
|  | 135 | } | 
|  | 136 |  | 
|  | 137 | auto top = 0ll; | 
|  | 138 | auto bottom = 0ll; | 
|  | 139 | for (auto i = 0; i < vsyncTS.size(); i++) { | 
|  | 140 | top += vsyncTS[i] * ordinals[i]; | 
|  | 141 | bottom += ordinals[i] * ordinals[i]; | 
|  | 142 | } | 
|  | 143 |  | 
|  | 144 | if (CC_UNLIKELY(bottom == 0)) { | 
|  | 145 | it->second = {mIdealPeriod, 0}; | 
| Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 146 | clearTimestamps(); | 
| Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 147 | return false; | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 148 | } | 
|  | 149 |  | 
| Kevin DuBois | 0049f8b | 2020-03-11 10:30:11 -0700 | [diff] [blame] | 150 | nsecs_t const anticipatedPeriod = top * kScalingFactor / bottom; | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 151 | nsecs_t const intercept = meanTS - (anticipatedPeriod * meanOrdinal / kScalingFactor); | 
|  | 152 |  | 
| Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 153 | auto const percent = std::abs(anticipatedPeriod - mIdealPeriod) * kMaxPercent / mIdealPeriod; | 
|  | 154 | if (percent >= kOutlierTolerancePercent) { | 
|  | 155 | it->second = {mIdealPeriod, 0}; | 
|  | 156 | clearTimestamps(); | 
|  | 157 | return false; | 
|  | 158 | } | 
|  | 159 |  | 
| Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 160 | traceInt64If("VSP-period", anticipatedPeriod); | 
|  | 161 | traceInt64If("VSP-intercept", intercept); | 
|  | 162 |  | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 163 | it->second = {anticipatedPeriod, intercept}; | 
|  | 164 |  | 
|  | 165 | ALOGV("model update ts: %" PRId64 " slope: %" PRId64 " intercept: %" PRId64, timestamp, | 
|  | 166 | anticipatedPeriod, intercept); | 
| Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 167 | return true; | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 168 | } | 
|  | 169 |  | 
|  | 170 | nsecs_t VSyncPredictor::nextAnticipatedVSyncTimeFrom(nsecs_t timePoint) const { | 
|  | 171 | std::lock_guard<std::mutex> lk(mMutex); | 
|  | 172 |  | 
|  | 173 | auto const [slope, intercept] = getVSyncPredictionModel(lk); | 
|  | 174 |  | 
| Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 175 | if (mTimestamps.empty()) { | 
| Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 176 | traceInt64If("VSP-mode", 1); | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 177 | auto const knownTimestamp = mKnownTimestamp ? *mKnownTimestamp : timePoint; | 
|  | 178 | auto const numPeriodsOut = ((timePoint - knownTimestamp) / mIdealPeriod) + 1; | 
|  | 179 | return knownTimestamp + numPeriodsOut * mIdealPeriod; | 
|  | 180 | } | 
|  | 181 |  | 
| Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 182 | auto const oldest = *std::min_element(mTimestamps.begin(), mTimestamps.end()); | 
| Kevin DuBois | 127a2d9 | 2019-12-04 13:52:52 -0800 | [diff] [blame] | 183 |  | 
|  | 184 | // See b/145667109, the ordinal calculation must take into account the intercept. | 
|  | 185 | auto const zeroPoint = oldest + intercept; | 
|  | 186 | auto const ordinalRequest = (timePoint - zeroPoint + slope) / slope; | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 187 | auto const prediction = (ordinalRequest * slope) + intercept + oldest; | 
|  | 188 |  | 
| Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 189 | traceInt64If("VSP-mode", 0); | 
|  | 190 | traceInt64If("VSP-timePoint", timePoint); | 
|  | 191 | traceInt64If("VSP-prediction", prediction); | 
|  | 192 |  | 
| Kevin DuBois | 127a2d9 | 2019-12-04 13:52:52 -0800 | [diff] [blame] | 193 | auto const printer = [&, slope = slope, intercept = intercept] { | 
|  | 194 | std::stringstream str; | 
|  | 195 | str << "prediction made from: " << timePoint << "prediction: " << prediction << " (+" | 
|  | 196 | << prediction - timePoint << ") slope: " << slope << " intercept: " << intercept | 
|  | 197 | << "oldestTS: " << oldest << " ordinal: " << ordinalRequest; | 
|  | 198 | return str.str(); | 
|  | 199 | }; | 
|  | 200 |  | 
|  | 201 | ALOGV("%s", printer().c_str()); | 
|  | 202 | LOG_ALWAYS_FATAL_IF(prediction < timePoint, "VSyncPredictor: model miscalculation: %s", | 
|  | 203 | printer().c_str()); | 
|  | 204 |  | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 205 | return prediction; | 
|  | 206 | } | 
|  | 207 |  | 
|  | 208 | std::tuple<nsecs_t, nsecs_t> VSyncPredictor::getVSyncPredictionModel() const { | 
|  | 209 | std::lock_guard<std::mutex> lk(mMutex); | 
|  | 210 | return VSyncPredictor::getVSyncPredictionModel(lk); | 
|  | 211 | } | 
|  | 212 |  | 
|  | 213 | std::tuple<nsecs_t, nsecs_t> VSyncPredictor::getVSyncPredictionModel( | 
|  | 214 | std::lock_guard<std::mutex> const&) const { | 
|  | 215 | return mRateMap.find(mIdealPeriod)->second; | 
|  | 216 | } | 
|  | 217 |  | 
|  | 218 | void VSyncPredictor::setPeriod(nsecs_t period) { | 
|  | 219 | ATRACE_CALL(); | 
|  | 220 |  | 
|  | 221 | std::lock_guard<std::mutex> lk(mMutex); | 
|  | 222 | static constexpr size_t kSizeLimit = 30; | 
|  | 223 | if (CC_UNLIKELY(mRateMap.size() == kSizeLimit)) { | 
|  | 224 | mRateMap.erase(mRateMap.begin()); | 
|  | 225 | } | 
|  | 226 |  | 
|  | 227 | mIdealPeriod = period; | 
|  | 228 | if (mRateMap.find(period) == mRateMap.end()) { | 
|  | 229 | mRateMap[mIdealPeriod] = {period, 0}; | 
|  | 230 | } | 
|  | 231 |  | 
| Kevin DuBois | c3e9e8e | 2020-01-07 09:06:52 -0800 | [diff] [blame] | 232 | clearTimestamps(); | 
|  | 233 | } | 
|  | 234 |  | 
|  | 235 | void VSyncPredictor::clearTimestamps() { | 
| Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 236 | if (!mTimestamps.empty()) { | 
|  | 237 | mKnownTimestamp = *std::max_element(mTimestamps.begin(), mTimestamps.end()); | 
|  | 238 | mTimestamps.clear(); | 
|  | 239 | mLastTimestampIndex = 0; | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 240 | } | 
|  | 241 | } | 
|  | 242 |  | 
|  | 243 | bool VSyncPredictor::needsMoreSamples(nsecs_t now) const { | 
|  | 244 | using namespace std::literals::chrono_literals; | 
|  | 245 | std::lock_guard<std::mutex> lk(mMutex); | 
|  | 246 | bool needsMoreSamples = true; | 
| Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 247 | if (mTimestamps.size() >= kMinimumSamplesForPrediction) { | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 248 | nsecs_t constexpr aLongTime = | 
|  | 249 | std::chrono::duration_cast<std::chrono::nanoseconds>(500ms).count(); | 
| Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 250 | if (!(mLastTimestampIndex < 0 || mTimestamps.empty())) { | 
|  | 251 | auto const lastTimestamp = mTimestamps[mLastTimestampIndex]; | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 252 | needsMoreSamples = !((lastTimestamp + aLongTime) > now); | 
|  | 253 | } | 
|  | 254 | } | 
|  | 255 |  | 
| Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 256 | ATRACE_INT("VSP-moreSamples", needsMoreSamples); | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 257 | return needsMoreSamples; | 
|  | 258 | } | 
|  | 259 |  | 
| Kevin DuBois | c3e9e8e | 2020-01-07 09:06:52 -0800 | [diff] [blame] | 260 | void VSyncPredictor::resetModel() { | 
|  | 261 | std::lock_guard<std::mutex> lk(mMutex); | 
|  | 262 | mRateMap[mIdealPeriod] = {mIdealPeriod, 0}; | 
|  | 263 | clearTimestamps(); | 
|  | 264 | } | 
|  | 265 |  | 
| Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 266 | } // namespace android::scheduler | 
| Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 267 |  | 
|  | 268 | // TODO(b/129481165): remove the #pragma below and fix conversion issues | 
|  | 269 | #pragma clang diagnostic pop // ignored "-Wconversion" |