Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | |
Greg Hackmann | 86efcc0 | 2014-03-07 12:44:02 -0800 | [diff] [blame] | 21 | #include <inttypes.h> |
| 22 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 23 | #include <android-base/stringprintf.h> |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 24 | #include <android/log.h> |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 25 | |
Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 26 | #include <ui/FrameStats.h> |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 27 | |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 28 | #include "FrameTracker.h" |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 29 | #include "EventLog/EventLog.h" |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | FrameTracker::FrameTracker() : |
| 34 | mOffset(0), |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 35 | mNumFences(0), |
| 36 | mDisplayPeriod(0) { |
| 37 | resetFrameCountersLocked(); |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void FrameTracker::setDesiredPresentTime(nsecs_t presentTime) { |
Jamie Gennis | 4b0eba9 | 2013-02-05 13:30:24 -0800 | [diff] [blame] | 41 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 42 | mFrameRecords[mOffset].desiredPresentTime = presentTime; |
| 43 | } |
| 44 | |
| 45 | void FrameTracker::setFrameReadyTime(nsecs_t readyTime) { |
Jamie Gennis | 4b0eba9 | 2013-02-05 13:30:24 -0800 | [diff] [blame] | 46 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 47 | mFrameRecords[mOffset].frameReadyTime = readyTime; |
| 48 | } |
| 49 | |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 50 | void FrameTracker::setFrameReadyFence( |
| 51 | std::shared_ptr<FenceTime>&& readyFence) { |
Jamie Gennis | 4b0eba9 | 2013-02-05 13:30:24 -0800 | [diff] [blame] | 52 | Mutex::Autolock lock(mMutex); |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 53 | mFrameRecords[mOffset].frameReadyFence = std::move(readyFence); |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 54 | mNumFences++; |
| 55 | } |
| 56 | |
| 57 | void FrameTracker::setActualPresentTime(nsecs_t presentTime) { |
Jamie Gennis | 4b0eba9 | 2013-02-05 13:30:24 -0800 | [diff] [blame] | 58 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 59 | mFrameRecords[mOffset].actualPresentTime = presentTime; |
| 60 | } |
| 61 | |
Ady Abraham | 6c1b7ac | 2021-03-31 16:56:03 -0700 | [diff] [blame] | 62 | void FrameTracker::setActualPresentFence(const std::shared_ptr<FenceTime>& readyFence) { |
Jamie Gennis | 4b0eba9 | 2013-02-05 13:30:24 -0800 | [diff] [blame] | 63 | Mutex::Autolock lock(mMutex); |
Ady Abraham | 6c1b7ac | 2021-03-31 16:56:03 -0700 | [diff] [blame] | 64 | mFrameRecords[mOffset].actualPresentFence = readyFence; |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 65 | mNumFences++; |
| 66 | } |
| 67 | |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 68 | void FrameTracker::setDisplayRefreshPeriod(nsecs_t displayPeriod) { |
| 69 | Mutex::Autolock lock(mMutex); |
| 70 | mDisplayPeriod = displayPeriod; |
| 71 | } |
| 72 | |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 73 | void FrameTracker::advanceFrame() { |
Jamie Gennis | 4b0eba9 | 2013-02-05 13:30:24 -0800 | [diff] [blame] | 74 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 75 | |
| 76 | // Update the statistic to include the frame we just finished. |
| 77 | updateStatsLocked(mOffset); |
| 78 | |
| 79 | // Advance to the next frame. |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 80 | mOffset = (mOffset+1) % NUM_FRAME_RECORDS; |
| 81 | mFrameRecords[mOffset].desiredPresentTime = INT64_MAX; |
| 82 | mFrameRecords[mOffset].frameReadyTime = INT64_MAX; |
| 83 | mFrameRecords[mOffset].actualPresentTime = INT64_MAX; |
| 84 | |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 85 | if (mFrameRecords[mOffset].frameReadyFence != nullptr) { |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 86 | // We're clobbering an unsignaled fence, so we need to decrement the |
| 87 | // fence count. |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 88 | mFrameRecords[mOffset].frameReadyFence = nullptr; |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 89 | mNumFences--; |
| 90 | } |
| 91 | |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 92 | if (mFrameRecords[mOffset].actualPresentFence != nullptr) { |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 93 | // We're clobbering an unsignaled fence, so we need to decrement the |
| 94 | // fence count. |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 95 | mFrameRecords[mOffset].actualPresentFence = nullptr; |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 96 | mNumFences--; |
| 97 | } |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 100 | void FrameTracker::clearStats() { |
Jamie Gennis | 4b0eba9 | 2013-02-05 13:30:24 -0800 | [diff] [blame] | 101 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 102 | for (size_t i = 0; i < NUM_FRAME_RECORDS; i++) { |
| 103 | mFrameRecords[i].desiredPresentTime = 0; |
| 104 | mFrameRecords[i].frameReadyTime = 0; |
| 105 | mFrameRecords[i].actualPresentTime = 0; |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 106 | mFrameRecords[i].frameReadyFence.reset(); |
| 107 | mFrameRecords[i].actualPresentFence.reset(); |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 108 | } |
| 109 | mNumFences = 0; |
Jamie Gennis | 4b0eba9 | 2013-02-05 13:30:24 -0800 | [diff] [blame] | 110 | mFrameRecords[mOffset].desiredPresentTime = INT64_MAX; |
| 111 | mFrameRecords[mOffset].frameReadyTime = INT64_MAX; |
| 112 | mFrameRecords[mOffset].actualPresentTime = INT64_MAX; |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Svetoslav | d85084b | 2014-03-20 10:28:31 -0700 | [diff] [blame] | 115 | void FrameTracker::getStats(FrameStats* outStats) const { |
| 116 | Mutex::Autolock lock(mMutex); |
| 117 | processFencesLocked(); |
| 118 | |
| 119 | outStats->refreshPeriodNano = mDisplayPeriod; |
| 120 | |
| 121 | const size_t offset = mOffset; |
| 122 | for (size_t i = 1; i < NUM_FRAME_RECORDS; i++) { |
| 123 | const size_t index = (offset + i) % NUM_FRAME_RECORDS; |
| 124 | |
| 125 | // Skip frame records with no data (if buffer not yet full). |
| 126 | if (mFrameRecords[index].desiredPresentTime == 0) { |
| 127 | continue; |
| 128 | } |
| 129 | |
| 130 | nsecs_t desiredPresentTimeNano = mFrameRecords[index].desiredPresentTime; |
| 131 | outStats->desiredPresentTimesNano.push_back(desiredPresentTimeNano); |
| 132 | |
| 133 | nsecs_t actualPresentTimeNano = mFrameRecords[index].actualPresentTime; |
| 134 | outStats->actualPresentTimesNano.push_back(actualPresentTimeNano); |
| 135 | |
| 136 | nsecs_t frameReadyTimeNano = mFrameRecords[index].frameReadyTime; |
| 137 | outStats->frameReadyTimesNano.push_back(frameReadyTimeNano); |
| 138 | } |
| 139 | } |
| 140 | |
Dominik Laskowski | 87a07e4 | 2019-10-10 20:38:02 -0700 | [diff] [blame] | 141 | void FrameTracker::logAndResetStats(const std::string_view& name) { |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 142 | Mutex::Autolock lock(mMutex); |
| 143 | logStatsLocked(name); |
| 144 | resetFrameCountersLocked(); |
| 145 | } |
| 146 | |
Jamie Gennis | 4b0eba9 | 2013-02-05 13:30:24 -0800 | [diff] [blame] | 147 | void FrameTracker::processFencesLocked() const { |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 148 | FrameRecord* records = const_cast<FrameRecord*>(mFrameRecords); |
| 149 | int& numFences = const_cast<int&>(mNumFences); |
| 150 | |
| 151 | for (int i = 1; i < NUM_FRAME_RECORDS && numFences > 0; i++) { |
| 152 | size_t idx = (mOffset+NUM_FRAME_RECORDS-i) % NUM_FRAME_RECORDS; |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 153 | bool updated = false; |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 154 | |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 155 | const std::shared_ptr<FenceTime>& rfence = records[idx].frameReadyFence; |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 156 | if (rfence != nullptr) { |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 157 | records[idx].frameReadyTime = rfence->getSignalTime(); |
| 158 | if (records[idx].frameReadyTime < INT64_MAX) { |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 159 | records[idx].frameReadyFence = nullptr; |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 160 | numFences--; |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 161 | updated = true; |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
Brian Anderson | 3d4039d | 2016-09-23 16:31:30 -0700 | [diff] [blame] | 165 | const std::shared_ptr<FenceTime>& pfence = |
| 166 | records[idx].actualPresentFence; |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 167 | if (pfence != nullptr) { |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 168 | records[idx].actualPresentTime = pfence->getSignalTime(); |
| 169 | if (records[idx].actualPresentTime < INT64_MAX) { |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 170 | records[idx].actualPresentFence = nullptr; |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 171 | numFences--; |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 172 | updated = true; |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 173 | } |
| 174 | } |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 175 | |
| 176 | if (updated) { |
| 177 | updateStatsLocked(idx); |
| 178 | } |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 182 | void FrameTracker::updateStatsLocked(size_t newFrameIdx) const { |
| 183 | int* numFrames = const_cast<int*>(mNumFrames); |
| 184 | |
| 185 | if (mDisplayPeriod > 0 && isFrameValidLocked(newFrameIdx)) { |
| 186 | size_t prevFrameIdx = (newFrameIdx+NUM_FRAME_RECORDS-1) % |
| 187 | NUM_FRAME_RECORDS; |
| 188 | |
| 189 | if (isFrameValidLocked(prevFrameIdx)) { |
| 190 | nsecs_t newPresentTime = |
| 191 | mFrameRecords[newFrameIdx].actualPresentTime; |
| 192 | nsecs_t prevPresentTime = |
| 193 | mFrameRecords[prevFrameIdx].actualPresentTime; |
| 194 | |
| 195 | nsecs_t duration = newPresentTime - prevPresentTime; |
| 196 | int numPeriods = int((duration + mDisplayPeriod/2) / |
| 197 | mDisplayPeriod); |
| 198 | |
| 199 | for (int i = 0; i < NUM_FRAME_BUCKETS-1; i++) { |
| 200 | int nextBucket = 1 << (i+1); |
| 201 | if (numPeriods < nextBucket) { |
| 202 | numFrames[i]++; |
| 203 | return; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | // The last duration bucket is a catch-all. |
| 208 | numFrames[NUM_FRAME_BUCKETS-1]++; |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | void FrameTracker::resetFrameCountersLocked() { |
| 214 | for (int i = 0; i < NUM_FRAME_BUCKETS; i++) { |
| 215 | mNumFrames[i] = 0; |
| 216 | } |
| 217 | } |
| 218 | |
Dominik Laskowski | 87a07e4 | 2019-10-10 20:38:02 -0700 | [diff] [blame] | 219 | void FrameTracker::logStatsLocked(const std::string_view& name) const { |
Jamie Gennis | 6547ff4 | 2013-07-16 20:12:42 -0700 | [diff] [blame] | 220 | for (int i = 0; i < NUM_FRAME_BUCKETS; i++) { |
| 221 | if (mNumFrames[i] > 0) { |
| 222 | EventLog::logFrameDurations(name, mNumFrames, NUM_FRAME_BUCKETS); |
| 223 | return; |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | bool FrameTracker::isFrameValidLocked(size_t idx) const { |
| 229 | return mFrameRecords[idx].actualPresentTime > 0 && |
| 230 | mFrameRecords[idx].actualPresentTime < INT64_MAX; |
| 231 | } |
| 232 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 233 | void FrameTracker::dumpStats(std::string& result) const { |
Jamie Gennis | 4b0eba9 | 2013-02-05 13:30:24 -0800 | [diff] [blame] | 234 | Mutex::Autolock lock(mMutex); |
| 235 | processFencesLocked(); |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 236 | |
| 237 | const size_t o = mOffset; |
| 238 | for (size_t i = 1; i < NUM_FRAME_RECORDS; i++) { |
| 239 | const size_t index = (o+i) % NUM_FRAME_RECORDS; |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 240 | base::StringAppendF(&result, "%" PRId64 "\t%" PRId64 "\t%" PRId64 "\n", |
| 241 | mFrameRecords[index].desiredPresentTime, |
| 242 | mFrameRecords[index].actualPresentTime, |
| 243 | mFrameRecords[index].frameReadyTime); |
Jamie Gennis | 82dbc74 | 2012-11-08 19:23:28 -0800 | [diff] [blame] | 244 | } |
| 245 | result.append("\n"); |
| 246 | } |
| 247 | |
| 248 | } // namespace android |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 249 | |
| 250 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 251 | #pragma clang diagnostic pop // ignored "-Wconversion" |