blob: 7c824eca00d1a9348b6e5ae6d1c058d5a0825b07 [file] [log] [blame]
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001/*
2 * Copyright 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080016
17// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
Yiwei Zhang0102ad22018-05-02 17:37:17 -070020#undef LOG_TAG
21#define LOG_TAG "TimeStats"
22#define ATRACE_TAG ATRACE_TAG_GRAPHICS
23
24#include "TimeStats.h"
25
26#include <android-base/stringprintf.h>
Alec Mouri37384342020-01-02 17:23:37 -080027#include <android/util/ProtoOutputStream.h>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070028#include <log/log.h>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070029#include <utils/String8.h>
Yiwei Zhang3a226d22018-10-16 09:23:03 -070030#include <utils/Timers.h>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070031#include <utils/Trace.h>
32
33#include <algorithm>
Alec Mouri9519bf12019-11-15 16:54:44 -080034#include <chrono>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070035
36namespace android {
37
Alec Mourifb571ea2019-01-24 18:42:10 -080038namespace impl {
39
Alec Mouri37384342020-01-02 17:23:37 -080040status_pull_atom_return_t TimeStats::pullAtomCallback(int32_t atom_tag,
41 pulled_stats_event_list* data, void* cookie) {
Alec Mouri8e2f31b2020-01-16 22:04:35 +000042 impl::TimeStats* timeStats = reinterpret_cast<impl::TimeStats*>(cookie);
Alec Mouri37384342020-01-02 17:23:37 -080043 if (atom_tag == android::util::SURFACEFLINGER_STATS_GLOBAL_INFO) {
44 return timeStats->populateGlobalAtom(data);
45 } else if (atom_tag == android::util::SURFACEFLINGER_STATS_LAYER_INFO) {
46 return timeStats->populateLayerAtom(data);
Alec Mouri8e2f31b2020-01-16 22:04:35 +000047 }
48
Alec Mouri37384342020-01-02 17:23:37 -080049 return STATS_PULL_SKIP;
50}
Alec Mouri8e2f31b2020-01-16 22:04:35 +000051
Alec Mouri37384342020-01-02 17:23:37 -080052status_pull_atom_return_t TimeStats::populateGlobalAtom(pulled_stats_event_list* data) {
53 std::lock_guard<std::mutex> lock(mMutex);
54
55 if (mTimeStats.statsStart == 0) {
Alec Mouri8e2f31b2020-01-16 22:04:35 +000056 return STATS_PULL_SKIP;
57 }
Alec Mouri37384342020-01-02 17:23:37 -080058 flushPowerTimeLocked();
Alec Mouri8e2f31b2020-01-16 22:04:35 +000059
Alec Mouri37384342020-01-02 17:23:37 -080060 struct stats_event* event = mStatsDelegate->addStatsEventToPullData(data);
61 mStatsDelegate->statsEventSetAtomId(event, android::util::SURFACEFLINGER_STATS_GLOBAL_INFO);
62 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.totalFrames);
63 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.missedFrames);
64 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.clientCompositionFrames);
65 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.displayOnTime);
66 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.presentToPresent.totalTime());
67 mStatsDelegate->statsEventBuild(event);
68 clearGlobalLocked();
Alec Mouri8e2f31b2020-01-16 22:04:35 +000069
70 return STATS_PULL_SUCCESS;
71}
72
Alec Mouri37384342020-01-02 17:23:37 -080073namespace {
74// Histograms align with the order of fields in SurfaceflingerStatsLayerInfo.
75const std::array<std::string, 6> kHistogramNames = {
76 "present2present", "post2present", "acquire2present",
77 "latch2present", "desired2present", "post2acquire",
78};
Alec Mouri8e2f31b2020-01-16 22:04:35 +000079
Alec Mouri37384342020-01-02 17:23:37 -080080std::string histogramToProtoByteString(const std::unordered_map<int32_t, int32_t>& histogram,
81 size_t maxPulledHistogramBuckets) {
82 auto buckets = std::vector<std::pair<int32_t, int32_t>>(histogram.begin(), histogram.end());
83 std::sort(buckets.begin(), buckets.end(),
84 [](std::pair<int32_t, int32_t>& left, std::pair<int32_t, int32_t>& right) {
85 return left.second > right.second;
86 });
87
88 util::ProtoOutputStream proto;
89 int histogramSize = 0;
90 for (const auto& bucket : buckets) {
91 if (++histogramSize > maxPulledHistogramBuckets) {
92 break;
93 }
94 proto.write(android::util::FIELD_TYPE_INT32 | android::util::FIELD_COUNT_REPEATED |
95 1 /* field id */,
96 (int32_t)bucket.first);
97 proto.write(android::util::FIELD_TYPE_INT64 | android::util::FIELD_COUNT_REPEATED |
98 2 /* field id */,
99 (int64_t)bucket.second);
100 }
101
102 std::string byteString;
103 proto.serializeToString(&byteString);
104 return byteString;
105}
106} // namespace
107
108status_pull_atom_return_t TimeStats::populateLayerAtom(pulled_stats_event_list* data) {
109 std::lock_guard<std::mutex> lock(mMutex);
110
111 std::vector<TimeStatsHelper::TimeStatsLayer const*> dumpStats;
112 for (const auto& ele : mTimeStats.stats) {
113 dumpStats.push_back(&ele.second);
114 }
115
116 std::sort(dumpStats.begin(), dumpStats.end(),
117 [](TimeStatsHelper::TimeStatsLayer const* l,
118 TimeStatsHelper::TimeStatsLayer const* r) {
119 return l->totalFrames > r->totalFrames;
120 });
121
122 if (mMaxPulledLayers < dumpStats.size()) {
123 dumpStats.resize(mMaxPulledLayers);
124 }
125
126 for (const auto& layer : dumpStats) {
127 struct stats_event* event = mStatsDelegate->addStatsEventToPullData(data);
128 mStatsDelegate->statsEventSetAtomId(event, android::util::SURFACEFLINGER_STATS_LAYER_INFO);
129 mStatsDelegate->statsEventWriteString8(event, layer->layerName.c_str());
130 mStatsDelegate->statsEventWriteInt64(event, layer->totalFrames);
131 mStatsDelegate->statsEventWriteInt64(event, layer->droppedFrames);
132
133 for (const auto& name : kHistogramNames) {
134 const auto& histogram = layer->deltas.find(name);
135 if (histogram == layer->deltas.cend()) {
136 mStatsDelegate->statsEventWriteByteArray(event, nullptr, 0);
137 } else {
138 std::string bytes = histogramToProtoByteString(histogram->second.hist,
139 mMaxPulledHistogramBuckets);
140 mStatsDelegate->statsEventWriteByteArray(event, (const uint8_t*)bytes.c_str(),
141 bytes.size());
142 }
143 }
144
145 mStatsDelegate->statsEventBuild(event);
146 }
147 clearLayersLocked();
148
149 return STATS_PULL_SUCCESS;
150}
151
152TimeStats::TimeStats() : TimeStats(nullptr, std::nullopt, std::nullopt) {}
153
154TimeStats::TimeStats(std::unique_ptr<StatsEventDelegate> statsDelegate,
155 std::optional<size_t> maxPulledLayers,
156 std::optional<size_t> maxPulledHistogramBuckets) {
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000157 if (statsDelegate != nullptr) {
158 mStatsDelegate = std::move(statsDelegate);
159 }
Alec Mouri37384342020-01-02 17:23:37 -0800160
161 if (maxPulledLayers) {
162 mMaxPulledLayers = *maxPulledLayers;
163 }
164
165 if (maxPulledHistogramBuckets) {
166 mMaxPulledHistogramBuckets = *maxPulledHistogramBuckets;
167 }
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000168}
169
170void TimeStats::onBootFinished() {
Alec Mourib3885ad2019-09-06 17:08:55 -0700171 // Temporarily enable TimeStats by default. Telemetry is disabled while
172 // we move onto statsd, so TimeStats is currently not exercised at all
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000173 // during testing without enabling by default.
174 // TODO: remove this, as we should only be paying this overhead on devices
175 // where statsd exists.
Alec Mourib3885ad2019-09-06 17:08:55 -0700176 enable();
177}
178
Dominik Laskowskic2867142019-01-21 11:33:38 -0800179void TimeStats::parseArgs(bool asProto, const Vector<String16>& args, std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700180 ATRACE_CALL();
181
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700182 std::unordered_map<std::string, int32_t> argsMap;
Dominik Laskowskic2867142019-01-21 11:33:38 -0800183 for (size_t index = 0; index < args.size(); ++index) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700184 argsMap[std::string(String8(args[index]).c_str())] = index;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700185 }
186
187 if (argsMap.count("-disable")) {
188 disable();
189 }
190
191 if (argsMap.count("-dump")) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700192 std::optional<uint32_t> maxLayers = std::nullopt;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700193 auto iter = argsMap.find("-maxlayers");
194 if (iter != argsMap.end() && iter->second + 1 < static_cast<int32_t>(args.size())) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700195 int64_t value = strtol(String8(args[iter->second + 1]).c_str(), nullptr, 10);
196 value = std::clamp(value, int64_t(0), int64_t(UINT32_MAX));
197 maxLayers = static_cast<uint32_t>(value);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700198 }
199
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700200 dump(asProto, maxLayers, result);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700201 }
202
203 if (argsMap.count("-clear")) {
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000204 clearAll();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700205 }
206
207 if (argsMap.count("-enable")) {
208 enable();
209 }
210}
211
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700212std::string TimeStats::miniDump() {
213 ATRACE_CALL();
214
215 std::string result = "TimeStats miniDump:\n";
216 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange926ab52019-08-14 15:16:00 -0700217 android::base::StringAppendF(&result, "Number of layers currently being tracked is %zu\n",
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700218 mTimeStatsTracker.size());
Yiwei Zhange926ab52019-08-14 15:16:00 -0700219 android::base::StringAppendF(&result, "Number of layers in the stats pool is %zu\n",
220 mTimeStats.stats.size());
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700221 return result;
222}
223
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700224void TimeStats::incrementTotalFrames() {
225 if (!mEnabled.load()) return;
226
227 ATRACE_CALL();
228
229 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700230 mTimeStats.totalFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700231}
232
Yiwei Zhang621f9d42018-05-07 10:40:55 -0700233void TimeStats::incrementMissedFrames() {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700234 if (!mEnabled.load()) return;
235
236 ATRACE_CALL();
237
238 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700239 mTimeStats.missedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700240}
241
242void TimeStats::incrementClientCompositionFrames() {
243 if (!mEnabled.load()) return;
244
245 ATRACE_CALL();
246
247 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700248 mTimeStats.clientCompositionFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700249}
250
Alec Mouri9519bf12019-11-15 16:54:44 -0800251static int32_t msBetween(nsecs_t start, nsecs_t end) {
252 int64_t delta = std::chrono::duration_cast<std::chrono::milliseconds>(
253 std::chrono::nanoseconds(end - start))
254 .count();
255 delta = std::clamp(delta, int64_t(INT32_MIN), int64_t(INT32_MAX));
256 return static_cast<int32_t>(delta);
257}
258
259void TimeStats::recordFrameDuration(nsecs_t startTime, nsecs_t endTime) {
260 if (!mEnabled.load()) return;
261
262 std::lock_guard<std::mutex> lock(mMutex);
263 if (mPowerTime.powerMode == HWC_POWER_MODE_NORMAL) {
264 mTimeStats.frameDuration.insert(msBetween(startTime, endTime));
265 }
266}
267
Alec Mourie4034bb2019-11-19 12:45:54 -0800268void TimeStats::recordRenderEngineDuration(nsecs_t startTime, nsecs_t endTime) {
269 if (!mEnabled.load()) return;
270
271 std::lock_guard<std::mutex> lock(mMutex);
272 if (mGlobalRecord.renderEngineDurations.size() == MAX_NUM_TIME_RECORDS) {
273 ALOGE("RenderEngineTimes are already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
274 mGlobalRecord.renderEngineDurations.pop_front();
275 }
276 mGlobalRecord.renderEngineDurations.push_back({startTime, endTime});
277}
278
279void TimeStats::recordRenderEngineDuration(nsecs_t startTime,
280 const std::shared_ptr<FenceTime>& endTime) {
281 if (!mEnabled.load()) return;
282
283 std::lock_guard<std::mutex> lock(mMutex);
284 if (mGlobalRecord.renderEngineDurations.size() == MAX_NUM_TIME_RECORDS) {
285 ALOGE("RenderEngineTimes are already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
286 mGlobalRecord.renderEngineDurations.pop_front();
287 }
288 mGlobalRecord.renderEngineDurations.push_back({startTime, endTime});
289}
290
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800291bool TimeStats::recordReadyLocked(int32_t layerId, TimeRecord* timeRecord) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700292 if (!timeRecord->ready) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800293 ALOGV("[%d]-[%" PRIu64 "]-presentFence is still not received", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700294 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700295 return false;
296 }
297
298 if (timeRecord->acquireFence != nullptr) {
299 if (timeRecord->acquireFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
300 return false;
301 }
302 if (timeRecord->acquireFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700303 timeRecord->frameTime.acquireTime = timeRecord->acquireFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700304 timeRecord->acquireFence = nullptr;
305 } else {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800306 ALOGV("[%d]-[%" PRIu64 "]-acquireFence signal time is invalid", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700307 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700308 }
309 }
310
311 if (timeRecord->presentFence != nullptr) {
312 if (timeRecord->presentFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
313 return false;
314 }
315 if (timeRecord->presentFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700316 timeRecord->frameTime.presentTime = timeRecord->presentFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700317 timeRecord->presentFence = nullptr;
318 } else {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800319 ALOGV("[%d]-[%" PRIu64 "]-presentFence signal time invalid", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700320 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700321 }
322 }
323
324 return true;
325}
326
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800327void TimeStats::flushAvailableRecordsToStatsLocked(int32_t layerId) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700328 ATRACE_CALL();
329
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800330 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700331 TimeRecord& prevTimeRecord = layerRecord.prevTimeRecord;
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700332 std::deque<TimeRecord>& timeRecords = layerRecord.timeRecords;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700333 while (!timeRecords.empty()) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800334 if (!recordReadyLocked(layerId, &timeRecords[0])) break;
335 ALOGV("[%d]-[%" PRIu64 "]-presentFenceTime[%" PRId64 "]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700336 timeRecords[0].frameTime.frameNumber, timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700337
338 if (prevTimeRecord.ready) {
Yiwei Zhangeafa5cc2019-07-26 15:06:25 -0700339 const std::string& layerName = layerRecord.layerName;
Yiwei Zhangdc224042018-10-18 15:34:00 -0700340 if (!mTimeStats.stats.count(layerName)) {
341 mTimeStats.stats[layerName].layerName = layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700342 }
Yiwei Zhangdc224042018-10-18 15:34:00 -0700343 TimeStatsHelper::TimeStatsLayer& timeStatsLayer = mTimeStats.stats[layerName];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700344 timeStatsLayer.totalFrames++;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700345 timeStatsLayer.droppedFrames += layerRecord.droppedFrames;
346 layerRecord.droppedFrames = 0;
347
348 const int32_t postToAcquireMs = msBetween(timeRecords[0].frameTime.postTime,
349 timeRecords[0].frameTime.acquireTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800350 ALOGV("[%d]-[%" PRIu64 "]-post2acquire[%d]", layerId,
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700351 timeRecords[0].frameTime.frameNumber, postToAcquireMs);
352 timeStatsLayer.deltas["post2acquire"].insert(postToAcquireMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700353
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700354 const int32_t postToPresentMs = msBetween(timeRecords[0].frameTime.postTime,
355 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800356 ALOGV("[%d]-[%" PRIu64 "]-post2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700357 timeRecords[0].frameTime.frameNumber, postToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700358 timeStatsLayer.deltas["post2present"].insert(postToPresentMs);
359
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700360 const int32_t acquireToPresentMs = msBetween(timeRecords[0].frameTime.acquireTime,
361 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800362 ALOGV("[%d]-[%" PRIu64 "]-acquire2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700363 timeRecords[0].frameTime.frameNumber, acquireToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700364 timeStatsLayer.deltas["acquire2present"].insert(acquireToPresentMs);
365
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700366 const int32_t latchToPresentMs = msBetween(timeRecords[0].frameTime.latchTime,
367 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800368 ALOGV("[%d]-[%" PRIu64 "]-latch2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700369 timeRecords[0].frameTime.frameNumber, latchToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700370 timeStatsLayer.deltas["latch2present"].insert(latchToPresentMs);
371
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700372 const int32_t desiredToPresentMs = msBetween(timeRecords[0].frameTime.desiredTime,
373 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800374 ALOGV("[%d]-[%" PRIu64 "]-desired2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700375 timeRecords[0].frameTime.frameNumber, desiredToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700376 timeStatsLayer.deltas["desired2present"].insert(desiredToPresentMs);
377
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700378 const int32_t presentToPresentMs = msBetween(prevTimeRecord.frameTime.presentTime,
379 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800380 ALOGV("[%d]-[%" PRIu64 "]-present2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700381 timeRecords[0].frameTime.frameNumber, presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700382 timeStatsLayer.deltas["present2present"].insert(presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700383 }
384 prevTimeRecord = timeRecords[0];
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700385 timeRecords.pop_front();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700386 layerRecord.waitData--;
387 }
388}
389
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700390static constexpr const char* kPopupWindowPrefix = "PopupWindow";
391static const size_t kMinLenLayerName = std::strlen(kPopupWindowPrefix);
Yiwei Zhangbd408322018-10-15 18:31:53 -0700392
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700393// Avoid tracking the "PopupWindow:<random hash>#<number>" layers
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700394static bool layerNameIsValid(const std::string& layerName) {
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700395 return layerName.length() >= kMinLenLayerName &&
396 layerName.compare(0, kMinLenLayerName, kPopupWindowPrefix) != 0;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700397}
398
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800399void TimeStats::setPostTime(int32_t layerId, uint64_t frameNumber, const std::string& layerName,
Yiwei Zhang8e8fe522018-11-02 18:34:07 -0700400 nsecs_t postTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700401 if (!mEnabled.load()) return;
402
403 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800404 ALOGV("[%d]-[%" PRIu64 "]-[%s]-PostTime[%" PRId64 "]", layerId, frameNumber, layerName.c_str(),
Yiwei Zhang8e8fe522018-11-02 18:34:07 -0700405 postTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700406
407 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange926ab52019-08-14 15:16:00 -0700408 if (!mTimeStats.stats.count(layerName) && mTimeStats.stats.size() >= MAX_NUM_LAYER_STATS) {
409 return;
410 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800411 if (!mTimeStatsTracker.count(layerId) && mTimeStatsTracker.size() < MAX_NUM_LAYER_RECORDS &&
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700412 layerNameIsValid(layerName)) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800413 mTimeStatsTracker[layerId].layerName = layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700414 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800415 if (!mTimeStatsTracker.count(layerId)) return;
416 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700417 if (layerRecord.timeRecords.size() == MAX_NUM_TIME_RECORDS) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800418 ALOGE("[%d]-[%s]-timeRecords is at its maximum size[%zu]. Ignore this when unittesting.",
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800419 layerId, layerRecord.layerName.c_str(), MAX_NUM_TIME_RECORDS);
420 mTimeStatsTracker.erase(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700421 return;
422 }
423 // For most media content, the acquireFence is invalid because the buffer is
424 // ready at the queueBuffer stage. In this case, acquireTime should be given
425 // a default value as postTime.
426 TimeRecord timeRecord = {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700427 .frameTime =
428 {
429 .frameNumber = frameNumber,
430 .postTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800431 .latchTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700432 .acquireTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800433 .desiredTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700434 },
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700435 };
436 layerRecord.timeRecords.push_back(timeRecord);
437 if (layerRecord.waitData < 0 ||
438 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
439 layerRecord.waitData = layerRecord.timeRecords.size() - 1;
440}
441
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800442void TimeStats::setLatchTime(int32_t layerId, uint64_t frameNumber, nsecs_t latchTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700443 if (!mEnabled.load()) return;
444
445 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800446 ALOGV("[%d]-[%" PRIu64 "]-LatchTime[%" PRId64 "]", layerId, frameNumber, latchTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700447
448 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800449 if (!mTimeStatsTracker.count(layerId)) return;
450 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700451 if (layerRecord.waitData < 0 ||
452 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
453 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700454 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700455 if (timeRecord.frameTime.frameNumber == frameNumber) {
456 timeRecord.frameTime.latchTime = latchTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700457 }
458}
459
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800460void TimeStats::setDesiredTime(int32_t layerId, uint64_t frameNumber, nsecs_t desiredTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700461 if (!mEnabled.load()) return;
462
463 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800464 ALOGV("[%d]-[%" PRIu64 "]-DesiredTime[%" PRId64 "]", layerId, frameNumber, desiredTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700465
466 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800467 if (!mTimeStatsTracker.count(layerId)) return;
468 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700469 if (layerRecord.waitData < 0 ||
470 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
471 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700472 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700473 if (timeRecord.frameTime.frameNumber == frameNumber) {
474 timeRecord.frameTime.desiredTime = desiredTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700475 }
476}
477
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800478void TimeStats::setAcquireTime(int32_t layerId, uint64_t frameNumber, nsecs_t acquireTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700479 if (!mEnabled.load()) return;
480
481 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800482 ALOGV("[%d]-[%" PRIu64 "]-AcquireTime[%" PRId64 "]", layerId, frameNumber, acquireTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700483
484 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800485 if (!mTimeStatsTracker.count(layerId)) return;
486 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700487 if (layerRecord.waitData < 0 ||
488 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
489 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700490 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700491 if (timeRecord.frameTime.frameNumber == frameNumber) {
492 timeRecord.frameTime.acquireTime = acquireTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700493 }
494}
495
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800496void TimeStats::setAcquireFence(int32_t layerId, uint64_t frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700497 const std::shared_ptr<FenceTime>& acquireFence) {
498 if (!mEnabled.load()) return;
499
500 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800501 ALOGV("[%d]-[%" PRIu64 "]-AcquireFenceTime[%" PRId64 "]", layerId, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700502 acquireFence->getSignalTime());
503
504 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800505 if (!mTimeStatsTracker.count(layerId)) return;
506 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700507 if (layerRecord.waitData < 0 ||
508 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
509 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700510 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700511 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700512 timeRecord.acquireFence = acquireFence;
513 }
514}
515
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800516void TimeStats::setPresentTime(int32_t layerId, uint64_t frameNumber, nsecs_t presentTime) {
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 "]-PresentTime[%" PRId64 "]", layerId, frameNumber, presentTime);
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.presentTime = presentTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700531 timeRecord.ready = true;
532 layerRecord.waitData++;
533 }
534
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800535 flushAvailableRecordsToStatsLocked(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700536}
537
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800538void TimeStats::setPresentFence(int32_t layerId, uint64_t frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700539 const std::shared_ptr<FenceTime>& presentFence) {
540 if (!mEnabled.load()) return;
541
542 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800543 ALOGV("[%d]-[%" PRIu64 "]-PresentFenceTime[%" PRId64 "]", layerId, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700544 presentFence->getSignalTime());
545
546 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800547 if (!mTimeStatsTracker.count(layerId)) return;
548 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700549 if (layerRecord.waitData < 0 ||
550 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
551 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700552 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700553 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700554 timeRecord.presentFence = presentFence;
555 timeRecord.ready = true;
556 layerRecord.waitData++;
557 }
558
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800559 flushAvailableRecordsToStatsLocked(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700560}
561
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800562void TimeStats::onDestroy(int32_t layerId) {
Yiwei Zhangdc224042018-10-18 15:34:00 -0700563 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800564 ALOGV("[%d]-onDestroy", layerId);
Mikael Pessa90092f42019-08-26 17:22:04 -0700565 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800566 mTimeStatsTracker.erase(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700567}
568
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800569void TimeStats::removeTimeRecord(int32_t layerId, uint64_t frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700570 if (!mEnabled.load()) return;
571
572 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800573 ALOGV("[%d]-[%" PRIu64 "]-removeTimeRecord", layerId, frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700574
575 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800576 if (!mTimeStatsTracker.count(layerId)) return;
577 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700578 size_t removeAt = 0;
579 for (const TimeRecord& record : layerRecord.timeRecords) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700580 if (record.frameTime.frameNumber == frameNumber) break;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700581 removeAt++;
582 }
583 if (removeAt == layerRecord.timeRecords.size()) return;
584 layerRecord.timeRecords.erase(layerRecord.timeRecords.begin() + removeAt);
585 if (layerRecord.waitData > static_cast<int32_t>(removeAt)) {
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700586 layerRecord.waitData--;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700587 }
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700588 layerRecord.droppedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700589}
590
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700591void TimeStats::flushPowerTimeLocked() {
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700592 if (!mEnabled.load()) return;
593
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700594 nsecs_t curTime = systemTime();
595 // elapsedTime is in milliseconds.
596 int64_t elapsedTime = (curTime - mPowerTime.prevTime) / 1000000;
597
598 switch (mPowerTime.powerMode) {
599 case HWC_POWER_MODE_NORMAL:
600 mTimeStats.displayOnTime += elapsedTime;
601 break;
602 case HWC_POWER_MODE_OFF:
603 case HWC_POWER_MODE_DOZE:
604 case HWC_POWER_MODE_DOZE_SUSPEND:
605 default:
606 break;
607 }
608
609 mPowerTime.prevTime = curTime;
610}
611
612void TimeStats::setPowerMode(int32_t powerMode) {
613 if (!mEnabled.load()) {
614 std::lock_guard<std::mutex> lock(mMutex);
615 mPowerTime.powerMode = powerMode;
616 return;
617 }
618
619 std::lock_guard<std::mutex> lock(mMutex);
620 if (powerMode == mPowerTime.powerMode) return;
621
622 flushPowerTimeLocked();
623 mPowerTime.powerMode = powerMode;
624}
625
Alec Mourifb571ea2019-01-24 18:42:10 -0800626void TimeStats::recordRefreshRate(uint32_t fps, nsecs_t duration) {
627 std::lock_guard<std::mutex> lock(mMutex);
628 if (mTimeStats.refreshRateStats.count(fps)) {
629 mTimeStats.refreshRateStats[fps] += duration;
630 } else {
631 mTimeStats.refreshRateStats.insert({fps, duration});
632 }
633}
634
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700635void TimeStats::flushAvailableGlobalRecordsToStatsLocked() {
636 ATRACE_CALL();
637
638 while (!mGlobalRecord.presentFences.empty()) {
639 const nsecs_t curPresentTime = mGlobalRecord.presentFences.front()->getSignalTime();
640 if (curPresentTime == Fence::SIGNAL_TIME_PENDING) break;
641
642 if (curPresentTime == Fence::SIGNAL_TIME_INVALID) {
643 ALOGE("GlobalPresentFence is invalid!");
644 mGlobalRecord.prevPresentTime = 0;
645 mGlobalRecord.presentFences.pop_front();
646 continue;
647 }
648
649 ALOGV("GlobalPresentFenceTime[%" PRId64 "]",
650 mGlobalRecord.presentFences.front()->getSignalTime());
651
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700652 if (mGlobalRecord.prevPresentTime != 0) {
653 const int32_t presentToPresentMs =
654 msBetween(mGlobalRecord.prevPresentTime, curPresentTime);
655 ALOGV("Global present2present[%d] prev[%" PRId64 "] curr[%" PRId64 "]",
656 presentToPresentMs, mGlobalRecord.prevPresentTime, curPresentTime);
657 mTimeStats.presentToPresent.insert(presentToPresentMs);
658 }
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700659
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700660 mGlobalRecord.prevPresentTime = curPresentTime;
661 mGlobalRecord.presentFences.pop_front();
662 }
Alec Mourie4034bb2019-11-19 12:45:54 -0800663 while (!mGlobalRecord.renderEngineDurations.empty()) {
664 const auto duration = mGlobalRecord.renderEngineDurations.front();
665 const auto& endTime = duration.endTime;
666
667 nsecs_t endNs = -1;
668
669 if (auto val = std::get_if<nsecs_t>(&endTime)) {
670 endNs = *val;
671 } else {
672 endNs = std::get<std::shared_ptr<FenceTime>>(endTime)->getSignalTime();
673 }
674
675 if (endNs == Fence::SIGNAL_TIME_PENDING) break;
676
677 if (endNs < 0) {
678 ALOGE("RenderEngineTiming is invalid!");
679 mGlobalRecord.renderEngineDurations.pop_front();
680 continue;
681 }
682
683 const int32_t renderEngineMs = msBetween(duration.startTime, endNs);
684 mTimeStats.renderEngineTiming.insert(renderEngineMs);
685
686 mGlobalRecord.renderEngineDurations.pop_front();
687 }
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700688}
689
690void TimeStats::setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) {
691 if (!mEnabled.load()) return;
692
693 ATRACE_CALL();
694 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700695 if (presentFence == nullptr || !presentFence->isValid()) {
696 mGlobalRecord.prevPresentTime = 0;
697 return;
698 }
699
700 if (mPowerTime.powerMode != HWC_POWER_MODE_NORMAL) {
701 // Try flushing the last present fence on HWC_POWER_MODE_NORMAL.
702 flushAvailableGlobalRecordsToStatsLocked();
703 mGlobalRecord.presentFences.clear();
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700704 mGlobalRecord.prevPresentTime = 0;
705 return;
706 }
707
708 if (mGlobalRecord.presentFences.size() == MAX_NUM_TIME_RECORDS) {
709 // The front presentFence must be trapped in pending status in this
710 // case. Try dequeuing the front one to recover.
711 ALOGE("GlobalPresentFences is already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
712 mGlobalRecord.prevPresentTime = 0;
713 mGlobalRecord.presentFences.pop_front();
714 }
715
716 mGlobalRecord.presentFences.emplace_back(presentFence);
717 flushAvailableGlobalRecordsToStatsLocked();
718}
719
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700720void TimeStats::enable() {
721 if (mEnabled.load()) return;
722
723 ATRACE_CALL();
724
725 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700726 mEnabled.store(true);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700727 mTimeStats.statsStart = static_cast<int64_t>(std::time(0));
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700728 mPowerTime.prevTime = systemTime();
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000729 mStatsDelegate->registerStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_GLOBAL_INFO,
Alec Mouri37384342020-01-02 17:23:37 -0800730 TimeStats::pullAtomCallback, nullptr, this);
731 mStatsDelegate->registerStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_LAYER_INFO,
732 TimeStats::pullAtomCallback, nullptr, this);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700733 ALOGD("Enabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700734}
735
736void TimeStats::disable() {
737 if (!mEnabled.load()) return;
738
739 ATRACE_CALL();
740
741 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700742 flushPowerTimeLocked();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700743 mEnabled.store(false);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700744 mTimeStats.statsEnd = static_cast<int64_t>(std::time(0));
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000745 mStatsDelegate->unregisterStatsPullAtomCallback(
746 android::util::SURFACEFLINGER_STATS_GLOBAL_INFO);
Alec Mouri37384342020-01-02 17:23:37 -0800747 mStatsDelegate->unregisterStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_LAYER_INFO);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700748 ALOGD("Disabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700749}
750
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000751void TimeStats::clearAll() {
752 std::lock_guard<std::mutex> lock(mMutex);
753 clearGlobalLocked();
754 clearLayersLocked();
755}
756
757void TimeStats::clearGlobalLocked() {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700758 ATRACE_CALL();
759
Yiwei Zhangdc224042018-10-18 15:34:00 -0700760 mTimeStats.statsStart = (mEnabled.load() ? static_cast<int64_t>(std::time(0)) : 0);
761 mTimeStats.statsEnd = 0;
762 mTimeStats.totalFrames = 0;
763 mTimeStats.missedFrames = 0;
764 mTimeStats.clientCompositionFrames = 0;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700765 mTimeStats.displayOnTime = 0;
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700766 mTimeStats.presentToPresent.hist.clear();
Alec Mouri31ac64a2020-01-09 09:26:22 -0800767 mTimeStats.frameDuration.hist.clear();
768 mTimeStats.renderEngineTiming.hist.clear();
Alec Mourifb571ea2019-01-24 18:42:10 -0800769 mTimeStats.refreshRateStats.clear();
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700770 mPowerTime.prevTime = systemTime();
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700771 mGlobalRecord.prevPresentTime = 0;
772 mGlobalRecord.presentFences.clear();
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000773 ALOGD("Cleared global stats");
774}
775
776void TimeStats::clearLayersLocked() {
777 ATRACE_CALL();
778
779 mTimeStatsTracker.clear();
780 mTimeStats.stats.clear();
781 ALOGD("Cleared layer stats");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700782}
783
784bool TimeStats::isEnabled() {
785 return mEnabled.load();
786}
787
Yiwei Zhang5434a782018-12-05 18:06:32 -0800788void TimeStats::dump(bool asProto, std::optional<uint32_t> maxLayers, std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700789 ATRACE_CALL();
790
791 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700792 if (mTimeStats.statsStart == 0) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700793 return;
794 }
795
Yiwei Zhangdc224042018-10-18 15:34:00 -0700796 mTimeStats.statsEnd = static_cast<int64_t>(std::time(0));
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700797
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700798 flushPowerTimeLocked();
799
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700800 if (asProto) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700801 ALOGD("Dumping TimeStats as proto");
Yiwei Zhangdc224042018-10-18 15:34:00 -0700802 SFTimeStatsGlobalProto timeStatsProto = mTimeStats.toProto(maxLayers);
Dominik Laskowski46470112019-08-02 13:13:11 -0700803 result.append(timeStatsProto.SerializeAsString());
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700804 } else {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700805 ALOGD("Dumping TimeStats as text");
Yiwei Zhang5434a782018-12-05 18:06:32 -0800806 result.append(mTimeStats.toString(maxLayers));
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700807 result.append("\n");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700808 }
809}
810
Alec Mourifb571ea2019-01-24 18:42:10 -0800811} // namespace impl
812
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700813} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800814
815// TODO(b/129481165): remove the #pragma below and fix conversion issues
816#pragma clang diagnostic pop // ignored "-Wconversion"