Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | #undef LOG_TAG |
| 17 | #define LOG_TAG "TimeStats" |
| 18 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 19 | |
| 20 | #include "TimeStats.h" |
| 21 | |
| 22 | #include <android-base/stringprintf.h> |
| 23 | |
| 24 | #include <log/log.h> |
| 25 | |
| 26 | #include <utils/String8.h> |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 27 | #include <utils/Timers.h> |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 28 | #include <utils/Trace.h> |
| 29 | |
| 30 | #include <algorithm> |
| 31 | #include <regex> |
| 32 | |
| 33 | namespace android { |
| 34 | |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 35 | namespace impl { |
| 36 | |
Dominik Laskowski | c286714 | 2019-01-21 11:33:38 -0800 | [diff] [blame] | 37 | void TimeStats::parseArgs(bool asProto, const Vector<String16>& args, std::string& result) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 38 | ATRACE_CALL(); |
| 39 | |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 40 | std::unordered_map<std::string, int32_t> argsMap; |
Dominik Laskowski | c286714 | 2019-01-21 11:33:38 -0800 | [diff] [blame] | 41 | for (size_t index = 0; index < args.size(); ++index) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 42 | argsMap[std::string(String8(args[index]).c_str())] = index; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | if (argsMap.count("-disable")) { |
| 46 | disable(); |
| 47 | } |
| 48 | |
| 49 | if (argsMap.count("-dump")) { |
Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 50 | std::optional<uint32_t> maxLayers = std::nullopt; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 51 | auto iter = argsMap.find("-maxlayers"); |
| 52 | if (iter != argsMap.end() && iter->second + 1 < static_cast<int32_t>(args.size())) { |
Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 53 | int64_t value = strtol(String8(args[iter->second + 1]).c_str(), nullptr, 10); |
| 54 | value = std::clamp(value, int64_t(0), int64_t(UINT32_MAX)); |
| 55 | maxLayers = static_cast<uint32_t>(value); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 58 | dump(asProto, maxLayers, result); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | if (argsMap.count("-clear")) { |
| 62 | clear(); |
| 63 | } |
| 64 | |
| 65 | if (argsMap.count("-enable")) { |
| 66 | enable(); |
| 67 | } |
| 68 | } |
| 69 | |
Yiwei Zhang | 7eb58b7 | 2019-04-22 19:00:02 -0700 | [diff] [blame] | 70 | std::string TimeStats::miniDump() { |
| 71 | ATRACE_CALL(); |
| 72 | |
| 73 | std::string result = "TimeStats miniDump:\n"; |
| 74 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | e926ab5 | 2019-08-14 15:16:00 -0700 | [diff] [blame] | 75 | android::base::StringAppendF(&result, "Number of layers currently being tracked is %zu\n", |
Yiwei Zhang | 7eb58b7 | 2019-04-22 19:00:02 -0700 | [diff] [blame] | 76 | mTimeStatsTracker.size()); |
Yiwei Zhang | e926ab5 | 2019-08-14 15:16:00 -0700 | [diff] [blame] | 77 | android::base::StringAppendF(&result, "Number of layers in the stats pool is %zu\n", |
| 78 | mTimeStats.stats.size()); |
Yiwei Zhang | 7eb58b7 | 2019-04-22 19:00:02 -0700 | [diff] [blame] | 79 | return result; |
| 80 | } |
| 81 | |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 82 | void TimeStats::incrementTotalFrames() { |
| 83 | if (!mEnabled.load()) return; |
| 84 | |
| 85 | ATRACE_CALL(); |
| 86 | |
| 87 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 88 | mTimeStats.totalFrames++; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Yiwei Zhang | 621f9d4 | 2018-05-07 10:40:55 -0700 | [diff] [blame] | 91 | void TimeStats::incrementMissedFrames() { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 92 | if (!mEnabled.load()) return; |
| 93 | |
| 94 | ATRACE_CALL(); |
| 95 | |
| 96 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 97 | mTimeStats.missedFrames++; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void TimeStats::incrementClientCompositionFrames() { |
| 101 | if (!mEnabled.load()) return; |
| 102 | |
| 103 | ATRACE_CALL(); |
| 104 | |
| 105 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 106 | mTimeStats.clientCompositionFrames++; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 109 | bool TimeStats::recordReadyLocked(int32_t layerID, TimeRecord* timeRecord) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 110 | if (!timeRecord->ready) { |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 111 | ALOGV("[%d]-[%" PRIu64 "]-presentFence is still not received", layerID, |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 112 | timeRecord->frameTime.frameNumber); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 113 | return false; |
| 114 | } |
| 115 | |
| 116 | if (timeRecord->acquireFence != nullptr) { |
| 117 | if (timeRecord->acquireFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) { |
| 118 | return false; |
| 119 | } |
| 120 | if (timeRecord->acquireFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) { |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 121 | timeRecord->frameTime.acquireTime = timeRecord->acquireFence->getSignalTime(); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 122 | timeRecord->acquireFence = nullptr; |
| 123 | } else { |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 124 | ALOGV("[%d]-[%" PRIu64 "]-acquireFence signal time is invalid", layerID, |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 125 | timeRecord->frameTime.frameNumber); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | if (timeRecord->presentFence != nullptr) { |
| 130 | if (timeRecord->presentFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) { |
| 131 | return false; |
| 132 | } |
| 133 | if (timeRecord->presentFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) { |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 134 | timeRecord->frameTime.presentTime = timeRecord->presentFence->getSignalTime(); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 135 | timeRecord->presentFence = nullptr; |
| 136 | } else { |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 137 | ALOGV("[%d]-[%" PRIu64 "]-presentFence signal time invalid", layerID, |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 138 | timeRecord->frameTime.frameNumber); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | static int32_t msBetween(nsecs_t start, nsecs_t end) { |
| 146 | int64_t delta = (end - start) / 1000000; |
| 147 | delta = std::clamp(delta, int64_t(INT32_MIN), int64_t(INT32_MAX)); |
| 148 | return static_cast<int32_t>(delta); |
| 149 | } |
| 150 | |
Yiwei Zhang | bd40832 | 2018-10-15 18:31:53 -0700 | [diff] [blame] | 151 | // This regular expression captures the following for instance: |
| 152 | // StatusBar in StatusBar#0 |
| 153 | // com.appname in com.appname/com.appname.activity#0 |
| 154 | // com.appname in SurfaceView - com.appname/com.appname.activity#0 |
| 155 | static const std::regex packageNameRegex("(?:SurfaceView[-\\s\\t]+)?([^/]+).*#\\d+"); |
| 156 | |
Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 157 | static std::string getPackageName(const std::string& layerName) { |
Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 158 | std::smatch match; |
Yiwei Zhang | bd40832 | 2018-10-15 18:31:53 -0700 | [diff] [blame] | 159 | if (std::regex_match(layerName.begin(), layerName.end(), match, packageNameRegex)) { |
Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 160 | // There must be a match for group 1 otherwise the whole string is not |
| 161 | // matched and the above will return false |
| 162 | return match[1]; |
| 163 | } |
| 164 | return ""; |
| 165 | } |
| 166 | |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 167 | void TimeStats::flushAvailableRecordsToStatsLocked(int32_t layerID) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 168 | ATRACE_CALL(); |
| 169 | |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 170 | LayerRecord& layerRecord = mTimeStatsTracker[layerID]; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 171 | TimeRecord& prevTimeRecord = layerRecord.prevTimeRecord; |
Yiwei Zhang | c5f2c45 | 2018-05-08 16:31:56 -0700 | [diff] [blame] | 172 | std::deque<TimeRecord>& timeRecords = layerRecord.timeRecords; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 173 | while (!timeRecords.empty()) { |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 174 | if (!recordReadyLocked(layerID, &timeRecords[0])) break; |
| 175 | ALOGV("[%d]-[%" PRIu64 "]-presentFenceTime[%" PRId64 "]", layerID, |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 176 | timeRecords[0].frameTime.frameNumber, timeRecords[0].frameTime.presentTime); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 177 | |
| 178 | if (prevTimeRecord.ready) { |
Yiwei Zhang | eafa5cc | 2019-07-26 15:06:25 -0700 | [diff] [blame] | 179 | const std::string& layerName = layerRecord.layerName; |
Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 180 | if (!mTimeStats.stats.count(layerName)) { |
| 181 | mTimeStats.stats[layerName].layerName = layerName; |
| 182 | mTimeStats.stats[layerName].packageName = getPackageName(layerName); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 183 | } |
Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 184 | TimeStatsHelper::TimeStatsLayer& timeStatsLayer = mTimeStats.stats[layerName]; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 185 | timeStatsLayer.totalFrames++; |
Yiwei Zhang | eaeea06 | 2018-06-28 14:46:51 -0700 | [diff] [blame] | 186 | timeStatsLayer.droppedFrames += layerRecord.droppedFrames; |
| 187 | layerRecord.droppedFrames = 0; |
| 188 | |
| 189 | const int32_t postToAcquireMs = msBetween(timeRecords[0].frameTime.postTime, |
| 190 | timeRecords[0].frameTime.acquireTime); |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 191 | ALOGV("[%d]-[%" PRIu64 "]-post2acquire[%d]", layerID, |
Yiwei Zhang | eaeea06 | 2018-06-28 14:46:51 -0700 | [diff] [blame] | 192 | timeRecords[0].frameTime.frameNumber, postToAcquireMs); |
| 193 | timeStatsLayer.deltas["post2acquire"].insert(postToAcquireMs); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 194 | |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 195 | const int32_t postToPresentMs = msBetween(timeRecords[0].frameTime.postTime, |
| 196 | timeRecords[0].frameTime.presentTime); |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 197 | ALOGV("[%d]-[%" PRIu64 "]-post2present[%d]", layerID, |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 198 | timeRecords[0].frameTime.frameNumber, postToPresentMs); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 199 | timeStatsLayer.deltas["post2present"].insert(postToPresentMs); |
| 200 | |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 201 | const int32_t acquireToPresentMs = msBetween(timeRecords[0].frameTime.acquireTime, |
| 202 | timeRecords[0].frameTime.presentTime); |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 203 | ALOGV("[%d]-[%" PRIu64 "]-acquire2present[%d]", layerID, |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 204 | timeRecords[0].frameTime.frameNumber, acquireToPresentMs); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 205 | timeStatsLayer.deltas["acquire2present"].insert(acquireToPresentMs); |
| 206 | |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 207 | const int32_t latchToPresentMs = msBetween(timeRecords[0].frameTime.latchTime, |
| 208 | timeRecords[0].frameTime.presentTime); |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 209 | ALOGV("[%d]-[%" PRIu64 "]-latch2present[%d]", layerID, |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 210 | timeRecords[0].frameTime.frameNumber, latchToPresentMs); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 211 | timeStatsLayer.deltas["latch2present"].insert(latchToPresentMs); |
| 212 | |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 213 | const int32_t desiredToPresentMs = msBetween(timeRecords[0].frameTime.desiredTime, |
| 214 | timeRecords[0].frameTime.presentTime); |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 215 | ALOGV("[%d]-[%" PRIu64 "]-desired2present[%d]", layerID, |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 216 | timeRecords[0].frameTime.frameNumber, desiredToPresentMs); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 217 | timeStatsLayer.deltas["desired2present"].insert(desiredToPresentMs); |
| 218 | |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 219 | const int32_t presentToPresentMs = msBetween(prevTimeRecord.frameTime.presentTime, |
| 220 | timeRecords[0].frameTime.presentTime); |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 221 | ALOGV("[%d]-[%" PRIu64 "]-present2present[%d]", layerID, |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 222 | timeRecords[0].frameTime.frameNumber, presentToPresentMs); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 223 | timeStatsLayer.deltas["present2present"].insert(presentToPresentMs); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 224 | } |
| 225 | prevTimeRecord = timeRecords[0]; |
Yiwei Zhang | c5f2c45 | 2018-05-08 16:31:56 -0700 | [diff] [blame] | 226 | timeRecords.pop_front(); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 227 | layerRecord.waitData--; |
| 228 | } |
| 229 | } |
| 230 | |
Yiwei Zhang | bd40832 | 2018-10-15 18:31:53 -0700 | [diff] [blame] | 231 | // This regular expression captures the following layer names for instance: |
| 232 | // 1) StatusBat#0 |
| 233 | // 2) NavigationBar#1 |
| 234 | // 3) co(m).*#0 |
| 235 | // 4) SurfaceView - co(m).*#0 |
| 236 | // Using [-\\s\t]+ for the conjunction part between SurfaceView and co(m).* |
| 237 | // is a bit more robust in case there's a slight change. |
| 238 | // The layer name would only consist of . / $ _ 0-9 a-z A-Z in most cases. |
| 239 | static const std::regex layerNameRegex( |
| 240 | "(((SurfaceView[-\\s\\t]+)?com?\\.[./$\\w]+)|((Status|Navigation)Bar))#\\d+"); |
| 241 | |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 242 | static bool layerNameIsValid(const std::string& layerName) { |
Yiwei Zhang | bd40832 | 2018-10-15 18:31:53 -0700 | [diff] [blame] | 243 | return std::regex_match(layerName.begin(), layerName.end(), layerNameRegex); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Yiwei Zhang | 8e8fe52 | 2018-11-02 18:34:07 -0700 | [diff] [blame] | 246 | void TimeStats::setPostTime(int32_t layerID, uint64_t frameNumber, const std::string& layerName, |
| 247 | nsecs_t postTime) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 248 | if (!mEnabled.load()) return; |
| 249 | |
| 250 | ATRACE_CALL(); |
Yiwei Zhang | 8e8fe52 | 2018-11-02 18:34:07 -0700 | [diff] [blame] | 251 | ALOGV("[%d]-[%" PRIu64 "]-[%s]-PostTime[%" PRId64 "]", layerID, frameNumber, layerName.c_str(), |
| 252 | postTime); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 253 | |
| 254 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | e926ab5 | 2019-08-14 15:16:00 -0700 | [diff] [blame] | 255 | if (!mTimeStats.stats.count(layerName) && mTimeStats.stats.size() >= MAX_NUM_LAYER_STATS) { |
| 256 | return; |
| 257 | } |
Yiwei Zhang | 7eb58b7 | 2019-04-22 19:00:02 -0700 | [diff] [blame] | 258 | if (!mTimeStatsTracker.count(layerID) && mTimeStatsTracker.size() < MAX_NUM_LAYER_RECORDS && |
| 259 | layerNameIsValid(layerName)) { |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 260 | mTimeStatsTracker[layerID].layerName = layerName; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 261 | } |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 262 | if (!mTimeStatsTracker.count(layerID)) return; |
| 263 | LayerRecord& layerRecord = mTimeStatsTracker[layerID]; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 264 | if (layerRecord.timeRecords.size() == MAX_NUM_TIME_RECORDS) { |
Yiwei Zhang | af8ee94 | 2018-11-22 00:15:23 -0800 | [diff] [blame] | 265 | ALOGE("[%d]-[%s]-timeRecords is at its maximum size[%zu]. Ignore this when unittesting.", |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 266 | layerID, layerRecord.layerName.c_str(), MAX_NUM_TIME_RECORDS); |
Yiwei Zhang | af8ee94 | 2018-11-22 00:15:23 -0800 | [diff] [blame] | 267 | mTimeStatsTracker.erase(layerID); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 268 | return; |
| 269 | } |
| 270 | // For most media content, the acquireFence is invalid because the buffer is |
| 271 | // ready at the queueBuffer stage. In this case, acquireTime should be given |
| 272 | // a default value as postTime. |
| 273 | TimeRecord timeRecord = { |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 274 | .frameTime = |
| 275 | { |
| 276 | .frameNumber = frameNumber, |
| 277 | .postTime = postTime, |
Yiwei Zhang | af8ee94 | 2018-11-22 00:15:23 -0800 | [diff] [blame] | 278 | .latchTime = postTime, |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 279 | .acquireTime = postTime, |
Yiwei Zhang | af8ee94 | 2018-11-22 00:15:23 -0800 | [diff] [blame] | 280 | .desiredTime = postTime, |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 281 | }, |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 282 | }; |
| 283 | layerRecord.timeRecords.push_back(timeRecord); |
| 284 | if (layerRecord.waitData < 0 || |
| 285 | layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size())) |
| 286 | layerRecord.waitData = layerRecord.timeRecords.size() - 1; |
| 287 | } |
| 288 | |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 289 | void TimeStats::setLatchTime(int32_t layerID, uint64_t frameNumber, nsecs_t latchTime) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 290 | if (!mEnabled.load()) return; |
| 291 | |
| 292 | ATRACE_CALL(); |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 293 | ALOGV("[%d]-[%" PRIu64 "]-LatchTime[%" PRId64 "]", layerID, frameNumber, latchTime); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 294 | |
| 295 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 296 | if (!mTimeStatsTracker.count(layerID)) return; |
| 297 | LayerRecord& layerRecord = mTimeStatsTracker[layerID]; |
Yiwei Zhang | cb7dd00 | 2019-04-16 11:03:01 -0700 | [diff] [blame] | 298 | if (layerRecord.waitData < 0 || |
| 299 | layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size())) |
| 300 | return; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 301 | TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData]; |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 302 | if (timeRecord.frameTime.frameNumber == frameNumber) { |
| 303 | timeRecord.frameTime.latchTime = latchTime; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 307 | void TimeStats::setDesiredTime(int32_t layerID, uint64_t frameNumber, nsecs_t desiredTime) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 308 | if (!mEnabled.load()) return; |
| 309 | |
| 310 | ATRACE_CALL(); |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 311 | ALOGV("[%d]-[%" PRIu64 "]-DesiredTime[%" PRId64 "]", layerID, frameNumber, desiredTime); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 312 | |
| 313 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 314 | if (!mTimeStatsTracker.count(layerID)) return; |
| 315 | LayerRecord& layerRecord = mTimeStatsTracker[layerID]; |
Yiwei Zhang | cb7dd00 | 2019-04-16 11:03:01 -0700 | [diff] [blame] | 316 | if (layerRecord.waitData < 0 || |
| 317 | layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size())) |
| 318 | return; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 319 | TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData]; |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 320 | if (timeRecord.frameTime.frameNumber == frameNumber) { |
| 321 | timeRecord.frameTime.desiredTime = desiredTime; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 325 | void TimeStats::setAcquireTime(int32_t layerID, uint64_t frameNumber, nsecs_t acquireTime) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 326 | if (!mEnabled.load()) return; |
| 327 | |
| 328 | ATRACE_CALL(); |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 329 | ALOGV("[%d]-[%" PRIu64 "]-AcquireTime[%" PRId64 "]", layerID, frameNumber, acquireTime); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 330 | |
| 331 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 332 | if (!mTimeStatsTracker.count(layerID)) return; |
| 333 | LayerRecord& layerRecord = mTimeStatsTracker[layerID]; |
Yiwei Zhang | cb7dd00 | 2019-04-16 11:03:01 -0700 | [diff] [blame] | 334 | if (layerRecord.waitData < 0 || |
| 335 | layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size())) |
| 336 | return; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 337 | TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData]; |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 338 | if (timeRecord.frameTime.frameNumber == frameNumber) { |
| 339 | timeRecord.frameTime.acquireTime = acquireTime; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 343 | void TimeStats::setAcquireFence(int32_t layerID, uint64_t frameNumber, |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 344 | const std::shared_ptr<FenceTime>& acquireFence) { |
| 345 | if (!mEnabled.load()) return; |
| 346 | |
| 347 | ATRACE_CALL(); |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 348 | ALOGV("[%d]-[%" PRIu64 "]-AcquireFenceTime[%" PRId64 "]", layerID, frameNumber, |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 349 | acquireFence->getSignalTime()); |
| 350 | |
| 351 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 352 | if (!mTimeStatsTracker.count(layerID)) return; |
| 353 | LayerRecord& layerRecord = mTimeStatsTracker[layerID]; |
Yiwei Zhang | cb7dd00 | 2019-04-16 11:03:01 -0700 | [diff] [blame] | 354 | if (layerRecord.waitData < 0 || |
| 355 | layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size())) |
| 356 | return; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 357 | TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData]; |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 358 | if (timeRecord.frameTime.frameNumber == frameNumber) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 359 | timeRecord.acquireFence = acquireFence; |
| 360 | } |
| 361 | } |
| 362 | |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 363 | void TimeStats::setPresentTime(int32_t layerID, uint64_t frameNumber, nsecs_t presentTime) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 364 | if (!mEnabled.load()) return; |
| 365 | |
| 366 | ATRACE_CALL(); |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 367 | ALOGV("[%d]-[%" PRIu64 "]-PresentTime[%" PRId64 "]", layerID, frameNumber, presentTime); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 368 | |
| 369 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 370 | if (!mTimeStatsTracker.count(layerID)) return; |
| 371 | LayerRecord& layerRecord = mTimeStatsTracker[layerID]; |
Yiwei Zhang | cb7dd00 | 2019-04-16 11:03:01 -0700 | [diff] [blame] | 372 | if (layerRecord.waitData < 0 || |
| 373 | layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size())) |
| 374 | return; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 375 | TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData]; |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 376 | if (timeRecord.frameTime.frameNumber == frameNumber) { |
| 377 | timeRecord.frameTime.presentTime = presentTime; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 378 | timeRecord.ready = true; |
| 379 | layerRecord.waitData++; |
| 380 | } |
| 381 | |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 382 | flushAvailableRecordsToStatsLocked(layerID); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 385 | void TimeStats::setPresentFence(int32_t layerID, uint64_t frameNumber, |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 386 | const std::shared_ptr<FenceTime>& presentFence) { |
| 387 | if (!mEnabled.load()) return; |
| 388 | |
| 389 | ATRACE_CALL(); |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 390 | ALOGV("[%d]-[%" PRIu64 "]-PresentFenceTime[%" PRId64 "]", layerID, frameNumber, |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 391 | presentFence->getSignalTime()); |
| 392 | |
| 393 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 394 | if (!mTimeStatsTracker.count(layerID)) return; |
| 395 | LayerRecord& layerRecord = mTimeStatsTracker[layerID]; |
Yiwei Zhang | cb7dd00 | 2019-04-16 11:03:01 -0700 | [diff] [blame] | 396 | if (layerRecord.waitData < 0 || |
| 397 | layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size())) |
| 398 | return; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 399 | TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData]; |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 400 | if (timeRecord.frameTime.frameNumber == frameNumber) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 401 | timeRecord.presentFence = presentFence; |
| 402 | timeRecord.ready = true; |
| 403 | layerRecord.waitData++; |
| 404 | } |
| 405 | |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 406 | flushAvailableRecordsToStatsLocked(layerID); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 409 | void TimeStats::onDestroy(int32_t layerID) { |
Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 410 | ATRACE_CALL(); |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 411 | ALOGV("[%d]-onDestroy", layerID); |
Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame^] | 412 | std::lock_guard<std::mutex> lock(mMutex); |
| 413 | mTimeStatsTracker.erase(layerID); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 416 | void TimeStats::removeTimeRecord(int32_t layerID, uint64_t frameNumber) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 417 | if (!mEnabled.load()) return; |
| 418 | |
| 419 | ATRACE_CALL(); |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 420 | ALOGV("[%d]-[%" PRIu64 "]-removeTimeRecord", layerID, frameNumber); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 421 | |
| 422 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 423 | if (!mTimeStatsTracker.count(layerID)) return; |
| 424 | LayerRecord& layerRecord = mTimeStatsTracker[layerID]; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 425 | size_t removeAt = 0; |
| 426 | for (const TimeRecord& record : layerRecord.timeRecords) { |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 427 | if (record.frameTime.frameNumber == frameNumber) break; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 428 | removeAt++; |
| 429 | } |
| 430 | if (removeAt == layerRecord.timeRecords.size()) return; |
| 431 | layerRecord.timeRecords.erase(layerRecord.timeRecords.begin() + removeAt); |
| 432 | if (layerRecord.waitData > static_cast<int32_t>(removeAt)) { |
Yiwei Zhang | eaeea06 | 2018-06-28 14:46:51 -0700 | [diff] [blame] | 433 | layerRecord.waitData--; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 434 | } |
Yiwei Zhang | eaeea06 | 2018-06-28 14:46:51 -0700 | [diff] [blame] | 435 | layerRecord.droppedFrames++; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 438 | void TimeStats::flushPowerTimeLocked() { |
Yiwei Zhang | e5c49d5 | 2018-10-29 00:15:31 -0700 | [diff] [blame] | 439 | if (!mEnabled.load()) return; |
| 440 | |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 441 | nsecs_t curTime = systemTime(); |
| 442 | // elapsedTime is in milliseconds. |
| 443 | int64_t elapsedTime = (curTime - mPowerTime.prevTime) / 1000000; |
| 444 | |
| 445 | switch (mPowerTime.powerMode) { |
| 446 | case HWC_POWER_MODE_NORMAL: |
| 447 | mTimeStats.displayOnTime += elapsedTime; |
| 448 | break; |
| 449 | case HWC_POWER_MODE_OFF: |
| 450 | case HWC_POWER_MODE_DOZE: |
| 451 | case HWC_POWER_MODE_DOZE_SUSPEND: |
| 452 | default: |
| 453 | break; |
| 454 | } |
| 455 | |
| 456 | mPowerTime.prevTime = curTime; |
| 457 | } |
| 458 | |
| 459 | void TimeStats::setPowerMode(int32_t powerMode) { |
| 460 | if (!mEnabled.load()) { |
| 461 | std::lock_guard<std::mutex> lock(mMutex); |
| 462 | mPowerTime.powerMode = powerMode; |
| 463 | return; |
| 464 | } |
| 465 | |
| 466 | std::lock_guard<std::mutex> lock(mMutex); |
| 467 | if (powerMode == mPowerTime.powerMode) return; |
| 468 | |
| 469 | flushPowerTimeLocked(); |
| 470 | mPowerTime.powerMode = powerMode; |
| 471 | } |
| 472 | |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 473 | void TimeStats::recordRefreshRate(uint32_t fps, nsecs_t duration) { |
| 474 | std::lock_guard<std::mutex> lock(mMutex); |
| 475 | if (mTimeStats.refreshRateStats.count(fps)) { |
| 476 | mTimeStats.refreshRateStats[fps] += duration; |
| 477 | } else { |
| 478 | mTimeStats.refreshRateStats.insert({fps, duration}); |
| 479 | } |
| 480 | } |
| 481 | |
Yiwei Zhang | ce6ebc0 | 2018-10-20 12:42:38 -0700 | [diff] [blame] | 482 | void TimeStats::flushAvailableGlobalRecordsToStatsLocked() { |
| 483 | ATRACE_CALL(); |
| 484 | |
| 485 | while (!mGlobalRecord.presentFences.empty()) { |
| 486 | const nsecs_t curPresentTime = mGlobalRecord.presentFences.front()->getSignalTime(); |
| 487 | if (curPresentTime == Fence::SIGNAL_TIME_PENDING) break; |
| 488 | |
| 489 | if (curPresentTime == Fence::SIGNAL_TIME_INVALID) { |
| 490 | ALOGE("GlobalPresentFence is invalid!"); |
| 491 | mGlobalRecord.prevPresentTime = 0; |
| 492 | mGlobalRecord.presentFences.pop_front(); |
| 493 | continue; |
| 494 | } |
| 495 | |
| 496 | ALOGV("GlobalPresentFenceTime[%" PRId64 "]", |
| 497 | mGlobalRecord.presentFences.front()->getSignalTime()); |
| 498 | |
Yiwei Zhang | e5c49d5 | 2018-10-29 00:15:31 -0700 | [diff] [blame] | 499 | if (mGlobalRecord.prevPresentTime != 0) { |
| 500 | const int32_t presentToPresentMs = |
| 501 | msBetween(mGlobalRecord.prevPresentTime, curPresentTime); |
| 502 | ALOGV("Global present2present[%d] prev[%" PRId64 "] curr[%" PRId64 "]", |
| 503 | presentToPresentMs, mGlobalRecord.prevPresentTime, curPresentTime); |
| 504 | mTimeStats.presentToPresent.insert(presentToPresentMs); |
| 505 | } |
Yiwei Zhang | ce6ebc0 | 2018-10-20 12:42:38 -0700 | [diff] [blame] | 506 | |
Yiwei Zhang | ce6ebc0 | 2018-10-20 12:42:38 -0700 | [diff] [blame] | 507 | mGlobalRecord.prevPresentTime = curPresentTime; |
| 508 | mGlobalRecord.presentFences.pop_front(); |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | void TimeStats::setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) { |
| 513 | if (!mEnabled.load()) return; |
| 514 | |
| 515 | ATRACE_CALL(); |
| 516 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | e5c49d5 | 2018-10-29 00:15:31 -0700 | [diff] [blame] | 517 | if (presentFence == nullptr || !presentFence->isValid()) { |
| 518 | mGlobalRecord.prevPresentTime = 0; |
| 519 | return; |
| 520 | } |
| 521 | |
| 522 | if (mPowerTime.powerMode != HWC_POWER_MODE_NORMAL) { |
| 523 | // Try flushing the last present fence on HWC_POWER_MODE_NORMAL. |
| 524 | flushAvailableGlobalRecordsToStatsLocked(); |
| 525 | mGlobalRecord.presentFences.clear(); |
Yiwei Zhang | ce6ebc0 | 2018-10-20 12:42:38 -0700 | [diff] [blame] | 526 | mGlobalRecord.prevPresentTime = 0; |
| 527 | return; |
| 528 | } |
| 529 | |
| 530 | if (mGlobalRecord.presentFences.size() == MAX_NUM_TIME_RECORDS) { |
| 531 | // The front presentFence must be trapped in pending status in this |
| 532 | // case. Try dequeuing the front one to recover. |
| 533 | ALOGE("GlobalPresentFences is already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS); |
| 534 | mGlobalRecord.prevPresentTime = 0; |
| 535 | mGlobalRecord.presentFences.pop_front(); |
| 536 | } |
| 537 | |
| 538 | mGlobalRecord.presentFences.emplace_back(presentFence); |
| 539 | flushAvailableGlobalRecordsToStatsLocked(); |
| 540 | } |
| 541 | |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 542 | void TimeStats::enable() { |
| 543 | if (mEnabled.load()) return; |
| 544 | |
| 545 | ATRACE_CALL(); |
| 546 | |
| 547 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 548 | mEnabled.store(true); |
Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 549 | mTimeStats.statsStart = static_cast<int64_t>(std::time(0)); |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 550 | mPowerTime.prevTime = systemTime(); |
Yiwei Zhang | e5c49d5 | 2018-10-29 00:15:31 -0700 | [diff] [blame] | 551 | ALOGD("Enabled"); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | void TimeStats::disable() { |
| 555 | if (!mEnabled.load()) return; |
| 556 | |
| 557 | ATRACE_CALL(); |
| 558 | |
| 559 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | e5c49d5 | 2018-10-29 00:15:31 -0700 | [diff] [blame] | 560 | flushPowerTimeLocked(); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 561 | mEnabled.store(false); |
Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 562 | mTimeStats.statsEnd = static_cast<int64_t>(std::time(0)); |
Yiwei Zhang | e5c49d5 | 2018-10-29 00:15:31 -0700 | [diff] [blame] | 563 | ALOGD("Disabled"); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | void TimeStats::clear() { |
| 567 | ATRACE_CALL(); |
| 568 | |
| 569 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | e5c49d5 | 2018-10-29 00:15:31 -0700 | [diff] [blame] | 570 | mTimeStatsTracker.clear(); |
Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 571 | mTimeStats.stats.clear(); |
| 572 | mTimeStats.statsStart = (mEnabled.load() ? static_cast<int64_t>(std::time(0)) : 0); |
| 573 | mTimeStats.statsEnd = 0; |
| 574 | mTimeStats.totalFrames = 0; |
| 575 | mTimeStats.missedFrames = 0; |
| 576 | mTimeStats.clientCompositionFrames = 0; |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 577 | mTimeStats.displayOnTime = 0; |
Yiwei Zhang | ce6ebc0 | 2018-10-20 12:42:38 -0700 | [diff] [blame] | 578 | mTimeStats.presentToPresent.hist.clear(); |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 579 | mTimeStats.refreshRateStats.clear(); |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 580 | mPowerTime.prevTime = systemTime(); |
Yiwei Zhang | e5c49d5 | 2018-10-29 00:15:31 -0700 | [diff] [blame] | 581 | mGlobalRecord.prevPresentTime = 0; |
| 582 | mGlobalRecord.presentFences.clear(); |
| 583 | ALOGD("Cleared"); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | bool TimeStats::isEnabled() { |
| 587 | return mEnabled.load(); |
| 588 | } |
| 589 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 590 | void TimeStats::dump(bool asProto, std::optional<uint32_t> maxLayers, std::string& result) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 591 | ATRACE_CALL(); |
| 592 | |
| 593 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 594 | if (mTimeStats.statsStart == 0) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 595 | return; |
| 596 | } |
| 597 | |
Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 598 | mTimeStats.statsEnd = static_cast<int64_t>(std::time(0)); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 599 | |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 600 | flushPowerTimeLocked(); |
| 601 | |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 602 | if (asProto) { |
Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 603 | ALOGD("Dumping TimeStats as proto"); |
Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 604 | SFTimeStatsGlobalProto timeStatsProto = mTimeStats.toProto(maxLayers); |
Dominik Laskowski | 4647011 | 2019-08-02 13:13:11 -0700 | [diff] [blame] | 605 | result.append(timeStatsProto.SerializeAsString()); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 606 | } else { |
Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 607 | ALOGD("Dumping TimeStats as text"); |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 608 | result.append(mTimeStats.toString(maxLayers)); |
Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 609 | result.append("\n"); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 610 | } |
| 611 | } |
| 612 | |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 613 | } // namespace impl |
| 614 | |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 615 | } // namespace android |