blob: 78c6e74150ddad6eee88dfba5a5d329757473126 [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 Zhang0102ad22018-05-02 17:37:17 -0700294 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700295 if (timeRecord.frameTime.frameNumber == frameNumber) {
296 timeRecord.frameTime.latchTime = latchTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700297 }
298}
299
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700300void TimeStats::setDesiredTime(int32_t layerID, uint64_t frameNumber, nsecs_t desiredTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700301 if (!mEnabled.load()) return;
302
303 ATRACE_CALL();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700304 ALOGV("[%d]-[%" PRIu64 "]-DesiredTime[%" PRId64 "]", layerID, frameNumber, desiredTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700305
306 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700307 if (!mTimeStatsTracker.count(layerID)) return;
308 LayerRecord& layerRecord = mTimeStatsTracker[layerID];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700309 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700310 if (timeRecord.frameTime.frameNumber == frameNumber) {
311 timeRecord.frameTime.desiredTime = desiredTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700312 }
313}
314
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700315void TimeStats::setAcquireTime(int32_t layerID, uint64_t frameNumber, nsecs_t acquireTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700316 if (!mEnabled.load()) return;
317
318 ATRACE_CALL();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700319 ALOGV("[%d]-[%" PRIu64 "]-AcquireTime[%" PRId64 "]", layerID, frameNumber, acquireTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700320
321 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700322 if (!mTimeStatsTracker.count(layerID)) return;
323 LayerRecord& layerRecord = mTimeStatsTracker[layerID];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700324 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700325 if (timeRecord.frameTime.frameNumber == frameNumber) {
326 timeRecord.frameTime.acquireTime = acquireTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700327 }
328}
329
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700330void TimeStats::setAcquireFence(int32_t layerID, uint64_t frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700331 const std::shared_ptr<FenceTime>& acquireFence) {
332 if (!mEnabled.load()) return;
333
334 ATRACE_CALL();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700335 ALOGV("[%d]-[%" PRIu64 "]-AcquireFenceTime[%" PRId64 "]", layerID, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700336 acquireFence->getSignalTime());
337
338 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700339 if (!mTimeStatsTracker.count(layerID)) return;
340 LayerRecord& layerRecord = mTimeStatsTracker[layerID];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700341 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700342 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700343 timeRecord.acquireFence = acquireFence;
344 }
345}
346
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700347void TimeStats::setPresentTime(int32_t layerID, uint64_t frameNumber, nsecs_t presentTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700348 if (!mEnabled.load()) return;
349
350 ATRACE_CALL();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700351 ALOGV("[%d]-[%" PRIu64 "]-PresentTime[%" PRId64 "]", layerID, frameNumber, presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700352
353 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700354 if (!mTimeStatsTracker.count(layerID)) return;
355 LayerRecord& layerRecord = mTimeStatsTracker[layerID];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700356 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700357 if (timeRecord.frameTime.frameNumber == frameNumber) {
358 timeRecord.frameTime.presentTime = presentTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700359 timeRecord.ready = true;
360 layerRecord.waitData++;
361 }
362
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700363 flushAvailableRecordsToStatsLocked(layerID);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700364}
365
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700366void TimeStats::setPresentFence(int32_t layerID, uint64_t frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700367 const std::shared_ptr<FenceTime>& presentFence) {
368 if (!mEnabled.load()) return;
369
370 ATRACE_CALL();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700371 ALOGV("[%d]-[%" PRIu64 "]-PresentFenceTime[%" PRId64 "]", layerID, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700372 presentFence->getSignalTime());
373
374 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700375 if (!mTimeStatsTracker.count(layerID)) return;
376 LayerRecord& layerRecord = mTimeStatsTracker[layerID];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700377 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700378 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700379 timeRecord.presentFence = presentFence;
380 timeRecord.ready = true;
381 layerRecord.waitData++;
382 }
383
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700384 flushAvailableRecordsToStatsLocked(layerID);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700385}
386
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700387void TimeStats::onDestroy(int32_t layerID) {
Yiwei Zhangdc224042018-10-18 15:34:00 -0700388 if (!mEnabled.load()) return;
389
390 ATRACE_CALL();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700391 ALOGV("[%d]-onDestroy", layerID);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700392
393 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700394 if (!mTimeStatsTracker.count(layerID)) return;
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700395 mTimeStatsTracker.erase(layerID);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700396}
397
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700398void TimeStats::removeTimeRecord(int32_t layerID, uint64_t frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700399 if (!mEnabled.load()) return;
400
401 ATRACE_CALL();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700402 ALOGV("[%d]-[%" PRIu64 "]-removeTimeRecord", layerID, frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700403
404 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700405 if (!mTimeStatsTracker.count(layerID)) return;
406 LayerRecord& layerRecord = mTimeStatsTracker[layerID];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700407 size_t removeAt = 0;
408 for (const TimeRecord& record : layerRecord.timeRecords) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700409 if (record.frameTime.frameNumber == frameNumber) break;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700410 removeAt++;
411 }
412 if (removeAt == layerRecord.timeRecords.size()) return;
413 layerRecord.timeRecords.erase(layerRecord.timeRecords.begin() + removeAt);
414 if (layerRecord.waitData > static_cast<int32_t>(removeAt)) {
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700415 layerRecord.waitData--;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700416 }
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700417 layerRecord.droppedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700418}
419
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700420void TimeStats::flushPowerTimeLocked() {
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700421 if (!mEnabled.load()) return;
422
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700423 nsecs_t curTime = systemTime();
424 // elapsedTime is in milliseconds.
425 int64_t elapsedTime = (curTime - mPowerTime.prevTime) / 1000000;
426
427 switch (mPowerTime.powerMode) {
428 case HWC_POWER_MODE_NORMAL:
429 mTimeStats.displayOnTime += elapsedTime;
430 break;
431 case HWC_POWER_MODE_OFF:
432 case HWC_POWER_MODE_DOZE:
433 case HWC_POWER_MODE_DOZE_SUSPEND:
434 default:
435 break;
436 }
437
438 mPowerTime.prevTime = curTime;
439}
440
441void TimeStats::setPowerMode(int32_t powerMode) {
442 if (!mEnabled.load()) {
443 std::lock_guard<std::mutex> lock(mMutex);
444 mPowerTime.powerMode = powerMode;
445 return;
446 }
447
448 std::lock_guard<std::mutex> lock(mMutex);
449 if (powerMode == mPowerTime.powerMode) return;
450
451 flushPowerTimeLocked();
452 mPowerTime.powerMode = powerMode;
453}
454
Alec Mourifb571ea2019-01-24 18:42:10 -0800455void TimeStats::recordRefreshRate(uint32_t fps, nsecs_t duration) {
456 std::lock_guard<std::mutex> lock(mMutex);
457 if (mTimeStats.refreshRateStats.count(fps)) {
458 mTimeStats.refreshRateStats[fps] += duration;
459 } else {
460 mTimeStats.refreshRateStats.insert({fps, duration});
461 }
462}
463
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700464void TimeStats::flushAvailableGlobalRecordsToStatsLocked() {
465 ATRACE_CALL();
466
467 while (!mGlobalRecord.presentFences.empty()) {
468 const nsecs_t curPresentTime = mGlobalRecord.presentFences.front()->getSignalTime();
469 if (curPresentTime == Fence::SIGNAL_TIME_PENDING) break;
470
471 if (curPresentTime == Fence::SIGNAL_TIME_INVALID) {
472 ALOGE("GlobalPresentFence is invalid!");
473 mGlobalRecord.prevPresentTime = 0;
474 mGlobalRecord.presentFences.pop_front();
475 continue;
476 }
477
478 ALOGV("GlobalPresentFenceTime[%" PRId64 "]",
479 mGlobalRecord.presentFences.front()->getSignalTime());
480
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700481 if (mGlobalRecord.prevPresentTime != 0) {
482 const int32_t presentToPresentMs =
483 msBetween(mGlobalRecord.prevPresentTime, curPresentTime);
484 ALOGV("Global present2present[%d] prev[%" PRId64 "] curr[%" PRId64 "]",
485 presentToPresentMs, mGlobalRecord.prevPresentTime, curPresentTime);
486 mTimeStats.presentToPresent.insert(presentToPresentMs);
487 }
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700488
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700489 mGlobalRecord.prevPresentTime = curPresentTime;
490 mGlobalRecord.presentFences.pop_front();
491 }
492}
493
494void TimeStats::setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) {
495 if (!mEnabled.load()) return;
496
497 ATRACE_CALL();
498 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700499 if (presentFence == nullptr || !presentFence->isValid()) {
500 mGlobalRecord.prevPresentTime = 0;
501 return;
502 }
503
504 if (mPowerTime.powerMode != HWC_POWER_MODE_NORMAL) {
505 // Try flushing the last present fence on HWC_POWER_MODE_NORMAL.
506 flushAvailableGlobalRecordsToStatsLocked();
507 mGlobalRecord.presentFences.clear();
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700508 mGlobalRecord.prevPresentTime = 0;
509 return;
510 }
511
512 if (mGlobalRecord.presentFences.size() == MAX_NUM_TIME_RECORDS) {
513 // The front presentFence must be trapped in pending status in this
514 // case. Try dequeuing the front one to recover.
515 ALOGE("GlobalPresentFences is already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
516 mGlobalRecord.prevPresentTime = 0;
517 mGlobalRecord.presentFences.pop_front();
518 }
519
520 mGlobalRecord.presentFences.emplace_back(presentFence);
521 flushAvailableGlobalRecordsToStatsLocked();
522}
523
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700524void TimeStats::enable() {
525 if (mEnabled.load()) return;
526
527 ATRACE_CALL();
528
529 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700530 mEnabled.store(true);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700531 mTimeStats.statsStart = static_cast<int64_t>(std::time(0));
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700532 mPowerTime.prevTime = systemTime();
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700533 ALOGD("Enabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700534}
535
536void TimeStats::disable() {
537 if (!mEnabled.load()) return;
538
539 ATRACE_CALL();
540
541 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700542 flushPowerTimeLocked();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700543 mEnabled.store(false);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700544 mTimeStats.statsEnd = static_cast<int64_t>(std::time(0));
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700545 ALOGD("Disabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700546}
547
548void TimeStats::clear() {
549 ATRACE_CALL();
550
551 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700552 mTimeStatsTracker.clear();
Yiwei Zhangdc224042018-10-18 15:34:00 -0700553 mTimeStats.stats.clear();
554 mTimeStats.statsStart = (mEnabled.load() ? static_cast<int64_t>(std::time(0)) : 0);
555 mTimeStats.statsEnd = 0;
556 mTimeStats.totalFrames = 0;
557 mTimeStats.missedFrames = 0;
558 mTimeStats.clientCompositionFrames = 0;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700559 mTimeStats.displayOnTime = 0;
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700560 mTimeStats.presentToPresent.hist.clear();
Alec Mourifb571ea2019-01-24 18:42:10 -0800561 mTimeStats.refreshRateStats.clear();
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700562 mPowerTime.prevTime = systemTime();
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700563 mGlobalRecord.prevPresentTime = 0;
564 mGlobalRecord.presentFences.clear();
565 ALOGD("Cleared");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700566}
567
568bool TimeStats::isEnabled() {
569 return mEnabled.load();
570}
571
Yiwei Zhang5434a782018-12-05 18:06:32 -0800572void TimeStats::dump(bool asProto, std::optional<uint32_t> maxLayers, std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700573 ATRACE_CALL();
574
575 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700576 if (mTimeStats.statsStart == 0) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700577 return;
578 }
579
Yiwei Zhangdc224042018-10-18 15:34:00 -0700580 mTimeStats.statsEnd = static_cast<int64_t>(std::time(0));
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700581
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700582 flushPowerTimeLocked();
583
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700584 if (asProto) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700585 ALOGD("Dumping TimeStats as proto");
Yiwei Zhangdc224042018-10-18 15:34:00 -0700586 SFTimeStatsGlobalProto timeStatsProto = mTimeStats.toProto(maxLayers);
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700587 result.append(timeStatsProto.SerializeAsString().c_str(), timeStatsProto.ByteSize());
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700588 } else {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700589 ALOGD("Dumping TimeStats as text");
Yiwei Zhang5434a782018-12-05 18:06:32 -0800590 result.append(mTimeStats.toString(maxLayers));
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700591 result.append("\n");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700592 }
593}
594
Alec Mourifb571ea2019-01-24 18:42:10 -0800595} // namespace impl
596
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700597} // namespace android