blob: 130e99a514ecfea80843e05f50b2915869504530 [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 Mouri3ecd5cd2020-01-29 12:53:07 -080043 status_pull_atom_return_t result = STATS_PULL_SKIP;
Alec Mouri37384342020-01-02 17:23:37 -080044 if (atom_tag == android::util::SURFACEFLINGER_STATS_GLOBAL_INFO) {
Alec Mouri3ecd5cd2020-01-29 12:53:07 -080045 result = timeStats->populateGlobalAtom(data);
Alec Mouri37384342020-01-02 17:23:37 -080046 } else if (atom_tag == android::util::SURFACEFLINGER_STATS_LAYER_INFO) {
Alec Mouri3ecd5cd2020-01-29 12:53:07 -080047 result = timeStats->populateLayerAtom(data);
Alec Mouri8e2f31b2020-01-16 22:04:35 +000048 }
49
Alec Mouri3ecd5cd2020-01-29 12:53:07 -080050 // Enable timestats now. The first full pull for a given build is expected to
51 // have empty or very little stats, as stats are first enabled after the
52 // first pull is completed for either the global or layer stats.
53 timeStats->enable();
54 return result;
Alec Mouri37384342020-01-02 17:23:37 -080055}
Alec Mouri8e2f31b2020-01-16 22:04:35 +000056
Alec Mouri37384342020-01-02 17:23:37 -080057status_pull_atom_return_t TimeStats::populateGlobalAtom(pulled_stats_event_list* data) {
58 std::lock_guard<std::mutex> lock(mMutex);
59
60 if (mTimeStats.statsStart == 0) {
Alec Mouri8e2f31b2020-01-16 22:04:35 +000061 return STATS_PULL_SKIP;
62 }
Alec Mouri37384342020-01-02 17:23:37 -080063 flushPowerTimeLocked();
Alec Mouri8e2f31b2020-01-16 22:04:35 +000064
Alec Mouri37384342020-01-02 17:23:37 -080065 struct stats_event* event = mStatsDelegate->addStatsEventToPullData(data);
66 mStatsDelegate->statsEventSetAtomId(event, android::util::SURFACEFLINGER_STATS_GLOBAL_INFO);
67 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.totalFrames);
68 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.missedFrames);
69 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.clientCompositionFrames);
70 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.displayOnTime);
71 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.presentToPresent.totalTime());
72 mStatsDelegate->statsEventBuild(event);
73 clearGlobalLocked();
Alec Mouri8e2f31b2020-01-16 22:04:35 +000074
75 return STATS_PULL_SUCCESS;
76}
77
Alec Mouri37384342020-01-02 17:23:37 -080078namespace {
79// Histograms align with the order of fields in SurfaceflingerStatsLayerInfo.
80const std::array<std::string, 6> kHistogramNames = {
81 "present2present", "post2present", "acquire2present",
82 "latch2present", "desired2present", "post2acquire",
83};
Alec Mouri8e2f31b2020-01-16 22:04:35 +000084
Alec Mouri37384342020-01-02 17:23:37 -080085std::string histogramToProtoByteString(const std::unordered_map<int32_t, int32_t>& histogram,
86 size_t maxPulledHistogramBuckets) {
87 auto buckets = std::vector<std::pair<int32_t, int32_t>>(histogram.begin(), histogram.end());
88 std::sort(buckets.begin(), buckets.end(),
89 [](std::pair<int32_t, int32_t>& left, std::pair<int32_t, int32_t>& right) {
90 return left.second > right.second;
91 });
92
93 util::ProtoOutputStream proto;
94 int histogramSize = 0;
95 for (const auto& bucket : buckets) {
96 if (++histogramSize > maxPulledHistogramBuckets) {
97 break;
98 }
99 proto.write(android::util::FIELD_TYPE_INT32 | android::util::FIELD_COUNT_REPEATED |
100 1 /* field id */,
101 (int32_t)bucket.first);
102 proto.write(android::util::FIELD_TYPE_INT64 | android::util::FIELD_COUNT_REPEATED |
103 2 /* field id */,
104 (int64_t)bucket.second);
105 }
106
107 std::string byteString;
108 proto.serializeToString(&byteString);
109 return byteString;
110}
111} // namespace
112
113status_pull_atom_return_t TimeStats::populateLayerAtom(pulled_stats_event_list* data) {
114 std::lock_guard<std::mutex> lock(mMutex);
115
116 std::vector<TimeStatsHelper::TimeStatsLayer const*> dumpStats;
117 for (const auto& ele : mTimeStats.stats) {
118 dumpStats.push_back(&ele.second);
119 }
120
121 std::sort(dumpStats.begin(), dumpStats.end(),
122 [](TimeStatsHelper::TimeStatsLayer const* l,
123 TimeStatsHelper::TimeStatsLayer const* r) {
124 return l->totalFrames > r->totalFrames;
125 });
126
127 if (mMaxPulledLayers < dumpStats.size()) {
128 dumpStats.resize(mMaxPulledLayers);
129 }
130
131 for (const auto& layer : dumpStats) {
132 struct stats_event* event = mStatsDelegate->addStatsEventToPullData(data);
133 mStatsDelegate->statsEventSetAtomId(event, android::util::SURFACEFLINGER_STATS_LAYER_INFO);
134 mStatsDelegate->statsEventWriteString8(event, layer->layerName.c_str());
135 mStatsDelegate->statsEventWriteInt64(event, layer->totalFrames);
136 mStatsDelegate->statsEventWriteInt64(event, layer->droppedFrames);
137
138 for (const auto& name : kHistogramNames) {
139 const auto& histogram = layer->deltas.find(name);
140 if (histogram == layer->deltas.cend()) {
141 mStatsDelegate->statsEventWriteByteArray(event, nullptr, 0);
142 } else {
143 std::string bytes = histogramToProtoByteString(histogram->second.hist,
144 mMaxPulledHistogramBuckets);
145 mStatsDelegate->statsEventWriteByteArray(event, (const uint8_t*)bytes.c_str(),
146 bytes.size());
147 }
148 }
149
150 mStatsDelegate->statsEventBuild(event);
151 }
152 clearLayersLocked();
153
154 return STATS_PULL_SUCCESS;
155}
156
157TimeStats::TimeStats() : TimeStats(nullptr, std::nullopt, std::nullopt) {}
158
159TimeStats::TimeStats(std::unique_ptr<StatsEventDelegate> statsDelegate,
160 std::optional<size_t> maxPulledLayers,
161 std::optional<size_t> maxPulledHistogramBuckets) {
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000162 if (statsDelegate != nullptr) {
163 mStatsDelegate = std::move(statsDelegate);
164 }
Alec Mouri37384342020-01-02 17:23:37 -0800165
166 if (maxPulledLayers) {
167 mMaxPulledLayers = *maxPulledLayers;
168 }
169
170 if (maxPulledHistogramBuckets) {
171 mMaxPulledHistogramBuckets = *maxPulledHistogramBuckets;
172 }
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000173}
174
Alec Mouri3ecd5cd2020-01-29 12:53:07 -0800175TimeStats::~TimeStats() {
176 std::lock_guard<std::mutex> lock(mMutex);
177 mStatsDelegate->unregisterStatsPullAtomCallback(
178 android::util::SURFACEFLINGER_STATS_GLOBAL_INFO);
179 mStatsDelegate->unregisterStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_LAYER_INFO);
180}
181
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000182void TimeStats::onBootFinished() {
Alec Mouri3ecd5cd2020-01-29 12:53:07 -0800183 std::lock_guard<std::mutex> lock(mMutex);
184 mStatsDelegate->registerStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_GLOBAL_INFO,
185 TimeStats::pullAtomCallback, nullptr, this);
186 mStatsDelegate->registerStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_LAYER_INFO,
187 TimeStats::pullAtomCallback, nullptr, this);
Alec Mourib3885ad2019-09-06 17:08:55 -0700188}
189
Dominik Laskowskic2867142019-01-21 11:33:38 -0800190void TimeStats::parseArgs(bool asProto, const Vector<String16>& args, std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700191 ATRACE_CALL();
192
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700193 std::unordered_map<std::string, int32_t> argsMap;
Dominik Laskowskic2867142019-01-21 11:33:38 -0800194 for (size_t index = 0; index < args.size(); ++index) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700195 argsMap[std::string(String8(args[index]).c_str())] = index;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700196 }
197
198 if (argsMap.count("-disable")) {
199 disable();
200 }
201
202 if (argsMap.count("-dump")) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700203 std::optional<uint32_t> maxLayers = std::nullopt;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700204 auto iter = argsMap.find("-maxlayers");
205 if (iter != argsMap.end() && iter->second + 1 < static_cast<int32_t>(args.size())) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700206 int64_t value = strtol(String8(args[iter->second + 1]).c_str(), nullptr, 10);
207 value = std::clamp(value, int64_t(0), int64_t(UINT32_MAX));
208 maxLayers = static_cast<uint32_t>(value);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700209 }
210
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700211 dump(asProto, maxLayers, result);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700212 }
213
214 if (argsMap.count("-clear")) {
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000215 clearAll();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700216 }
217
218 if (argsMap.count("-enable")) {
219 enable();
220 }
221}
222
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700223std::string TimeStats::miniDump() {
224 ATRACE_CALL();
225
226 std::string result = "TimeStats miniDump:\n";
227 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange926ab52019-08-14 15:16:00 -0700228 android::base::StringAppendF(&result, "Number of layers currently being tracked is %zu\n",
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700229 mTimeStatsTracker.size());
Yiwei Zhange926ab52019-08-14 15:16:00 -0700230 android::base::StringAppendF(&result, "Number of layers in the stats pool is %zu\n",
231 mTimeStats.stats.size());
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700232 return result;
233}
234
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700235void TimeStats::incrementTotalFrames() {
236 if (!mEnabled.load()) return;
237
238 ATRACE_CALL();
239
240 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700241 mTimeStats.totalFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700242}
243
Yiwei Zhang621f9d42018-05-07 10:40:55 -0700244void TimeStats::incrementMissedFrames() {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700245 if (!mEnabled.load()) return;
246
247 ATRACE_CALL();
248
249 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700250 mTimeStats.missedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700251}
252
253void TimeStats::incrementClientCompositionFrames() {
254 if (!mEnabled.load()) return;
255
256 ATRACE_CALL();
257
258 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700259 mTimeStats.clientCompositionFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700260}
261
Vishnu Nair9b079a22020-01-21 14:36:08 -0800262void TimeStats::incrementClientCompositionReusedFrames() {
263 if (!mEnabled.load()) return;
264
265 ATRACE_CALL();
266
267 std::lock_guard<std::mutex> lock(mMutex);
268 mTimeStats.clientCompositionReusedFrames++;
269}
270
Alec Mouri9519bf12019-11-15 16:54:44 -0800271static int32_t msBetween(nsecs_t start, nsecs_t end) {
272 int64_t delta = std::chrono::duration_cast<std::chrono::milliseconds>(
273 std::chrono::nanoseconds(end - start))
274 .count();
275 delta = std::clamp(delta, int64_t(INT32_MIN), int64_t(INT32_MAX));
276 return static_cast<int32_t>(delta);
277}
278
279void TimeStats::recordFrameDuration(nsecs_t startTime, nsecs_t endTime) {
280 if (!mEnabled.load()) return;
281
282 std::lock_guard<std::mutex> lock(mMutex);
283 if (mPowerTime.powerMode == HWC_POWER_MODE_NORMAL) {
284 mTimeStats.frameDuration.insert(msBetween(startTime, endTime));
285 }
286}
287
Alec Mourie4034bb2019-11-19 12:45:54 -0800288void TimeStats::recordRenderEngineDuration(nsecs_t startTime, nsecs_t endTime) {
289 if (!mEnabled.load()) return;
290
291 std::lock_guard<std::mutex> lock(mMutex);
292 if (mGlobalRecord.renderEngineDurations.size() == MAX_NUM_TIME_RECORDS) {
293 ALOGE("RenderEngineTimes are already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
294 mGlobalRecord.renderEngineDurations.pop_front();
295 }
296 mGlobalRecord.renderEngineDurations.push_back({startTime, endTime});
297}
298
299void TimeStats::recordRenderEngineDuration(nsecs_t startTime,
300 const std::shared_ptr<FenceTime>& 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
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800311bool TimeStats::recordReadyLocked(int32_t layerId, TimeRecord* timeRecord) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700312 if (!timeRecord->ready) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800313 ALOGV("[%d]-[%" PRIu64 "]-presentFence is still not received", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700314 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700315 return false;
316 }
317
318 if (timeRecord->acquireFence != nullptr) {
319 if (timeRecord->acquireFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
320 return false;
321 }
322 if (timeRecord->acquireFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700323 timeRecord->frameTime.acquireTime = timeRecord->acquireFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700324 timeRecord->acquireFence = nullptr;
325 } else {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800326 ALOGV("[%d]-[%" PRIu64 "]-acquireFence signal time is invalid", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700327 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700328 }
329 }
330
331 if (timeRecord->presentFence != nullptr) {
332 if (timeRecord->presentFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
333 return false;
334 }
335 if (timeRecord->presentFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700336 timeRecord->frameTime.presentTime = timeRecord->presentFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700337 timeRecord->presentFence = nullptr;
338 } else {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800339 ALOGV("[%d]-[%" PRIu64 "]-presentFence signal time invalid", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700340 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700341 }
342 }
343
344 return true;
345}
346
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800347void TimeStats::flushAvailableRecordsToStatsLocked(int32_t layerId) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700348 ATRACE_CALL();
349
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800350 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700351 TimeRecord& prevTimeRecord = layerRecord.prevTimeRecord;
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700352 std::deque<TimeRecord>& timeRecords = layerRecord.timeRecords;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700353 while (!timeRecords.empty()) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800354 if (!recordReadyLocked(layerId, &timeRecords[0])) break;
355 ALOGV("[%d]-[%" PRIu64 "]-presentFenceTime[%" PRId64 "]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700356 timeRecords[0].frameTime.frameNumber, timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700357
358 if (prevTimeRecord.ready) {
Yiwei Zhangeafa5cc2019-07-26 15:06:25 -0700359 const std::string& layerName = layerRecord.layerName;
Yiwei Zhangdc224042018-10-18 15:34:00 -0700360 if (!mTimeStats.stats.count(layerName)) {
361 mTimeStats.stats[layerName].layerName = layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700362 }
Yiwei Zhangdc224042018-10-18 15:34:00 -0700363 TimeStatsHelper::TimeStatsLayer& timeStatsLayer = mTimeStats.stats[layerName];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700364 timeStatsLayer.totalFrames++;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700365 timeStatsLayer.droppedFrames += layerRecord.droppedFrames;
366 layerRecord.droppedFrames = 0;
367
368 const int32_t postToAcquireMs = msBetween(timeRecords[0].frameTime.postTime,
369 timeRecords[0].frameTime.acquireTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800370 ALOGV("[%d]-[%" PRIu64 "]-post2acquire[%d]", layerId,
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700371 timeRecords[0].frameTime.frameNumber, postToAcquireMs);
372 timeStatsLayer.deltas["post2acquire"].insert(postToAcquireMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700373
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700374 const int32_t postToPresentMs = msBetween(timeRecords[0].frameTime.postTime,
375 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800376 ALOGV("[%d]-[%" PRIu64 "]-post2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700377 timeRecords[0].frameTime.frameNumber, postToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700378 timeStatsLayer.deltas["post2present"].insert(postToPresentMs);
379
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700380 const int32_t acquireToPresentMs = msBetween(timeRecords[0].frameTime.acquireTime,
381 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800382 ALOGV("[%d]-[%" PRIu64 "]-acquire2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700383 timeRecords[0].frameTime.frameNumber, acquireToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700384 timeStatsLayer.deltas["acquire2present"].insert(acquireToPresentMs);
385
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700386 const int32_t latchToPresentMs = msBetween(timeRecords[0].frameTime.latchTime,
387 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800388 ALOGV("[%d]-[%" PRIu64 "]-latch2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700389 timeRecords[0].frameTime.frameNumber, latchToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700390 timeStatsLayer.deltas["latch2present"].insert(latchToPresentMs);
391
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700392 const int32_t desiredToPresentMs = msBetween(timeRecords[0].frameTime.desiredTime,
393 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800394 ALOGV("[%d]-[%" PRIu64 "]-desired2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700395 timeRecords[0].frameTime.frameNumber, desiredToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700396 timeStatsLayer.deltas["desired2present"].insert(desiredToPresentMs);
397
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700398 const int32_t presentToPresentMs = msBetween(prevTimeRecord.frameTime.presentTime,
399 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800400 ALOGV("[%d]-[%" PRIu64 "]-present2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700401 timeRecords[0].frameTime.frameNumber, presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700402 timeStatsLayer.deltas["present2present"].insert(presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700403 }
404 prevTimeRecord = timeRecords[0];
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700405 timeRecords.pop_front();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700406 layerRecord.waitData--;
407 }
408}
409
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700410static constexpr const char* kPopupWindowPrefix = "PopupWindow";
411static const size_t kMinLenLayerName = std::strlen(kPopupWindowPrefix);
Yiwei Zhangbd408322018-10-15 18:31:53 -0700412
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700413// Avoid tracking the "PopupWindow:<random hash>#<number>" layers
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700414static bool layerNameIsValid(const std::string& layerName) {
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700415 return layerName.length() >= kMinLenLayerName &&
416 layerName.compare(0, kMinLenLayerName, kPopupWindowPrefix) != 0;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700417}
418
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800419void TimeStats::setPostTime(int32_t layerId, uint64_t frameNumber, const std::string& layerName,
Yiwei Zhang8e8fe522018-11-02 18:34:07 -0700420 nsecs_t postTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700421 if (!mEnabled.load()) return;
422
423 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800424 ALOGV("[%d]-[%" PRIu64 "]-[%s]-PostTime[%" PRId64 "]", layerId, frameNumber, layerName.c_str(),
Yiwei Zhang8e8fe522018-11-02 18:34:07 -0700425 postTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700426
427 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange926ab52019-08-14 15:16:00 -0700428 if (!mTimeStats.stats.count(layerName) && mTimeStats.stats.size() >= MAX_NUM_LAYER_STATS) {
429 return;
430 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800431 if (!mTimeStatsTracker.count(layerId) && mTimeStatsTracker.size() < MAX_NUM_LAYER_RECORDS &&
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700432 layerNameIsValid(layerName)) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800433 mTimeStatsTracker[layerId].layerName = layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700434 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800435 if (!mTimeStatsTracker.count(layerId)) return;
436 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700437 if (layerRecord.timeRecords.size() == MAX_NUM_TIME_RECORDS) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800438 ALOGE("[%d]-[%s]-timeRecords is at its maximum size[%zu]. Ignore this when unittesting.",
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800439 layerId, layerRecord.layerName.c_str(), MAX_NUM_TIME_RECORDS);
440 mTimeStatsTracker.erase(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700441 return;
442 }
443 // For most media content, the acquireFence is invalid because the buffer is
444 // ready at the queueBuffer stage. In this case, acquireTime should be given
445 // a default value as postTime.
446 TimeRecord timeRecord = {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700447 .frameTime =
448 {
449 .frameNumber = frameNumber,
450 .postTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800451 .latchTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700452 .acquireTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800453 .desiredTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700454 },
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700455 };
456 layerRecord.timeRecords.push_back(timeRecord);
457 if (layerRecord.waitData < 0 ||
458 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
459 layerRecord.waitData = layerRecord.timeRecords.size() - 1;
460}
461
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800462void TimeStats::setLatchTime(int32_t layerId, uint64_t frameNumber, nsecs_t latchTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700463 if (!mEnabled.load()) return;
464
465 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800466 ALOGV("[%d]-[%" PRIu64 "]-LatchTime[%" PRId64 "]", layerId, frameNumber, latchTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700467
468 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800469 if (!mTimeStatsTracker.count(layerId)) return;
470 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700471 if (layerRecord.waitData < 0 ||
472 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
473 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700474 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700475 if (timeRecord.frameTime.frameNumber == frameNumber) {
476 timeRecord.frameTime.latchTime = latchTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700477 }
478}
479
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800480void TimeStats::setDesiredTime(int32_t layerId, uint64_t frameNumber, nsecs_t desiredTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700481 if (!mEnabled.load()) return;
482
483 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800484 ALOGV("[%d]-[%" PRIu64 "]-DesiredTime[%" PRId64 "]", layerId, frameNumber, desiredTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700485
486 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800487 if (!mTimeStatsTracker.count(layerId)) return;
488 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700489 if (layerRecord.waitData < 0 ||
490 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
491 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700492 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700493 if (timeRecord.frameTime.frameNumber == frameNumber) {
494 timeRecord.frameTime.desiredTime = desiredTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700495 }
496}
497
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800498void TimeStats::setAcquireTime(int32_t layerId, uint64_t frameNumber, nsecs_t acquireTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700499 if (!mEnabled.load()) return;
500
501 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800502 ALOGV("[%d]-[%" PRIu64 "]-AcquireTime[%" PRId64 "]", layerId, frameNumber, acquireTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700503
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) {
512 timeRecord.frameTime.acquireTime = acquireTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700513 }
514}
515
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800516void TimeStats::setAcquireFence(int32_t layerId, uint64_t frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700517 const std::shared_ptr<FenceTime>& acquireFence) {
518 if (!mEnabled.load()) return;
519
520 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800521 ALOGV("[%d]-[%" PRIu64 "]-AcquireFenceTime[%" PRId64 "]", layerId, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700522 acquireFence->getSignalTime());
523
524 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800525 if (!mTimeStatsTracker.count(layerId)) return;
526 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700527 if (layerRecord.waitData < 0 ||
528 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
529 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700530 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700531 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700532 timeRecord.acquireFence = acquireFence;
533 }
534}
535
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800536void TimeStats::setPresentTime(int32_t layerId, uint64_t frameNumber, nsecs_t presentTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700537 if (!mEnabled.load()) return;
538
539 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800540 ALOGV("[%d]-[%" PRIu64 "]-PresentTime[%" PRId64 "]", layerId, frameNumber, presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700541
542 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800543 if (!mTimeStatsTracker.count(layerId)) return;
544 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700545 if (layerRecord.waitData < 0 ||
546 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
547 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700548 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700549 if (timeRecord.frameTime.frameNumber == frameNumber) {
550 timeRecord.frameTime.presentTime = presentTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700551 timeRecord.ready = true;
552 layerRecord.waitData++;
553 }
554
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800555 flushAvailableRecordsToStatsLocked(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700556}
557
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800558void TimeStats::setPresentFence(int32_t layerId, uint64_t frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700559 const std::shared_ptr<FenceTime>& presentFence) {
560 if (!mEnabled.load()) return;
561
562 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800563 ALOGV("[%d]-[%" PRIu64 "]-PresentFenceTime[%" PRId64 "]", layerId, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700564 presentFence->getSignalTime());
565
566 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800567 if (!mTimeStatsTracker.count(layerId)) return;
568 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700569 if (layerRecord.waitData < 0 ||
570 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
571 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700572 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700573 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700574 timeRecord.presentFence = presentFence;
575 timeRecord.ready = true;
576 layerRecord.waitData++;
577 }
578
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800579 flushAvailableRecordsToStatsLocked(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700580}
581
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800582void TimeStats::onDestroy(int32_t layerId) {
Yiwei Zhangdc224042018-10-18 15:34:00 -0700583 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800584 ALOGV("[%d]-onDestroy", layerId);
Mikael Pessa90092f42019-08-26 17:22:04 -0700585 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800586 mTimeStatsTracker.erase(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700587}
588
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800589void TimeStats::removeTimeRecord(int32_t layerId, uint64_t frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700590 if (!mEnabled.load()) return;
591
592 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800593 ALOGV("[%d]-[%" PRIu64 "]-removeTimeRecord", layerId, frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700594
595 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800596 if (!mTimeStatsTracker.count(layerId)) return;
597 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700598 size_t removeAt = 0;
599 for (const TimeRecord& record : layerRecord.timeRecords) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700600 if (record.frameTime.frameNumber == frameNumber) break;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700601 removeAt++;
602 }
603 if (removeAt == layerRecord.timeRecords.size()) return;
604 layerRecord.timeRecords.erase(layerRecord.timeRecords.begin() + removeAt);
605 if (layerRecord.waitData > static_cast<int32_t>(removeAt)) {
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700606 layerRecord.waitData--;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700607 }
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700608 layerRecord.droppedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700609}
610
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700611void TimeStats::flushPowerTimeLocked() {
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700612 if (!mEnabled.load()) return;
613
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700614 nsecs_t curTime = systemTime();
615 // elapsedTime is in milliseconds.
616 int64_t elapsedTime = (curTime - mPowerTime.prevTime) / 1000000;
617
618 switch (mPowerTime.powerMode) {
619 case HWC_POWER_MODE_NORMAL:
620 mTimeStats.displayOnTime += elapsedTime;
621 break;
622 case HWC_POWER_MODE_OFF:
623 case HWC_POWER_MODE_DOZE:
624 case HWC_POWER_MODE_DOZE_SUSPEND:
625 default:
626 break;
627 }
628
629 mPowerTime.prevTime = curTime;
630}
631
632void TimeStats::setPowerMode(int32_t powerMode) {
633 if (!mEnabled.load()) {
634 std::lock_guard<std::mutex> lock(mMutex);
635 mPowerTime.powerMode = powerMode;
636 return;
637 }
638
639 std::lock_guard<std::mutex> lock(mMutex);
640 if (powerMode == mPowerTime.powerMode) return;
641
642 flushPowerTimeLocked();
643 mPowerTime.powerMode = powerMode;
644}
645
Alec Mourifb571ea2019-01-24 18:42:10 -0800646void TimeStats::recordRefreshRate(uint32_t fps, nsecs_t duration) {
647 std::lock_guard<std::mutex> lock(mMutex);
648 if (mTimeStats.refreshRateStats.count(fps)) {
649 mTimeStats.refreshRateStats[fps] += duration;
650 } else {
651 mTimeStats.refreshRateStats.insert({fps, duration});
652 }
653}
654
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700655void TimeStats::flushAvailableGlobalRecordsToStatsLocked() {
656 ATRACE_CALL();
657
658 while (!mGlobalRecord.presentFences.empty()) {
659 const nsecs_t curPresentTime = mGlobalRecord.presentFences.front()->getSignalTime();
660 if (curPresentTime == Fence::SIGNAL_TIME_PENDING) break;
661
662 if (curPresentTime == Fence::SIGNAL_TIME_INVALID) {
663 ALOGE("GlobalPresentFence is invalid!");
664 mGlobalRecord.prevPresentTime = 0;
665 mGlobalRecord.presentFences.pop_front();
666 continue;
667 }
668
669 ALOGV("GlobalPresentFenceTime[%" PRId64 "]",
670 mGlobalRecord.presentFences.front()->getSignalTime());
671
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700672 if (mGlobalRecord.prevPresentTime != 0) {
673 const int32_t presentToPresentMs =
674 msBetween(mGlobalRecord.prevPresentTime, curPresentTime);
675 ALOGV("Global present2present[%d] prev[%" PRId64 "] curr[%" PRId64 "]",
676 presentToPresentMs, mGlobalRecord.prevPresentTime, curPresentTime);
677 mTimeStats.presentToPresent.insert(presentToPresentMs);
678 }
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700679
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700680 mGlobalRecord.prevPresentTime = curPresentTime;
681 mGlobalRecord.presentFences.pop_front();
682 }
Alec Mourie4034bb2019-11-19 12:45:54 -0800683 while (!mGlobalRecord.renderEngineDurations.empty()) {
684 const auto duration = mGlobalRecord.renderEngineDurations.front();
685 const auto& endTime = duration.endTime;
686
687 nsecs_t endNs = -1;
688
689 if (auto val = std::get_if<nsecs_t>(&endTime)) {
690 endNs = *val;
691 } else {
692 endNs = std::get<std::shared_ptr<FenceTime>>(endTime)->getSignalTime();
693 }
694
695 if (endNs == Fence::SIGNAL_TIME_PENDING) break;
696
697 if (endNs < 0) {
698 ALOGE("RenderEngineTiming is invalid!");
699 mGlobalRecord.renderEngineDurations.pop_front();
700 continue;
701 }
702
703 const int32_t renderEngineMs = msBetween(duration.startTime, endNs);
704 mTimeStats.renderEngineTiming.insert(renderEngineMs);
705
706 mGlobalRecord.renderEngineDurations.pop_front();
707 }
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700708}
709
710void TimeStats::setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) {
711 if (!mEnabled.load()) return;
712
713 ATRACE_CALL();
714 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700715 if (presentFence == nullptr || !presentFence->isValid()) {
716 mGlobalRecord.prevPresentTime = 0;
717 return;
718 }
719
720 if (mPowerTime.powerMode != HWC_POWER_MODE_NORMAL) {
721 // Try flushing the last present fence on HWC_POWER_MODE_NORMAL.
722 flushAvailableGlobalRecordsToStatsLocked();
723 mGlobalRecord.presentFences.clear();
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700724 mGlobalRecord.prevPresentTime = 0;
725 return;
726 }
727
728 if (mGlobalRecord.presentFences.size() == MAX_NUM_TIME_RECORDS) {
729 // The front presentFence must be trapped in pending status in this
730 // case. Try dequeuing the front one to recover.
731 ALOGE("GlobalPresentFences is already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
732 mGlobalRecord.prevPresentTime = 0;
733 mGlobalRecord.presentFences.pop_front();
734 }
735
736 mGlobalRecord.presentFences.emplace_back(presentFence);
737 flushAvailableGlobalRecordsToStatsLocked();
738}
739
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700740void TimeStats::enable() {
741 if (mEnabled.load()) return;
742
743 ATRACE_CALL();
744
745 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700746 mEnabled.store(true);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700747 mTimeStats.statsStart = static_cast<int64_t>(std::time(0));
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700748 mPowerTime.prevTime = systemTime();
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700749 ALOGD("Enabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700750}
751
752void TimeStats::disable() {
753 if (!mEnabled.load()) return;
754
755 ATRACE_CALL();
756
757 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700758 flushPowerTimeLocked();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700759 mEnabled.store(false);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700760 mTimeStats.statsEnd = static_cast<int64_t>(std::time(0));
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700761 ALOGD("Disabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700762}
763
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000764void TimeStats::clearAll() {
765 std::lock_guard<std::mutex> lock(mMutex);
766 clearGlobalLocked();
767 clearLayersLocked();
768}
769
770void TimeStats::clearGlobalLocked() {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700771 ATRACE_CALL();
772
Yiwei Zhangdc224042018-10-18 15:34:00 -0700773 mTimeStats.statsStart = (mEnabled.load() ? static_cast<int64_t>(std::time(0)) : 0);
774 mTimeStats.statsEnd = 0;
775 mTimeStats.totalFrames = 0;
776 mTimeStats.missedFrames = 0;
777 mTimeStats.clientCompositionFrames = 0;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800778 mTimeStats.clientCompositionReusedFrames = 0;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700779 mTimeStats.displayOnTime = 0;
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700780 mTimeStats.presentToPresent.hist.clear();
Alec Mouri31ac64a2020-01-09 09:26:22 -0800781 mTimeStats.frameDuration.hist.clear();
782 mTimeStats.renderEngineTiming.hist.clear();
Alec Mourifb571ea2019-01-24 18:42:10 -0800783 mTimeStats.refreshRateStats.clear();
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700784 mPowerTime.prevTime = systemTime();
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700785 mGlobalRecord.prevPresentTime = 0;
786 mGlobalRecord.presentFences.clear();
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000787 ALOGD("Cleared global stats");
788}
789
790void TimeStats::clearLayersLocked() {
791 ATRACE_CALL();
792
793 mTimeStatsTracker.clear();
794 mTimeStats.stats.clear();
795 ALOGD("Cleared layer stats");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700796}
797
798bool TimeStats::isEnabled() {
799 return mEnabled.load();
800}
801
Yiwei Zhang5434a782018-12-05 18:06:32 -0800802void TimeStats::dump(bool asProto, std::optional<uint32_t> maxLayers, std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700803 ATRACE_CALL();
804
805 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700806 if (mTimeStats.statsStart == 0) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700807 return;
808 }
809
Yiwei Zhangdc224042018-10-18 15:34:00 -0700810 mTimeStats.statsEnd = static_cast<int64_t>(std::time(0));
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700811
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700812 flushPowerTimeLocked();
813
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700814 if (asProto) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700815 ALOGD("Dumping TimeStats as proto");
Yiwei Zhangdc224042018-10-18 15:34:00 -0700816 SFTimeStatsGlobalProto timeStatsProto = mTimeStats.toProto(maxLayers);
Dominik Laskowski46470112019-08-02 13:13:11 -0700817 result.append(timeStatsProto.SerializeAsString());
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700818 } else {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700819 ALOGD("Dumping TimeStats as text");
Yiwei Zhang5434a782018-12-05 18:06:32 -0800820 result.append(mTimeStats.toString(maxLayers));
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700821 result.append("\n");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700822 }
823}
824
Alec Mourifb571ea2019-01-24 18:42:10 -0800825} // namespace impl
826
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700827} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800828
829// TODO(b/129481165): remove the #pragma below and fix conversion issues
830#pragma clang diagnostic pop // ignored "-Wconversion"