blob: c14d93b0510fbcb66f6eb3d949926cf194e1a9a6 [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>
Yiwei Zhang3a226d22018-10-16 09:23:03 -070027#include <utils/Timers.h>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070028#include <utils/Trace.h>
29
30#include <algorithm>
31#include <regex>
32
33namespace android {
34
35TimeStats& TimeStats::getInstance() {
Chia-I Wuebd88882018-09-14 11:02:37 -070036 static TimeStats sInstance;
37 return sInstance;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070038}
39
40void TimeStats::parseArgs(bool asProto, const Vector<String16>& args, size_t& index,
41 String8& result) {
42 ATRACE_CALL();
43
44 if (args.size() > index + 10) {
45 ALOGD("Invalid args count");
46 return;
47 }
48
49 std::unordered_map<std::string, int32_t> argsMap;
50 while (index < args.size()) {
51 argsMap[std::string(String8(args[index]).c_str())] = index;
52 ++index;
53 }
54
55 if (argsMap.count("-disable")) {
56 disable();
57 }
58
59 if (argsMap.count("-dump")) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070060 std::optional<uint32_t> maxLayers = std::nullopt;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070061 auto iter = argsMap.find("-maxlayers");
62 if (iter != argsMap.end() && iter->second + 1 < static_cast<int32_t>(args.size())) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070063 int64_t value = strtol(String8(args[iter->second + 1]).c_str(), nullptr, 10);
64 value = std::clamp(value, int64_t(0), int64_t(UINT32_MAX));
65 maxLayers = static_cast<uint32_t>(value);
Yiwei Zhang0102ad22018-05-02 17:37:17 -070066 }
67
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070068 dump(asProto, maxLayers, result);
Yiwei Zhang0102ad22018-05-02 17:37:17 -070069 }
70
71 if (argsMap.count("-clear")) {
72 clear();
73 }
74
75 if (argsMap.count("-enable")) {
76 enable();
77 }
78}
79
80void TimeStats::incrementTotalFrames() {
81 if (!mEnabled.load()) return;
82
83 ATRACE_CALL();
84
85 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -070086 mTimeStats.totalFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070087}
88
Yiwei Zhang621f9d42018-05-07 10:40:55 -070089void TimeStats::incrementMissedFrames() {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070090 if (!mEnabled.load()) return;
91
92 ATRACE_CALL();
93
94 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -070095 mTimeStats.missedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070096}
97
98void TimeStats::incrementClientCompositionFrames() {
99 if (!mEnabled.load()) return;
100
101 ATRACE_CALL();
102
103 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700104 mTimeStats.clientCompositionFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700105}
106
107bool TimeStats::recordReadyLocked(const std::string& layerName, TimeRecord* timeRecord) {
108 if (!timeRecord->ready) {
109 ALOGV("[%s]-[%" PRIu64 "]-presentFence is still not received", layerName.c_str(),
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700110 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700111 return false;
112 }
113
114 if (timeRecord->acquireFence != nullptr) {
115 if (timeRecord->acquireFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
116 return false;
117 }
118 if (timeRecord->acquireFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700119 timeRecord->frameTime.acquireTime = timeRecord->acquireFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700120 timeRecord->acquireFence = nullptr;
121 } else {
122 ALOGV("[%s]-[%" PRIu64 "]-acquireFence signal time is invalid", layerName.c_str(),
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700123 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700124 }
125 }
126
127 if (timeRecord->presentFence != nullptr) {
128 if (timeRecord->presentFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
129 return false;
130 }
131 if (timeRecord->presentFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700132 timeRecord->frameTime.presentTime = timeRecord->presentFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700133 timeRecord->presentFence = nullptr;
134 } else {
135 ALOGV("[%s]-[%" PRIu64 "]-presentFence signal time invalid", layerName.c_str(),
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700136 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700137 }
138 }
139
140 return true;
141}
142
143static int32_t msBetween(nsecs_t start, nsecs_t end) {
144 int64_t delta = (end - start) / 1000000;
145 delta = std::clamp(delta, int64_t(INT32_MIN), int64_t(INT32_MAX));
146 return static_cast<int32_t>(delta);
147}
148
Yiwei Zhangbd408322018-10-15 18:31:53 -0700149// 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
153static const std::regex packageNameRegex("(?:SurfaceView[-\\s\\t]+)?([^/]+).*#\\d+");
154
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700155static std::string getPackageName(const std::string& layerName) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700156 std::smatch match;
Yiwei Zhangbd408322018-10-15 18:31:53 -0700157 if (std::regex_match(layerName.begin(), layerName.end(), match, packageNameRegex)) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700158 // There must be a match for group 1 otherwise the whole string is not
159 // matched and the above will return false
160 return match[1];
161 }
162 return "";
163}
164
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700165void TimeStats::flushAvailableRecordsToStatsLocked(const std::string& layerName) {
166 ATRACE_CALL();
167
Yiwei Zhangdc224042018-10-18 15:34:00 -0700168 LayerRecord& layerRecord = mTimeStatsTracker[layerName];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700169 TimeRecord& prevTimeRecord = layerRecord.prevTimeRecord;
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700170 std::deque<TimeRecord>& timeRecords = layerRecord.timeRecords;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700171 while (!timeRecords.empty()) {
172 if (!recordReadyLocked(layerName, &timeRecords[0])) break;
173 ALOGV("[%s]-[%" PRIu64 "]-presentFenceTime[%" PRId64 "]", layerName.c_str(),
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700174 timeRecords[0].frameTime.frameNumber, timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700175
176 if (prevTimeRecord.ready) {
Yiwei Zhangdc224042018-10-18 15:34:00 -0700177 if (!mTimeStats.stats.count(layerName)) {
178 mTimeStats.stats[layerName].layerName = layerName;
179 mTimeStats.stats[layerName].packageName = getPackageName(layerName);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700180 }
Yiwei Zhangdc224042018-10-18 15:34:00 -0700181 TimeStatsHelper::TimeStatsLayer& timeStatsLayer = mTimeStats.stats[layerName];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700182 timeStatsLayer.totalFrames++;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700183 timeStatsLayer.droppedFrames += layerRecord.droppedFrames;
184 layerRecord.droppedFrames = 0;
185
186 const int32_t postToAcquireMs = msBetween(timeRecords[0].frameTime.postTime,
187 timeRecords[0].frameTime.acquireTime);
188 ALOGV("[%s]-[%" PRIu64 "]-post2acquire[%d]", layerName.c_str(),
189 timeRecords[0].frameTime.frameNumber, postToAcquireMs);
190 timeStatsLayer.deltas["post2acquire"].insert(postToAcquireMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700191
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700192 const int32_t postToPresentMs = msBetween(timeRecords[0].frameTime.postTime,
193 timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700194 ALOGV("[%s]-[%" PRIu64 "]-post2present[%d]", layerName.c_str(),
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700195 timeRecords[0].frameTime.frameNumber, postToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700196 timeStatsLayer.deltas["post2present"].insert(postToPresentMs);
197
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700198 const int32_t acquireToPresentMs = msBetween(timeRecords[0].frameTime.acquireTime,
199 timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700200 ALOGV("[%s]-[%" PRIu64 "]-acquire2present[%d]", layerName.c_str(),
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700201 timeRecords[0].frameTime.frameNumber, acquireToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700202 timeStatsLayer.deltas["acquire2present"].insert(acquireToPresentMs);
203
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700204 const int32_t latchToPresentMs = msBetween(timeRecords[0].frameTime.latchTime,
205 timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700206 ALOGV("[%s]-[%" PRIu64 "]-latch2present[%d]", layerName.c_str(),
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700207 timeRecords[0].frameTime.frameNumber, latchToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700208 timeStatsLayer.deltas["latch2present"].insert(latchToPresentMs);
209
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700210 const int32_t desiredToPresentMs = msBetween(timeRecords[0].frameTime.desiredTime,
211 timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700212 ALOGV("[%s]-[%" PRIu64 "]-desired2present[%d]", layerName.c_str(),
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700213 timeRecords[0].frameTime.frameNumber, desiredToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700214 timeStatsLayer.deltas["desired2present"].insert(desiredToPresentMs);
215
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700216 const int32_t presentToPresentMs = msBetween(prevTimeRecord.frameTime.presentTime,
217 timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700218 ALOGV("[%s]-[%" PRIu64 "]-present2present[%d]", layerName.c_str(),
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700219 timeRecords[0].frameTime.frameNumber, presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700220 timeStatsLayer.deltas["present2present"].insert(presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700221 }
222 prevTimeRecord = timeRecords[0];
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700223 timeRecords.pop_front();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700224 layerRecord.waitData--;
225 }
226}
227
Yiwei Zhangbd408322018-10-15 18:31:53 -0700228// This regular expression captures the following layer names for instance:
229// 1) StatusBat#0
230// 2) NavigationBar#1
231// 3) co(m).*#0
232// 4) SurfaceView - co(m).*#0
233// Using [-\\s\t]+ for the conjunction part between SurfaceView and co(m).*
234// is a bit more robust in case there's a slight change.
235// The layer name would only consist of . / $ _ 0-9 a-z A-Z in most cases.
236static const std::regex layerNameRegex(
237 "(((SurfaceView[-\\s\\t]+)?com?\\.[./$\\w]+)|((Status|Navigation)Bar))#\\d+");
238
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700239static bool layerNameIsValid(const std::string& layerName) {
Yiwei Zhangbd408322018-10-15 18:31:53 -0700240 return std::regex_match(layerName.begin(), layerName.end(), layerNameRegex);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700241}
242
243void TimeStats::setPostTime(const std::string& layerName, uint64_t frameNumber, nsecs_t postTime) {
244 if (!mEnabled.load()) return;
245
246 ATRACE_CALL();
247 ALOGV("[%s]-[%" PRIu64 "]-PostTime[%" PRId64 "]", layerName.c_str(), frameNumber, postTime);
248
249 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700250 if (!mTimeStatsTracker.count(layerName) && !layerNameIsValid(layerName)) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700251 return;
252 }
Yiwei Zhangdc224042018-10-18 15:34:00 -0700253 LayerRecord& layerRecord = mTimeStatsTracker[layerName];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700254 if (layerRecord.timeRecords.size() == MAX_NUM_TIME_RECORDS) {
255 ALOGV("[%s]-timeRecords is already at its maximum size[%zu]", layerName.c_str(),
256 MAX_NUM_TIME_RECORDS);
257 // TODO(zzyiwei): if this happens, there must be a present fence missing
258 // or waitData is not in the correct position. Need to think out a
259 // reasonable way to recover from this state.
260 return;
261 }
262 // For most media content, the acquireFence is invalid because the buffer is
263 // ready at the queueBuffer stage. In this case, acquireTime should be given
264 // a default value as postTime.
265 TimeRecord timeRecord = {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700266 .frameTime =
267 {
268 .frameNumber = frameNumber,
269 .postTime = postTime,
270 .acquireTime = postTime,
271 },
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700272 };
273 layerRecord.timeRecords.push_back(timeRecord);
274 if (layerRecord.waitData < 0 ||
275 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
276 layerRecord.waitData = layerRecord.timeRecords.size() - 1;
277}
278
279void TimeStats::setLatchTime(const std::string& layerName, uint64_t frameNumber,
280 nsecs_t latchTime) {
281 if (!mEnabled.load()) return;
282
283 ATRACE_CALL();
284 ALOGV("[%s]-[%" PRIu64 "]-LatchTime[%" PRId64 "]", layerName.c_str(), frameNumber, latchTime);
285
286 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700287 if (!mTimeStatsTracker.count(layerName)) return;
288 LayerRecord& layerRecord = mTimeStatsTracker[layerName];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700289 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700290 if (timeRecord.frameTime.frameNumber == frameNumber) {
291 timeRecord.frameTime.latchTime = latchTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700292 }
293}
294
295void TimeStats::setDesiredTime(const std::string& layerName, uint64_t frameNumber,
296 nsecs_t desiredTime) {
297 if (!mEnabled.load()) return;
298
299 ATRACE_CALL();
300 ALOGV("[%s]-[%" PRIu64 "]-DesiredTime[%" PRId64 "]", layerName.c_str(), frameNumber,
301 desiredTime);
302
303 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700304 if (!mTimeStatsTracker.count(layerName)) return;
305 LayerRecord& layerRecord = mTimeStatsTracker[layerName];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700306 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700307 if (timeRecord.frameTime.frameNumber == frameNumber) {
308 timeRecord.frameTime.desiredTime = desiredTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700309 }
310}
311
312void TimeStats::setAcquireTime(const std::string& layerName, uint64_t frameNumber,
313 nsecs_t acquireTime) {
314 if (!mEnabled.load()) return;
315
316 ATRACE_CALL();
317 ALOGV("[%s]-[%" PRIu64 "]-AcquireTime[%" PRId64 "]", layerName.c_str(), frameNumber,
318 acquireTime);
319
320 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700321 if (!mTimeStatsTracker.count(layerName)) return;
322 LayerRecord& layerRecord = mTimeStatsTracker[layerName];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700323 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700324 if (timeRecord.frameTime.frameNumber == frameNumber) {
325 timeRecord.frameTime.acquireTime = acquireTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700326 }
327}
328
329void TimeStats::setAcquireFence(const std::string& layerName, uint64_t frameNumber,
330 const std::shared_ptr<FenceTime>& acquireFence) {
331 if (!mEnabled.load()) return;
332
333 ATRACE_CALL();
334 ALOGV("[%s]-[%" PRIu64 "]-AcquireFenceTime[%" PRId64 "]", layerName.c_str(), frameNumber,
335 acquireFence->getSignalTime());
336
337 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700338 if (!mTimeStatsTracker.count(layerName)) return;
339 LayerRecord& layerRecord = mTimeStatsTracker[layerName];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700340 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700341 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700342 timeRecord.acquireFence = acquireFence;
343 }
344}
345
346void TimeStats::setPresentTime(const std::string& layerName, uint64_t frameNumber,
347 nsecs_t presentTime) {
348 if (!mEnabled.load()) return;
349
350 ATRACE_CALL();
351 ALOGV("[%s]-[%" PRIu64 "]-PresentTime[%" PRId64 "]", layerName.c_str(), frameNumber,
352 presentTime);
353
354 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700355 if (!mTimeStatsTracker.count(layerName)) return;
356 LayerRecord& layerRecord = mTimeStatsTracker[layerName];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700357 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700358 if (timeRecord.frameTime.frameNumber == frameNumber) {
359 timeRecord.frameTime.presentTime = presentTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700360 timeRecord.ready = true;
361 layerRecord.waitData++;
362 }
363
364 flushAvailableRecordsToStatsLocked(layerName);
365}
366
367void TimeStats::setPresentFence(const std::string& layerName, uint64_t frameNumber,
368 const std::shared_ptr<FenceTime>& presentFence) {
369 if (!mEnabled.load()) return;
370
371 ATRACE_CALL();
372 ALOGV("[%s]-[%" PRIu64 "]-PresentFenceTime[%" PRId64 "]", layerName.c_str(), frameNumber,
373 presentFence->getSignalTime());
374
375 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700376 if (!mTimeStatsTracker.count(layerName)) return;
377 LayerRecord& layerRecord = mTimeStatsTracker[layerName];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700378 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700379 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700380 timeRecord.presentFence = presentFence;
381 timeRecord.ready = true;
382 layerRecord.waitData++;
383 }
384
385 flushAvailableRecordsToStatsLocked(layerName);
386}
387
388void TimeStats::onDisconnect(const std::string& layerName) {
389 if (!mEnabled.load()) return;
390
391 ATRACE_CALL();
392 ALOGV("[%s]-onDisconnect", layerName.c_str());
393
394 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700395 if (!mTimeStatsTracker.count(layerName)) return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700396 flushAvailableRecordsToStatsLocked(layerName);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700397 mTimeStatsTracker.erase(layerName);
398}
399
400void TimeStats::onDestroy(const std::string& layerName) {
401 if (!mEnabled.load()) return;
402
403 ATRACE_CALL();
404 ALOGV("[%s]-onDestroy", layerName.c_str());
405
406 std::lock_guard<std::mutex> lock(mMutex);
407 if (!mTimeStatsTracker.count(layerName)) return;
408 flushAvailableRecordsToStatsLocked(layerName);
409 mTimeStatsTracker.erase(layerName);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700410}
411
412void TimeStats::clearLayerRecord(const std::string& layerName) {
413 if (!mEnabled.load()) return;
414
415 ATRACE_CALL();
416 ALOGV("[%s]-clearLayerRecord", layerName.c_str());
417
418 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700419 if (!mTimeStatsTracker.count(layerName)) return;
420 LayerRecord& layerRecord = mTimeStatsTracker[layerName];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700421 layerRecord.timeRecords.clear();
422 layerRecord.prevTimeRecord.ready = false;
423 layerRecord.waitData = -1;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700424 layerRecord.droppedFrames = 0;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700425}
426
427void TimeStats::removeTimeRecord(const std::string& layerName, uint64_t frameNumber) {
428 if (!mEnabled.load()) return;
429
430 ATRACE_CALL();
431 ALOGV("[%s]-[%" PRIu64 "]-removeTimeRecord", layerName.c_str(), frameNumber);
432
433 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700434 if (!mTimeStatsTracker.count(layerName)) return;
435 LayerRecord& layerRecord = mTimeStatsTracker[layerName];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700436 size_t removeAt = 0;
437 for (const TimeRecord& record : layerRecord.timeRecords) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700438 if (record.frameTime.frameNumber == frameNumber) break;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700439 removeAt++;
440 }
441 if (removeAt == layerRecord.timeRecords.size()) return;
442 layerRecord.timeRecords.erase(layerRecord.timeRecords.begin() + removeAt);
443 if (layerRecord.waitData > static_cast<int32_t>(removeAt)) {
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700444 layerRecord.waitData--;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700445 }
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700446 layerRecord.droppedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700447}
448
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700449void TimeStats::flushPowerTimeLocked() {
450 nsecs_t curTime = systemTime();
451 // elapsedTime is in milliseconds.
452 int64_t elapsedTime = (curTime - mPowerTime.prevTime) / 1000000;
453
454 switch (mPowerTime.powerMode) {
455 case HWC_POWER_MODE_NORMAL:
456 mTimeStats.displayOnTime += elapsedTime;
457 break;
458 case HWC_POWER_MODE_OFF:
459 case HWC_POWER_MODE_DOZE:
460 case HWC_POWER_MODE_DOZE_SUSPEND:
461 default:
462 break;
463 }
464
465 mPowerTime.prevTime = curTime;
466}
467
468void TimeStats::setPowerMode(int32_t powerMode) {
469 if (!mEnabled.load()) {
470 std::lock_guard<std::mutex> lock(mMutex);
471 mPowerTime.powerMode = powerMode;
472 return;
473 }
474
475 std::lock_guard<std::mutex> lock(mMutex);
476 if (powerMode == mPowerTime.powerMode) return;
477
478 flushPowerTimeLocked();
479 mPowerTime.powerMode = powerMode;
480}
481
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700482void TimeStats::enable() {
483 if (mEnabled.load()) return;
484
485 ATRACE_CALL();
486
487 std::lock_guard<std::mutex> lock(mMutex);
488 ALOGD("Enabled");
489 mEnabled.store(true);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700490 mTimeStats.statsStart = static_cast<int64_t>(std::time(0));
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700491 mPowerTime.prevTime = systemTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700492}
493
494void TimeStats::disable() {
495 if (!mEnabled.load()) return;
496
497 ATRACE_CALL();
498
499 std::lock_guard<std::mutex> lock(mMutex);
500 ALOGD("Disabled");
501 mEnabled.store(false);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700502 mTimeStats.statsEnd = static_cast<int64_t>(std::time(0));
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700503}
504
505void TimeStats::clear() {
506 ATRACE_CALL();
507
508 std::lock_guard<std::mutex> lock(mMutex);
509 ALOGD("Cleared");
Yiwei Zhangdc224042018-10-18 15:34:00 -0700510 mTimeStats.stats.clear();
511 mTimeStats.statsStart = (mEnabled.load() ? static_cast<int64_t>(std::time(0)) : 0);
512 mTimeStats.statsEnd = 0;
513 mTimeStats.totalFrames = 0;
514 mTimeStats.missedFrames = 0;
515 mTimeStats.clientCompositionFrames = 0;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700516 mTimeStats.displayOnTime = 0;
517 mPowerTime.prevTime = systemTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700518}
519
520bool TimeStats::isEnabled() {
521 return mEnabled.load();
522}
523
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700524void TimeStats::dump(bool asProto, std::optional<uint32_t> maxLayers, String8& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700525 ATRACE_CALL();
526
527 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700528 if (mTimeStats.statsStart == 0) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700529 return;
530 }
531
Yiwei Zhangdc224042018-10-18 15:34:00 -0700532 mTimeStats.statsEnd = static_cast<int64_t>(std::time(0));
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700533
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700534 flushPowerTimeLocked();
535
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700536 if (asProto) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700537 ALOGD("Dumping TimeStats as proto");
Yiwei Zhangdc224042018-10-18 15:34:00 -0700538 SFTimeStatsGlobalProto timeStatsProto = mTimeStats.toProto(maxLayers);
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700539 result.append(timeStatsProto.SerializeAsString().c_str(), timeStatsProto.ByteSize());
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700540 } else {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700541 ALOGD("Dumping TimeStats as text");
Yiwei Zhangdc224042018-10-18 15:34:00 -0700542 result.append(mTimeStats.toString(maxLayers).c_str());
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700543 result.append("\n");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700544 }
545}
546
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700547} // namespace android