blob: d7a288693e0b1bc962202427073e9bb34aa74127 [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 timeStats.stats[layerName].statsStart = static_cast<int64_t>(std::time(0));
182 }
183 TimeStatsHelper::TimeStatsLayer& timeStatsLayer = timeStats.stats[layerName];
184 timeStatsLayer.totalFrames++;
185
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700186 const int32_t postToPresentMs = msBetween(timeRecords[0].frameTime.postTime,
187 timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700188 ALOGV("[%s]-[%" PRIu64 "]-post2present[%d]", layerName.c_str(),
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700189 timeRecords[0].frameTime.frameNumber, postToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700190 timeStatsLayer.deltas["post2present"].insert(postToPresentMs);
191
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700192 const int32_t acquireToPresentMs = msBetween(timeRecords[0].frameTime.acquireTime,
193 timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700194 ALOGV("[%s]-[%" PRIu64 "]-acquire2present[%d]", layerName.c_str(),
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700195 timeRecords[0].frameTime.frameNumber, acquireToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700196 timeStatsLayer.deltas["acquire2present"].insert(acquireToPresentMs);
197
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700198 const int32_t latchToPresentMs = msBetween(timeRecords[0].frameTime.latchTime,
199 timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700200 ALOGV("[%s]-[%" PRIu64 "]-latch2present[%d]", layerName.c_str(),
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700201 timeRecords[0].frameTime.frameNumber, latchToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700202 timeStatsLayer.deltas["latch2present"].insert(latchToPresentMs);
203
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700204 const int32_t desiredToPresentMs = msBetween(timeRecords[0].frameTime.desiredTime,
205 timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700206 ALOGV("[%s]-[%" PRIu64 "]-desired2present[%d]", layerName.c_str(),
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700207 timeRecords[0].frameTime.frameNumber, desiredToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700208 timeStatsLayer.deltas["desired2present"].insert(desiredToPresentMs);
209
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700210 const int32_t presentToPresentMs = msBetween(prevTimeRecord.frameTime.presentTime,
211 timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700212 ALOGV("[%s]-[%" PRIu64 "]-present2present[%d]", layerName.c_str(),
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700213 timeRecords[0].frameTime.frameNumber, presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700214 timeStatsLayer.deltas["present2present"].insert(presentToPresentMs);
215
216 timeStats.stats[layerName].statsEnd = static_cast<int64_t>(std::time(0));
217 }
218 prevTimeRecord = timeRecords[0];
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700219 timeRecords.pop_front();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700220 layerRecord.waitData--;
221 }
222}
223
224static bool layerNameIsValid(const std::string& layerName) {
225 // This regular expression captures the following layer names for instance:
226 // 1) StatusBat#0
227 // 2) NavigationBar#1
Yiwei Zhangf5d89a02018-05-14 13:16:31 -0700228 // 3) co(m).*#0
229 // 4) SurfaceView - co(m).*#0
230 // Using [-\\s\t]+ for the conjunction part between SurfaceView and co(m).*
231 // is a bit more robust in case there's a slight change.
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700232 // The layer name would only consist of . / $ _ 0-9 a-z A-Z in most cases.
Yiwei Zhangf5d89a02018-05-14 13:16:31 -0700233 std::regex re("(((SurfaceView[-\\s\\t]+)?com?\\.[./$\\w]+)|((Status|Navigation)Bar))#\\d+");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700234 return std::regex_match(layerName.begin(), layerName.end(), re);
235}
236
237void TimeStats::setPostTime(const std::string& layerName, uint64_t frameNumber, nsecs_t postTime) {
238 if (!mEnabled.load()) return;
239
240 ATRACE_CALL();
241 ALOGV("[%s]-[%" PRIu64 "]-PostTime[%" PRId64 "]", layerName.c_str(), frameNumber, postTime);
242
243 std::lock_guard<std::mutex> lock(mMutex);
244 if (!timeStatsTracker.count(layerName) && !layerNameIsValid(layerName)) {
245 return;
246 }
247 LayerRecord& layerRecord = timeStatsTracker[layerName];
248 if (layerRecord.timeRecords.size() == MAX_NUM_TIME_RECORDS) {
249 ALOGV("[%s]-timeRecords is already at its maximum size[%zu]", layerName.c_str(),
250 MAX_NUM_TIME_RECORDS);
251 // TODO(zzyiwei): if this happens, there must be a present fence missing
252 // or waitData is not in the correct position. Need to think out a
253 // reasonable way to recover from this state.
254 return;
255 }
256 // For most media content, the acquireFence is invalid because the buffer is
257 // ready at the queueBuffer stage. In this case, acquireTime should be given
258 // a default value as postTime.
259 TimeRecord timeRecord = {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700260 .frameTime =
261 {
262 .frameNumber = frameNumber,
263 .postTime = postTime,
264 .acquireTime = postTime,
265 },
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700266 };
267 layerRecord.timeRecords.push_back(timeRecord);
268 if (layerRecord.waitData < 0 ||
269 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
270 layerRecord.waitData = layerRecord.timeRecords.size() - 1;
271}
272
273void TimeStats::setLatchTime(const std::string& layerName, uint64_t frameNumber,
274 nsecs_t latchTime) {
275 if (!mEnabled.load()) return;
276
277 ATRACE_CALL();
278 ALOGV("[%s]-[%" PRIu64 "]-LatchTime[%" PRId64 "]", layerName.c_str(), frameNumber, latchTime);
279
280 std::lock_guard<std::mutex> lock(mMutex);
281 if (!timeStatsTracker.count(layerName)) return;
282 LayerRecord& layerRecord = timeStatsTracker[layerName];
283 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700284 if (timeRecord.frameTime.frameNumber == frameNumber) {
285 timeRecord.frameTime.latchTime = latchTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700286 }
287}
288
289void TimeStats::setDesiredTime(const std::string& layerName, uint64_t frameNumber,
290 nsecs_t desiredTime) {
291 if (!mEnabled.load()) return;
292
293 ATRACE_CALL();
294 ALOGV("[%s]-[%" PRIu64 "]-DesiredTime[%" PRId64 "]", layerName.c_str(), frameNumber,
295 desiredTime);
296
297 std::lock_guard<std::mutex> lock(mMutex);
298 if (!timeStatsTracker.count(layerName)) return;
299 LayerRecord& layerRecord = timeStatsTracker[layerName];
300 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700301 if (timeRecord.frameTime.frameNumber == frameNumber) {
302 timeRecord.frameTime.desiredTime = desiredTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700303 }
304}
305
306void TimeStats::setAcquireTime(const std::string& layerName, uint64_t frameNumber,
307 nsecs_t acquireTime) {
308 if (!mEnabled.load()) return;
309
310 ATRACE_CALL();
311 ALOGV("[%s]-[%" PRIu64 "]-AcquireTime[%" PRId64 "]", layerName.c_str(), frameNumber,
312 acquireTime);
313
314 std::lock_guard<std::mutex> lock(mMutex);
315 if (!timeStatsTracker.count(layerName)) return;
316 LayerRecord& layerRecord = timeStatsTracker[layerName];
317 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700318 if (timeRecord.frameTime.frameNumber == frameNumber) {
319 timeRecord.frameTime.acquireTime = acquireTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700320 }
321}
322
323void TimeStats::setAcquireFence(const std::string& layerName, uint64_t frameNumber,
324 const std::shared_ptr<FenceTime>& acquireFence) {
325 if (!mEnabled.load()) return;
326
327 ATRACE_CALL();
328 ALOGV("[%s]-[%" PRIu64 "]-AcquireFenceTime[%" PRId64 "]", layerName.c_str(), frameNumber,
329 acquireFence->getSignalTime());
330
331 std::lock_guard<std::mutex> lock(mMutex);
332 if (!timeStatsTracker.count(layerName)) return;
333 LayerRecord& layerRecord = timeStatsTracker[layerName];
334 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700335 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700336 timeRecord.acquireFence = acquireFence;
337 }
338}
339
340void TimeStats::setPresentTime(const std::string& layerName, uint64_t frameNumber,
341 nsecs_t presentTime) {
342 if (!mEnabled.load()) return;
343
344 ATRACE_CALL();
345 ALOGV("[%s]-[%" PRIu64 "]-PresentTime[%" PRId64 "]", layerName.c_str(), frameNumber,
346 presentTime);
347
348 std::lock_guard<std::mutex> lock(mMutex);
349 if (!timeStatsTracker.count(layerName)) return;
350 LayerRecord& layerRecord = timeStatsTracker[layerName];
351 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700352 if (timeRecord.frameTime.frameNumber == frameNumber) {
353 timeRecord.frameTime.presentTime = presentTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700354 timeRecord.ready = true;
355 layerRecord.waitData++;
356 }
357
358 flushAvailableRecordsToStatsLocked(layerName);
359}
360
361void TimeStats::setPresentFence(const std::string& layerName, uint64_t frameNumber,
362 const std::shared_ptr<FenceTime>& presentFence) {
363 if (!mEnabled.load()) return;
364
365 ATRACE_CALL();
366 ALOGV("[%s]-[%" PRIu64 "]-PresentFenceTime[%" PRId64 "]", layerName.c_str(), frameNumber,
367 presentFence->getSignalTime());
368
369 std::lock_guard<std::mutex> lock(mMutex);
370 if (!timeStatsTracker.count(layerName)) return;
371 LayerRecord& layerRecord = timeStatsTracker[layerName];
372 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700373 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700374 timeRecord.presentFence = presentFence;
375 timeRecord.ready = true;
376 layerRecord.waitData++;
377 }
378
379 flushAvailableRecordsToStatsLocked(layerName);
380}
381
382void TimeStats::onDisconnect(const std::string& layerName) {
383 if (!mEnabled.load()) return;
384
385 ATRACE_CALL();
386 ALOGV("[%s]-onDisconnect", layerName.c_str());
387
388 std::lock_guard<std::mutex> lock(mMutex);
389 if (!timeStatsTracker.count(layerName)) return;
390 flushAvailableRecordsToStatsLocked(layerName);
391 timeStatsTracker.erase(layerName);
392}
393
394void TimeStats::clearLayerRecord(const std::string& layerName) {
395 if (!mEnabled.load()) return;
396
397 ATRACE_CALL();
398 ALOGV("[%s]-clearLayerRecord", layerName.c_str());
399
400 std::lock_guard<std::mutex> lock(mMutex);
401 if (!timeStatsTracker.count(layerName)) return;
402 LayerRecord& layerRecord = timeStatsTracker[layerName];
403 layerRecord.timeRecords.clear();
404 layerRecord.prevTimeRecord.ready = false;
405 layerRecord.waitData = -1;
406}
407
408void TimeStats::removeTimeRecord(const std::string& layerName, uint64_t frameNumber) {
409 if (!mEnabled.load()) return;
410
411 ATRACE_CALL();
412 ALOGV("[%s]-[%" PRIu64 "]-removeTimeRecord", layerName.c_str(), frameNumber);
413
414 std::lock_guard<std::mutex> lock(mMutex);
415 if (!timeStatsTracker.count(layerName)) return;
416 LayerRecord& layerRecord = timeStatsTracker[layerName];
417 size_t removeAt = 0;
418 for (const TimeRecord& record : layerRecord.timeRecords) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700419 if (record.frameTime.frameNumber == frameNumber) break;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700420 removeAt++;
421 }
422 if (removeAt == layerRecord.timeRecords.size()) return;
423 layerRecord.timeRecords.erase(layerRecord.timeRecords.begin() + removeAt);
424 if (layerRecord.waitData > static_cast<int32_t>(removeAt)) {
425 --layerRecord.waitData;
426 }
427}
428
429void TimeStats::enable() {
430 if (mEnabled.load()) return;
431
432 ATRACE_CALL();
433
434 std::lock_guard<std::mutex> lock(mMutex);
435 ALOGD("Enabled");
436 mEnabled.store(true);
437 timeStats.statsStart = static_cast<int64_t>(std::time(0));
438}
439
440void TimeStats::disable() {
441 if (!mEnabled.load()) return;
442
443 ATRACE_CALL();
444
445 std::lock_guard<std::mutex> lock(mMutex);
446 ALOGD("Disabled");
447 mEnabled.store(false);
448 timeStats.statsEnd = static_cast<int64_t>(std::time(0));
449}
450
451void TimeStats::clear() {
452 ATRACE_CALL();
453
454 std::lock_guard<std::mutex> lock(mMutex);
455 ALOGD("Cleared");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700456 timeStats.stats.clear();
457 timeStats.statsStart = (mEnabled.load() ? static_cast<int64_t>(std::time(0)) : 0);
458 timeStats.statsEnd = 0;
459 timeStats.totalFrames = 0;
460 timeStats.missedFrames = 0;
461 timeStats.clientCompositionFrames = 0;
462}
463
464bool TimeStats::isEnabled() {
465 return mEnabled.load();
466}
467
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700468void TimeStats::dump(bool asProto, std::optional<uint32_t> maxLayers, String8& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700469 ATRACE_CALL();
470
471 std::lock_guard<std::mutex> lock(mMutex);
472 if (timeStats.statsStart == 0) {
473 return;
474 }
475
476 timeStats.statsEnd = static_cast<int64_t>(std::time(0));
477
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700478 if (asProto) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700479 ALOGD("Dumping TimeStats as proto");
480 SFTimeStatsGlobalProto timeStatsProto = timeStats.toProto(maxLayers);
481 result.append(timeStatsProto.SerializeAsString().c_str(), timeStatsProto.ByteSize());
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700482 } else {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700483 ALOGD("Dumping TimeStats as text");
484 result.append(timeStats.toString(maxLayers).c_str());
485 result.append("\n");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700486 }
487}
488
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700489} // namespace android