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