blob: 8fbce56c37120a66b6069ae5ba37a9e0506b64d2 [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
Tej Singh2a457b62020-01-31 16:16:10 -080058AStatsManager_PullAtomCallbackReturn TimeStats::populateGlobalAtom(AStatsEventList* data) {
Alec Mouri37384342020-01-02 17:23:37 -080059 std::lock_guard<std::mutex> lock(mMutex);
60
61 if (mTimeStats.statsStart == 0) {
Tej Singh2a457b62020-01-31 16:16:10 -080062 return AStatsManager_PULL_SKIP;
Alec Mouri8e2f31b2020-01-16 22:04:35 +000063 }
Alec Mouri37384342020-01-02 17:23:37 -080064 flushPowerTimeLocked();
Alec Mouri8e2f31b2020-01-16 22:04:35 +000065
Tej Singh2a457b62020-01-31 16:16:10 -080066 AStatsEvent* event = mStatsDelegate->addStatsEventToPullData(data);
Alec Mouri37384342020-01-02 17:23:37 -080067 mStatsDelegate->statsEventSetAtomId(event, android::util::SURFACEFLINGER_STATS_GLOBAL_INFO);
68 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.totalFrames);
69 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.missedFrames);
70 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.clientCompositionFrames);
71 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.displayOnTime);
72 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.presentToPresent.totalTime());
Alec Mouri717bcb62020-02-10 17:07:19 -080073 mStatsDelegate->statsEventWriteInt32(event, mTimeStats.displayEventConnectionsCount);
Alec Mouri37384342020-01-02 17:23:37 -080074 mStatsDelegate->statsEventBuild(event);
75 clearGlobalLocked();
Alec Mouri8e2f31b2020-01-16 22:04:35 +000076
Tej Singh2a457b62020-01-31 16:16:10 -080077 return AStatsManager_PULL_SUCCESS;
Alec Mouri8e2f31b2020-01-16 22:04:35 +000078}
79
Alec Mouri37384342020-01-02 17:23:37 -080080namespace {
81// Histograms align with the order of fields in SurfaceflingerStatsLayerInfo.
82const std::array<std::string, 6> kHistogramNames = {
83 "present2present", "post2present", "acquire2present",
84 "latch2present", "desired2present", "post2acquire",
85};
Alec Mouri8e2f31b2020-01-16 22:04:35 +000086
Alec Mouri37384342020-01-02 17:23:37 -080087std::string histogramToProtoByteString(const std::unordered_map<int32_t, int32_t>& histogram,
88 size_t maxPulledHistogramBuckets) {
89 auto buckets = std::vector<std::pair<int32_t, int32_t>>(histogram.begin(), histogram.end());
90 std::sort(buckets.begin(), buckets.end(),
91 [](std::pair<int32_t, int32_t>& left, std::pair<int32_t, int32_t>& right) {
92 return left.second > right.second;
93 });
94
95 util::ProtoOutputStream proto;
96 int histogramSize = 0;
97 for (const auto& bucket : buckets) {
98 if (++histogramSize > maxPulledHistogramBuckets) {
99 break;
100 }
101 proto.write(android::util::FIELD_TYPE_INT32 | android::util::FIELD_COUNT_REPEATED |
102 1 /* field id */,
103 (int32_t)bucket.first);
104 proto.write(android::util::FIELD_TYPE_INT64 | android::util::FIELD_COUNT_REPEATED |
105 2 /* field id */,
106 (int64_t)bucket.second);
107 }
108
109 std::string byteString;
110 proto.serializeToString(&byteString);
111 return byteString;
112}
113} // namespace
114
Tej Singh2a457b62020-01-31 16:16:10 -0800115AStatsManager_PullAtomCallbackReturn TimeStats::populateLayerAtom(AStatsEventList* data) {
Alec Mouri37384342020-01-02 17:23:37 -0800116 std::lock_guard<std::mutex> lock(mMutex);
117
118 std::vector<TimeStatsHelper::TimeStatsLayer const*> dumpStats;
119 for (const auto& ele : mTimeStats.stats) {
120 dumpStats.push_back(&ele.second);
121 }
122
123 std::sort(dumpStats.begin(), dumpStats.end(),
124 [](TimeStatsHelper::TimeStatsLayer const* l,
125 TimeStatsHelper::TimeStatsLayer const* r) {
126 return l->totalFrames > r->totalFrames;
127 });
128
129 if (mMaxPulledLayers < dumpStats.size()) {
130 dumpStats.resize(mMaxPulledLayers);
131 }
132
133 for (const auto& layer : dumpStats) {
Tej Singh2a457b62020-01-31 16:16:10 -0800134 AStatsEvent* event = mStatsDelegate->addStatsEventToPullData(data);
Alec Mouri37384342020-01-02 17:23:37 -0800135 mStatsDelegate->statsEventSetAtomId(event, android::util::SURFACEFLINGER_STATS_LAYER_INFO);
136 mStatsDelegate->statsEventWriteString8(event, layer->layerName.c_str());
137 mStatsDelegate->statsEventWriteInt64(event, layer->totalFrames);
138 mStatsDelegate->statsEventWriteInt64(event, layer->droppedFrames);
139
140 for (const auto& name : kHistogramNames) {
141 const auto& histogram = layer->deltas.find(name);
142 if (histogram == layer->deltas.cend()) {
143 mStatsDelegate->statsEventWriteByteArray(event, nullptr, 0);
144 } else {
145 std::string bytes = histogramToProtoByteString(histogram->second.hist,
146 mMaxPulledHistogramBuckets);
147 mStatsDelegate->statsEventWriteByteArray(event, (const uint8_t*)bytes.c_str(),
148 bytes.size());
149 }
150 }
151
152 mStatsDelegate->statsEventBuild(event);
153 }
154 clearLayersLocked();
155
Tej Singh2a457b62020-01-31 16:16:10 -0800156 return AStatsManager_PULL_SUCCESS;
Alec Mouri37384342020-01-02 17:23:37 -0800157}
158
159TimeStats::TimeStats() : TimeStats(nullptr, std::nullopt, std::nullopt) {}
160
161TimeStats::TimeStats(std::unique_ptr<StatsEventDelegate> statsDelegate,
162 std::optional<size_t> maxPulledLayers,
163 std::optional<size_t> maxPulledHistogramBuckets) {
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000164 if (statsDelegate != nullptr) {
165 mStatsDelegate = std::move(statsDelegate);
166 }
Alec Mouri37384342020-01-02 17:23:37 -0800167
168 if (maxPulledLayers) {
169 mMaxPulledLayers = *maxPulledLayers;
170 }
171
172 if (maxPulledHistogramBuckets) {
173 mMaxPulledHistogramBuckets = *maxPulledHistogramBuckets;
174 }
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000175}
176
Alec Mouri3ecd5cd2020-01-29 12:53:07 -0800177TimeStats::~TimeStats() {
178 std::lock_guard<std::mutex> lock(mMutex);
179 mStatsDelegate->unregisterStatsPullAtomCallback(
180 android::util::SURFACEFLINGER_STATS_GLOBAL_INFO);
181 mStatsDelegate->unregisterStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_LAYER_INFO);
182}
183
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000184void TimeStats::onBootFinished() {
Alec Mouri3ecd5cd2020-01-29 12:53:07 -0800185 std::lock_guard<std::mutex> lock(mMutex);
186 mStatsDelegate->registerStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_GLOBAL_INFO,
187 TimeStats::pullAtomCallback, nullptr, this);
188 mStatsDelegate->registerStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_LAYER_INFO,
189 TimeStats::pullAtomCallback, nullptr, this);
Alec Mourib3885ad2019-09-06 17:08:55 -0700190}
191
Dominik Laskowskic2867142019-01-21 11:33:38 -0800192void TimeStats::parseArgs(bool asProto, const Vector<String16>& args, std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700193 ATRACE_CALL();
194
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700195 std::unordered_map<std::string, int32_t> argsMap;
Dominik Laskowskic2867142019-01-21 11:33:38 -0800196 for (size_t index = 0; index < args.size(); ++index) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700197 argsMap[std::string(String8(args[index]).c_str())] = index;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700198 }
199
200 if (argsMap.count("-disable")) {
201 disable();
202 }
203
204 if (argsMap.count("-dump")) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700205 std::optional<uint32_t> maxLayers = std::nullopt;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700206 auto iter = argsMap.find("-maxlayers");
207 if (iter != argsMap.end() && iter->second + 1 < static_cast<int32_t>(args.size())) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700208 int64_t value = strtol(String8(args[iter->second + 1]).c_str(), nullptr, 10);
209 value = std::clamp(value, int64_t(0), int64_t(UINT32_MAX));
210 maxLayers = static_cast<uint32_t>(value);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700211 }
212
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700213 dump(asProto, maxLayers, result);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700214 }
215
216 if (argsMap.count("-clear")) {
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000217 clearAll();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700218 }
219
220 if (argsMap.count("-enable")) {
221 enable();
222 }
223}
224
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700225std::string TimeStats::miniDump() {
226 ATRACE_CALL();
227
228 std::string result = "TimeStats miniDump:\n";
229 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange926ab52019-08-14 15:16:00 -0700230 android::base::StringAppendF(&result, "Number of layers currently being tracked is %zu\n",
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700231 mTimeStatsTracker.size());
Yiwei Zhange926ab52019-08-14 15:16:00 -0700232 android::base::StringAppendF(&result, "Number of layers in the stats pool is %zu\n",
233 mTimeStats.stats.size());
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700234 return result;
235}
236
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700237void TimeStats::incrementTotalFrames() {
238 if (!mEnabled.load()) return;
239
240 ATRACE_CALL();
241
242 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700243 mTimeStats.totalFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700244}
245
Yiwei Zhang621f9d42018-05-07 10:40:55 -0700246void TimeStats::incrementMissedFrames() {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700247 if (!mEnabled.load()) return;
248
249 ATRACE_CALL();
250
251 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700252 mTimeStats.missedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700253}
254
255void TimeStats::incrementClientCompositionFrames() {
256 if (!mEnabled.load()) return;
257
258 ATRACE_CALL();
259
260 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700261 mTimeStats.clientCompositionFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700262}
263
Vishnu Nair9b079a22020-01-21 14:36:08 -0800264void TimeStats::incrementClientCompositionReusedFrames() {
265 if (!mEnabled.load()) return;
266
267 ATRACE_CALL();
268
269 std::lock_guard<std::mutex> lock(mMutex);
270 mTimeStats.clientCompositionReusedFrames++;
271}
272
Alec Mouri717bcb62020-02-10 17:07:19 -0800273void TimeStats::recordDisplayEventConnectionCount(int32_t count) {
274 if (!mEnabled.load()) return;
275
276 ATRACE_CALL();
277
278 std::lock_guard<std::mutex> lock(mMutex);
279 mTimeStats.displayEventConnectionsCount =
280 std::max(mTimeStats.displayEventConnectionsCount, count);
281}
282
Alec Mouri9519bf12019-11-15 16:54:44 -0800283static int32_t msBetween(nsecs_t start, nsecs_t end) {
284 int64_t delta = std::chrono::duration_cast<std::chrono::milliseconds>(
285 std::chrono::nanoseconds(end - start))
286 .count();
287 delta = std::clamp(delta, int64_t(INT32_MIN), int64_t(INT32_MAX));
288 return static_cast<int32_t>(delta);
289}
290
291void TimeStats::recordFrameDuration(nsecs_t startTime, nsecs_t endTime) {
292 if (!mEnabled.load()) return;
293
294 std::lock_guard<std::mutex> lock(mMutex);
295 if (mPowerTime.powerMode == HWC_POWER_MODE_NORMAL) {
296 mTimeStats.frameDuration.insert(msBetween(startTime, endTime));
297 }
298}
299
Alec Mourie4034bb2019-11-19 12:45:54 -0800300void TimeStats::recordRenderEngineDuration(nsecs_t startTime, nsecs_t 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
311void TimeStats::recordRenderEngineDuration(nsecs_t startTime,
312 const std::shared_ptr<FenceTime>& 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
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800323bool TimeStats::recordReadyLocked(int32_t layerId, TimeRecord* timeRecord) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700324 if (!timeRecord->ready) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800325 ALOGV("[%d]-[%" PRIu64 "]-presentFence is still not received", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700326 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700327 return false;
328 }
329
330 if (timeRecord->acquireFence != nullptr) {
331 if (timeRecord->acquireFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
332 return false;
333 }
334 if (timeRecord->acquireFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700335 timeRecord->frameTime.acquireTime = timeRecord->acquireFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700336 timeRecord->acquireFence = nullptr;
337 } else {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800338 ALOGV("[%d]-[%" PRIu64 "]-acquireFence signal time is invalid", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700339 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700340 }
341 }
342
343 if (timeRecord->presentFence != nullptr) {
344 if (timeRecord->presentFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
345 return false;
346 }
347 if (timeRecord->presentFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700348 timeRecord->frameTime.presentTime = timeRecord->presentFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700349 timeRecord->presentFence = nullptr;
350 } else {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800351 ALOGV("[%d]-[%" PRIu64 "]-presentFence signal time invalid", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700352 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700353 }
354 }
355
356 return true;
357}
358
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800359void TimeStats::flushAvailableRecordsToStatsLocked(int32_t layerId) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700360 ATRACE_CALL();
361
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800362 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700363 TimeRecord& prevTimeRecord = layerRecord.prevTimeRecord;
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700364 std::deque<TimeRecord>& timeRecords = layerRecord.timeRecords;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700365 while (!timeRecords.empty()) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800366 if (!recordReadyLocked(layerId, &timeRecords[0])) break;
367 ALOGV("[%d]-[%" PRIu64 "]-presentFenceTime[%" PRId64 "]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700368 timeRecords[0].frameTime.frameNumber, timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700369
370 if (prevTimeRecord.ready) {
Yiwei Zhangeafa5cc2019-07-26 15:06:25 -0700371 const std::string& layerName = layerRecord.layerName;
Yiwei Zhangdc224042018-10-18 15:34:00 -0700372 if (!mTimeStats.stats.count(layerName)) {
373 mTimeStats.stats[layerName].layerName = layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700374 }
Yiwei Zhangdc224042018-10-18 15:34:00 -0700375 TimeStatsHelper::TimeStatsLayer& timeStatsLayer = mTimeStats.stats[layerName];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700376 timeStatsLayer.totalFrames++;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700377 timeStatsLayer.droppedFrames += layerRecord.droppedFrames;
Alec Mouri91f6df32020-01-30 08:48:58 -0800378 timeStatsLayer.lateAcquireFrames += layerRecord.lateAcquireFrames;
379 timeStatsLayer.badDesiredPresentFrames += layerRecord.badDesiredPresentFrames;
380
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700381 layerRecord.droppedFrames = 0;
Alec Mouri91f6df32020-01-30 08:48:58 -0800382 layerRecord.lateAcquireFrames = 0;
383 layerRecord.badDesiredPresentFrames = 0;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700384
385 const int32_t postToAcquireMs = msBetween(timeRecords[0].frameTime.postTime,
386 timeRecords[0].frameTime.acquireTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800387 ALOGV("[%d]-[%" PRIu64 "]-post2acquire[%d]", layerId,
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700388 timeRecords[0].frameTime.frameNumber, postToAcquireMs);
389 timeStatsLayer.deltas["post2acquire"].insert(postToAcquireMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700390
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700391 const int32_t postToPresentMs = msBetween(timeRecords[0].frameTime.postTime,
392 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800393 ALOGV("[%d]-[%" PRIu64 "]-post2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700394 timeRecords[0].frameTime.frameNumber, postToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700395 timeStatsLayer.deltas["post2present"].insert(postToPresentMs);
396
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700397 const int32_t acquireToPresentMs = msBetween(timeRecords[0].frameTime.acquireTime,
398 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800399 ALOGV("[%d]-[%" PRIu64 "]-acquire2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700400 timeRecords[0].frameTime.frameNumber, acquireToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700401 timeStatsLayer.deltas["acquire2present"].insert(acquireToPresentMs);
402
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700403 const int32_t latchToPresentMs = msBetween(timeRecords[0].frameTime.latchTime,
404 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800405 ALOGV("[%d]-[%" PRIu64 "]-latch2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700406 timeRecords[0].frameTime.frameNumber, latchToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700407 timeStatsLayer.deltas["latch2present"].insert(latchToPresentMs);
408
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700409 const int32_t desiredToPresentMs = msBetween(timeRecords[0].frameTime.desiredTime,
410 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800411 ALOGV("[%d]-[%" PRIu64 "]-desired2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700412 timeRecords[0].frameTime.frameNumber, desiredToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700413 timeStatsLayer.deltas["desired2present"].insert(desiredToPresentMs);
414
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700415 const int32_t presentToPresentMs = msBetween(prevTimeRecord.frameTime.presentTime,
416 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800417 ALOGV("[%d]-[%" PRIu64 "]-present2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700418 timeRecords[0].frameTime.frameNumber, presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700419 timeStatsLayer.deltas["present2present"].insert(presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700420 }
421 prevTimeRecord = timeRecords[0];
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700422 timeRecords.pop_front();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700423 layerRecord.waitData--;
424 }
425}
426
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700427static constexpr const char* kPopupWindowPrefix = "PopupWindow";
428static const size_t kMinLenLayerName = std::strlen(kPopupWindowPrefix);
Yiwei Zhangbd408322018-10-15 18:31:53 -0700429
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700430// Avoid tracking the "PopupWindow:<random hash>#<number>" layers
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700431static bool layerNameIsValid(const std::string& layerName) {
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700432 return layerName.length() >= kMinLenLayerName &&
433 layerName.compare(0, kMinLenLayerName, kPopupWindowPrefix) != 0;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700434}
435
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800436void TimeStats::setPostTime(int32_t layerId, uint64_t frameNumber, const std::string& layerName,
Yiwei Zhang8e8fe522018-11-02 18:34:07 -0700437 nsecs_t postTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700438 if (!mEnabled.load()) return;
439
440 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800441 ALOGV("[%d]-[%" PRIu64 "]-[%s]-PostTime[%" PRId64 "]", layerId, frameNumber, layerName.c_str(),
Yiwei Zhang8e8fe522018-11-02 18:34:07 -0700442 postTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700443
444 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange926ab52019-08-14 15:16:00 -0700445 if (!mTimeStats.stats.count(layerName) && mTimeStats.stats.size() >= MAX_NUM_LAYER_STATS) {
446 return;
447 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800448 if (!mTimeStatsTracker.count(layerId) && mTimeStatsTracker.size() < MAX_NUM_LAYER_RECORDS &&
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700449 layerNameIsValid(layerName)) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800450 mTimeStatsTracker[layerId].layerName = layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700451 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800452 if (!mTimeStatsTracker.count(layerId)) return;
453 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700454 if (layerRecord.timeRecords.size() == MAX_NUM_TIME_RECORDS) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800455 ALOGE("[%d]-[%s]-timeRecords is at its maximum size[%zu]. Ignore this when unittesting.",
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800456 layerId, layerRecord.layerName.c_str(), MAX_NUM_TIME_RECORDS);
457 mTimeStatsTracker.erase(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700458 return;
459 }
460 // For most media content, the acquireFence is invalid because the buffer is
461 // ready at the queueBuffer stage. In this case, acquireTime should be given
462 // a default value as postTime.
463 TimeRecord timeRecord = {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700464 .frameTime =
465 {
466 .frameNumber = frameNumber,
467 .postTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800468 .latchTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700469 .acquireTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800470 .desiredTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700471 },
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700472 };
473 layerRecord.timeRecords.push_back(timeRecord);
474 if (layerRecord.waitData < 0 ||
475 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
476 layerRecord.waitData = layerRecord.timeRecords.size() - 1;
477}
478
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800479void TimeStats::setLatchTime(int32_t layerId, uint64_t frameNumber, nsecs_t latchTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700480 if (!mEnabled.load()) return;
481
482 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800483 ALOGV("[%d]-[%" PRIu64 "]-LatchTime[%" PRId64 "]", layerId, frameNumber, latchTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700484
485 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800486 if (!mTimeStatsTracker.count(layerId)) return;
487 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700488 if (layerRecord.waitData < 0 ||
489 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
490 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700491 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700492 if (timeRecord.frameTime.frameNumber == frameNumber) {
493 timeRecord.frameTime.latchTime = latchTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700494 }
495}
496
Alec Mouri91f6df32020-01-30 08:48:58 -0800497void TimeStats::incrementLatchSkipped(int32_t layerId, LatchSkipReason reason) {
498 if (!mEnabled.load()) return;
499
500 ATRACE_CALL();
501 ALOGV("[%d]-LatchSkipped-Reason[%d]", layerId,
502 static_cast<std::underlying_type<LatchSkipReason>::type>(reason));
503
504 std::lock_guard<std::mutex> lock(mMutex);
505 if (!mTimeStatsTracker.count(layerId)) return;
506 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
507
508 switch (reason) {
509 case LatchSkipReason::LateAcquire:
510 layerRecord.lateAcquireFrames++;
511 break;
512 }
513}
514
515void TimeStats::incrementBadDesiredPresent(int32_t layerId) {
516 if (!mEnabled.load()) return;
517
518 ATRACE_CALL();
519 ALOGV("[%d]-BadDesiredPresent", layerId);
520
521 std::lock_guard<std::mutex> lock(mMutex);
522 if (!mTimeStatsTracker.count(layerId)) return;
523 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
524 layerRecord.badDesiredPresentFrames++;
525}
526
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800527void TimeStats::setDesiredTime(int32_t layerId, uint64_t frameNumber, nsecs_t desiredTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700528 if (!mEnabled.load()) return;
529
530 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800531 ALOGV("[%d]-[%" PRIu64 "]-DesiredTime[%" PRId64 "]", layerId, frameNumber, desiredTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700532
533 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800534 if (!mTimeStatsTracker.count(layerId)) return;
535 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700536 if (layerRecord.waitData < 0 ||
537 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
538 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700539 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700540 if (timeRecord.frameTime.frameNumber == frameNumber) {
541 timeRecord.frameTime.desiredTime = desiredTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700542 }
543}
544
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800545void TimeStats::setAcquireTime(int32_t layerId, uint64_t frameNumber, nsecs_t acquireTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700546 if (!mEnabled.load()) return;
547
548 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800549 ALOGV("[%d]-[%" PRIu64 "]-AcquireTime[%" PRId64 "]", layerId, frameNumber, acquireTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700550
551 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800552 if (!mTimeStatsTracker.count(layerId)) return;
553 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700554 if (layerRecord.waitData < 0 ||
555 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
556 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700557 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700558 if (timeRecord.frameTime.frameNumber == frameNumber) {
559 timeRecord.frameTime.acquireTime = acquireTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700560 }
561}
562
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800563void TimeStats::setAcquireFence(int32_t layerId, uint64_t frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700564 const std::shared_ptr<FenceTime>& acquireFence) {
565 if (!mEnabled.load()) return;
566
567 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800568 ALOGV("[%d]-[%" PRIu64 "]-AcquireFenceTime[%" PRId64 "]", layerId, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700569 acquireFence->getSignalTime());
570
571 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800572 if (!mTimeStatsTracker.count(layerId)) return;
573 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700574 if (layerRecord.waitData < 0 ||
575 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
576 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700577 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700578 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700579 timeRecord.acquireFence = acquireFence;
580 }
581}
582
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800583void TimeStats::setPresentTime(int32_t layerId, uint64_t frameNumber, nsecs_t presentTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700584 if (!mEnabled.load()) return;
585
586 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800587 ALOGV("[%d]-[%" PRIu64 "]-PresentTime[%" PRId64 "]", layerId, frameNumber, presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700588
589 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800590 if (!mTimeStatsTracker.count(layerId)) return;
591 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700592 if (layerRecord.waitData < 0 ||
593 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
594 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700595 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700596 if (timeRecord.frameTime.frameNumber == frameNumber) {
597 timeRecord.frameTime.presentTime = presentTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700598 timeRecord.ready = true;
599 layerRecord.waitData++;
600 }
601
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800602 flushAvailableRecordsToStatsLocked(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700603}
604
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800605void TimeStats::setPresentFence(int32_t layerId, uint64_t frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700606 const std::shared_ptr<FenceTime>& presentFence) {
607 if (!mEnabled.load()) return;
608
609 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800610 ALOGV("[%d]-[%" PRIu64 "]-PresentFenceTime[%" PRId64 "]", layerId, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700611 presentFence->getSignalTime());
612
613 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800614 if (!mTimeStatsTracker.count(layerId)) return;
615 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700616 if (layerRecord.waitData < 0 ||
617 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
618 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700619 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700620 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700621 timeRecord.presentFence = presentFence;
622 timeRecord.ready = true;
623 layerRecord.waitData++;
624 }
625
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800626 flushAvailableRecordsToStatsLocked(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700627}
628
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800629void TimeStats::onDestroy(int32_t layerId) {
Yiwei Zhangdc224042018-10-18 15:34:00 -0700630 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800631 ALOGV("[%d]-onDestroy", layerId);
Mikael Pessa90092f42019-08-26 17:22:04 -0700632 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800633 mTimeStatsTracker.erase(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700634}
635
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800636void TimeStats::removeTimeRecord(int32_t layerId, uint64_t frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700637 if (!mEnabled.load()) return;
638
639 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800640 ALOGV("[%d]-[%" PRIu64 "]-removeTimeRecord", layerId, frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700641
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 Zhang0102ad22018-05-02 17:37:17 -0700645 size_t removeAt = 0;
646 for (const TimeRecord& record : layerRecord.timeRecords) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700647 if (record.frameTime.frameNumber == frameNumber) break;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700648 removeAt++;
649 }
650 if (removeAt == layerRecord.timeRecords.size()) return;
651 layerRecord.timeRecords.erase(layerRecord.timeRecords.begin() + removeAt);
652 if (layerRecord.waitData > static_cast<int32_t>(removeAt)) {
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700653 layerRecord.waitData--;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700654 }
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700655 layerRecord.droppedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700656}
657
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700658void TimeStats::flushPowerTimeLocked() {
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700659 if (!mEnabled.load()) return;
660
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700661 nsecs_t curTime = systemTime();
662 // elapsedTime is in milliseconds.
663 int64_t elapsedTime = (curTime - mPowerTime.prevTime) / 1000000;
664
665 switch (mPowerTime.powerMode) {
666 case HWC_POWER_MODE_NORMAL:
667 mTimeStats.displayOnTime += elapsedTime;
668 break;
669 case HWC_POWER_MODE_OFF:
670 case HWC_POWER_MODE_DOZE:
671 case HWC_POWER_MODE_DOZE_SUSPEND:
672 default:
673 break;
674 }
675
676 mPowerTime.prevTime = curTime;
677}
678
679void TimeStats::setPowerMode(int32_t powerMode) {
680 if (!mEnabled.load()) {
681 std::lock_guard<std::mutex> lock(mMutex);
682 mPowerTime.powerMode = powerMode;
683 return;
684 }
685
686 std::lock_guard<std::mutex> lock(mMutex);
687 if (powerMode == mPowerTime.powerMode) return;
688
689 flushPowerTimeLocked();
690 mPowerTime.powerMode = powerMode;
691}
692
Alec Mourifb571ea2019-01-24 18:42:10 -0800693void TimeStats::recordRefreshRate(uint32_t fps, nsecs_t duration) {
694 std::lock_guard<std::mutex> lock(mMutex);
695 if (mTimeStats.refreshRateStats.count(fps)) {
696 mTimeStats.refreshRateStats[fps] += duration;
697 } else {
698 mTimeStats.refreshRateStats.insert({fps, duration});
699 }
700}
701
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700702void TimeStats::flushAvailableGlobalRecordsToStatsLocked() {
703 ATRACE_CALL();
704
705 while (!mGlobalRecord.presentFences.empty()) {
706 const nsecs_t curPresentTime = mGlobalRecord.presentFences.front()->getSignalTime();
707 if (curPresentTime == Fence::SIGNAL_TIME_PENDING) break;
708
709 if (curPresentTime == Fence::SIGNAL_TIME_INVALID) {
710 ALOGE("GlobalPresentFence is invalid!");
711 mGlobalRecord.prevPresentTime = 0;
712 mGlobalRecord.presentFences.pop_front();
713 continue;
714 }
715
716 ALOGV("GlobalPresentFenceTime[%" PRId64 "]",
717 mGlobalRecord.presentFences.front()->getSignalTime());
718
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700719 if (mGlobalRecord.prevPresentTime != 0) {
720 const int32_t presentToPresentMs =
721 msBetween(mGlobalRecord.prevPresentTime, curPresentTime);
722 ALOGV("Global present2present[%d] prev[%" PRId64 "] curr[%" PRId64 "]",
723 presentToPresentMs, mGlobalRecord.prevPresentTime, curPresentTime);
724 mTimeStats.presentToPresent.insert(presentToPresentMs);
725 }
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700726
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700727 mGlobalRecord.prevPresentTime = curPresentTime;
728 mGlobalRecord.presentFences.pop_front();
729 }
Alec Mourie4034bb2019-11-19 12:45:54 -0800730 while (!mGlobalRecord.renderEngineDurations.empty()) {
731 const auto duration = mGlobalRecord.renderEngineDurations.front();
732 const auto& endTime = duration.endTime;
733
734 nsecs_t endNs = -1;
735
736 if (auto val = std::get_if<nsecs_t>(&endTime)) {
737 endNs = *val;
738 } else {
739 endNs = std::get<std::shared_ptr<FenceTime>>(endTime)->getSignalTime();
740 }
741
742 if (endNs == Fence::SIGNAL_TIME_PENDING) break;
743
744 if (endNs < 0) {
745 ALOGE("RenderEngineTiming is invalid!");
746 mGlobalRecord.renderEngineDurations.pop_front();
747 continue;
748 }
749
750 const int32_t renderEngineMs = msBetween(duration.startTime, endNs);
751 mTimeStats.renderEngineTiming.insert(renderEngineMs);
752
753 mGlobalRecord.renderEngineDurations.pop_front();
754 }
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700755}
756
757void TimeStats::setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) {
758 if (!mEnabled.load()) return;
759
760 ATRACE_CALL();
761 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700762 if (presentFence == nullptr || !presentFence->isValid()) {
763 mGlobalRecord.prevPresentTime = 0;
764 return;
765 }
766
767 if (mPowerTime.powerMode != HWC_POWER_MODE_NORMAL) {
768 // Try flushing the last present fence on HWC_POWER_MODE_NORMAL.
769 flushAvailableGlobalRecordsToStatsLocked();
770 mGlobalRecord.presentFences.clear();
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700771 mGlobalRecord.prevPresentTime = 0;
772 return;
773 }
774
775 if (mGlobalRecord.presentFences.size() == MAX_NUM_TIME_RECORDS) {
776 // The front presentFence must be trapped in pending status in this
777 // case. Try dequeuing the front one to recover.
778 ALOGE("GlobalPresentFences is already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
779 mGlobalRecord.prevPresentTime = 0;
780 mGlobalRecord.presentFences.pop_front();
781 }
782
783 mGlobalRecord.presentFences.emplace_back(presentFence);
784 flushAvailableGlobalRecordsToStatsLocked();
785}
786
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700787void TimeStats::enable() {
788 if (mEnabled.load()) return;
789
790 ATRACE_CALL();
791
792 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700793 mEnabled.store(true);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700794 mTimeStats.statsStart = static_cast<int64_t>(std::time(0));
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700795 mPowerTime.prevTime = systemTime();
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700796 ALOGD("Enabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700797}
798
799void TimeStats::disable() {
800 if (!mEnabled.load()) return;
801
802 ATRACE_CALL();
803
804 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700805 flushPowerTimeLocked();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700806 mEnabled.store(false);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700807 mTimeStats.statsEnd = static_cast<int64_t>(std::time(0));
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700808 ALOGD("Disabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700809}
810
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000811void TimeStats::clearAll() {
812 std::lock_guard<std::mutex> lock(mMutex);
813 clearGlobalLocked();
814 clearLayersLocked();
815}
816
817void TimeStats::clearGlobalLocked() {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700818 ATRACE_CALL();
819
Yiwei Zhangdc224042018-10-18 15:34:00 -0700820 mTimeStats.statsStart = (mEnabled.load() ? static_cast<int64_t>(std::time(0)) : 0);
821 mTimeStats.statsEnd = 0;
822 mTimeStats.totalFrames = 0;
823 mTimeStats.missedFrames = 0;
824 mTimeStats.clientCompositionFrames = 0;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800825 mTimeStats.clientCompositionReusedFrames = 0;
Alec Mouri717bcb62020-02-10 17:07:19 -0800826 mTimeStats.displayEventConnectionsCount = 0;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700827 mTimeStats.displayOnTime = 0;
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700828 mTimeStats.presentToPresent.hist.clear();
Alec Mouri31ac64a2020-01-09 09:26:22 -0800829 mTimeStats.frameDuration.hist.clear();
830 mTimeStats.renderEngineTiming.hist.clear();
Alec Mourifb571ea2019-01-24 18:42:10 -0800831 mTimeStats.refreshRateStats.clear();
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700832 mPowerTime.prevTime = systemTime();
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700833 mGlobalRecord.prevPresentTime = 0;
834 mGlobalRecord.presentFences.clear();
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000835 ALOGD("Cleared global stats");
836}
837
838void TimeStats::clearLayersLocked() {
839 ATRACE_CALL();
840
841 mTimeStatsTracker.clear();
842 mTimeStats.stats.clear();
843 ALOGD("Cleared layer stats");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700844}
845
846bool TimeStats::isEnabled() {
847 return mEnabled.load();
848}
849
Yiwei Zhang5434a782018-12-05 18:06:32 -0800850void TimeStats::dump(bool asProto, std::optional<uint32_t> maxLayers, std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700851 ATRACE_CALL();
852
853 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700854 if (mTimeStats.statsStart == 0) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700855 return;
856 }
857
Yiwei Zhangdc224042018-10-18 15:34:00 -0700858 mTimeStats.statsEnd = static_cast<int64_t>(std::time(0));
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700859
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700860 flushPowerTimeLocked();
861
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700862 if (asProto) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700863 ALOGD("Dumping TimeStats as proto");
Yiwei Zhangdc224042018-10-18 15:34:00 -0700864 SFTimeStatsGlobalProto timeStatsProto = mTimeStats.toProto(maxLayers);
Dominik Laskowski46470112019-08-02 13:13:11 -0700865 result.append(timeStatsProto.SerializeAsString());
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700866 } else {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700867 ALOGD("Dumping TimeStats as text");
Yiwei Zhang5434a782018-12-05 18:06:32 -0800868 result.append(mTimeStats.toString(maxLayers));
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700869 result.append("\n");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700870 }
871}
872
Alec Mourifb571ea2019-01-24 18:42:10 -0800873} // namespace impl
874
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700875} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800876
877// TODO(b/129481165): remove the #pragma below and fix conversion issues
878#pragma clang diagnostic pop // ignored "-Wconversion"