blob: 493a7093a0e4cce50cf79d3edb62e4f04cd29f42 [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());
73 mStatsDelegate->statsEventBuild(event);
74 clearGlobalLocked();
Alec Mouri8e2f31b2020-01-16 22:04:35 +000075
Tej Singh2a457b62020-01-31 16:16:10 -080076 return AStatsManager_PULL_SUCCESS;
Alec Mouri8e2f31b2020-01-16 22:04:35 +000077}
78
Alec Mouri37384342020-01-02 17:23:37 -080079namespace {
80// Histograms align with the order of fields in SurfaceflingerStatsLayerInfo.
81const std::array<std::string, 6> kHistogramNames = {
82 "present2present", "post2present", "acquire2present",
83 "latch2present", "desired2present", "post2acquire",
84};
Alec Mouri8e2f31b2020-01-16 22:04:35 +000085
Alec Mouri37384342020-01-02 17:23:37 -080086std::string histogramToProtoByteString(const std::unordered_map<int32_t, int32_t>& histogram,
87 size_t maxPulledHistogramBuckets) {
88 auto buckets = std::vector<std::pair<int32_t, int32_t>>(histogram.begin(), histogram.end());
89 std::sort(buckets.begin(), buckets.end(),
90 [](std::pair<int32_t, int32_t>& left, std::pair<int32_t, int32_t>& right) {
91 return left.second > right.second;
92 });
93
94 util::ProtoOutputStream proto;
95 int histogramSize = 0;
96 for (const auto& bucket : buckets) {
97 if (++histogramSize > maxPulledHistogramBuckets) {
98 break;
99 }
100 proto.write(android::util::FIELD_TYPE_INT32 | android::util::FIELD_COUNT_REPEATED |
101 1 /* field id */,
102 (int32_t)bucket.first);
103 proto.write(android::util::FIELD_TYPE_INT64 | android::util::FIELD_COUNT_REPEATED |
104 2 /* field id */,
105 (int64_t)bucket.second);
106 }
107
108 std::string byteString;
109 proto.serializeToString(&byteString);
110 return byteString;
111}
112} // namespace
113
Tej Singh2a457b62020-01-31 16:16:10 -0800114AStatsManager_PullAtomCallbackReturn TimeStats::populateLayerAtom(AStatsEventList* data) {
Alec Mouri37384342020-01-02 17:23:37 -0800115 std::lock_guard<std::mutex> lock(mMutex);
116
117 std::vector<TimeStatsHelper::TimeStatsLayer const*> dumpStats;
118 for (const auto& ele : mTimeStats.stats) {
119 dumpStats.push_back(&ele.second);
120 }
121
122 std::sort(dumpStats.begin(), dumpStats.end(),
123 [](TimeStatsHelper::TimeStatsLayer const* l,
124 TimeStatsHelper::TimeStatsLayer const* r) {
125 return l->totalFrames > r->totalFrames;
126 });
127
128 if (mMaxPulledLayers < dumpStats.size()) {
129 dumpStats.resize(mMaxPulledLayers);
130 }
131
132 for (const auto& layer : dumpStats) {
Tej Singh2a457b62020-01-31 16:16:10 -0800133 AStatsEvent* event = mStatsDelegate->addStatsEventToPullData(data);
Alec Mouri37384342020-01-02 17:23:37 -0800134 mStatsDelegate->statsEventSetAtomId(event, android::util::SURFACEFLINGER_STATS_LAYER_INFO);
135 mStatsDelegate->statsEventWriteString8(event, layer->layerName.c_str());
136 mStatsDelegate->statsEventWriteInt64(event, layer->totalFrames);
137 mStatsDelegate->statsEventWriteInt64(event, layer->droppedFrames);
138
139 for (const auto& name : kHistogramNames) {
140 const auto& histogram = layer->deltas.find(name);
141 if (histogram == layer->deltas.cend()) {
142 mStatsDelegate->statsEventWriteByteArray(event, nullptr, 0);
143 } else {
144 std::string bytes = histogramToProtoByteString(histogram->second.hist,
145 mMaxPulledHistogramBuckets);
146 mStatsDelegate->statsEventWriteByteArray(event, (const uint8_t*)bytes.c_str(),
147 bytes.size());
148 }
149 }
150
151 mStatsDelegate->statsEventBuild(event);
152 }
153 clearLayersLocked();
154
Tej Singh2a457b62020-01-31 16:16:10 -0800155 return AStatsManager_PULL_SUCCESS;
Alec Mouri37384342020-01-02 17:23:37 -0800156}
157
158TimeStats::TimeStats() : TimeStats(nullptr, std::nullopt, std::nullopt) {}
159
160TimeStats::TimeStats(std::unique_ptr<StatsEventDelegate> statsDelegate,
161 std::optional<size_t> maxPulledLayers,
162 std::optional<size_t> maxPulledHistogramBuckets) {
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000163 if (statsDelegate != nullptr) {
164 mStatsDelegate = std::move(statsDelegate);
165 }
Alec Mouri37384342020-01-02 17:23:37 -0800166
167 if (maxPulledLayers) {
168 mMaxPulledLayers = *maxPulledLayers;
169 }
170
171 if (maxPulledHistogramBuckets) {
172 mMaxPulledHistogramBuckets = *maxPulledHistogramBuckets;
173 }
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000174}
175
Alec Mouri3ecd5cd2020-01-29 12:53:07 -0800176TimeStats::~TimeStats() {
177 std::lock_guard<std::mutex> lock(mMutex);
178 mStatsDelegate->unregisterStatsPullAtomCallback(
179 android::util::SURFACEFLINGER_STATS_GLOBAL_INFO);
180 mStatsDelegate->unregisterStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_LAYER_INFO);
181}
182
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000183void TimeStats::onBootFinished() {
Alec Mouri3ecd5cd2020-01-29 12:53:07 -0800184 std::lock_guard<std::mutex> lock(mMutex);
185 mStatsDelegate->registerStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_GLOBAL_INFO,
186 TimeStats::pullAtomCallback, nullptr, this);
187 mStatsDelegate->registerStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_LAYER_INFO,
188 TimeStats::pullAtomCallback, nullptr, this);
Alec Mourib3885ad2019-09-06 17:08:55 -0700189}
190
Dominik Laskowskic2867142019-01-21 11:33:38 -0800191void TimeStats::parseArgs(bool asProto, const Vector<String16>& args, std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700192 ATRACE_CALL();
193
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700194 std::unordered_map<std::string, int32_t> argsMap;
Dominik Laskowskic2867142019-01-21 11:33:38 -0800195 for (size_t index = 0; index < args.size(); ++index) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700196 argsMap[std::string(String8(args[index]).c_str())] = index;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700197 }
198
199 if (argsMap.count("-disable")) {
200 disable();
201 }
202
203 if (argsMap.count("-dump")) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700204 std::optional<uint32_t> maxLayers = std::nullopt;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700205 auto iter = argsMap.find("-maxlayers");
206 if (iter != argsMap.end() && iter->second + 1 < static_cast<int32_t>(args.size())) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700207 int64_t value = strtol(String8(args[iter->second + 1]).c_str(), nullptr, 10);
208 value = std::clamp(value, int64_t(0), int64_t(UINT32_MAX));
209 maxLayers = static_cast<uint32_t>(value);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700210 }
211
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700212 dump(asProto, maxLayers, result);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700213 }
214
215 if (argsMap.count("-clear")) {
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000216 clearAll();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700217 }
218
219 if (argsMap.count("-enable")) {
220 enable();
221 }
222}
223
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700224std::string TimeStats::miniDump() {
225 ATRACE_CALL();
226
227 std::string result = "TimeStats miniDump:\n";
228 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange926ab52019-08-14 15:16:00 -0700229 android::base::StringAppendF(&result, "Number of layers currently being tracked is %zu\n",
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700230 mTimeStatsTracker.size());
Yiwei Zhange926ab52019-08-14 15:16:00 -0700231 android::base::StringAppendF(&result, "Number of layers in the stats pool is %zu\n",
232 mTimeStats.stats.size());
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700233 return result;
234}
235
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700236void TimeStats::incrementTotalFrames() {
237 if (!mEnabled.load()) return;
238
239 ATRACE_CALL();
240
241 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700242 mTimeStats.totalFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700243}
244
Yiwei Zhang621f9d42018-05-07 10:40:55 -0700245void TimeStats::incrementMissedFrames() {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700246 if (!mEnabled.load()) return;
247
248 ATRACE_CALL();
249
250 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700251 mTimeStats.missedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700252}
253
254void TimeStats::incrementClientCompositionFrames() {
255 if (!mEnabled.load()) return;
256
257 ATRACE_CALL();
258
259 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700260 mTimeStats.clientCompositionFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700261}
262
Vishnu Nair9b079a22020-01-21 14:36:08 -0800263void TimeStats::incrementClientCompositionReusedFrames() {
264 if (!mEnabled.load()) return;
265
266 ATRACE_CALL();
267
268 std::lock_guard<std::mutex> lock(mMutex);
269 mTimeStats.clientCompositionReusedFrames++;
270}
271
Alec Mouri9519bf12019-11-15 16:54:44 -0800272static int32_t msBetween(nsecs_t start, nsecs_t end) {
273 int64_t delta = std::chrono::duration_cast<std::chrono::milliseconds>(
274 std::chrono::nanoseconds(end - start))
275 .count();
276 delta = std::clamp(delta, int64_t(INT32_MIN), int64_t(INT32_MAX));
277 return static_cast<int32_t>(delta);
278}
279
280void TimeStats::recordFrameDuration(nsecs_t startTime, nsecs_t endTime) {
281 if (!mEnabled.load()) return;
282
283 std::lock_guard<std::mutex> lock(mMutex);
284 if (mPowerTime.powerMode == HWC_POWER_MODE_NORMAL) {
285 mTimeStats.frameDuration.insert(msBetween(startTime, endTime));
286 }
287}
288
Alec Mourie4034bb2019-11-19 12:45:54 -0800289void TimeStats::recordRenderEngineDuration(nsecs_t startTime, nsecs_t endTime) {
290 if (!mEnabled.load()) return;
291
292 std::lock_guard<std::mutex> lock(mMutex);
293 if (mGlobalRecord.renderEngineDurations.size() == MAX_NUM_TIME_RECORDS) {
294 ALOGE("RenderEngineTimes are already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
295 mGlobalRecord.renderEngineDurations.pop_front();
296 }
297 mGlobalRecord.renderEngineDurations.push_back({startTime, endTime});
298}
299
300void TimeStats::recordRenderEngineDuration(nsecs_t startTime,
301 const std::shared_ptr<FenceTime>& endTime) {
302 if (!mEnabled.load()) return;
303
304 std::lock_guard<std::mutex> lock(mMutex);
305 if (mGlobalRecord.renderEngineDurations.size() == MAX_NUM_TIME_RECORDS) {
306 ALOGE("RenderEngineTimes are already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
307 mGlobalRecord.renderEngineDurations.pop_front();
308 }
309 mGlobalRecord.renderEngineDurations.push_back({startTime, endTime});
310}
311
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800312bool TimeStats::recordReadyLocked(int32_t layerId, TimeRecord* timeRecord) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700313 if (!timeRecord->ready) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800314 ALOGV("[%d]-[%" PRIu64 "]-presentFence is still not received", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700315 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700316 return false;
317 }
318
319 if (timeRecord->acquireFence != nullptr) {
320 if (timeRecord->acquireFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
321 return false;
322 }
323 if (timeRecord->acquireFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700324 timeRecord->frameTime.acquireTime = timeRecord->acquireFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700325 timeRecord->acquireFence = nullptr;
326 } else {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800327 ALOGV("[%d]-[%" PRIu64 "]-acquireFence signal time is invalid", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700328 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700329 }
330 }
331
332 if (timeRecord->presentFence != nullptr) {
333 if (timeRecord->presentFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
334 return false;
335 }
336 if (timeRecord->presentFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700337 timeRecord->frameTime.presentTime = timeRecord->presentFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700338 timeRecord->presentFence = nullptr;
339 } else {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800340 ALOGV("[%d]-[%" PRIu64 "]-presentFence signal time invalid", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700341 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700342 }
343 }
344
345 return true;
346}
347
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800348void TimeStats::flushAvailableRecordsToStatsLocked(int32_t layerId) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700349 ATRACE_CALL();
350
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800351 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700352 TimeRecord& prevTimeRecord = layerRecord.prevTimeRecord;
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700353 std::deque<TimeRecord>& timeRecords = layerRecord.timeRecords;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700354 while (!timeRecords.empty()) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800355 if (!recordReadyLocked(layerId, &timeRecords[0])) break;
356 ALOGV("[%d]-[%" PRIu64 "]-presentFenceTime[%" PRId64 "]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700357 timeRecords[0].frameTime.frameNumber, timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700358
359 if (prevTimeRecord.ready) {
Yiwei Zhangeafa5cc2019-07-26 15:06:25 -0700360 const std::string& layerName = layerRecord.layerName;
Yiwei Zhangdc224042018-10-18 15:34:00 -0700361 if (!mTimeStats.stats.count(layerName)) {
362 mTimeStats.stats[layerName].layerName = layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700363 }
Yiwei Zhangdc224042018-10-18 15:34:00 -0700364 TimeStatsHelper::TimeStatsLayer& timeStatsLayer = mTimeStats.stats[layerName];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700365 timeStatsLayer.totalFrames++;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700366 timeStatsLayer.droppedFrames += layerRecord.droppedFrames;
Alec Mouri91f6df32020-01-30 08:48:58 -0800367 timeStatsLayer.lateAcquireFrames += layerRecord.lateAcquireFrames;
368 timeStatsLayer.badDesiredPresentFrames += layerRecord.badDesiredPresentFrames;
369
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700370 layerRecord.droppedFrames = 0;
Alec Mouri91f6df32020-01-30 08:48:58 -0800371 layerRecord.lateAcquireFrames = 0;
372 layerRecord.badDesiredPresentFrames = 0;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700373
374 const int32_t postToAcquireMs = msBetween(timeRecords[0].frameTime.postTime,
375 timeRecords[0].frameTime.acquireTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800376 ALOGV("[%d]-[%" PRIu64 "]-post2acquire[%d]", layerId,
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700377 timeRecords[0].frameTime.frameNumber, postToAcquireMs);
378 timeStatsLayer.deltas["post2acquire"].insert(postToAcquireMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700379
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700380 const int32_t postToPresentMs = msBetween(timeRecords[0].frameTime.postTime,
381 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800382 ALOGV("[%d]-[%" PRIu64 "]-post2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700383 timeRecords[0].frameTime.frameNumber, postToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700384 timeStatsLayer.deltas["post2present"].insert(postToPresentMs);
385
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700386 const int32_t acquireToPresentMs = msBetween(timeRecords[0].frameTime.acquireTime,
387 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800388 ALOGV("[%d]-[%" PRIu64 "]-acquire2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700389 timeRecords[0].frameTime.frameNumber, acquireToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700390 timeStatsLayer.deltas["acquire2present"].insert(acquireToPresentMs);
391
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700392 const int32_t latchToPresentMs = msBetween(timeRecords[0].frameTime.latchTime,
393 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800394 ALOGV("[%d]-[%" PRIu64 "]-latch2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700395 timeRecords[0].frameTime.frameNumber, latchToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700396 timeStatsLayer.deltas["latch2present"].insert(latchToPresentMs);
397
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700398 const int32_t desiredToPresentMs = msBetween(timeRecords[0].frameTime.desiredTime,
399 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800400 ALOGV("[%d]-[%" PRIu64 "]-desired2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700401 timeRecords[0].frameTime.frameNumber, desiredToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700402 timeStatsLayer.deltas["desired2present"].insert(desiredToPresentMs);
403
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700404 const int32_t presentToPresentMs = msBetween(prevTimeRecord.frameTime.presentTime,
405 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800406 ALOGV("[%d]-[%" PRIu64 "]-present2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700407 timeRecords[0].frameTime.frameNumber, presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700408 timeStatsLayer.deltas["present2present"].insert(presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700409 }
410 prevTimeRecord = timeRecords[0];
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700411 timeRecords.pop_front();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700412 layerRecord.waitData--;
413 }
414}
415
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700416static constexpr const char* kPopupWindowPrefix = "PopupWindow";
417static const size_t kMinLenLayerName = std::strlen(kPopupWindowPrefix);
Yiwei Zhangbd408322018-10-15 18:31:53 -0700418
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700419// Avoid tracking the "PopupWindow:<random hash>#<number>" layers
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700420static bool layerNameIsValid(const std::string& layerName) {
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700421 return layerName.length() >= kMinLenLayerName &&
422 layerName.compare(0, kMinLenLayerName, kPopupWindowPrefix) != 0;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700423}
424
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800425void TimeStats::setPostTime(int32_t layerId, uint64_t frameNumber, const std::string& layerName,
Yiwei Zhang8e8fe522018-11-02 18:34:07 -0700426 nsecs_t postTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700427 if (!mEnabled.load()) return;
428
429 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800430 ALOGV("[%d]-[%" PRIu64 "]-[%s]-PostTime[%" PRId64 "]", layerId, frameNumber, layerName.c_str(),
Yiwei Zhang8e8fe522018-11-02 18:34:07 -0700431 postTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700432
433 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange926ab52019-08-14 15:16:00 -0700434 if (!mTimeStats.stats.count(layerName) && mTimeStats.stats.size() >= MAX_NUM_LAYER_STATS) {
435 return;
436 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800437 if (!mTimeStatsTracker.count(layerId) && mTimeStatsTracker.size() < MAX_NUM_LAYER_RECORDS &&
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700438 layerNameIsValid(layerName)) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800439 mTimeStatsTracker[layerId].layerName = layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700440 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800441 if (!mTimeStatsTracker.count(layerId)) return;
442 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700443 if (layerRecord.timeRecords.size() == MAX_NUM_TIME_RECORDS) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800444 ALOGE("[%d]-[%s]-timeRecords is at its maximum size[%zu]. Ignore this when unittesting.",
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800445 layerId, layerRecord.layerName.c_str(), MAX_NUM_TIME_RECORDS);
446 mTimeStatsTracker.erase(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700447 return;
448 }
449 // For most media content, the acquireFence is invalid because the buffer is
450 // ready at the queueBuffer stage. In this case, acquireTime should be given
451 // a default value as postTime.
452 TimeRecord timeRecord = {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700453 .frameTime =
454 {
455 .frameNumber = frameNumber,
456 .postTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800457 .latchTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700458 .acquireTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800459 .desiredTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700460 },
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700461 };
462 layerRecord.timeRecords.push_back(timeRecord);
463 if (layerRecord.waitData < 0 ||
464 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
465 layerRecord.waitData = layerRecord.timeRecords.size() - 1;
466}
467
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800468void TimeStats::setLatchTime(int32_t layerId, uint64_t frameNumber, nsecs_t latchTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700469 if (!mEnabled.load()) return;
470
471 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800472 ALOGV("[%d]-[%" PRIu64 "]-LatchTime[%" PRId64 "]", layerId, frameNumber, latchTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700473
474 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800475 if (!mTimeStatsTracker.count(layerId)) return;
476 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700477 if (layerRecord.waitData < 0 ||
478 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
479 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700480 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700481 if (timeRecord.frameTime.frameNumber == frameNumber) {
482 timeRecord.frameTime.latchTime = latchTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700483 }
484}
485
Alec Mouri91f6df32020-01-30 08:48:58 -0800486void TimeStats::incrementLatchSkipped(int32_t layerId, LatchSkipReason reason) {
487 if (!mEnabled.load()) return;
488
489 ATRACE_CALL();
490 ALOGV("[%d]-LatchSkipped-Reason[%d]", layerId,
491 static_cast<std::underlying_type<LatchSkipReason>::type>(reason));
492
493 std::lock_guard<std::mutex> lock(mMutex);
494 if (!mTimeStatsTracker.count(layerId)) return;
495 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
496
497 switch (reason) {
498 case LatchSkipReason::LateAcquire:
499 layerRecord.lateAcquireFrames++;
500 break;
501 }
502}
503
504void TimeStats::incrementBadDesiredPresent(int32_t layerId) {
505 if (!mEnabled.load()) return;
506
507 ATRACE_CALL();
508 ALOGV("[%d]-BadDesiredPresent", layerId);
509
510 std::lock_guard<std::mutex> lock(mMutex);
511 if (!mTimeStatsTracker.count(layerId)) return;
512 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
513 layerRecord.badDesiredPresentFrames++;
514}
515
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800516void TimeStats::setDesiredTime(int32_t layerId, uint64_t frameNumber, nsecs_t desiredTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700517 if (!mEnabled.load()) return;
518
519 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800520 ALOGV("[%d]-[%" PRIu64 "]-DesiredTime[%" PRId64 "]", layerId, frameNumber, desiredTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700521
522 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800523 if (!mTimeStatsTracker.count(layerId)) return;
524 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700525 if (layerRecord.waitData < 0 ||
526 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
527 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700528 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700529 if (timeRecord.frameTime.frameNumber == frameNumber) {
530 timeRecord.frameTime.desiredTime = desiredTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700531 }
532}
533
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800534void TimeStats::setAcquireTime(int32_t layerId, uint64_t frameNumber, nsecs_t acquireTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700535 if (!mEnabled.load()) return;
536
537 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800538 ALOGV("[%d]-[%" PRIu64 "]-AcquireTime[%" PRId64 "]", layerId, frameNumber, acquireTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700539
540 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800541 if (!mTimeStatsTracker.count(layerId)) return;
542 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700543 if (layerRecord.waitData < 0 ||
544 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
545 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700546 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700547 if (timeRecord.frameTime.frameNumber == frameNumber) {
548 timeRecord.frameTime.acquireTime = acquireTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700549 }
550}
551
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800552void TimeStats::setAcquireFence(int32_t layerId, uint64_t frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700553 const std::shared_ptr<FenceTime>& acquireFence) {
554 if (!mEnabled.load()) return;
555
556 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800557 ALOGV("[%d]-[%" PRIu64 "]-AcquireFenceTime[%" PRId64 "]", layerId, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700558 acquireFence->getSignalTime());
559
560 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800561 if (!mTimeStatsTracker.count(layerId)) return;
562 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700563 if (layerRecord.waitData < 0 ||
564 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
565 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700566 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700567 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700568 timeRecord.acquireFence = acquireFence;
569 }
570}
571
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800572void TimeStats::setPresentTime(int32_t layerId, uint64_t frameNumber, nsecs_t presentTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700573 if (!mEnabled.load()) return;
574
575 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800576 ALOGV("[%d]-[%" PRIu64 "]-PresentTime[%" PRId64 "]", layerId, frameNumber, presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700577
578 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800579 if (!mTimeStatsTracker.count(layerId)) return;
580 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700581 if (layerRecord.waitData < 0 ||
582 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
583 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700584 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700585 if (timeRecord.frameTime.frameNumber == frameNumber) {
586 timeRecord.frameTime.presentTime = presentTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700587 timeRecord.ready = true;
588 layerRecord.waitData++;
589 }
590
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800591 flushAvailableRecordsToStatsLocked(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700592}
593
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800594void TimeStats::setPresentFence(int32_t layerId, uint64_t frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700595 const std::shared_ptr<FenceTime>& presentFence) {
596 if (!mEnabled.load()) return;
597
598 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800599 ALOGV("[%d]-[%" PRIu64 "]-PresentFenceTime[%" PRId64 "]", layerId, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700600 presentFence->getSignalTime());
601
602 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800603 if (!mTimeStatsTracker.count(layerId)) return;
604 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700605 if (layerRecord.waitData < 0 ||
606 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
607 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700608 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700609 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700610 timeRecord.presentFence = presentFence;
611 timeRecord.ready = true;
612 layerRecord.waitData++;
613 }
614
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800615 flushAvailableRecordsToStatsLocked(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700616}
617
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800618void TimeStats::onDestroy(int32_t layerId) {
Yiwei Zhangdc224042018-10-18 15:34:00 -0700619 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800620 ALOGV("[%d]-onDestroy", layerId);
Mikael Pessa90092f42019-08-26 17:22:04 -0700621 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800622 mTimeStatsTracker.erase(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700623}
624
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800625void TimeStats::removeTimeRecord(int32_t layerId, uint64_t frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700626 if (!mEnabled.load()) return;
627
628 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800629 ALOGV("[%d]-[%" PRIu64 "]-removeTimeRecord", layerId, frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700630
631 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800632 if (!mTimeStatsTracker.count(layerId)) return;
633 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700634 size_t removeAt = 0;
635 for (const TimeRecord& record : layerRecord.timeRecords) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700636 if (record.frameTime.frameNumber == frameNumber) break;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700637 removeAt++;
638 }
639 if (removeAt == layerRecord.timeRecords.size()) return;
640 layerRecord.timeRecords.erase(layerRecord.timeRecords.begin() + removeAt);
641 if (layerRecord.waitData > static_cast<int32_t>(removeAt)) {
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700642 layerRecord.waitData--;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700643 }
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700644 layerRecord.droppedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700645}
646
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700647void TimeStats::flushPowerTimeLocked() {
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700648 if (!mEnabled.load()) return;
649
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700650 nsecs_t curTime = systemTime();
651 // elapsedTime is in milliseconds.
652 int64_t elapsedTime = (curTime - mPowerTime.prevTime) / 1000000;
653
654 switch (mPowerTime.powerMode) {
655 case HWC_POWER_MODE_NORMAL:
656 mTimeStats.displayOnTime += elapsedTime;
657 break;
658 case HWC_POWER_MODE_OFF:
659 case HWC_POWER_MODE_DOZE:
660 case HWC_POWER_MODE_DOZE_SUSPEND:
661 default:
662 break;
663 }
664
665 mPowerTime.prevTime = curTime;
666}
667
668void TimeStats::setPowerMode(int32_t powerMode) {
669 if (!mEnabled.load()) {
670 std::lock_guard<std::mutex> lock(mMutex);
671 mPowerTime.powerMode = powerMode;
672 return;
673 }
674
675 std::lock_guard<std::mutex> lock(mMutex);
676 if (powerMode == mPowerTime.powerMode) return;
677
678 flushPowerTimeLocked();
679 mPowerTime.powerMode = powerMode;
680}
681
Alec Mourifb571ea2019-01-24 18:42:10 -0800682void TimeStats::recordRefreshRate(uint32_t fps, nsecs_t duration) {
683 std::lock_guard<std::mutex> lock(mMutex);
684 if (mTimeStats.refreshRateStats.count(fps)) {
685 mTimeStats.refreshRateStats[fps] += duration;
686 } else {
687 mTimeStats.refreshRateStats.insert({fps, duration});
688 }
689}
690
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700691void TimeStats::flushAvailableGlobalRecordsToStatsLocked() {
692 ATRACE_CALL();
693
694 while (!mGlobalRecord.presentFences.empty()) {
695 const nsecs_t curPresentTime = mGlobalRecord.presentFences.front()->getSignalTime();
696 if (curPresentTime == Fence::SIGNAL_TIME_PENDING) break;
697
698 if (curPresentTime == Fence::SIGNAL_TIME_INVALID) {
699 ALOGE("GlobalPresentFence is invalid!");
700 mGlobalRecord.prevPresentTime = 0;
701 mGlobalRecord.presentFences.pop_front();
702 continue;
703 }
704
705 ALOGV("GlobalPresentFenceTime[%" PRId64 "]",
706 mGlobalRecord.presentFences.front()->getSignalTime());
707
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700708 if (mGlobalRecord.prevPresentTime != 0) {
709 const int32_t presentToPresentMs =
710 msBetween(mGlobalRecord.prevPresentTime, curPresentTime);
711 ALOGV("Global present2present[%d] prev[%" PRId64 "] curr[%" PRId64 "]",
712 presentToPresentMs, mGlobalRecord.prevPresentTime, curPresentTime);
713 mTimeStats.presentToPresent.insert(presentToPresentMs);
714 }
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700715
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700716 mGlobalRecord.prevPresentTime = curPresentTime;
717 mGlobalRecord.presentFences.pop_front();
718 }
Alec Mourie4034bb2019-11-19 12:45:54 -0800719 while (!mGlobalRecord.renderEngineDurations.empty()) {
720 const auto duration = mGlobalRecord.renderEngineDurations.front();
721 const auto& endTime = duration.endTime;
722
723 nsecs_t endNs = -1;
724
725 if (auto val = std::get_if<nsecs_t>(&endTime)) {
726 endNs = *val;
727 } else {
728 endNs = std::get<std::shared_ptr<FenceTime>>(endTime)->getSignalTime();
729 }
730
731 if (endNs == Fence::SIGNAL_TIME_PENDING) break;
732
733 if (endNs < 0) {
734 ALOGE("RenderEngineTiming is invalid!");
735 mGlobalRecord.renderEngineDurations.pop_front();
736 continue;
737 }
738
739 const int32_t renderEngineMs = msBetween(duration.startTime, endNs);
740 mTimeStats.renderEngineTiming.insert(renderEngineMs);
741
742 mGlobalRecord.renderEngineDurations.pop_front();
743 }
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700744}
745
746void TimeStats::setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) {
747 if (!mEnabled.load()) return;
748
749 ATRACE_CALL();
750 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700751 if (presentFence == nullptr || !presentFence->isValid()) {
752 mGlobalRecord.prevPresentTime = 0;
753 return;
754 }
755
756 if (mPowerTime.powerMode != HWC_POWER_MODE_NORMAL) {
757 // Try flushing the last present fence on HWC_POWER_MODE_NORMAL.
758 flushAvailableGlobalRecordsToStatsLocked();
759 mGlobalRecord.presentFences.clear();
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700760 mGlobalRecord.prevPresentTime = 0;
761 return;
762 }
763
764 if (mGlobalRecord.presentFences.size() == MAX_NUM_TIME_RECORDS) {
765 // The front presentFence must be trapped in pending status in this
766 // case. Try dequeuing the front one to recover.
767 ALOGE("GlobalPresentFences is already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
768 mGlobalRecord.prevPresentTime = 0;
769 mGlobalRecord.presentFences.pop_front();
770 }
771
772 mGlobalRecord.presentFences.emplace_back(presentFence);
773 flushAvailableGlobalRecordsToStatsLocked();
774}
775
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700776void TimeStats::enable() {
777 if (mEnabled.load()) return;
778
779 ATRACE_CALL();
780
781 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700782 mEnabled.store(true);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700783 mTimeStats.statsStart = static_cast<int64_t>(std::time(0));
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700784 mPowerTime.prevTime = systemTime();
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700785 ALOGD("Enabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700786}
787
788void TimeStats::disable() {
789 if (!mEnabled.load()) return;
790
791 ATRACE_CALL();
792
793 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700794 flushPowerTimeLocked();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700795 mEnabled.store(false);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700796 mTimeStats.statsEnd = static_cast<int64_t>(std::time(0));
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700797 ALOGD("Disabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700798}
799
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000800void TimeStats::clearAll() {
801 std::lock_guard<std::mutex> lock(mMutex);
802 clearGlobalLocked();
803 clearLayersLocked();
804}
805
806void TimeStats::clearGlobalLocked() {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700807 ATRACE_CALL();
808
Yiwei Zhangdc224042018-10-18 15:34:00 -0700809 mTimeStats.statsStart = (mEnabled.load() ? static_cast<int64_t>(std::time(0)) : 0);
810 mTimeStats.statsEnd = 0;
811 mTimeStats.totalFrames = 0;
812 mTimeStats.missedFrames = 0;
813 mTimeStats.clientCompositionFrames = 0;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800814 mTimeStats.clientCompositionReusedFrames = 0;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700815 mTimeStats.displayOnTime = 0;
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700816 mTimeStats.presentToPresent.hist.clear();
Alec Mouri31ac64a2020-01-09 09:26:22 -0800817 mTimeStats.frameDuration.hist.clear();
818 mTimeStats.renderEngineTiming.hist.clear();
Alec Mourifb571ea2019-01-24 18:42:10 -0800819 mTimeStats.refreshRateStats.clear();
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700820 mPowerTime.prevTime = systemTime();
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700821 mGlobalRecord.prevPresentTime = 0;
822 mGlobalRecord.presentFences.clear();
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000823 ALOGD("Cleared global stats");
824}
825
826void TimeStats::clearLayersLocked() {
827 ATRACE_CALL();
828
829 mTimeStatsTracker.clear();
830 mTimeStats.stats.clear();
831 ALOGD("Cleared layer stats");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700832}
833
834bool TimeStats::isEnabled() {
835 return mEnabled.load();
836}
837
Yiwei Zhang5434a782018-12-05 18:06:32 -0800838void TimeStats::dump(bool asProto, std::optional<uint32_t> maxLayers, std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700839 ATRACE_CALL();
840
841 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700842 if (mTimeStats.statsStart == 0) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700843 return;
844 }
845
Yiwei Zhangdc224042018-10-18 15:34:00 -0700846 mTimeStats.statsEnd = static_cast<int64_t>(std::time(0));
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700847
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700848 flushPowerTimeLocked();
849
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700850 if (asProto) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700851 ALOGD("Dumping TimeStats as proto");
Yiwei Zhangdc224042018-10-18 15:34:00 -0700852 SFTimeStatsGlobalProto timeStatsProto = mTimeStats.toProto(maxLayers);
Dominik Laskowski46470112019-08-02 13:13:11 -0700853 result.append(timeStatsProto.SerializeAsString());
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700854 } else {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700855 ALOGD("Dumping TimeStats as text");
Yiwei Zhang5434a782018-12-05 18:06:32 -0800856 result.append(mTimeStats.toString(maxLayers));
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700857 result.append("\n");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700858 }
859}
860
Alec Mourifb571ea2019-01-24 18:42:10 -0800861} // namespace impl
862
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700863} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800864
865// TODO(b/129481165): remove the #pragma below and fix conversion issues
866#pragma clang diagnostic pop // ignored "-Wconversion"