blob: 6fb70f82cb90c916b57a0298827cde878bd10129 [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>
27#include <utils/Trace.h>
28
29#include <algorithm>
30#include <regex>
31
32namespace android {
33
34TimeStats& TimeStats::getInstance() {
Chia-I Wuebd88882018-09-14 11:02:37 -070035 static TimeStats sInstance;
36 return sInstance;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070037}
38
39void TimeStats::parseArgs(bool asProto, const Vector<String16>& args, size_t& index,
40 String8& result) {
41 ATRACE_CALL();
42
43 if (args.size() > index + 10) {
44 ALOGD("Invalid args count");
45 return;
46 }
47
48 std::unordered_map<std::string, int32_t> argsMap;
49 while (index < args.size()) {
50 argsMap[std::string(String8(args[index]).c_str())] = index;
51 ++index;
52 }
53
54 if (argsMap.count("-disable")) {
55 disable();
56 }
57
58 if (argsMap.count("-dump")) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070059 std::optional<uint32_t> maxLayers = std::nullopt;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070060 auto iter = argsMap.find("-maxlayers");
61 if (iter != argsMap.end() && iter->second + 1 < static_cast<int32_t>(args.size())) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070062 int64_t value = strtol(String8(args[iter->second + 1]).c_str(), nullptr, 10);
63 value = std::clamp(value, int64_t(0), int64_t(UINT32_MAX));
64 maxLayers = static_cast<uint32_t>(value);
Yiwei Zhang0102ad22018-05-02 17:37:17 -070065 }
66
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070067 dump(asProto, maxLayers, result);
Yiwei Zhang0102ad22018-05-02 17:37:17 -070068 }
69
70 if (argsMap.count("-clear")) {
71 clear();
72 }
73
74 if (argsMap.count("-enable")) {
75 enable();
76 }
77}
78
79void TimeStats::incrementTotalFrames() {
80 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.totalFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070086}
87
Yiwei Zhang621f9d42018-05-07 10:40:55 -070088void TimeStats::incrementMissedFrames() {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070089 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.missedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070095}
96
97void TimeStats::incrementClientCompositionFrames() {
98 if (!mEnabled.load()) return;
99
100 ATRACE_CALL();
101
102 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700103 mTimeStats.clientCompositionFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700104}
105
106bool TimeStats::recordReadyLocked(const std::string& layerName, TimeRecord* timeRecord) {
107 if (!timeRecord->ready) {
108 ALOGV("[%s]-[%" PRIu64 "]-presentFence is still not received", layerName.c_str(),
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700109 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700110 return false;
111 }
112
113 if (timeRecord->acquireFence != nullptr) {
114 if (timeRecord->acquireFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
115 return false;
116 }
117 if (timeRecord->acquireFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700118 timeRecord->frameTime.acquireTime = timeRecord->acquireFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700119 timeRecord->acquireFence = nullptr;
120 } else {
121 ALOGV("[%s]-[%" PRIu64 "]-acquireFence signal time is invalid", layerName.c_str(),
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700122 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700123 }
124 }
125
126 if (timeRecord->presentFence != nullptr) {
127 if (timeRecord->presentFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
128 return false;
129 }
130 if (timeRecord->presentFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700131 timeRecord->frameTime.presentTime = timeRecord->presentFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700132 timeRecord->presentFence = nullptr;
133 } else {
134 ALOGV("[%s]-[%" PRIu64 "]-presentFence signal time invalid", layerName.c_str(),
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700135 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700136 }
137 }
138
139 return true;
140}
141
142static int32_t msBetween(nsecs_t start, nsecs_t end) {
143 int64_t delta = (end - start) / 1000000;
144 delta = std::clamp(delta, int64_t(INT32_MIN), int64_t(INT32_MAX));
145 return static_cast<int32_t>(delta);
146}
147
Yiwei Zhangbd408322018-10-15 18:31:53 -0700148// This regular expression captures the following for instance:
149// StatusBar in StatusBar#0
150// com.appname in com.appname/com.appname.activity#0
151// com.appname in SurfaceView - com.appname/com.appname.activity#0
152static const std::regex packageNameRegex("(?:SurfaceView[-\\s\\t]+)?([^/]+).*#\\d+");
153
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700154static std::string getPackageName(const std::string& layerName) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700155 std::smatch match;
Yiwei Zhangbd408322018-10-15 18:31:53 -0700156 if (std::regex_match(layerName.begin(), layerName.end(), match, packageNameRegex)) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700157 // There must be a match for group 1 otherwise the whole string is not
158 // matched and the above will return false
159 return match[1];
160 }
161 return "";
162}
163
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700164void TimeStats::flushAvailableRecordsToStatsLocked(const std::string& layerName) {
165 ATRACE_CALL();
166
Yiwei Zhangdc224042018-10-18 15:34:00 -0700167 LayerRecord& layerRecord = mTimeStatsTracker[layerName];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700168 TimeRecord& prevTimeRecord = layerRecord.prevTimeRecord;
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700169 std::deque<TimeRecord>& timeRecords = layerRecord.timeRecords;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700170 while (!timeRecords.empty()) {
171 if (!recordReadyLocked(layerName, &timeRecords[0])) break;
172 ALOGV("[%s]-[%" PRIu64 "]-presentFenceTime[%" PRId64 "]", layerName.c_str(),
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700173 timeRecords[0].frameTime.frameNumber, timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700174
175 if (prevTimeRecord.ready) {
Yiwei Zhangdc224042018-10-18 15:34:00 -0700176 if (!mTimeStats.stats.count(layerName)) {
177 mTimeStats.stats[layerName].layerName = layerName;
178 mTimeStats.stats[layerName].packageName = getPackageName(layerName);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700179 }
Yiwei Zhangdc224042018-10-18 15:34:00 -0700180 TimeStatsHelper::TimeStatsLayer& timeStatsLayer = mTimeStats.stats[layerName];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700181 timeStatsLayer.totalFrames++;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700182 timeStatsLayer.droppedFrames += layerRecord.droppedFrames;
183 layerRecord.droppedFrames = 0;
184
185 const int32_t postToAcquireMs = msBetween(timeRecords[0].frameTime.postTime,
186 timeRecords[0].frameTime.acquireTime);
187 ALOGV("[%s]-[%" PRIu64 "]-post2acquire[%d]", layerName.c_str(),
188 timeRecords[0].frameTime.frameNumber, postToAcquireMs);
189 timeStatsLayer.deltas["post2acquire"].insert(postToAcquireMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700190
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700191 const int32_t postToPresentMs = msBetween(timeRecords[0].frameTime.postTime,
192 timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700193 ALOGV("[%s]-[%" PRIu64 "]-post2present[%d]", layerName.c_str(),
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700194 timeRecords[0].frameTime.frameNumber, postToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700195 timeStatsLayer.deltas["post2present"].insert(postToPresentMs);
196
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700197 const int32_t acquireToPresentMs = msBetween(timeRecords[0].frameTime.acquireTime,
198 timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700199 ALOGV("[%s]-[%" PRIu64 "]-acquire2present[%d]", layerName.c_str(),
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700200 timeRecords[0].frameTime.frameNumber, acquireToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700201 timeStatsLayer.deltas["acquire2present"].insert(acquireToPresentMs);
202
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700203 const int32_t latchToPresentMs = msBetween(timeRecords[0].frameTime.latchTime,
204 timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700205 ALOGV("[%s]-[%" PRIu64 "]-latch2present[%d]", layerName.c_str(),
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700206 timeRecords[0].frameTime.frameNumber, latchToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700207 timeStatsLayer.deltas["latch2present"].insert(latchToPresentMs);
208
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700209 const int32_t desiredToPresentMs = msBetween(timeRecords[0].frameTime.desiredTime,
210 timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700211 ALOGV("[%s]-[%" PRIu64 "]-desired2present[%d]", layerName.c_str(),
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700212 timeRecords[0].frameTime.frameNumber, desiredToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700213 timeStatsLayer.deltas["desired2present"].insert(desiredToPresentMs);
214
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700215 const int32_t presentToPresentMs = msBetween(prevTimeRecord.frameTime.presentTime,
216 timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700217 ALOGV("[%s]-[%" PRIu64 "]-present2present[%d]", layerName.c_str(),
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700218 timeRecords[0].frameTime.frameNumber, presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700219 timeStatsLayer.deltas["present2present"].insert(presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700220 }
221 prevTimeRecord = timeRecords[0];
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700222 timeRecords.pop_front();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700223 layerRecord.waitData--;
224 }
225}
226
Yiwei Zhangbd408322018-10-15 18:31:53 -0700227// This regular expression captures the following layer names for instance:
228// 1) StatusBat#0
229// 2) NavigationBar#1
230// 3) co(m).*#0
231// 4) SurfaceView - co(m).*#0
232// Using [-\\s\t]+ for the conjunction part between SurfaceView and co(m).*
233// is a bit more robust in case there's a slight change.
234// The layer name would only consist of . / $ _ 0-9 a-z A-Z in most cases.
235static const std::regex layerNameRegex(
236 "(((SurfaceView[-\\s\\t]+)?com?\\.[./$\\w]+)|((Status|Navigation)Bar))#\\d+");
237
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700238static bool layerNameIsValid(const std::string& layerName) {
Yiwei Zhangbd408322018-10-15 18:31:53 -0700239 return std::regex_match(layerName.begin(), layerName.end(), layerNameRegex);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700240}
241
242void TimeStats::setPostTime(const std::string& layerName, uint64_t frameNumber, nsecs_t postTime) {
243 if (!mEnabled.load()) return;
244
245 ATRACE_CALL();
246 ALOGV("[%s]-[%" PRIu64 "]-PostTime[%" PRId64 "]", layerName.c_str(), frameNumber, postTime);
247
248 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700249 if (!mTimeStatsTracker.count(layerName) && !layerNameIsValid(layerName)) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700250 return;
251 }
Yiwei Zhangdc224042018-10-18 15:34:00 -0700252 LayerRecord& layerRecord = mTimeStatsTracker[layerName];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700253 if (layerRecord.timeRecords.size() == MAX_NUM_TIME_RECORDS) {
254 ALOGV("[%s]-timeRecords is already at its maximum size[%zu]", layerName.c_str(),
255 MAX_NUM_TIME_RECORDS);
256 // TODO(zzyiwei): if this happens, there must be a present fence missing
257 // or waitData is not in the correct position. Need to think out a
258 // reasonable way to recover from this state.
259 return;
260 }
261 // For most media content, the acquireFence is invalid because the buffer is
262 // ready at the queueBuffer stage. In this case, acquireTime should be given
263 // a default value as postTime.
264 TimeRecord timeRecord = {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700265 .frameTime =
266 {
267 .frameNumber = frameNumber,
268 .postTime = postTime,
269 .acquireTime = postTime,
270 },
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700271 };
272 layerRecord.timeRecords.push_back(timeRecord);
273 if (layerRecord.waitData < 0 ||
274 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
275 layerRecord.waitData = layerRecord.timeRecords.size() - 1;
276}
277
278void TimeStats::setLatchTime(const std::string& layerName, uint64_t frameNumber,
279 nsecs_t latchTime) {
280 if (!mEnabled.load()) return;
281
282 ATRACE_CALL();
283 ALOGV("[%s]-[%" PRIu64 "]-LatchTime[%" PRId64 "]", layerName.c_str(), frameNumber, latchTime);
284
285 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700286 if (!mTimeStatsTracker.count(layerName)) return;
287 LayerRecord& layerRecord = mTimeStatsTracker[layerName];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700288 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700289 if (timeRecord.frameTime.frameNumber == frameNumber) {
290 timeRecord.frameTime.latchTime = latchTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700291 }
292}
293
294void TimeStats::setDesiredTime(const std::string& layerName, uint64_t frameNumber,
295 nsecs_t desiredTime) {
296 if (!mEnabled.load()) return;
297
298 ATRACE_CALL();
299 ALOGV("[%s]-[%" PRIu64 "]-DesiredTime[%" PRId64 "]", layerName.c_str(), frameNumber,
300 desiredTime);
301
302 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700303 if (!mTimeStatsTracker.count(layerName)) return;
304 LayerRecord& layerRecord = mTimeStatsTracker[layerName];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700305 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700306 if (timeRecord.frameTime.frameNumber == frameNumber) {
307 timeRecord.frameTime.desiredTime = desiredTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700308 }
309}
310
311void TimeStats::setAcquireTime(const std::string& layerName, uint64_t frameNumber,
312 nsecs_t acquireTime) {
313 if (!mEnabled.load()) return;
314
315 ATRACE_CALL();
316 ALOGV("[%s]-[%" PRIu64 "]-AcquireTime[%" PRId64 "]", layerName.c_str(), frameNumber,
317 acquireTime);
318
319 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700320 if (!mTimeStatsTracker.count(layerName)) return;
321 LayerRecord& layerRecord = mTimeStatsTracker[layerName];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700322 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700323 if (timeRecord.frameTime.frameNumber == frameNumber) {
324 timeRecord.frameTime.acquireTime = acquireTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700325 }
326}
327
328void TimeStats::setAcquireFence(const std::string& layerName, uint64_t frameNumber,
329 const std::shared_ptr<FenceTime>& acquireFence) {
330 if (!mEnabled.load()) return;
331
332 ATRACE_CALL();
333 ALOGV("[%s]-[%" PRIu64 "]-AcquireFenceTime[%" PRId64 "]", layerName.c_str(), frameNumber,
334 acquireFence->getSignalTime());
335
336 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700337 if (!mTimeStatsTracker.count(layerName)) return;
338 LayerRecord& layerRecord = mTimeStatsTracker[layerName];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700339 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700340 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700341 timeRecord.acquireFence = acquireFence;
342 }
343}
344
345void TimeStats::setPresentTime(const std::string& layerName, uint64_t frameNumber,
346 nsecs_t presentTime) {
347 if (!mEnabled.load()) return;
348
349 ATRACE_CALL();
350 ALOGV("[%s]-[%" PRIu64 "]-PresentTime[%" PRId64 "]", layerName.c_str(), frameNumber,
351 presentTime);
352
353 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700354 if (!mTimeStatsTracker.count(layerName)) return;
355 LayerRecord& layerRecord = mTimeStatsTracker[layerName];
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
363 flushAvailableRecordsToStatsLocked(layerName);
364}
365
366void TimeStats::setPresentFence(const std::string& layerName, uint64_t frameNumber,
367 const std::shared_ptr<FenceTime>& presentFence) {
368 if (!mEnabled.load()) return;
369
370 ATRACE_CALL();
371 ALOGV("[%s]-[%" PRIu64 "]-PresentFenceTime[%" PRId64 "]", layerName.c_str(), frameNumber,
372 presentFence->getSignalTime());
373
374 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700375 if (!mTimeStatsTracker.count(layerName)) return;
376 LayerRecord& layerRecord = mTimeStatsTracker[layerName];
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
384 flushAvailableRecordsToStatsLocked(layerName);
385}
386
387void TimeStats::onDisconnect(const std::string& layerName) {
388 if (!mEnabled.load()) return;
389
390 ATRACE_CALL();
391 ALOGV("[%s]-onDisconnect", layerName.c_str());
392
393 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700394 if (!mTimeStatsTracker.count(layerName)) return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700395 flushAvailableRecordsToStatsLocked(layerName);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700396 mTimeStatsTracker.erase(layerName);
397}
398
399void TimeStats::onDestroy(const std::string& layerName) {
400 if (!mEnabled.load()) return;
401
402 ATRACE_CALL();
403 ALOGV("[%s]-onDestroy", layerName.c_str());
404
405 std::lock_guard<std::mutex> lock(mMutex);
406 if (!mTimeStatsTracker.count(layerName)) return;
407 flushAvailableRecordsToStatsLocked(layerName);
408 mTimeStatsTracker.erase(layerName);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700409}
410
411void TimeStats::clearLayerRecord(const std::string& layerName) {
412 if (!mEnabled.load()) return;
413
414 ATRACE_CALL();
415 ALOGV("[%s]-clearLayerRecord", layerName.c_str());
416
417 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700418 if (!mTimeStatsTracker.count(layerName)) return;
419 LayerRecord& layerRecord = mTimeStatsTracker[layerName];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700420 layerRecord.timeRecords.clear();
421 layerRecord.prevTimeRecord.ready = false;
422 layerRecord.waitData = -1;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700423 layerRecord.droppedFrames = 0;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700424}
425
426void TimeStats::removeTimeRecord(const std::string& layerName, uint64_t frameNumber) {
427 if (!mEnabled.load()) return;
428
429 ATRACE_CALL();
430 ALOGV("[%s]-[%" PRIu64 "]-removeTimeRecord", layerName.c_str(), frameNumber);
431
432 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700433 if (!mTimeStatsTracker.count(layerName)) return;
434 LayerRecord& layerRecord = mTimeStatsTracker[layerName];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700435 size_t removeAt = 0;
436 for (const TimeRecord& record : layerRecord.timeRecords) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700437 if (record.frameTime.frameNumber == frameNumber) break;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700438 removeAt++;
439 }
440 if (removeAt == layerRecord.timeRecords.size()) return;
441 layerRecord.timeRecords.erase(layerRecord.timeRecords.begin() + removeAt);
442 if (layerRecord.waitData > static_cast<int32_t>(removeAt)) {
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700443 layerRecord.waitData--;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700444 }
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700445 layerRecord.droppedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700446}
447
448void TimeStats::enable() {
449 if (mEnabled.load()) return;
450
451 ATRACE_CALL();
452
453 std::lock_guard<std::mutex> lock(mMutex);
454 ALOGD("Enabled");
455 mEnabled.store(true);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700456 mTimeStats.statsStart = static_cast<int64_t>(std::time(0));
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700457}
458
459void TimeStats::disable() {
460 if (!mEnabled.load()) return;
461
462 ATRACE_CALL();
463
464 std::lock_guard<std::mutex> lock(mMutex);
465 ALOGD("Disabled");
466 mEnabled.store(false);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700467 mTimeStats.statsEnd = static_cast<int64_t>(std::time(0));
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700468}
469
470void TimeStats::clear() {
471 ATRACE_CALL();
472
473 std::lock_guard<std::mutex> lock(mMutex);
474 ALOGD("Cleared");
Yiwei Zhangdc224042018-10-18 15:34:00 -0700475 mTimeStats.stats.clear();
476 mTimeStats.statsStart = (mEnabled.load() ? static_cast<int64_t>(std::time(0)) : 0);
477 mTimeStats.statsEnd = 0;
478 mTimeStats.totalFrames = 0;
479 mTimeStats.missedFrames = 0;
480 mTimeStats.clientCompositionFrames = 0;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700481}
482
483bool TimeStats::isEnabled() {
484 return mEnabled.load();
485}
486
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700487void TimeStats::dump(bool asProto, std::optional<uint32_t> maxLayers, String8& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700488 ATRACE_CALL();
489
490 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700491 if (mTimeStats.statsStart == 0) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700492 return;
493 }
494
Yiwei Zhangdc224042018-10-18 15:34:00 -0700495 mTimeStats.statsEnd = static_cast<int64_t>(std::time(0));
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700496
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700497 if (asProto) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700498 ALOGD("Dumping TimeStats as proto");
Yiwei Zhangdc224042018-10-18 15:34:00 -0700499 SFTimeStatsGlobalProto timeStatsProto = mTimeStats.toProto(maxLayers);
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700500 result.append(timeStatsProto.SerializeAsString().c_str(), timeStatsProto.ByteSize());
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700501 } else {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700502 ALOGD("Dumping TimeStats as text");
Yiwei Zhangdc224042018-10-18 15:34:00 -0700503 result.append(mTimeStats.toString(maxLayers).c_str());
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700504 result.append("\n");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700505 }
506}
507
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700508} // namespace android