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> |
| 27 | #include <utils/Trace.h> |
| 28 | |
| 29 | #include <algorithm> |
| 30 | #include <regex> |
| 31 | |
| 32 | namespace android { |
| 33 | |
| 34 | TimeStats& TimeStats::getInstance() { |
Chia-I Wu | ebd8888 | 2018-09-14 11:02:37 -0700 | [diff] [blame] | 35 | static TimeStats sInstance; |
| 36 | return sInstance; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | void TimeStats::parseArgs(bool asProto, const Vector<String16>& args, size_t& index, |
| 40 | String8& result) { |
| 41 | ATRACE_CALL(); |
| 42 | |
| 43 | if (args.size() > index + 10) { |
| 44 | ALOGD("Invalid args count"); |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | std::unordered_map<std::string, int32_t> argsMap; |
| 49 | while (index < args.size()) { |
| 50 | argsMap[std::string(String8(args[index]).c_str())] = index; |
| 51 | ++index; |
| 52 | } |
| 53 | |
| 54 | if (argsMap.count("-disable")) { |
| 55 | disable(); |
| 56 | } |
| 57 | |
| 58 | if (argsMap.count("-dump")) { |
Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 59 | std::optional<uint32_t> maxLayers = std::nullopt; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 60 | auto iter = argsMap.find("-maxlayers"); |
| 61 | 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] | 62 | int64_t value = strtol(String8(args[iter->second + 1]).c_str(), nullptr, 10); |
| 63 | value = std::clamp(value, int64_t(0), int64_t(UINT32_MAX)); |
| 64 | maxLayers = static_cast<uint32_t>(value); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 67 | dump(asProto, maxLayers, result); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | if (argsMap.count("-clear")) { |
| 71 | clear(); |
| 72 | } |
| 73 | |
| 74 | if (argsMap.count("-enable")) { |
| 75 | enable(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | void TimeStats::incrementTotalFrames() { |
| 80 | if (!mEnabled.load()) return; |
| 81 | |
| 82 | ATRACE_CALL(); |
| 83 | |
| 84 | std::lock_guard<std::mutex> lock(mMutex); |
| 85 | timeStats.totalFrames++; |
| 86 | } |
| 87 | |
Yiwei Zhang | 621f9d4 | 2018-05-07 10:40:55 -0700 | [diff] [blame] | 88 | void TimeStats::incrementMissedFrames() { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 89 | if (!mEnabled.load()) return; |
| 90 | |
| 91 | ATRACE_CALL(); |
| 92 | |
| 93 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 94 | timeStats.missedFrames++; |
| 95 | } |
| 96 | |
| 97 | void TimeStats::incrementClientCompositionFrames() { |
| 98 | if (!mEnabled.load()) return; |
| 99 | |
| 100 | ATRACE_CALL(); |
| 101 | |
| 102 | std::lock_guard<std::mutex> lock(mMutex); |
| 103 | timeStats.clientCompositionFrames++; |
| 104 | } |
| 105 | |
| 106 | bool TimeStats::recordReadyLocked(const std::string& layerName, TimeRecord* timeRecord) { |
| 107 | if (!timeRecord->ready) { |
| 108 | ALOGV("[%s]-[%" PRIu64 "]-presentFence is still not received", layerName.c_str(), |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 109 | timeRecord->frameTime.frameNumber); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 110 | return false; |
| 111 | } |
| 112 | |
| 113 | if (timeRecord->acquireFence != nullptr) { |
| 114 | if (timeRecord->acquireFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) { |
| 115 | return false; |
| 116 | } |
| 117 | if (timeRecord->acquireFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) { |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 118 | timeRecord->frameTime.acquireTime = timeRecord->acquireFence->getSignalTime(); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 119 | timeRecord->acquireFence = nullptr; |
| 120 | } else { |
| 121 | ALOGV("[%s]-[%" PRIu64 "]-acquireFence signal time is invalid", layerName.c_str(), |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 122 | timeRecord->frameTime.frameNumber); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
| 126 | if (timeRecord->presentFence != nullptr) { |
| 127 | if (timeRecord->presentFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) { |
| 128 | return false; |
| 129 | } |
| 130 | if (timeRecord->presentFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) { |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 131 | timeRecord->frameTime.presentTime = timeRecord->presentFence->getSignalTime(); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 132 | timeRecord->presentFence = nullptr; |
| 133 | } else { |
| 134 | ALOGV("[%s]-[%" PRIu64 "]-presentFence signal time invalid", layerName.c_str(), |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 135 | timeRecord->frameTime.frameNumber); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | static int32_t msBetween(nsecs_t start, nsecs_t end) { |
| 143 | int64_t delta = (end - start) / 1000000; |
| 144 | delta = std::clamp(delta, int64_t(INT32_MIN), int64_t(INT32_MAX)); |
| 145 | return static_cast<int32_t>(delta); |
| 146 | } |
| 147 | |
Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 148 | static std::string getPackageName(const std::string& layerName) { |
| 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 | const std::regex re("(?:SurfaceView[-\\s\\t]+)?([^/]+).*#\\d+"); |
| 154 | std::smatch match; |
| 155 | if (std::regex_match(layerName.begin(), layerName.end(), match, re)) { |
| 156 | // There must be a match for group 1 otherwise the whole string is not |
| 157 | // matched and the above will return false |
| 158 | return match[1]; |
| 159 | } |
| 160 | return ""; |
| 161 | } |
| 162 | |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 163 | void TimeStats::flushAvailableRecordsToStatsLocked(const std::string& layerName) { |
| 164 | ATRACE_CALL(); |
| 165 | |
| 166 | LayerRecord& layerRecord = timeStatsTracker[layerName]; |
| 167 | TimeRecord& prevTimeRecord = layerRecord.prevTimeRecord; |
Yiwei Zhang | c5f2c45 | 2018-05-08 16:31:56 -0700 | [diff] [blame] | 168 | std::deque<TimeRecord>& timeRecords = layerRecord.timeRecords; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 169 | while (!timeRecords.empty()) { |
| 170 | if (!recordReadyLocked(layerName, &timeRecords[0])) break; |
| 171 | ALOGV("[%s]-[%" PRIu64 "]-presentFenceTime[%" PRId64 "]", layerName.c_str(), |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 172 | timeRecords[0].frameTime.frameNumber, timeRecords[0].frameTime.presentTime); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 173 | |
| 174 | if (prevTimeRecord.ready) { |
| 175 | if (!timeStats.stats.count(layerName)) { |
| 176 | timeStats.stats[layerName].layerName = layerName; |
Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 177 | timeStats.stats[layerName].packageName = getPackageName(layerName); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 178 | } |
| 179 | TimeStatsHelper::TimeStatsLayer& timeStatsLayer = timeStats.stats[layerName]; |
| 180 | timeStatsLayer.totalFrames++; |
Yiwei Zhang | eaeea06 | 2018-06-28 14:46:51 -0700 | [diff] [blame] | 181 | timeStatsLayer.droppedFrames += layerRecord.droppedFrames; |
| 182 | layerRecord.droppedFrames = 0; |
| 183 | |
| 184 | const int32_t postToAcquireMs = msBetween(timeRecords[0].frameTime.postTime, |
| 185 | timeRecords[0].frameTime.acquireTime); |
| 186 | ALOGV("[%s]-[%" PRIu64 "]-post2acquire[%d]", layerName.c_str(), |
| 187 | timeRecords[0].frameTime.frameNumber, postToAcquireMs); |
| 188 | timeStatsLayer.deltas["post2acquire"].insert(postToAcquireMs); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 189 | |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 190 | const int32_t postToPresentMs = msBetween(timeRecords[0].frameTime.postTime, |
| 191 | timeRecords[0].frameTime.presentTime); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 192 | ALOGV("[%s]-[%" PRIu64 "]-post2present[%d]", layerName.c_str(), |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 193 | timeRecords[0].frameTime.frameNumber, postToPresentMs); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 194 | timeStatsLayer.deltas["post2present"].insert(postToPresentMs); |
| 195 | |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 196 | const int32_t acquireToPresentMs = msBetween(timeRecords[0].frameTime.acquireTime, |
| 197 | timeRecords[0].frameTime.presentTime); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 198 | ALOGV("[%s]-[%" PRIu64 "]-acquire2present[%d]", layerName.c_str(), |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 199 | timeRecords[0].frameTime.frameNumber, acquireToPresentMs); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 200 | timeStatsLayer.deltas["acquire2present"].insert(acquireToPresentMs); |
| 201 | |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 202 | const int32_t latchToPresentMs = msBetween(timeRecords[0].frameTime.latchTime, |
| 203 | timeRecords[0].frameTime.presentTime); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 204 | ALOGV("[%s]-[%" PRIu64 "]-latch2present[%d]", layerName.c_str(), |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 205 | timeRecords[0].frameTime.frameNumber, latchToPresentMs); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 206 | timeStatsLayer.deltas["latch2present"].insert(latchToPresentMs); |
| 207 | |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 208 | const int32_t desiredToPresentMs = msBetween(timeRecords[0].frameTime.desiredTime, |
| 209 | timeRecords[0].frameTime.presentTime); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 210 | ALOGV("[%s]-[%" PRIu64 "]-desired2present[%d]", layerName.c_str(), |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 211 | timeRecords[0].frameTime.frameNumber, desiredToPresentMs); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 212 | timeStatsLayer.deltas["desired2present"].insert(desiredToPresentMs); |
| 213 | |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 214 | const int32_t presentToPresentMs = msBetween(prevTimeRecord.frameTime.presentTime, |
| 215 | timeRecords[0].frameTime.presentTime); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 216 | ALOGV("[%s]-[%" PRIu64 "]-present2present[%d]", layerName.c_str(), |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 217 | timeRecords[0].frameTime.frameNumber, presentToPresentMs); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 218 | timeStatsLayer.deltas["present2present"].insert(presentToPresentMs); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 219 | } |
| 220 | prevTimeRecord = timeRecords[0]; |
Yiwei Zhang | c5f2c45 | 2018-05-08 16:31:56 -0700 | [diff] [blame] | 221 | timeRecords.pop_front(); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 222 | layerRecord.waitData--; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | static bool layerNameIsValid(const std::string& layerName) { |
| 227 | // This regular expression captures the following layer names for instance: |
| 228 | // 1) StatusBat#0 |
| 229 | // 2) NavigationBar#1 |
Yiwei Zhang | f5d89a0 | 2018-05-14 13:16:31 -0700 | [diff] [blame] | 230 | // 3) co(m).*#0 |
| 231 | // 4) SurfaceView - co(m).*#0 |
| 232 | // Using [-\\s\t]+ for the conjunction part between SurfaceView and co(m).* |
| 233 | // is a bit more robust in case there's a slight change. |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 234 | // The layer name would only consist of . / $ _ 0-9 a-z A-Z in most cases. |
Yiwei Zhang | f5d89a0 | 2018-05-14 13:16:31 -0700 | [diff] [blame] | 235 | std::regex re("(((SurfaceView[-\\s\\t]+)?com?\\.[./$\\w]+)|((Status|Navigation)Bar))#\\d+"); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 236 | return std::regex_match(layerName.begin(), layerName.end(), re); |
| 237 | } |
| 238 | |
| 239 | void TimeStats::setPostTime(const std::string& layerName, uint64_t frameNumber, nsecs_t postTime) { |
| 240 | if (!mEnabled.load()) return; |
| 241 | |
| 242 | ATRACE_CALL(); |
| 243 | ALOGV("[%s]-[%" PRIu64 "]-PostTime[%" PRId64 "]", layerName.c_str(), frameNumber, postTime); |
| 244 | |
| 245 | std::lock_guard<std::mutex> lock(mMutex); |
| 246 | if (!timeStatsTracker.count(layerName) && !layerNameIsValid(layerName)) { |
| 247 | return; |
| 248 | } |
| 249 | LayerRecord& layerRecord = timeStatsTracker[layerName]; |
| 250 | if (layerRecord.timeRecords.size() == MAX_NUM_TIME_RECORDS) { |
| 251 | ALOGV("[%s]-timeRecords is already at its maximum size[%zu]", layerName.c_str(), |
| 252 | MAX_NUM_TIME_RECORDS); |
| 253 | // TODO(zzyiwei): if this happens, there must be a present fence missing |
| 254 | // or waitData is not in the correct position. Need to think out a |
| 255 | // reasonable way to recover from this state. |
| 256 | return; |
| 257 | } |
| 258 | // For most media content, the acquireFence is invalid because the buffer is |
| 259 | // ready at the queueBuffer stage. In this case, acquireTime should be given |
| 260 | // a default value as postTime. |
| 261 | TimeRecord timeRecord = { |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 262 | .frameTime = |
| 263 | { |
| 264 | .frameNumber = frameNumber, |
| 265 | .postTime = postTime, |
| 266 | .acquireTime = postTime, |
| 267 | }, |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 268 | }; |
| 269 | layerRecord.timeRecords.push_back(timeRecord); |
| 270 | if (layerRecord.waitData < 0 || |
| 271 | layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size())) |
| 272 | layerRecord.waitData = layerRecord.timeRecords.size() - 1; |
| 273 | } |
| 274 | |
| 275 | void TimeStats::setLatchTime(const std::string& layerName, uint64_t frameNumber, |
| 276 | nsecs_t latchTime) { |
| 277 | if (!mEnabled.load()) return; |
| 278 | |
| 279 | ATRACE_CALL(); |
| 280 | ALOGV("[%s]-[%" PRIu64 "]-LatchTime[%" PRId64 "]", layerName.c_str(), frameNumber, latchTime); |
| 281 | |
| 282 | std::lock_guard<std::mutex> lock(mMutex); |
| 283 | if (!timeStatsTracker.count(layerName)) return; |
| 284 | LayerRecord& layerRecord = timeStatsTracker[layerName]; |
| 285 | TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData]; |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 286 | if (timeRecord.frameTime.frameNumber == frameNumber) { |
| 287 | timeRecord.frameTime.latchTime = latchTime; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
| 291 | void TimeStats::setDesiredTime(const std::string& layerName, uint64_t frameNumber, |
| 292 | nsecs_t desiredTime) { |
| 293 | if (!mEnabled.load()) return; |
| 294 | |
| 295 | ATRACE_CALL(); |
| 296 | ALOGV("[%s]-[%" PRIu64 "]-DesiredTime[%" PRId64 "]", layerName.c_str(), frameNumber, |
| 297 | desiredTime); |
| 298 | |
| 299 | std::lock_guard<std::mutex> lock(mMutex); |
| 300 | if (!timeStatsTracker.count(layerName)) return; |
| 301 | LayerRecord& layerRecord = timeStatsTracker[layerName]; |
| 302 | TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData]; |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 303 | if (timeRecord.frameTime.frameNumber == frameNumber) { |
| 304 | timeRecord.frameTime.desiredTime = desiredTime; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
| 308 | void TimeStats::setAcquireTime(const std::string& layerName, uint64_t frameNumber, |
| 309 | nsecs_t acquireTime) { |
| 310 | if (!mEnabled.load()) return; |
| 311 | |
| 312 | ATRACE_CALL(); |
| 313 | ALOGV("[%s]-[%" PRIu64 "]-AcquireTime[%" PRId64 "]", layerName.c_str(), frameNumber, |
| 314 | acquireTime); |
| 315 | |
| 316 | std::lock_guard<std::mutex> lock(mMutex); |
| 317 | if (!timeStatsTracker.count(layerName)) return; |
| 318 | LayerRecord& layerRecord = timeStatsTracker[layerName]; |
| 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.acquireTime = acquireTime; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
| 325 | void TimeStats::setAcquireFence(const std::string& layerName, uint64_t frameNumber, |
| 326 | const std::shared_ptr<FenceTime>& acquireFence) { |
| 327 | if (!mEnabled.load()) return; |
| 328 | |
| 329 | ATRACE_CALL(); |
| 330 | ALOGV("[%s]-[%" PRIu64 "]-AcquireFenceTime[%" PRId64 "]", layerName.c_str(), frameNumber, |
| 331 | acquireFence->getSignalTime()); |
| 332 | |
| 333 | std::lock_guard<std::mutex> lock(mMutex); |
| 334 | if (!timeStatsTracker.count(layerName)) return; |
| 335 | LayerRecord& layerRecord = timeStatsTracker[layerName]; |
| 336 | TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData]; |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 337 | if (timeRecord.frameTime.frameNumber == frameNumber) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 338 | timeRecord.acquireFence = acquireFence; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | void TimeStats::setPresentTime(const std::string& layerName, uint64_t frameNumber, |
| 343 | nsecs_t presentTime) { |
| 344 | if (!mEnabled.load()) return; |
| 345 | |
| 346 | ATRACE_CALL(); |
| 347 | ALOGV("[%s]-[%" PRIu64 "]-PresentTime[%" PRId64 "]", layerName.c_str(), frameNumber, |
| 348 | presentTime); |
| 349 | |
| 350 | std::lock_guard<std::mutex> lock(mMutex); |
| 351 | if (!timeStatsTracker.count(layerName)) return; |
| 352 | LayerRecord& layerRecord = timeStatsTracker[layerName]; |
| 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) { |
| 355 | timeRecord.frameTime.presentTime = presentTime; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 356 | timeRecord.ready = true; |
| 357 | layerRecord.waitData++; |
| 358 | } |
| 359 | |
| 360 | flushAvailableRecordsToStatsLocked(layerName); |
| 361 | } |
| 362 | |
| 363 | void TimeStats::setPresentFence(const std::string& layerName, uint64_t frameNumber, |
| 364 | const std::shared_ptr<FenceTime>& presentFence) { |
| 365 | if (!mEnabled.load()) return; |
| 366 | |
| 367 | ATRACE_CALL(); |
| 368 | ALOGV("[%s]-[%" PRIu64 "]-PresentFenceTime[%" PRId64 "]", layerName.c_str(), frameNumber, |
| 369 | presentFence->getSignalTime()); |
| 370 | |
| 371 | std::lock_guard<std::mutex> lock(mMutex); |
| 372 | if (!timeStatsTracker.count(layerName)) return; |
| 373 | LayerRecord& layerRecord = timeStatsTracker[layerName]; |
| 374 | TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData]; |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 375 | if (timeRecord.frameTime.frameNumber == frameNumber) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 376 | timeRecord.presentFence = presentFence; |
| 377 | timeRecord.ready = true; |
| 378 | layerRecord.waitData++; |
| 379 | } |
| 380 | |
| 381 | flushAvailableRecordsToStatsLocked(layerName); |
| 382 | } |
| 383 | |
| 384 | void TimeStats::onDisconnect(const std::string& layerName) { |
| 385 | if (!mEnabled.load()) return; |
| 386 | |
| 387 | ATRACE_CALL(); |
| 388 | ALOGV("[%s]-onDisconnect", layerName.c_str()); |
| 389 | |
| 390 | std::lock_guard<std::mutex> lock(mMutex); |
| 391 | if (!timeStatsTracker.count(layerName)) return; |
| 392 | flushAvailableRecordsToStatsLocked(layerName); |
| 393 | timeStatsTracker.erase(layerName); |
| 394 | } |
| 395 | |
| 396 | void TimeStats::clearLayerRecord(const std::string& layerName) { |
| 397 | if (!mEnabled.load()) return; |
| 398 | |
| 399 | ATRACE_CALL(); |
| 400 | ALOGV("[%s]-clearLayerRecord", layerName.c_str()); |
| 401 | |
| 402 | std::lock_guard<std::mutex> lock(mMutex); |
| 403 | if (!timeStatsTracker.count(layerName)) return; |
| 404 | LayerRecord& layerRecord = timeStatsTracker[layerName]; |
| 405 | layerRecord.timeRecords.clear(); |
| 406 | layerRecord.prevTimeRecord.ready = false; |
| 407 | layerRecord.waitData = -1; |
Yiwei Zhang | eaeea06 | 2018-06-28 14:46:51 -0700 | [diff] [blame] | 408 | layerRecord.droppedFrames = 0; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | void TimeStats::removeTimeRecord(const std::string& layerName, uint64_t frameNumber) { |
| 412 | if (!mEnabled.load()) return; |
| 413 | |
| 414 | ATRACE_CALL(); |
| 415 | ALOGV("[%s]-[%" PRIu64 "]-removeTimeRecord", layerName.c_str(), frameNumber); |
| 416 | |
| 417 | std::lock_guard<std::mutex> lock(mMutex); |
| 418 | if (!timeStatsTracker.count(layerName)) return; |
| 419 | LayerRecord& layerRecord = timeStatsTracker[layerName]; |
| 420 | size_t removeAt = 0; |
| 421 | for (const TimeRecord& record : layerRecord.timeRecords) { |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 422 | if (record.frameTime.frameNumber == frameNumber) break; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 423 | removeAt++; |
| 424 | } |
| 425 | if (removeAt == layerRecord.timeRecords.size()) return; |
| 426 | layerRecord.timeRecords.erase(layerRecord.timeRecords.begin() + removeAt); |
| 427 | if (layerRecord.waitData > static_cast<int32_t>(removeAt)) { |
Yiwei Zhang | eaeea06 | 2018-06-28 14:46:51 -0700 | [diff] [blame] | 428 | layerRecord.waitData--; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 429 | } |
Yiwei Zhang | eaeea06 | 2018-06-28 14:46:51 -0700 | [diff] [blame] | 430 | layerRecord.droppedFrames++; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | void TimeStats::enable() { |
| 434 | if (mEnabled.load()) return; |
| 435 | |
| 436 | ATRACE_CALL(); |
| 437 | |
| 438 | std::lock_guard<std::mutex> lock(mMutex); |
| 439 | ALOGD("Enabled"); |
| 440 | mEnabled.store(true); |
| 441 | timeStats.statsStart = static_cast<int64_t>(std::time(0)); |
| 442 | } |
| 443 | |
| 444 | void TimeStats::disable() { |
| 445 | if (!mEnabled.load()) return; |
| 446 | |
| 447 | ATRACE_CALL(); |
| 448 | |
| 449 | std::lock_guard<std::mutex> lock(mMutex); |
| 450 | ALOGD("Disabled"); |
| 451 | mEnabled.store(false); |
| 452 | timeStats.statsEnd = static_cast<int64_t>(std::time(0)); |
| 453 | } |
| 454 | |
| 455 | void TimeStats::clear() { |
| 456 | ATRACE_CALL(); |
| 457 | |
| 458 | std::lock_guard<std::mutex> lock(mMutex); |
| 459 | ALOGD("Cleared"); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 460 | timeStats.stats.clear(); |
| 461 | timeStats.statsStart = (mEnabled.load() ? static_cast<int64_t>(std::time(0)) : 0); |
| 462 | timeStats.statsEnd = 0; |
| 463 | timeStats.totalFrames = 0; |
| 464 | timeStats.missedFrames = 0; |
| 465 | timeStats.clientCompositionFrames = 0; |
| 466 | } |
| 467 | |
| 468 | bool TimeStats::isEnabled() { |
| 469 | return mEnabled.load(); |
| 470 | } |
| 471 | |
Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 472 | void TimeStats::dump(bool asProto, std::optional<uint32_t> maxLayers, String8& result) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 473 | ATRACE_CALL(); |
| 474 | |
| 475 | std::lock_guard<std::mutex> lock(mMutex); |
| 476 | if (timeStats.statsStart == 0) { |
| 477 | return; |
| 478 | } |
| 479 | |
| 480 | timeStats.statsEnd = static_cast<int64_t>(std::time(0)); |
| 481 | |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 482 | if (asProto) { |
Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 483 | ALOGD("Dumping TimeStats as proto"); |
| 484 | SFTimeStatsGlobalProto timeStatsProto = timeStats.toProto(maxLayers); |
| 485 | result.append(timeStatsProto.SerializeAsString().c_str(), timeStatsProto.ByteSize()); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 486 | } else { |
Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 487 | ALOGD("Dumping TimeStats as text"); |
| 488 | result.append(timeStats.toString(maxLayers).c_str()); |
| 489 | result.append("\n"); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 490 | } |
| 491 | } |
| 492 | |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 493 | } // namespace android |