blob: 611afce60452fb58e96e4acfef59a952cb4da4cd [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>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070031
32namespace android {
33
Alec Mourifb571ea2019-01-24 18:42:10 -080034namespace impl {
35
Alec Mourib3885ad2019-09-06 17:08:55 -070036TimeStats::TimeStats() {
37 // Temporarily enable TimeStats by default. Telemetry is disabled while
38 // we move onto statsd, so TimeStats is currently not exercised at all
39 // during testing.
40 // TODO: remove this.
41 enable();
42}
43
Dominik Laskowskic2867142019-01-21 11:33:38 -080044void TimeStats::parseArgs(bool asProto, const Vector<String16>& args, std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070045 ATRACE_CALL();
46
Yiwei Zhang0102ad22018-05-02 17:37:17 -070047 std::unordered_map<std::string, int32_t> argsMap;
Dominik Laskowskic2867142019-01-21 11:33:38 -080048 for (size_t index = 0; index < args.size(); ++index) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070049 argsMap[std::string(String8(args[index]).c_str())] = index;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070050 }
51
52 if (argsMap.count("-disable")) {
53 disable();
54 }
55
56 if (argsMap.count("-dump")) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070057 std::optional<uint32_t> maxLayers = std::nullopt;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070058 auto iter = argsMap.find("-maxlayers");
59 if (iter != argsMap.end() && iter->second + 1 < static_cast<int32_t>(args.size())) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070060 int64_t value = strtol(String8(args[iter->second + 1]).c_str(), nullptr, 10);
61 value = std::clamp(value, int64_t(0), int64_t(UINT32_MAX));
62 maxLayers = static_cast<uint32_t>(value);
Yiwei Zhang0102ad22018-05-02 17:37:17 -070063 }
64
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070065 dump(asProto, maxLayers, result);
Yiwei Zhang0102ad22018-05-02 17:37:17 -070066 }
67
68 if (argsMap.count("-clear")) {
69 clear();
70 }
71
72 if (argsMap.count("-enable")) {
73 enable();
74 }
75}
76
Yiwei Zhang7eb58b72019-04-22 19:00:02 -070077std::string TimeStats::miniDump() {
78 ATRACE_CALL();
79
80 std::string result = "TimeStats miniDump:\n";
81 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange926ab52019-08-14 15:16:00 -070082 android::base::StringAppendF(&result, "Number of layers currently being tracked is %zu\n",
Yiwei Zhang7eb58b72019-04-22 19:00:02 -070083 mTimeStatsTracker.size());
Yiwei Zhange926ab52019-08-14 15:16:00 -070084 android::base::StringAppendF(&result, "Number of layers in the stats pool is %zu\n",
85 mTimeStats.stats.size());
Yiwei Zhang7eb58b72019-04-22 19:00:02 -070086 return result;
87}
88
Yiwei Zhang0102ad22018-05-02 17:37:17 -070089void TimeStats::incrementTotalFrames() {
90 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.totalFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070096}
97
Yiwei Zhang621f9d42018-05-07 10:40:55 -070098void TimeStats::incrementMissedFrames() {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070099 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.missedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700105}
106
107void TimeStats::incrementClientCompositionFrames() {
108 if (!mEnabled.load()) return;
109
110 ATRACE_CALL();
111
112 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700113 mTimeStats.clientCompositionFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700114}
115
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800116bool TimeStats::recordReadyLocked(int32_t layerId, TimeRecord* timeRecord) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700117 if (!timeRecord->ready) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800118 ALOGV("[%d]-[%" PRIu64 "]-presentFence is still not received", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700119 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700120 return false;
121 }
122
123 if (timeRecord->acquireFence != nullptr) {
124 if (timeRecord->acquireFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
125 return false;
126 }
127 if (timeRecord->acquireFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700128 timeRecord->frameTime.acquireTime = timeRecord->acquireFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700129 timeRecord->acquireFence = nullptr;
130 } else {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800131 ALOGV("[%d]-[%" PRIu64 "]-acquireFence signal time is invalid", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700132 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700133 }
134 }
135
136 if (timeRecord->presentFence != nullptr) {
137 if (timeRecord->presentFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
138 return false;
139 }
140 if (timeRecord->presentFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700141 timeRecord->frameTime.presentTime = timeRecord->presentFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700142 timeRecord->presentFence = nullptr;
143 } else {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800144 ALOGV("[%d]-[%" PRIu64 "]-presentFence signal time invalid", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700145 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700146 }
147 }
148
149 return true;
150}
151
152static int32_t msBetween(nsecs_t start, nsecs_t end) {
153 int64_t delta = (end - start) / 1000000;
154 delta = std::clamp(delta, int64_t(INT32_MIN), int64_t(INT32_MAX));
155 return static_cast<int32_t>(delta);
156}
157
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800158void TimeStats::flushAvailableRecordsToStatsLocked(int32_t layerId) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700159 ATRACE_CALL();
160
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800161 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700162 TimeRecord& prevTimeRecord = layerRecord.prevTimeRecord;
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700163 std::deque<TimeRecord>& timeRecords = layerRecord.timeRecords;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700164 while (!timeRecords.empty()) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800165 if (!recordReadyLocked(layerId, &timeRecords[0])) break;
166 ALOGV("[%d]-[%" PRIu64 "]-presentFenceTime[%" PRId64 "]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700167 timeRecords[0].frameTime.frameNumber, timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700168
169 if (prevTimeRecord.ready) {
Yiwei Zhangeafa5cc2019-07-26 15:06:25 -0700170 const std::string& layerName = layerRecord.layerName;
Yiwei Zhangdc224042018-10-18 15:34:00 -0700171 if (!mTimeStats.stats.count(layerName)) {
172 mTimeStats.stats[layerName].layerName = layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700173 }
Yiwei Zhangdc224042018-10-18 15:34:00 -0700174 TimeStatsHelper::TimeStatsLayer& timeStatsLayer = mTimeStats.stats[layerName];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700175 timeStatsLayer.totalFrames++;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700176 timeStatsLayer.droppedFrames += layerRecord.droppedFrames;
177 layerRecord.droppedFrames = 0;
178
179 const int32_t postToAcquireMs = msBetween(timeRecords[0].frameTime.postTime,
180 timeRecords[0].frameTime.acquireTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800181 ALOGV("[%d]-[%" PRIu64 "]-post2acquire[%d]", layerId,
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700182 timeRecords[0].frameTime.frameNumber, postToAcquireMs);
183 timeStatsLayer.deltas["post2acquire"].insert(postToAcquireMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700184
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700185 const int32_t postToPresentMs = msBetween(timeRecords[0].frameTime.postTime,
186 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800187 ALOGV("[%d]-[%" PRIu64 "]-post2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700188 timeRecords[0].frameTime.frameNumber, postToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700189 timeStatsLayer.deltas["post2present"].insert(postToPresentMs);
190
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700191 const int32_t acquireToPresentMs = msBetween(timeRecords[0].frameTime.acquireTime,
192 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800193 ALOGV("[%d]-[%" PRIu64 "]-acquire2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700194 timeRecords[0].frameTime.frameNumber, acquireToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700195 timeStatsLayer.deltas["acquire2present"].insert(acquireToPresentMs);
196
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700197 const int32_t latchToPresentMs = msBetween(timeRecords[0].frameTime.latchTime,
198 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800199 ALOGV("[%d]-[%" PRIu64 "]-latch2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700200 timeRecords[0].frameTime.frameNumber, latchToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700201 timeStatsLayer.deltas["latch2present"].insert(latchToPresentMs);
202
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700203 const int32_t desiredToPresentMs = msBetween(timeRecords[0].frameTime.desiredTime,
204 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800205 ALOGV("[%d]-[%" PRIu64 "]-desired2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700206 timeRecords[0].frameTime.frameNumber, desiredToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700207 timeStatsLayer.deltas["desired2present"].insert(desiredToPresentMs);
208
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700209 const int32_t presentToPresentMs = msBetween(prevTimeRecord.frameTime.presentTime,
210 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800211 ALOGV("[%d]-[%" PRIu64 "]-present2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700212 timeRecords[0].frameTime.frameNumber, presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700213 timeStatsLayer.deltas["present2present"].insert(presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700214 }
215 prevTimeRecord = timeRecords[0];
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700216 timeRecords.pop_front();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700217 layerRecord.waitData--;
218 }
219}
220
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700221static constexpr const char* kPopupWindowPrefix = "PopupWindow";
222static const size_t kMinLenLayerName = std::strlen(kPopupWindowPrefix);
Yiwei Zhangbd408322018-10-15 18:31:53 -0700223
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700224// Avoid tracking the "PopupWindow:<random hash>#<number>" layers
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700225static bool layerNameIsValid(const std::string& layerName) {
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700226 return layerName.length() >= kMinLenLayerName &&
227 layerName.compare(0, kMinLenLayerName, kPopupWindowPrefix) != 0;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700228}
229
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800230void TimeStats::setPostTime(int32_t layerId, uint64_t frameNumber, const std::string& layerName,
Yiwei Zhang8e8fe522018-11-02 18:34:07 -0700231 nsecs_t postTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700232 if (!mEnabled.load()) return;
233
234 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800235 ALOGV("[%d]-[%" PRIu64 "]-[%s]-PostTime[%" PRId64 "]", layerId, frameNumber, layerName.c_str(),
Yiwei Zhang8e8fe522018-11-02 18:34:07 -0700236 postTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700237
238 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange926ab52019-08-14 15:16:00 -0700239 if (!mTimeStats.stats.count(layerName) && mTimeStats.stats.size() >= MAX_NUM_LAYER_STATS) {
240 return;
241 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800242 if (!mTimeStatsTracker.count(layerId) && mTimeStatsTracker.size() < MAX_NUM_LAYER_RECORDS &&
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700243 layerNameIsValid(layerName)) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800244 mTimeStatsTracker[layerId].layerName = layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700245 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800246 if (!mTimeStatsTracker.count(layerId)) return;
247 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700248 if (layerRecord.timeRecords.size() == MAX_NUM_TIME_RECORDS) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800249 ALOGE("[%d]-[%s]-timeRecords is at its maximum size[%zu]. Ignore this when unittesting.",
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800250 layerId, layerRecord.layerName.c_str(), MAX_NUM_TIME_RECORDS);
251 mTimeStatsTracker.erase(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700252 return;
253 }
254 // For most media content, the acquireFence is invalid because the buffer is
255 // ready at the queueBuffer stage. In this case, acquireTime should be given
256 // a default value as postTime.
257 TimeRecord timeRecord = {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700258 .frameTime =
259 {
260 .frameNumber = frameNumber,
261 .postTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800262 .latchTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700263 .acquireTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800264 .desiredTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700265 },
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
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800273void TimeStats::setLatchTime(int32_t layerId, uint64_t frameNumber, nsecs_t latchTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700274 if (!mEnabled.load()) return;
275
276 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800277 ALOGV("[%d]-[%" PRIu64 "]-LatchTime[%" PRId64 "]", layerId, frameNumber, latchTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700278
279 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800280 if (!mTimeStatsTracker.count(layerId)) return;
281 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700282 if (layerRecord.waitData < 0 ||
283 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
284 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700285 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
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800291void TimeStats::setDesiredTime(int32_t layerId, uint64_t frameNumber, nsecs_t desiredTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700292 if (!mEnabled.load()) return;
293
294 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800295 ALOGV("[%d]-[%" PRIu64 "]-DesiredTime[%" PRId64 "]", layerId, frameNumber, desiredTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700296
297 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800298 if (!mTimeStatsTracker.count(layerId)) return;
299 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700300 if (layerRecord.waitData < 0 ||
301 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
302 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700303 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700304 if (timeRecord.frameTime.frameNumber == frameNumber) {
305 timeRecord.frameTime.desiredTime = desiredTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700306 }
307}
308
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800309void TimeStats::setAcquireTime(int32_t layerId, uint64_t frameNumber, nsecs_t acquireTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700310 if (!mEnabled.load()) return;
311
312 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800313 ALOGV("[%d]-[%" PRIu64 "]-AcquireTime[%" PRId64 "]", layerId, frameNumber, acquireTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700314
315 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800316 if (!mTimeStatsTracker.count(layerId)) return;
317 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700318 if (layerRecord.waitData < 0 ||
319 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
320 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700321 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700322 if (timeRecord.frameTime.frameNumber == frameNumber) {
323 timeRecord.frameTime.acquireTime = acquireTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700324 }
325}
326
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800327void TimeStats::setAcquireFence(int32_t layerId, uint64_t frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700328 const std::shared_ptr<FenceTime>& acquireFence) {
329 if (!mEnabled.load()) return;
330
331 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800332 ALOGV("[%d]-[%" PRIu64 "]-AcquireFenceTime[%" PRId64 "]", layerId, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700333 acquireFence->getSignalTime());
334
335 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800336 if (!mTimeStatsTracker.count(layerId)) return;
337 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700338 if (layerRecord.waitData < 0 ||
339 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
340 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700341 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700342 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700343 timeRecord.acquireFence = acquireFence;
344 }
345}
346
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800347void TimeStats::setPresentTime(int32_t layerId, uint64_t frameNumber, nsecs_t presentTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700348 if (!mEnabled.load()) return;
349
350 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800351 ALOGV("[%d]-[%" PRIu64 "]-PresentTime[%" PRId64 "]", layerId, frameNumber, presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700352
353 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800354 if (!mTimeStatsTracker.count(layerId)) return;
355 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700356 if (layerRecord.waitData < 0 ||
357 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
358 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700359 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700360 if (timeRecord.frameTime.frameNumber == frameNumber) {
361 timeRecord.frameTime.presentTime = presentTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700362 timeRecord.ready = true;
363 layerRecord.waitData++;
364 }
365
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800366 flushAvailableRecordsToStatsLocked(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700367}
368
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800369void TimeStats::setPresentFence(int32_t layerId, uint64_t frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700370 const std::shared_ptr<FenceTime>& presentFence) {
371 if (!mEnabled.load()) return;
372
373 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800374 ALOGV("[%d]-[%" PRIu64 "]-PresentFenceTime[%" PRId64 "]", layerId, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700375 presentFence->getSignalTime());
376
377 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800378 if (!mTimeStatsTracker.count(layerId)) return;
379 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700380 if (layerRecord.waitData < 0 ||
381 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
382 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700383 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700384 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700385 timeRecord.presentFence = presentFence;
386 timeRecord.ready = true;
387 layerRecord.waitData++;
388 }
389
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800390 flushAvailableRecordsToStatsLocked(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700391}
392
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800393void TimeStats::onDestroy(int32_t layerId) {
Yiwei Zhangdc224042018-10-18 15:34:00 -0700394 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800395 ALOGV("[%d]-onDestroy", layerId);
Mikael Pessa90092f42019-08-26 17:22:04 -0700396 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800397 mTimeStatsTracker.erase(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700398}
399
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800400void TimeStats::removeTimeRecord(int32_t layerId, uint64_t frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700401 if (!mEnabled.load()) return;
402
403 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800404 ALOGV("[%d]-[%" PRIu64 "]-removeTimeRecord", layerId, frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700405
406 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800407 if (!mTimeStatsTracker.count(layerId)) return;
408 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700409 size_t removeAt = 0;
410 for (const TimeRecord& record : layerRecord.timeRecords) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700411 if (record.frameTime.frameNumber == frameNumber) break;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700412 removeAt++;
413 }
414 if (removeAt == layerRecord.timeRecords.size()) return;
415 layerRecord.timeRecords.erase(layerRecord.timeRecords.begin() + removeAt);
416 if (layerRecord.waitData > static_cast<int32_t>(removeAt)) {
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700417 layerRecord.waitData--;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700418 }
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700419 layerRecord.droppedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700420}
421
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700422void TimeStats::flushPowerTimeLocked() {
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700423 if (!mEnabled.load()) return;
424
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700425 nsecs_t curTime = systemTime();
426 // elapsedTime is in milliseconds.
427 int64_t elapsedTime = (curTime - mPowerTime.prevTime) / 1000000;
428
429 switch (mPowerTime.powerMode) {
430 case HWC_POWER_MODE_NORMAL:
431 mTimeStats.displayOnTime += elapsedTime;
432 break;
433 case HWC_POWER_MODE_OFF:
434 case HWC_POWER_MODE_DOZE:
435 case HWC_POWER_MODE_DOZE_SUSPEND:
436 default:
437 break;
438 }
439
440 mPowerTime.prevTime = curTime;
441}
442
443void TimeStats::setPowerMode(int32_t powerMode) {
444 if (!mEnabled.load()) {
445 std::lock_guard<std::mutex> lock(mMutex);
446 mPowerTime.powerMode = powerMode;
447 return;
448 }
449
450 std::lock_guard<std::mutex> lock(mMutex);
451 if (powerMode == mPowerTime.powerMode) return;
452
453 flushPowerTimeLocked();
454 mPowerTime.powerMode = powerMode;
455}
456
Alec Mourifb571ea2019-01-24 18:42:10 -0800457void TimeStats::recordRefreshRate(uint32_t fps, nsecs_t duration) {
458 std::lock_guard<std::mutex> lock(mMutex);
459 if (mTimeStats.refreshRateStats.count(fps)) {
460 mTimeStats.refreshRateStats[fps] += duration;
461 } else {
462 mTimeStats.refreshRateStats.insert({fps, duration});
463 }
464}
465
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700466void TimeStats::flushAvailableGlobalRecordsToStatsLocked() {
467 ATRACE_CALL();
468
469 while (!mGlobalRecord.presentFences.empty()) {
470 const nsecs_t curPresentTime = mGlobalRecord.presentFences.front()->getSignalTime();
471 if (curPresentTime == Fence::SIGNAL_TIME_PENDING) break;
472
473 if (curPresentTime == Fence::SIGNAL_TIME_INVALID) {
474 ALOGE("GlobalPresentFence is invalid!");
475 mGlobalRecord.prevPresentTime = 0;
476 mGlobalRecord.presentFences.pop_front();
477 continue;
478 }
479
480 ALOGV("GlobalPresentFenceTime[%" PRId64 "]",
481 mGlobalRecord.presentFences.front()->getSignalTime());
482
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700483 if (mGlobalRecord.prevPresentTime != 0) {
484 const int32_t presentToPresentMs =
485 msBetween(mGlobalRecord.prevPresentTime, curPresentTime);
486 ALOGV("Global present2present[%d] prev[%" PRId64 "] curr[%" PRId64 "]",
487 presentToPresentMs, mGlobalRecord.prevPresentTime, curPresentTime);
488 mTimeStats.presentToPresent.insert(presentToPresentMs);
489 }
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700490
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700491 mGlobalRecord.prevPresentTime = curPresentTime;
492 mGlobalRecord.presentFences.pop_front();
493 }
494}
495
496void TimeStats::setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) {
497 if (!mEnabled.load()) return;
498
499 ATRACE_CALL();
500 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700501 if (presentFence == nullptr || !presentFence->isValid()) {
502 mGlobalRecord.prevPresentTime = 0;
503 return;
504 }
505
506 if (mPowerTime.powerMode != HWC_POWER_MODE_NORMAL) {
507 // Try flushing the last present fence on HWC_POWER_MODE_NORMAL.
508 flushAvailableGlobalRecordsToStatsLocked();
509 mGlobalRecord.presentFences.clear();
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700510 mGlobalRecord.prevPresentTime = 0;
511 return;
512 }
513
514 if (mGlobalRecord.presentFences.size() == MAX_NUM_TIME_RECORDS) {
515 // The front presentFence must be trapped in pending status in this
516 // case. Try dequeuing the front one to recover.
517 ALOGE("GlobalPresentFences is already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
518 mGlobalRecord.prevPresentTime = 0;
519 mGlobalRecord.presentFences.pop_front();
520 }
521
522 mGlobalRecord.presentFences.emplace_back(presentFence);
523 flushAvailableGlobalRecordsToStatsLocked();
524}
525
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700526void TimeStats::enable() {
527 if (mEnabled.load()) return;
528
529 ATRACE_CALL();
530
531 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700532 mEnabled.store(true);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700533 mTimeStats.statsStart = static_cast<int64_t>(std::time(0));
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700534 mPowerTime.prevTime = systemTime();
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700535 ALOGD("Enabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700536}
537
538void TimeStats::disable() {
539 if (!mEnabled.load()) return;
540
541 ATRACE_CALL();
542
543 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700544 flushPowerTimeLocked();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700545 mEnabled.store(false);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700546 mTimeStats.statsEnd = static_cast<int64_t>(std::time(0));
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700547 ALOGD("Disabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700548}
549
550void TimeStats::clear() {
551 ATRACE_CALL();
552
553 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700554 mTimeStatsTracker.clear();
Yiwei Zhangdc224042018-10-18 15:34:00 -0700555 mTimeStats.stats.clear();
556 mTimeStats.statsStart = (mEnabled.load() ? static_cast<int64_t>(std::time(0)) : 0);
557 mTimeStats.statsEnd = 0;
558 mTimeStats.totalFrames = 0;
559 mTimeStats.missedFrames = 0;
560 mTimeStats.clientCompositionFrames = 0;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700561 mTimeStats.displayOnTime = 0;
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700562 mTimeStats.presentToPresent.hist.clear();
Alec Mourifb571ea2019-01-24 18:42:10 -0800563 mTimeStats.refreshRateStats.clear();
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700564 mPowerTime.prevTime = systemTime();
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700565 mGlobalRecord.prevPresentTime = 0;
566 mGlobalRecord.presentFences.clear();
567 ALOGD("Cleared");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700568}
569
570bool TimeStats::isEnabled() {
571 return mEnabled.load();
572}
573
Yiwei Zhang5434a782018-12-05 18:06:32 -0800574void TimeStats::dump(bool asProto, std::optional<uint32_t> maxLayers, std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700575 ATRACE_CALL();
576
577 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700578 if (mTimeStats.statsStart == 0) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700579 return;
580 }
581
Yiwei Zhangdc224042018-10-18 15:34:00 -0700582 mTimeStats.statsEnd = static_cast<int64_t>(std::time(0));
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700583
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700584 flushPowerTimeLocked();
585
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700586 if (asProto) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700587 ALOGD("Dumping TimeStats as proto");
Yiwei Zhangdc224042018-10-18 15:34:00 -0700588 SFTimeStatsGlobalProto timeStatsProto = mTimeStats.toProto(maxLayers);
Dominik Laskowski46470112019-08-02 13:13:11 -0700589 result.append(timeStatsProto.SerializeAsString());
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700590 } else {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700591 ALOGD("Dumping TimeStats as text");
Yiwei Zhang5434a782018-12-05 18:06:32 -0800592 result.append(mTimeStats.toString(maxLayers));
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700593 result.append("\n");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700594 }
595}
596
Alec Mourifb571ea2019-01-24 18:42:10 -0800597} // namespace impl
598
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700599} // namespace android