blob: 37194c6355b8716b17dbf6eefae6db798b02f602 [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 Mouri8de697e2020-03-19 10:52:01 -0700284void TimeStats::incrementRefreshRateSwitches() {
285 if (!mEnabled.load()) return;
286
287 ATRACE_CALL();
288
289 std::lock_guard<std::mutex> lock(mMutex);
290 mTimeStats.refreshRateSwitches++;
291}
292
Alec Mouri8f7a0102020-04-15 12:11:10 -0700293void TimeStats::incrementCompositionStrategyChanges() {
294 if (!mEnabled.load()) return;
295
296 ATRACE_CALL();
297
298 std::lock_guard<std::mutex> lock(mMutex);
299 mTimeStats.compositionStrategyChanges++;
300}
301
Alec Mouri717bcb62020-02-10 17:07:19 -0800302void TimeStats::recordDisplayEventConnectionCount(int32_t count) {
303 if (!mEnabled.load()) return;
304
305 ATRACE_CALL();
306
307 std::lock_guard<std::mutex> lock(mMutex);
308 mTimeStats.displayEventConnectionsCount =
309 std::max(mTimeStats.displayEventConnectionsCount, count);
310}
311
Alec Mouri9519bf12019-11-15 16:54:44 -0800312static int32_t msBetween(nsecs_t start, nsecs_t end) {
313 int64_t delta = std::chrono::duration_cast<std::chrono::milliseconds>(
314 std::chrono::nanoseconds(end - start))
315 .count();
316 delta = std::clamp(delta, int64_t(INT32_MIN), int64_t(INT32_MAX));
317 return static_cast<int32_t>(delta);
318}
319
320void TimeStats::recordFrameDuration(nsecs_t startTime, nsecs_t endTime) {
321 if (!mEnabled.load()) return;
322
323 std::lock_guard<std::mutex> lock(mMutex);
Peiyong Lin65248e02020-04-18 21:15:07 -0700324 if (mPowerTime.powerMode == PowerMode::ON) {
Alec Mouri9519bf12019-11-15 16:54:44 -0800325 mTimeStats.frameDuration.insert(msBetween(startTime, endTime));
326 }
327}
328
Alec Mourie4034bb2019-11-19 12:45:54 -0800329void TimeStats::recordRenderEngineDuration(nsecs_t startTime, nsecs_t endTime) {
330 if (!mEnabled.load()) return;
331
332 std::lock_guard<std::mutex> lock(mMutex);
333 if (mGlobalRecord.renderEngineDurations.size() == MAX_NUM_TIME_RECORDS) {
334 ALOGE("RenderEngineTimes are already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
335 mGlobalRecord.renderEngineDurations.pop_front();
336 }
337 mGlobalRecord.renderEngineDurations.push_back({startTime, endTime});
338}
339
340void TimeStats::recordRenderEngineDuration(nsecs_t startTime,
341 const std::shared_ptr<FenceTime>& endTime) {
342 if (!mEnabled.load()) return;
343
344 std::lock_guard<std::mutex> lock(mMutex);
345 if (mGlobalRecord.renderEngineDurations.size() == MAX_NUM_TIME_RECORDS) {
346 ALOGE("RenderEngineTimes are already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
347 mGlobalRecord.renderEngineDurations.pop_front();
348 }
349 mGlobalRecord.renderEngineDurations.push_back({startTime, endTime});
350}
351
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800352bool TimeStats::recordReadyLocked(int32_t layerId, TimeRecord* timeRecord) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700353 if (!timeRecord->ready) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800354 ALOGV("[%d]-[%" PRIu64 "]-presentFence is still not received", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700355 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700356 return false;
357 }
358
359 if (timeRecord->acquireFence != nullptr) {
360 if (timeRecord->acquireFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
361 return false;
362 }
363 if (timeRecord->acquireFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700364 timeRecord->frameTime.acquireTime = timeRecord->acquireFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700365 timeRecord->acquireFence = nullptr;
366 } else {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800367 ALOGV("[%d]-[%" PRIu64 "]-acquireFence signal time is invalid", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700368 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700369 }
370 }
371
372 if (timeRecord->presentFence != nullptr) {
373 if (timeRecord->presentFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
374 return false;
375 }
376 if (timeRecord->presentFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700377 timeRecord->frameTime.presentTime = timeRecord->presentFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700378 timeRecord->presentFence = nullptr;
379 } else {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800380 ALOGV("[%d]-[%" PRIu64 "]-presentFence signal time invalid", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700381 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700382 }
383 }
384
385 return true;
386}
387
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800388void TimeStats::flushAvailableRecordsToStatsLocked(int32_t layerId) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700389 ATRACE_CALL();
390
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800391 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700392 TimeRecord& prevTimeRecord = layerRecord.prevTimeRecord;
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700393 std::deque<TimeRecord>& timeRecords = layerRecord.timeRecords;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700394 while (!timeRecords.empty()) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800395 if (!recordReadyLocked(layerId, &timeRecords[0])) break;
396 ALOGV("[%d]-[%" PRIu64 "]-presentFenceTime[%" PRId64 "]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700397 timeRecords[0].frameTime.frameNumber, timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700398
399 if (prevTimeRecord.ready) {
Yiwei Zhangeafa5cc2019-07-26 15:06:25 -0700400 const std::string& layerName = layerRecord.layerName;
Yiwei Zhangdc224042018-10-18 15:34:00 -0700401 if (!mTimeStats.stats.count(layerName)) {
402 mTimeStats.stats[layerName].layerName = layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700403 }
Yiwei Zhangdc224042018-10-18 15:34:00 -0700404 TimeStatsHelper::TimeStatsLayer& timeStatsLayer = mTimeStats.stats[layerName];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700405 timeStatsLayer.totalFrames++;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700406 timeStatsLayer.droppedFrames += layerRecord.droppedFrames;
Alec Mouri91f6df32020-01-30 08:48:58 -0800407 timeStatsLayer.lateAcquireFrames += layerRecord.lateAcquireFrames;
408 timeStatsLayer.badDesiredPresentFrames += layerRecord.badDesiredPresentFrames;
409
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700410 layerRecord.droppedFrames = 0;
Alec Mouri91f6df32020-01-30 08:48:58 -0800411 layerRecord.lateAcquireFrames = 0;
412 layerRecord.badDesiredPresentFrames = 0;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700413
414 const int32_t postToAcquireMs = msBetween(timeRecords[0].frameTime.postTime,
415 timeRecords[0].frameTime.acquireTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800416 ALOGV("[%d]-[%" PRIu64 "]-post2acquire[%d]", layerId,
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700417 timeRecords[0].frameTime.frameNumber, postToAcquireMs);
418 timeStatsLayer.deltas["post2acquire"].insert(postToAcquireMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700419
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700420 const int32_t postToPresentMs = msBetween(timeRecords[0].frameTime.postTime,
421 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800422 ALOGV("[%d]-[%" PRIu64 "]-post2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700423 timeRecords[0].frameTime.frameNumber, postToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700424 timeStatsLayer.deltas["post2present"].insert(postToPresentMs);
425
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700426 const int32_t acquireToPresentMs = msBetween(timeRecords[0].frameTime.acquireTime,
427 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800428 ALOGV("[%d]-[%" PRIu64 "]-acquire2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700429 timeRecords[0].frameTime.frameNumber, acquireToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700430 timeStatsLayer.deltas["acquire2present"].insert(acquireToPresentMs);
431
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700432 const int32_t latchToPresentMs = msBetween(timeRecords[0].frameTime.latchTime,
433 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800434 ALOGV("[%d]-[%" PRIu64 "]-latch2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700435 timeRecords[0].frameTime.frameNumber, latchToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700436 timeStatsLayer.deltas["latch2present"].insert(latchToPresentMs);
437
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700438 const int32_t desiredToPresentMs = msBetween(timeRecords[0].frameTime.desiredTime,
439 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800440 ALOGV("[%d]-[%" PRIu64 "]-desired2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700441 timeRecords[0].frameTime.frameNumber, desiredToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700442 timeStatsLayer.deltas["desired2present"].insert(desiredToPresentMs);
443
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700444 const int32_t presentToPresentMs = msBetween(prevTimeRecord.frameTime.presentTime,
445 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800446 ALOGV("[%d]-[%" PRIu64 "]-present2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700447 timeRecords[0].frameTime.frameNumber, presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700448 timeStatsLayer.deltas["present2present"].insert(presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700449 }
450 prevTimeRecord = timeRecords[0];
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700451 timeRecords.pop_front();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700452 layerRecord.waitData--;
453 }
454}
455
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700456static constexpr const char* kPopupWindowPrefix = "PopupWindow";
457static const size_t kMinLenLayerName = std::strlen(kPopupWindowPrefix);
Yiwei Zhangbd408322018-10-15 18:31:53 -0700458
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700459// Avoid tracking the "PopupWindow:<random hash>#<number>" layers
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700460static bool layerNameIsValid(const std::string& layerName) {
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700461 return layerName.length() >= kMinLenLayerName &&
462 layerName.compare(0, kMinLenLayerName, kPopupWindowPrefix) != 0;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700463}
464
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800465void TimeStats::setPostTime(int32_t layerId, uint64_t frameNumber, const std::string& layerName,
Yiwei Zhang8e8fe522018-11-02 18:34:07 -0700466 nsecs_t postTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700467 if (!mEnabled.load()) return;
468
469 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800470 ALOGV("[%d]-[%" PRIu64 "]-[%s]-PostTime[%" PRId64 "]", layerId, frameNumber, layerName.c_str(),
Yiwei Zhang8e8fe522018-11-02 18:34:07 -0700471 postTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700472
473 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange926ab52019-08-14 15:16:00 -0700474 if (!mTimeStats.stats.count(layerName) && mTimeStats.stats.size() >= MAX_NUM_LAYER_STATS) {
475 return;
476 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800477 if (!mTimeStatsTracker.count(layerId) && mTimeStatsTracker.size() < MAX_NUM_LAYER_RECORDS &&
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700478 layerNameIsValid(layerName)) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800479 mTimeStatsTracker[layerId].layerName = layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700480 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800481 if (!mTimeStatsTracker.count(layerId)) return;
482 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700483 if (layerRecord.timeRecords.size() == MAX_NUM_TIME_RECORDS) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800484 ALOGE("[%d]-[%s]-timeRecords is at its maximum size[%zu]. Ignore this when unittesting.",
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800485 layerId, layerRecord.layerName.c_str(), MAX_NUM_TIME_RECORDS);
486 mTimeStatsTracker.erase(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700487 return;
488 }
489 // For most media content, the acquireFence is invalid because the buffer is
490 // ready at the queueBuffer stage. In this case, acquireTime should be given
491 // a default value as postTime.
492 TimeRecord timeRecord = {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700493 .frameTime =
494 {
495 .frameNumber = frameNumber,
496 .postTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800497 .latchTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700498 .acquireTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800499 .desiredTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700500 },
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700501 };
502 layerRecord.timeRecords.push_back(timeRecord);
503 if (layerRecord.waitData < 0 ||
504 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
505 layerRecord.waitData = layerRecord.timeRecords.size() - 1;
506}
507
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800508void TimeStats::setLatchTime(int32_t layerId, uint64_t frameNumber, nsecs_t latchTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700509 if (!mEnabled.load()) return;
510
511 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800512 ALOGV("[%d]-[%" PRIu64 "]-LatchTime[%" PRId64 "]", layerId, frameNumber, latchTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700513
514 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800515 if (!mTimeStatsTracker.count(layerId)) return;
516 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700517 if (layerRecord.waitData < 0 ||
518 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
519 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700520 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700521 if (timeRecord.frameTime.frameNumber == frameNumber) {
522 timeRecord.frameTime.latchTime = latchTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700523 }
524}
525
Alec Mouri91f6df32020-01-30 08:48:58 -0800526void TimeStats::incrementLatchSkipped(int32_t layerId, LatchSkipReason reason) {
527 if (!mEnabled.load()) return;
528
529 ATRACE_CALL();
530 ALOGV("[%d]-LatchSkipped-Reason[%d]", layerId,
531 static_cast<std::underlying_type<LatchSkipReason>::type>(reason));
532
533 std::lock_guard<std::mutex> lock(mMutex);
534 if (!mTimeStatsTracker.count(layerId)) return;
535 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
536
537 switch (reason) {
538 case LatchSkipReason::LateAcquire:
539 layerRecord.lateAcquireFrames++;
540 break;
541 }
542}
543
544void TimeStats::incrementBadDesiredPresent(int32_t layerId) {
545 if (!mEnabled.load()) return;
546
547 ATRACE_CALL();
548 ALOGV("[%d]-BadDesiredPresent", layerId);
549
550 std::lock_guard<std::mutex> lock(mMutex);
551 if (!mTimeStatsTracker.count(layerId)) return;
552 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
553 layerRecord.badDesiredPresentFrames++;
554}
555
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800556void TimeStats::setDesiredTime(int32_t layerId, uint64_t frameNumber, nsecs_t desiredTime) {
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 "]-DesiredTime[%" PRId64 "]", layerId, frameNumber, desiredTime);
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.desiredTime = desiredTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700571 }
572}
573
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800574void TimeStats::setAcquireTime(int32_t layerId, uint64_t frameNumber, nsecs_t acquireTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700575 if (!mEnabled.load()) return;
576
577 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800578 ALOGV("[%d]-[%" PRIu64 "]-AcquireTime[%" PRId64 "]", layerId, frameNumber, acquireTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700579
580 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800581 if (!mTimeStatsTracker.count(layerId)) return;
582 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700583 if (layerRecord.waitData < 0 ||
584 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
585 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700586 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700587 if (timeRecord.frameTime.frameNumber == frameNumber) {
588 timeRecord.frameTime.acquireTime = acquireTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700589 }
590}
591
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800592void TimeStats::setAcquireFence(int32_t layerId, uint64_t frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700593 const std::shared_ptr<FenceTime>& acquireFence) {
594 if (!mEnabled.load()) return;
595
596 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800597 ALOGV("[%d]-[%" PRIu64 "]-AcquireFenceTime[%" PRId64 "]", layerId, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700598 acquireFence->getSignalTime());
599
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) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700608 timeRecord.acquireFence = acquireFence;
609 }
610}
611
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800612void TimeStats::setPresentTime(int32_t layerId, uint64_t frameNumber, nsecs_t presentTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700613 if (!mEnabled.load()) return;
614
615 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800616 ALOGV("[%d]-[%" PRIu64 "]-PresentTime[%" PRId64 "]", layerId, frameNumber, presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700617
618 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800619 if (!mTimeStatsTracker.count(layerId)) return;
620 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700621 if (layerRecord.waitData < 0 ||
622 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
623 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700624 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700625 if (timeRecord.frameTime.frameNumber == frameNumber) {
626 timeRecord.frameTime.presentTime = presentTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700627 timeRecord.ready = true;
628 layerRecord.waitData++;
629 }
630
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800631 flushAvailableRecordsToStatsLocked(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700632}
633
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800634void TimeStats::setPresentFence(int32_t layerId, uint64_t frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700635 const std::shared_ptr<FenceTime>& presentFence) {
636 if (!mEnabled.load()) return;
637
638 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800639 ALOGV("[%d]-[%" PRIu64 "]-PresentFenceTime[%" PRId64 "]", layerId, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700640 presentFence->getSignalTime());
641
642 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800643 if (!mTimeStatsTracker.count(layerId)) return;
644 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700645 if (layerRecord.waitData < 0 ||
646 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
647 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700648 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700649 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700650 timeRecord.presentFence = presentFence;
651 timeRecord.ready = true;
652 layerRecord.waitData++;
653 }
654
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800655 flushAvailableRecordsToStatsLocked(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700656}
657
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800658void TimeStats::onDestroy(int32_t layerId) {
Yiwei Zhangdc224042018-10-18 15:34:00 -0700659 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800660 ALOGV("[%d]-onDestroy", layerId);
Mikael Pessa90092f42019-08-26 17:22:04 -0700661 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800662 mTimeStatsTracker.erase(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700663}
664
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800665void TimeStats::removeTimeRecord(int32_t layerId, uint64_t frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700666 if (!mEnabled.load()) return;
667
668 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800669 ALOGV("[%d]-[%" PRIu64 "]-removeTimeRecord", layerId, frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700670
671 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800672 if (!mTimeStatsTracker.count(layerId)) return;
673 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700674 size_t removeAt = 0;
675 for (const TimeRecord& record : layerRecord.timeRecords) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700676 if (record.frameTime.frameNumber == frameNumber) break;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700677 removeAt++;
678 }
679 if (removeAt == layerRecord.timeRecords.size()) return;
680 layerRecord.timeRecords.erase(layerRecord.timeRecords.begin() + removeAt);
681 if (layerRecord.waitData > static_cast<int32_t>(removeAt)) {
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700682 layerRecord.waitData--;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700683 }
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700684 layerRecord.droppedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700685}
686
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700687void TimeStats::flushPowerTimeLocked() {
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700688 if (!mEnabled.load()) return;
689
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700690 nsecs_t curTime = systemTime();
691 // elapsedTime is in milliseconds.
692 int64_t elapsedTime = (curTime - mPowerTime.prevTime) / 1000000;
693
694 switch (mPowerTime.powerMode) {
Peiyong Lin65248e02020-04-18 21:15:07 -0700695 case PowerMode::ON:
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700696 mTimeStats.displayOnTime += elapsedTime;
697 break;
Peiyong Lin65248e02020-04-18 21:15:07 -0700698 case PowerMode::OFF:
699 case PowerMode::DOZE:
700 case PowerMode::DOZE_SUSPEND:
701 case PowerMode::ON_SUSPEND:
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700702 default:
703 break;
704 }
705
706 mPowerTime.prevTime = curTime;
707}
708
Peiyong Lin65248e02020-04-18 21:15:07 -0700709void TimeStats::setPowerMode(PowerMode powerMode) {
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700710 if (!mEnabled.load()) {
711 std::lock_guard<std::mutex> lock(mMutex);
712 mPowerTime.powerMode = powerMode;
713 return;
714 }
715
716 std::lock_guard<std::mutex> lock(mMutex);
717 if (powerMode == mPowerTime.powerMode) return;
718
719 flushPowerTimeLocked();
720 mPowerTime.powerMode = powerMode;
721}
722
Alec Mourifb571ea2019-01-24 18:42:10 -0800723void TimeStats::recordRefreshRate(uint32_t fps, nsecs_t duration) {
724 std::lock_guard<std::mutex> lock(mMutex);
725 if (mTimeStats.refreshRateStats.count(fps)) {
726 mTimeStats.refreshRateStats[fps] += duration;
727 } else {
728 mTimeStats.refreshRateStats.insert({fps, duration});
729 }
730}
731
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700732void TimeStats::flushAvailableGlobalRecordsToStatsLocked() {
733 ATRACE_CALL();
734
735 while (!mGlobalRecord.presentFences.empty()) {
736 const nsecs_t curPresentTime = mGlobalRecord.presentFences.front()->getSignalTime();
737 if (curPresentTime == Fence::SIGNAL_TIME_PENDING) break;
738
739 if (curPresentTime == Fence::SIGNAL_TIME_INVALID) {
740 ALOGE("GlobalPresentFence is invalid!");
741 mGlobalRecord.prevPresentTime = 0;
742 mGlobalRecord.presentFences.pop_front();
743 continue;
744 }
745
746 ALOGV("GlobalPresentFenceTime[%" PRId64 "]",
747 mGlobalRecord.presentFences.front()->getSignalTime());
748
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700749 if (mGlobalRecord.prevPresentTime != 0) {
750 const int32_t presentToPresentMs =
751 msBetween(mGlobalRecord.prevPresentTime, curPresentTime);
752 ALOGV("Global present2present[%d] prev[%" PRId64 "] curr[%" PRId64 "]",
753 presentToPresentMs, mGlobalRecord.prevPresentTime, curPresentTime);
754 mTimeStats.presentToPresent.insert(presentToPresentMs);
755 }
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700756
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700757 mGlobalRecord.prevPresentTime = curPresentTime;
758 mGlobalRecord.presentFences.pop_front();
759 }
Alec Mourie4034bb2019-11-19 12:45:54 -0800760 while (!mGlobalRecord.renderEngineDurations.empty()) {
761 const auto duration = mGlobalRecord.renderEngineDurations.front();
762 const auto& endTime = duration.endTime;
763
764 nsecs_t endNs = -1;
765
766 if (auto val = std::get_if<nsecs_t>(&endTime)) {
767 endNs = *val;
768 } else {
769 endNs = std::get<std::shared_ptr<FenceTime>>(endTime)->getSignalTime();
770 }
771
772 if (endNs == Fence::SIGNAL_TIME_PENDING) break;
773
774 if (endNs < 0) {
775 ALOGE("RenderEngineTiming is invalid!");
776 mGlobalRecord.renderEngineDurations.pop_front();
777 continue;
778 }
779
780 const int32_t renderEngineMs = msBetween(duration.startTime, endNs);
781 mTimeStats.renderEngineTiming.insert(renderEngineMs);
782
783 mGlobalRecord.renderEngineDurations.pop_front();
784 }
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700785}
786
787void TimeStats::setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) {
788 if (!mEnabled.load()) return;
789
790 ATRACE_CALL();
791 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700792 if (presentFence == nullptr || !presentFence->isValid()) {
793 mGlobalRecord.prevPresentTime = 0;
794 return;
795 }
796
Peiyong Lin65248e02020-04-18 21:15:07 -0700797 if (mPowerTime.powerMode != PowerMode::ON) {
798 // Try flushing the last present fence on PowerMode::ON.
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700799 flushAvailableGlobalRecordsToStatsLocked();
800 mGlobalRecord.presentFences.clear();
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700801 mGlobalRecord.prevPresentTime = 0;
802 return;
803 }
804
805 if (mGlobalRecord.presentFences.size() == MAX_NUM_TIME_RECORDS) {
806 // The front presentFence must be trapped in pending status in this
807 // case. Try dequeuing the front one to recover.
808 ALOGE("GlobalPresentFences is already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
809 mGlobalRecord.prevPresentTime = 0;
810 mGlobalRecord.presentFences.pop_front();
811 }
812
813 mGlobalRecord.presentFences.emplace_back(presentFence);
814 flushAvailableGlobalRecordsToStatsLocked();
815}
816
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700817void TimeStats::enable() {
818 if (mEnabled.load()) return;
819
820 ATRACE_CALL();
821
822 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700823 mEnabled.store(true);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700824 mTimeStats.statsStart = static_cast<int64_t>(std::time(0));
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700825 mPowerTime.prevTime = systemTime();
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700826 ALOGD("Enabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700827}
828
829void TimeStats::disable() {
830 if (!mEnabled.load()) return;
831
832 ATRACE_CALL();
833
834 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700835 flushPowerTimeLocked();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700836 mEnabled.store(false);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700837 mTimeStats.statsEnd = static_cast<int64_t>(std::time(0));
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700838 ALOGD("Disabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700839}
840
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000841void TimeStats::clearAll() {
842 std::lock_guard<std::mutex> lock(mMutex);
843 clearGlobalLocked();
844 clearLayersLocked();
845}
846
847void TimeStats::clearGlobalLocked() {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700848 ATRACE_CALL();
849
Yiwei Zhangdc224042018-10-18 15:34:00 -0700850 mTimeStats.statsStart = (mEnabled.load() ? static_cast<int64_t>(std::time(0)) : 0);
851 mTimeStats.statsEnd = 0;
852 mTimeStats.totalFrames = 0;
853 mTimeStats.missedFrames = 0;
854 mTimeStats.clientCompositionFrames = 0;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800855 mTimeStats.clientCompositionReusedFrames = 0;
Alec Mouri8de697e2020-03-19 10:52:01 -0700856 mTimeStats.refreshRateSwitches = 0;
Alec Mouri8f7a0102020-04-15 12:11:10 -0700857 mTimeStats.compositionStrategyChanges = 0;
Alec Mouri717bcb62020-02-10 17:07:19 -0800858 mTimeStats.displayEventConnectionsCount = 0;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700859 mTimeStats.displayOnTime = 0;
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700860 mTimeStats.presentToPresent.hist.clear();
Alec Mouri31ac64a2020-01-09 09:26:22 -0800861 mTimeStats.frameDuration.hist.clear();
862 mTimeStats.renderEngineTiming.hist.clear();
Alec Mourifb571ea2019-01-24 18:42:10 -0800863 mTimeStats.refreshRateStats.clear();
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700864 mPowerTime.prevTime = systemTime();
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700865 mGlobalRecord.prevPresentTime = 0;
866 mGlobalRecord.presentFences.clear();
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000867 ALOGD("Cleared global stats");
868}
869
870void TimeStats::clearLayersLocked() {
871 ATRACE_CALL();
872
873 mTimeStatsTracker.clear();
874 mTimeStats.stats.clear();
875 ALOGD("Cleared layer stats");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700876}
877
878bool TimeStats::isEnabled() {
879 return mEnabled.load();
880}
881
Yiwei Zhang5434a782018-12-05 18:06:32 -0800882void TimeStats::dump(bool asProto, std::optional<uint32_t> maxLayers, std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700883 ATRACE_CALL();
884
885 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700886 if (mTimeStats.statsStart == 0) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700887 return;
888 }
889
Yiwei Zhangdc224042018-10-18 15:34:00 -0700890 mTimeStats.statsEnd = static_cast<int64_t>(std::time(0));
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700891
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700892 flushPowerTimeLocked();
893
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700894 if (asProto) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700895 ALOGD("Dumping TimeStats as proto");
Yiwei Zhangdc224042018-10-18 15:34:00 -0700896 SFTimeStatsGlobalProto timeStatsProto = mTimeStats.toProto(maxLayers);
Dominik Laskowski46470112019-08-02 13:13:11 -0700897 result.append(timeStatsProto.SerializeAsString());
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700898 } else {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700899 ALOGD("Dumping TimeStats as text");
Yiwei Zhang5434a782018-12-05 18:06:32 -0800900 result.append(mTimeStats.toString(maxLayers));
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700901 result.append("\n");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700902 }
903}
904
Alec Mourifb571ea2019-01-24 18:42:10 -0800905} // namespace impl
906
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700907} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800908
909// TODO(b/129481165): remove the #pragma below and fix conversion issues
910#pragma clang diagnostic pop // ignored "-Wconversion"