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