blob: 0bc1c0a452781230f1ef1e030db62226a7f0414e [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 Zhang8a4015c2018-05-08 16:03:47 -0700148static std::string getPackageName(const std::string& layerName) {
149 // This regular expression captures the following for instance:
150 // StatusBar in StatusBar#0
151 // com.appname in com.appname/com.appname.activity#0
152 // com.appname in SurfaceView - com.appname/com.appname.activity#0
153 const std::regex re("(?:SurfaceView[-\\s\\t]+)?([^/]+).*#\\d+");
154 std::smatch match;
155 if (std::regex_match(layerName.begin(), layerName.end(), match, re)) {
156 // There must be a match for group 1 otherwise the whole string is not
157 // matched and the above will return false
158 return match[1];
159 }
160 return "";
161}
162
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700163void TimeStats::flushAvailableRecordsToStatsLocked(const std::string& layerName) {
164 ATRACE_CALL();
165
166 LayerRecord& layerRecord = timeStatsTracker[layerName];
167 TimeRecord& prevTimeRecord = layerRecord.prevTimeRecord;
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700168 std::deque<TimeRecord>& timeRecords = layerRecord.timeRecords;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700169 while (!timeRecords.empty()) {
170 if (!recordReadyLocked(layerName, &timeRecords[0])) break;
171 ALOGV("[%s]-[%" PRIu64 "]-presentFenceTime[%" PRId64 "]", layerName.c_str(),
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700172 timeRecords[0].frameTime.frameNumber, timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700173
174 if (prevTimeRecord.ready) {
175 if (!timeStats.stats.count(layerName)) {
176 timeStats.stats[layerName].layerName = layerName;
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700177 timeStats.stats[layerName].packageName = getPackageName(layerName);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700178 }
179 TimeStatsHelper::TimeStatsLayer& timeStatsLayer = timeStats.stats[layerName];
180 timeStatsLayer.totalFrames++;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700181 timeStatsLayer.droppedFrames += layerRecord.droppedFrames;
182 layerRecord.droppedFrames = 0;
183
184 const int32_t postToAcquireMs = msBetween(timeRecords[0].frameTime.postTime,
185 timeRecords[0].frameTime.acquireTime);
186 ALOGV("[%s]-[%" PRIu64 "]-post2acquire[%d]", layerName.c_str(),
187 timeRecords[0].frameTime.frameNumber, postToAcquireMs);
188 timeStatsLayer.deltas["post2acquire"].insert(postToAcquireMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700189
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700190 const int32_t postToPresentMs = msBetween(timeRecords[0].frameTime.postTime,
191 timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700192 ALOGV("[%s]-[%" PRIu64 "]-post2present[%d]", layerName.c_str(),
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700193 timeRecords[0].frameTime.frameNumber, postToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700194 timeStatsLayer.deltas["post2present"].insert(postToPresentMs);
195
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700196 const int32_t acquireToPresentMs = msBetween(timeRecords[0].frameTime.acquireTime,
197 timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700198 ALOGV("[%s]-[%" PRIu64 "]-acquire2present[%d]", layerName.c_str(),
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700199 timeRecords[0].frameTime.frameNumber, acquireToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700200 timeStatsLayer.deltas["acquire2present"].insert(acquireToPresentMs);
201
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700202 const int32_t latchToPresentMs = msBetween(timeRecords[0].frameTime.latchTime,
203 timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700204 ALOGV("[%s]-[%" PRIu64 "]-latch2present[%d]", layerName.c_str(),
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700205 timeRecords[0].frameTime.frameNumber, latchToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700206 timeStatsLayer.deltas["latch2present"].insert(latchToPresentMs);
207
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700208 const int32_t desiredToPresentMs = msBetween(timeRecords[0].frameTime.desiredTime,
209 timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700210 ALOGV("[%s]-[%" PRIu64 "]-desired2present[%d]", layerName.c_str(),
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700211 timeRecords[0].frameTime.frameNumber, desiredToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700212 timeStatsLayer.deltas["desired2present"].insert(desiredToPresentMs);
213
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700214 const int32_t presentToPresentMs = msBetween(prevTimeRecord.frameTime.presentTime,
215 timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700216 ALOGV("[%s]-[%" PRIu64 "]-present2present[%d]", layerName.c_str(),
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700217 timeRecords[0].frameTime.frameNumber, presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700218 timeStatsLayer.deltas["present2present"].insert(presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700219 }
220 prevTimeRecord = timeRecords[0];
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700221 timeRecords.pop_front();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700222 layerRecord.waitData--;
223 }
224}
225
226static bool layerNameIsValid(const std::string& layerName) {
227 // This regular expression captures the following layer names for instance:
228 // 1) StatusBat#0
229 // 2) NavigationBar#1
Yiwei Zhangf5d89a02018-05-14 13:16:31 -0700230 // 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.
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700234 // The layer name would only consist of . / $ _ 0-9 a-z A-Z in most cases.
Yiwei Zhangf5d89a02018-05-14 13:16:31 -0700235 std::regex re("(((SurfaceView[-\\s\\t]+)?com?\\.[./$\\w]+)|((Status|Navigation)Bar))#\\d+");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700236 return std::regex_match(layerName.begin(), layerName.end(), re);
237}
238
239void TimeStats::setPostTime(const std::string& layerName, uint64_t frameNumber, nsecs_t postTime) {
240 if (!mEnabled.load()) return;
241
242 ATRACE_CALL();
243 ALOGV("[%s]-[%" PRIu64 "]-PostTime[%" PRId64 "]", layerName.c_str(), frameNumber, postTime);
244
245 std::lock_guard<std::mutex> lock(mMutex);
246 if (!timeStatsTracker.count(layerName) && !layerNameIsValid(layerName)) {
247 return;
248 }
249 LayerRecord& layerRecord = timeStatsTracker[layerName];
250 if (layerRecord.timeRecords.size() == MAX_NUM_TIME_RECORDS) {
251 ALOGV("[%s]-timeRecords is already at its maximum size[%zu]", layerName.c_str(),
252 MAX_NUM_TIME_RECORDS);
253 // TODO(zzyiwei): if this happens, there must be a present fence missing
254 // or waitData is not in the correct position. Need to think out a
255 // reasonable way to recover from this state.
256 return;
257 }
258 // For most media content, the acquireFence is invalid because the buffer is
259 // ready at the queueBuffer stage. In this case, acquireTime should be given
260 // a default value as postTime.
261 TimeRecord timeRecord = {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700262 .frameTime =
263 {
264 .frameNumber = frameNumber,
265 .postTime = postTime,
266 .acquireTime = postTime,
267 },
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700268 };
269 layerRecord.timeRecords.push_back(timeRecord);
270 if (layerRecord.waitData < 0 ||
271 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
272 layerRecord.waitData = layerRecord.timeRecords.size() - 1;
273}
274
275void TimeStats::setLatchTime(const std::string& layerName, uint64_t frameNumber,
276 nsecs_t latchTime) {
277 if (!mEnabled.load()) return;
278
279 ATRACE_CALL();
280 ALOGV("[%s]-[%" PRIu64 "]-LatchTime[%" PRId64 "]", layerName.c_str(), frameNumber, latchTime);
281
282 std::lock_guard<std::mutex> lock(mMutex);
283 if (!timeStatsTracker.count(layerName)) return;
284 LayerRecord& layerRecord = timeStatsTracker[layerName];
285 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700286 if (timeRecord.frameTime.frameNumber == frameNumber) {
287 timeRecord.frameTime.latchTime = latchTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700288 }
289}
290
291void TimeStats::setDesiredTime(const std::string& layerName, uint64_t frameNumber,
292 nsecs_t desiredTime) {
293 if (!mEnabled.load()) return;
294
295 ATRACE_CALL();
296 ALOGV("[%s]-[%" PRIu64 "]-DesiredTime[%" PRId64 "]", layerName.c_str(), frameNumber,
297 desiredTime);
298
299 std::lock_guard<std::mutex> lock(mMutex);
300 if (!timeStatsTracker.count(layerName)) return;
301 LayerRecord& layerRecord = timeStatsTracker[layerName];
302 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700303 if (timeRecord.frameTime.frameNumber == frameNumber) {
304 timeRecord.frameTime.desiredTime = desiredTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700305 }
306}
307
308void TimeStats::setAcquireTime(const std::string& layerName, uint64_t frameNumber,
309 nsecs_t acquireTime) {
310 if (!mEnabled.load()) return;
311
312 ATRACE_CALL();
313 ALOGV("[%s]-[%" PRIu64 "]-AcquireTime[%" PRId64 "]", layerName.c_str(), frameNumber,
314 acquireTime);
315
316 std::lock_guard<std::mutex> lock(mMutex);
317 if (!timeStatsTracker.count(layerName)) return;
318 LayerRecord& layerRecord = timeStatsTracker[layerName];
319 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700320 if (timeRecord.frameTime.frameNumber == frameNumber) {
321 timeRecord.frameTime.acquireTime = acquireTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700322 }
323}
324
325void TimeStats::setAcquireFence(const std::string& layerName, uint64_t frameNumber,
326 const std::shared_ptr<FenceTime>& acquireFence) {
327 if (!mEnabled.load()) return;
328
329 ATRACE_CALL();
330 ALOGV("[%s]-[%" PRIu64 "]-AcquireFenceTime[%" PRId64 "]", layerName.c_str(), frameNumber,
331 acquireFence->getSignalTime());
332
333 std::lock_guard<std::mutex> lock(mMutex);
334 if (!timeStatsTracker.count(layerName)) return;
335 LayerRecord& layerRecord = timeStatsTracker[layerName];
336 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700337 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700338 timeRecord.acquireFence = acquireFence;
339 }
340}
341
342void TimeStats::setPresentTime(const std::string& layerName, uint64_t frameNumber,
343 nsecs_t presentTime) {
344 if (!mEnabled.load()) return;
345
346 ATRACE_CALL();
347 ALOGV("[%s]-[%" PRIu64 "]-PresentTime[%" PRId64 "]", layerName.c_str(), frameNumber,
348 presentTime);
349
350 std::lock_guard<std::mutex> lock(mMutex);
351 if (!timeStatsTracker.count(layerName)) return;
352 LayerRecord& layerRecord = timeStatsTracker[layerName];
353 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700354 if (timeRecord.frameTime.frameNumber == frameNumber) {
355 timeRecord.frameTime.presentTime = presentTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700356 timeRecord.ready = true;
357 layerRecord.waitData++;
358 }
359
360 flushAvailableRecordsToStatsLocked(layerName);
361}
362
363void TimeStats::setPresentFence(const std::string& layerName, uint64_t frameNumber,
364 const std::shared_ptr<FenceTime>& presentFence) {
365 if (!mEnabled.load()) return;
366
367 ATRACE_CALL();
368 ALOGV("[%s]-[%" PRIu64 "]-PresentFenceTime[%" PRId64 "]", layerName.c_str(), frameNumber,
369 presentFence->getSignalTime());
370
371 std::lock_guard<std::mutex> lock(mMutex);
372 if (!timeStatsTracker.count(layerName)) return;
373 LayerRecord& layerRecord = timeStatsTracker[layerName];
374 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700375 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700376 timeRecord.presentFence = presentFence;
377 timeRecord.ready = true;
378 layerRecord.waitData++;
379 }
380
381 flushAvailableRecordsToStatsLocked(layerName);
382}
383
384void TimeStats::onDisconnect(const std::string& layerName) {
385 if (!mEnabled.load()) return;
386
387 ATRACE_CALL();
388 ALOGV("[%s]-onDisconnect", layerName.c_str());
389
390 std::lock_guard<std::mutex> lock(mMutex);
391 if (!timeStatsTracker.count(layerName)) return;
392 flushAvailableRecordsToStatsLocked(layerName);
393 timeStatsTracker.erase(layerName);
394}
395
396void TimeStats::clearLayerRecord(const std::string& layerName) {
397 if (!mEnabled.load()) return;
398
399 ATRACE_CALL();
400 ALOGV("[%s]-clearLayerRecord", layerName.c_str());
401
402 std::lock_guard<std::mutex> lock(mMutex);
403 if (!timeStatsTracker.count(layerName)) return;
404 LayerRecord& layerRecord = timeStatsTracker[layerName];
405 layerRecord.timeRecords.clear();
406 layerRecord.prevTimeRecord.ready = false;
407 layerRecord.waitData = -1;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700408 layerRecord.droppedFrames = 0;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700409}
410
411void TimeStats::removeTimeRecord(const std::string& layerName, uint64_t frameNumber) {
412 if (!mEnabled.load()) return;
413
414 ATRACE_CALL();
415 ALOGV("[%s]-[%" PRIu64 "]-removeTimeRecord", layerName.c_str(), frameNumber);
416
417 std::lock_guard<std::mutex> lock(mMutex);
418 if (!timeStatsTracker.count(layerName)) return;
419 LayerRecord& layerRecord = timeStatsTracker[layerName];
420 size_t removeAt = 0;
421 for (const TimeRecord& record : layerRecord.timeRecords) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700422 if (record.frameTime.frameNumber == frameNumber) break;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700423 removeAt++;
424 }
425 if (removeAt == layerRecord.timeRecords.size()) return;
426 layerRecord.timeRecords.erase(layerRecord.timeRecords.begin() + removeAt);
427 if (layerRecord.waitData > static_cast<int32_t>(removeAt)) {
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700428 layerRecord.waitData--;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700429 }
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700430 layerRecord.droppedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700431}
432
433void TimeStats::enable() {
434 if (mEnabled.load()) return;
435
436 ATRACE_CALL();
437
438 std::lock_guard<std::mutex> lock(mMutex);
439 ALOGD("Enabled");
440 mEnabled.store(true);
441 timeStats.statsStart = static_cast<int64_t>(std::time(0));
442}
443
444void TimeStats::disable() {
445 if (!mEnabled.load()) return;
446
447 ATRACE_CALL();
448
449 std::lock_guard<std::mutex> lock(mMutex);
450 ALOGD("Disabled");
451 mEnabled.store(false);
452 timeStats.statsEnd = static_cast<int64_t>(std::time(0));
453}
454
455void TimeStats::clear() {
456 ATRACE_CALL();
457
458 std::lock_guard<std::mutex> lock(mMutex);
459 ALOGD("Cleared");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700460 timeStats.stats.clear();
461 timeStats.statsStart = (mEnabled.load() ? static_cast<int64_t>(std::time(0)) : 0);
462 timeStats.statsEnd = 0;
463 timeStats.totalFrames = 0;
464 timeStats.missedFrames = 0;
465 timeStats.clientCompositionFrames = 0;
466}
467
468bool TimeStats::isEnabled() {
469 return mEnabled.load();
470}
471
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700472void TimeStats::dump(bool asProto, std::optional<uint32_t> maxLayers, String8& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700473 ATRACE_CALL();
474
475 std::lock_guard<std::mutex> lock(mMutex);
476 if (timeStats.statsStart == 0) {
477 return;
478 }
479
480 timeStats.statsEnd = static_cast<int64_t>(std::time(0));
481
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700482 if (asProto) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700483 ALOGD("Dumping TimeStats as proto");
484 SFTimeStatsGlobalProto timeStatsProto = timeStats.toProto(maxLayers);
485 result.append(timeStatsProto.SerializeAsString().c_str(), timeStatsProto.ByteSize());
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700486 } else {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700487 ALOGD("Dumping TimeStats as text");
488 result.append(timeStats.toString(maxLayers).c_str());
489 result.append("\n");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700490 }
491}
492
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700493} // namespace android