blob: 4f59bf247cf161c2e3b3f239bc32a9fad2a3ebb2 [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 */
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080016
17// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
Yiwei Zhang0102ad22018-05-02 17:37:17 -070020#undef LOG_TAG
21#define LOG_TAG "TimeStats"
22#define ATRACE_TAG ATRACE_TAG_GRAPHICS
23
24#include "TimeStats.h"
25
26#include <android-base/stringprintf.h>
Alec Mouri37384342020-01-02 17:23:37 -080027#include <android/util/ProtoOutputStream.h>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070028#include <log/log.h>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070029#include <utils/String8.h>
Yiwei Zhang3a226d22018-10-16 09:23:03 -070030#include <utils/Timers.h>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070031#include <utils/Trace.h>
32
33#include <algorithm>
Alec Mouri9519bf12019-11-15 16:54:44 -080034#include <chrono>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070035
36namespace android {
37
Alec Mourifb571ea2019-01-24 18:42:10 -080038namespace impl {
39
Tej Singh2a457b62020-01-31 16:16:10 -080040AStatsManager_PullAtomCallbackReturn TimeStats::pullAtomCallback(int32_t atom_tag,
41 AStatsEventList* data,
42 void* cookie) {
Alec Mouri8e2f31b2020-01-16 22:04:35 +000043 impl::TimeStats* timeStats = reinterpret_cast<impl::TimeStats*>(cookie);
Tej Singh2a457b62020-01-31 16:16:10 -080044 AStatsManager_PullAtomCallbackReturn result = AStatsManager_PULL_SKIP;
Alec Mouri37384342020-01-02 17:23:37 -080045 if (atom_tag == android::util::SURFACEFLINGER_STATS_GLOBAL_INFO) {
Alec Mouri3ecd5cd2020-01-29 12:53:07 -080046 result = timeStats->populateGlobalAtom(data);
Alec Mouri37384342020-01-02 17:23:37 -080047 } else if (atom_tag == android::util::SURFACEFLINGER_STATS_LAYER_INFO) {
Alec Mouri3ecd5cd2020-01-29 12:53:07 -080048 result = timeStats->populateLayerAtom(data);
Alec Mouri8e2f31b2020-01-16 22:04:35 +000049 }
50
Alec Mouri3ecd5cd2020-01-29 12:53:07 -080051 // Enable timestats now. The first full pull for a given build is expected to
52 // have empty or very little stats, as stats are first enabled after the
53 // first pull is completed for either the global or layer stats.
54 timeStats->enable();
55 return result;
Alec Mouri37384342020-01-02 17:23:37 -080056}
Alec Mouri8e2f31b2020-01-16 22:04:35 +000057
Alec Mouri37384342020-01-02 17:23:37 -080058namespace {
59// Histograms align with the order of fields in SurfaceflingerStatsLayerInfo.
60const std::array<std::string, 6> kHistogramNames = {
61 "present2present", "post2present", "acquire2present",
62 "latch2present", "desired2present", "post2acquire",
63};
Alec Mouri8e2f31b2020-01-16 22:04:35 +000064
Alec Mouri37384342020-01-02 17:23:37 -080065std::string histogramToProtoByteString(const std::unordered_map<int32_t, int32_t>& histogram,
66 size_t maxPulledHistogramBuckets) {
67 auto buckets = std::vector<std::pair<int32_t, int32_t>>(histogram.begin(), histogram.end());
68 std::sort(buckets.begin(), buckets.end(),
69 [](std::pair<int32_t, int32_t>& left, std::pair<int32_t, int32_t>& right) {
70 return left.second > right.second;
71 });
72
73 util::ProtoOutputStream proto;
74 int histogramSize = 0;
75 for (const auto& bucket : buckets) {
76 if (++histogramSize > maxPulledHistogramBuckets) {
77 break;
78 }
79 proto.write(android::util::FIELD_TYPE_INT32 | android::util::FIELD_COUNT_REPEATED |
80 1 /* field id */,
81 (int32_t)bucket.first);
82 proto.write(android::util::FIELD_TYPE_INT64 | android::util::FIELD_COUNT_REPEATED |
83 2 /* field id */,
84 (int64_t)bucket.second);
85 }
86
87 std::string byteString;
88 proto.serializeToString(&byteString);
89 return byteString;
90}
91} // namespace
92
Alec Mouridfad9002020-02-12 17:49:09 -080093AStatsManager_PullAtomCallbackReturn TimeStats::populateGlobalAtom(AStatsEventList* data) {
94 std::lock_guard<std::mutex> lock(mMutex);
95
96 if (mTimeStats.statsStart == 0) {
97 return AStatsManager_PULL_SKIP;
98 }
99 flushPowerTimeLocked();
100
101 AStatsEvent* event = mStatsDelegate->addStatsEventToPullData(data);
102 mStatsDelegate->statsEventSetAtomId(event, android::util::SURFACEFLINGER_STATS_GLOBAL_INFO);
103 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.totalFrames);
104 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.missedFrames);
105 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.clientCompositionFrames);
106 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.displayOnTime);
107 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.presentToPresent.totalTime());
108 mStatsDelegate->statsEventWriteInt32(event, mTimeStats.displayEventConnectionsCount);
109 std::string frameDurationBytes =
110 histogramToProtoByteString(mTimeStats.frameDuration.hist, mMaxPulledHistogramBuckets);
111 mStatsDelegate->statsEventWriteByteArray(event, (const uint8_t*)frameDurationBytes.c_str(),
112 frameDurationBytes.size());
113 std::string renderEngineTimingBytes =
114 histogramToProtoByteString(mTimeStats.renderEngineTiming.hist,
115 mMaxPulledHistogramBuckets);
116 mStatsDelegate->statsEventWriteByteArray(event, (const uint8_t*)renderEngineTimingBytes.c_str(),
117 renderEngineTimingBytes.size());
118 mStatsDelegate->statsEventBuild(event);
119 clearGlobalLocked();
120
121 return AStatsManager_PULL_SUCCESS;
122}
123
Tej Singh2a457b62020-01-31 16:16:10 -0800124AStatsManager_PullAtomCallbackReturn TimeStats::populateLayerAtom(AStatsEventList* data) {
Alec Mouri37384342020-01-02 17:23:37 -0800125 std::lock_guard<std::mutex> lock(mMutex);
126
127 std::vector<TimeStatsHelper::TimeStatsLayer const*> dumpStats;
128 for (const auto& ele : mTimeStats.stats) {
129 dumpStats.push_back(&ele.second);
130 }
131
132 std::sort(dumpStats.begin(), dumpStats.end(),
133 [](TimeStatsHelper::TimeStatsLayer const* l,
134 TimeStatsHelper::TimeStatsLayer const* r) {
135 return l->totalFrames > r->totalFrames;
136 });
137
138 if (mMaxPulledLayers < dumpStats.size()) {
139 dumpStats.resize(mMaxPulledLayers);
140 }
141
142 for (const auto& layer : dumpStats) {
Tej Singh2a457b62020-01-31 16:16:10 -0800143 AStatsEvent* event = mStatsDelegate->addStatsEventToPullData(data);
Alec Mouri37384342020-01-02 17:23:37 -0800144 mStatsDelegate->statsEventSetAtomId(event, android::util::SURFACEFLINGER_STATS_LAYER_INFO);
145 mStatsDelegate->statsEventWriteString8(event, layer->layerName.c_str());
146 mStatsDelegate->statsEventWriteInt64(event, layer->totalFrames);
147 mStatsDelegate->statsEventWriteInt64(event, layer->droppedFrames);
148
149 for (const auto& name : kHistogramNames) {
150 const auto& histogram = layer->deltas.find(name);
151 if (histogram == layer->deltas.cend()) {
152 mStatsDelegate->statsEventWriteByteArray(event, nullptr, 0);
153 } else {
154 std::string bytes = histogramToProtoByteString(histogram->second.hist,
155 mMaxPulledHistogramBuckets);
156 mStatsDelegate->statsEventWriteByteArray(event, (const uint8_t*)bytes.c_str(),
157 bytes.size());
158 }
159 }
160
Yiwei Zhang7bfc75b2020-02-10 11:20:34 -0800161 mStatsDelegate->statsEventWriteInt64(event, layer->lateAcquireFrames);
162 mStatsDelegate->statsEventWriteInt64(event, layer->badDesiredPresentFrames);
163
Alec Mouri37384342020-01-02 17:23:37 -0800164 mStatsDelegate->statsEventBuild(event);
165 }
166 clearLayersLocked();
167
Tej Singh2a457b62020-01-31 16:16:10 -0800168 return AStatsManager_PULL_SUCCESS;
Alec Mouri37384342020-01-02 17:23:37 -0800169}
170
171TimeStats::TimeStats() : TimeStats(nullptr, std::nullopt, std::nullopt) {}
172
173TimeStats::TimeStats(std::unique_ptr<StatsEventDelegate> statsDelegate,
174 std::optional<size_t> maxPulledLayers,
175 std::optional<size_t> maxPulledHistogramBuckets) {
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000176 if (statsDelegate != nullptr) {
177 mStatsDelegate = std::move(statsDelegate);
178 }
Alec Mouri37384342020-01-02 17:23:37 -0800179
180 if (maxPulledLayers) {
181 mMaxPulledLayers = *maxPulledLayers;
182 }
183
184 if (maxPulledHistogramBuckets) {
185 mMaxPulledHistogramBuckets = *maxPulledHistogramBuckets;
186 }
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000187}
188
Alec Mouri3ecd5cd2020-01-29 12:53:07 -0800189TimeStats::~TimeStats() {
190 std::lock_guard<std::mutex> lock(mMutex);
Tej Singh38a4b212020-03-13 19:04:51 -0700191 mStatsDelegate->clearStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_GLOBAL_INFO);
192 mStatsDelegate->clearStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_LAYER_INFO);
Alec Mouri3ecd5cd2020-01-29 12:53:07 -0800193}
194
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000195void TimeStats::onBootFinished() {
Alec Mouri3ecd5cd2020-01-29 12:53:07 -0800196 std::lock_guard<std::mutex> lock(mMutex);
Tej Singh38a4b212020-03-13 19:04:51 -0700197 mStatsDelegate->setStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_GLOBAL_INFO,
198 nullptr, TimeStats::pullAtomCallback, this);
199 mStatsDelegate->setStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_LAYER_INFO,
200 nullptr, TimeStats::pullAtomCallback, this);
Alec Mourib3885ad2019-09-06 17:08:55 -0700201}
202
Dominik Laskowskic2867142019-01-21 11:33:38 -0800203void TimeStats::parseArgs(bool asProto, const Vector<String16>& args, std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700204 ATRACE_CALL();
205
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700206 std::unordered_map<std::string, int32_t> argsMap;
Dominik Laskowskic2867142019-01-21 11:33:38 -0800207 for (size_t index = 0; index < args.size(); ++index) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700208 argsMap[std::string(String8(args[index]).c_str())] = index;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700209 }
210
211 if (argsMap.count("-disable")) {
212 disable();
213 }
214
215 if (argsMap.count("-dump")) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700216 std::optional<uint32_t> maxLayers = std::nullopt;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700217 auto iter = argsMap.find("-maxlayers");
218 if (iter != argsMap.end() && iter->second + 1 < static_cast<int32_t>(args.size())) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700219 int64_t value = strtol(String8(args[iter->second + 1]).c_str(), nullptr, 10);
220 value = std::clamp(value, int64_t(0), int64_t(UINT32_MAX));
221 maxLayers = static_cast<uint32_t>(value);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700222 }
223
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700224 dump(asProto, maxLayers, result);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700225 }
226
227 if (argsMap.count("-clear")) {
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000228 clearAll();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700229 }
230
231 if (argsMap.count("-enable")) {
232 enable();
233 }
234}
235
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700236std::string TimeStats::miniDump() {
237 ATRACE_CALL();
238
239 std::string result = "TimeStats miniDump:\n";
240 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange926ab52019-08-14 15:16:00 -0700241 android::base::StringAppendF(&result, "Number of layers currently being tracked is %zu\n",
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700242 mTimeStatsTracker.size());
Yiwei Zhange926ab52019-08-14 15:16:00 -0700243 android::base::StringAppendF(&result, "Number of layers in the stats pool is %zu\n",
244 mTimeStats.stats.size());
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700245 return result;
246}
247
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700248void TimeStats::incrementTotalFrames() {
249 if (!mEnabled.load()) return;
250
251 ATRACE_CALL();
252
253 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700254 mTimeStats.totalFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700255}
256
Yiwei Zhang621f9d42018-05-07 10:40:55 -0700257void TimeStats::incrementMissedFrames() {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700258 if (!mEnabled.load()) return;
259
260 ATRACE_CALL();
261
262 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700263 mTimeStats.missedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700264}
265
266void TimeStats::incrementClientCompositionFrames() {
267 if (!mEnabled.load()) return;
268
269 ATRACE_CALL();
270
271 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700272 mTimeStats.clientCompositionFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700273}
274
Vishnu Nair9b079a22020-01-21 14:36:08 -0800275void TimeStats::incrementClientCompositionReusedFrames() {
276 if (!mEnabled.load()) return;
277
278 ATRACE_CALL();
279
280 std::lock_guard<std::mutex> lock(mMutex);
281 mTimeStats.clientCompositionReusedFrames++;
282}
283
Alec Mouri717bcb62020-02-10 17:07:19 -0800284void TimeStats::recordDisplayEventConnectionCount(int32_t count) {
285 if (!mEnabled.load()) return;
286
287 ATRACE_CALL();
288
289 std::lock_guard<std::mutex> lock(mMutex);
290 mTimeStats.displayEventConnectionsCount =
291 std::max(mTimeStats.displayEventConnectionsCount, count);
292}
293
Alec Mouri9519bf12019-11-15 16:54:44 -0800294static int32_t msBetween(nsecs_t start, nsecs_t end) {
295 int64_t delta = std::chrono::duration_cast<std::chrono::milliseconds>(
296 std::chrono::nanoseconds(end - start))
297 .count();
298 delta = std::clamp(delta, int64_t(INT32_MIN), int64_t(INT32_MAX));
299 return static_cast<int32_t>(delta);
300}
301
302void TimeStats::recordFrameDuration(nsecs_t startTime, nsecs_t endTime) {
303 if (!mEnabled.load()) return;
304
305 std::lock_guard<std::mutex> lock(mMutex);
306 if (mPowerTime.powerMode == HWC_POWER_MODE_NORMAL) {
307 mTimeStats.frameDuration.insert(msBetween(startTime, endTime));
308 }
309}
310
Alec Mourie4034bb2019-11-19 12:45:54 -0800311void TimeStats::recordRenderEngineDuration(nsecs_t startTime, nsecs_t endTime) {
312 if (!mEnabled.load()) return;
313
314 std::lock_guard<std::mutex> lock(mMutex);
315 if (mGlobalRecord.renderEngineDurations.size() == MAX_NUM_TIME_RECORDS) {
316 ALOGE("RenderEngineTimes are already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
317 mGlobalRecord.renderEngineDurations.pop_front();
318 }
319 mGlobalRecord.renderEngineDurations.push_back({startTime, endTime});
320}
321
322void TimeStats::recordRenderEngineDuration(nsecs_t startTime,
323 const std::shared_ptr<FenceTime>& endTime) {
324 if (!mEnabled.load()) return;
325
326 std::lock_guard<std::mutex> lock(mMutex);
327 if (mGlobalRecord.renderEngineDurations.size() == MAX_NUM_TIME_RECORDS) {
328 ALOGE("RenderEngineTimes are already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
329 mGlobalRecord.renderEngineDurations.pop_front();
330 }
331 mGlobalRecord.renderEngineDurations.push_back({startTime, endTime});
332}
333
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800334bool TimeStats::recordReadyLocked(int32_t layerId, TimeRecord* timeRecord) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700335 if (!timeRecord->ready) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800336 ALOGV("[%d]-[%" PRIu64 "]-presentFence is still not received", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700337 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700338 return false;
339 }
340
341 if (timeRecord->acquireFence != nullptr) {
342 if (timeRecord->acquireFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
343 return false;
344 }
345 if (timeRecord->acquireFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700346 timeRecord->frameTime.acquireTime = timeRecord->acquireFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700347 timeRecord->acquireFence = nullptr;
348 } else {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800349 ALOGV("[%d]-[%" PRIu64 "]-acquireFence signal time is invalid", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700350 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700351 }
352 }
353
354 if (timeRecord->presentFence != nullptr) {
355 if (timeRecord->presentFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
356 return false;
357 }
358 if (timeRecord->presentFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700359 timeRecord->frameTime.presentTime = timeRecord->presentFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700360 timeRecord->presentFence = nullptr;
361 } else {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800362 ALOGV("[%d]-[%" PRIu64 "]-presentFence signal time invalid", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700363 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700364 }
365 }
366
367 return true;
368}
369
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800370void TimeStats::flushAvailableRecordsToStatsLocked(int32_t layerId) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700371 ATRACE_CALL();
372
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800373 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700374 TimeRecord& prevTimeRecord = layerRecord.prevTimeRecord;
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700375 std::deque<TimeRecord>& timeRecords = layerRecord.timeRecords;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700376 while (!timeRecords.empty()) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800377 if (!recordReadyLocked(layerId, &timeRecords[0])) break;
378 ALOGV("[%d]-[%" PRIu64 "]-presentFenceTime[%" PRId64 "]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700379 timeRecords[0].frameTime.frameNumber, timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700380
381 if (prevTimeRecord.ready) {
Yiwei Zhangeafa5cc2019-07-26 15:06:25 -0700382 const std::string& layerName = layerRecord.layerName;
Yiwei Zhangdc224042018-10-18 15:34:00 -0700383 if (!mTimeStats.stats.count(layerName)) {
384 mTimeStats.stats[layerName].layerName = layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700385 }
Yiwei Zhangdc224042018-10-18 15:34:00 -0700386 TimeStatsHelper::TimeStatsLayer& timeStatsLayer = mTimeStats.stats[layerName];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700387 timeStatsLayer.totalFrames++;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700388 timeStatsLayer.droppedFrames += layerRecord.droppedFrames;
Alec Mouri91f6df32020-01-30 08:48:58 -0800389 timeStatsLayer.lateAcquireFrames += layerRecord.lateAcquireFrames;
390 timeStatsLayer.badDesiredPresentFrames += layerRecord.badDesiredPresentFrames;
391
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700392 layerRecord.droppedFrames = 0;
Alec Mouri91f6df32020-01-30 08:48:58 -0800393 layerRecord.lateAcquireFrames = 0;
394 layerRecord.badDesiredPresentFrames = 0;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700395
396 const int32_t postToAcquireMs = msBetween(timeRecords[0].frameTime.postTime,
397 timeRecords[0].frameTime.acquireTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800398 ALOGV("[%d]-[%" PRIu64 "]-post2acquire[%d]", layerId,
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700399 timeRecords[0].frameTime.frameNumber, postToAcquireMs);
400 timeStatsLayer.deltas["post2acquire"].insert(postToAcquireMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700401
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700402 const int32_t postToPresentMs = msBetween(timeRecords[0].frameTime.postTime,
403 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800404 ALOGV("[%d]-[%" PRIu64 "]-post2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700405 timeRecords[0].frameTime.frameNumber, postToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700406 timeStatsLayer.deltas["post2present"].insert(postToPresentMs);
407
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700408 const int32_t acquireToPresentMs = msBetween(timeRecords[0].frameTime.acquireTime,
409 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800410 ALOGV("[%d]-[%" PRIu64 "]-acquire2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700411 timeRecords[0].frameTime.frameNumber, acquireToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700412 timeStatsLayer.deltas["acquire2present"].insert(acquireToPresentMs);
413
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700414 const int32_t latchToPresentMs = msBetween(timeRecords[0].frameTime.latchTime,
415 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800416 ALOGV("[%d]-[%" PRIu64 "]-latch2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700417 timeRecords[0].frameTime.frameNumber, latchToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700418 timeStatsLayer.deltas["latch2present"].insert(latchToPresentMs);
419
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700420 const int32_t desiredToPresentMs = msBetween(timeRecords[0].frameTime.desiredTime,
421 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800422 ALOGV("[%d]-[%" PRIu64 "]-desired2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700423 timeRecords[0].frameTime.frameNumber, desiredToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700424 timeStatsLayer.deltas["desired2present"].insert(desiredToPresentMs);
425
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700426 const int32_t presentToPresentMs = msBetween(prevTimeRecord.frameTime.presentTime,
427 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800428 ALOGV("[%d]-[%" PRIu64 "]-present2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700429 timeRecords[0].frameTime.frameNumber, presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700430 timeStatsLayer.deltas["present2present"].insert(presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700431 }
432 prevTimeRecord = timeRecords[0];
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700433 timeRecords.pop_front();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700434 layerRecord.waitData--;
435 }
436}
437
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700438static constexpr const char* kPopupWindowPrefix = "PopupWindow";
439static const size_t kMinLenLayerName = std::strlen(kPopupWindowPrefix);
Yiwei Zhangbd408322018-10-15 18:31:53 -0700440
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700441// Avoid tracking the "PopupWindow:<random hash>#<number>" layers
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700442static bool layerNameIsValid(const std::string& layerName) {
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700443 return layerName.length() >= kMinLenLayerName &&
444 layerName.compare(0, kMinLenLayerName, kPopupWindowPrefix) != 0;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700445}
446
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800447void TimeStats::setPostTime(int32_t layerId, uint64_t frameNumber, const std::string& layerName,
Yiwei Zhang8e8fe522018-11-02 18:34:07 -0700448 nsecs_t postTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700449 if (!mEnabled.load()) return;
450
451 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800452 ALOGV("[%d]-[%" PRIu64 "]-[%s]-PostTime[%" PRId64 "]", layerId, frameNumber, layerName.c_str(),
Yiwei Zhang8e8fe522018-11-02 18:34:07 -0700453 postTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700454
455 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange926ab52019-08-14 15:16:00 -0700456 if (!mTimeStats.stats.count(layerName) && mTimeStats.stats.size() >= MAX_NUM_LAYER_STATS) {
457 return;
458 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800459 if (!mTimeStatsTracker.count(layerId) && mTimeStatsTracker.size() < MAX_NUM_LAYER_RECORDS &&
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700460 layerNameIsValid(layerName)) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800461 mTimeStatsTracker[layerId].layerName = layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700462 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800463 if (!mTimeStatsTracker.count(layerId)) return;
464 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700465 if (layerRecord.timeRecords.size() == MAX_NUM_TIME_RECORDS) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800466 ALOGE("[%d]-[%s]-timeRecords is at its maximum size[%zu]. Ignore this when unittesting.",
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800467 layerId, layerRecord.layerName.c_str(), MAX_NUM_TIME_RECORDS);
468 mTimeStatsTracker.erase(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700469 return;
470 }
471 // For most media content, the acquireFence is invalid because the buffer is
472 // ready at the queueBuffer stage. In this case, acquireTime should be given
473 // a default value as postTime.
474 TimeRecord timeRecord = {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700475 .frameTime =
476 {
477 .frameNumber = frameNumber,
478 .postTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800479 .latchTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700480 .acquireTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800481 .desiredTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700482 },
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700483 };
484 layerRecord.timeRecords.push_back(timeRecord);
485 if (layerRecord.waitData < 0 ||
486 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
487 layerRecord.waitData = layerRecord.timeRecords.size() - 1;
488}
489
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800490void TimeStats::setLatchTime(int32_t layerId, uint64_t frameNumber, nsecs_t latchTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700491 if (!mEnabled.load()) return;
492
493 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800494 ALOGV("[%d]-[%" PRIu64 "]-LatchTime[%" PRId64 "]", layerId, frameNumber, latchTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700495
496 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800497 if (!mTimeStatsTracker.count(layerId)) return;
498 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700499 if (layerRecord.waitData < 0 ||
500 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
501 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700502 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700503 if (timeRecord.frameTime.frameNumber == frameNumber) {
504 timeRecord.frameTime.latchTime = latchTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700505 }
506}
507
Alec Mouri91f6df32020-01-30 08:48:58 -0800508void TimeStats::incrementLatchSkipped(int32_t layerId, LatchSkipReason reason) {
509 if (!mEnabled.load()) return;
510
511 ATRACE_CALL();
512 ALOGV("[%d]-LatchSkipped-Reason[%d]", layerId,
513 static_cast<std::underlying_type<LatchSkipReason>::type>(reason));
514
515 std::lock_guard<std::mutex> lock(mMutex);
516 if (!mTimeStatsTracker.count(layerId)) return;
517 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
518
519 switch (reason) {
520 case LatchSkipReason::LateAcquire:
521 layerRecord.lateAcquireFrames++;
522 break;
523 }
524}
525
526void TimeStats::incrementBadDesiredPresent(int32_t layerId) {
527 if (!mEnabled.load()) return;
528
529 ATRACE_CALL();
530 ALOGV("[%d]-BadDesiredPresent", layerId);
531
532 std::lock_guard<std::mutex> lock(mMutex);
533 if (!mTimeStatsTracker.count(layerId)) return;
534 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
535 layerRecord.badDesiredPresentFrames++;
536}
537
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800538void TimeStats::setDesiredTime(int32_t layerId, uint64_t frameNumber, nsecs_t desiredTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700539 if (!mEnabled.load()) return;
540
541 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800542 ALOGV("[%d]-[%" PRIu64 "]-DesiredTime[%" PRId64 "]", layerId, frameNumber, desiredTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700543
544 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800545 if (!mTimeStatsTracker.count(layerId)) return;
546 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700547 if (layerRecord.waitData < 0 ||
548 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
549 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700550 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700551 if (timeRecord.frameTime.frameNumber == frameNumber) {
552 timeRecord.frameTime.desiredTime = desiredTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700553 }
554}
555
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800556void TimeStats::setAcquireTime(int32_t layerId, uint64_t frameNumber, nsecs_t acquireTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700557 if (!mEnabled.load()) return;
558
559 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800560 ALOGV("[%d]-[%" PRIu64 "]-AcquireTime[%" PRId64 "]", layerId, frameNumber, acquireTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700561
562 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800563 if (!mTimeStatsTracker.count(layerId)) return;
564 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700565 if (layerRecord.waitData < 0 ||
566 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
567 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700568 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700569 if (timeRecord.frameTime.frameNumber == frameNumber) {
570 timeRecord.frameTime.acquireTime = acquireTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700571 }
572}
573
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800574void TimeStats::setAcquireFence(int32_t layerId, uint64_t frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700575 const std::shared_ptr<FenceTime>& acquireFence) {
576 if (!mEnabled.load()) return;
577
578 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800579 ALOGV("[%d]-[%" PRIu64 "]-AcquireFenceTime[%" PRId64 "]", layerId, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700580 acquireFence->getSignalTime());
581
582 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800583 if (!mTimeStatsTracker.count(layerId)) return;
584 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700585 if (layerRecord.waitData < 0 ||
586 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
587 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700588 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700589 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700590 timeRecord.acquireFence = acquireFence;
591 }
592}
593
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800594void TimeStats::setPresentTime(int32_t layerId, uint64_t frameNumber, nsecs_t presentTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700595 if (!mEnabled.load()) return;
596
597 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800598 ALOGV("[%d]-[%" PRIu64 "]-PresentTime[%" PRId64 "]", layerId, frameNumber, presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700599
600 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800601 if (!mTimeStatsTracker.count(layerId)) return;
602 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700603 if (layerRecord.waitData < 0 ||
604 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
605 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700606 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700607 if (timeRecord.frameTime.frameNumber == frameNumber) {
608 timeRecord.frameTime.presentTime = presentTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700609 timeRecord.ready = true;
610 layerRecord.waitData++;
611 }
612
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800613 flushAvailableRecordsToStatsLocked(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700614}
615
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800616void TimeStats::setPresentFence(int32_t layerId, uint64_t frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700617 const std::shared_ptr<FenceTime>& presentFence) {
618 if (!mEnabled.load()) return;
619
620 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800621 ALOGV("[%d]-[%" PRIu64 "]-PresentFenceTime[%" PRId64 "]", layerId, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700622 presentFence->getSignalTime());
623
624 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800625 if (!mTimeStatsTracker.count(layerId)) return;
626 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700627 if (layerRecord.waitData < 0 ||
628 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
629 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700630 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700631 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700632 timeRecord.presentFence = presentFence;
633 timeRecord.ready = true;
634 layerRecord.waitData++;
635 }
636
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800637 flushAvailableRecordsToStatsLocked(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700638}
639
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800640void TimeStats::onDestroy(int32_t layerId) {
Yiwei Zhangdc224042018-10-18 15:34:00 -0700641 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800642 ALOGV("[%d]-onDestroy", layerId);
Mikael Pessa90092f42019-08-26 17:22:04 -0700643 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800644 mTimeStatsTracker.erase(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700645}
646
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800647void TimeStats::removeTimeRecord(int32_t layerId, uint64_t frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700648 if (!mEnabled.load()) return;
649
650 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800651 ALOGV("[%d]-[%" PRIu64 "]-removeTimeRecord", layerId, frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700652
653 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800654 if (!mTimeStatsTracker.count(layerId)) return;
655 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700656 size_t removeAt = 0;
657 for (const TimeRecord& record : layerRecord.timeRecords) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700658 if (record.frameTime.frameNumber == frameNumber) break;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700659 removeAt++;
660 }
661 if (removeAt == layerRecord.timeRecords.size()) return;
662 layerRecord.timeRecords.erase(layerRecord.timeRecords.begin() + removeAt);
663 if (layerRecord.waitData > static_cast<int32_t>(removeAt)) {
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700664 layerRecord.waitData--;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700665 }
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700666 layerRecord.droppedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700667}
668
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700669void TimeStats::flushPowerTimeLocked() {
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700670 if (!mEnabled.load()) return;
671
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700672 nsecs_t curTime = systemTime();
673 // elapsedTime is in milliseconds.
674 int64_t elapsedTime = (curTime - mPowerTime.prevTime) / 1000000;
675
676 switch (mPowerTime.powerMode) {
677 case HWC_POWER_MODE_NORMAL:
678 mTimeStats.displayOnTime += elapsedTime;
679 break;
680 case HWC_POWER_MODE_OFF:
681 case HWC_POWER_MODE_DOZE:
682 case HWC_POWER_MODE_DOZE_SUSPEND:
683 default:
684 break;
685 }
686
687 mPowerTime.prevTime = curTime;
688}
689
690void TimeStats::setPowerMode(int32_t powerMode) {
691 if (!mEnabled.load()) {
692 std::lock_guard<std::mutex> lock(mMutex);
693 mPowerTime.powerMode = powerMode;
694 return;
695 }
696
697 std::lock_guard<std::mutex> lock(mMutex);
698 if (powerMode == mPowerTime.powerMode) return;
699
700 flushPowerTimeLocked();
701 mPowerTime.powerMode = powerMode;
702}
703
Alec Mourifb571ea2019-01-24 18:42:10 -0800704void TimeStats::recordRefreshRate(uint32_t fps, nsecs_t duration) {
705 std::lock_guard<std::mutex> lock(mMutex);
706 if (mTimeStats.refreshRateStats.count(fps)) {
707 mTimeStats.refreshRateStats[fps] += duration;
708 } else {
709 mTimeStats.refreshRateStats.insert({fps, duration});
710 }
711}
712
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700713void TimeStats::flushAvailableGlobalRecordsToStatsLocked() {
714 ATRACE_CALL();
715
716 while (!mGlobalRecord.presentFences.empty()) {
717 const nsecs_t curPresentTime = mGlobalRecord.presentFences.front()->getSignalTime();
718 if (curPresentTime == Fence::SIGNAL_TIME_PENDING) break;
719
720 if (curPresentTime == Fence::SIGNAL_TIME_INVALID) {
721 ALOGE("GlobalPresentFence is invalid!");
722 mGlobalRecord.prevPresentTime = 0;
723 mGlobalRecord.presentFences.pop_front();
724 continue;
725 }
726
727 ALOGV("GlobalPresentFenceTime[%" PRId64 "]",
728 mGlobalRecord.presentFences.front()->getSignalTime());
729
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700730 if (mGlobalRecord.prevPresentTime != 0) {
731 const int32_t presentToPresentMs =
732 msBetween(mGlobalRecord.prevPresentTime, curPresentTime);
733 ALOGV("Global present2present[%d] prev[%" PRId64 "] curr[%" PRId64 "]",
734 presentToPresentMs, mGlobalRecord.prevPresentTime, curPresentTime);
735 mTimeStats.presentToPresent.insert(presentToPresentMs);
736 }
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700737
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700738 mGlobalRecord.prevPresentTime = curPresentTime;
739 mGlobalRecord.presentFences.pop_front();
740 }
Alec Mourie4034bb2019-11-19 12:45:54 -0800741 while (!mGlobalRecord.renderEngineDurations.empty()) {
742 const auto duration = mGlobalRecord.renderEngineDurations.front();
743 const auto& endTime = duration.endTime;
744
745 nsecs_t endNs = -1;
746
747 if (auto val = std::get_if<nsecs_t>(&endTime)) {
748 endNs = *val;
749 } else {
750 endNs = std::get<std::shared_ptr<FenceTime>>(endTime)->getSignalTime();
751 }
752
753 if (endNs == Fence::SIGNAL_TIME_PENDING) break;
754
755 if (endNs < 0) {
756 ALOGE("RenderEngineTiming is invalid!");
757 mGlobalRecord.renderEngineDurations.pop_front();
758 continue;
759 }
760
761 const int32_t renderEngineMs = msBetween(duration.startTime, endNs);
762 mTimeStats.renderEngineTiming.insert(renderEngineMs);
763
764 mGlobalRecord.renderEngineDurations.pop_front();
765 }
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700766}
767
768void TimeStats::setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) {
769 if (!mEnabled.load()) return;
770
771 ATRACE_CALL();
772 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700773 if (presentFence == nullptr || !presentFence->isValid()) {
774 mGlobalRecord.prevPresentTime = 0;
775 return;
776 }
777
778 if (mPowerTime.powerMode != HWC_POWER_MODE_NORMAL) {
779 // Try flushing the last present fence on HWC_POWER_MODE_NORMAL.
780 flushAvailableGlobalRecordsToStatsLocked();
781 mGlobalRecord.presentFences.clear();
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700782 mGlobalRecord.prevPresentTime = 0;
783 return;
784 }
785
786 if (mGlobalRecord.presentFences.size() == MAX_NUM_TIME_RECORDS) {
787 // The front presentFence must be trapped in pending status in this
788 // case. Try dequeuing the front one to recover.
789 ALOGE("GlobalPresentFences is already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
790 mGlobalRecord.prevPresentTime = 0;
791 mGlobalRecord.presentFences.pop_front();
792 }
793
794 mGlobalRecord.presentFences.emplace_back(presentFence);
795 flushAvailableGlobalRecordsToStatsLocked();
796}
797
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700798void TimeStats::enable() {
799 if (mEnabled.load()) return;
800
801 ATRACE_CALL();
802
803 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700804 mEnabled.store(true);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700805 mTimeStats.statsStart = static_cast<int64_t>(std::time(0));
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700806 mPowerTime.prevTime = systemTime();
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700807 ALOGD("Enabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700808}
809
810void TimeStats::disable() {
811 if (!mEnabled.load()) return;
812
813 ATRACE_CALL();
814
815 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700816 flushPowerTimeLocked();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700817 mEnabled.store(false);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700818 mTimeStats.statsEnd = static_cast<int64_t>(std::time(0));
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700819 ALOGD("Disabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700820}
821
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000822void TimeStats::clearAll() {
823 std::lock_guard<std::mutex> lock(mMutex);
824 clearGlobalLocked();
825 clearLayersLocked();
826}
827
828void TimeStats::clearGlobalLocked() {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700829 ATRACE_CALL();
830
Yiwei Zhangdc224042018-10-18 15:34:00 -0700831 mTimeStats.statsStart = (mEnabled.load() ? static_cast<int64_t>(std::time(0)) : 0);
832 mTimeStats.statsEnd = 0;
833 mTimeStats.totalFrames = 0;
834 mTimeStats.missedFrames = 0;
835 mTimeStats.clientCompositionFrames = 0;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800836 mTimeStats.clientCompositionReusedFrames = 0;
Alec Mouri717bcb62020-02-10 17:07:19 -0800837 mTimeStats.displayEventConnectionsCount = 0;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700838 mTimeStats.displayOnTime = 0;
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700839 mTimeStats.presentToPresent.hist.clear();
Alec Mouri31ac64a2020-01-09 09:26:22 -0800840 mTimeStats.frameDuration.hist.clear();
841 mTimeStats.renderEngineTiming.hist.clear();
Alec Mourifb571ea2019-01-24 18:42:10 -0800842 mTimeStats.refreshRateStats.clear();
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700843 mPowerTime.prevTime = systemTime();
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700844 mGlobalRecord.prevPresentTime = 0;
845 mGlobalRecord.presentFences.clear();
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000846 ALOGD("Cleared global stats");
847}
848
849void TimeStats::clearLayersLocked() {
850 ATRACE_CALL();
851
852 mTimeStatsTracker.clear();
853 mTimeStats.stats.clear();
854 ALOGD("Cleared layer stats");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700855}
856
857bool TimeStats::isEnabled() {
858 return mEnabled.load();
859}
860
Yiwei Zhang5434a782018-12-05 18:06:32 -0800861void TimeStats::dump(bool asProto, std::optional<uint32_t> maxLayers, std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700862 ATRACE_CALL();
863
864 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700865 if (mTimeStats.statsStart == 0) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700866 return;
867 }
868
Yiwei Zhangdc224042018-10-18 15:34:00 -0700869 mTimeStats.statsEnd = static_cast<int64_t>(std::time(0));
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700870
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700871 flushPowerTimeLocked();
872
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700873 if (asProto) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700874 ALOGD("Dumping TimeStats as proto");
Yiwei Zhangdc224042018-10-18 15:34:00 -0700875 SFTimeStatsGlobalProto timeStatsProto = mTimeStats.toProto(maxLayers);
Dominik Laskowski46470112019-08-02 13:13:11 -0700876 result.append(timeStatsProto.SerializeAsString());
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700877 } else {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700878 ALOGD("Dumping TimeStats as text");
Yiwei Zhang5434a782018-12-05 18:06:32 -0800879 result.append(mTimeStats.toString(maxLayers));
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700880 result.append("\n");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700881 }
882}
883
Alec Mourifb571ea2019-01-24 18:42:10 -0800884} // namespace impl
885
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700886} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800887
888// TODO(b/129481165): remove the #pragma below and fix conversion issues
889#pragma clang diagnostic pop // ignored "-Wconversion"