blob: 6a5488aa2cebcf7ec111a9a05a9fa437b901f11c [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
Yiwei Zhang0102ad22018-05-02 17:37:17 -070035void TimeStats::parseArgs(bool asProto, const Vector<String16>& args, size_t& index,
Yiwei Zhang5434a782018-12-05 18:06:32 -080036 std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070037 ATRACE_CALL();
38
39 if (args.size() > index + 10) {
40 ALOGD("Invalid args count");
41 return;
42 }
43
44 std::unordered_map<std::string, int32_t> argsMap;
45 while (index < args.size()) {
46 argsMap[std::string(String8(args[index]).c_str())] = index;
47 ++index;
48 }
49
50 if (argsMap.count("-disable")) {
51 disable();
52 }
53
54 if (argsMap.count("-dump")) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070055 std::optional<uint32_t> maxLayers = std::nullopt;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070056 auto iter = argsMap.find("-maxlayers");
57 if (iter != argsMap.end() && iter->second + 1 < static_cast<int32_t>(args.size())) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070058 int64_t value = strtol(String8(args[iter->second + 1]).c_str(), nullptr, 10);
59 value = std::clamp(value, int64_t(0), int64_t(UINT32_MAX));
60 maxLayers = static_cast<uint32_t>(value);
Yiwei Zhang0102ad22018-05-02 17:37:17 -070061 }
62
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070063 dump(asProto, maxLayers, result);
Yiwei Zhang0102ad22018-05-02 17:37:17 -070064 }
65
66 if (argsMap.count("-clear")) {
67 clear();
68 }
69
70 if (argsMap.count("-enable")) {
71 enable();
72 }
73}
74
75void TimeStats::incrementTotalFrames() {
76 if (!mEnabled.load()) return;
77
78 ATRACE_CALL();
79
80 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -070081 mTimeStats.totalFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070082}
83
Yiwei Zhang621f9d42018-05-07 10:40:55 -070084void TimeStats::incrementMissedFrames() {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070085 if (!mEnabled.load()) return;
86
87 ATRACE_CALL();
88
89 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -070090 mTimeStats.missedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070091}
92
93void TimeStats::incrementClientCompositionFrames() {
94 if (!mEnabled.load()) return;
95
96 ATRACE_CALL();
97
98 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -070099 mTimeStats.clientCompositionFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700100}
101
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700102bool TimeStats::recordReadyLocked(int32_t layerID, TimeRecord* timeRecord) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700103 if (!timeRecord->ready) {
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700104 ALOGV("[%d]-[%" PRIu64 "]-presentFence is still not received", layerID,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700105 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700106 return false;
107 }
108
109 if (timeRecord->acquireFence != nullptr) {
110 if (timeRecord->acquireFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
111 return false;
112 }
113 if (timeRecord->acquireFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700114 timeRecord->frameTime.acquireTime = timeRecord->acquireFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700115 timeRecord->acquireFence = nullptr;
116 } else {
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700117 ALOGV("[%d]-[%" PRIu64 "]-acquireFence signal time is invalid", layerID,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700118 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700119 }
120 }
121
122 if (timeRecord->presentFence != nullptr) {
123 if (timeRecord->presentFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
124 return false;
125 }
126 if (timeRecord->presentFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700127 timeRecord->frameTime.presentTime = timeRecord->presentFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700128 timeRecord->presentFence = nullptr;
129 } else {
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700130 ALOGV("[%d]-[%" PRIu64 "]-presentFence signal time invalid", layerID,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700131 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700132 }
133 }
134
135 return true;
136}
137
138static int32_t msBetween(nsecs_t start, nsecs_t end) {
139 int64_t delta = (end - start) / 1000000;
140 delta = std::clamp(delta, int64_t(INT32_MIN), int64_t(INT32_MAX));
141 return static_cast<int32_t>(delta);
142}
143
Yiwei Zhangbd408322018-10-15 18:31:53 -0700144// This regular expression captures the following for instance:
145// StatusBar in StatusBar#0
146// com.appname in com.appname/com.appname.activity#0
147// com.appname in SurfaceView - com.appname/com.appname.activity#0
148static const std::regex packageNameRegex("(?:SurfaceView[-\\s\\t]+)?([^/]+).*#\\d+");
149
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700150static std::string getPackageName(const std::string& layerName) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700151 std::smatch match;
Yiwei Zhangbd408322018-10-15 18:31:53 -0700152 if (std::regex_match(layerName.begin(), layerName.end(), match, packageNameRegex)) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700153 // There must be a match for group 1 otherwise the whole string is not
154 // matched and the above will return false
155 return match[1];
156 }
157 return "";
158}
159
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700160void TimeStats::flushAvailableRecordsToStatsLocked(int32_t layerID) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700161 ATRACE_CALL();
162
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700163 LayerRecord& layerRecord = mTimeStatsTracker[layerID];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700164 TimeRecord& prevTimeRecord = layerRecord.prevTimeRecord;
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700165 std::deque<TimeRecord>& timeRecords = layerRecord.timeRecords;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700166 while (!timeRecords.empty()) {
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700167 if (!recordReadyLocked(layerID, &timeRecords[0])) break;
168 ALOGV("[%d]-[%" PRIu64 "]-presentFenceTime[%" PRId64 "]", layerID,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700169 timeRecords[0].frameTime.frameNumber, timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700170
Raymond Chiu361ea972018-10-26 14:14:37 -0700171 const std::string& layerName = layerRecord.layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700172 if (prevTimeRecord.ready) {
Yiwei Zhangdc224042018-10-18 15:34:00 -0700173 if (!mTimeStats.stats.count(layerName)) {
174 mTimeStats.stats[layerName].layerName = layerName;
175 mTimeStats.stats[layerName].packageName = getPackageName(layerName);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700176 }
Yiwei Zhangdc224042018-10-18 15:34:00 -0700177 TimeStatsHelper::TimeStatsLayer& timeStatsLayer = mTimeStats.stats[layerName];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700178 timeStatsLayer.totalFrames++;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700179 timeStatsLayer.droppedFrames += layerRecord.droppedFrames;
180 layerRecord.droppedFrames = 0;
181
182 const int32_t postToAcquireMs = msBetween(timeRecords[0].frameTime.postTime,
183 timeRecords[0].frameTime.acquireTime);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700184 ALOGV("[%d]-[%" PRIu64 "]-post2acquire[%d]", layerID,
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700185 timeRecords[0].frameTime.frameNumber, postToAcquireMs);
186 timeStatsLayer.deltas["post2acquire"].insert(postToAcquireMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700187
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700188 const int32_t postToPresentMs = msBetween(timeRecords[0].frameTime.postTime,
189 timeRecords[0].frameTime.presentTime);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700190 ALOGV("[%d]-[%" PRIu64 "]-post2present[%d]", layerID,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700191 timeRecords[0].frameTime.frameNumber, postToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700192 timeStatsLayer.deltas["post2present"].insert(postToPresentMs);
193
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700194 const int32_t acquireToPresentMs = msBetween(timeRecords[0].frameTime.acquireTime,
195 timeRecords[0].frameTime.presentTime);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700196 ALOGV("[%d]-[%" PRIu64 "]-acquire2present[%d]", layerID,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700197 timeRecords[0].frameTime.frameNumber, acquireToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700198 timeStatsLayer.deltas["acquire2present"].insert(acquireToPresentMs);
199
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700200 const int32_t latchToPresentMs = msBetween(timeRecords[0].frameTime.latchTime,
201 timeRecords[0].frameTime.presentTime);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700202 ALOGV("[%d]-[%" PRIu64 "]-latch2present[%d]", layerID,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700203 timeRecords[0].frameTime.frameNumber, latchToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700204 timeStatsLayer.deltas["latch2present"].insert(latchToPresentMs);
205
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700206 const int32_t desiredToPresentMs = msBetween(timeRecords[0].frameTime.desiredTime,
207 timeRecords[0].frameTime.presentTime);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700208 ALOGV("[%d]-[%" PRIu64 "]-desired2present[%d]", layerID,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700209 timeRecords[0].frameTime.frameNumber, desiredToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700210 timeStatsLayer.deltas["desired2present"].insert(desiredToPresentMs);
211
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700212 const int32_t presentToPresentMs = msBetween(prevTimeRecord.frameTime.presentTime,
213 timeRecords[0].frameTime.presentTime);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700214 ALOGV("[%d]-[%" PRIu64 "]-present2present[%d]", layerID,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700215 timeRecords[0].frameTime.frameNumber, presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700216 timeStatsLayer.deltas["present2present"].insert(presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700217 }
Raymond Chiu361ea972018-10-26 14:14:37 -0700218
219 // Output additional trace points to track frame time.
220 ATRACE_INT64(("TimeStats-Post - " + layerName).c_str(), timeRecords[0].frameTime.postTime);
221 ATRACE_INT64(("TimeStats-Acquire - " + layerName).c_str(),
222 timeRecords[0].frameTime.acquireTime);
223 ATRACE_INT64(("TimeStats-Latch - " + layerName).c_str(),
224 timeRecords[0].frameTime.latchTime);
225 ATRACE_INT64(("TimeStats-Desired - " + layerName).c_str(),
226 timeRecords[0].frameTime.desiredTime);
227 ATRACE_INT64(("TimeStats-Present - " + layerName).c_str(),
228 timeRecords[0].frameTime.presentTime);
229
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700230 prevTimeRecord = timeRecords[0];
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700231 timeRecords.pop_front();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700232 layerRecord.waitData--;
233 }
234}
235
Yiwei Zhangbd408322018-10-15 18:31:53 -0700236// This regular expression captures the following layer names for instance:
237// 1) StatusBat#0
238// 2) NavigationBar#1
239// 3) co(m).*#0
240// 4) SurfaceView - co(m).*#0
241// Using [-\\s\t]+ for the conjunction part between SurfaceView and co(m).*
242// is a bit more robust in case there's a slight change.
243// The layer name would only consist of . / $ _ 0-9 a-z A-Z in most cases.
244static const std::regex layerNameRegex(
245 "(((SurfaceView[-\\s\\t]+)?com?\\.[./$\\w]+)|((Status|Navigation)Bar))#\\d+");
246
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700247static bool layerNameIsValid(const std::string& layerName) {
Yiwei Zhangbd408322018-10-15 18:31:53 -0700248 return std::regex_match(layerName.begin(), layerName.end(), layerNameRegex);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700249}
250
Yiwei Zhang8e8fe522018-11-02 18:34:07 -0700251void TimeStats::setPostTime(int32_t layerID, uint64_t frameNumber, const std::string& layerName,
252 nsecs_t postTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700253 if (!mEnabled.load()) return;
254
255 ATRACE_CALL();
Yiwei Zhang8e8fe522018-11-02 18:34:07 -0700256 ALOGV("[%d]-[%" PRIu64 "]-[%s]-PostTime[%" PRId64 "]", layerID, frameNumber, layerName.c_str(),
257 postTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700258
259 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700260 if (!mTimeStatsTracker.count(layerID) && layerNameIsValid(layerName)) {
261 mTimeStatsTracker[layerID].layerName = layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700262 }
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700263 if (!mTimeStatsTracker.count(layerID)) return;
264 LayerRecord& layerRecord = mTimeStatsTracker[layerID];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700265 if (layerRecord.timeRecords.size() == MAX_NUM_TIME_RECORDS) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800266 ALOGE("[%d]-[%s]-timeRecords is at its maximum size[%zu]. Ignore this when unittesting.",
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700267 layerID, layerRecord.layerName.c_str(), MAX_NUM_TIME_RECORDS);
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800268 mTimeStatsTracker.erase(layerID);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700269 return;
270 }
271 // For most media content, the acquireFence is invalid because the buffer is
272 // ready at the queueBuffer stage. In this case, acquireTime should be given
273 // a default value as postTime.
274 TimeRecord timeRecord = {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700275 .frameTime =
276 {
277 .frameNumber = frameNumber,
278 .postTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800279 .latchTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700280 .acquireTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800281 .desiredTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700282 },
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700283 };
284 layerRecord.timeRecords.push_back(timeRecord);
285 if (layerRecord.waitData < 0 ||
286 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
287 layerRecord.waitData = layerRecord.timeRecords.size() - 1;
288}
289
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700290void TimeStats::setLatchTime(int32_t layerID, uint64_t frameNumber, nsecs_t latchTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700291 if (!mEnabled.load()) return;
292
293 ATRACE_CALL();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700294 ALOGV("[%d]-[%" PRIu64 "]-LatchTime[%" PRId64 "]", layerID, frameNumber, latchTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700295
296 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700297 if (!mTimeStatsTracker.count(layerID)) return;
298 LayerRecord& layerRecord = mTimeStatsTracker[layerID];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700299 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700300 if (timeRecord.frameTime.frameNumber == frameNumber) {
301 timeRecord.frameTime.latchTime = latchTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700302 }
303}
304
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700305void TimeStats::setDesiredTime(int32_t layerID, uint64_t frameNumber, nsecs_t desiredTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700306 if (!mEnabled.load()) return;
307
308 ATRACE_CALL();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700309 ALOGV("[%d]-[%" PRIu64 "]-DesiredTime[%" PRId64 "]", layerID, frameNumber, desiredTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700310
311 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700312 if (!mTimeStatsTracker.count(layerID)) return;
313 LayerRecord& layerRecord = mTimeStatsTracker[layerID];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700314 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700315 if (timeRecord.frameTime.frameNumber == frameNumber) {
316 timeRecord.frameTime.desiredTime = desiredTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700317 }
318}
319
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700320void TimeStats::setAcquireTime(int32_t layerID, uint64_t frameNumber, nsecs_t acquireTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700321 if (!mEnabled.load()) return;
322
323 ATRACE_CALL();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700324 ALOGV("[%d]-[%" PRIu64 "]-AcquireTime[%" PRId64 "]", layerID, frameNumber, acquireTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700325
326 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700327 if (!mTimeStatsTracker.count(layerID)) return;
328 LayerRecord& layerRecord = mTimeStatsTracker[layerID];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700329 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700330 if (timeRecord.frameTime.frameNumber == frameNumber) {
331 timeRecord.frameTime.acquireTime = acquireTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700332 }
333}
334
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700335void TimeStats::setAcquireFence(int32_t layerID, uint64_t frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700336 const std::shared_ptr<FenceTime>& acquireFence) {
337 if (!mEnabled.load()) return;
338
339 ATRACE_CALL();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700340 ALOGV("[%d]-[%" PRIu64 "]-AcquireFenceTime[%" PRId64 "]", layerID, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700341 acquireFence->getSignalTime());
342
343 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700344 if (!mTimeStatsTracker.count(layerID)) return;
345 LayerRecord& layerRecord = mTimeStatsTracker[layerID];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700346 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700347 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700348 timeRecord.acquireFence = acquireFence;
349 }
350}
351
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700352void TimeStats::setPresentTime(int32_t layerID, uint64_t frameNumber, nsecs_t presentTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700353 if (!mEnabled.load()) return;
354
355 ATRACE_CALL();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700356 ALOGV("[%d]-[%" PRIu64 "]-PresentTime[%" PRId64 "]", layerID, frameNumber, presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700357
358 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700359 if (!mTimeStatsTracker.count(layerID)) return;
360 LayerRecord& layerRecord = mTimeStatsTracker[layerID];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700361 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700362 if (timeRecord.frameTime.frameNumber == frameNumber) {
363 timeRecord.frameTime.presentTime = presentTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700364 timeRecord.ready = true;
365 layerRecord.waitData++;
366 }
367
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700368 flushAvailableRecordsToStatsLocked(layerID);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700369}
370
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700371void TimeStats::setPresentFence(int32_t layerID, uint64_t frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700372 const std::shared_ptr<FenceTime>& presentFence) {
373 if (!mEnabled.load()) return;
374
375 ATRACE_CALL();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700376 ALOGV("[%d]-[%" PRIu64 "]-PresentFenceTime[%" PRId64 "]", layerID, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700377 presentFence->getSignalTime());
378
379 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700380 if (!mTimeStatsTracker.count(layerID)) return;
381 LayerRecord& layerRecord = mTimeStatsTracker[layerID];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700382 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700383 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700384 timeRecord.presentFence = presentFence;
385 timeRecord.ready = true;
386 layerRecord.waitData++;
387 }
388
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700389 flushAvailableRecordsToStatsLocked(layerID);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700390}
391
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700392void TimeStats::onDestroy(int32_t layerID) {
Yiwei Zhangdc224042018-10-18 15:34:00 -0700393 if (!mEnabled.load()) return;
394
395 ATRACE_CALL();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700396 ALOGV("[%d]-onDestroy", layerID);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700397
398 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700399 if (!mTimeStatsTracker.count(layerID)) return;
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700400 mTimeStatsTracker.erase(layerID);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700401}
402
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700403void TimeStats::removeTimeRecord(int32_t layerID, uint64_t frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700404 if (!mEnabled.load()) return;
405
406 ATRACE_CALL();
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700407 ALOGV("[%d]-[%" PRIu64 "]-removeTimeRecord", layerID, frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700408
409 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700410 if (!mTimeStatsTracker.count(layerID)) return;
411 LayerRecord& layerRecord = mTimeStatsTracker[layerID];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700412 size_t removeAt = 0;
413 for (const TimeRecord& record : layerRecord.timeRecords) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700414 if (record.frameTime.frameNumber == frameNumber) break;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700415 removeAt++;
416 }
417 if (removeAt == layerRecord.timeRecords.size()) return;
418 layerRecord.timeRecords.erase(layerRecord.timeRecords.begin() + removeAt);
419 if (layerRecord.waitData > static_cast<int32_t>(removeAt)) {
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700420 layerRecord.waitData--;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700421 }
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700422 layerRecord.droppedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700423}
424
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700425void TimeStats::flushPowerTimeLocked() {
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700426 if (!mEnabled.load()) return;
427
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700428 nsecs_t curTime = systemTime();
429 // elapsedTime is in milliseconds.
430 int64_t elapsedTime = (curTime - mPowerTime.prevTime) / 1000000;
431
432 switch (mPowerTime.powerMode) {
433 case HWC_POWER_MODE_NORMAL:
434 mTimeStats.displayOnTime += elapsedTime;
435 break;
436 case HWC_POWER_MODE_OFF:
437 case HWC_POWER_MODE_DOZE:
438 case HWC_POWER_MODE_DOZE_SUSPEND:
439 default:
440 break;
441 }
442
443 mPowerTime.prevTime = curTime;
444}
445
446void TimeStats::setPowerMode(int32_t powerMode) {
447 if (!mEnabled.load()) {
448 std::lock_guard<std::mutex> lock(mMutex);
449 mPowerTime.powerMode = powerMode;
450 return;
451 }
452
453 std::lock_guard<std::mutex> lock(mMutex);
454 if (powerMode == mPowerTime.powerMode) return;
455
456 flushPowerTimeLocked();
457 mPowerTime.powerMode = powerMode;
458}
459
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700460void TimeStats::flushAvailableGlobalRecordsToStatsLocked() {
461 ATRACE_CALL();
462
463 while (!mGlobalRecord.presentFences.empty()) {
464 const nsecs_t curPresentTime = mGlobalRecord.presentFences.front()->getSignalTime();
465 if (curPresentTime == Fence::SIGNAL_TIME_PENDING) break;
466
467 if (curPresentTime == Fence::SIGNAL_TIME_INVALID) {
468 ALOGE("GlobalPresentFence is invalid!");
469 mGlobalRecord.prevPresentTime = 0;
470 mGlobalRecord.presentFences.pop_front();
471 continue;
472 }
473
474 ALOGV("GlobalPresentFenceTime[%" PRId64 "]",
475 mGlobalRecord.presentFences.front()->getSignalTime());
476
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700477 if (mGlobalRecord.prevPresentTime != 0) {
478 const int32_t presentToPresentMs =
479 msBetween(mGlobalRecord.prevPresentTime, curPresentTime);
480 ALOGV("Global present2present[%d] prev[%" PRId64 "] curr[%" PRId64 "]",
481 presentToPresentMs, mGlobalRecord.prevPresentTime, curPresentTime);
482 mTimeStats.presentToPresent.insert(presentToPresentMs);
483 }
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700484
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700485 mGlobalRecord.prevPresentTime = curPresentTime;
486 mGlobalRecord.presentFences.pop_front();
487 }
488}
489
490void TimeStats::setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) {
491 if (!mEnabled.load()) return;
492
493 ATRACE_CALL();
494 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700495 if (presentFence == nullptr || !presentFence->isValid()) {
496 mGlobalRecord.prevPresentTime = 0;
497 return;
498 }
499
500 if (mPowerTime.powerMode != HWC_POWER_MODE_NORMAL) {
501 // Try flushing the last present fence on HWC_POWER_MODE_NORMAL.
502 flushAvailableGlobalRecordsToStatsLocked();
503 mGlobalRecord.presentFences.clear();
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700504 mGlobalRecord.prevPresentTime = 0;
505 return;
506 }
507
508 if (mGlobalRecord.presentFences.size() == MAX_NUM_TIME_RECORDS) {
509 // The front presentFence must be trapped in pending status in this
510 // case. Try dequeuing the front one to recover.
511 ALOGE("GlobalPresentFences is already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
512 mGlobalRecord.prevPresentTime = 0;
513 mGlobalRecord.presentFences.pop_front();
514 }
515
516 mGlobalRecord.presentFences.emplace_back(presentFence);
517 flushAvailableGlobalRecordsToStatsLocked();
518}
519
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700520void TimeStats::enable() {
521 if (mEnabled.load()) return;
522
523 ATRACE_CALL();
524
525 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700526 mEnabled.store(true);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700527 mTimeStats.statsStart = static_cast<int64_t>(std::time(0));
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700528 mPowerTime.prevTime = systemTime();
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700529 ALOGD("Enabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700530}
531
532void TimeStats::disable() {
533 if (!mEnabled.load()) return;
534
535 ATRACE_CALL();
536
537 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700538 flushPowerTimeLocked();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700539 mEnabled.store(false);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700540 mTimeStats.statsEnd = static_cast<int64_t>(std::time(0));
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700541 ALOGD("Disabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700542}
543
544void TimeStats::clear() {
545 ATRACE_CALL();
546
547 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700548 mTimeStatsTracker.clear();
Yiwei Zhangdc224042018-10-18 15:34:00 -0700549 mTimeStats.stats.clear();
550 mTimeStats.statsStart = (mEnabled.load() ? static_cast<int64_t>(std::time(0)) : 0);
551 mTimeStats.statsEnd = 0;
552 mTimeStats.totalFrames = 0;
553 mTimeStats.missedFrames = 0;
554 mTimeStats.clientCompositionFrames = 0;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700555 mTimeStats.displayOnTime = 0;
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700556 mTimeStats.presentToPresent.hist.clear();
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700557 mPowerTime.prevTime = systemTime();
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700558 mGlobalRecord.prevPresentTime = 0;
559 mGlobalRecord.presentFences.clear();
560 ALOGD("Cleared");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700561}
562
563bool TimeStats::isEnabled() {
564 return mEnabled.load();
565}
566
Yiwei Zhang5434a782018-12-05 18:06:32 -0800567void TimeStats::dump(bool asProto, std::optional<uint32_t> maxLayers, std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700568 ATRACE_CALL();
569
570 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700571 if (mTimeStats.statsStart == 0) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700572 return;
573 }
574
Yiwei Zhangdc224042018-10-18 15:34:00 -0700575 mTimeStats.statsEnd = static_cast<int64_t>(std::time(0));
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700576
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700577 flushPowerTimeLocked();
578
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700579 if (asProto) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700580 ALOGD("Dumping TimeStats as proto");
Yiwei Zhangdc224042018-10-18 15:34:00 -0700581 SFTimeStatsGlobalProto timeStatsProto = mTimeStats.toProto(maxLayers);
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700582 result.append(timeStatsProto.SerializeAsString().c_str(), timeStatsProto.ByteSize());
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700583 } else {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700584 ALOGD("Dumping TimeStats as text");
Yiwei Zhang5434a782018-12-05 18:06:32 -0800585 result.append(mTimeStats.toString(maxLayers));
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700586 result.append("\n");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700587 }
588}
589
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700590} // namespace android