blob: 740099e350d38e2cd68f66d73875b9b85ee86f64 [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
Alec Mourifb571ea2019-01-24 18:42:10 -080035namespace impl {
36
Dominik Laskowskic2867142019-01-21 11:33:38 -080037void TimeStats::parseArgs(bool asProto, const Vector<String16>& args, std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070038 ATRACE_CALL();
39
Yiwei Zhang0102ad22018-05-02 17:37:17 -070040 std::unordered_map<std::string, int32_t> argsMap;
Dominik Laskowskic2867142019-01-21 11:33:38 -080041 for (size_t index = 0; index < args.size(); ++index) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070042 argsMap[std::string(String8(args[index]).c_str())] = index;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070043 }
44
45 if (argsMap.count("-disable")) {
46 disable();
47 }
48
49 if (argsMap.count("-dump")) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070050 std::optional<uint32_t> maxLayers = std::nullopt;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070051 auto iter = argsMap.find("-maxlayers");
52 if (iter != argsMap.end() && iter->second + 1 < static_cast<int32_t>(args.size())) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070053 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 Zhang0102ad22018-05-02 17:37:17 -070056 }
57
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070058 dump(asProto, maxLayers, result);
Yiwei Zhang0102ad22018-05-02 17:37:17 -070059 }
60
61 if (argsMap.count("-clear")) {
62 clear();
63 }
64
65 if (argsMap.count("-enable")) {
66 enable();
67 }
68}
69
70void TimeStats::incrementTotalFrames() {
71 if (!mEnabled.load()) return;
72
73 ATRACE_CALL();
74
75 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -070076 mTimeStats.totalFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070077}
78
Yiwei Zhang621f9d42018-05-07 10:40:55 -070079void TimeStats::incrementMissedFrames() {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070080 if (!mEnabled.load()) return;
81
82 ATRACE_CALL();
83
84 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -070085 mTimeStats.missedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070086}
87
88void TimeStats::incrementClientCompositionFrames() {
89 if (!mEnabled.load()) return;
90
91 ATRACE_CALL();
92
93 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -070094 mTimeStats.clientCompositionFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070095}
96
Yiwei Zhang9689e2f2018-05-11 12:33:23 -070097bool TimeStats::recordReadyLocked(int32_t layerID, TimeRecord* timeRecord) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070098 if (!timeRecord->ready) {
Yiwei Zhang9689e2f2018-05-11 12:33:23 -070099 ALOGV("[%d]-[%" PRIu64 "]-presentFence is still not received", layerID,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700100 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700101 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 Zhangcf50ab92018-06-14 10:50:12 -0700109 timeRecord->frameTime.acquireTime = timeRecord->acquireFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700110 timeRecord->acquireFence = nullptr;
111 } else {
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700112 ALOGV("[%d]-[%" PRIu64 "]-acquireFence signal time is invalid", layerID,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700113 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700114 }
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 Zhangcf50ab92018-06-14 10:50:12 -0700122 timeRecord->frameTime.presentTime = timeRecord->presentFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700123 timeRecord->presentFence = nullptr;
124 } else {
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700125 ALOGV("[%d]-[%" PRIu64 "]-presentFence signal time invalid", layerID,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700126 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700127 }
128 }
129
130 return true;
131}
132
133static 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 Zhangbd408322018-10-15 18:31:53 -0700139// 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
143static const std::regex packageNameRegex("(?:SurfaceView[-\\s\\t]+)?([^/]+).*#\\d+");
144
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700145static std::string getPackageName(const std::string& layerName) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700146 std::smatch match;
Yiwei Zhangbd408322018-10-15 18:31:53 -0700147 if (std::regex_match(layerName.begin(), layerName.end(), match, packageNameRegex)) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700148 // 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 Zhang9689e2f2018-05-11 12:33:23 -0700155void TimeStats::flushAvailableRecordsToStatsLocked(int32_t layerID) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700156 ATRACE_CALL();
157
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700158 LayerRecord& layerRecord = mTimeStatsTracker[layerID];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700159 TimeRecord& prevTimeRecord = layerRecord.prevTimeRecord;
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700160 std::deque<TimeRecord>& timeRecords = layerRecord.timeRecords;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700161 while (!timeRecords.empty()) {
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700162 if (!recordReadyLocked(layerID, &timeRecords[0])) break;
163 ALOGV("[%d]-[%" PRIu64 "]-presentFenceTime[%" PRId64 "]", layerID,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700164 timeRecords[0].frameTime.frameNumber, timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700165
Raymond Chiu361ea972018-10-26 14:14:37 -0700166 const std::string& layerName = layerRecord.layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700167 if (prevTimeRecord.ready) {
Yiwei Zhangdc224042018-10-18 15:34:00 -0700168 if (!mTimeStats.stats.count(layerName)) {
169 mTimeStats.stats[layerName].layerName = layerName;
170 mTimeStats.stats[layerName].packageName = getPackageName(layerName);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700171 }
Yiwei Zhangdc224042018-10-18 15:34:00 -0700172 TimeStatsHelper::TimeStatsLayer& timeStatsLayer = mTimeStats.stats[layerName];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700173 timeStatsLayer.totalFrames++;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700174 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 Zhang9689e2f2018-05-11 12:33:23 -0700179 ALOGV("[%d]-[%" PRIu64 "]-post2acquire[%d]", layerID,
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700180 timeRecords[0].frameTime.frameNumber, postToAcquireMs);
181 timeStatsLayer.deltas["post2acquire"].insert(postToAcquireMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700182
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700183 const int32_t postToPresentMs = msBetween(timeRecords[0].frameTime.postTime,
184 timeRecords[0].frameTime.presentTime);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700185 ALOGV("[%d]-[%" PRIu64 "]-post2present[%d]", layerID,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700186 timeRecords[0].frameTime.frameNumber, postToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700187 timeStatsLayer.deltas["post2present"].insert(postToPresentMs);
188
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700189 const int32_t acquireToPresentMs = msBetween(timeRecords[0].frameTime.acquireTime,
190 timeRecords[0].frameTime.presentTime);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700191 ALOGV("[%d]-[%" PRIu64 "]-acquire2present[%d]", layerID,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700192 timeRecords[0].frameTime.frameNumber, acquireToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700193 timeStatsLayer.deltas["acquire2present"].insert(acquireToPresentMs);
194
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700195 const int32_t latchToPresentMs = msBetween(timeRecords[0].frameTime.latchTime,
196 timeRecords[0].frameTime.presentTime);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700197 ALOGV("[%d]-[%" PRIu64 "]-latch2present[%d]", layerID,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700198 timeRecords[0].frameTime.frameNumber, latchToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700199 timeStatsLayer.deltas["latch2present"].insert(latchToPresentMs);
200
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700201 const int32_t desiredToPresentMs = msBetween(timeRecords[0].frameTime.desiredTime,
202 timeRecords[0].frameTime.presentTime);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700203 ALOGV("[%d]-[%" PRIu64 "]-desired2present[%d]", layerID,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700204 timeRecords[0].frameTime.frameNumber, desiredToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700205 timeStatsLayer.deltas["desired2present"].insert(desiredToPresentMs);
206
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700207 const int32_t presentToPresentMs = msBetween(prevTimeRecord.frameTime.presentTime,
208 timeRecords[0].frameTime.presentTime);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700209 ALOGV("[%d]-[%" PRIu64 "]-present2present[%d]", layerID,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700210 timeRecords[0].frameTime.frameNumber, presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700211 timeStatsLayer.deltas["present2present"].insert(presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700212 }
Raymond Chiu361ea972018-10-26 14:14:37 -0700213
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 Zhang0102ad22018-05-02 17:37:17 -0700225 prevTimeRecord = timeRecords[0];
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700226 timeRecords.pop_front();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700227 layerRecord.waitData--;
228 }
229}
230
Yiwei Zhangbd408322018-10-15 18:31:53 -0700231// 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.
239static const std::regex layerNameRegex(
240 "(((SurfaceView[-\\s\\t]+)?com?\\.[./$\\w]+)|((Status|Navigation)Bar))#\\d+");
241
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700242static bool layerNameIsValid(const std::string& layerName) {
Yiwei Zhangbd408322018-10-15 18:31:53 -0700243 return std::regex_match(layerName.begin(), layerName.end(), layerNameRegex);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700244}
245
Yiwei Zhang8e8fe522018-11-02 18:34:07 -0700246void TimeStats::setPostTime(int32_t layerID, uint64_t frameNumber, const std::string& layerName,
247 nsecs_t postTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700248 if (!mEnabled.load()) return;
249
250 ATRACE_CALL();
Yiwei Zhang8e8fe522018-11-02 18:34:07 -0700251 ALOGV("[%d]-[%" PRIu64 "]-[%s]-PostTime[%" PRId64 "]", layerID, frameNumber, layerName.c_str(),
252 postTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700253
254 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700255 if (!mTimeStatsTracker.count(layerID) && layerNameIsValid(layerName)) {
256 mTimeStatsTracker[layerID].layerName = layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700257 }
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700258 if (!mTimeStatsTracker.count(layerID)) return;
259 LayerRecord& layerRecord = mTimeStatsTracker[layerID];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700260 if (layerRecord.timeRecords.size() == MAX_NUM_TIME_RECORDS) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800261 ALOGE("[%d]-[%s]-timeRecords is at its maximum size[%zu]. Ignore this when unittesting.",
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700262 layerID, layerRecord.layerName.c_str(), MAX_NUM_TIME_RECORDS);
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800263 mTimeStatsTracker.erase(layerID);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700264 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 Zhangcf50ab92018-06-14 10:50:12 -0700270 .frameTime =
271 {
272 .frameNumber = frameNumber,
273 .postTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800274 .latchTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700275 .acquireTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800276 .desiredTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700277 },
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700278 };
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 Zhang9689e2f2018-05-11 12:33:23 -0700285void TimeStats::setLatchTime(int32_t layerID, uint64_t frameNumber, nsecs_t latchTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700286 if (!mEnabled.load()) return;
287
288 ATRACE_CALL();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700289 ALOGV("[%d]-[%" PRIu64 "]-LatchTime[%" PRId64 "]", layerID, frameNumber, latchTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700290
291 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700292 if (!mTimeStatsTracker.count(layerID)) return;
293 LayerRecord& layerRecord = mTimeStatsTracker[layerID];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700294 if (layerRecord.waitData < 0 ||
295 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
296 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700297 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700298 if (timeRecord.frameTime.frameNumber == frameNumber) {
299 timeRecord.frameTime.latchTime = latchTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700300 }
301}
302
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700303void TimeStats::setDesiredTime(int32_t layerID, uint64_t frameNumber, nsecs_t desiredTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700304 if (!mEnabled.load()) return;
305
306 ATRACE_CALL();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700307 ALOGV("[%d]-[%" PRIu64 "]-DesiredTime[%" PRId64 "]", layerID, frameNumber, desiredTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700308
309 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700310 if (!mTimeStatsTracker.count(layerID)) return;
311 LayerRecord& layerRecord = mTimeStatsTracker[layerID];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700312 if (layerRecord.waitData < 0 ||
313 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
314 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700315 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700316 if (timeRecord.frameTime.frameNumber == frameNumber) {
317 timeRecord.frameTime.desiredTime = desiredTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700318 }
319}
320
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700321void TimeStats::setAcquireTime(int32_t layerID, uint64_t frameNumber, nsecs_t acquireTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700322 if (!mEnabled.load()) return;
323
324 ATRACE_CALL();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700325 ALOGV("[%d]-[%" PRIu64 "]-AcquireTime[%" PRId64 "]", layerID, frameNumber, acquireTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700326
327 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700328 if (!mTimeStatsTracker.count(layerID)) return;
329 LayerRecord& layerRecord = mTimeStatsTracker[layerID];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700330 if (layerRecord.waitData < 0 ||
331 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
332 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700333 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700334 if (timeRecord.frameTime.frameNumber == frameNumber) {
335 timeRecord.frameTime.acquireTime = acquireTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700336 }
337}
338
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700339void TimeStats::setAcquireFence(int32_t layerID, uint64_t frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700340 const std::shared_ptr<FenceTime>& acquireFence) {
341 if (!mEnabled.load()) return;
342
343 ATRACE_CALL();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700344 ALOGV("[%d]-[%" PRIu64 "]-AcquireFenceTime[%" PRId64 "]", layerID, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700345 acquireFence->getSignalTime());
346
347 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700348 if (!mTimeStatsTracker.count(layerID)) return;
349 LayerRecord& layerRecord = mTimeStatsTracker[layerID];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700350 if (layerRecord.waitData < 0 ||
351 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
352 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700353 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700354 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700355 timeRecord.acquireFence = acquireFence;
356 }
357}
358
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700359void TimeStats::setPresentTime(int32_t layerID, uint64_t frameNumber, nsecs_t presentTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700360 if (!mEnabled.load()) return;
361
362 ATRACE_CALL();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700363 ALOGV("[%d]-[%" PRIu64 "]-PresentTime[%" PRId64 "]", layerID, frameNumber, presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700364
365 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700366 if (!mTimeStatsTracker.count(layerID)) return;
367 LayerRecord& layerRecord = mTimeStatsTracker[layerID];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700368 if (layerRecord.waitData < 0 ||
369 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
370 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700371 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700372 if (timeRecord.frameTime.frameNumber == frameNumber) {
373 timeRecord.frameTime.presentTime = presentTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700374 timeRecord.ready = true;
375 layerRecord.waitData++;
376 }
377
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700378 flushAvailableRecordsToStatsLocked(layerID);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700379}
380
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700381void TimeStats::setPresentFence(int32_t layerID, uint64_t frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700382 const std::shared_ptr<FenceTime>& presentFence) {
383 if (!mEnabled.load()) return;
384
385 ATRACE_CALL();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700386 ALOGV("[%d]-[%" PRIu64 "]-PresentFenceTime[%" PRId64 "]", layerID, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700387 presentFence->getSignalTime());
388
389 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700390 if (!mTimeStatsTracker.count(layerID)) return;
391 LayerRecord& layerRecord = mTimeStatsTracker[layerID];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700392 if (layerRecord.waitData < 0 ||
393 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
394 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700395 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700396 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700397 timeRecord.presentFence = presentFence;
398 timeRecord.ready = true;
399 layerRecord.waitData++;
400 }
401
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700402 flushAvailableRecordsToStatsLocked(layerID);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700403}
404
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700405void TimeStats::onDestroy(int32_t layerID) {
Yiwei Zhangdc224042018-10-18 15:34:00 -0700406 if (!mEnabled.load()) return;
407
408 ATRACE_CALL();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700409 ALOGV("[%d]-onDestroy", layerID);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700410
411 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700412 if (!mTimeStatsTracker.count(layerID)) return;
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700413 mTimeStatsTracker.erase(layerID);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700414}
415
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700416void TimeStats::removeTimeRecord(int32_t layerID, uint64_t frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700417 if (!mEnabled.load()) return;
418
419 ATRACE_CALL();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700420 ALOGV("[%d]-[%" PRIu64 "]-removeTimeRecord", layerID, frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700421
422 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700423 if (!mTimeStatsTracker.count(layerID)) return;
424 LayerRecord& layerRecord = mTimeStatsTracker[layerID];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700425 size_t removeAt = 0;
426 for (const TimeRecord& record : layerRecord.timeRecords) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700427 if (record.frameTime.frameNumber == frameNumber) break;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700428 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 Zhangeaeea062018-06-28 14:46:51 -0700433 layerRecord.waitData--;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700434 }
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700435 layerRecord.droppedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700436}
437
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700438void TimeStats::flushPowerTimeLocked() {
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700439 if (!mEnabled.load()) return;
440
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700441 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
459void 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 Mourifb571ea2019-01-24 18:42:10 -0800473void 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 Zhangce6ebc02018-10-20 12:42:38 -0700482void 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 Zhange5c49d52018-10-29 00:15:31 -0700499 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 Zhangce6ebc02018-10-20 12:42:38 -0700506
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700507 mGlobalRecord.prevPresentTime = curPresentTime;
508 mGlobalRecord.presentFences.pop_front();
509 }
510}
511
512void 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 Zhange5c49d52018-10-29 00:15:31 -0700517 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 Zhangce6ebc02018-10-20 12:42:38 -0700526 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 Zhang0102ad22018-05-02 17:37:17 -0700542void TimeStats::enable() {
543 if (mEnabled.load()) return;
544
545 ATRACE_CALL();
546
547 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700548 mEnabled.store(true);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700549 mTimeStats.statsStart = static_cast<int64_t>(std::time(0));
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700550 mPowerTime.prevTime = systemTime();
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700551 ALOGD("Enabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700552}
553
554void TimeStats::disable() {
555 if (!mEnabled.load()) return;
556
557 ATRACE_CALL();
558
559 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700560 flushPowerTimeLocked();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700561 mEnabled.store(false);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700562 mTimeStats.statsEnd = static_cast<int64_t>(std::time(0));
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700563 ALOGD("Disabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700564}
565
566void TimeStats::clear() {
567 ATRACE_CALL();
568
569 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700570 mTimeStatsTracker.clear();
Yiwei Zhangdc224042018-10-18 15:34:00 -0700571 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 Zhang3a226d22018-10-16 09:23:03 -0700577 mTimeStats.displayOnTime = 0;
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700578 mTimeStats.presentToPresent.hist.clear();
Alec Mourifb571ea2019-01-24 18:42:10 -0800579 mTimeStats.refreshRateStats.clear();
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700580 mPowerTime.prevTime = systemTime();
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700581 mGlobalRecord.prevPresentTime = 0;
582 mGlobalRecord.presentFences.clear();
583 ALOGD("Cleared");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700584}
585
586bool TimeStats::isEnabled() {
587 return mEnabled.load();
588}
589
Yiwei Zhang5434a782018-12-05 18:06:32 -0800590void TimeStats::dump(bool asProto, std::optional<uint32_t> maxLayers, std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700591 ATRACE_CALL();
592
593 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700594 if (mTimeStats.statsStart == 0) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700595 return;
596 }
597
Yiwei Zhangdc224042018-10-18 15:34:00 -0700598 mTimeStats.statsEnd = static_cast<int64_t>(std::time(0));
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700599
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700600 flushPowerTimeLocked();
601
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700602 if (asProto) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700603 ALOGD("Dumping TimeStats as proto");
Yiwei Zhangdc224042018-10-18 15:34:00 -0700604 SFTimeStatsGlobalProto timeStatsProto = mTimeStats.toProto(maxLayers);
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700605 result.append(timeStatsProto.SerializeAsString().c_str(), timeStatsProto.ByteSize());
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700606 } else {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700607 ALOGD("Dumping TimeStats as text");
Yiwei Zhang5434a782018-12-05 18:06:32 -0800608 result.append(mTimeStats.toString(maxLayers));
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700609 result.append("\n");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700610 }
611}
612
Alec Mourifb571ea2019-01-24 18:42:10 -0800613} // namespace impl
614
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700615} // namespace android