blob: 8d3776bd4e6fa8299dd01e46e9b65d2644c8c08a [file] [log] [blame]
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001/*
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 Zhang3a226d22018-10-16 09:23:03 -070027#include <utils/Timers.h>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070028#include <utils/Trace.h>
29
30#include <algorithm>
31#include <regex>
32
33namespace android {
34
Dominik Laskowskic2867142019-01-21 11:33:38 -080035void TimeStats::parseArgs(bool asProto, const Vector<String16>& args, std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070036 ATRACE_CALL();
37
Yiwei Zhang0102ad22018-05-02 17:37:17 -070038 std::unordered_map<std::string, int32_t> argsMap;
Dominik Laskowskic2867142019-01-21 11:33:38 -080039 for (size_t index = 0; index < args.size(); ++index) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070040 argsMap[std::string(String8(args[index]).c_str())] = index;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070041 }
42
43 if (argsMap.count("-disable")) {
44 disable();
45 }
46
47 if (argsMap.count("-dump")) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070048 std::optional<uint32_t> maxLayers = std::nullopt;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070049 auto iter = argsMap.find("-maxlayers");
50 if (iter != argsMap.end() && iter->second + 1 < static_cast<int32_t>(args.size())) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070051 int64_t value = strtol(String8(args[iter->second + 1]).c_str(), nullptr, 10);
52 value = std::clamp(value, int64_t(0), int64_t(UINT32_MAX));
53 maxLayers = static_cast<uint32_t>(value);
Yiwei Zhang0102ad22018-05-02 17:37:17 -070054 }
55
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070056 dump(asProto, maxLayers, result);
Yiwei Zhang0102ad22018-05-02 17:37:17 -070057 }
58
59 if (argsMap.count("-clear")) {
60 clear();
61 }
62
63 if (argsMap.count("-enable")) {
64 enable();
65 }
66}
67
68void TimeStats::incrementTotalFrames() {
69 if (!mEnabled.load()) return;
70
71 ATRACE_CALL();
72
73 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -070074 mTimeStats.totalFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070075}
76
Yiwei Zhang621f9d42018-05-07 10:40:55 -070077void TimeStats::incrementMissedFrames() {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070078 if (!mEnabled.load()) return;
79
80 ATRACE_CALL();
81
82 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -070083 mTimeStats.missedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070084}
85
86void TimeStats::incrementClientCompositionFrames() {
87 if (!mEnabled.load()) return;
88
89 ATRACE_CALL();
90
91 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -070092 mTimeStats.clientCompositionFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070093}
94
Yiwei Zhang9689e2f2018-05-11 12:33:23 -070095bool TimeStats::recordReadyLocked(int32_t layerID, TimeRecord* timeRecord) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070096 if (!timeRecord->ready) {
Yiwei Zhang9689e2f2018-05-11 12:33:23 -070097 ALOGV("[%d]-[%" PRIu64 "]-presentFence is still not received", layerID,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -070098 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -070099 return false;
100 }
101
102 if (timeRecord->acquireFence != nullptr) {
103 if (timeRecord->acquireFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
104 return false;
105 }
106 if (timeRecord->acquireFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700107 timeRecord->frameTime.acquireTime = timeRecord->acquireFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700108 timeRecord->acquireFence = nullptr;
109 } else {
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700110 ALOGV("[%d]-[%" PRIu64 "]-acquireFence signal time is invalid", layerID,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700111 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700112 }
113 }
114
115 if (timeRecord->presentFence != nullptr) {
116 if (timeRecord->presentFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
117 return false;
118 }
119 if (timeRecord->presentFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700120 timeRecord->frameTime.presentTime = timeRecord->presentFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700121 timeRecord->presentFence = nullptr;
122 } else {
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700123 ALOGV("[%d]-[%" PRIu64 "]-presentFence signal time invalid", layerID,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700124 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700125 }
126 }
127
128 return true;
129}
130
131static int32_t msBetween(nsecs_t start, nsecs_t end) {
132 int64_t delta = (end - start) / 1000000;
133 delta = std::clamp(delta, int64_t(INT32_MIN), int64_t(INT32_MAX));
134 return static_cast<int32_t>(delta);
135}
136
Yiwei Zhangbd408322018-10-15 18:31:53 -0700137// This regular expression captures the following for instance:
138// StatusBar in StatusBar#0
139// com.appname in com.appname/com.appname.activity#0
140// com.appname in SurfaceView - com.appname/com.appname.activity#0
141static const std::regex packageNameRegex("(?:SurfaceView[-\\s\\t]+)?([^/]+).*#\\d+");
142
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700143static std::string getPackageName(const std::string& layerName) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700144 std::smatch match;
Yiwei Zhangbd408322018-10-15 18:31:53 -0700145 if (std::regex_match(layerName.begin(), layerName.end(), match, packageNameRegex)) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700146 // There must be a match for group 1 otherwise the whole string is not
147 // matched and the above will return false
148 return match[1];
149 }
150 return "";
151}
152
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700153void TimeStats::flushAvailableRecordsToStatsLocked(int32_t layerID) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700154 ATRACE_CALL();
155
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700156 LayerRecord& layerRecord = mTimeStatsTracker[layerID];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700157 TimeRecord& prevTimeRecord = layerRecord.prevTimeRecord;
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700158 std::deque<TimeRecord>& timeRecords = layerRecord.timeRecords;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700159 while (!timeRecords.empty()) {
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700160 if (!recordReadyLocked(layerID, &timeRecords[0])) break;
161 ALOGV("[%d]-[%" PRIu64 "]-presentFenceTime[%" PRId64 "]", layerID,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700162 timeRecords[0].frameTime.frameNumber, timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700163
Raymond Chiu361ea972018-10-26 14:14:37 -0700164 const std::string& layerName = layerRecord.layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700165 if (prevTimeRecord.ready) {
Yiwei Zhangdc224042018-10-18 15:34:00 -0700166 if (!mTimeStats.stats.count(layerName)) {
167 mTimeStats.stats[layerName].layerName = layerName;
168 mTimeStats.stats[layerName].packageName = getPackageName(layerName);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700169 }
Yiwei Zhangdc224042018-10-18 15:34:00 -0700170 TimeStatsHelper::TimeStatsLayer& timeStatsLayer = mTimeStats.stats[layerName];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700171 timeStatsLayer.totalFrames++;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700172 timeStatsLayer.droppedFrames += layerRecord.droppedFrames;
173 layerRecord.droppedFrames = 0;
174
175 const int32_t postToAcquireMs = msBetween(timeRecords[0].frameTime.postTime,
176 timeRecords[0].frameTime.acquireTime);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700177 ALOGV("[%d]-[%" PRIu64 "]-post2acquire[%d]", layerID,
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700178 timeRecords[0].frameTime.frameNumber, postToAcquireMs);
179 timeStatsLayer.deltas["post2acquire"].insert(postToAcquireMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700180
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700181 const int32_t postToPresentMs = msBetween(timeRecords[0].frameTime.postTime,
182 timeRecords[0].frameTime.presentTime);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700183 ALOGV("[%d]-[%" PRIu64 "]-post2present[%d]", layerID,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700184 timeRecords[0].frameTime.frameNumber, postToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700185 timeStatsLayer.deltas["post2present"].insert(postToPresentMs);
186
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700187 const int32_t acquireToPresentMs = msBetween(timeRecords[0].frameTime.acquireTime,
188 timeRecords[0].frameTime.presentTime);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700189 ALOGV("[%d]-[%" PRIu64 "]-acquire2present[%d]", layerID,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700190 timeRecords[0].frameTime.frameNumber, acquireToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700191 timeStatsLayer.deltas["acquire2present"].insert(acquireToPresentMs);
192
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700193 const int32_t latchToPresentMs = msBetween(timeRecords[0].frameTime.latchTime,
194 timeRecords[0].frameTime.presentTime);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700195 ALOGV("[%d]-[%" PRIu64 "]-latch2present[%d]", layerID,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700196 timeRecords[0].frameTime.frameNumber, latchToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700197 timeStatsLayer.deltas["latch2present"].insert(latchToPresentMs);
198
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700199 const int32_t desiredToPresentMs = msBetween(timeRecords[0].frameTime.desiredTime,
200 timeRecords[0].frameTime.presentTime);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700201 ALOGV("[%d]-[%" PRIu64 "]-desired2present[%d]", layerID,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700202 timeRecords[0].frameTime.frameNumber, desiredToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700203 timeStatsLayer.deltas["desired2present"].insert(desiredToPresentMs);
204
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700205 const int32_t presentToPresentMs = msBetween(prevTimeRecord.frameTime.presentTime,
206 timeRecords[0].frameTime.presentTime);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700207 ALOGV("[%d]-[%" PRIu64 "]-present2present[%d]", layerID,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700208 timeRecords[0].frameTime.frameNumber, presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700209 timeStatsLayer.deltas["present2present"].insert(presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700210 }
Raymond Chiu361ea972018-10-26 14:14:37 -0700211
212 // Output additional trace points to track frame time.
213 ATRACE_INT64(("TimeStats-Post - " + layerName).c_str(), timeRecords[0].frameTime.postTime);
214 ATRACE_INT64(("TimeStats-Acquire - " + layerName).c_str(),
215 timeRecords[0].frameTime.acquireTime);
216 ATRACE_INT64(("TimeStats-Latch - " + layerName).c_str(),
217 timeRecords[0].frameTime.latchTime);
218 ATRACE_INT64(("TimeStats-Desired - " + layerName).c_str(),
219 timeRecords[0].frameTime.desiredTime);
220 ATRACE_INT64(("TimeStats-Present - " + layerName).c_str(),
221 timeRecords[0].frameTime.presentTime);
222
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700223 prevTimeRecord = timeRecords[0];
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700224 timeRecords.pop_front();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700225 layerRecord.waitData--;
226 }
227}
228
Yiwei Zhangbd408322018-10-15 18:31:53 -0700229// This regular expression captures the following layer names for instance:
230// 1) StatusBat#0
231// 2) NavigationBar#1
232// 3) co(m).*#0
233// 4) SurfaceView - co(m).*#0
234// Using [-\\s\t]+ for the conjunction part between SurfaceView and co(m).*
235// is a bit more robust in case there's a slight change.
236// The layer name would only consist of . / $ _ 0-9 a-z A-Z in most cases.
237static const std::regex layerNameRegex(
238 "(((SurfaceView[-\\s\\t]+)?com?\\.[./$\\w]+)|((Status|Navigation)Bar))#\\d+");
239
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700240static bool layerNameIsValid(const std::string& layerName) {
Yiwei Zhangbd408322018-10-15 18:31:53 -0700241 return std::regex_match(layerName.begin(), layerName.end(), layerNameRegex);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700242}
243
Yiwei Zhang8e8fe522018-11-02 18:34:07 -0700244void TimeStats::setPostTime(int32_t layerID, uint64_t frameNumber, const std::string& layerName,
245 nsecs_t postTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700246 if (!mEnabled.load()) return;
247
248 ATRACE_CALL();
Yiwei Zhang8e8fe522018-11-02 18:34:07 -0700249 ALOGV("[%d]-[%" PRIu64 "]-[%s]-PostTime[%" PRId64 "]", layerID, frameNumber, layerName.c_str(),
250 postTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700251
252 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700253 if (!mTimeStatsTracker.count(layerID) && layerNameIsValid(layerName)) {
254 mTimeStatsTracker[layerID].layerName = layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700255 }
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700256 if (!mTimeStatsTracker.count(layerID)) return;
257 LayerRecord& layerRecord = mTimeStatsTracker[layerID];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700258 if (layerRecord.timeRecords.size() == MAX_NUM_TIME_RECORDS) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800259 ALOGE("[%d]-[%s]-timeRecords is at its maximum size[%zu]. Ignore this when unittesting.",
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700260 layerID, layerRecord.layerName.c_str(), MAX_NUM_TIME_RECORDS);
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800261 mTimeStatsTracker.erase(layerID);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700262 return;
263 }
264 // For most media content, the acquireFence is invalid because the buffer is
265 // ready at the queueBuffer stage. In this case, acquireTime should be given
266 // a default value as postTime.
267 TimeRecord timeRecord = {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700268 .frameTime =
269 {
270 .frameNumber = frameNumber,
271 .postTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800272 .latchTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700273 .acquireTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800274 .desiredTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700275 },
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700276 };
277 layerRecord.timeRecords.push_back(timeRecord);
278 if (layerRecord.waitData < 0 ||
279 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
280 layerRecord.waitData = layerRecord.timeRecords.size() - 1;
281}
282
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700283void TimeStats::setLatchTime(int32_t layerID, uint64_t frameNumber, nsecs_t latchTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700284 if (!mEnabled.load()) return;
285
286 ATRACE_CALL();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700287 ALOGV("[%d]-[%" PRIu64 "]-LatchTime[%" PRId64 "]", layerID, frameNumber, latchTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700288
289 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700290 if (!mTimeStatsTracker.count(layerID)) return;
291 LayerRecord& layerRecord = mTimeStatsTracker[layerID];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700292 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700293 if (timeRecord.frameTime.frameNumber == frameNumber) {
294 timeRecord.frameTime.latchTime = latchTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700295 }
296}
297
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700298void TimeStats::setDesiredTime(int32_t layerID, uint64_t frameNumber, nsecs_t desiredTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700299 if (!mEnabled.load()) return;
300
301 ATRACE_CALL();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700302 ALOGV("[%d]-[%" PRIu64 "]-DesiredTime[%" PRId64 "]", layerID, frameNumber, desiredTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700303
304 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700305 if (!mTimeStatsTracker.count(layerID)) return;
306 LayerRecord& layerRecord = mTimeStatsTracker[layerID];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700307 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700308 if (timeRecord.frameTime.frameNumber == frameNumber) {
309 timeRecord.frameTime.desiredTime = desiredTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700310 }
311}
312
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700313void TimeStats::setAcquireTime(int32_t layerID, uint64_t frameNumber, nsecs_t acquireTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700314 if (!mEnabled.load()) return;
315
316 ATRACE_CALL();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700317 ALOGV("[%d]-[%" PRIu64 "]-AcquireTime[%" PRId64 "]", layerID, frameNumber, acquireTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700318
319 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700320 if (!mTimeStatsTracker.count(layerID)) return;
321 LayerRecord& layerRecord = mTimeStatsTracker[layerID];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700322 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700323 if (timeRecord.frameTime.frameNumber == frameNumber) {
324 timeRecord.frameTime.acquireTime = acquireTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700325 }
326}
327
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700328void TimeStats::setAcquireFence(int32_t layerID, uint64_t frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700329 const std::shared_ptr<FenceTime>& acquireFence) {
330 if (!mEnabled.load()) return;
331
332 ATRACE_CALL();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700333 ALOGV("[%d]-[%" PRIu64 "]-AcquireFenceTime[%" PRId64 "]", layerID, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700334 acquireFence->getSignalTime());
335
336 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700337 if (!mTimeStatsTracker.count(layerID)) return;
338 LayerRecord& layerRecord = mTimeStatsTracker[layerID];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700339 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700340 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700341 timeRecord.acquireFence = acquireFence;
342 }
343}
344
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700345void TimeStats::setPresentTime(int32_t layerID, uint64_t frameNumber, nsecs_t presentTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700346 if (!mEnabled.load()) return;
347
348 ATRACE_CALL();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700349 ALOGV("[%d]-[%" PRIu64 "]-PresentTime[%" PRId64 "]", layerID, frameNumber, presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700350
351 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700352 if (!mTimeStatsTracker.count(layerID)) return;
353 LayerRecord& layerRecord = mTimeStatsTracker[layerID];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700354 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700355 if (timeRecord.frameTime.frameNumber == frameNumber) {
356 timeRecord.frameTime.presentTime = presentTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700357 timeRecord.ready = true;
358 layerRecord.waitData++;
359 }
360
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700361 flushAvailableRecordsToStatsLocked(layerID);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700362}
363
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700364void TimeStats::setPresentFence(int32_t layerID, uint64_t frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700365 const std::shared_ptr<FenceTime>& presentFence) {
366 if (!mEnabled.load()) return;
367
368 ATRACE_CALL();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700369 ALOGV("[%d]-[%" PRIu64 "]-PresentFenceTime[%" PRId64 "]", layerID, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700370 presentFence->getSignalTime());
371
372 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700373 if (!mTimeStatsTracker.count(layerID)) return;
374 LayerRecord& layerRecord = mTimeStatsTracker[layerID];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700375 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700376 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700377 timeRecord.presentFence = presentFence;
378 timeRecord.ready = true;
379 layerRecord.waitData++;
380 }
381
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700382 flushAvailableRecordsToStatsLocked(layerID);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700383}
384
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700385void TimeStats::onDestroy(int32_t layerID) {
Yiwei Zhangdc224042018-10-18 15:34:00 -0700386 if (!mEnabled.load()) return;
387
388 ATRACE_CALL();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700389 ALOGV("[%d]-onDestroy", layerID);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700390
391 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700392 if (!mTimeStatsTracker.count(layerID)) return;
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700393 mTimeStatsTracker.erase(layerID);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700394}
395
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700396void TimeStats::removeTimeRecord(int32_t layerID, uint64_t frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700397 if (!mEnabled.load()) return;
398
399 ATRACE_CALL();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700400 ALOGV("[%d]-[%" PRIu64 "]-removeTimeRecord", layerID, frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700401
402 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700403 if (!mTimeStatsTracker.count(layerID)) return;
404 LayerRecord& layerRecord = mTimeStatsTracker[layerID];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700405 size_t removeAt = 0;
406 for (const TimeRecord& record : layerRecord.timeRecords) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700407 if (record.frameTime.frameNumber == frameNumber) break;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700408 removeAt++;
409 }
410 if (removeAt == layerRecord.timeRecords.size()) return;
411 layerRecord.timeRecords.erase(layerRecord.timeRecords.begin() + removeAt);
412 if (layerRecord.waitData > static_cast<int32_t>(removeAt)) {
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700413 layerRecord.waitData--;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700414 }
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700415 layerRecord.droppedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700416}
417
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700418void TimeStats::flushPowerTimeLocked() {
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700419 if (!mEnabled.load()) return;
420
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700421 nsecs_t curTime = systemTime();
422 // elapsedTime is in milliseconds.
423 int64_t elapsedTime = (curTime - mPowerTime.prevTime) / 1000000;
424
425 switch (mPowerTime.powerMode) {
426 case HWC_POWER_MODE_NORMAL:
427 mTimeStats.displayOnTime += elapsedTime;
428 break;
429 case HWC_POWER_MODE_OFF:
430 case HWC_POWER_MODE_DOZE:
431 case HWC_POWER_MODE_DOZE_SUSPEND:
432 default:
433 break;
434 }
435
436 mPowerTime.prevTime = curTime;
437}
438
439void TimeStats::setPowerMode(int32_t powerMode) {
440 if (!mEnabled.load()) {
441 std::lock_guard<std::mutex> lock(mMutex);
442 mPowerTime.powerMode = powerMode;
443 return;
444 }
445
446 std::lock_guard<std::mutex> lock(mMutex);
447 if (powerMode == mPowerTime.powerMode) return;
448
449 flushPowerTimeLocked();
450 mPowerTime.powerMode = powerMode;
451}
452
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700453void TimeStats::flushAvailableGlobalRecordsToStatsLocked() {
454 ATRACE_CALL();
455
456 while (!mGlobalRecord.presentFences.empty()) {
457 const nsecs_t curPresentTime = mGlobalRecord.presentFences.front()->getSignalTime();
458 if (curPresentTime == Fence::SIGNAL_TIME_PENDING) break;
459
460 if (curPresentTime == Fence::SIGNAL_TIME_INVALID) {
461 ALOGE("GlobalPresentFence is invalid!");
462 mGlobalRecord.prevPresentTime = 0;
463 mGlobalRecord.presentFences.pop_front();
464 continue;
465 }
466
467 ALOGV("GlobalPresentFenceTime[%" PRId64 "]",
468 mGlobalRecord.presentFences.front()->getSignalTime());
469
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700470 if (mGlobalRecord.prevPresentTime != 0) {
471 const int32_t presentToPresentMs =
472 msBetween(mGlobalRecord.prevPresentTime, curPresentTime);
473 ALOGV("Global present2present[%d] prev[%" PRId64 "] curr[%" PRId64 "]",
474 presentToPresentMs, mGlobalRecord.prevPresentTime, curPresentTime);
475 mTimeStats.presentToPresent.insert(presentToPresentMs);
476 }
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700477
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700478 mGlobalRecord.prevPresentTime = curPresentTime;
479 mGlobalRecord.presentFences.pop_front();
480 }
481}
482
483void TimeStats::setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) {
484 if (!mEnabled.load()) return;
485
486 ATRACE_CALL();
487 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700488 if (presentFence == nullptr || !presentFence->isValid()) {
489 mGlobalRecord.prevPresentTime = 0;
490 return;
491 }
492
493 if (mPowerTime.powerMode != HWC_POWER_MODE_NORMAL) {
494 // Try flushing the last present fence on HWC_POWER_MODE_NORMAL.
495 flushAvailableGlobalRecordsToStatsLocked();
496 mGlobalRecord.presentFences.clear();
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700497 mGlobalRecord.prevPresentTime = 0;
498 return;
499 }
500
501 if (mGlobalRecord.presentFences.size() == MAX_NUM_TIME_RECORDS) {
502 // The front presentFence must be trapped in pending status in this
503 // case. Try dequeuing the front one to recover.
504 ALOGE("GlobalPresentFences is already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
505 mGlobalRecord.prevPresentTime = 0;
506 mGlobalRecord.presentFences.pop_front();
507 }
508
509 mGlobalRecord.presentFences.emplace_back(presentFence);
510 flushAvailableGlobalRecordsToStatsLocked();
511}
512
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700513void TimeStats::enable() {
514 if (mEnabled.load()) return;
515
516 ATRACE_CALL();
517
518 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700519 mEnabled.store(true);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700520 mTimeStats.statsStart = static_cast<int64_t>(std::time(0));
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700521 mPowerTime.prevTime = systemTime();
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700522 ALOGD("Enabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700523}
524
525void TimeStats::disable() {
526 if (!mEnabled.load()) return;
527
528 ATRACE_CALL();
529
530 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700531 flushPowerTimeLocked();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700532 mEnabled.store(false);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700533 mTimeStats.statsEnd = static_cast<int64_t>(std::time(0));
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700534 ALOGD("Disabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700535}
536
537void TimeStats::clear() {
538 ATRACE_CALL();
539
540 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700541 mTimeStatsTracker.clear();
Yiwei Zhangdc224042018-10-18 15:34:00 -0700542 mTimeStats.stats.clear();
543 mTimeStats.statsStart = (mEnabled.load() ? static_cast<int64_t>(std::time(0)) : 0);
544 mTimeStats.statsEnd = 0;
545 mTimeStats.totalFrames = 0;
546 mTimeStats.missedFrames = 0;
547 mTimeStats.clientCompositionFrames = 0;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700548 mTimeStats.displayOnTime = 0;
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700549 mTimeStats.presentToPresent.hist.clear();
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700550 mPowerTime.prevTime = systemTime();
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700551 mGlobalRecord.prevPresentTime = 0;
552 mGlobalRecord.presentFences.clear();
553 ALOGD("Cleared");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700554}
555
556bool TimeStats::isEnabled() {
557 return mEnabled.load();
558}
559
Yiwei Zhang5434a782018-12-05 18:06:32 -0800560void TimeStats::dump(bool asProto, std::optional<uint32_t> maxLayers, std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700561 ATRACE_CALL();
562
563 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700564 if (mTimeStats.statsStart == 0) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700565 return;
566 }
567
Yiwei Zhangdc224042018-10-18 15:34:00 -0700568 mTimeStats.statsEnd = static_cast<int64_t>(std::time(0));
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700569
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700570 flushPowerTimeLocked();
571
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700572 if (asProto) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700573 ALOGD("Dumping TimeStats as proto");
Yiwei Zhangdc224042018-10-18 15:34:00 -0700574 SFTimeStatsGlobalProto timeStatsProto = mTimeStats.toProto(maxLayers);
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700575 result.append(timeStatsProto.SerializeAsString().c_str(), timeStatsProto.ByteSize());
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700576 } else {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700577 ALOGD("Dumping TimeStats as text");
Yiwei Zhang5434a782018-12-05 18:06:32 -0800578 result.append(mTimeStats.toString(maxLayers));
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700579 result.append("\n");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700580 }
581}
582
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700583} // namespace android