| 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 |  | 
|  | 70 | void TimeStats::incrementTotalFrames() { | 
|  | 71 | if (!mEnabled.load()) return; | 
|  | 72 |  | 
|  | 73 | ATRACE_CALL(); | 
|  | 74 |  | 
|  | 75 | std::lock_guard<std::mutex> lock(mMutex); | 
| Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 76 | mTimeStats.totalFrames++; | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 77 | } | 
|  | 78 |  | 
| Yiwei Zhang | 621f9d4 | 2018-05-07 10:40:55 -0700 | [diff] [blame] | 79 | void TimeStats::incrementMissedFrames() { | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 80 | if (!mEnabled.load()) return; | 
|  | 81 |  | 
|  | 82 | ATRACE_CALL(); | 
|  | 83 |  | 
|  | 84 | std::lock_guard<std::mutex> lock(mMutex); | 
| Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 85 | mTimeStats.missedFrames++; | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 86 | } | 
|  | 87 |  | 
|  | 88 | void TimeStats::incrementClientCompositionFrames() { | 
|  | 89 | if (!mEnabled.load()) return; | 
|  | 90 |  | 
|  | 91 | ATRACE_CALL(); | 
|  | 92 |  | 
|  | 93 | std::lock_guard<std::mutex> lock(mMutex); | 
| Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 94 | mTimeStats.clientCompositionFrames++; | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 95 | } | 
|  | 96 |  | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 97 | bool TimeStats::recordReadyLocked(int32_t layerID, TimeRecord* timeRecord) { | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 98 | if (!timeRecord->ready) { | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 99 | ALOGV("[%d]-[%" PRIu64 "]-presentFence is still not received", layerID, | 
| Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 100 | timeRecord->frameTime.frameNumber); | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 101 | return false; | 
|  | 102 | } | 
|  | 103 |  | 
|  | 104 | if (timeRecord->acquireFence != nullptr) { | 
|  | 105 | if (timeRecord->acquireFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) { | 
|  | 106 | return false; | 
|  | 107 | } | 
|  | 108 | if (timeRecord->acquireFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) { | 
| Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 109 | timeRecord->frameTime.acquireTime = timeRecord->acquireFence->getSignalTime(); | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 110 | timeRecord->acquireFence = nullptr; | 
|  | 111 | } else { | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 112 | ALOGV("[%d]-[%" PRIu64 "]-acquireFence signal time is invalid", layerID, | 
| Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 113 | timeRecord->frameTime.frameNumber); | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 114 | } | 
|  | 115 | } | 
|  | 116 |  | 
|  | 117 | if (timeRecord->presentFence != nullptr) { | 
|  | 118 | if (timeRecord->presentFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) { | 
|  | 119 | return false; | 
|  | 120 | } | 
|  | 121 | if (timeRecord->presentFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) { | 
| Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 122 | timeRecord->frameTime.presentTime = timeRecord->presentFence->getSignalTime(); | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 123 | timeRecord->presentFence = nullptr; | 
|  | 124 | } else { | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 125 | ALOGV("[%d]-[%" PRIu64 "]-presentFence signal time invalid", layerID, | 
| Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 126 | timeRecord->frameTime.frameNumber); | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 127 | } | 
|  | 128 | } | 
|  | 129 |  | 
|  | 130 | return true; | 
|  | 131 | } | 
|  | 132 |  | 
|  | 133 | static int32_t msBetween(nsecs_t start, nsecs_t end) { | 
|  | 134 | int64_t delta = (end - start) / 1000000; | 
|  | 135 | delta = std::clamp(delta, int64_t(INT32_MIN), int64_t(INT32_MAX)); | 
|  | 136 | return static_cast<int32_t>(delta); | 
|  | 137 | } | 
|  | 138 |  | 
| Yiwei Zhang | bd40832 | 2018-10-15 18:31:53 -0700 | [diff] [blame] | 139 | // This regular expression captures the following for instance: | 
|  | 140 | // StatusBar in StatusBar#0 | 
|  | 141 | // com.appname in com.appname/com.appname.activity#0 | 
|  | 142 | // com.appname in SurfaceView - com.appname/com.appname.activity#0 | 
|  | 143 | static const std::regex packageNameRegex("(?:SurfaceView[-\\s\\t]+)?([^/]+).*#\\d+"); | 
|  | 144 |  | 
| Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 145 | static std::string getPackageName(const std::string& layerName) { | 
| Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 146 | std::smatch match; | 
| Yiwei Zhang | bd40832 | 2018-10-15 18:31:53 -0700 | [diff] [blame] | 147 | if (std::regex_match(layerName.begin(), layerName.end(), match, packageNameRegex)) { | 
| Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 148 | // There must be a match for group 1 otherwise the whole string is not | 
|  | 149 | // matched and the above will return false | 
|  | 150 | return match[1]; | 
|  | 151 | } | 
|  | 152 | return ""; | 
|  | 153 | } | 
|  | 154 |  | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 155 | void TimeStats::flushAvailableRecordsToStatsLocked(int32_t layerID) { | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 156 | ATRACE_CALL(); | 
|  | 157 |  | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 158 | LayerRecord& layerRecord = mTimeStatsTracker[layerID]; | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 159 | TimeRecord& prevTimeRecord = layerRecord.prevTimeRecord; | 
| Yiwei Zhang | c5f2c45 | 2018-05-08 16:31:56 -0700 | [diff] [blame] | 160 | std::deque<TimeRecord>& timeRecords = layerRecord.timeRecords; | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 161 | while (!timeRecords.empty()) { | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 162 | if (!recordReadyLocked(layerID, &timeRecords[0])) break; | 
|  | 163 | ALOGV("[%d]-[%" PRIu64 "]-presentFenceTime[%" PRId64 "]", layerID, | 
| Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 164 | timeRecords[0].frameTime.frameNumber, timeRecords[0].frameTime.presentTime); | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 165 |  | 
| Raymond Chiu | 361ea97 | 2018-10-26 14:14:37 -0700 | [diff] [blame] | 166 | const std::string& layerName = layerRecord.layerName; | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 167 | if (prevTimeRecord.ready) { | 
| Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 168 | if (!mTimeStats.stats.count(layerName)) { | 
|  | 169 | mTimeStats.stats[layerName].layerName = layerName; | 
|  | 170 | mTimeStats.stats[layerName].packageName = getPackageName(layerName); | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 171 | } | 
| Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 172 | TimeStatsHelper::TimeStatsLayer& timeStatsLayer = mTimeStats.stats[layerName]; | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 173 | timeStatsLayer.totalFrames++; | 
| Yiwei Zhang | eaeea06 | 2018-06-28 14:46:51 -0700 | [diff] [blame] | 174 | timeStatsLayer.droppedFrames += layerRecord.droppedFrames; | 
|  | 175 | layerRecord.droppedFrames = 0; | 
|  | 176 |  | 
|  | 177 | const int32_t postToAcquireMs = msBetween(timeRecords[0].frameTime.postTime, | 
|  | 178 | timeRecords[0].frameTime.acquireTime); | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 179 | ALOGV("[%d]-[%" PRIu64 "]-post2acquire[%d]", layerID, | 
| Yiwei Zhang | eaeea06 | 2018-06-28 14:46:51 -0700 | [diff] [blame] | 180 | timeRecords[0].frameTime.frameNumber, postToAcquireMs); | 
|  | 181 | timeStatsLayer.deltas["post2acquire"].insert(postToAcquireMs); | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 182 |  | 
| Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 183 | const int32_t postToPresentMs = msBetween(timeRecords[0].frameTime.postTime, | 
|  | 184 | timeRecords[0].frameTime.presentTime); | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 185 | ALOGV("[%d]-[%" PRIu64 "]-post2present[%d]", layerID, | 
| Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 186 | timeRecords[0].frameTime.frameNumber, postToPresentMs); | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 187 | timeStatsLayer.deltas["post2present"].insert(postToPresentMs); | 
|  | 188 |  | 
| Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 189 | const int32_t acquireToPresentMs = msBetween(timeRecords[0].frameTime.acquireTime, | 
|  | 190 | timeRecords[0].frameTime.presentTime); | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 191 | ALOGV("[%d]-[%" PRIu64 "]-acquire2present[%d]", layerID, | 
| Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 192 | timeRecords[0].frameTime.frameNumber, acquireToPresentMs); | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 193 | timeStatsLayer.deltas["acquire2present"].insert(acquireToPresentMs); | 
|  | 194 |  | 
| Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 195 | const int32_t latchToPresentMs = msBetween(timeRecords[0].frameTime.latchTime, | 
|  | 196 | timeRecords[0].frameTime.presentTime); | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 197 | ALOGV("[%d]-[%" PRIu64 "]-latch2present[%d]", layerID, | 
| Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 198 | timeRecords[0].frameTime.frameNumber, latchToPresentMs); | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 199 | timeStatsLayer.deltas["latch2present"].insert(latchToPresentMs); | 
|  | 200 |  | 
| Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 201 | const int32_t desiredToPresentMs = msBetween(timeRecords[0].frameTime.desiredTime, | 
|  | 202 | timeRecords[0].frameTime.presentTime); | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 203 | ALOGV("[%d]-[%" PRIu64 "]-desired2present[%d]", layerID, | 
| Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 204 | timeRecords[0].frameTime.frameNumber, desiredToPresentMs); | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 205 | timeStatsLayer.deltas["desired2present"].insert(desiredToPresentMs); | 
|  | 206 |  | 
| Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 207 | const int32_t presentToPresentMs = msBetween(prevTimeRecord.frameTime.presentTime, | 
|  | 208 | timeRecords[0].frameTime.presentTime); | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 209 | ALOGV("[%d]-[%" PRIu64 "]-present2present[%d]", layerID, | 
| Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 210 | timeRecords[0].frameTime.frameNumber, presentToPresentMs); | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 211 | timeStatsLayer.deltas["present2present"].insert(presentToPresentMs); | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 212 | } | 
| Raymond Chiu | 361ea97 | 2018-10-26 14:14:37 -0700 | [diff] [blame] | 213 |  | 
|  | 214 | // Output additional trace points to track frame time. | 
|  | 215 | ATRACE_INT64(("TimeStats-Post - " + layerName).c_str(), timeRecords[0].frameTime.postTime); | 
|  | 216 | ATRACE_INT64(("TimeStats-Acquire - " + layerName).c_str(), | 
|  | 217 | timeRecords[0].frameTime.acquireTime); | 
|  | 218 | ATRACE_INT64(("TimeStats-Latch - " + layerName).c_str(), | 
|  | 219 | timeRecords[0].frameTime.latchTime); | 
|  | 220 | ATRACE_INT64(("TimeStats-Desired - " + layerName).c_str(), | 
|  | 221 | timeRecords[0].frameTime.desiredTime); | 
|  | 222 | ATRACE_INT64(("TimeStats-Present - " + layerName).c_str(), | 
|  | 223 | timeRecords[0].frameTime.presentTime); | 
|  | 224 |  | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 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 | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 255 | if (!mTimeStatsTracker.count(layerID) && layerNameIsValid(layerName)) { | 
|  | 256 | mTimeStatsTracker[layerID].layerName = layerName; | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 257 | } | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 258 | if (!mTimeStatsTracker.count(layerID)) return; | 
|  | 259 | LayerRecord& layerRecord = mTimeStatsTracker[layerID]; | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 260 | if (layerRecord.timeRecords.size() == MAX_NUM_TIME_RECORDS) { | 
| Yiwei Zhang | af8ee94 | 2018-11-22 00:15:23 -0800 | [diff] [blame] | 261 | 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] | 262 | layerID, layerRecord.layerName.c_str(), MAX_NUM_TIME_RECORDS); | 
| Yiwei Zhang | af8ee94 | 2018-11-22 00:15:23 -0800 | [diff] [blame] | 263 | mTimeStatsTracker.erase(layerID); | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 264 | return; | 
|  | 265 | } | 
|  | 266 | // For most media content, the acquireFence is invalid because the buffer is | 
|  | 267 | // ready at the queueBuffer stage. In this case, acquireTime should be given | 
|  | 268 | // a default value as postTime. | 
|  | 269 | TimeRecord timeRecord = { | 
| Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 270 | .frameTime = | 
|  | 271 | { | 
|  | 272 | .frameNumber = frameNumber, | 
|  | 273 | .postTime = postTime, | 
| Yiwei Zhang | af8ee94 | 2018-11-22 00:15:23 -0800 | [diff] [blame] | 274 | .latchTime = postTime, | 
| Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 275 | .acquireTime = postTime, | 
| Yiwei Zhang | af8ee94 | 2018-11-22 00:15:23 -0800 | [diff] [blame] | 276 | .desiredTime = postTime, | 
| Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 277 | }, | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 278 | }; | 
|  | 279 | layerRecord.timeRecords.push_back(timeRecord); | 
|  | 280 | if (layerRecord.waitData < 0 || | 
|  | 281 | layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size())) | 
|  | 282 | layerRecord.waitData = layerRecord.timeRecords.size() - 1; | 
|  | 283 | } | 
|  | 284 |  | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 285 | void TimeStats::setLatchTime(int32_t layerID, uint64_t frameNumber, nsecs_t latchTime) { | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 286 | if (!mEnabled.load()) return; | 
|  | 287 |  | 
|  | 288 | ATRACE_CALL(); | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 289 | ALOGV("[%d]-[%" PRIu64 "]-LatchTime[%" PRId64 "]", layerID, frameNumber, latchTime); | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 290 |  | 
|  | 291 | std::lock_guard<std::mutex> lock(mMutex); | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 292 | if (!mTimeStatsTracker.count(layerID)) return; | 
|  | 293 | LayerRecord& layerRecord = mTimeStatsTracker[layerID]; | 
| Yiwei Zhang | cb7dd00 | 2019-04-16 11:03:01 -0700 | [diff] [blame] | 294 | if (layerRecord.waitData < 0 || | 
|  | 295 | layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size())) | 
|  | 296 | return; | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 297 | TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData]; | 
| Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 298 | if (timeRecord.frameTime.frameNumber == frameNumber) { | 
|  | 299 | timeRecord.frameTime.latchTime = latchTime; | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 300 | } | 
|  | 301 | } | 
|  | 302 |  | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 303 | void TimeStats::setDesiredTime(int32_t layerID, uint64_t frameNumber, nsecs_t desiredTime) { | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 304 | if (!mEnabled.load()) return; | 
|  | 305 |  | 
|  | 306 | ATRACE_CALL(); | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 307 | ALOGV("[%d]-[%" PRIu64 "]-DesiredTime[%" PRId64 "]", layerID, frameNumber, desiredTime); | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 308 |  | 
|  | 309 | std::lock_guard<std::mutex> lock(mMutex); | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 310 | if (!mTimeStatsTracker.count(layerID)) return; | 
|  | 311 | LayerRecord& layerRecord = mTimeStatsTracker[layerID]; | 
| Yiwei Zhang | cb7dd00 | 2019-04-16 11:03:01 -0700 | [diff] [blame] | 312 | if (layerRecord.waitData < 0 || | 
|  | 313 | layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size())) | 
|  | 314 | return; | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 315 | TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData]; | 
| Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 316 | if (timeRecord.frameTime.frameNumber == frameNumber) { | 
|  | 317 | timeRecord.frameTime.desiredTime = desiredTime; | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 318 | } | 
|  | 319 | } | 
|  | 320 |  | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 321 | void TimeStats::setAcquireTime(int32_t layerID, uint64_t frameNumber, nsecs_t acquireTime) { | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 322 | if (!mEnabled.load()) return; | 
|  | 323 |  | 
|  | 324 | ATRACE_CALL(); | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 325 | ALOGV("[%d]-[%" PRIu64 "]-AcquireTime[%" PRId64 "]", layerID, frameNumber, acquireTime); | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 326 |  | 
|  | 327 | std::lock_guard<std::mutex> lock(mMutex); | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 328 | if (!mTimeStatsTracker.count(layerID)) return; | 
|  | 329 | LayerRecord& layerRecord = mTimeStatsTracker[layerID]; | 
| Yiwei Zhang | cb7dd00 | 2019-04-16 11:03:01 -0700 | [diff] [blame] | 330 | if (layerRecord.waitData < 0 || | 
|  | 331 | layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size())) | 
|  | 332 | return; | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 333 | TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData]; | 
| Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 334 | if (timeRecord.frameTime.frameNumber == frameNumber) { | 
|  | 335 | timeRecord.frameTime.acquireTime = acquireTime; | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 336 | } | 
|  | 337 | } | 
|  | 338 |  | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 339 | void TimeStats::setAcquireFence(int32_t layerID, uint64_t frameNumber, | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 340 | const std::shared_ptr<FenceTime>& acquireFence) { | 
|  | 341 | if (!mEnabled.load()) return; | 
|  | 342 |  | 
|  | 343 | ATRACE_CALL(); | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 344 | ALOGV("[%d]-[%" PRIu64 "]-AcquireFenceTime[%" PRId64 "]", layerID, frameNumber, | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 345 | acquireFence->getSignalTime()); | 
|  | 346 |  | 
|  | 347 | std::lock_guard<std::mutex> lock(mMutex); | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 348 | if (!mTimeStatsTracker.count(layerID)) return; | 
|  | 349 | LayerRecord& layerRecord = mTimeStatsTracker[layerID]; | 
| Yiwei Zhang | cb7dd00 | 2019-04-16 11:03:01 -0700 | [diff] [blame] | 350 | if (layerRecord.waitData < 0 || | 
|  | 351 | layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size())) | 
|  | 352 | return; | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 353 | TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData]; | 
| Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 354 | if (timeRecord.frameTime.frameNumber == frameNumber) { | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 355 | timeRecord.acquireFence = acquireFence; | 
|  | 356 | } | 
|  | 357 | } | 
|  | 358 |  | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 359 | void TimeStats::setPresentTime(int32_t layerID, uint64_t frameNumber, nsecs_t presentTime) { | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 360 | if (!mEnabled.load()) return; | 
|  | 361 |  | 
|  | 362 | ATRACE_CALL(); | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 363 | ALOGV("[%d]-[%" PRIu64 "]-PresentTime[%" PRId64 "]", layerID, frameNumber, presentTime); | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 364 |  | 
|  | 365 | std::lock_guard<std::mutex> lock(mMutex); | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 366 | if (!mTimeStatsTracker.count(layerID)) return; | 
|  | 367 | LayerRecord& layerRecord = mTimeStatsTracker[layerID]; | 
| Yiwei Zhang | cb7dd00 | 2019-04-16 11:03:01 -0700 | [diff] [blame] | 368 | if (layerRecord.waitData < 0 || | 
|  | 369 | layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size())) | 
|  | 370 | return; | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 371 | TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData]; | 
| Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 372 | if (timeRecord.frameTime.frameNumber == frameNumber) { | 
|  | 373 | timeRecord.frameTime.presentTime = presentTime; | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 374 | timeRecord.ready = true; | 
|  | 375 | layerRecord.waitData++; | 
|  | 376 | } | 
|  | 377 |  | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 378 | flushAvailableRecordsToStatsLocked(layerID); | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 379 | } | 
|  | 380 |  | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 381 | void TimeStats::setPresentFence(int32_t layerID, uint64_t frameNumber, | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 382 | const std::shared_ptr<FenceTime>& presentFence) { | 
|  | 383 | if (!mEnabled.load()) return; | 
|  | 384 |  | 
|  | 385 | ATRACE_CALL(); | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 386 | ALOGV("[%d]-[%" PRIu64 "]-PresentFenceTime[%" PRId64 "]", layerID, frameNumber, | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 387 | presentFence->getSignalTime()); | 
|  | 388 |  | 
|  | 389 | std::lock_guard<std::mutex> lock(mMutex); | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 390 | if (!mTimeStatsTracker.count(layerID)) return; | 
|  | 391 | LayerRecord& layerRecord = mTimeStatsTracker[layerID]; | 
| Yiwei Zhang | cb7dd00 | 2019-04-16 11:03:01 -0700 | [diff] [blame] | 392 | if (layerRecord.waitData < 0 || | 
|  | 393 | layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size())) | 
|  | 394 | return; | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 395 | TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData]; | 
| Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 396 | if (timeRecord.frameTime.frameNumber == frameNumber) { | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 397 | timeRecord.presentFence = presentFence; | 
|  | 398 | timeRecord.ready = true; | 
|  | 399 | layerRecord.waitData++; | 
|  | 400 | } | 
|  | 401 |  | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 402 | flushAvailableRecordsToStatsLocked(layerID); | 
| Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 403 | } | 
|  | 404 |  | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 405 | void TimeStats::onDestroy(int32_t layerID) { | 
| Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 406 | if (!mEnabled.load()) return; | 
|  | 407 |  | 
|  | 408 | ATRACE_CALL(); | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 409 | ALOGV("[%d]-onDestroy", layerID); | 
| Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 410 |  | 
|  | 411 | std::lock_guard<std::mutex> lock(mMutex); | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 412 | if (!mTimeStatsTracker.count(layerID)) return; | 
| Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 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); | 
| Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 605 | result.append(timeStatsProto.SerializeAsString().c_str(), timeStatsProto.ByteSize()); | 
| 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 |