blob: 2e1231bb2a170b3402991af85e5e4964a78cc036 [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);
85 timeStats.totalFrames++;
86}
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 Zhang0102ad22018-05-02 17:37:17 -070094 timeStats.missedFrames++;
95}
96
97void TimeStats::incrementClientCompositionFrames() {
98 if (!mEnabled.load()) return;
99
100 ATRACE_CALL();
101
102 std::lock_guard<std::mutex> lock(mMutex);
103 timeStats.clientCompositionFrames++;
104}
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
167 LayerRecord& layerRecord = timeStatsTracker[layerName];
168 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) {
176 if (!timeStats.stats.count(layerName)) {
177 timeStats.stats[layerName].layerName = layerName;
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700178 timeStats.stats[layerName].packageName = getPackageName(layerName);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700179 }
180 TimeStatsHelper::TimeStatsLayer& timeStatsLayer = timeStats.stats[layerName];
181 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);
249 if (!timeStatsTracker.count(layerName) && !layerNameIsValid(layerName)) {
250 return;
251 }
252 LayerRecord& layerRecord = timeStatsTracker[layerName];
253 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);
286 if (!timeStatsTracker.count(layerName)) return;
287 LayerRecord& layerRecord = timeStatsTracker[layerName];
288 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);
303 if (!timeStatsTracker.count(layerName)) return;
304 LayerRecord& layerRecord = timeStatsTracker[layerName];
305 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);
320 if (!timeStatsTracker.count(layerName)) return;
321 LayerRecord& layerRecord = timeStatsTracker[layerName];
322 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);
337 if (!timeStatsTracker.count(layerName)) return;
338 LayerRecord& layerRecord = timeStatsTracker[layerName];
339 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);
354 if (!timeStatsTracker.count(layerName)) return;
355 LayerRecord& layerRecord = timeStatsTracker[layerName];
356 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);
375 if (!timeStatsTracker.count(layerName)) return;
376 LayerRecord& layerRecord = timeStatsTracker[layerName];
377 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);
394 if (!timeStatsTracker.count(layerName)) return;
395 flushAvailableRecordsToStatsLocked(layerName);
396 timeStatsTracker.erase(layerName);
397}
398
399void TimeStats::clearLayerRecord(const std::string& layerName) {
400 if (!mEnabled.load()) return;
401
402 ATRACE_CALL();
403 ALOGV("[%s]-clearLayerRecord", layerName.c_str());
404
405 std::lock_guard<std::mutex> lock(mMutex);
406 if (!timeStatsTracker.count(layerName)) return;
407 LayerRecord& layerRecord = timeStatsTracker[layerName];
408 layerRecord.timeRecords.clear();
409 layerRecord.prevTimeRecord.ready = false;
410 layerRecord.waitData = -1;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700411 layerRecord.droppedFrames = 0;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700412}
413
414void TimeStats::removeTimeRecord(const std::string& layerName, uint64_t frameNumber) {
415 if (!mEnabled.load()) return;
416
417 ATRACE_CALL();
418 ALOGV("[%s]-[%" PRIu64 "]-removeTimeRecord", layerName.c_str(), frameNumber);
419
420 std::lock_guard<std::mutex> lock(mMutex);
421 if (!timeStatsTracker.count(layerName)) return;
422 LayerRecord& layerRecord = timeStatsTracker[layerName];
423 size_t removeAt = 0;
424 for (const TimeRecord& record : layerRecord.timeRecords) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700425 if (record.frameTime.frameNumber == frameNumber) break;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700426 removeAt++;
427 }
428 if (removeAt == layerRecord.timeRecords.size()) return;
429 layerRecord.timeRecords.erase(layerRecord.timeRecords.begin() + removeAt);
430 if (layerRecord.waitData > static_cast<int32_t>(removeAt)) {
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700431 layerRecord.waitData--;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700432 }
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700433 layerRecord.droppedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700434}
435
436void TimeStats::enable() {
437 if (mEnabled.load()) return;
438
439 ATRACE_CALL();
440
441 std::lock_guard<std::mutex> lock(mMutex);
442 ALOGD("Enabled");
443 mEnabled.store(true);
444 timeStats.statsStart = static_cast<int64_t>(std::time(0));
445}
446
447void TimeStats::disable() {
448 if (!mEnabled.load()) return;
449
450 ATRACE_CALL();
451
452 std::lock_guard<std::mutex> lock(mMutex);
453 ALOGD("Disabled");
454 mEnabled.store(false);
455 timeStats.statsEnd = static_cast<int64_t>(std::time(0));
456}
457
458void TimeStats::clear() {
459 ATRACE_CALL();
460
461 std::lock_guard<std::mutex> lock(mMutex);
462 ALOGD("Cleared");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700463 timeStats.stats.clear();
464 timeStats.statsStart = (mEnabled.load() ? static_cast<int64_t>(std::time(0)) : 0);
465 timeStats.statsEnd = 0;
466 timeStats.totalFrames = 0;
467 timeStats.missedFrames = 0;
468 timeStats.clientCompositionFrames = 0;
469}
470
471bool TimeStats::isEnabled() {
472 return mEnabled.load();
473}
474
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700475void TimeStats::dump(bool asProto, std::optional<uint32_t> maxLayers, String8& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700476 ATRACE_CALL();
477
478 std::lock_guard<std::mutex> lock(mMutex);
479 if (timeStats.statsStart == 0) {
480 return;
481 }
482
483 timeStats.statsEnd = static_cast<int64_t>(std::time(0));
484
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700485 if (asProto) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700486 ALOGD("Dumping TimeStats as proto");
487 SFTimeStatsGlobalProto timeStatsProto = timeStats.toProto(maxLayers);
488 result.append(timeStatsProto.SerializeAsString().c_str(), timeStatsProto.ByteSize());
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700489 } else {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700490 ALOGD("Dumping TimeStats as text");
491 result.append(timeStats.toString(maxLayers).c_str());
492 result.append("\n");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700493 }
494}
495
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700496} // namespace android