blob: 626efb856fe32c4fe63ec63c4fc40dc876e2d18c [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>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070023#include <log/log.h>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070024#include <utils/String8.h>
Yiwei Zhang3a226d22018-10-16 09:23:03 -070025#include <utils/Timers.h>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070026#include <utils/Trace.h>
27
28#include <algorithm>
Alec Mouri9519bf12019-11-15 16:54:44 -080029#include <chrono>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070030
31namespace android {
32
Alec Mourifb571ea2019-01-24 18:42:10 -080033namespace impl {
34
Alec Mourib3885ad2019-09-06 17:08:55 -070035TimeStats::TimeStats() {
36 // Temporarily enable TimeStats by default. Telemetry is disabled while
37 // we move onto statsd, so TimeStats is currently not exercised at all
38 // during testing.
39 // TODO: remove this.
40 enable();
41}
42
Dominik Laskowskic2867142019-01-21 11:33:38 -080043void TimeStats::parseArgs(bool asProto, const Vector<String16>& args, std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070044 ATRACE_CALL();
45
Yiwei Zhang0102ad22018-05-02 17:37:17 -070046 std::unordered_map<std::string, int32_t> argsMap;
Dominik Laskowskic2867142019-01-21 11:33:38 -080047 for (size_t index = 0; index < args.size(); ++index) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070048 argsMap[std::string(String8(args[index]).c_str())] = index;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070049 }
50
51 if (argsMap.count("-disable")) {
52 disable();
53 }
54
55 if (argsMap.count("-dump")) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070056 std::optional<uint32_t> maxLayers = std::nullopt;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070057 auto iter = argsMap.find("-maxlayers");
58 if (iter != argsMap.end() && iter->second + 1 < static_cast<int32_t>(args.size())) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070059 int64_t value = strtol(String8(args[iter->second + 1]).c_str(), nullptr, 10);
60 value = std::clamp(value, int64_t(0), int64_t(UINT32_MAX));
61 maxLayers = static_cast<uint32_t>(value);
Yiwei Zhang0102ad22018-05-02 17:37:17 -070062 }
63
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070064 dump(asProto, maxLayers, result);
Yiwei Zhang0102ad22018-05-02 17:37:17 -070065 }
66
67 if (argsMap.count("-clear")) {
68 clear();
69 }
70
71 if (argsMap.count("-enable")) {
72 enable();
73 }
74}
75
Yiwei Zhang7eb58b72019-04-22 19:00:02 -070076std::string TimeStats::miniDump() {
77 ATRACE_CALL();
78
79 std::string result = "TimeStats miniDump:\n";
80 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange926ab52019-08-14 15:16:00 -070081 android::base::StringAppendF(&result, "Number of layers currently being tracked is %zu\n",
Yiwei Zhang7eb58b72019-04-22 19:00:02 -070082 mTimeStatsTracker.size());
Yiwei Zhange926ab52019-08-14 15:16:00 -070083 android::base::StringAppendF(&result, "Number of layers in the stats pool is %zu\n",
84 mTimeStats.stats.size());
Yiwei Zhang7eb58b72019-04-22 19:00:02 -070085 return result;
86}
87
Yiwei Zhang0102ad22018-05-02 17:37:17 -070088void TimeStats::incrementTotalFrames() {
89 if (!mEnabled.load()) return;
90
91 ATRACE_CALL();
92
93 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -070094 mTimeStats.totalFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -070095}
96
Yiwei Zhang621f9d42018-05-07 10:40:55 -070097void TimeStats::incrementMissedFrames() {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070098 if (!mEnabled.load()) return;
99
100 ATRACE_CALL();
101
102 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700103 mTimeStats.missedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700104}
105
106void TimeStats::incrementClientCompositionFrames() {
107 if (!mEnabled.load()) return;
108
109 ATRACE_CALL();
110
111 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700112 mTimeStats.clientCompositionFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700113}
114
Alec Mouri9519bf12019-11-15 16:54:44 -0800115static int32_t msBetween(nsecs_t start, nsecs_t end) {
116 int64_t delta = std::chrono::duration_cast<std::chrono::milliseconds>(
117 std::chrono::nanoseconds(end - start))
118 .count();
119 delta = std::clamp(delta, int64_t(INT32_MIN), int64_t(INT32_MAX));
120 return static_cast<int32_t>(delta);
121}
122
123void TimeStats::recordFrameDuration(nsecs_t startTime, nsecs_t endTime) {
124 if (!mEnabled.load()) return;
125
126 std::lock_guard<std::mutex> lock(mMutex);
127 if (mPowerTime.powerMode == HWC_POWER_MODE_NORMAL) {
128 mTimeStats.frameDuration.insert(msBetween(startTime, endTime));
129 }
130}
131
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800132bool TimeStats::recordReadyLocked(int32_t layerId, TimeRecord* timeRecord) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700133 if (!timeRecord->ready) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800134 ALOGV("[%d]-[%" PRIu64 "]-presentFence is still not received", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700135 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700136 return false;
137 }
138
139 if (timeRecord->acquireFence != nullptr) {
140 if (timeRecord->acquireFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
141 return false;
142 }
143 if (timeRecord->acquireFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700144 timeRecord->frameTime.acquireTime = timeRecord->acquireFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700145 timeRecord->acquireFence = nullptr;
146 } else {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800147 ALOGV("[%d]-[%" PRIu64 "]-acquireFence signal time is invalid", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700148 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700149 }
150 }
151
152 if (timeRecord->presentFence != nullptr) {
153 if (timeRecord->presentFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
154 return false;
155 }
156 if (timeRecord->presentFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700157 timeRecord->frameTime.presentTime = timeRecord->presentFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700158 timeRecord->presentFence = nullptr;
159 } else {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800160 ALOGV("[%d]-[%" PRIu64 "]-presentFence signal time invalid", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700161 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700162 }
163 }
164
165 return true;
166}
167
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800168void TimeStats::flushAvailableRecordsToStatsLocked(int32_t layerId) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700169 ATRACE_CALL();
170
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800171 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700172 TimeRecord& prevTimeRecord = layerRecord.prevTimeRecord;
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700173 std::deque<TimeRecord>& timeRecords = layerRecord.timeRecords;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700174 while (!timeRecords.empty()) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800175 if (!recordReadyLocked(layerId, &timeRecords[0])) break;
176 ALOGV("[%d]-[%" PRIu64 "]-presentFenceTime[%" PRId64 "]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700177 timeRecords[0].frameTime.frameNumber, timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700178
179 if (prevTimeRecord.ready) {
Yiwei Zhangeafa5cc2019-07-26 15:06:25 -0700180 const std::string& layerName = layerRecord.layerName;
Yiwei Zhangdc224042018-10-18 15:34:00 -0700181 if (!mTimeStats.stats.count(layerName)) {
182 mTimeStats.stats[layerName].layerName = layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700183 }
Yiwei Zhangdc224042018-10-18 15:34:00 -0700184 TimeStatsHelper::TimeStatsLayer& timeStatsLayer = mTimeStats.stats[layerName];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700185 timeStatsLayer.totalFrames++;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700186 timeStatsLayer.droppedFrames += layerRecord.droppedFrames;
187 layerRecord.droppedFrames = 0;
188
189 const int32_t postToAcquireMs = msBetween(timeRecords[0].frameTime.postTime,
190 timeRecords[0].frameTime.acquireTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800191 ALOGV("[%d]-[%" PRIu64 "]-post2acquire[%d]", layerId,
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700192 timeRecords[0].frameTime.frameNumber, postToAcquireMs);
193 timeStatsLayer.deltas["post2acquire"].insert(postToAcquireMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700194
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700195 const int32_t postToPresentMs = msBetween(timeRecords[0].frameTime.postTime,
196 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800197 ALOGV("[%d]-[%" PRIu64 "]-post2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700198 timeRecords[0].frameTime.frameNumber, postToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700199 timeStatsLayer.deltas["post2present"].insert(postToPresentMs);
200
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700201 const int32_t acquireToPresentMs = msBetween(timeRecords[0].frameTime.acquireTime,
202 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800203 ALOGV("[%d]-[%" PRIu64 "]-acquire2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700204 timeRecords[0].frameTime.frameNumber, acquireToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700205 timeStatsLayer.deltas["acquire2present"].insert(acquireToPresentMs);
206
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700207 const int32_t latchToPresentMs = msBetween(timeRecords[0].frameTime.latchTime,
208 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800209 ALOGV("[%d]-[%" PRIu64 "]-latch2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700210 timeRecords[0].frameTime.frameNumber, latchToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700211 timeStatsLayer.deltas["latch2present"].insert(latchToPresentMs);
212
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700213 const int32_t desiredToPresentMs = msBetween(timeRecords[0].frameTime.desiredTime,
214 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800215 ALOGV("[%d]-[%" PRIu64 "]-desired2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700216 timeRecords[0].frameTime.frameNumber, desiredToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700217 timeStatsLayer.deltas["desired2present"].insert(desiredToPresentMs);
218
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700219 const int32_t presentToPresentMs = msBetween(prevTimeRecord.frameTime.presentTime,
220 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800221 ALOGV("[%d]-[%" PRIu64 "]-present2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700222 timeRecords[0].frameTime.frameNumber, presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700223 timeStatsLayer.deltas["present2present"].insert(presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700224 }
225 prevTimeRecord = timeRecords[0];
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700226 timeRecords.pop_front();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700227 layerRecord.waitData--;
228 }
229}
230
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700231static constexpr const char* kPopupWindowPrefix = "PopupWindow";
232static const size_t kMinLenLayerName = std::strlen(kPopupWindowPrefix);
Yiwei Zhangbd408322018-10-15 18:31:53 -0700233
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700234// Avoid tracking the "PopupWindow:<random hash>#<number>" layers
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700235static bool layerNameIsValid(const std::string& layerName) {
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700236 return layerName.length() >= kMinLenLayerName &&
237 layerName.compare(0, kMinLenLayerName, kPopupWindowPrefix) != 0;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700238}
239
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800240void TimeStats::setPostTime(int32_t layerId, uint64_t frameNumber, const std::string& layerName,
Yiwei Zhang8e8fe522018-11-02 18:34:07 -0700241 nsecs_t postTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700242 if (!mEnabled.load()) return;
243
244 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800245 ALOGV("[%d]-[%" PRIu64 "]-[%s]-PostTime[%" PRId64 "]", layerId, frameNumber, layerName.c_str(),
Yiwei Zhang8e8fe522018-11-02 18:34:07 -0700246 postTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700247
248 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange926ab52019-08-14 15:16:00 -0700249 if (!mTimeStats.stats.count(layerName) && mTimeStats.stats.size() >= MAX_NUM_LAYER_STATS) {
250 return;
251 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800252 if (!mTimeStatsTracker.count(layerId) && mTimeStatsTracker.size() < MAX_NUM_LAYER_RECORDS &&
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700253 layerNameIsValid(layerName)) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800254 mTimeStatsTracker[layerId].layerName = layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700255 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800256 if (!mTimeStatsTracker.count(layerId)) return;
257 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700258 if (layerRecord.timeRecords.size() == MAX_NUM_TIME_RECORDS) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800259 ALOGE("[%d]-[%s]-timeRecords is at its maximum size[%zu]. Ignore this when unittesting.",
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800260 layerId, layerRecord.layerName.c_str(), MAX_NUM_TIME_RECORDS);
261 mTimeStatsTracker.erase(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700262 return;
263 }
264 // For most media content, the acquireFence is invalid because the buffer is
265 // ready at the queueBuffer stage. In this case, acquireTime should be given
266 // a default value as postTime.
267 TimeRecord timeRecord = {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700268 .frameTime =
269 {
270 .frameNumber = frameNumber,
271 .postTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800272 .latchTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700273 .acquireTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800274 .desiredTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700275 },
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700276 };
277 layerRecord.timeRecords.push_back(timeRecord);
278 if (layerRecord.waitData < 0 ||
279 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
280 layerRecord.waitData = layerRecord.timeRecords.size() - 1;
281}
282
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800283void TimeStats::setLatchTime(int32_t layerId, uint64_t frameNumber, nsecs_t latchTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700284 if (!mEnabled.load()) return;
285
286 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800287 ALOGV("[%d]-[%" PRIu64 "]-LatchTime[%" PRId64 "]", layerId, frameNumber, latchTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700288
289 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800290 if (!mTimeStatsTracker.count(layerId)) return;
291 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700292 if (layerRecord.waitData < 0 ||
293 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
294 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700295 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700296 if (timeRecord.frameTime.frameNumber == frameNumber) {
297 timeRecord.frameTime.latchTime = latchTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700298 }
299}
300
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800301void TimeStats::setDesiredTime(int32_t layerId, uint64_t frameNumber, nsecs_t desiredTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700302 if (!mEnabled.load()) return;
303
304 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800305 ALOGV("[%d]-[%" PRIu64 "]-DesiredTime[%" PRId64 "]", layerId, frameNumber, desiredTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700306
307 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800308 if (!mTimeStatsTracker.count(layerId)) return;
309 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700310 if (layerRecord.waitData < 0 ||
311 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
312 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700313 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700314 if (timeRecord.frameTime.frameNumber == frameNumber) {
315 timeRecord.frameTime.desiredTime = desiredTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700316 }
317}
318
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800319void TimeStats::setAcquireTime(int32_t layerId, uint64_t frameNumber, nsecs_t acquireTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700320 if (!mEnabled.load()) return;
321
322 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800323 ALOGV("[%d]-[%" PRIu64 "]-AcquireTime[%" PRId64 "]", layerId, frameNumber, acquireTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700324
325 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800326 if (!mTimeStatsTracker.count(layerId)) return;
327 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700328 if (layerRecord.waitData < 0 ||
329 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
330 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700331 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700332 if (timeRecord.frameTime.frameNumber == frameNumber) {
333 timeRecord.frameTime.acquireTime = acquireTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700334 }
335}
336
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800337void TimeStats::setAcquireFence(int32_t layerId, uint64_t frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700338 const std::shared_ptr<FenceTime>& acquireFence) {
339 if (!mEnabled.load()) return;
340
341 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800342 ALOGV("[%d]-[%" PRIu64 "]-AcquireFenceTime[%" PRId64 "]", layerId, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700343 acquireFence->getSignalTime());
344
345 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800346 if (!mTimeStatsTracker.count(layerId)) return;
347 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700348 if (layerRecord.waitData < 0 ||
349 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
350 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700351 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700352 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700353 timeRecord.acquireFence = acquireFence;
354 }
355}
356
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800357void TimeStats::setPresentTime(int32_t layerId, uint64_t frameNumber, nsecs_t presentTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700358 if (!mEnabled.load()) return;
359
360 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800361 ALOGV("[%d]-[%" PRIu64 "]-PresentTime[%" PRId64 "]", layerId, frameNumber, presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700362
363 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800364 if (!mTimeStatsTracker.count(layerId)) return;
365 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700366 if (layerRecord.waitData < 0 ||
367 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
368 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700369 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700370 if (timeRecord.frameTime.frameNumber == frameNumber) {
371 timeRecord.frameTime.presentTime = presentTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700372 timeRecord.ready = true;
373 layerRecord.waitData++;
374 }
375
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800376 flushAvailableRecordsToStatsLocked(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700377}
378
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800379void TimeStats::setPresentFence(int32_t layerId, uint64_t frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700380 const std::shared_ptr<FenceTime>& presentFence) {
381 if (!mEnabled.load()) return;
382
383 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800384 ALOGV("[%d]-[%" PRIu64 "]-PresentFenceTime[%" PRId64 "]", layerId, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700385 presentFence->getSignalTime());
386
387 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800388 if (!mTimeStatsTracker.count(layerId)) return;
389 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700390 if (layerRecord.waitData < 0 ||
391 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
392 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700393 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700394 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700395 timeRecord.presentFence = presentFence;
396 timeRecord.ready = true;
397 layerRecord.waitData++;
398 }
399
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800400 flushAvailableRecordsToStatsLocked(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700401}
402
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800403void TimeStats::onDestroy(int32_t layerId) {
Yiwei Zhangdc224042018-10-18 15:34:00 -0700404 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800405 ALOGV("[%d]-onDestroy", layerId);
Mikael Pessa90092f42019-08-26 17:22:04 -0700406 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800407 mTimeStatsTracker.erase(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700408}
409
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800410void TimeStats::removeTimeRecord(int32_t layerId, uint64_t frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700411 if (!mEnabled.load()) return;
412
413 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800414 ALOGV("[%d]-[%" PRIu64 "]-removeTimeRecord", layerId, frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700415
416 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800417 if (!mTimeStatsTracker.count(layerId)) return;
418 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700419 size_t removeAt = 0;
420 for (const TimeRecord& record : layerRecord.timeRecords) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700421 if (record.frameTime.frameNumber == frameNumber) break;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700422 removeAt++;
423 }
424 if (removeAt == layerRecord.timeRecords.size()) return;
425 layerRecord.timeRecords.erase(layerRecord.timeRecords.begin() + removeAt);
426 if (layerRecord.waitData > static_cast<int32_t>(removeAt)) {
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700427 layerRecord.waitData--;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700428 }
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700429 layerRecord.droppedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700430}
431
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700432void TimeStats::flushPowerTimeLocked() {
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700433 if (!mEnabled.load()) return;
434
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700435 nsecs_t curTime = systemTime();
436 // elapsedTime is in milliseconds.
437 int64_t elapsedTime = (curTime - mPowerTime.prevTime) / 1000000;
438
439 switch (mPowerTime.powerMode) {
440 case HWC_POWER_MODE_NORMAL:
441 mTimeStats.displayOnTime += elapsedTime;
442 break;
443 case HWC_POWER_MODE_OFF:
444 case HWC_POWER_MODE_DOZE:
445 case HWC_POWER_MODE_DOZE_SUSPEND:
446 default:
447 break;
448 }
449
450 mPowerTime.prevTime = curTime;
451}
452
453void TimeStats::setPowerMode(int32_t powerMode) {
454 if (!mEnabled.load()) {
455 std::lock_guard<std::mutex> lock(mMutex);
456 mPowerTime.powerMode = powerMode;
457 return;
458 }
459
460 std::lock_guard<std::mutex> lock(mMutex);
461 if (powerMode == mPowerTime.powerMode) return;
462
463 flushPowerTimeLocked();
464 mPowerTime.powerMode = powerMode;
465}
466
Alec Mourifb571ea2019-01-24 18:42:10 -0800467void TimeStats::recordRefreshRate(uint32_t fps, nsecs_t duration) {
468 std::lock_guard<std::mutex> lock(mMutex);
469 if (mTimeStats.refreshRateStats.count(fps)) {
470 mTimeStats.refreshRateStats[fps] += duration;
471 } else {
472 mTimeStats.refreshRateStats.insert({fps, duration});
473 }
474}
475
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700476void TimeStats::flushAvailableGlobalRecordsToStatsLocked() {
477 ATRACE_CALL();
478
479 while (!mGlobalRecord.presentFences.empty()) {
480 const nsecs_t curPresentTime = mGlobalRecord.presentFences.front()->getSignalTime();
481 if (curPresentTime == Fence::SIGNAL_TIME_PENDING) break;
482
483 if (curPresentTime == Fence::SIGNAL_TIME_INVALID) {
484 ALOGE("GlobalPresentFence is invalid!");
485 mGlobalRecord.prevPresentTime = 0;
486 mGlobalRecord.presentFences.pop_front();
487 continue;
488 }
489
490 ALOGV("GlobalPresentFenceTime[%" PRId64 "]",
491 mGlobalRecord.presentFences.front()->getSignalTime());
492
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700493 if (mGlobalRecord.prevPresentTime != 0) {
494 const int32_t presentToPresentMs =
495 msBetween(mGlobalRecord.prevPresentTime, curPresentTime);
496 ALOGV("Global present2present[%d] prev[%" PRId64 "] curr[%" PRId64 "]",
497 presentToPresentMs, mGlobalRecord.prevPresentTime, curPresentTime);
498 mTimeStats.presentToPresent.insert(presentToPresentMs);
499 }
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700500
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700501 mGlobalRecord.prevPresentTime = curPresentTime;
502 mGlobalRecord.presentFences.pop_front();
503 }
504}
505
506void TimeStats::setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) {
507 if (!mEnabled.load()) return;
508
509 ATRACE_CALL();
510 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700511 if (presentFence == nullptr || !presentFence->isValid()) {
512 mGlobalRecord.prevPresentTime = 0;
513 return;
514 }
515
516 if (mPowerTime.powerMode != HWC_POWER_MODE_NORMAL) {
517 // Try flushing the last present fence on HWC_POWER_MODE_NORMAL.
518 flushAvailableGlobalRecordsToStatsLocked();
519 mGlobalRecord.presentFences.clear();
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700520 mGlobalRecord.prevPresentTime = 0;
521 return;
522 }
523
524 if (mGlobalRecord.presentFences.size() == MAX_NUM_TIME_RECORDS) {
525 // The front presentFence must be trapped in pending status in this
526 // case. Try dequeuing the front one to recover.
527 ALOGE("GlobalPresentFences is already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
528 mGlobalRecord.prevPresentTime = 0;
529 mGlobalRecord.presentFences.pop_front();
530 }
531
532 mGlobalRecord.presentFences.emplace_back(presentFence);
533 flushAvailableGlobalRecordsToStatsLocked();
534}
535
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700536void TimeStats::enable() {
537 if (mEnabled.load()) return;
538
539 ATRACE_CALL();
540
541 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700542 mEnabled.store(true);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700543 mTimeStats.statsStart = static_cast<int64_t>(std::time(0));
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700544 mPowerTime.prevTime = systemTime();
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700545 ALOGD("Enabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700546}
547
548void TimeStats::disable() {
549 if (!mEnabled.load()) return;
550
551 ATRACE_CALL();
552
553 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700554 flushPowerTimeLocked();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700555 mEnabled.store(false);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700556 mTimeStats.statsEnd = static_cast<int64_t>(std::time(0));
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700557 ALOGD("Disabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700558}
559
560void TimeStats::clear() {
561 ATRACE_CALL();
562
563 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700564 mTimeStatsTracker.clear();
Yiwei Zhangdc224042018-10-18 15:34:00 -0700565 mTimeStats.stats.clear();
566 mTimeStats.statsStart = (mEnabled.load() ? static_cast<int64_t>(std::time(0)) : 0);
567 mTimeStats.statsEnd = 0;
568 mTimeStats.totalFrames = 0;
569 mTimeStats.missedFrames = 0;
570 mTimeStats.clientCompositionFrames = 0;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700571 mTimeStats.displayOnTime = 0;
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700572 mTimeStats.presentToPresent.hist.clear();
Alec Mourifb571ea2019-01-24 18:42:10 -0800573 mTimeStats.refreshRateStats.clear();
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700574 mPowerTime.prevTime = systemTime();
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700575 mGlobalRecord.prevPresentTime = 0;
576 mGlobalRecord.presentFences.clear();
577 ALOGD("Cleared");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700578}
579
580bool TimeStats::isEnabled() {
581 return mEnabled.load();
582}
583
Yiwei Zhang5434a782018-12-05 18:06:32 -0800584void TimeStats::dump(bool asProto, std::optional<uint32_t> maxLayers, std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700585 ATRACE_CALL();
586
587 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700588 if (mTimeStats.statsStart == 0) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700589 return;
590 }
591
Yiwei Zhangdc224042018-10-18 15:34:00 -0700592 mTimeStats.statsEnd = static_cast<int64_t>(std::time(0));
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700593
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700594 flushPowerTimeLocked();
595
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700596 if (asProto) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700597 ALOGD("Dumping TimeStats as proto");
Yiwei Zhangdc224042018-10-18 15:34:00 -0700598 SFTimeStatsGlobalProto timeStatsProto = mTimeStats.toProto(maxLayers);
Dominik Laskowski46470112019-08-02 13:13:11 -0700599 result.append(timeStatsProto.SerializeAsString());
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700600 } else {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700601 ALOGD("Dumping TimeStats as text");
Yiwei Zhang5434a782018-12-05 18:06:32 -0800602 result.append(mTimeStats.toString(maxLayers));
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700603 result.append("\n");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700604 }
605}
606
Alec Mourifb571ea2019-01-24 18:42:10 -0800607} // namespace impl
608
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700609} // namespace android