blob: d77a32402723959c0ba134bb614894565d026a07 [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() {
35 static std::unique_ptr<TimeStats> sInstance;
36 static std::once_flag sOnceFlag;
37
38 std::call_once(sOnceFlag, [] { sInstance.reset(new TimeStats); });
39 return *sInstance.get();
40}
41
42void TimeStats::parseArgs(bool asProto, const Vector<String16>& args, size_t& index,
43 String8& result) {
44 ATRACE_CALL();
45
46 if (args.size() > index + 10) {
47 ALOGD("Invalid args count");
48 return;
49 }
50
51 std::unordered_map<std::string, int32_t> argsMap;
52 while (index < args.size()) {
53 argsMap[std::string(String8(args[index]).c_str())] = index;
54 ++index;
55 }
56
57 if (argsMap.count("-disable")) {
58 disable();
59 }
60
61 if (argsMap.count("-dump")) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070062 std::optional<uint32_t> maxLayers = std::nullopt;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070063 auto iter = argsMap.find("-maxlayers");
64 if (iter != argsMap.end() && iter->second + 1 < static_cast<int32_t>(args.size())) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070065 int64_t value = strtol(String8(args[iter->second + 1]).c_str(), nullptr, 10);
66 value = std::clamp(value, int64_t(0), int64_t(UINT32_MAX));
67 maxLayers = static_cast<uint32_t>(value);
Yiwei Zhang0102ad22018-05-02 17:37:17 -070068 }
69
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070070 dump(asProto, maxLayers, result);
Yiwei Zhang0102ad22018-05-02 17:37:17 -070071 }
72
73 if (argsMap.count("-clear")) {
74 clear();
75 }
76
77 if (argsMap.count("-enable")) {
78 enable();
79 }
80}
81
82void TimeStats::incrementTotalFrames() {
83 if (!mEnabled.load()) return;
84
85 ATRACE_CALL();
86
87 std::lock_guard<std::mutex> lock(mMutex);
88 timeStats.totalFrames++;
89}
90
Yiwei Zhang621f9d42018-05-07 10:40:55 -070091void TimeStats::incrementMissedFrames() {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070092 if (!mEnabled.load()) return;
93
94 ATRACE_CALL();
95
96 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang0102ad22018-05-02 17:37:17 -070097 timeStats.missedFrames++;
98}
99
100void TimeStats::incrementClientCompositionFrames() {
101 if (!mEnabled.load()) return;
102
103 ATRACE_CALL();
104
105 std::lock_guard<std::mutex> lock(mMutex);
106 timeStats.clientCompositionFrames++;
107}
108
109bool TimeStats::recordReadyLocked(const std::string& layerName, TimeRecord* timeRecord) {
110 if (!timeRecord->ready) {
111 ALOGV("[%s]-[%" PRIu64 "]-presentFence is still not received", layerName.c_str(),
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700112 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700113 return false;
114 }
115
116 if (timeRecord->acquireFence != nullptr) {
117 if (timeRecord->acquireFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
118 return false;
119 }
120 if (timeRecord->acquireFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700121 timeRecord->frameTime.acquireTime = timeRecord->acquireFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700122 timeRecord->acquireFence = nullptr;
123 } else {
124 ALOGV("[%s]-[%" PRIu64 "]-acquireFence signal time is invalid", layerName.c_str(),
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700125 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700126 }
127 }
128
129 if (timeRecord->presentFence != nullptr) {
130 if (timeRecord->presentFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
131 return false;
132 }
133 if (timeRecord->presentFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700134 timeRecord->frameTime.presentTime = timeRecord->presentFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700135 timeRecord->presentFence = nullptr;
136 } else {
137 ALOGV("[%s]-[%" PRIu64 "]-presentFence signal time invalid", layerName.c_str(),
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700138 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700139 }
140 }
141
142 return true;
143}
144
145static int32_t msBetween(nsecs_t start, nsecs_t end) {
146 int64_t delta = (end - start) / 1000000;
147 delta = std::clamp(delta, int64_t(INT32_MIN), int64_t(INT32_MAX));
148 return static_cast<int32_t>(delta);
149}
150
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700151static std::string getPackageName(const std::string& layerName) {
152 // This regular expression captures the following for instance:
153 // StatusBar in StatusBar#0
154 // com.appname in com.appname/com.appname.activity#0
155 // com.appname in SurfaceView - com.appname/com.appname.activity#0
156 const std::regex re("(?:SurfaceView[-\\s\\t]+)?([^/]+).*#\\d+");
157 std::smatch match;
158 if (std::regex_match(layerName.begin(), layerName.end(), match, re)) {
159 // There must be a match for group 1 otherwise the whole string is not
160 // matched and the above will return false
161 return match[1];
162 }
163 return "";
164}
165
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700166void TimeStats::flushAvailableRecordsToStatsLocked(const std::string& layerName) {
167 ATRACE_CALL();
168
169 LayerRecord& layerRecord = timeStatsTracker[layerName];
170 TimeRecord& prevTimeRecord = layerRecord.prevTimeRecord;
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700171 std::deque<TimeRecord>& timeRecords = layerRecord.timeRecords;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700172 while (!timeRecords.empty()) {
173 if (!recordReadyLocked(layerName, &timeRecords[0])) break;
174 ALOGV("[%s]-[%" PRIu64 "]-presentFenceTime[%" PRId64 "]", layerName.c_str(),
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700175 timeRecords[0].frameTime.frameNumber, timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700176
177 if (prevTimeRecord.ready) {
178 if (!timeStats.stats.count(layerName)) {
179 timeStats.stats[layerName].layerName = layerName;
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700180 timeStats.stats[layerName].packageName = getPackageName(layerName);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700181 }
182 TimeStatsHelper::TimeStatsLayer& timeStatsLayer = timeStats.stats[layerName];
183 timeStatsLayer.totalFrames++;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700184 timeStatsLayer.droppedFrames += layerRecord.droppedFrames;
185 layerRecord.droppedFrames = 0;
186
187 const int32_t postToAcquireMs = msBetween(timeRecords[0].frameTime.postTime,
188 timeRecords[0].frameTime.acquireTime);
189 ALOGV("[%s]-[%" PRIu64 "]-post2acquire[%d]", layerName.c_str(),
190 timeRecords[0].frameTime.frameNumber, postToAcquireMs);
191 timeStatsLayer.deltas["post2acquire"].insert(postToAcquireMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700192
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700193 const int32_t postToPresentMs = msBetween(timeRecords[0].frameTime.postTime,
194 timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700195 ALOGV("[%s]-[%" PRIu64 "]-post2present[%d]", layerName.c_str(),
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700196 timeRecords[0].frameTime.frameNumber, postToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700197 timeStatsLayer.deltas["post2present"].insert(postToPresentMs);
198
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700199 const int32_t acquireToPresentMs = msBetween(timeRecords[0].frameTime.acquireTime,
200 timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700201 ALOGV("[%s]-[%" PRIu64 "]-acquire2present[%d]", layerName.c_str(),
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700202 timeRecords[0].frameTime.frameNumber, acquireToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700203 timeStatsLayer.deltas["acquire2present"].insert(acquireToPresentMs);
204
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700205 const int32_t latchToPresentMs = msBetween(timeRecords[0].frameTime.latchTime,
206 timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700207 ALOGV("[%s]-[%" PRIu64 "]-latch2present[%d]", layerName.c_str(),
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700208 timeRecords[0].frameTime.frameNumber, latchToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700209 timeStatsLayer.deltas["latch2present"].insert(latchToPresentMs);
210
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700211 const int32_t desiredToPresentMs = msBetween(timeRecords[0].frameTime.desiredTime,
212 timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700213 ALOGV("[%s]-[%" PRIu64 "]-desired2present[%d]", layerName.c_str(),
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700214 timeRecords[0].frameTime.frameNumber, desiredToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700215 timeStatsLayer.deltas["desired2present"].insert(desiredToPresentMs);
216
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700217 const int32_t presentToPresentMs = msBetween(prevTimeRecord.frameTime.presentTime,
218 timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700219 ALOGV("[%s]-[%" PRIu64 "]-present2present[%d]", layerName.c_str(),
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700220 timeRecords[0].frameTime.frameNumber, presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700221 timeStatsLayer.deltas["present2present"].insert(presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700222 }
223 prevTimeRecord = timeRecords[0];
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700224 timeRecords.pop_front();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700225 layerRecord.waitData--;
226 }
227}
228
229static bool layerNameIsValid(const std::string& layerName) {
230 // This regular expression captures the following layer names for instance:
231 // 1) StatusBat#0
232 // 2) NavigationBar#1
Yiwei Zhangf5d89a02018-05-14 13:16:31 -0700233 // 3) co(m).*#0
234 // 4) SurfaceView - co(m).*#0
235 // Using [-\\s\t]+ for the conjunction part between SurfaceView and co(m).*
236 // is a bit more robust in case there's a slight change.
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700237 // The layer name would only consist of . / $ _ 0-9 a-z A-Z in most cases.
Yiwei Zhangf5d89a02018-05-14 13:16:31 -0700238 std::regex re("(((SurfaceView[-\\s\\t]+)?com?\\.[./$\\w]+)|((Status|Navigation)Bar))#\\d+");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700239 return std::regex_match(layerName.begin(), layerName.end(), re);
240}
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