blob: fdf8a41840f86b49b39b6b29be3515701300fe91 [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
Alec Mouri37384342020-01-02 17:23:37 -080040status_pull_atom_return_t TimeStats::pullAtomCallback(int32_t atom_tag,
41 pulled_stats_event_list* data, void* cookie) {
Alec Mouri8e2f31b2020-01-16 22:04:35 +000042 impl::TimeStats* timeStats = reinterpret_cast<impl::TimeStats*>(cookie);
Alec Mouri3ecd5cd2020-01-29 12:53:07 -080043 status_pull_atom_return_t result = STATS_PULL_SKIP;
Alec Mouri37384342020-01-02 17:23:37 -080044 if (atom_tag == android::util::SURFACEFLINGER_STATS_GLOBAL_INFO) {
Alec Mouri3ecd5cd2020-01-29 12:53:07 -080045 result = timeStats->populateGlobalAtom(data);
Alec Mouri37384342020-01-02 17:23:37 -080046 } else if (atom_tag == android::util::SURFACEFLINGER_STATS_LAYER_INFO) {
Alec Mouri3ecd5cd2020-01-29 12:53:07 -080047 result = timeStats->populateLayerAtom(data);
Alec Mouri8e2f31b2020-01-16 22:04:35 +000048 }
49
Alec Mouri3ecd5cd2020-01-29 12:53:07 -080050 // Enable timestats now. The first full pull for a given build is expected to
51 // have empty or very little stats, as stats are first enabled after the
52 // first pull is completed for either the global or layer stats.
53 timeStats->enable();
54 return result;
Alec Mouri37384342020-01-02 17:23:37 -080055}
Alec Mouri8e2f31b2020-01-16 22:04:35 +000056
Alec Mouri37384342020-01-02 17:23:37 -080057status_pull_atom_return_t TimeStats::populateGlobalAtom(pulled_stats_event_list* data) {
58 std::lock_guard<std::mutex> lock(mMutex);
59
60 if (mTimeStats.statsStart == 0) {
Alec Mouri8e2f31b2020-01-16 22:04:35 +000061 return STATS_PULL_SKIP;
62 }
Alec Mouri37384342020-01-02 17:23:37 -080063 flushPowerTimeLocked();
Alec Mouri8e2f31b2020-01-16 22:04:35 +000064
Alec Mouri37384342020-01-02 17:23:37 -080065 struct stats_event* event = mStatsDelegate->addStatsEventToPullData(data);
66 mStatsDelegate->statsEventSetAtomId(event, android::util::SURFACEFLINGER_STATS_GLOBAL_INFO);
67 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.totalFrames);
68 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.missedFrames);
69 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.clientCompositionFrames);
70 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.displayOnTime);
71 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.presentToPresent.totalTime());
72 mStatsDelegate->statsEventBuild(event);
73 clearGlobalLocked();
Alec Mouri8e2f31b2020-01-16 22:04:35 +000074
75 return STATS_PULL_SUCCESS;
76}
77
Alec Mouri37384342020-01-02 17:23:37 -080078namespace {
79// Histograms align with the order of fields in SurfaceflingerStatsLayerInfo.
80const std::array<std::string, 6> kHistogramNames = {
81 "present2present", "post2present", "acquire2present",
82 "latch2present", "desired2present", "post2acquire",
83};
Alec Mouri8e2f31b2020-01-16 22:04:35 +000084
Alec Mouri37384342020-01-02 17:23:37 -080085std::string histogramToProtoByteString(const std::unordered_map<int32_t, int32_t>& histogram,
86 size_t maxPulledHistogramBuckets) {
87 auto buckets = std::vector<std::pair<int32_t, int32_t>>(histogram.begin(), histogram.end());
88 std::sort(buckets.begin(), buckets.end(),
89 [](std::pair<int32_t, int32_t>& left, std::pair<int32_t, int32_t>& right) {
90 return left.second > right.second;
91 });
92
93 util::ProtoOutputStream proto;
94 int histogramSize = 0;
95 for (const auto& bucket : buckets) {
96 if (++histogramSize > maxPulledHistogramBuckets) {
97 break;
98 }
99 proto.write(android::util::FIELD_TYPE_INT32 | android::util::FIELD_COUNT_REPEATED |
100 1 /* field id */,
101 (int32_t)bucket.first);
102 proto.write(android::util::FIELD_TYPE_INT64 | android::util::FIELD_COUNT_REPEATED |
103 2 /* field id */,
104 (int64_t)bucket.second);
105 }
106
107 std::string byteString;
108 proto.serializeToString(&byteString);
109 return byteString;
110}
111} // namespace
112
113status_pull_atom_return_t TimeStats::populateLayerAtom(pulled_stats_event_list* data) {
114 std::lock_guard<std::mutex> lock(mMutex);
115
116 std::vector<TimeStatsHelper::TimeStatsLayer const*> dumpStats;
117 for (const auto& ele : mTimeStats.stats) {
118 dumpStats.push_back(&ele.second);
119 }
120
121 std::sort(dumpStats.begin(), dumpStats.end(),
122 [](TimeStatsHelper::TimeStatsLayer const* l,
123 TimeStatsHelper::TimeStatsLayer const* r) {
124 return l->totalFrames > r->totalFrames;
125 });
126
127 if (mMaxPulledLayers < dumpStats.size()) {
128 dumpStats.resize(mMaxPulledLayers);
129 }
130
131 for (const auto& layer : dumpStats) {
132 struct stats_event* event = mStatsDelegate->addStatsEventToPullData(data);
133 mStatsDelegate->statsEventSetAtomId(event, android::util::SURFACEFLINGER_STATS_LAYER_INFO);
134 mStatsDelegate->statsEventWriteString8(event, layer->layerName.c_str());
135 mStatsDelegate->statsEventWriteInt64(event, layer->totalFrames);
136 mStatsDelegate->statsEventWriteInt64(event, layer->droppedFrames);
137
138 for (const auto& name : kHistogramNames) {
139 const auto& histogram = layer->deltas.find(name);
140 if (histogram == layer->deltas.cend()) {
141 mStatsDelegate->statsEventWriteByteArray(event, nullptr, 0);
142 } else {
143 std::string bytes = histogramToProtoByteString(histogram->second.hist,
144 mMaxPulledHistogramBuckets);
145 mStatsDelegate->statsEventWriteByteArray(event, (const uint8_t*)bytes.c_str(),
146 bytes.size());
147 }
148 }
149
150 mStatsDelegate->statsEventBuild(event);
151 }
152 clearLayersLocked();
153
154 return STATS_PULL_SUCCESS;
155}
156
157TimeStats::TimeStats() : TimeStats(nullptr, std::nullopt, std::nullopt) {}
158
159TimeStats::TimeStats(std::unique_ptr<StatsEventDelegate> statsDelegate,
160 std::optional<size_t> maxPulledLayers,
161 std::optional<size_t> maxPulledHistogramBuckets) {
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000162 if (statsDelegate != nullptr) {
163 mStatsDelegate = std::move(statsDelegate);
164 }
Alec Mouri37384342020-01-02 17:23:37 -0800165
166 if (maxPulledLayers) {
167 mMaxPulledLayers = *maxPulledLayers;
168 }
169
170 if (maxPulledHistogramBuckets) {
171 mMaxPulledHistogramBuckets = *maxPulledHistogramBuckets;
172 }
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000173}
174
Alec Mouri3ecd5cd2020-01-29 12:53:07 -0800175TimeStats::~TimeStats() {
176 std::lock_guard<std::mutex> lock(mMutex);
177 mStatsDelegate->unregisterStatsPullAtomCallback(
178 android::util::SURFACEFLINGER_STATS_GLOBAL_INFO);
179 mStatsDelegate->unregisterStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_LAYER_INFO);
180}
181
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000182void TimeStats::onBootFinished() {
Alec Mouri3ecd5cd2020-01-29 12:53:07 -0800183 std::lock_guard<std::mutex> lock(mMutex);
184 mStatsDelegate->registerStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_GLOBAL_INFO,
185 TimeStats::pullAtomCallback, nullptr, this);
186 mStatsDelegate->registerStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_LAYER_INFO,
187 TimeStats::pullAtomCallback, nullptr, this);
Alec Mourib3885ad2019-09-06 17:08:55 -0700188}
189
Dominik Laskowskic2867142019-01-21 11:33:38 -0800190void TimeStats::parseArgs(bool asProto, const Vector<String16>& args, std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700191 ATRACE_CALL();
192
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700193 std::unordered_map<std::string, int32_t> argsMap;
Dominik Laskowskic2867142019-01-21 11:33:38 -0800194 for (size_t index = 0; index < args.size(); ++index) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700195 argsMap[std::string(String8(args[index]).c_str())] = index;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700196 }
197
198 if (argsMap.count("-disable")) {
199 disable();
200 }
201
202 if (argsMap.count("-dump")) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700203 std::optional<uint32_t> maxLayers = std::nullopt;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700204 auto iter = argsMap.find("-maxlayers");
205 if (iter != argsMap.end() && iter->second + 1 < static_cast<int32_t>(args.size())) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700206 int64_t value = strtol(String8(args[iter->second + 1]).c_str(), nullptr, 10);
207 value = std::clamp(value, int64_t(0), int64_t(UINT32_MAX));
208 maxLayers = static_cast<uint32_t>(value);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700209 }
210
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700211 dump(asProto, maxLayers, result);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700212 }
213
214 if (argsMap.count("-clear")) {
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000215 clearAll();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700216 }
217
218 if (argsMap.count("-enable")) {
219 enable();
220 }
221}
222
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700223std::string TimeStats::miniDump() {
224 ATRACE_CALL();
225
226 std::string result = "TimeStats miniDump:\n";
227 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange926ab52019-08-14 15:16:00 -0700228 android::base::StringAppendF(&result, "Number of layers currently being tracked is %zu\n",
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700229 mTimeStatsTracker.size());
Yiwei Zhange926ab52019-08-14 15:16:00 -0700230 android::base::StringAppendF(&result, "Number of layers in the stats pool is %zu\n",
231 mTimeStats.stats.size());
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700232 return result;
233}
234
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700235void TimeStats::incrementTotalFrames() {
236 if (!mEnabled.load()) return;
237
238 ATRACE_CALL();
239
240 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700241 mTimeStats.totalFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700242}
243
Yiwei Zhang621f9d42018-05-07 10:40:55 -0700244void TimeStats::incrementMissedFrames() {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700245 if (!mEnabled.load()) return;
246
247 ATRACE_CALL();
248
249 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700250 mTimeStats.missedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700251}
252
253void TimeStats::incrementClientCompositionFrames() {
254 if (!mEnabled.load()) return;
255
256 ATRACE_CALL();
257
258 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700259 mTimeStats.clientCompositionFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700260}
261
Vishnu Nair9b079a22020-01-21 14:36:08 -0800262void TimeStats::incrementClientCompositionReusedFrames() {
263 if (!mEnabled.load()) return;
264
265 ATRACE_CALL();
266
267 std::lock_guard<std::mutex> lock(mMutex);
268 mTimeStats.clientCompositionReusedFrames++;
269}
270
Alec Mouri9519bf12019-11-15 16:54:44 -0800271static int32_t msBetween(nsecs_t start, nsecs_t end) {
272 int64_t delta = std::chrono::duration_cast<std::chrono::milliseconds>(
273 std::chrono::nanoseconds(end - start))
274 .count();
275 delta = std::clamp(delta, int64_t(INT32_MIN), int64_t(INT32_MAX));
276 return static_cast<int32_t>(delta);
277}
278
279void TimeStats::recordFrameDuration(nsecs_t startTime, nsecs_t endTime) {
280 if (!mEnabled.load()) return;
281
282 std::lock_guard<std::mutex> lock(mMutex);
283 if (mPowerTime.powerMode == HWC_POWER_MODE_NORMAL) {
284 mTimeStats.frameDuration.insert(msBetween(startTime, endTime));
285 }
286}
287
Alec Mourie4034bb2019-11-19 12:45:54 -0800288void TimeStats::recordRenderEngineDuration(nsecs_t startTime, nsecs_t endTime) {
289 if (!mEnabled.load()) return;
290
291 std::lock_guard<std::mutex> lock(mMutex);
292 if (mGlobalRecord.renderEngineDurations.size() == MAX_NUM_TIME_RECORDS) {
293 ALOGE("RenderEngineTimes are already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
294 mGlobalRecord.renderEngineDurations.pop_front();
295 }
296 mGlobalRecord.renderEngineDurations.push_back({startTime, endTime});
297}
298
299void TimeStats::recordRenderEngineDuration(nsecs_t startTime,
300 const std::shared_ptr<FenceTime>& endTime) {
301 if (!mEnabled.load()) return;
302
303 std::lock_guard<std::mutex> lock(mMutex);
304 if (mGlobalRecord.renderEngineDurations.size() == MAX_NUM_TIME_RECORDS) {
305 ALOGE("RenderEngineTimes are already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
306 mGlobalRecord.renderEngineDurations.pop_front();
307 }
308 mGlobalRecord.renderEngineDurations.push_back({startTime, endTime});
309}
310
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800311bool TimeStats::recordReadyLocked(int32_t layerId, TimeRecord* timeRecord) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700312 if (!timeRecord->ready) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800313 ALOGV("[%d]-[%" PRIu64 "]-presentFence is still not received", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700314 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700315 return false;
316 }
317
318 if (timeRecord->acquireFence != nullptr) {
319 if (timeRecord->acquireFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
320 return false;
321 }
322 if (timeRecord->acquireFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700323 timeRecord->frameTime.acquireTime = timeRecord->acquireFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700324 timeRecord->acquireFence = nullptr;
325 } else {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800326 ALOGV("[%d]-[%" PRIu64 "]-acquireFence signal time is invalid", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700327 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700328 }
329 }
330
331 if (timeRecord->presentFence != nullptr) {
332 if (timeRecord->presentFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
333 return false;
334 }
335 if (timeRecord->presentFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700336 timeRecord->frameTime.presentTime = timeRecord->presentFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700337 timeRecord->presentFence = nullptr;
338 } else {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800339 ALOGV("[%d]-[%" PRIu64 "]-presentFence signal time invalid", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700340 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700341 }
342 }
343
344 return true;
345}
346
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800347void TimeStats::flushAvailableRecordsToStatsLocked(int32_t layerId) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700348 ATRACE_CALL();
349
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800350 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700351 TimeRecord& prevTimeRecord = layerRecord.prevTimeRecord;
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700352 std::deque<TimeRecord>& timeRecords = layerRecord.timeRecords;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700353 while (!timeRecords.empty()) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800354 if (!recordReadyLocked(layerId, &timeRecords[0])) break;
355 ALOGV("[%d]-[%" PRIu64 "]-presentFenceTime[%" PRId64 "]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700356 timeRecords[0].frameTime.frameNumber, timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700357
358 if (prevTimeRecord.ready) {
Yiwei Zhangeafa5cc2019-07-26 15:06:25 -0700359 const std::string& layerName = layerRecord.layerName;
Yiwei Zhangdc224042018-10-18 15:34:00 -0700360 if (!mTimeStats.stats.count(layerName)) {
361 mTimeStats.stats[layerName].layerName = layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700362 }
Yiwei Zhangdc224042018-10-18 15:34:00 -0700363 TimeStatsHelper::TimeStatsLayer& timeStatsLayer = mTimeStats.stats[layerName];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700364 timeStatsLayer.totalFrames++;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700365 timeStatsLayer.droppedFrames += layerRecord.droppedFrames;
Alec Mouri91f6df32020-01-30 08:48:58 -0800366 timeStatsLayer.lateAcquireFrames += layerRecord.lateAcquireFrames;
367 timeStatsLayer.badDesiredPresentFrames += layerRecord.badDesiredPresentFrames;
368
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700369 layerRecord.droppedFrames = 0;
Alec Mouri91f6df32020-01-30 08:48:58 -0800370 layerRecord.lateAcquireFrames = 0;
371 layerRecord.badDesiredPresentFrames = 0;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700372
373 const int32_t postToAcquireMs = msBetween(timeRecords[0].frameTime.postTime,
374 timeRecords[0].frameTime.acquireTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800375 ALOGV("[%d]-[%" PRIu64 "]-post2acquire[%d]", layerId,
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700376 timeRecords[0].frameTime.frameNumber, postToAcquireMs);
377 timeStatsLayer.deltas["post2acquire"].insert(postToAcquireMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700378
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700379 const int32_t postToPresentMs = msBetween(timeRecords[0].frameTime.postTime,
380 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800381 ALOGV("[%d]-[%" PRIu64 "]-post2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700382 timeRecords[0].frameTime.frameNumber, postToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700383 timeStatsLayer.deltas["post2present"].insert(postToPresentMs);
384
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700385 const int32_t acquireToPresentMs = msBetween(timeRecords[0].frameTime.acquireTime,
386 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800387 ALOGV("[%d]-[%" PRIu64 "]-acquire2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700388 timeRecords[0].frameTime.frameNumber, acquireToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700389 timeStatsLayer.deltas["acquire2present"].insert(acquireToPresentMs);
390
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700391 const int32_t latchToPresentMs = msBetween(timeRecords[0].frameTime.latchTime,
392 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800393 ALOGV("[%d]-[%" PRIu64 "]-latch2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700394 timeRecords[0].frameTime.frameNumber, latchToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700395 timeStatsLayer.deltas["latch2present"].insert(latchToPresentMs);
396
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700397 const int32_t desiredToPresentMs = msBetween(timeRecords[0].frameTime.desiredTime,
398 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800399 ALOGV("[%d]-[%" PRIu64 "]-desired2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700400 timeRecords[0].frameTime.frameNumber, desiredToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700401 timeStatsLayer.deltas["desired2present"].insert(desiredToPresentMs);
402
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700403 const int32_t presentToPresentMs = msBetween(prevTimeRecord.frameTime.presentTime,
404 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800405 ALOGV("[%d]-[%" PRIu64 "]-present2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700406 timeRecords[0].frameTime.frameNumber, presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700407 timeStatsLayer.deltas["present2present"].insert(presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700408 }
409 prevTimeRecord = timeRecords[0];
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700410 timeRecords.pop_front();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700411 layerRecord.waitData--;
412 }
413}
414
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700415static constexpr const char* kPopupWindowPrefix = "PopupWindow";
416static const size_t kMinLenLayerName = std::strlen(kPopupWindowPrefix);
Yiwei Zhangbd408322018-10-15 18:31:53 -0700417
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700418// Avoid tracking the "PopupWindow:<random hash>#<number>" layers
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700419static bool layerNameIsValid(const std::string& layerName) {
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700420 return layerName.length() >= kMinLenLayerName &&
421 layerName.compare(0, kMinLenLayerName, kPopupWindowPrefix) != 0;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700422}
423
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800424void TimeStats::setPostTime(int32_t layerId, uint64_t frameNumber, const std::string& layerName,
Yiwei Zhang8e8fe522018-11-02 18:34:07 -0700425 nsecs_t postTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700426 if (!mEnabled.load()) return;
427
428 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800429 ALOGV("[%d]-[%" PRIu64 "]-[%s]-PostTime[%" PRId64 "]", layerId, frameNumber, layerName.c_str(),
Yiwei Zhang8e8fe522018-11-02 18:34:07 -0700430 postTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700431
432 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange926ab52019-08-14 15:16:00 -0700433 if (!mTimeStats.stats.count(layerName) && mTimeStats.stats.size() >= MAX_NUM_LAYER_STATS) {
434 return;
435 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800436 if (!mTimeStatsTracker.count(layerId) && mTimeStatsTracker.size() < MAX_NUM_LAYER_RECORDS &&
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700437 layerNameIsValid(layerName)) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800438 mTimeStatsTracker[layerId].layerName = layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700439 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800440 if (!mTimeStatsTracker.count(layerId)) return;
441 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700442 if (layerRecord.timeRecords.size() == MAX_NUM_TIME_RECORDS) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800443 ALOGE("[%d]-[%s]-timeRecords is at its maximum size[%zu]. Ignore this when unittesting.",
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800444 layerId, layerRecord.layerName.c_str(), MAX_NUM_TIME_RECORDS);
445 mTimeStatsTracker.erase(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700446 return;
447 }
448 // For most media content, the acquireFence is invalid because the buffer is
449 // ready at the queueBuffer stage. In this case, acquireTime should be given
450 // a default value as postTime.
451 TimeRecord timeRecord = {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700452 .frameTime =
453 {
454 .frameNumber = frameNumber,
455 .postTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800456 .latchTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700457 .acquireTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800458 .desiredTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700459 },
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700460 };
461 layerRecord.timeRecords.push_back(timeRecord);
462 if (layerRecord.waitData < 0 ||
463 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
464 layerRecord.waitData = layerRecord.timeRecords.size() - 1;
465}
466
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800467void TimeStats::setLatchTime(int32_t layerId, uint64_t frameNumber, nsecs_t latchTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700468 if (!mEnabled.load()) return;
469
470 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800471 ALOGV("[%d]-[%" PRIu64 "]-LatchTime[%" PRId64 "]", layerId, frameNumber, latchTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700472
473 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800474 if (!mTimeStatsTracker.count(layerId)) return;
475 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700476 if (layerRecord.waitData < 0 ||
477 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
478 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700479 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700480 if (timeRecord.frameTime.frameNumber == frameNumber) {
481 timeRecord.frameTime.latchTime = latchTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700482 }
483}
484
Alec Mouri91f6df32020-01-30 08:48:58 -0800485void TimeStats::incrementLatchSkipped(int32_t layerId, LatchSkipReason reason) {
486 if (!mEnabled.load()) return;
487
488 ATRACE_CALL();
489 ALOGV("[%d]-LatchSkipped-Reason[%d]", layerId,
490 static_cast<std::underlying_type<LatchSkipReason>::type>(reason));
491
492 std::lock_guard<std::mutex> lock(mMutex);
493 if (!mTimeStatsTracker.count(layerId)) return;
494 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
495
496 switch (reason) {
497 case LatchSkipReason::LateAcquire:
498 layerRecord.lateAcquireFrames++;
499 break;
500 }
501}
502
503void TimeStats::incrementBadDesiredPresent(int32_t layerId) {
504 if (!mEnabled.load()) return;
505
506 ATRACE_CALL();
507 ALOGV("[%d]-BadDesiredPresent", layerId);
508
509 std::lock_guard<std::mutex> lock(mMutex);
510 if (!mTimeStatsTracker.count(layerId)) return;
511 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
512 layerRecord.badDesiredPresentFrames++;
513}
514
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800515void TimeStats::setDesiredTime(int32_t layerId, uint64_t frameNumber, nsecs_t desiredTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700516 if (!mEnabled.load()) return;
517
518 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800519 ALOGV("[%d]-[%" PRIu64 "]-DesiredTime[%" PRId64 "]", layerId, frameNumber, desiredTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700520
521 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800522 if (!mTimeStatsTracker.count(layerId)) return;
523 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700524 if (layerRecord.waitData < 0 ||
525 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
526 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700527 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700528 if (timeRecord.frameTime.frameNumber == frameNumber) {
529 timeRecord.frameTime.desiredTime = desiredTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700530 }
531}
532
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800533void TimeStats::setAcquireTime(int32_t layerId, uint64_t frameNumber, nsecs_t acquireTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700534 if (!mEnabled.load()) return;
535
536 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800537 ALOGV("[%d]-[%" PRIu64 "]-AcquireTime[%" PRId64 "]", layerId, frameNumber, acquireTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700538
539 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800540 if (!mTimeStatsTracker.count(layerId)) return;
541 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700542 if (layerRecord.waitData < 0 ||
543 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
544 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700545 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700546 if (timeRecord.frameTime.frameNumber == frameNumber) {
547 timeRecord.frameTime.acquireTime = acquireTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700548 }
549}
550
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800551void TimeStats::setAcquireFence(int32_t layerId, uint64_t frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700552 const std::shared_ptr<FenceTime>& acquireFence) {
553 if (!mEnabled.load()) return;
554
555 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800556 ALOGV("[%d]-[%" PRIu64 "]-AcquireFenceTime[%" PRId64 "]", layerId, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700557 acquireFence->getSignalTime());
558
559 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800560 if (!mTimeStatsTracker.count(layerId)) return;
561 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700562 if (layerRecord.waitData < 0 ||
563 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
564 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700565 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700566 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700567 timeRecord.acquireFence = acquireFence;
568 }
569}
570
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800571void TimeStats::setPresentTime(int32_t layerId, uint64_t frameNumber, nsecs_t presentTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700572 if (!mEnabled.load()) return;
573
574 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800575 ALOGV("[%d]-[%" PRIu64 "]-PresentTime[%" PRId64 "]", layerId, frameNumber, presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700576
577 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800578 if (!mTimeStatsTracker.count(layerId)) return;
579 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700580 if (layerRecord.waitData < 0 ||
581 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
582 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700583 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700584 if (timeRecord.frameTime.frameNumber == frameNumber) {
585 timeRecord.frameTime.presentTime = presentTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700586 timeRecord.ready = true;
587 layerRecord.waitData++;
588 }
589
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800590 flushAvailableRecordsToStatsLocked(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700591}
592
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800593void TimeStats::setPresentFence(int32_t layerId, uint64_t frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700594 const std::shared_ptr<FenceTime>& presentFence) {
595 if (!mEnabled.load()) return;
596
597 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800598 ALOGV("[%d]-[%" PRIu64 "]-PresentFenceTime[%" PRId64 "]", layerId, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700599 presentFence->getSignalTime());
600
601 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800602 if (!mTimeStatsTracker.count(layerId)) return;
603 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700604 if (layerRecord.waitData < 0 ||
605 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
606 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700607 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700608 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700609 timeRecord.presentFence = presentFence;
610 timeRecord.ready = true;
611 layerRecord.waitData++;
612 }
613
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800614 flushAvailableRecordsToStatsLocked(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700615}
616
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800617void TimeStats::onDestroy(int32_t layerId) {
Yiwei Zhangdc224042018-10-18 15:34:00 -0700618 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800619 ALOGV("[%d]-onDestroy", layerId);
Mikael Pessa90092f42019-08-26 17:22:04 -0700620 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800621 mTimeStatsTracker.erase(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700622}
623
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800624void TimeStats::removeTimeRecord(int32_t layerId, uint64_t frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700625 if (!mEnabled.load()) return;
626
627 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800628 ALOGV("[%d]-[%" PRIu64 "]-removeTimeRecord", layerId, frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700629
630 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800631 if (!mTimeStatsTracker.count(layerId)) return;
632 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700633 size_t removeAt = 0;
634 for (const TimeRecord& record : layerRecord.timeRecords) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700635 if (record.frameTime.frameNumber == frameNumber) break;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700636 removeAt++;
637 }
638 if (removeAt == layerRecord.timeRecords.size()) return;
639 layerRecord.timeRecords.erase(layerRecord.timeRecords.begin() + removeAt);
640 if (layerRecord.waitData > static_cast<int32_t>(removeAt)) {
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700641 layerRecord.waitData--;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700642 }
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700643 layerRecord.droppedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700644}
645
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700646void TimeStats::flushPowerTimeLocked() {
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700647 if (!mEnabled.load()) return;
648
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700649 nsecs_t curTime = systemTime();
650 // elapsedTime is in milliseconds.
651 int64_t elapsedTime = (curTime - mPowerTime.prevTime) / 1000000;
652
653 switch (mPowerTime.powerMode) {
654 case HWC_POWER_MODE_NORMAL:
655 mTimeStats.displayOnTime += elapsedTime;
656 break;
657 case HWC_POWER_MODE_OFF:
658 case HWC_POWER_MODE_DOZE:
659 case HWC_POWER_MODE_DOZE_SUSPEND:
660 default:
661 break;
662 }
663
664 mPowerTime.prevTime = curTime;
665}
666
667void TimeStats::setPowerMode(int32_t powerMode) {
668 if (!mEnabled.load()) {
669 std::lock_guard<std::mutex> lock(mMutex);
670 mPowerTime.powerMode = powerMode;
671 return;
672 }
673
674 std::lock_guard<std::mutex> lock(mMutex);
675 if (powerMode == mPowerTime.powerMode) return;
676
677 flushPowerTimeLocked();
678 mPowerTime.powerMode = powerMode;
679}
680
Alec Mourifb571ea2019-01-24 18:42:10 -0800681void TimeStats::recordRefreshRate(uint32_t fps, nsecs_t duration) {
682 std::lock_guard<std::mutex> lock(mMutex);
683 if (mTimeStats.refreshRateStats.count(fps)) {
684 mTimeStats.refreshRateStats[fps] += duration;
685 } else {
686 mTimeStats.refreshRateStats.insert({fps, duration});
687 }
688}
689
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700690void TimeStats::flushAvailableGlobalRecordsToStatsLocked() {
691 ATRACE_CALL();
692
693 while (!mGlobalRecord.presentFences.empty()) {
694 const nsecs_t curPresentTime = mGlobalRecord.presentFences.front()->getSignalTime();
695 if (curPresentTime == Fence::SIGNAL_TIME_PENDING) break;
696
697 if (curPresentTime == Fence::SIGNAL_TIME_INVALID) {
698 ALOGE("GlobalPresentFence is invalid!");
699 mGlobalRecord.prevPresentTime = 0;
700 mGlobalRecord.presentFences.pop_front();
701 continue;
702 }
703
704 ALOGV("GlobalPresentFenceTime[%" PRId64 "]",
705 mGlobalRecord.presentFences.front()->getSignalTime());
706
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700707 if (mGlobalRecord.prevPresentTime != 0) {
708 const int32_t presentToPresentMs =
709 msBetween(mGlobalRecord.prevPresentTime, curPresentTime);
710 ALOGV("Global present2present[%d] prev[%" PRId64 "] curr[%" PRId64 "]",
711 presentToPresentMs, mGlobalRecord.prevPresentTime, curPresentTime);
712 mTimeStats.presentToPresent.insert(presentToPresentMs);
713 }
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700714
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700715 mGlobalRecord.prevPresentTime = curPresentTime;
716 mGlobalRecord.presentFences.pop_front();
717 }
Alec Mourie4034bb2019-11-19 12:45:54 -0800718 while (!mGlobalRecord.renderEngineDurations.empty()) {
719 const auto duration = mGlobalRecord.renderEngineDurations.front();
720 const auto& endTime = duration.endTime;
721
722 nsecs_t endNs = -1;
723
724 if (auto val = std::get_if<nsecs_t>(&endTime)) {
725 endNs = *val;
726 } else {
727 endNs = std::get<std::shared_ptr<FenceTime>>(endTime)->getSignalTime();
728 }
729
730 if (endNs == Fence::SIGNAL_TIME_PENDING) break;
731
732 if (endNs < 0) {
733 ALOGE("RenderEngineTiming is invalid!");
734 mGlobalRecord.renderEngineDurations.pop_front();
735 continue;
736 }
737
738 const int32_t renderEngineMs = msBetween(duration.startTime, endNs);
739 mTimeStats.renderEngineTiming.insert(renderEngineMs);
740
741 mGlobalRecord.renderEngineDurations.pop_front();
742 }
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700743}
744
745void TimeStats::setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) {
746 if (!mEnabled.load()) return;
747
748 ATRACE_CALL();
749 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700750 if (presentFence == nullptr || !presentFence->isValid()) {
751 mGlobalRecord.prevPresentTime = 0;
752 return;
753 }
754
755 if (mPowerTime.powerMode != HWC_POWER_MODE_NORMAL) {
756 // Try flushing the last present fence on HWC_POWER_MODE_NORMAL.
757 flushAvailableGlobalRecordsToStatsLocked();
758 mGlobalRecord.presentFences.clear();
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700759 mGlobalRecord.prevPresentTime = 0;
760 return;
761 }
762
763 if (mGlobalRecord.presentFences.size() == MAX_NUM_TIME_RECORDS) {
764 // The front presentFence must be trapped in pending status in this
765 // case. Try dequeuing the front one to recover.
766 ALOGE("GlobalPresentFences is already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
767 mGlobalRecord.prevPresentTime = 0;
768 mGlobalRecord.presentFences.pop_front();
769 }
770
771 mGlobalRecord.presentFences.emplace_back(presentFence);
772 flushAvailableGlobalRecordsToStatsLocked();
773}
774
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700775void TimeStats::enable() {
776 if (mEnabled.load()) return;
777
778 ATRACE_CALL();
779
780 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700781 mEnabled.store(true);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700782 mTimeStats.statsStart = static_cast<int64_t>(std::time(0));
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700783 mPowerTime.prevTime = systemTime();
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700784 ALOGD("Enabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700785}
786
787void TimeStats::disable() {
788 if (!mEnabled.load()) return;
789
790 ATRACE_CALL();
791
792 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700793 flushPowerTimeLocked();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700794 mEnabled.store(false);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700795 mTimeStats.statsEnd = static_cast<int64_t>(std::time(0));
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700796 ALOGD("Disabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700797}
798
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000799void TimeStats::clearAll() {
800 std::lock_guard<std::mutex> lock(mMutex);
801 clearGlobalLocked();
802 clearLayersLocked();
803}
804
805void TimeStats::clearGlobalLocked() {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700806 ATRACE_CALL();
807
Yiwei Zhangdc224042018-10-18 15:34:00 -0700808 mTimeStats.statsStart = (mEnabled.load() ? static_cast<int64_t>(std::time(0)) : 0);
809 mTimeStats.statsEnd = 0;
810 mTimeStats.totalFrames = 0;
811 mTimeStats.missedFrames = 0;
812 mTimeStats.clientCompositionFrames = 0;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800813 mTimeStats.clientCompositionReusedFrames = 0;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700814 mTimeStats.displayOnTime = 0;
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700815 mTimeStats.presentToPresent.hist.clear();
Alec Mouri31ac64a2020-01-09 09:26:22 -0800816 mTimeStats.frameDuration.hist.clear();
817 mTimeStats.renderEngineTiming.hist.clear();
Alec Mourifb571ea2019-01-24 18:42:10 -0800818 mTimeStats.refreshRateStats.clear();
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700819 mPowerTime.prevTime = systemTime();
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700820 mGlobalRecord.prevPresentTime = 0;
821 mGlobalRecord.presentFences.clear();
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000822 ALOGD("Cleared global stats");
823}
824
825void TimeStats::clearLayersLocked() {
826 ATRACE_CALL();
827
828 mTimeStatsTracker.clear();
829 mTimeStats.stats.clear();
830 ALOGD("Cleared layer stats");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700831}
832
833bool TimeStats::isEnabled() {
834 return mEnabled.load();
835}
836
Yiwei Zhang5434a782018-12-05 18:06:32 -0800837void TimeStats::dump(bool asProto, std::optional<uint32_t> maxLayers, std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700838 ATRACE_CALL();
839
840 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700841 if (mTimeStats.statsStart == 0) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700842 return;
843 }
844
Yiwei Zhangdc224042018-10-18 15:34:00 -0700845 mTimeStats.statsEnd = static_cast<int64_t>(std::time(0));
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700846
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700847 flushPowerTimeLocked();
848
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700849 if (asProto) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700850 ALOGD("Dumping TimeStats as proto");
Yiwei Zhangdc224042018-10-18 15:34:00 -0700851 SFTimeStatsGlobalProto timeStatsProto = mTimeStats.toProto(maxLayers);
Dominik Laskowski46470112019-08-02 13:13:11 -0700852 result.append(timeStatsProto.SerializeAsString());
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700853 } else {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700854 ALOGD("Dumping TimeStats as text");
Yiwei Zhang5434a782018-12-05 18:06:32 -0800855 result.append(mTimeStats.toString(maxLayers));
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700856 result.append("\n");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700857 }
858}
859
Alec Mourifb571ea2019-01-24 18:42:10 -0800860} // namespace impl
861
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700862} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800863
864// TODO(b/129481165): remove the #pragma below and fix conversion issues
865#pragma clang diagnostic pop // ignored "-Wconversion"