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 | |
| 17 | #pragma once |
| 18 | |
| 19 | #include <android-base/thread_annotations.h> |
| 20 | #include <mutex> |
| 21 | #include <unordered_map> |
| 22 | #include <vector> |
Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 23 | #include "SchedulerUtils.h" |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 24 | #include "VSyncTracker.h" |
| 25 | |
| 26 | namespace android::scheduler { |
| 27 | |
| 28 | class VSyncPredictor : public VSyncTracker { |
| 29 | public: |
| 30 | /* |
| 31 | * \param [in] idealPeriod The initial ideal period to use. |
| 32 | * \param [in] historySize The internal amount of entries to store in the model. |
| 33 | * \param [in] minimumSamplesForPrediction The minimum number of samples to collect before |
| 34 | * predicting. \param [in] outlierTolerancePercent a number 0 to 100 that will be used to filter |
| 35 | * samples that fall outlierTolerancePercent from an anticipated vsync event. |
| 36 | */ |
| 37 | VSyncPredictor(nsecs_t idealPeriod, size_t historySize, size_t minimumSamplesForPrediction, |
| 38 | uint32_t outlierTolerancePercent); |
| 39 | ~VSyncPredictor(); |
| 40 | |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame^] | 41 | bool addVsyncTimestamp(nsecs_t timestamp) final EXCLUDES(mMutex); |
| 42 | nsecs_t nextAnticipatedVSyncTimeFrom(nsecs_t timePoint) const final EXCLUDES(mMutex); |
| 43 | nsecs_t currentPeriod() const final EXCLUDES(mMutex); |
| 44 | void resetModel() final EXCLUDES(mMutex); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 45 | |
| 46 | /* |
| 47 | * Inform the model that the period is anticipated to change to a new value. |
| 48 | * model will use the period parameter to predict vsync events until enough |
| 49 | * timestamps with the new period have been collected. |
| 50 | * |
| 51 | * \param [in] period The new period that should be used. |
| 52 | */ |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame^] | 53 | void setPeriod(nsecs_t period) final EXCLUDES(mMutex); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 54 | |
Kevin DuBois | b818bfa | 2020-07-10 14:29:36 -0700 | [diff] [blame] | 55 | /* Query if the model is in need of more samples to make a prediction. |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 56 | * \return True, if model would benefit from more samples, False if not. |
| 57 | */ |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame^] | 58 | bool needsMoreSamples() const final EXCLUDES(mMutex); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 59 | |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame^] | 60 | struct Model { |
| 61 | nsecs_t slope; |
| 62 | nsecs_t intercept; |
| 63 | }; |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 64 | |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame^] | 65 | VSyncPredictor::Model getVSyncPredictionModel() const EXCLUDES(mMutex); |
| 66 | |
| 67 | bool isVSyncInPhase(nsecs_t timePoint, int divider) const final EXCLUDES(mMutex); |
| 68 | |
| 69 | void dump(std::string& result) const final EXCLUDES(mMutex); |
Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 70 | |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 71 | private: |
| 72 | VSyncPredictor(VSyncPredictor const&) = delete; |
| 73 | VSyncPredictor& operator=(VSyncPredictor const&) = delete; |
Kevin DuBois | c3e9e8e | 2020-01-07 09:06:52 -0800 | [diff] [blame] | 74 | void clearTimestamps() REQUIRES(mMutex); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 75 | |
Kevin DuBois | ecb1f0d | 2019-12-12 10:47:41 -0800 | [diff] [blame] | 76 | inline void traceInt64If(const char* name, int64_t value) const; |
| 77 | bool const mTraceOn; |
| 78 | |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 79 | size_t const kHistorySize; |
| 80 | size_t const kMinimumSamplesForPrediction; |
| 81 | size_t const kOutlierTolerancePercent; |
| 82 | |
| 83 | std::mutex mutable mMutex; |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 84 | size_t next(size_t i) const REQUIRES(mMutex); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 85 | bool validate(nsecs_t timestamp) const REQUIRES(mMutex); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame^] | 86 | |
| 87 | Model getVSyncPredictionModelLocked() const REQUIRES(mMutex); |
| 88 | |
| 89 | nsecs_t nextAnticipatedVSyncTimeFromLocked(nsecs_t timePoint) const REQUIRES(mMutex); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 90 | |
| 91 | nsecs_t mIdealPeriod GUARDED_BY(mMutex); |
| 92 | std::optional<nsecs_t> mKnownTimestamp GUARDED_BY(mMutex); |
| 93 | |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame^] | 94 | // Map between ideal vsync period and the calculated model |
| 95 | std::unordered_map<nsecs_t, Model> mutable mRateMap GUARDED_BY(mMutex); |
| 96 | |
| 97 | // Map between the divided vsync period and the last known vsync timestamp |
| 98 | std::unordered_map<nsecs_t, nsecs_t> mutable mRateDividerKnownTimestampMap GUARDED_BY(mMutex); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 99 | |
Ady Abraham | 9c53ee7 | 2020-07-22 21:16:18 -0700 | [diff] [blame] | 100 | size_t mLastTimestampIndex GUARDED_BY(mMutex) = 0; |
Ady Abraham | 92fa2f4 | 2020-02-11 15:33:56 -0800 | [diff] [blame] | 101 | std::vector<nsecs_t> mTimestamps GUARDED_BY(mMutex); |
Kevin DuBois | 1678e2c | 2019-08-22 12:26:24 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | } // namespace android::scheduler |