blob: 96fd03a316d857a783fe73f03186ae50237e577d [file] [log] [blame]
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001/*
2 * Copyright 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080016
17// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
Yiwei Zhang0102ad22018-05-02 17:37:17 -070020#undef LOG_TAG
21#define LOG_TAG "TimeStats"
22#define ATRACE_TAG ATRACE_TAG_GRAPHICS
23
24#include "TimeStats.h"
25
26#include <android-base/stringprintf.h>
Alec Mouri37384342020-01-02 17:23:37 -080027#include <android/util/ProtoOutputStream.h>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070028#include <log/log.h>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070029#include <utils/String8.h>
Yiwei Zhang3a226d22018-10-16 09:23:03 -070030#include <utils/Timers.h>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070031#include <utils/Trace.h>
32
33#include <algorithm>
Alec Mouri9519bf12019-11-15 16:54:44 -080034#include <chrono>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070035
36namespace android {
37
Alec Mourifb571ea2019-01-24 18:42:10 -080038namespace impl {
39
Tej Singh2a457b62020-01-31 16:16:10 -080040AStatsManager_PullAtomCallbackReturn TimeStats::pullAtomCallback(int32_t atom_tag,
41 AStatsEventList* data,
42 void* cookie) {
Alec Mouri8e2f31b2020-01-16 22:04:35 +000043 impl::TimeStats* timeStats = reinterpret_cast<impl::TimeStats*>(cookie);
Tej Singh2a457b62020-01-31 16:16:10 -080044 AStatsManager_PullAtomCallbackReturn result = AStatsManager_PULL_SKIP;
Alec Mouri37384342020-01-02 17:23:37 -080045 if (atom_tag == android::util::SURFACEFLINGER_STATS_GLOBAL_INFO) {
Alec Mouri3ecd5cd2020-01-29 12:53:07 -080046 result = timeStats->populateGlobalAtom(data);
Alec Mouri37384342020-01-02 17:23:37 -080047 } else if (atom_tag == android::util::SURFACEFLINGER_STATS_LAYER_INFO) {
Alec Mouri3ecd5cd2020-01-29 12:53:07 -080048 result = timeStats->populateLayerAtom(data);
Alec Mouri8e2f31b2020-01-16 22:04:35 +000049 }
50
Alec Mouri3ecd5cd2020-01-29 12:53:07 -080051 // Enable timestats now. The first full pull for a given build is expected to
52 // have empty or very little stats, as stats are first enabled after the
53 // first pull is completed for either the global or layer stats.
54 timeStats->enable();
55 return result;
Alec Mouri37384342020-01-02 17:23:37 -080056}
Alec Mouri8e2f31b2020-01-16 22:04:35 +000057
Alec Mouri37384342020-01-02 17:23:37 -080058namespace {
59// Histograms align with the order of fields in SurfaceflingerStatsLayerInfo.
60const std::array<std::string, 6> kHistogramNames = {
61 "present2present", "post2present", "acquire2present",
62 "latch2present", "desired2present", "post2acquire",
63};
Alec Mouri8e2f31b2020-01-16 22:04:35 +000064
Alec Mouri37384342020-01-02 17:23:37 -080065std::string histogramToProtoByteString(const std::unordered_map<int32_t, int32_t>& histogram,
66 size_t maxPulledHistogramBuckets) {
67 auto buckets = std::vector<std::pair<int32_t, int32_t>>(histogram.begin(), histogram.end());
68 std::sort(buckets.begin(), buckets.end(),
69 [](std::pair<int32_t, int32_t>& left, std::pair<int32_t, int32_t>& right) {
70 return left.second > right.second;
71 });
72
73 util::ProtoOutputStream proto;
74 int histogramSize = 0;
75 for (const auto& bucket : buckets) {
76 if (++histogramSize > maxPulledHistogramBuckets) {
77 break;
78 }
79 proto.write(android::util::FIELD_TYPE_INT32 | android::util::FIELD_COUNT_REPEATED |
80 1 /* field id */,
81 (int32_t)bucket.first);
82 proto.write(android::util::FIELD_TYPE_INT64 | android::util::FIELD_COUNT_REPEATED |
83 2 /* field id */,
84 (int64_t)bucket.second);
85 }
86
87 std::string byteString;
88 proto.serializeToString(&byteString);
89 return byteString;
90}
91} // namespace
92
Alec Mouridfad9002020-02-12 17:49:09 -080093AStatsManager_PullAtomCallbackReturn TimeStats::populateGlobalAtom(AStatsEventList* data) {
94 std::lock_guard<std::mutex> lock(mMutex);
95
96 if (mTimeStats.statsStart == 0) {
97 return AStatsManager_PULL_SKIP;
98 }
99 flushPowerTimeLocked();
100
101 AStatsEvent* event = mStatsDelegate->addStatsEventToPullData(data);
102 mStatsDelegate->statsEventSetAtomId(event, android::util::SURFACEFLINGER_STATS_GLOBAL_INFO);
103 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.totalFrames);
104 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.missedFrames);
105 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.clientCompositionFrames);
106 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.displayOnTime);
107 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.presentToPresent.totalTime());
108 mStatsDelegate->statsEventWriteInt32(event, mTimeStats.displayEventConnectionsCount);
109 std::string frameDurationBytes =
110 histogramToProtoByteString(mTimeStats.frameDuration.hist, mMaxPulledHistogramBuckets);
111 mStatsDelegate->statsEventWriteByteArray(event, (const uint8_t*)frameDurationBytes.c_str(),
112 frameDurationBytes.size());
113 std::string renderEngineTimingBytes =
114 histogramToProtoByteString(mTimeStats.renderEngineTiming.hist,
115 mMaxPulledHistogramBuckets);
116 mStatsDelegate->statsEventWriteByteArray(event, (const uint8_t*)renderEngineTimingBytes.c_str(),
117 renderEngineTimingBytes.size());
118 mStatsDelegate->statsEventBuild(event);
119 clearGlobalLocked();
120
121 return AStatsManager_PULL_SUCCESS;
122}
123
Tej Singh2a457b62020-01-31 16:16:10 -0800124AStatsManager_PullAtomCallbackReturn TimeStats::populateLayerAtom(AStatsEventList* data) {
Alec Mouri37384342020-01-02 17:23:37 -0800125 std::lock_guard<std::mutex> lock(mMutex);
126
127 std::vector<TimeStatsHelper::TimeStatsLayer const*> dumpStats;
128 for (const auto& ele : mTimeStats.stats) {
129 dumpStats.push_back(&ele.second);
130 }
131
132 std::sort(dumpStats.begin(), dumpStats.end(),
133 [](TimeStatsHelper::TimeStatsLayer const* l,
134 TimeStatsHelper::TimeStatsLayer const* r) {
135 return l->totalFrames > r->totalFrames;
136 });
137
138 if (mMaxPulledLayers < dumpStats.size()) {
139 dumpStats.resize(mMaxPulledLayers);
140 }
141
142 for (const auto& layer : dumpStats) {
Tej Singh2a457b62020-01-31 16:16:10 -0800143 AStatsEvent* event = mStatsDelegate->addStatsEventToPullData(data);
Alec Mouri37384342020-01-02 17:23:37 -0800144 mStatsDelegate->statsEventSetAtomId(event, android::util::SURFACEFLINGER_STATS_LAYER_INFO);
145 mStatsDelegate->statsEventWriteString8(event, layer->layerName.c_str());
146 mStatsDelegate->statsEventWriteInt64(event, layer->totalFrames);
147 mStatsDelegate->statsEventWriteInt64(event, layer->droppedFrames);
148
149 for (const auto& name : kHistogramNames) {
150 const auto& histogram = layer->deltas.find(name);
151 if (histogram == layer->deltas.cend()) {
152 mStatsDelegate->statsEventWriteByteArray(event, nullptr, 0);
153 } else {
154 std::string bytes = histogramToProtoByteString(histogram->second.hist,
155 mMaxPulledHistogramBuckets);
156 mStatsDelegate->statsEventWriteByteArray(event, (const uint8_t*)bytes.c_str(),
157 bytes.size());
158 }
159 }
160
Yiwei Zhang7bfc75b2020-02-10 11:20:34 -0800161 mStatsDelegate->statsEventWriteInt64(event, layer->lateAcquireFrames);
162 mStatsDelegate->statsEventWriteInt64(event, layer->badDesiredPresentFrames);
163
Alec Mouri37384342020-01-02 17:23:37 -0800164 mStatsDelegate->statsEventBuild(event);
165 }
166 clearLayersLocked();
167
Tej Singh2a457b62020-01-31 16:16:10 -0800168 return AStatsManager_PULL_SUCCESS;
Alec Mouri37384342020-01-02 17:23:37 -0800169}
170
171TimeStats::TimeStats() : TimeStats(nullptr, std::nullopt, std::nullopt) {}
172
173TimeStats::TimeStats(std::unique_ptr<StatsEventDelegate> statsDelegate,
174 std::optional<size_t> maxPulledLayers,
175 std::optional<size_t> maxPulledHistogramBuckets) {
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000176 if (statsDelegate != nullptr) {
177 mStatsDelegate = std::move(statsDelegate);
178 }
Alec Mouri37384342020-01-02 17:23:37 -0800179
180 if (maxPulledLayers) {
181 mMaxPulledLayers = *maxPulledLayers;
182 }
183
184 if (maxPulledHistogramBuckets) {
185 mMaxPulledHistogramBuckets = *maxPulledHistogramBuckets;
186 }
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000187}
188
Alec Mouri3ecd5cd2020-01-29 12:53:07 -0800189TimeStats::~TimeStats() {
190 std::lock_guard<std::mutex> lock(mMutex);
191 mStatsDelegate->unregisterStatsPullAtomCallback(
192 android::util::SURFACEFLINGER_STATS_GLOBAL_INFO);
193 mStatsDelegate->unregisterStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_LAYER_INFO);
194}
195
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000196void TimeStats::onBootFinished() {
Alec Mouri3ecd5cd2020-01-29 12:53:07 -0800197 std::lock_guard<std::mutex> lock(mMutex);
198 mStatsDelegate->registerStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_GLOBAL_INFO,
199 TimeStats::pullAtomCallback, nullptr, this);
200 mStatsDelegate->registerStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_LAYER_INFO,
201 TimeStats::pullAtomCallback, nullptr, this);
Alec Mourib3885ad2019-09-06 17:08:55 -0700202}
203
Dominik Laskowskic2867142019-01-21 11:33:38 -0800204void TimeStats::parseArgs(bool asProto, const Vector<String16>& args, std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700205 ATRACE_CALL();
206
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700207 std::unordered_map<std::string, int32_t> argsMap;
Dominik Laskowskic2867142019-01-21 11:33:38 -0800208 for (size_t index = 0; index < args.size(); ++index) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700209 argsMap[std::string(String8(args[index]).c_str())] = index;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700210 }
211
212 if (argsMap.count("-disable")) {
213 disable();
214 }
215
216 if (argsMap.count("-dump")) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700217 std::optional<uint32_t> maxLayers = std::nullopt;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700218 auto iter = argsMap.find("-maxlayers");
219 if (iter != argsMap.end() && iter->second + 1 < static_cast<int32_t>(args.size())) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700220 int64_t value = strtol(String8(args[iter->second + 1]).c_str(), nullptr, 10);
221 value = std::clamp(value, int64_t(0), int64_t(UINT32_MAX));
222 maxLayers = static_cast<uint32_t>(value);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700223 }
224
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700225 dump(asProto, maxLayers, result);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700226 }
227
228 if (argsMap.count("-clear")) {
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000229 clearAll();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700230 }
231
232 if (argsMap.count("-enable")) {
233 enable();
234 }
235}
236
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700237std::string TimeStats::miniDump() {
238 ATRACE_CALL();
239
240 std::string result = "TimeStats miniDump:\n";
241 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange926ab52019-08-14 15:16:00 -0700242 android::base::StringAppendF(&result, "Number of layers currently being tracked is %zu\n",
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700243 mTimeStatsTracker.size());
Yiwei Zhange926ab52019-08-14 15:16:00 -0700244 android::base::StringAppendF(&result, "Number of layers in the stats pool is %zu\n",
245 mTimeStats.stats.size());
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700246 return result;
247}
248
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700249void TimeStats::incrementTotalFrames() {
250 if (!mEnabled.load()) return;
251
252 ATRACE_CALL();
253
254 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700255 mTimeStats.totalFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700256}
257
Yiwei Zhang621f9d42018-05-07 10:40:55 -0700258void TimeStats::incrementMissedFrames() {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700259 if (!mEnabled.load()) return;
260
261 ATRACE_CALL();
262
263 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700264 mTimeStats.missedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700265}
266
267void TimeStats::incrementClientCompositionFrames() {
268 if (!mEnabled.load()) return;
269
270 ATRACE_CALL();
271
272 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700273 mTimeStats.clientCompositionFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700274}
275
Vishnu Nair9b079a22020-01-21 14:36:08 -0800276void TimeStats::incrementClientCompositionReusedFrames() {
277 if (!mEnabled.load()) return;
278
279 ATRACE_CALL();
280
281 std::lock_guard<std::mutex> lock(mMutex);
282 mTimeStats.clientCompositionReusedFrames++;
283}
284
Alec Mouri8de697e2020-03-19 10:52:01 -0700285void TimeStats::incrementRefreshRateSwitches() {
286 if (!mEnabled.load()) return;
287
288 ATRACE_CALL();
289
290 std::lock_guard<std::mutex> lock(mMutex);
291 mTimeStats.refreshRateSwitches++;
292}
293
Alec Mouri717bcb62020-02-10 17:07:19 -0800294void TimeStats::recordDisplayEventConnectionCount(int32_t count) {
295 if (!mEnabled.load()) return;
296
297 ATRACE_CALL();
298
299 std::lock_guard<std::mutex> lock(mMutex);
300 mTimeStats.displayEventConnectionsCount =
301 std::max(mTimeStats.displayEventConnectionsCount, count);
302}
303
Alec Mouri9519bf12019-11-15 16:54:44 -0800304static int32_t msBetween(nsecs_t start, nsecs_t end) {
305 int64_t delta = std::chrono::duration_cast<std::chrono::milliseconds>(
306 std::chrono::nanoseconds(end - start))
307 .count();
308 delta = std::clamp(delta, int64_t(INT32_MIN), int64_t(INT32_MAX));
309 return static_cast<int32_t>(delta);
310}
311
312void TimeStats::recordFrameDuration(nsecs_t startTime, nsecs_t endTime) {
313 if (!mEnabled.load()) return;
314
315 std::lock_guard<std::mutex> lock(mMutex);
316 if (mPowerTime.powerMode == HWC_POWER_MODE_NORMAL) {
317 mTimeStats.frameDuration.insert(msBetween(startTime, endTime));
318 }
319}
320
Alec Mourie4034bb2019-11-19 12:45:54 -0800321void TimeStats::recordRenderEngineDuration(nsecs_t startTime, nsecs_t endTime) {
322 if (!mEnabled.load()) return;
323
324 std::lock_guard<std::mutex> lock(mMutex);
325 if (mGlobalRecord.renderEngineDurations.size() == MAX_NUM_TIME_RECORDS) {
326 ALOGE("RenderEngineTimes are already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
327 mGlobalRecord.renderEngineDurations.pop_front();
328 }
329 mGlobalRecord.renderEngineDurations.push_back({startTime, endTime});
330}
331
332void TimeStats::recordRenderEngineDuration(nsecs_t startTime,
333 const std::shared_ptr<FenceTime>& endTime) {
334 if (!mEnabled.load()) return;
335
336 std::lock_guard<std::mutex> lock(mMutex);
337 if (mGlobalRecord.renderEngineDurations.size() == MAX_NUM_TIME_RECORDS) {
338 ALOGE("RenderEngineTimes are already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
339 mGlobalRecord.renderEngineDurations.pop_front();
340 }
341 mGlobalRecord.renderEngineDurations.push_back({startTime, endTime});
342}
343
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800344bool TimeStats::recordReadyLocked(int32_t layerId, TimeRecord* timeRecord) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700345 if (!timeRecord->ready) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800346 ALOGV("[%d]-[%" PRIu64 "]-presentFence is still not received", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700347 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700348 return false;
349 }
350
351 if (timeRecord->acquireFence != nullptr) {
352 if (timeRecord->acquireFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
353 return false;
354 }
355 if (timeRecord->acquireFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700356 timeRecord->frameTime.acquireTime = timeRecord->acquireFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700357 timeRecord->acquireFence = nullptr;
358 } else {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800359 ALOGV("[%d]-[%" PRIu64 "]-acquireFence signal time is invalid", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700360 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700361 }
362 }
363
364 if (timeRecord->presentFence != nullptr) {
365 if (timeRecord->presentFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
366 return false;
367 }
368 if (timeRecord->presentFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700369 timeRecord->frameTime.presentTime = timeRecord->presentFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700370 timeRecord->presentFence = nullptr;
371 } else {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800372 ALOGV("[%d]-[%" PRIu64 "]-presentFence signal time invalid", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700373 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700374 }
375 }
376
377 return true;
378}
379
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800380void TimeStats::flushAvailableRecordsToStatsLocked(int32_t layerId) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700381 ATRACE_CALL();
382
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800383 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700384 TimeRecord& prevTimeRecord = layerRecord.prevTimeRecord;
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700385 std::deque<TimeRecord>& timeRecords = layerRecord.timeRecords;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700386 while (!timeRecords.empty()) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800387 if (!recordReadyLocked(layerId, &timeRecords[0])) break;
388 ALOGV("[%d]-[%" PRIu64 "]-presentFenceTime[%" PRId64 "]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700389 timeRecords[0].frameTime.frameNumber, timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700390
391 if (prevTimeRecord.ready) {
Yiwei Zhangeafa5cc2019-07-26 15:06:25 -0700392 const std::string& layerName = layerRecord.layerName;
Yiwei Zhangdc224042018-10-18 15:34:00 -0700393 if (!mTimeStats.stats.count(layerName)) {
394 mTimeStats.stats[layerName].layerName = layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700395 }
Yiwei Zhangdc224042018-10-18 15:34:00 -0700396 TimeStatsHelper::TimeStatsLayer& timeStatsLayer = mTimeStats.stats[layerName];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700397 timeStatsLayer.totalFrames++;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700398 timeStatsLayer.droppedFrames += layerRecord.droppedFrames;
Alec Mouri91f6df32020-01-30 08:48:58 -0800399 timeStatsLayer.lateAcquireFrames += layerRecord.lateAcquireFrames;
400 timeStatsLayer.badDesiredPresentFrames += layerRecord.badDesiredPresentFrames;
401
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700402 layerRecord.droppedFrames = 0;
Alec Mouri91f6df32020-01-30 08:48:58 -0800403 layerRecord.lateAcquireFrames = 0;
404 layerRecord.badDesiredPresentFrames = 0;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700405
406 const int32_t postToAcquireMs = msBetween(timeRecords[0].frameTime.postTime,
407 timeRecords[0].frameTime.acquireTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800408 ALOGV("[%d]-[%" PRIu64 "]-post2acquire[%d]", layerId,
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700409 timeRecords[0].frameTime.frameNumber, postToAcquireMs);
410 timeStatsLayer.deltas["post2acquire"].insert(postToAcquireMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700411
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700412 const int32_t postToPresentMs = msBetween(timeRecords[0].frameTime.postTime,
413 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800414 ALOGV("[%d]-[%" PRIu64 "]-post2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700415 timeRecords[0].frameTime.frameNumber, postToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700416 timeStatsLayer.deltas["post2present"].insert(postToPresentMs);
417
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700418 const int32_t acquireToPresentMs = msBetween(timeRecords[0].frameTime.acquireTime,
419 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800420 ALOGV("[%d]-[%" PRIu64 "]-acquire2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700421 timeRecords[0].frameTime.frameNumber, acquireToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700422 timeStatsLayer.deltas["acquire2present"].insert(acquireToPresentMs);
423
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700424 const int32_t latchToPresentMs = msBetween(timeRecords[0].frameTime.latchTime,
425 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800426 ALOGV("[%d]-[%" PRIu64 "]-latch2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700427 timeRecords[0].frameTime.frameNumber, latchToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700428 timeStatsLayer.deltas["latch2present"].insert(latchToPresentMs);
429
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700430 const int32_t desiredToPresentMs = msBetween(timeRecords[0].frameTime.desiredTime,
431 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800432 ALOGV("[%d]-[%" PRIu64 "]-desired2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700433 timeRecords[0].frameTime.frameNumber, desiredToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700434 timeStatsLayer.deltas["desired2present"].insert(desiredToPresentMs);
435
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700436 const int32_t presentToPresentMs = msBetween(prevTimeRecord.frameTime.presentTime,
437 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800438 ALOGV("[%d]-[%" PRIu64 "]-present2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700439 timeRecords[0].frameTime.frameNumber, presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700440 timeStatsLayer.deltas["present2present"].insert(presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700441 }
442 prevTimeRecord = timeRecords[0];
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700443 timeRecords.pop_front();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700444 layerRecord.waitData--;
445 }
446}
447
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700448static constexpr const char* kPopupWindowPrefix = "PopupWindow";
449static const size_t kMinLenLayerName = std::strlen(kPopupWindowPrefix);
Yiwei Zhangbd408322018-10-15 18:31:53 -0700450
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700451// Avoid tracking the "PopupWindow:<random hash>#<number>" layers
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700452static bool layerNameIsValid(const std::string& layerName) {
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700453 return layerName.length() >= kMinLenLayerName &&
454 layerName.compare(0, kMinLenLayerName, kPopupWindowPrefix) != 0;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700455}
456
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800457void TimeStats::setPostTime(int32_t layerId, uint64_t frameNumber, const std::string& layerName,
Yiwei Zhang8e8fe522018-11-02 18:34:07 -0700458 nsecs_t postTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700459 if (!mEnabled.load()) return;
460
461 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800462 ALOGV("[%d]-[%" PRIu64 "]-[%s]-PostTime[%" PRId64 "]", layerId, frameNumber, layerName.c_str(),
Yiwei Zhang8e8fe522018-11-02 18:34:07 -0700463 postTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700464
465 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange926ab52019-08-14 15:16:00 -0700466 if (!mTimeStats.stats.count(layerName) && mTimeStats.stats.size() >= MAX_NUM_LAYER_STATS) {
467 return;
468 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800469 if (!mTimeStatsTracker.count(layerId) && mTimeStatsTracker.size() < MAX_NUM_LAYER_RECORDS &&
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700470 layerNameIsValid(layerName)) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800471 mTimeStatsTracker[layerId].layerName = layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700472 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800473 if (!mTimeStatsTracker.count(layerId)) return;
474 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700475 if (layerRecord.timeRecords.size() == MAX_NUM_TIME_RECORDS) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800476 ALOGE("[%d]-[%s]-timeRecords is at its maximum size[%zu]. Ignore this when unittesting.",
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800477 layerId, layerRecord.layerName.c_str(), MAX_NUM_TIME_RECORDS);
478 mTimeStatsTracker.erase(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700479 return;
480 }
481 // For most media content, the acquireFence is invalid because the buffer is
482 // ready at the queueBuffer stage. In this case, acquireTime should be given
483 // a default value as postTime.
484 TimeRecord timeRecord = {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700485 .frameTime =
486 {
487 .frameNumber = frameNumber,
488 .postTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800489 .latchTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700490 .acquireTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800491 .desiredTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700492 },
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700493 };
494 layerRecord.timeRecords.push_back(timeRecord);
495 if (layerRecord.waitData < 0 ||
496 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
497 layerRecord.waitData = layerRecord.timeRecords.size() - 1;
498}
499
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800500void TimeStats::setLatchTime(int32_t layerId, uint64_t frameNumber, nsecs_t latchTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700501 if (!mEnabled.load()) return;
502
503 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800504 ALOGV("[%d]-[%" PRIu64 "]-LatchTime[%" PRId64 "]", layerId, frameNumber, latchTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700505
506 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800507 if (!mTimeStatsTracker.count(layerId)) return;
508 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700509 if (layerRecord.waitData < 0 ||
510 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
511 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700512 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700513 if (timeRecord.frameTime.frameNumber == frameNumber) {
514 timeRecord.frameTime.latchTime = latchTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700515 }
516}
517
Alec Mouri91f6df32020-01-30 08:48:58 -0800518void TimeStats::incrementLatchSkipped(int32_t layerId, LatchSkipReason reason) {
519 if (!mEnabled.load()) return;
520
521 ATRACE_CALL();
522 ALOGV("[%d]-LatchSkipped-Reason[%d]", layerId,
523 static_cast<std::underlying_type<LatchSkipReason>::type>(reason));
524
525 std::lock_guard<std::mutex> lock(mMutex);
526 if (!mTimeStatsTracker.count(layerId)) return;
527 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
528
529 switch (reason) {
530 case LatchSkipReason::LateAcquire:
531 layerRecord.lateAcquireFrames++;
532 break;
533 }
534}
535
536void TimeStats::incrementBadDesiredPresent(int32_t layerId) {
537 if (!mEnabled.load()) return;
538
539 ATRACE_CALL();
540 ALOGV("[%d]-BadDesiredPresent", layerId);
541
542 std::lock_guard<std::mutex> lock(mMutex);
543 if (!mTimeStatsTracker.count(layerId)) return;
544 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
545 layerRecord.badDesiredPresentFrames++;
546}
547
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800548void TimeStats::setDesiredTime(int32_t layerId, uint64_t frameNumber, nsecs_t desiredTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700549 if (!mEnabled.load()) return;
550
551 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800552 ALOGV("[%d]-[%" PRIu64 "]-DesiredTime[%" PRId64 "]", layerId, frameNumber, desiredTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700553
554 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800555 if (!mTimeStatsTracker.count(layerId)) return;
556 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700557 if (layerRecord.waitData < 0 ||
558 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
559 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700560 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700561 if (timeRecord.frameTime.frameNumber == frameNumber) {
562 timeRecord.frameTime.desiredTime = desiredTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700563 }
564}
565
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800566void TimeStats::setAcquireTime(int32_t layerId, uint64_t frameNumber, nsecs_t acquireTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700567 if (!mEnabled.load()) return;
568
569 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800570 ALOGV("[%d]-[%" PRIu64 "]-AcquireTime[%" PRId64 "]", layerId, frameNumber, acquireTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700571
572 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800573 if (!mTimeStatsTracker.count(layerId)) return;
574 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700575 if (layerRecord.waitData < 0 ||
576 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
577 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700578 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700579 if (timeRecord.frameTime.frameNumber == frameNumber) {
580 timeRecord.frameTime.acquireTime = acquireTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700581 }
582}
583
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800584void TimeStats::setAcquireFence(int32_t layerId, uint64_t frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700585 const std::shared_ptr<FenceTime>& acquireFence) {
586 if (!mEnabled.load()) return;
587
588 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800589 ALOGV("[%d]-[%" PRIu64 "]-AcquireFenceTime[%" PRId64 "]", layerId, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700590 acquireFence->getSignalTime());
591
592 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800593 if (!mTimeStatsTracker.count(layerId)) return;
594 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700595 if (layerRecord.waitData < 0 ||
596 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
597 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700598 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700599 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700600 timeRecord.acquireFence = acquireFence;
601 }
602}
603
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800604void TimeStats::setPresentTime(int32_t layerId, uint64_t frameNumber, nsecs_t presentTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700605 if (!mEnabled.load()) return;
606
607 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800608 ALOGV("[%d]-[%" PRIu64 "]-PresentTime[%" PRId64 "]", layerId, frameNumber, presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700609
610 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800611 if (!mTimeStatsTracker.count(layerId)) return;
612 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700613 if (layerRecord.waitData < 0 ||
614 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
615 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700616 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700617 if (timeRecord.frameTime.frameNumber == frameNumber) {
618 timeRecord.frameTime.presentTime = presentTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700619 timeRecord.ready = true;
620 layerRecord.waitData++;
621 }
622
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800623 flushAvailableRecordsToStatsLocked(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700624}
625
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800626void TimeStats::setPresentFence(int32_t layerId, uint64_t frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700627 const std::shared_ptr<FenceTime>& presentFence) {
628 if (!mEnabled.load()) return;
629
630 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800631 ALOGV("[%d]-[%" PRIu64 "]-PresentFenceTime[%" PRId64 "]", layerId, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700632 presentFence->getSignalTime());
633
634 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800635 if (!mTimeStatsTracker.count(layerId)) return;
636 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700637 if (layerRecord.waitData < 0 ||
638 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
639 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700640 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700641 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700642 timeRecord.presentFence = presentFence;
643 timeRecord.ready = true;
644 layerRecord.waitData++;
645 }
646
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800647 flushAvailableRecordsToStatsLocked(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700648}
649
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800650void TimeStats::onDestroy(int32_t layerId) {
Yiwei Zhangdc224042018-10-18 15:34:00 -0700651 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800652 ALOGV("[%d]-onDestroy", layerId);
Mikael Pessa90092f42019-08-26 17:22:04 -0700653 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800654 mTimeStatsTracker.erase(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700655}
656
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800657void TimeStats::removeTimeRecord(int32_t layerId, uint64_t frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700658 if (!mEnabled.load()) return;
659
660 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800661 ALOGV("[%d]-[%" PRIu64 "]-removeTimeRecord", layerId, frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700662
663 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800664 if (!mTimeStatsTracker.count(layerId)) return;
665 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700666 size_t removeAt = 0;
667 for (const TimeRecord& record : layerRecord.timeRecords) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700668 if (record.frameTime.frameNumber == frameNumber) break;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700669 removeAt++;
670 }
671 if (removeAt == layerRecord.timeRecords.size()) return;
672 layerRecord.timeRecords.erase(layerRecord.timeRecords.begin() + removeAt);
673 if (layerRecord.waitData > static_cast<int32_t>(removeAt)) {
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700674 layerRecord.waitData--;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700675 }
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700676 layerRecord.droppedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700677}
678
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700679void TimeStats::flushPowerTimeLocked() {
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700680 if (!mEnabled.load()) return;
681
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700682 nsecs_t curTime = systemTime();
683 // elapsedTime is in milliseconds.
684 int64_t elapsedTime = (curTime - mPowerTime.prevTime) / 1000000;
685
686 switch (mPowerTime.powerMode) {
687 case HWC_POWER_MODE_NORMAL:
688 mTimeStats.displayOnTime += elapsedTime;
689 break;
690 case HWC_POWER_MODE_OFF:
691 case HWC_POWER_MODE_DOZE:
692 case HWC_POWER_MODE_DOZE_SUSPEND:
693 default:
694 break;
695 }
696
697 mPowerTime.prevTime = curTime;
698}
699
700void TimeStats::setPowerMode(int32_t powerMode) {
701 if (!mEnabled.load()) {
702 std::lock_guard<std::mutex> lock(mMutex);
703 mPowerTime.powerMode = powerMode;
704 return;
705 }
706
707 std::lock_guard<std::mutex> lock(mMutex);
708 if (powerMode == mPowerTime.powerMode) return;
709
710 flushPowerTimeLocked();
711 mPowerTime.powerMode = powerMode;
712}
713
Alec Mourifb571ea2019-01-24 18:42:10 -0800714void TimeStats::recordRefreshRate(uint32_t fps, nsecs_t duration) {
715 std::lock_guard<std::mutex> lock(mMutex);
716 if (mTimeStats.refreshRateStats.count(fps)) {
717 mTimeStats.refreshRateStats[fps] += duration;
718 } else {
719 mTimeStats.refreshRateStats.insert({fps, duration});
720 }
721}
722
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700723void TimeStats::flushAvailableGlobalRecordsToStatsLocked() {
724 ATRACE_CALL();
725
726 while (!mGlobalRecord.presentFences.empty()) {
727 const nsecs_t curPresentTime = mGlobalRecord.presentFences.front()->getSignalTime();
728 if (curPresentTime == Fence::SIGNAL_TIME_PENDING) break;
729
730 if (curPresentTime == Fence::SIGNAL_TIME_INVALID) {
731 ALOGE("GlobalPresentFence is invalid!");
732 mGlobalRecord.prevPresentTime = 0;
733 mGlobalRecord.presentFences.pop_front();
734 continue;
735 }
736
737 ALOGV("GlobalPresentFenceTime[%" PRId64 "]",
738 mGlobalRecord.presentFences.front()->getSignalTime());
739
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700740 if (mGlobalRecord.prevPresentTime != 0) {
741 const int32_t presentToPresentMs =
742 msBetween(mGlobalRecord.prevPresentTime, curPresentTime);
743 ALOGV("Global present2present[%d] prev[%" PRId64 "] curr[%" PRId64 "]",
744 presentToPresentMs, mGlobalRecord.prevPresentTime, curPresentTime);
745 mTimeStats.presentToPresent.insert(presentToPresentMs);
746 }
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700747
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700748 mGlobalRecord.prevPresentTime = curPresentTime;
749 mGlobalRecord.presentFences.pop_front();
750 }
Alec Mourie4034bb2019-11-19 12:45:54 -0800751 while (!mGlobalRecord.renderEngineDurations.empty()) {
752 const auto duration = mGlobalRecord.renderEngineDurations.front();
753 const auto& endTime = duration.endTime;
754
755 nsecs_t endNs = -1;
756
757 if (auto val = std::get_if<nsecs_t>(&endTime)) {
758 endNs = *val;
759 } else {
760 endNs = std::get<std::shared_ptr<FenceTime>>(endTime)->getSignalTime();
761 }
762
763 if (endNs == Fence::SIGNAL_TIME_PENDING) break;
764
765 if (endNs < 0) {
766 ALOGE("RenderEngineTiming is invalid!");
767 mGlobalRecord.renderEngineDurations.pop_front();
768 continue;
769 }
770
771 const int32_t renderEngineMs = msBetween(duration.startTime, endNs);
772 mTimeStats.renderEngineTiming.insert(renderEngineMs);
773
774 mGlobalRecord.renderEngineDurations.pop_front();
775 }
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700776}
777
778void TimeStats::setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) {
779 if (!mEnabled.load()) return;
780
781 ATRACE_CALL();
782 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700783 if (presentFence == nullptr || !presentFence->isValid()) {
784 mGlobalRecord.prevPresentTime = 0;
785 return;
786 }
787
788 if (mPowerTime.powerMode != HWC_POWER_MODE_NORMAL) {
789 // Try flushing the last present fence on HWC_POWER_MODE_NORMAL.
790 flushAvailableGlobalRecordsToStatsLocked();
791 mGlobalRecord.presentFences.clear();
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700792 mGlobalRecord.prevPresentTime = 0;
793 return;
794 }
795
796 if (mGlobalRecord.presentFences.size() == MAX_NUM_TIME_RECORDS) {
797 // The front presentFence must be trapped in pending status in this
798 // case. Try dequeuing the front one to recover.
799 ALOGE("GlobalPresentFences is already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
800 mGlobalRecord.prevPresentTime = 0;
801 mGlobalRecord.presentFences.pop_front();
802 }
803
804 mGlobalRecord.presentFences.emplace_back(presentFence);
805 flushAvailableGlobalRecordsToStatsLocked();
806}
807
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700808void TimeStats::enable() {
809 if (mEnabled.load()) return;
810
811 ATRACE_CALL();
812
813 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700814 mEnabled.store(true);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700815 mTimeStats.statsStart = static_cast<int64_t>(std::time(0));
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700816 mPowerTime.prevTime = systemTime();
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700817 ALOGD("Enabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700818}
819
820void TimeStats::disable() {
821 if (!mEnabled.load()) return;
822
823 ATRACE_CALL();
824
825 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700826 flushPowerTimeLocked();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700827 mEnabled.store(false);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700828 mTimeStats.statsEnd = static_cast<int64_t>(std::time(0));
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700829 ALOGD("Disabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700830}
831
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000832void TimeStats::clearAll() {
833 std::lock_guard<std::mutex> lock(mMutex);
834 clearGlobalLocked();
835 clearLayersLocked();
836}
837
838void TimeStats::clearGlobalLocked() {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700839 ATRACE_CALL();
840
Yiwei Zhangdc224042018-10-18 15:34:00 -0700841 mTimeStats.statsStart = (mEnabled.load() ? static_cast<int64_t>(std::time(0)) : 0);
842 mTimeStats.statsEnd = 0;
843 mTimeStats.totalFrames = 0;
844 mTimeStats.missedFrames = 0;
845 mTimeStats.clientCompositionFrames = 0;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800846 mTimeStats.clientCompositionReusedFrames = 0;
Alec Mouri8de697e2020-03-19 10:52:01 -0700847 mTimeStats.refreshRateSwitches = 0;
Alec Mouri717bcb62020-02-10 17:07:19 -0800848 mTimeStats.displayEventConnectionsCount = 0;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700849 mTimeStats.displayOnTime = 0;
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700850 mTimeStats.presentToPresent.hist.clear();
Alec Mouri31ac64a2020-01-09 09:26:22 -0800851 mTimeStats.frameDuration.hist.clear();
852 mTimeStats.renderEngineTiming.hist.clear();
Alec Mourifb571ea2019-01-24 18:42:10 -0800853 mTimeStats.refreshRateStats.clear();
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700854 mPowerTime.prevTime = systemTime();
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700855 mGlobalRecord.prevPresentTime = 0;
856 mGlobalRecord.presentFences.clear();
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000857 ALOGD("Cleared global stats");
858}
859
860void TimeStats::clearLayersLocked() {
861 ATRACE_CALL();
862
863 mTimeStatsTracker.clear();
864 mTimeStats.stats.clear();
865 ALOGD("Cleared layer stats");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700866}
867
868bool TimeStats::isEnabled() {
869 return mEnabled.load();
870}
871
Yiwei Zhang5434a782018-12-05 18:06:32 -0800872void TimeStats::dump(bool asProto, std::optional<uint32_t> maxLayers, std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700873 ATRACE_CALL();
874
875 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhangdc224042018-10-18 15:34:00 -0700876 if (mTimeStats.statsStart == 0) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700877 return;
878 }
879
Yiwei Zhangdc224042018-10-18 15:34:00 -0700880 mTimeStats.statsEnd = static_cast<int64_t>(std::time(0));
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700881
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700882 flushPowerTimeLocked();
883
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700884 if (asProto) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700885 ALOGD("Dumping TimeStats as proto");
Yiwei Zhangdc224042018-10-18 15:34:00 -0700886 SFTimeStatsGlobalProto timeStatsProto = mTimeStats.toProto(maxLayers);
Dominik Laskowski46470112019-08-02 13:13:11 -0700887 result.append(timeStatsProto.SerializeAsString());
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700888 } else {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700889 ALOGD("Dumping TimeStats as text");
Yiwei Zhang5434a782018-12-05 18:06:32 -0800890 result.append(mTimeStats.toString(maxLayers));
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700891 result.append("\n");
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700892 }
893}
894
Alec Mourifb571ea2019-01-24 18:42:10 -0800895} // namespace impl
896
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700897} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800898
899// TODO(b/129481165): remove the #pragma below and fix conversion issues
900#pragma clang diagnostic pop // ignored "-Wconversion"