blob: 2094972ed0e4b78138f18b557f71f9f07732ff6f [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
Alec Mouri75de8f22021-01-20 14:53:44 -080017#include <unordered_map>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070018#undef LOG_TAG
19#define LOG_TAG "TimeStats"
20#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
22#include "TimeStats.h"
23
24#include <android-base/stringprintf.h>
Alec Mouri37384342020-01-02 17:23:37 -080025#include <android/util/ProtoOutputStream.h>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070026#include <log/log.h>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070027#include <utils/String8.h>
Yiwei Zhang3a226d22018-10-16 09:23:03 -070028#include <utils/Timers.h>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070029#include <utils/Trace.h>
30
31#include <algorithm>
Alec Mouri9519bf12019-11-15 16:54:44 -080032#include <chrono>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070033
Alec Mouri9a29e672020-09-14 12:39:14 -070034#include "timestatsproto/TimeStatsHelper.h"
35
Yiwei Zhang0102ad22018-05-02 17:37:17 -070036namespace 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}
Alec Mouri75de8f22021-01-20 14:53:44 -080091
Ady Abraham8b9e6122021-01-26 19:11:45 -080092std::string frameRateVoteToProtoByteString(
93 float refreshRate,
94 TimeStats::SetFrameRateVote::FrameRateCompatibility frameRateCompatibility,
95 TimeStats::SetFrameRateVote::Seamlessness seamlessness) {
Alec Mouri75de8f22021-01-20 14:53:44 -080096 util::ProtoOutputStream proto;
97 proto.write(android::util::FIELD_TYPE_FLOAT | 1 /* field id */, refreshRate);
Ady Abraham8b9e6122021-01-26 19:11:45 -080098 proto.write(android::util::FIELD_TYPE_ENUM | 2 /* field id */,
99 static_cast<int>(frameRateCompatibility));
100 proto.write(android::util::FIELD_TYPE_ENUM | 3 /* field id */, static_cast<int>(seamlessness));
Alec Mouri75de8f22021-01-20 14:53:44 -0800101
102 std::string byteString;
103 proto.serializeToString(&byteString);
104 return byteString;
105}
Alec Mouri37384342020-01-02 17:23:37 -0800106} // namespace
107
Alec Mouridfad9002020-02-12 17:49:09 -0800108AStatsManager_PullAtomCallbackReturn TimeStats::populateGlobalAtom(AStatsEventList* data) {
109 std::lock_guard<std::mutex> lock(mMutex);
110
Alec Mouri7d436ec2021-01-27 20:40:50 -0800111 if (mTimeStats.statsStartLegacy == 0) {
Alec Mouridfad9002020-02-12 17:49:09 -0800112 return AStatsManager_PULL_SKIP;
113 }
114 flushPowerTimeLocked();
115
Alec Mouri7d436ec2021-01-27 20:40:50 -0800116 for (const auto& globalSlice : mTimeStats.stats) {
117 AStatsEvent* event = mStatsDelegate->addStatsEventToPullData(data);
118 mStatsDelegate->statsEventSetAtomId(event, android::util::SURFACEFLINGER_STATS_GLOBAL_INFO);
119 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.totalFramesLegacy);
120 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.missedFramesLegacy);
121 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.clientCompositionFramesLegacy);
122 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.displayOnTimeLegacy);
123 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.presentToPresentLegacy.totalTime());
124 mStatsDelegate->statsEventWriteInt32(event, mTimeStats.displayEventConnectionsCountLegacy);
125 std::string frameDurationBytes =
126 histogramToProtoByteString(mTimeStats.frameDurationLegacy.hist,
127 mMaxPulledHistogramBuckets);
128 mStatsDelegate->statsEventWriteByteArray(event, (const uint8_t*)frameDurationBytes.c_str(),
129 frameDurationBytes.size());
130 std::string renderEngineTimingBytes =
131 histogramToProtoByteString(mTimeStats.renderEngineTimingLegacy.hist,
132 mMaxPulledHistogramBuckets);
133 mStatsDelegate->statsEventWriteByteArray(event,
134 (const uint8_t*)renderEngineTimingBytes.c_str(),
135 renderEngineTimingBytes.size());
Alec Mouri9a29e672020-09-14 12:39:14 -0700136
Alec Mouri7d436ec2021-01-27 20:40:50 -0800137 mStatsDelegate->statsEventWriteInt32(event, globalSlice.second.jankPayload.totalFrames);
138 mStatsDelegate->statsEventWriteInt32(event,
139 globalSlice.second.jankPayload.totalJankyFrames);
140 mStatsDelegate->statsEventWriteInt32(event, globalSlice.second.jankPayload.totalSFLongCpu);
141 mStatsDelegate->statsEventWriteInt32(event, globalSlice.second.jankPayload.totalSFLongGpu);
142 mStatsDelegate->statsEventWriteInt32(event,
143 globalSlice.second.jankPayload.totalSFUnattributed);
144 mStatsDelegate->statsEventWriteInt32(event,
145 globalSlice.second.jankPayload.totalAppUnattributed);
Adithya Srinivasanead17162021-02-18 02:17:37 +0000146 mStatsDelegate->statsEventWriteInt32(event,
147 globalSlice.second.jankPayload.totalSFScheduling);
148 mStatsDelegate->statsEventWriteInt32(event,
149 globalSlice.second.jankPayload.totalSFPredictionError);
150 mStatsDelegate->statsEventWriteInt32(event,
151 globalSlice.second.jankPayload.totalAppBufferStuffing);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800152 mStatsDelegate->statsEventWriteInt32(event, globalSlice.first.displayRefreshRateBucket);
153 std::string sfDeadlineMissedBytes =
Alec Mouri363faf02021-01-29 16:34:55 -0800154 histogramToProtoByteString(globalSlice.second.displayDeadlineDeltas.hist,
Alec Mouri7d436ec2021-01-27 20:40:50 -0800155 mMaxPulledHistogramBuckets);
Alec Mouri363faf02021-01-29 16:34:55 -0800156 mStatsDelegate->statsEventWriteByteArray(event,
157 (const uint8_t*)sfDeadlineMissedBytes.c_str(),
158 sfDeadlineMissedBytes.size());
Alec Mouri7d436ec2021-01-27 20:40:50 -0800159 std::string sfPredictionErrorBytes =
Alec Mouri363faf02021-01-29 16:34:55 -0800160 histogramToProtoByteString(globalSlice.second.displayPresentDeltas.hist,
Alec Mouri7d436ec2021-01-27 20:40:50 -0800161 mMaxPulledHistogramBuckets);
Alec Mouri363faf02021-01-29 16:34:55 -0800162 mStatsDelegate->statsEventWriteByteArray(event,
163 (const uint8_t*)sfPredictionErrorBytes.c_str(),
164 sfPredictionErrorBytes.size());
Alec Mouri7d436ec2021-01-27 20:40:50 -0800165 mStatsDelegate->statsEventWriteInt32(event, globalSlice.first.renderRateBucket);
166 mStatsDelegate->statsEventBuild(event);
167 }
168
Alec Mouridfad9002020-02-12 17:49:09 -0800169 clearGlobalLocked();
170
171 return AStatsManager_PULL_SUCCESS;
172}
173
Tej Singh2a457b62020-01-31 16:16:10 -0800174AStatsManager_PullAtomCallbackReturn TimeStats::populateLayerAtom(AStatsEventList* data) {
Alec Mouri37384342020-01-02 17:23:37 -0800175 std::lock_guard<std::mutex> lock(mMutex);
176
Alec Mouri363faf02021-01-29 16:34:55 -0800177 std::vector<TimeStatsHelper::TimeStatsLayer*> dumpStats;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800178 uint32_t numLayers = 0;
179 for (const auto& globalSlice : mTimeStats.stats) {
180 numLayers += globalSlice.second.stats.size();
181 }
182
183 dumpStats.reserve(numLayers);
184
Alec Mouri363faf02021-01-29 16:34:55 -0800185 for (auto& globalSlice : mTimeStats.stats) {
186 for (auto& layerSlice : globalSlice.second.stats) {
Alec Mouri7d436ec2021-01-27 20:40:50 -0800187 dumpStats.push_back(&layerSlice.second);
188 }
Alec Mouri37384342020-01-02 17:23:37 -0800189 }
190
191 std::sort(dumpStats.begin(), dumpStats.end(),
192 [](TimeStatsHelper::TimeStatsLayer const* l,
193 TimeStatsHelper::TimeStatsLayer const* r) {
194 return l->totalFrames > r->totalFrames;
195 });
196
197 if (mMaxPulledLayers < dumpStats.size()) {
198 dumpStats.resize(mMaxPulledLayers);
199 }
200
Alec Mouri363faf02021-01-29 16:34:55 -0800201 for (auto& layer : dumpStats) {
Tej Singh2a457b62020-01-31 16:16:10 -0800202 AStatsEvent* event = mStatsDelegate->addStatsEventToPullData(data);
Alec Mouri37384342020-01-02 17:23:37 -0800203 mStatsDelegate->statsEventSetAtomId(event, android::util::SURFACEFLINGER_STATS_LAYER_INFO);
204 mStatsDelegate->statsEventWriteString8(event, layer->layerName.c_str());
205 mStatsDelegate->statsEventWriteInt64(event, layer->totalFrames);
206 mStatsDelegate->statsEventWriteInt64(event, layer->droppedFrames);
207
208 for (const auto& name : kHistogramNames) {
209 const auto& histogram = layer->deltas.find(name);
210 if (histogram == layer->deltas.cend()) {
211 mStatsDelegate->statsEventWriteByteArray(event, nullptr, 0);
212 } else {
213 std::string bytes = histogramToProtoByteString(histogram->second.hist,
214 mMaxPulledHistogramBuckets);
215 mStatsDelegate->statsEventWriteByteArray(event, (const uint8_t*)bytes.c_str(),
216 bytes.size());
217 }
218 }
219
Yiwei Zhang7bfc75b2020-02-10 11:20:34 -0800220 mStatsDelegate->statsEventWriteInt64(event, layer->lateAcquireFrames);
221 mStatsDelegate->statsEventWriteInt64(event, layer->badDesiredPresentFrames);
Alec Mouri9a29e672020-09-14 12:39:14 -0700222 mStatsDelegate->statsEventWriteInt32(event, layer->uid);
223 mStatsDelegate->statsEventWriteInt32(event, layer->jankPayload.totalFrames);
224 mStatsDelegate->statsEventWriteInt32(event, layer->jankPayload.totalJankyFrames);
225 mStatsDelegate->statsEventWriteInt32(event, layer->jankPayload.totalSFLongCpu);
226 mStatsDelegate->statsEventWriteInt32(event, layer->jankPayload.totalSFLongGpu);
227 mStatsDelegate->statsEventWriteInt32(event, layer->jankPayload.totalSFUnattributed);
228 mStatsDelegate->statsEventWriteInt32(event, layer->jankPayload.totalAppUnattributed);
Adithya Srinivasanead17162021-02-18 02:17:37 +0000229 mStatsDelegate->statsEventWriteInt32(event, layer->jankPayload.totalSFScheduling);
230 mStatsDelegate->statsEventWriteInt32(event, layer->jankPayload.totalSFPredictionError);
231 mStatsDelegate->statsEventWriteInt32(event, layer->jankPayload.totalAppBufferStuffing);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800232 mStatsDelegate->statsEventWriteInt32(
233 event, layer->displayRefreshRateBucket); // display_refresh_rate_bucket
234 mStatsDelegate->statsEventWriteInt32(event, layer->renderRateBucket); // render_rate_bucket
Ady Abraham8b9e6122021-01-26 19:11:45 -0800235 std::string frameRateVoteBytes =
236 frameRateVoteToProtoByteString(layer->setFrameRateVote.frameRate,
237 layer->setFrameRateVote.frameRateCompatibility,
238 layer->setFrameRateVote.seamlessness);
Alec Mouri75de8f22021-01-20 14:53:44 -0800239 mStatsDelegate->statsEventWriteByteArray(event, (const uint8_t*)frameRateVoteBytes.c_str(),
240 frameRateVoteBytes.size()); // set_frame_rate_vote
241 std::string appDeadlineMissedBytes =
Alec Mouri363faf02021-01-29 16:34:55 -0800242 histogramToProtoByteString(layer->deltas["appDeadlineDeltas"].hist,
Alec Mouri75de8f22021-01-20 14:53:44 -0800243 mMaxPulledHistogramBuckets);
Alec Mouri363faf02021-01-29 16:34:55 -0800244 mStatsDelegate->statsEventWriteByteArray(event,
245 (const uint8_t*)appDeadlineMissedBytes.c_str(),
246 appDeadlineMissedBytes.size());
Alec Mouri75de8f22021-01-20 14:53:44 -0800247
Alec Mouri37384342020-01-02 17:23:37 -0800248 mStatsDelegate->statsEventBuild(event);
249 }
250 clearLayersLocked();
251
Tej Singh2a457b62020-01-31 16:16:10 -0800252 return AStatsManager_PULL_SUCCESS;
Alec Mouri37384342020-01-02 17:23:37 -0800253}
254
255TimeStats::TimeStats() : TimeStats(nullptr, std::nullopt, std::nullopt) {}
256
257TimeStats::TimeStats(std::unique_ptr<StatsEventDelegate> statsDelegate,
258 std::optional<size_t> maxPulledLayers,
259 std::optional<size_t> maxPulledHistogramBuckets) {
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000260 if (statsDelegate != nullptr) {
261 mStatsDelegate = std::move(statsDelegate);
262 }
Alec Mouri37384342020-01-02 17:23:37 -0800263
264 if (maxPulledLayers) {
265 mMaxPulledLayers = *maxPulledLayers;
266 }
267
268 if (maxPulledHistogramBuckets) {
269 mMaxPulledHistogramBuckets = *maxPulledHistogramBuckets;
270 }
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000271}
272
Alec Mouri3ecd5cd2020-01-29 12:53:07 -0800273TimeStats::~TimeStats() {
274 std::lock_guard<std::mutex> lock(mMutex);
Tej Singh38a4b212020-03-13 19:04:51 -0700275 mStatsDelegate->clearStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_GLOBAL_INFO);
276 mStatsDelegate->clearStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_LAYER_INFO);
Alec Mouri3ecd5cd2020-01-29 12:53:07 -0800277}
278
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000279void TimeStats::onBootFinished() {
Alec Mouri3ecd5cd2020-01-29 12:53:07 -0800280 std::lock_guard<std::mutex> lock(mMutex);
Tej Singh38a4b212020-03-13 19:04:51 -0700281 mStatsDelegate->setStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_GLOBAL_INFO,
282 nullptr, TimeStats::pullAtomCallback, this);
283 mStatsDelegate->setStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_LAYER_INFO,
284 nullptr, TimeStats::pullAtomCallback, this);
Alec Mourib3885ad2019-09-06 17:08:55 -0700285}
286
Dominik Laskowskic2867142019-01-21 11:33:38 -0800287void TimeStats::parseArgs(bool asProto, const Vector<String16>& args, std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700288 ATRACE_CALL();
289
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700290 std::unordered_map<std::string, int32_t> argsMap;
Dominik Laskowskic2867142019-01-21 11:33:38 -0800291 for (size_t index = 0; index < args.size(); ++index) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700292 argsMap[std::string(String8(args[index]).c_str())] = index;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700293 }
294
295 if (argsMap.count("-disable")) {
296 disable();
297 }
298
299 if (argsMap.count("-dump")) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700300 std::optional<uint32_t> maxLayers = std::nullopt;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700301 auto iter = argsMap.find("-maxlayers");
302 if (iter != argsMap.end() && iter->second + 1 < static_cast<int32_t>(args.size())) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700303 int64_t value = strtol(String8(args[iter->second + 1]).c_str(), nullptr, 10);
304 value = std::clamp(value, int64_t(0), int64_t(UINT32_MAX));
305 maxLayers = static_cast<uint32_t>(value);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700306 }
307
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700308 dump(asProto, maxLayers, result);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700309 }
310
311 if (argsMap.count("-clear")) {
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000312 clearAll();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700313 }
314
315 if (argsMap.count("-enable")) {
316 enable();
317 }
318}
319
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700320std::string TimeStats::miniDump() {
321 ATRACE_CALL();
322
323 std::string result = "TimeStats miniDump:\n";
324 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange926ab52019-08-14 15:16:00 -0700325 android::base::StringAppendF(&result, "Number of layers currently being tracked is %zu\n",
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700326 mTimeStatsTracker.size());
Yiwei Zhange926ab52019-08-14 15:16:00 -0700327 android::base::StringAppendF(&result, "Number of layers in the stats pool is %zu\n",
328 mTimeStats.stats.size());
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700329 return result;
330}
331
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700332void TimeStats::incrementTotalFrames() {
333 if (!mEnabled.load()) return;
334
335 ATRACE_CALL();
336
337 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800338 mTimeStats.totalFramesLegacy++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700339}
340
Yiwei Zhang621f9d42018-05-07 10:40:55 -0700341void TimeStats::incrementMissedFrames() {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700342 if (!mEnabled.load()) return;
343
344 ATRACE_CALL();
345
346 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800347 mTimeStats.missedFramesLegacy++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700348}
349
350void TimeStats::incrementClientCompositionFrames() {
351 if (!mEnabled.load()) return;
352
353 ATRACE_CALL();
354
355 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800356 mTimeStats.clientCompositionFramesLegacy++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700357}
358
Vishnu Nair9b079a22020-01-21 14:36:08 -0800359void TimeStats::incrementClientCompositionReusedFrames() {
360 if (!mEnabled.load()) return;
361
362 ATRACE_CALL();
363
364 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800365 mTimeStats.clientCompositionReusedFramesLegacy++;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800366}
367
Alec Mouri8de697e2020-03-19 10:52:01 -0700368void TimeStats::incrementRefreshRateSwitches() {
369 if (!mEnabled.load()) return;
370
371 ATRACE_CALL();
372
373 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800374 mTimeStats.refreshRateSwitchesLegacy++;
Alec Mouri8de697e2020-03-19 10:52:01 -0700375}
376
Alec Mouri8f7a0102020-04-15 12:11:10 -0700377void TimeStats::incrementCompositionStrategyChanges() {
378 if (!mEnabled.load()) return;
379
380 ATRACE_CALL();
381
382 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800383 mTimeStats.compositionStrategyChangesLegacy++;
Alec Mouri8f7a0102020-04-15 12:11:10 -0700384}
385
Alec Mouri717bcb62020-02-10 17:07:19 -0800386void TimeStats::recordDisplayEventConnectionCount(int32_t count) {
387 if (!mEnabled.load()) return;
388
389 ATRACE_CALL();
390
391 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800392 mTimeStats.displayEventConnectionsCountLegacy =
393 std::max(mTimeStats.displayEventConnectionsCountLegacy, count);
Alec Mouri717bcb62020-02-10 17:07:19 -0800394}
395
Alec Mouri9519bf12019-11-15 16:54:44 -0800396static int32_t msBetween(nsecs_t start, nsecs_t end) {
397 int64_t delta = std::chrono::duration_cast<std::chrono::milliseconds>(
398 std::chrono::nanoseconds(end - start))
399 .count();
400 delta = std::clamp(delta, int64_t(INT32_MIN), int64_t(INT32_MAX));
401 return static_cast<int32_t>(delta);
402}
403
404void TimeStats::recordFrameDuration(nsecs_t startTime, nsecs_t endTime) {
405 if (!mEnabled.load()) return;
406
407 std::lock_guard<std::mutex> lock(mMutex);
Peiyong Lin65248e02020-04-18 21:15:07 -0700408 if (mPowerTime.powerMode == PowerMode::ON) {
Alec Mouri7d436ec2021-01-27 20:40:50 -0800409 mTimeStats.frameDurationLegacy.insert(msBetween(startTime, endTime));
Alec Mouri9519bf12019-11-15 16:54:44 -0800410 }
411}
412
Alec Mourie4034bb2019-11-19 12:45:54 -0800413void TimeStats::recordRenderEngineDuration(nsecs_t startTime, nsecs_t endTime) {
414 if (!mEnabled.load()) return;
415
416 std::lock_guard<std::mutex> lock(mMutex);
417 if (mGlobalRecord.renderEngineDurations.size() == MAX_NUM_TIME_RECORDS) {
418 ALOGE("RenderEngineTimes are already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
419 mGlobalRecord.renderEngineDurations.pop_front();
420 }
421 mGlobalRecord.renderEngineDurations.push_back({startTime, endTime});
422}
423
424void TimeStats::recordRenderEngineDuration(nsecs_t startTime,
425 const std::shared_ptr<FenceTime>& endTime) {
426 if (!mEnabled.load()) return;
427
428 std::lock_guard<std::mutex> lock(mMutex);
429 if (mGlobalRecord.renderEngineDurations.size() == MAX_NUM_TIME_RECORDS) {
430 ALOGE("RenderEngineTimes are already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
431 mGlobalRecord.renderEngineDurations.pop_front();
432 }
433 mGlobalRecord.renderEngineDurations.push_back({startTime, endTime});
434}
435
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800436bool TimeStats::recordReadyLocked(int32_t layerId, TimeRecord* timeRecord) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700437 if (!timeRecord->ready) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800438 ALOGV("[%d]-[%" PRIu64 "]-presentFence is still not received", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700439 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700440 return false;
441 }
442
443 if (timeRecord->acquireFence != nullptr) {
444 if (timeRecord->acquireFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
445 return false;
446 }
447 if (timeRecord->acquireFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700448 timeRecord->frameTime.acquireTime = timeRecord->acquireFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700449 timeRecord->acquireFence = nullptr;
450 } else {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800451 ALOGV("[%d]-[%" PRIu64 "]-acquireFence signal time is invalid", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700452 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700453 }
454 }
455
456 if (timeRecord->presentFence != nullptr) {
457 if (timeRecord->presentFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
458 return false;
459 }
460 if (timeRecord->presentFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700461 timeRecord->frameTime.presentTime = timeRecord->presentFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700462 timeRecord->presentFence = nullptr;
463 } else {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800464 ALOGV("[%d]-[%" PRIu64 "]-presentFence signal time invalid", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700465 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700466 }
467 }
468
469 return true;
470}
471
Alec Mouri7d436ec2021-01-27 20:40:50 -0800472static int32_t clampToSmallestBucket(Fps fps, size_t bucketWidth) {
473 return (fps.getIntValue() / bucketWidth) * bucketWidth;
474}
475
476void TimeStats::flushAvailableRecordsToStatsLocked(int32_t layerId, Fps displayRefreshRate,
Ady Abraham8b9e6122021-01-26 19:11:45 -0800477 std::optional<Fps> renderRate,
478 SetFrameRateVote frameRateVote) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700479 ATRACE_CALL();
Ady Abraham8b9e6122021-01-26 19:11:45 -0800480 ALOGV("[%d]-flushAvailableRecordsToStatsLocked", layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700481
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800482 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700483 TimeRecord& prevTimeRecord = layerRecord.prevTimeRecord;
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700484 std::deque<TimeRecord>& timeRecords = layerRecord.timeRecords;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800485 const int32_t refreshRateBucket =
486 clampToSmallestBucket(displayRefreshRate, REFRESH_RATE_BUCKET_WIDTH);
487 const int32_t renderRateBucket =
488 clampToSmallestBucket(renderRate ? *renderRate : displayRefreshRate,
489 RENDER_RATE_BUCKET_WIDTH);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700490 while (!timeRecords.empty()) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800491 if (!recordReadyLocked(layerId, &timeRecords[0])) break;
492 ALOGV("[%d]-[%" PRIu64 "]-presentFenceTime[%" PRId64 "]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700493 timeRecords[0].frameTime.frameNumber, timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700494
495 if (prevTimeRecord.ready) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700496 uid_t uid = layerRecord.uid;
Yiwei Zhangeafa5cc2019-07-26 15:06:25 -0700497 const std::string& layerName = layerRecord.layerName;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800498 TimeStatsHelper::TimelineStatsKey timelineKey = {refreshRateBucket, renderRateBucket};
499 if (!mTimeStats.stats.count(timelineKey)) {
500 mTimeStats.stats[timelineKey].key = timelineKey;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700501 }
Alec Mouri7d436ec2021-01-27 20:40:50 -0800502
503 TimeStatsHelper::TimelineStats& displayStats = mTimeStats.stats[timelineKey];
504
505 TimeStatsHelper::LayerStatsKey layerKey = {uid, layerName};
506 if (!displayStats.stats.count(layerKey)) {
507 displayStats.stats[layerKey].displayRefreshRateBucket = refreshRateBucket;
508 displayStats.stats[layerKey].renderRateBucket = renderRateBucket;
509 displayStats.stats[layerKey].uid = uid;
510 displayStats.stats[layerKey].layerName = layerName;
511 }
Ady Abraham8b9e6122021-01-26 19:11:45 -0800512 if (frameRateVote.frameRate > 0.0f) {
513 displayStats.stats[layerKey].setFrameRateVote = frameRateVote;
514 }
Alec Mouri7d436ec2021-01-27 20:40:50 -0800515 TimeStatsHelper::TimeStatsLayer& timeStatsLayer = displayStats.stats[layerKey];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700516 timeStatsLayer.totalFrames++;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700517 timeStatsLayer.droppedFrames += layerRecord.droppedFrames;
Alec Mouri91f6df32020-01-30 08:48:58 -0800518 timeStatsLayer.lateAcquireFrames += layerRecord.lateAcquireFrames;
519 timeStatsLayer.badDesiredPresentFrames += layerRecord.badDesiredPresentFrames;
520
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700521 layerRecord.droppedFrames = 0;
Alec Mouri91f6df32020-01-30 08:48:58 -0800522 layerRecord.lateAcquireFrames = 0;
523 layerRecord.badDesiredPresentFrames = 0;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700524
525 const int32_t postToAcquireMs = msBetween(timeRecords[0].frameTime.postTime,
526 timeRecords[0].frameTime.acquireTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800527 ALOGV("[%d]-[%" PRIu64 "]-post2acquire[%d]", layerId,
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700528 timeRecords[0].frameTime.frameNumber, postToAcquireMs);
529 timeStatsLayer.deltas["post2acquire"].insert(postToAcquireMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700530
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700531 const int32_t postToPresentMs = msBetween(timeRecords[0].frameTime.postTime,
532 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800533 ALOGV("[%d]-[%" PRIu64 "]-post2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700534 timeRecords[0].frameTime.frameNumber, postToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700535 timeStatsLayer.deltas["post2present"].insert(postToPresentMs);
536
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700537 const int32_t acquireToPresentMs = msBetween(timeRecords[0].frameTime.acquireTime,
538 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800539 ALOGV("[%d]-[%" PRIu64 "]-acquire2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700540 timeRecords[0].frameTime.frameNumber, acquireToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700541 timeStatsLayer.deltas["acquire2present"].insert(acquireToPresentMs);
542
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700543 const int32_t latchToPresentMs = msBetween(timeRecords[0].frameTime.latchTime,
544 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800545 ALOGV("[%d]-[%" PRIu64 "]-latch2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700546 timeRecords[0].frameTime.frameNumber, latchToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700547 timeStatsLayer.deltas["latch2present"].insert(latchToPresentMs);
548
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700549 const int32_t desiredToPresentMs = msBetween(timeRecords[0].frameTime.desiredTime,
550 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800551 ALOGV("[%d]-[%" PRIu64 "]-desired2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700552 timeRecords[0].frameTime.frameNumber, desiredToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700553 timeStatsLayer.deltas["desired2present"].insert(desiredToPresentMs);
554
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700555 const int32_t presentToPresentMs = msBetween(prevTimeRecord.frameTime.presentTime,
556 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800557 ALOGV("[%d]-[%" PRIu64 "]-present2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700558 timeRecords[0].frameTime.frameNumber, presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700559 timeStatsLayer.deltas["present2present"].insert(presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700560 }
561 prevTimeRecord = timeRecords[0];
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700562 timeRecords.pop_front();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700563 layerRecord.waitData--;
564 }
565}
566
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700567static constexpr const char* kPopupWindowPrefix = "PopupWindow";
568static const size_t kMinLenLayerName = std::strlen(kPopupWindowPrefix);
Yiwei Zhangbd408322018-10-15 18:31:53 -0700569
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700570// Avoid tracking the "PopupWindow:<random hash>#<number>" layers
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700571static bool layerNameIsValid(const std::string& layerName) {
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700572 return layerName.length() >= kMinLenLayerName &&
573 layerName.compare(0, kMinLenLayerName, kPopupWindowPrefix) != 0;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700574}
575
Alec Mouri9a29e672020-09-14 12:39:14 -0700576bool TimeStats::canAddNewAggregatedStats(uid_t uid, const std::string& layerName) {
Alec Mouri7d436ec2021-01-27 20:40:50 -0800577 uint32_t layerRecords = 0;
578 for (const auto& record : mTimeStats.stats) {
579 if (record.second.stats.count({uid, layerName}) > 0) {
580 return true;
581 }
582
583 layerRecords += record.second.stats.size();
584 }
585
586 return mTimeStats.stats.size() < MAX_NUM_LAYER_STATS;
Alec Mouri9a29e672020-09-14 12:39:14 -0700587}
588
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800589void TimeStats::setPostTime(int32_t layerId, uint64_t frameNumber, const std::string& layerName,
Alec Mouri9a29e672020-09-14 12:39:14 -0700590 uid_t uid, nsecs_t postTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700591 if (!mEnabled.load()) return;
592
593 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800594 ALOGV("[%d]-[%" PRIu64 "]-[%s]-PostTime[%" PRId64 "]", layerId, frameNumber, layerName.c_str(),
Yiwei Zhang8e8fe522018-11-02 18:34:07 -0700595 postTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700596
597 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri9a29e672020-09-14 12:39:14 -0700598 if (!canAddNewAggregatedStats(uid, layerName)) {
Yiwei Zhange926ab52019-08-14 15:16:00 -0700599 return;
600 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800601 if (!mTimeStatsTracker.count(layerId) && mTimeStatsTracker.size() < MAX_NUM_LAYER_RECORDS &&
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700602 layerNameIsValid(layerName)) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700603 mTimeStatsTracker[layerId].uid = uid;
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800604 mTimeStatsTracker[layerId].layerName = layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700605 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800606 if (!mTimeStatsTracker.count(layerId)) return;
607 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700608 if (layerRecord.timeRecords.size() == MAX_NUM_TIME_RECORDS) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800609 ALOGE("[%d]-[%s]-timeRecords is at its maximum size[%zu]. Ignore this when unittesting.",
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800610 layerId, layerRecord.layerName.c_str(), MAX_NUM_TIME_RECORDS);
611 mTimeStatsTracker.erase(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700612 return;
613 }
614 // For most media content, the acquireFence is invalid because the buffer is
615 // ready at the queueBuffer stage. In this case, acquireTime should be given
616 // a default value as postTime.
617 TimeRecord timeRecord = {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700618 .frameTime =
619 {
620 .frameNumber = frameNumber,
621 .postTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800622 .latchTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700623 .acquireTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800624 .desiredTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700625 },
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700626 };
627 layerRecord.timeRecords.push_back(timeRecord);
628 if (layerRecord.waitData < 0 ||
629 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
630 layerRecord.waitData = layerRecord.timeRecords.size() - 1;
631}
632
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800633void TimeStats::setLatchTime(int32_t layerId, uint64_t frameNumber, nsecs_t latchTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700634 if (!mEnabled.load()) return;
635
636 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800637 ALOGV("[%d]-[%" PRIu64 "]-LatchTime[%" PRId64 "]", layerId, frameNumber, latchTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700638
639 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800640 if (!mTimeStatsTracker.count(layerId)) return;
641 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700642 if (layerRecord.waitData < 0 ||
643 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
644 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700645 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700646 if (timeRecord.frameTime.frameNumber == frameNumber) {
647 timeRecord.frameTime.latchTime = latchTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700648 }
649}
650
Alec Mouri91f6df32020-01-30 08:48:58 -0800651void TimeStats::incrementLatchSkipped(int32_t layerId, LatchSkipReason reason) {
652 if (!mEnabled.load()) return;
653
654 ATRACE_CALL();
655 ALOGV("[%d]-LatchSkipped-Reason[%d]", layerId,
656 static_cast<std::underlying_type<LatchSkipReason>::type>(reason));
657
658 std::lock_guard<std::mutex> lock(mMutex);
659 if (!mTimeStatsTracker.count(layerId)) return;
660 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
661
662 switch (reason) {
663 case LatchSkipReason::LateAcquire:
664 layerRecord.lateAcquireFrames++;
665 break;
666 }
667}
668
669void TimeStats::incrementBadDesiredPresent(int32_t layerId) {
670 if (!mEnabled.load()) return;
671
672 ATRACE_CALL();
673 ALOGV("[%d]-BadDesiredPresent", layerId);
674
675 std::lock_guard<std::mutex> lock(mMutex);
676 if (!mTimeStatsTracker.count(layerId)) return;
677 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
678 layerRecord.badDesiredPresentFrames++;
679}
680
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800681void TimeStats::setDesiredTime(int32_t layerId, uint64_t frameNumber, nsecs_t desiredTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700682 if (!mEnabled.load()) return;
683
684 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800685 ALOGV("[%d]-[%" PRIu64 "]-DesiredTime[%" PRId64 "]", layerId, frameNumber, desiredTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700686
687 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800688 if (!mTimeStatsTracker.count(layerId)) return;
689 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700690 if (layerRecord.waitData < 0 ||
691 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
692 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700693 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700694 if (timeRecord.frameTime.frameNumber == frameNumber) {
695 timeRecord.frameTime.desiredTime = desiredTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700696 }
697}
698
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800699void TimeStats::setAcquireTime(int32_t layerId, uint64_t frameNumber, nsecs_t acquireTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700700 if (!mEnabled.load()) return;
701
702 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800703 ALOGV("[%d]-[%" PRIu64 "]-AcquireTime[%" PRId64 "]", layerId, frameNumber, acquireTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700704
705 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800706 if (!mTimeStatsTracker.count(layerId)) return;
707 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700708 if (layerRecord.waitData < 0 ||
709 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
710 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700711 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700712 if (timeRecord.frameTime.frameNumber == frameNumber) {
713 timeRecord.frameTime.acquireTime = acquireTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700714 }
715}
716
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800717void TimeStats::setAcquireFence(int32_t layerId, uint64_t frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700718 const std::shared_ptr<FenceTime>& acquireFence) {
719 if (!mEnabled.load()) return;
720
721 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800722 ALOGV("[%d]-[%" PRIu64 "]-AcquireFenceTime[%" PRId64 "]", layerId, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700723 acquireFence->getSignalTime());
724
725 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800726 if (!mTimeStatsTracker.count(layerId)) return;
727 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700728 if (layerRecord.waitData < 0 ||
729 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
730 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700731 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700732 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700733 timeRecord.acquireFence = acquireFence;
734 }
735}
736
Alec Mouri7d436ec2021-01-27 20:40:50 -0800737void TimeStats::setPresentTime(int32_t layerId, uint64_t frameNumber, nsecs_t presentTime,
Ady Abraham8b9e6122021-01-26 19:11:45 -0800738 Fps displayRefreshRate, std::optional<Fps> renderRate,
739 SetFrameRateVote frameRateVote) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700740 if (!mEnabled.load()) return;
741
742 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800743 ALOGV("[%d]-[%" PRIu64 "]-PresentTime[%" PRId64 "]", layerId, frameNumber, presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700744
745 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800746 if (!mTimeStatsTracker.count(layerId)) return;
747 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700748 if (layerRecord.waitData < 0 ||
749 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
750 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700751 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700752 if (timeRecord.frameTime.frameNumber == frameNumber) {
753 timeRecord.frameTime.presentTime = presentTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700754 timeRecord.ready = true;
755 layerRecord.waitData++;
756 }
757
Ady Abraham8b9e6122021-01-26 19:11:45 -0800758 flushAvailableRecordsToStatsLocked(layerId, displayRefreshRate, renderRate, frameRateVote);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700759}
760
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800761void TimeStats::setPresentFence(int32_t layerId, uint64_t frameNumber,
Alec Mouri7d436ec2021-01-27 20:40:50 -0800762 const std::shared_ptr<FenceTime>& presentFence,
Ady Abraham8b9e6122021-01-26 19:11:45 -0800763 Fps displayRefreshRate, std::optional<Fps> renderRate,
764 SetFrameRateVote frameRateVote) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700765 if (!mEnabled.load()) return;
766
767 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800768 ALOGV("[%d]-[%" PRIu64 "]-PresentFenceTime[%" PRId64 "]", layerId, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700769 presentFence->getSignalTime());
770
771 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800772 if (!mTimeStatsTracker.count(layerId)) return;
773 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700774 if (layerRecord.waitData < 0 ||
775 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
776 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700777 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700778 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700779 timeRecord.presentFence = presentFence;
780 timeRecord.ready = true;
781 layerRecord.waitData++;
782 }
783
Ady Abraham8b9e6122021-01-26 19:11:45 -0800784 flushAvailableRecordsToStatsLocked(layerId, displayRefreshRate, renderRate, frameRateVote);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700785}
786
Adithya Srinivasanead17162021-02-18 02:17:37 +0000787static const constexpr int32_t kValidJankyReason = JankType::DisplayHAL |
788 JankType::SurfaceFlingerCpuDeadlineMissed | JankType::SurfaceFlingerGpuDeadlineMissed |
789 JankType::AppDeadlineMissed | JankType::PredictionError |
790 JankType::SurfaceFlingerScheduling | JankType::BufferStuffing;
Alec Mouri363faf02021-01-29 16:34:55 -0800791
Alec Mouri9a29e672020-09-14 12:39:14 -0700792template <class T>
793static void updateJankPayload(T& t, int32_t reasons) {
794 t.jankPayload.totalFrames++;
795
Alec Mouri9a29e672020-09-14 12:39:14 -0700796 if (reasons & kValidJankyReason) {
797 t.jankPayload.totalJankyFrames++;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800798 if ((reasons & JankType::SurfaceFlingerCpuDeadlineMissed) != 0) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700799 t.jankPayload.totalSFLongCpu++;
800 }
Jorim Jaggi5814ab82020-12-03 20:45:58 +0100801 if ((reasons & JankType::SurfaceFlingerGpuDeadlineMissed) != 0) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700802 t.jankPayload.totalSFLongGpu++;
803 }
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800804 if ((reasons & JankType::DisplayHAL) != 0) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700805 t.jankPayload.totalSFUnattributed++;
806 }
Jorim Jaggi5814ab82020-12-03 20:45:58 +0100807 if ((reasons & JankType::AppDeadlineMissed) != 0) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700808 t.jankPayload.totalAppUnattributed++;
809 }
Adithya Srinivasanead17162021-02-18 02:17:37 +0000810 if ((reasons & JankType::PredictionError) != 0) {
811 t.jankPayload.totalSFPredictionError++;
812 }
813 if ((reasons & JankType::SurfaceFlingerScheduling) != 0) {
814 t.jankPayload.totalSFScheduling++;
815 }
816 if ((reasons & JankType::BufferStuffing) != 0) {
817 t.jankPayload.totalAppBufferStuffing++;
818 }
Alec Mouri9a29e672020-09-14 12:39:14 -0700819 }
820}
821
Alec Mouri363faf02021-01-29 16:34:55 -0800822void TimeStats::incrementJankyFrames(const JankyFramesInfo& info) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700823 if (!mEnabled.load()) return;
824
825 ATRACE_CALL();
826 std::lock_guard<std::mutex> lock(mMutex);
827
Alec Mouri542de112020-11-13 12:07:32 -0800828 // Only update layer stats if we're already tracking the layer in TimeStats.
829 // Otherwise, continue tracking the statistic but use a default layer name instead.
Alec Mouri9a29e672020-09-14 12:39:14 -0700830 // As an implementation detail, we do this because this method is expected to be
Alec Mouri542de112020-11-13 12:07:32 -0800831 // called from FrameTimeline, whose jank classification includes transaction jank
832 // that occurs without a buffer. But, in general those layer names are not suitable as
833 // aggregation keys: e.g., it's normal and expected for Window Manager to include the hash code
834 // for an animation leash. So while we can show that jank in dumpsys, aggregating based on the
835 // layer blows up the stats size, so as a workaround drop those stats. This assumes that
836 // TimeStats will flush the first present fence for a layer *before* FrameTimeline does so that
837 // the first jank record is not dropped.
Alec Mouri9a29e672020-09-14 12:39:14 -0700838
Alec Mouri542de112020-11-13 12:07:32 -0800839 static const std::string kDefaultLayerName = "none";
Alec Mouri7d436ec2021-01-27 20:40:50 -0800840
Alec Mouri363faf02021-01-29 16:34:55 -0800841 const int32_t refreshRateBucket =
842 clampToSmallestBucket(info.refreshRate, REFRESH_RATE_BUCKET_WIDTH);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800843 const int32_t renderRateBucket =
Alec Mouri363faf02021-01-29 16:34:55 -0800844 clampToSmallestBucket(info.renderRate ? *info.renderRate : info.refreshRate,
845 RENDER_RATE_BUCKET_WIDTH);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800846 const TimeStatsHelper::TimelineStatsKey timelineKey = {refreshRateBucket, renderRateBucket};
847
848 if (!mTimeStats.stats.count(timelineKey)) {
849 mTimeStats.stats[timelineKey].key = timelineKey;
Alec Mouri9a29e672020-09-14 12:39:14 -0700850 }
851
Alec Mouri7d436ec2021-01-27 20:40:50 -0800852 TimeStatsHelper::TimelineStats& timelineStats = mTimeStats.stats[timelineKey];
853
Alec Mouri363faf02021-01-29 16:34:55 -0800854 updateJankPayload<TimeStatsHelper::TimelineStats>(timelineStats, info.reasons);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800855
Alec Mouri363faf02021-01-29 16:34:55 -0800856 TimeStatsHelper::LayerStatsKey layerKey = {info.uid, info.layerName};
Alec Mouri7d436ec2021-01-27 20:40:50 -0800857 if (!timelineStats.stats.count(layerKey)) {
Alec Mouri363faf02021-01-29 16:34:55 -0800858 layerKey = {info.uid, kDefaultLayerName};
Alec Mouri7d436ec2021-01-27 20:40:50 -0800859 timelineStats.stats[layerKey].displayRefreshRateBucket = refreshRateBucket;
860 timelineStats.stats[layerKey].renderRateBucket = renderRateBucket;
Alec Mouri363faf02021-01-29 16:34:55 -0800861 timelineStats.stats[layerKey].uid = info.uid;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800862 timelineStats.stats[layerKey].layerName = kDefaultLayerName;
863 }
864
865 TimeStatsHelper::TimeStatsLayer& timeStatsLayer = timelineStats.stats[layerKey];
Alec Mouri363faf02021-01-29 16:34:55 -0800866 updateJankPayload<TimeStatsHelper::TimeStatsLayer>(timeStatsLayer, info.reasons);
867
868 if (info.reasons & kValidJankyReason) {
869 // TimeStats Histograms only retain positive values, so we don't need to check if these
870 // deadlines were really missed if we know that the frame had jank, since deadlines
871 // that were met will be dropped.
872 timelineStats.displayDeadlineDeltas.insert(static_cast<int32_t>(info.displayDeadlineDelta));
873 timelineStats.displayPresentDeltas.insert(static_cast<int32_t>(info.displayPresentJitter));
874 timeStatsLayer.deltas["appDeadlineDeltas"].insert(
875 static_cast<int32_t>(info.appDeadlineDelta));
876 }
Alec Mouri9a29e672020-09-14 12:39:14 -0700877}
878
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800879void TimeStats::onDestroy(int32_t layerId) {
Yiwei Zhangdc224042018-10-18 15:34:00 -0700880 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800881 ALOGV("[%d]-onDestroy", layerId);
Mikael Pessa90092f42019-08-26 17:22:04 -0700882 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800883 mTimeStatsTracker.erase(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700884}
885
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800886void TimeStats::removeTimeRecord(int32_t layerId, uint64_t frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700887 if (!mEnabled.load()) return;
888
889 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800890 ALOGV("[%d]-[%" PRIu64 "]-removeTimeRecord", layerId, frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700891
892 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800893 if (!mTimeStatsTracker.count(layerId)) return;
894 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700895 size_t removeAt = 0;
896 for (const TimeRecord& record : layerRecord.timeRecords) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700897 if (record.frameTime.frameNumber == frameNumber) break;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700898 removeAt++;
899 }
900 if (removeAt == layerRecord.timeRecords.size()) return;
901 layerRecord.timeRecords.erase(layerRecord.timeRecords.begin() + removeAt);
902 if (layerRecord.waitData > static_cast<int32_t>(removeAt)) {
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700903 layerRecord.waitData--;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700904 }
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700905 layerRecord.droppedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700906}
907
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700908void TimeStats::flushPowerTimeLocked() {
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700909 if (!mEnabled.load()) return;
910
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700911 nsecs_t curTime = systemTime();
912 // elapsedTime is in milliseconds.
913 int64_t elapsedTime = (curTime - mPowerTime.prevTime) / 1000000;
914
915 switch (mPowerTime.powerMode) {
Peiyong Lin65248e02020-04-18 21:15:07 -0700916 case PowerMode::ON:
Alec Mouri7d436ec2021-01-27 20:40:50 -0800917 mTimeStats.displayOnTimeLegacy += elapsedTime;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700918 break;
Peiyong Lin65248e02020-04-18 21:15:07 -0700919 case PowerMode::OFF:
920 case PowerMode::DOZE:
921 case PowerMode::DOZE_SUSPEND:
922 case PowerMode::ON_SUSPEND:
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700923 default:
924 break;
925 }
926
927 mPowerTime.prevTime = curTime;
928}
929
Peiyong Lin65248e02020-04-18 21:15:07 -0700930void TimeStats::setPowerMode(PowerMode powerMode) {
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700931 if (!mEnabled.load()) {
932 std::lock_guard<std::mutex> lock(mMutex);
933 mPowerTime.powerMode = powerMode;
934 return;
935 }
936
937 std::lock_guard<std::mutex> lock(mMutex);
938 if (powerMode == mPowerTime.powerMode) return;
939
940 flushPowerTimeLocked();
941 mPowerTime.powerMode = powerMode;
942}
943
Alec Mourifb571ea2019-01-24 18:42:10 -0800944void TimeStats::recordRefreshRate(uint32_t fps, nsecs_t duration) {
945 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800946 if (mTimeStats.refreshRateStatsLegacy.count(fps)) {
947 mTimeStats.refreshRateStatsLegacy[fps] += duration;
Alec Mourifb571ea2019-01-24 18:42:10 -0800948 } else {
Alec Mouri7d436ec2021-01-27 20:40:50 -0800949 mTimeStats.refreshRateStatsLegacy.insert({fps, duration});
Alec Mourifb571ea2019-01-24 18:42:10 -0800950 }
951}
952
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700953void TimeStats::flushAvailableGlobalRecordsToStatsLocked() {
954 ATRACE_CALL();
955
956 while (!mGlobalRecord.presentFences.empty()) {
957 const nsecs_t curPresentTime = mGlobalRecord.presentFences.front()->getSignalTime();
958 if (curPresentTime == Fence::SIGNAL_TIME_PENDING) break;
959
960 if (curPresentTime == Fence::SIGNAL_TIME_INVALID) {
961 ALOGE("GlobalPresentFence is invalid!");
962 mGlobalRecord.prevPresentTime = 0;
963 mGlobalRecord.presentFences.pop_front();
964 continue;
965 }
966
967 ALOGV("GlobalPresentFenceTime[%" PRId64 "]",
968 mGlobalRecord.presentFences.front()->getSignalTime());
969
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700970 if (mGlobalRecord.prevPresentTime != 0) {
971 const int32_t presentToPresentMs =
972 msBetween(mGlobalRecord.prevPresentTime, curPresentTime);
973 ALOGV("Global present2present[%d] prev[%" PRId64 "] curr[%" PRId64 "]",
974 presentToPresentMs, mGlobalRecord.prevPresentTime, curPresentTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800975 mTimeStats.presentToPresentLegacy.insert(presentToPresentMs);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700976 }
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700977
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700978 mGlobalRecord.prevPresentTime = curPresentTime;
979 mGlobalRecord.presentFences.pop_front();
980 }
Alec Mourie4034bb2019-11-19 12:45:54 -0800981 while (!mGlobalRecord.renderEngineDurations.empty()) {
982 const auto duration = mGlobalRecord.renderEngineDurations.front();
983 const auto& endTime = duration.endTime;
984
985 nsecs_t endNs = -1;
986
987 if (auto val = std::get_if<nsecs_t>(&endTime)) {
988 endNs = *val;
989 } else {
990 endNs = std::get<std::shared_ptr<FenceTime>>(endTime)->getSignalTime();
991 }
992
993 if (endNs == Fence::SIGNAL_TIME_PENDING) break;
994
995 if (endNs < 0) {
996 ALOGE("RenderEngineTiming is invalid!");
997 mGlobalRecord.renderEngineDurations.pop_front();
998 continue;
999 }
1000
1001 const int32_t renderEngineMs = msBetween(duration.startTime, endNs);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001002 mTimeStats.renderEngineTimingLegacy.insert(renderEngineMs);
Alec Mourie4034bb2019-11-19 12:45:54 -08001003
1004 mGlobalRecord.renderEngineDurations.pop_front();
1005 }
Yiwei Zhangce6ebc02018-10-20 12:42:38 -07001006}
1007
1008void TimeStats::setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) {
1009 if (!mEnabled.load()) return;
1010
1011 ATRACE_CALL();
1012 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -07001013 if (presentFence == nullptr || !presentFence->isValid()) {
1014 mGlobalRecord.prevPresentTime = 0;
1015 return;
1016 }
1017
Peiyong Lin65248e02020-04-18 21:15:07 -07001018 if (mPowerTime.powerMode != PowerMode::ON) {
1019 // Try flushing the last present fence on PowerMode::ON.
Yiwei Zhange5c49d52018-10-29 00:15:31 -07001020 flushAvailableGlobalRecordsToStatsLocked();
1021 mGlobalRecord.presentFences.clear();
Yiwei Zhangce6ebc02018-10-20 12:42:38 -07001022 mGlobalRecord.prevPresentTime = 0;
1023 return;
1024 }
1025
1026 if (mGlobalRecord.presentFences.size() == MAX_NUM_TIME_RECORDS) {
1027 // The front presentFence must be trapped in pending status in this
1028 // case. Try dequeuing the front one to recover.
1029 ALOGE("GlobalPresentFences is already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
1030 mGlobalRecord.prevPresentTime = 0;
1031 mGlobalRecord.presentFences.pop_front();
1032 }
1033
1034 mGlobalRecord.presentFences.emplace_back(presentFence);
1035 flushAvailableGlobalRecordsToStatsLocked();
1036}
1037
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001038void TimeStats::enable() {
1039 if (mEnabled.load()) return;
1040
1041 ATRACE_CALL();
1042
1043 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001044 mEnabled.store(true);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001045 mTimeStats.statsStartLegacy = static_cast<int64_t>(std::time(0));
Yiwei Zhang3a226d22018-10-16 09:23:03 -07001046 mPowerTime.prevTime = systemTime();
Yiwei Zhange5c49d52018-10-29 00:15:31 -07001047 ALOGD("Enabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001048}
1049
1050void TimeStats::disable() {
1051 if (!mEnabled.load()) return;
1052
1053 ATRACE_CALL();
1054
1055 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -07001056 flushPowerTimeLocked();
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001057 mEnabled.store(false);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001058 mTimeStats.statsEndLegacy = static_cast<int64_t>(std::time(0));
Yiwei Zhange5c49d52018-10-29 00:15:31 -07001059 ALOGD("Disabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001060}
1061
Alec Mouri8e2f31b2020-01-16 22:04:35 +00001062void TimeStats::clearAll() {
1063 std::lock_guard<std::mutex> lock(mMutex);
1064 clearGlobalLocked();
1065 clearLayersLocked();
1066}
1067
1068void TimeStats::clearGlobalLocked() {
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001069 ATRACE_CALL();
1070
Alec Mouri7d436ec2021-01-27 20:40:50 -08001071 mTimeStats.statsStartLegacy = (mEnabled.load() ? static_cast<int64_t>(std::time(0)) : 0);
1072 mTimeStats.statsEndLegacy = 0;
1073 mTimeStats.totalFramesLegacy = 0;
1074 mTimeStats.missedFramesLegacy = 0;
1075 mTimeStats.clientCompositionFramesLegacy = 0;
1076 mTimeStats.clientCompositionReusedFramesLegacy = 0;
1077 mTimeStats.refreshRateSwitchesLegacy = 0;
1078 mTimeStats.compositionStrategyChangesLegacy = 0;
1079 mTimeStats.displayEventConnectionsCountLegacy = 0;
1080 mTimeStats.displayOnTimeLegacy = 0;
1081 mTimeStats.presentToPresentLegacy.hist.clear();
1082 mTimeStats.frameDurationLegacy.hist.clear();
1083 mTimeStats.renderEngineTimingLegacy.hist.clear();
1084 mTimeStats.refreshRateStatsLegacy.clear();
Yiwei Zhang3a226d22018-10-16 09:23:03 -07001085 mPowerTime.prevTime = systemTime();
Alec Mouri56e63852021-03-09 18:17:25 -08001086 for (auto& globalRecord : mTimeStats.stats) {
1087 globalRecord.second.clearGlobals();
1088 }
Yiwei Zhange5c49d52018-10-29 00:15:31 -07001089 mGlobalRecord.prevPresentTime = 0;
1090 mGlobalRecord.presentFences.clear();
Alec Mouri8e2f31b2020-01-16 22:04:35 +00001091 ALOGD("Cleared global stats");
1092}
1093
1094void TimeStats::clearLayersLocked() {
1095 ATRACE_CALL();
1096
1097 mTimeStatsTracker.clear();
Alec Mouri56e63852021-03-09 18:17:25 -08001098
1099 for (auto& globalRecord : mTimeStats.stats) {
1100 globalRecord.second.stats.clear();
1101 }
Alec Mouri8e2f31b2020-01-16 22:04:35 +00001102 ALOGD("Cleared layer stats");
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001103}
1104
1105bool TimeStats::isEnabled() {
1106 return mEnabled.load();
1107}
1108
Yiwei Zhang5434a782018-12-05 18:06:32 -08001109void TimeStats::dump(bool asProto, std::optional<uint32_t> maxLayers, std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001110 ATRACE_CALL();
1111
1112 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001113 if (mTimeStats.statsStartLegacy == 0) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001114 return;
1115 }
1116
Alec Mouri7d436ec2021-01-27 20:40:50 -08001117 mTimeStats.statsEndLegacy = static_cast<int64_t>(std::time(0));
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001118
Yiwei Zhang3a226d22018-10-16 09:23:03 -07001119 flushPowerTimeLocked();
1120
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001121 if (asProto) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -07001122 ALOGD("Dumping TimeStats as proto");
Yiwei Zhangdc224042018-10-18 15:34:00 -07001123 SFTimeStatsGlobalProto timeStatsProto = mTimeStats.toProto(maxLayers);
Dominik Laskowski46470112019-08-02 13:13:11 -07001124 result.append(timeStatsProto.SerializeAsString());
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001125 } else {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -07001126 ALOGD("Dumping TimeStats as text");
Yiwei Zhang5434a782018-12-05 18:06:32 -08001127 result.append(mTimeStats.toString(maxLayers));
Yiwei Zhang8a4015c2018-05-08 16:03:47 -07001128 result.append("\n");
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001129 }
1130}
1131
Alec Mourifb571ea2019-01-24 18:42:10 -08001132} // namespace impl
1133
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001134} // namespace android