blob: 8038eba2e14ab7cb7a106858417b17a5f97e3ef4 [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);
191 mStatsDelegate->unregisterStatsPullAtomCallback(
192 android::util::SURFACEFLINGER_STATS_GLOBAL_INFO);
193 mStatsDelegate->unregisterStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_LAYER_INFO);
194}
195
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000196void TimeStats::onBootFinished() {
Alec Mouri3ecd5cd2020-01-29 12:53:07 -0800197 std::lock_guard<std::mutex> lock(mMutex);
198 mStatsDelegate->registerStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_GLOBAL_INFO,
199 TimeStats::pullAtomCallback, nullptr, this);
200 mStatsDelegate->registerStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_LAYER_INFO,
201 TimeStats::pullAtomCallback, nullptr, this);
Alec Mourib3885ad2019-09-06 17:08:55 -0700202}
203
Dominik Laskowskic2867142019-01-21 11:33:38 -0800204void TimeStats::parseArgs(bool asProto, const Vector<String16>& args, std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700205 ATRACE_CALL();
206
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700207 std::unordered_map<std::string, int32_t> argsMap;
Dominik Laskowskic2867142019-01-21 11:33:38 -0800208 for (size_t index = 0; index < args.size(); ++index) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700209 argsMap[std::string(String8(args[index]).c_str())] = index;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700210 }
211
212 if (argsMap.count("-disable")) {
213 disable();
214 }
215
216 if (argsMap.count("-dump")) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700217 std::optional<uint32_t> maxLayers = std::nullopt;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700218 auto iter = argsMap.find("-maxlayers");
219 if (iter != argsMap.end() && iter->second + 1 < static_cast<int32_t>(args.size())) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700220 int64_t value = strtol(String8(args[iter->second + 1]).c_str(), nullptr, 10);
221 value = std::clamp(value, int64_t(0), int64_t(UINT32_MAX));
222 maxLayers = static_cast<uint32_t>(value);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700223 }
224
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700225 dump(asProto, maxLayers, result);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700226 }
227
228 if (argsMap.count("-clear")) {
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000229 clearAll();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700230 }
231
232 if (argsMap.count("-enable")) {
233 enable();
234 }
235}
236
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700237std::string TimeStats::miniDump() {
238 ATRACE_CALL();
239
240 std::string result = "TimeStats miniDump:\n";
241 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange926ab52019-08-14 15:16:00 -0700242 android::base::StringAppendF(&result, "Number of layers currently being tracked is %zu\n",
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700243 mTimeStatsTracker.size());
Yiwei Zhange926ab52019-08-14 15:16:00 -0700244 android::base::StringAppendF(&result, "Number of layers in the stats pool is %zu\n",
245 mTimeStats.stats.size());
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700246 return result;
247}
248
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700249void TimeStats::incrementTotalFrames() {
250 if (!mEnabled.load()) return;
251
252 ATRACE_CALL();
253
254 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700255 mTimeStats.totalFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700256}
257
Yiwei Zhang621f9d42018-05-07 10:40:55 -0700258void TimeStats::incrementMissedFrames() {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700259 if (!mEnabled.load()) return;
260
261 ATRACE_CALL();
262
263 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700264 mTimeStats.missedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700265}
266
267void TimeStats::incrementClientCompositionFrames() {
268 if (!mEnabled.load()) return;
269
270 ATRACE_CALL();
271
272 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700273 mTimeStats.clientCompositionFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700274}
275
Vishnu Nair9b079a22020-01-21 14:36:08 -0800276void TimeStats::incrementClientCompositionReusedFrames() {
277 if (!mEnabled.load()) return;
278
279 ATRACE_CALL();
280
281 std::lock_guard<std::mutex> lock(mMutex);
282 mTimeStats.clientCompositionReusedFrames++;
283}
284
Alec Mouri717bcb62020-02-10 17:07:19 -0800285void TimeStats::recordDisplayEventConnectionCount(int32_t count) {
286 if (!mEnabled.load()) return;
287
288 ATRACE_CALL();
289
290 std::lock_guard<std::mutex> lock(mMutex);
291 mTimeStats.displayEventConnectionsCount =
292 std::max(mTimeStats.displayEventConnectionsCount, count);
293}
294
Alec Mouri9519bf12019-11-15 16:54:44 -0800295static int32_t msBetween(nsecs_t start, nsecs_t end) {
296 int64_t delta = std::chrono::duration_cast<std::chrono::milliseconds>(
297 std::chrono::nanoseconds(end - start))
298 .count();
299 delta = std::clamp(delta, int64_t(INT32_MIN), int64_t(INT32_MAX));
300 return static_cast<int32_t>(delta);
301}
302
303void TimeStats::recordFrameDuration(nsecs_t startTime, nsecs_t endTime) {
304 if (!mEnabled.load()) return;
305
306 std::lock_guard<std::mutex> lock(mMutex);
307 if (mPowerTime.powerMode == HWC_POWER_MODE_NORMAL) {
308 mTimeStats.frameDuration.insert(msBetween(startTime, endTime));
309 }
310}
311
Alec Mourie4034bb2019-11-19 12:45:54 -0800312void TimeStats::recordRenderEngineDuration(nsecs_t startTime, nsecs_t endTime) {
313 if (!mEnabled.load()) return;
314
315 std::lock_guard<std::mutex> lock(mMutex);
316 if (mGlobalRecord.renderEngineDurations.size() == MAX_NUM_TIME_RECORDS) {
317 ALOGE("RenderEngineTimes are already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
318 mGlobalRecord.renderEngineDurations.pop_front();
319 }
320 mGlobalRecord.renderEngineDurations.push_back({startTime, endTime});
321}
322
323void TimeStats::recordRenderEngineDuration(nsecs_t startTime,
324 const std::shared_ptr<FenceTime>& endTime) {
325 if (!mEnabled.load()) return;
326
327 std::lock_guard<std::mutex> lock(mMutex);
328 if (mGlobalRecord.renderEngineDurations.size() == MAX_NUM_TIME_RECORDS) {
329 ALOGE("RenderEngineTimes are already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
330 mGlobalRecord.renderEngineDurations.pop_front();
331 }
332 mGlobalRecord.renderEngineDurations.push_back({startTime, endTime});
333}
334
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800335bool TimeStats::recordReadyLocked(int32_t layerId, TimeRecord* timeRecord) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700336 if (!timeRecord->ready) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800337 ALOGV("[%d]-[%" PRIu64 "]-presentFence is still not received", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700338 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700339 return false;
340 }
341
342 if (timeRecord->acquireFence != nullptr) {
343 if (timeRecord->acquireFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
344 return false;
345 }
346 if (timeRecord->acquireFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700347 timeRecord->frameTime.acquireTime = timeRecord->acquireFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700348 timeRecord->acquireFence = nullptr;
349 } else {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800350 ALOGV("[%d]-[%" PRIu64 "]-acquireFence signal time is invalid", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700351 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700352 }
353 }
354
355 if (timeRecord->presentFence != nullptr) {
356 if (timeRecord->presentFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
357 return false;
358 }
359 if (timeRecord->presentFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700360 timeRecord->frameTime.presentTime = timeRecord->presentFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700361 timeRecord->presentFence = nullptr;
362 } else {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800363 ALOGV("[%d]-[%" PRIu64 "]-presentFence signal time invalid", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700364 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700365 }
366 }
367
368 return true;
369}
370
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800371void TimeStats::flushAvailableRecordsToStatsLocked(int32_t layerId) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700372 ATRACE_CALL();
373
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800374 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700375 TimeRecord& prevTimeRecord = layerRecord.prevTimeRecord;
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700376 std::deque<TimeRecord>& timeRecords = layerRecord.timeRecords;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700377 while (!timeRecords.empty()) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800378 if (!recordReadyLocked(layerId, &timeRecords[0])) break;
379 ALOGV("[%d]-[%" PRIu64 "]-presentFenceTime[%" PRId64 "]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700380 timeRecords[0].frameTime.frameNumber, timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700381
382 if (prevTimeRecord.ready) {
Yiwei Zhangeafa5cc2019-07-26 15:06:25 -0700383 const std::string& layerName = layerRecord.layerName;
Yiwei Zhangdc224042018-10-18 15:34:00 -0700384 if (!mTimeStats.stats.count(layerName)) {
385 mTimeStats.stats[layerName].layerName = layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700386 }
Yiwei Zhangdc224042018-10-18 15:34:00 -0700387 TimeStatsHelper::TimeStatsLayer& timeStatsLayer = mTimeStats.stats[layerName];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700388 timeStatsLayer.totalFrames++;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700389 timeStatsLayer.droppedFrames += layerRecord.droppedFrames;
Alec Mouri91f6df32020-01-30 08:48:58 -0800390 timeStatsLayer.lateAcquireFrames += layerRecord.lateAcquireFrames;
391 timeStatsLayer.badDesiredPresentFrames += layerRecord.badDesiredPresentFrames;
392
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700393 layerRecord.droppedFrames = 0;
Alec Mouri91f6df32020-01-30 08:48:58 -0800394 layerRecord.lateAcquireFrames = 0;
395 layerRecord.badDesiredPresentFrames = 0;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700396
397 const int32_t postToAcquireMs = msBetween(timeRecords[0].frameTime.postTime,
398 timeRecords[0].frameTime.acquireTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800399 ALOGV("[%d]-[%" PRIu64 "]-post2acquire[%d]", layerId,
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700400 timeRecords[0].frameTime.frameNumber, postToAcquireMs);
401 timeStatsLayer.deltas["post2acquire"].insert(postToAcquireMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700402
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700403 const int32_t postToPresentMs = msBetween(timeRecords[0].frameTime.postTime,
404 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800405 ALOGV("[%d]-[%" PRIu64 "]-post2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700406 timeRecords[0].frameTime.frameNumber, postToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700407 timeStatsLayer.deltas["post2present"].insert(postToPresentMs);
408
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700409 const int32_t acquireToPresentMs = msBetween(timeRecords[0].frameTime.acquireTime,
410 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800411 ALOGV("[%d]-[%" PRIu64 "]-acquire2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700412 timeRecords[0].frameTime.frameNumber, acquireToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700413 timeStatsLayer.deltas["acquire2present"].insert(acquireToPresentMs);
414
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700415 const int32_t latchToPresentMs = msBetween(timeRecords[0].frameTime.latchTime,
416 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800417 ALOGV("[%d]-[%" PRIu64 "]-latch2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700418 timeRecords[0].frameTime.frameNumber, latchToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700419 timeStatsLayer.deltas["latch2present"].insert(latchToPresentMs);
420
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700421 const int32_t desiredToPresentMs = msBetween(timeRecords[0].frameTime.desiredTime,
422 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800423 ALOGV("[%d]-[%" PRIu64 "]-desired2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700424 timeRecords[0].frameTime.frameNumber, desiredToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700425 timeStatsLayer.deltas["desired2present"].insert(desiredToPresentMs);
426
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700427 const int32_t presentToPresentMs = msBetween(prevTimeRecord.frameTime.presentTime,
428 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800429 ALOGV("[%d]-[%" PRIu64 "]-present2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700430 timeRecords[0].frameTime.frameNumber, presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700431 timeStatsLayer.deltas["present2present"].insert(presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700432 }
433 prevTimeRecord = timeRecords[0];
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700434 timeRecords.pop_front();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700435 layerRecord.waitData--;
436 }
437}
438
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700439static constexpr const char* kPopupWindowPrefix = "PopupWindow";
440static const size_t kMinLenLayerName = std::strlen(kPopupWindowPrefix);
Yiwei Zhangbd408322018-10-15 18:31:53 -0700441
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700442// Avoid tracking the "PopupWindow:<random hash>#<number>" layers
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700443static bool layerNameIsValid(const std::string& layerName) {
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700444 return layerName.length() >= kMinLenLayerName &&
445 layerName.compare(0, kMinLenLayerName, kPopupWindowPrefix) != 0;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700446}
447
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800448void TimeStats::setPostTime(int32_t layerId, uint64_t frameNumber, const std::string& layerName,
Yiwei Zhang8e8fe522018-11-02 18:34:07 -0700449 nsecs_t postTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700450 if (!mEnabled.load()) return;
451
452 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800453 ALOGV("[%d]-[%" PRIu64 "]-[%s]-PostTime[%" PRId64 "]", layerId, frameNumber, layerName.c_str(),
Yiwei Zhang8e8fe522018-11-02 18:34:07 -0700454 postTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700455
456 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange926ab52019-08-14 15:16:00 -0700457 if (!mTimeStats.stats.count(layerName) && mTimeStats.stats.size() >= MAX_NUM_LAYER_STATS) {
458 return;
459 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800460 if (!mTimeStatsTracker.count(layerId) && mTimeStatsTracker.size() < MAX_NUM_LAYER_RECORDS &&
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700461 layerNameIsValid(layerName)) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800462 mTimeStatsTracker[layerId].layerName = layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700463 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800464 if (!mTimeStatsTracker.count(layerId)) return;
465 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700466 if (layerRecord.timeRecords.size() == MAX_NUM_TIME_RECORDS) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800467 ALOGE("[%d]-[%s]-timeRecords is at its maximum size[%zu]. Ignore this when unittesting.",
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800468 layerId, layerRecord.layerName.c_str(), MAX_NUM_TIME_RECORDS);
469 mTimeStatsTracker.erase(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700470 return;
471 }
472 // For most media content, the acquireFence is invalid because the buffer is
473 // ready at the queueBuffer stage. In this case, acquireTime should be given
474 // a default value as postTime.
475 TimeRecord timeRecord = {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700476 .frameTime =
477 {
478 .frameNumber = frameNumber,
479 .postTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800480 .latchTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700481 .acquireTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800482 .desiredTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700483 },
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700484 };
485 layerRecord.timeRecords.push_back(timeRecord);
486 if (layerRecord.waitData < 0 ||
487 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
488 layerRecord.waitData = layerRecord.timeRecords.size() - 1;
489}
490
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800491void TimeStats::setLatchTime(int32_t layerId, uint64_t frameNumber, nsecs_t latchTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700492 if (!mEnabled.load()) return;
493
494 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800495 ALOGV("[%d]-[%" PRIu64 "]-LatchTime[%" PRId64 "]", layerId, frameNumber, latchTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700496
497 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800498 if (!mTimeStatsTracker.count(layerId)) return;
499 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700500 if (layerRecord.waitData < 0 ||
501 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
502 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700503 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700504 if (timeRecord.frameTime.frameNumber == frameNumber) {
505 timeRecord.frameTime.latchTime = latchTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700506 }
507}
508
Alec Mouri91f6df32020-01-30 08:48:58 -0800509void TimeStats::incrementLatchSkipped(int32_t layerId, LatchSkipReason reason) {
510 if (!mEnabled.load()) return;
511
512 ATRACE_CALL();
513 ALOGV("[%d]-LatchSkipped-Reason[%d]", layerId,
514 static_cast<std::underlying_type<LatchSkipReason>::type>(reason));
515
516 std::lock_guard<std::mutex> lock(mMutex);
517 if (!mTimeStatsTracker.count(layerId)) return;
518 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
519
520 switch (reason) {
521 case LatchSkipReason::LateAcquire:
522 layerRecord.lateAcquireFrames++;
523 break;
524 }
525}
526
527void TimeStats::incrementBadDesiredPresent(int32_t layerId) {
528 if (!mEnabled.load()) return;
529
530 ATRACE_CALL();
531 ALOGV("[%d]-BadDesiredPresent", layerId);
532
533 std::lock_guard<std::mutex> lock(mMutex);
534 if (!mTimeStatsTracker.count(layerId)) return;
535 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
536 layerRecord.badDesiredPresentFrames++;
537}
538
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800539void TimeStats::setDesiredTime(int32_t layerId, uint64_t frameNumber, nsecs_t desiredTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700540 if (!mEnabled.load()) return;
541
542 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800543 ALOGV("[%d]-[%" PRIu64 "]-DesiredTime[%" PRId64 "]", layerId, frameNumber, desiredTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700544
545 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800546 if (!mTimeStatsTracker.count(layerId)) return;
547 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700548 if (layerRecord.waitData < 0 ||
549 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
550 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700551 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700552 if (timeRecord.frameTime.frameNumber == frameNumber) {
553 timeRecord.frameTime.desiredTime = desiredTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700554 }
555}
556
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800557void TimeStats::setAcquireTime(int32_t layerId, uint64_t frameNumber, nsecs_t acquireTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700558 if (!mEnabled.load()) return;
559
560 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800561 ALOGV("[%d]-[%" PRIu64 "]-AcquireTime[%" PRId64 "]", layerId, frameNumber, acquireTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700562
563 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800564 if (!mTimeStatsTracker.count(layerId)) return;
565 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700566 if (layerRecord.waitData < 0 ||
567 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
568 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700569 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700570 if (timeRecord.frameTime.frameNumber == frameNumber) {
571 timeRecord.frameTime.acquireTime = acquireTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700572 }
573}
574
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800575void TimeStats::setAcquireFence(int32_t layerId, uint64_t frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700576 const std::shared_ptr<FenceTime>& acquireFence) {
577 if (!mEnabled.load()) return;
578
579 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800580 ALOGV("[%d]-[%" PRIu64 "]-AcquireFenceTime[%" PRId64 "]", layerId, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700581 acquireFence->getSignalTime());
582
583 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800584 if (!mTimeStatsTracker.count(layerId)) return;
585 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700586 if (layerRecord.waitData < 0 ||
587 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
588 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700589 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700590 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700591 timeRecord.acquireFence = acquireFence;
592 }
593}
594
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800595void TimeStats::setPresentTime(int32_t layerId, uint64_t frameNumber, nsecs_t presentTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700596 if (!mEnabled.load()) return;
597
598 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800599 ALOGV("[%d]-[%" PRIu64 "]-PresentTime[%" PRId64 "]", layerId, frameNumber, presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700600
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) {
609 timeRecord.frameTime.presentTime = presentTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700610 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::setPresentFence(int32_t layerId, uint64_t frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700618 const std::shared_ptr<FenceTime>& presentFence) {
619 if (!mEnabled.load()) return;
620
621 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800622 ALOGV("[%d]-[%" PRIu64 "]-PresentFenceTime[%" PRId64 "]", layerId, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700623 presentFence->getSignalTime());
624
625 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800626 if (!mTimeStatsTracker.count(layerId)) return;
627 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700628 if (layerRecord.waitData < 0 ||
629 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
630 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700631 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700632 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700633 timeRecord.presentFence = presentFence;
634 timeRecord.ready = true;
635 layerRecord.waitData++;
636 }
637
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800638 flushAvailableRecordsToStatsLocked(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700639}
640
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800641void TimeStats::onDestroy(int32_t layerId) {
Yiwei Zhangdc224042018-10-18 15:34:00 -0700642 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800643 ALOGV("[%d]-onDestroy", layerId);
Mikael Pessa90092f42019-08-26 17:22:04 -0700644 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800645 mTimeStatsTracker.erase(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700646}
647
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800648void TimeStats::removeTimeRecord(int32_t layerId, uint64_t frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700649 if (!mEnabled.load()) return;
650
651 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800652 ALOGV("[%d]-[%" PRIu64 "]-removeTimeRecord", layerId, frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700653
654 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800655 if (!mTimeStatsTracker.count(layerId)) return;
656 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700657 size_t removeAt = 0;
658 for (const TimeRecord& record : layerRecord.timeRecords) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700659 if (record.frameTime.frameNumber == frameNumber) break;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700660 removeAt++;
661 }
662 if (removeAt == layerRecord.timeRecords.size()) return;
663 layerRecord.timeRecords.erase(layerRecord.timeRecords.begin() + removeAt);
664 if (layerRecord.waitData > static_cast<int32_t>(removeAt)) {
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700665 layerRecord.waitData--;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700666 }
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700667 layerRecord.droppedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700668}
669
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700670void TimeStats::flushPowerTimeLocked() {
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700671 if (!mEnabled.load()) return;
672
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700673 nsecs_t curTime = systemTime();
674 // elapsedTime is in milliseconds.
675 int64_t elapsedTime = (curTime - mPowerTime.prevTime) / 1000000;
676
677 switch (mPowerTime.powerMode) {
678 case HWC_POWER_MODE_NORMAL:
679 mTimeStats.displayOnTime += elapsedTime;
680 break;
681 case HWC_POWER_MODE_OFF:
682 case HWC_POWER_MODE_DOZE:
683 case HWC_POWER_MODE_DOZE_SUSPEND:
684 default:
685 break;
686 }
687
688 mPowerTime.prevTime = curTime;
689}
690
691void TimeStats::setPowerMode(int32_t powerMode) {
692 if (!mEnabled.load()) {
693 std::lock_guard<std::mutex> lock(mMutex);
694 mPowerTime.powerMode = powerMode;
695 return;
696 }
697
698 std::lock_guard<std::mutex> lock(mMutex);
699 if (powerMode == mPowerTime.powerMode) return;
700
701 flushPowerTimeLocked();
702 mPowerTime.powerMode = powerMode;
703}
704
Alec Mourifb571ea2019-01-24 18:42:10 -0800705void TimeStats::recordRefreshRate(uint32_t fps, nsecs_t duration) {
706 std::lock_guard<std::mutex> lock(mMutex);
707 if (mTimeStats.refreshRateStats.count(fps)) {
708 mTimeStats.refreshRateStats[fps] += duration;
709 } else {
710 mTimeStats.refreshRateStats.insert({fps, duration});
711 }
712}
713
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700714void TimeStats::flushAvailableGlobalRecordsToStatsLocked() {
715 ATRACE_CALL();
716
717 while (!mGlobalRecord.presentFences.empty()) {
718 const nsecs_t curPresentTime = mGlobalRecord.presentFences.front()->getSignalTime();
719 if (curPresentTime == Fence::SIGNAL_TIME_PENDING) break;
720
721 if (curPresentTime == Fence::SIGNAL_TIME_INVALID) {
722 ALOGE("GlobalPresentFence is invalid!");
723 mGlobalRecord.prevPresentTime = 0;
724 mGlobalRecord.presentFences.pop_front();
725 continue;
726 }
727
728 ALOGV("GlobalPresentFenceTime[%" PRId64 "]",
729 mGlobalRecord.presentFences.front()->getSignalTime());
730
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700731 if (mGlobalRecord.prevPresentTime != 0) {
732 const int32_t presentToPresentMs =
733 msBetween(mGlobalRecord.prevPresentTime, curPresentTime);
734 ALOGV("Global present2present[%d] prev[%" PRId64 "] curr[%" PRId64 "]",
735 presentToPresentMs, mGlobalRecord.prevPresentTime, curPresentTime);
736 mTimeStats.presentToPresent.insert(presentToPresentMs);
737 }
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700738
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700739 mGlobalRecord.prevPresentTime = curPresentTime;
740 mGlobalRecord.presentFences.pop_front();
741 }
Alec Mourie4034bb2019-11-19 12:45:54 -0800742 while (!mGlobalRecord.renderEngineDurations.empty()) {
743 const auto duration = mGlobalRecord.renderEngineDurations.front();
744 const auto& endTime = duration.endTime;
745
746 nsecs_t endNs = -1;
747
748 if (auto val = std::get_if<nsecs_t>(&endTime)) {
749 endNs = *val;
750 } else {
751 endNs = std::get<std::shared_ptr<FenceTime>>(endTime)->getSignalTime();
752 }
753
754 if (endNs == Fence::SIGNAL_TIME_PENDING) break;
755
756 if (endNs < 0) {
757 ALOGE("RenderEngineTiming is invalid!");
758 mGlobalRecord.renderEngineDurations.pop_front();
759 continue;
760 }
761
762 const int32_t renderEngineMs = msBetween(duration.startTime, endNs);
763 mTimeStats.renderEngineTiming.insert(renderEngineMs);
764
765 mGlobalRecord.renderEngineDurations.pop_front();
766 }
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700767}
768
769void TimeStats::setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) {
770 if (!mEnabled.load()) return;
771
772 ATRACE_CALL();
773 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700774 if (presentFence == nullptr || !presentFence->isValid()) {
775 mGlobalRecord.prevPresentTime = 0;
776 return;
777 }
778
779 if (mPowerTime.powerMode != HWC_POWER_MODE_NORMAL) {
780 // Try flushing the last present fence on HWC_POWER_MODE_NORMAL.
781 flushAvailableGlobalRecordsToStatsLocked();
782 mGlobalRecord.presentFences.clear();
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700783 mGlobalRecord.prevPresentTime = 0;
784 return;
785 }
786
787 if (mGlobalRecord.presentFences.size() == MAX_NUM_TIME_RECORDS) {
788 // The front presentFence must be trapped in pending status in this
789 // case. Try dequeuing the front one to recover.
790 ALOGE("GlobalPresentFences is already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
791 mGlobalRecord.prevPresentTime = 0;
792 mGlobalRecord.presentFences.pop_front();
793 }
794
795 mGlobalRecord.presentFences.emplace_back(presentFence);
796 flushAvailableGlobalRecordsToStatsLocked();
797}
798
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700799void TimeStats::enable() {
800 if (mEnabled.load()) return;
801
802 ATRACE_CALL();
803
804 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700805 mEnabled.store(true);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700806 mTimeStats.statsStart = static_cast<int64_t>(std::time(0));
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700807 mPowerTime.prevTime = systemTime();
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700808 ALOGD("Enabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700809}
810
811void TimeStats::disable() {
812 if (!mEnabled.load()) return;
813
814 ATRACE_CALL();
815
816 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700817 flushPowerTimeLocked();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700818 mEnabled.store(false);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700819 mTimeStats.statsEnd = static_cast<int64_t>(std::time(0));
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700820 ALOGD("Disabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700821}
822
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000823void TimeStats::clearAll() {
824 std::lock_guard<std::mutex> lock(mMutex);
825 clearGlobalLocked();
826 clearLayersLocked();
827}
828
829void TimeStats::clearGlobalLocked() {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700830 ATRACE_CALL();
831
Yiwei Zhangdc224042018-10-18 15:34:00 -0700832 mTimeStats.statsStart = (mEnabled.load() ? static_cast<int64_t>(std::time(0)) : 0);
833 mTimeStats.statsEnd = 0;
834 mTimeStats.totalFrames = 0;
835 mTimeStats.missedFrames = 0;
836 mTimeStats.clientCompositionFrames = 0;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800837 mTimeStats.clientCompositionReusedFrames = 0;
Alec Mouri717bcb62020-02-10 17:07:19 -0800838 mTimeStats.displayEventConnectionsCount = 0;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700839 mTimeStats.displayOnTime = 0;
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700840 mTimeStats.presentToPresent.hist.clear();
Alec Mouri31ac64a2020-01-09 09:26:22 -0800841 mTimeStats.frameDuration.hist.clear();
842 mTimeStats.renderEngineTiming.hist.clear();
Alec Mourifb571ea2019-01-24 18:42:10 -0800843 mTimeStats.refreshRateStats.clear();
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700844 mPowerTime.prevTime = systemTime();
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700845 mGlobalRecord.prevPresentTime = 0;
846 mGlobalRecord.presentFences.clear();
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000847 ALOGD("Cleared global stats");
848}
849
850void TimeStats::clearLayersLocked() {
851 ATRACE_CALL();
852
853 mTimeStatsTracker.clear();
854 mTimeStats.stats.clear();
855 ALOGD("Cleared layer stats");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700856}
857
858bool TimeStats::isEnabled() {
859 return mEnabled.load();
860}
861
Yiwei Zhang5434a782018-12-05 18:06:32 -0800862void TimeStats::dump(bool asProto, std::optional<uint32_t> maxLayers, std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700863 ATRACE_CALL();
864
865 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700866 if (mTimeStats.statsStart == 0) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700867 return;
868 }
869
Yiwei Zhangdc224042018-10-18 15:34:00 -0700870 mTimeStats.statsEnd = static_cast<int64_t>(std::time(0));
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700871
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700872 flushPowerTimeLocked();
873
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700874 if (asProto) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700875 ALOGD("Dumping TimeStats as proto");
Yiwei Zhangdc224042018-10-18 15:34:00 -0700876 SFTimeStatsGlobalProto timeStatsProto = mTimeStats.toProto(maxLayers);
Dominik Laskowski46470112019-08-02 13:13:11 -0700877 result.append(timeStatsProto.SerializeAsString());
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700878 } else {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700879 ALOGD("Dumping TimeStats as text");
Yiwei Zhang5434a782018-12-05 18:06:32 -0800880 result.append(mTimeStats.toString(maxLayers));
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700881 result.append("\n");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700882 }
883}
884
Alec Mourifb571ea2019-01-24 18:42:10 -0800885} // namespace impl
886
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700887} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800888
889// TODO(b/129481165): remove the #pragma below and fix conversion issues
890#pragma clang diagnostic pop // ignored "-Wconversion"