blob: b93f30ebbf6e97206a30683aa910d4b1565020bf [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
92std::string frameRateVoteToProtoByteString(float refreshRate, int frameRateCompatibility,
93 int seamlessness) {
94 util::ProtoOutputStream proto;
95 proto.write(android::util::FIELD_TYPE_FLOAT | 1 /* field id */, refreshRate);
96 proto.write(android::util::FIELD_TYPE_ENUM | 2 /* field id */, frameRateCompatibility);
97 proto.write(android::util::FIELD_TYPE_ENUM | 3 /* field id */, seamlessness);
98
99 std::string byteString;
100 proto.serializeToString(&byteString);
101 return byteString;
102}
Alec Mouri37384342020-01-02 17:23:37 -0800103} // namespace
104
Alec Mouridfad9002020-02-12 17:49:09 -0800105AStatsManager_PullAtomCallbackReturn TimeStats::populateGlobalAtom(AStatsEventList* data) {
106 std::lock_guard<std::mutex> lock(mMutex);
107
Alec Mouri7d436ec2021-01-27 20:40:50 -0800108 if (mTimeStats.statsStartLegacy == 0) {
Alec Mouridfad9002020-02-12 17:49:09 -0800109 return AStatsManager_PULL_SKIP;
110 }
111 flushPowerTimeLocked();
112
Alec Mouri7d436ec2021-01-27 20:40:50 -0800113 for (const auto& globalSlice : mTimeStats.stats) {
114 AStatsEvent* event = mStatsDelegate->addStatsEventToPullData(data);
115 mStatsDelegate->statsEventSetAtomId(event, android::util::SURFACEFLINGER_STATS_GLOBAL_INFO);
116 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.totalFramesLegacy);
117 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.missedFramesLegacy);
118 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.clientCompositionFramesLegacy);
119 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.displayOnTimeLegacy);
120 mStatsDelegate->statsEventWriteInt64(event, mTimeStats.presentToPresentLegacy.totalTime());
121 mStatsDelegate->statsEventWriteInt32(event, mTimeStats.displayEventConnectionsCountLegacy);
122 std::string frameDurationBytes =
123 histogramToProtoByteString(mTimeStats.frameDurationLegacy.hist,
124 mMaxPulledHistogramBuckets);
125 mStatsDelegate->statsEventWriteByteArray(event, (const uint8_t*)frameDurationBytes.c_str(),
126 frameDurationBytes.size());
127 std::string renderEngineTimingBytes =
128 histogramToProtoByteString(mTimeStats.renderEngineTimingLegacy.hist,
129 mMaxPulledHistogramBuckets);
130 mStatsDelegate->statsEventWriteByteArray(event,
131 (const uint8_t*)renderEngineTimingBytes.c_str(),
132 renderEngineTimingBytes.size());
Alec Mouri9a29e672020-09-14 12:39:14 -0700133
Alec Mouri7d436ec2021-01-27 20:40:50 -0800134 mStatsDelegate->statsEventWriteInt32(event, globalSlice.second.jankPayload.totalFrames);
135 mStatsDelegate->statsEventWriteInt32(event,
136 globalSlice.second.jankPayload.totalJankyFrames);
137 mStatsDelegate->statsEventWriteInt32(event, globalSlice.second.jankPayload.totalSFLongCpu);
138 mStatsDelegate->statsEventWriteInt32(event, globalSlice.second.jankPayload.totalSFLongGpu);
139 mStatsDelegate->statsEventWriteInt32(event,
140 globalSlice.second.jankPayload.totalSFUnattributed);
141 mStatsDelegate->statsEventWriteInt32(event,
142 globalSlice.second.jankPayload.totalAppUnattributed);
Alec Mouri75de8f22021-01-20 14:53:44 -0800143
Alec Mouri7d436ec2021-01-27 20:40:50 -0800144 // TODO: populate these with real values
145 mStatsDelegate->statsEventWriteInt32(event, 0); // total_janky_frames_sf_scheduling
146 mStatsDelegate->statsEventWriteInt32(event, 0); // total_jank_frames_sf_prediction_error
147 mStatsDelegate->statsEventWriteInt32(event, 0); // total_jank_frames_app_buffer_stuffing
148 mStatsDelegate->statsEventWriteInt32(event, globalSlice.first.displayRefreshRateBucket);
149 std::string sfDeadlineMissedBytes =
Alec Mouri363faf02021-01-29 16:34:55 -0800150 histogramToProtoByteString(globalSlice.second.displayDeadlineDeltas.hist,
Alec Mouri7d436ec2021-01-27 20:40:50 -0800151 mMaxPulledHistogramBuckets);
Alec Mouri363faf02021-01-29 16:34:55 -0800152 mStatsDelegate->statsEventWriteByteArray(event,
153 (const uint8_t*)sfDeadlineMissedBytes.c_str(),
154 sfDeadlineMissedBytes.size());
Alec Mouri7d436ec2021-01-27 20:40:50 -0800155 std::string sfPredictionErrorBytes =
Alec Mouri363faf02021-01-29 16:34:55 -0800156 histogramToProtoByteString(globalSlice.second.displayPresentDeltas.hist,
Alec Mouri7d436ec2021-01-27 20:40:50 -0800157 mMaxPulledHistogramBuckets);
Alec Mouri363faf02021-01-29 16:34:55 -0800158 mStatsDelegate->statsEventWriteByteArray(event,
159 (const uint8_t*)sfPredictionErrorBytes.c_str(),
160 sfPredictionErrorBytes.size());
Alec Mouri7d436ec2021-01-27 20:40:50 -0800161 mStatsDelegate->statsEventWriteInt32(event, globalSlice.first.renderRateBucket);
162 mStatsDelegate->statsEventBuild(event);
163 }
164
Alec Mouridfad9002020-02-12 17:49:09 -0800165 clearGlobalLocked();
166
167 return AStatsManager_PULL_SUCCESS;
168}
169
Tej Singh2a457b62020-01-31 16:16:10 -0800170AStatsManager_PullAtomCallbackReturn TimeStats::populateLayerAtom(AStatsEventList* data) {
Alec Mouri37384342020-01-02 17:23:37 -0800171 std::lock_guard<std::mutex> lock(mMutex);
172
Alec Mouri363faf02021-01-29 16:34:55 -0800173 std::vector<TimeStatsHelper::TimeStatsLayer*> dumpStats;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800174 uint32_t numLayers = 0;
175 for (const auto& globalSlice : mTimeStats.stats) {
176 numLayers += globalSlice.second.stats.size();
177 }
178
179 dumpStats.reserve(numLayers);
180
Alec Mouri363faf02021-01-29 16:34:55 -0800181 for (auto& globalSlice : mTimeStats.stats) {
182 for (auto& layerSlice : globalSlice.second.stats) {
Alec Mouri7d436ec2021-01-27 20:40:50 -0800183 dumpStats.push_back(&layerSlice.second);
184 }
Alec Mouri37384342020-01-02 17:23:37 -0800185 }
186
187 std::sort(dumpStats.begin(), dumpStats.end(),
188 [](TimeStatsHelper::TimeStatsLayer const* l,
189 TimeStatsHelper::TimeStatsLayer const* r) {
190 return l->totalFrames > r->totalFrames;
191 });
192
193 if (mMaxPulledLayers < dumpStats.size()) {
194 dumpStats.resize(mMaxPulledLayers);
195 }
196
Alec Mouri363faf02021-01-29 16:34:55 -0800197 for (auto& layer : dumpStats) {
Tej Singh2a457b62020-01-31 16:16:10 -0800198 AStatsEvent* event = mStatsDelegate->addStatsEventToPullData(data);
Alec Mouri37384342020-01-02 17:23:37 -0800199 mStatsDelegate->statsEventSetAtomId(event, android::util::SURFACEFLINGER_STATS_LAYER_INFO);
200 mStatsDelegate->statsEventWriteString8(event, layer->layerName.c_str());
201 mStatsDelegate->statsEventWriteInt64(event, layer->totalFrames);
202 mStatsDelegate->statsEventWriteInt64(event, layer->droppedFrames);
203
204 for (const auto& name : kHistogramNames) {
205 const auto& histogram = layer->deltas.find(name);
206 if (histogram == layer->deltas.cend()) {
207 mStatsDelegate->statsEventWriteByteArray(event, nullptr, 0);
208 } else {
209 std::string bytes = histogramToProtoByteString(histogram->second.hist,
210 mMaxPulledHistogramBuckets);
211 mStatsDelegate->statsEventWriteByteArray(event, (const uint8_t*)bytes.c_str(),
212 bytes.size());
213 }
214 }
215
Yiwei Zhang7bfc75b2020-02-10 11:20:34 -0800216 mStatsDelegate->statsEventWriteInt64(event, layer->lateAcquireFrames);
217 mStatsDelegate->statsEventWriteInt64(event, layer->badDesiredPresentFrames);
Alec Mouri9a29e672020-09-14 12:39:14 -0700218 mStatsDelegate->statsEventWriteInt32(event, layer->uid);
219 mStatsDelegate->statsEventWriteInt32(event, layer->jankPayload.totalFrames);
220 mStatsDelegate->statsEventWriteInt32(event, layer->jankPayload.totalJankyFrames);
221 mStatsDelegate->statsEventWriteInt32(event, layer->jankPayload.totalSFLongCpu);
222 mStatsDelegate->statsEventWriteInt32(event, layer->jankPayload.totalSFLongGpu);
223 mStatsDelegate->statsEventWriteInt32(event, layer->jankPayload.totalSFUnattributed);
224 mStatsDelegate->statsEventWriteInt32(event, layer->jankPayload.totalAppUnattributed);
Yiwei Zhang7bfc75b2020-02-10 11:20:34 -0800225
Alec Mouri75de8f22021-01-20 14:53:44 -0800226 // TODO: populate these with real values
227 mStatsDelegate->statsEventWriteInt32(event, 0); // total_janky_frames_sf_scheduling
228 mStatsDelegate->statsEventWriteInt32(event, 0); // total_jank_frames_sf_prediction_error
229 mStatsDelegate->statsEventWriteInt32(event, 0); // total_jank_frames_app_buffer_stuffing
Alec Mouri7d436ec2021-01-27 20:40:50 -0800230 mStatsDelegate->statsEventWriteInt32(
231 event, layer->displayRefreshRateBucket); // display_refresh_rate_bucket
232 mStatsDelegate->statsEventWriteInt32(event, layer->renderRateBucket); // render_rate_bucket
Alec Mouri75de8f22021-01-20 14:53:44 -0800233 std::string frameRateVoteBytes = frameRateVoteToProtoByteString(0.0, 0, 0);
234 mStatsDelegate->statsEventWriteByteArray(event, (const uint8_t*)frameRateVoteBytes.c_str(),
235 frameRateVoteBytes.size()); // set_frame_rate_vote
236 std::string appDeadlineMissedBytes =
Alec Mouri363faf02021-01-29 16:34:55 -0800237 histogramToProtoByteString(layer->deltas["appDeadlineDeltas"].hist,
Alec Mouri75de8f22021-01-20 14:53:44 -0800238 mMaxPulledHistogramBuckets);
Alec Mouri363faf02021-01-29 16:34:55 -0800239 mStatsDelegate->statsEventWriteByteArray(event,
240 (const uint8_t*)appDeadlineMissedBytes.c_str(),
241 appDeadlineMissedBytes.size());
Alec Mouri75de8f22021-01-20 14:53:44 -0800242
Alec Mouri37384342020-01-02 17:23:37 -0800243 mStatsDelegate->statsEventBuild(event);
244 }
245 clearLayersLocked();
246
Tej Singh2a457b62020-01-31 16:16:10 -0800247 return AStatsManager_PULL_SUCCESS;
Alec Mouri37384342020-01-02 17:23:37 -0800248}
249
250TimeStats::TimeStats() : TimeStats(nullptr, std::nullopt, std::nullopt) {}
251
252TimeStats::TimeStats(std::unique_ptr<StatsEventDelegate> statsDelegate,
253 std::optional<size_t> maxPulledLayers,
254 std::optional<size_t> maxPulledHistogramBuckets) {
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000255 if (statsDelegate != nullptr) {
256 mStatsDelegate = std::move(statsDelegate);
257 }
Alec Mouri37384342020-01-02 17:23:37 -0800258
259 if (maxPulledLayers) {
260 mMaxPulledLayers = *maxPulledLayers;
261 }
262
263 if (maxPulledHistogramBuckets) {
264 mMaxPulledHistogramBuckets = *maxPulledHistogramBuckets;
265 }
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000266}
267
Alec Mouri3ecd5cd2020-01-29 12:53:07 -0800268TimeStats::~TimeStats() {
269 std::lock_guard<std::mutex> lock(mMutex);
Tej Singh38a4b212020-03-13 19:04:51 -0700270 mStatsDelegate->clearStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_GLOBAL_INFO);
271 mStatsDelegate->clearStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_LAYER_INFO);
Alec Mouri3ecd5cd2020-01-29 12:53:07 -0800272}
273
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000274void TimeStats::onBootFinished() {
Alec Mouri3ecd5cd2020-01-29 12:53:07 -0800275 std::lock_guard<std::mutex> lock(mMutex);
Tej Singh38a4b212020-03-13 19:04:51 -0700276 mStatsDelegate->setStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_GLOBAL_INFO,
277 nullptr, TimeStats::pullAtomCallback, this);
278 mStatsDelegate->setStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_LAYER_INFO,
279 nullptr, TimeStats::pullAtomCallback, this);
Alec Mourib3885ad2019-09-06 17:08:55 -0700280}
281
Dominik Laskowskic2867142019-01-21 11:33:38 -0800282void TimeStats::parseArgs(bool asProto, const Vector<String16>& args, std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700283 ATRACE_CALL();
284
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700285 std::unordered_map<std::string, int32_t> argsMap;
Dominik Laskowskic2867142019-01-21 11:33:38 -0800286 for (size_t index = 0; index < args.size(); ++index) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700287 argsMap[std::string(String8(args[index]).c_str())] = index;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700288 }
289
290 if (argsMap.count("-disable")) {
291 disable();
292 }
293
294 if (argsMap.count("-dump")) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700295 std::optional<uint32_t> maxLayers = std::nullopt;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700296 auto iter = argsMap.find("-maxlayers");
297 if (iter != argsMap.end() && iter->second + 1 < static_cast<int32_t>(args.size())) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700298 int64_t value = strtol(String8(args[iter->second + 1]).c_str(), nullptr, 10);
299 value = std::clamp(value, int64_t(0), int64_t(UINT32_MAX));
300 maxLayers = static_cast<uint32_t>(value);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700301 }
302
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700303 dump(asProto, maxLayers, result);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700304 }
305
306 if (argsMap.count("-clear")) {
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000307 clearAll();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700308 }
309
310 if (argsMap.count("-enable")) {
311 enable();
312 }
313}
314
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700315std::string TimeStats::miniDump() {
316 ATRACE_CALL();
317
318 std::string result = "TimeStats miniDump:\n";
319 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange926ab52019-08-14 15:16:00 -0700320 android::base::StringAppendF(&result, "Number of layers currently being tracked is %zu\n",
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700321 mTimeStatsTracker.size());
Yiwei Zhange926ab52019-08-14 15:16:00 -0700322 android::base::StringAppendF(&result, "Number of layers in the stats pool is %zu\n",
323 mTimeStats.stats.size());
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700324 return result;
325}
326
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700327void TimeStats::incrementTotalFrames() {
328 if (!mEnabled.load()) return;
329
330 ATRACE_CALL();
331
332 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800333 mTimeStats.totalFramesLegacy++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700334}
335
Yiwei Zhang621f9d42018-05-07 10:40:55 -0700336void TimeStats::incrementMissedFrames() {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700337 if (!mEnabled.load()) return;
338
339 ATRACE_CALL();
340
341 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800342 mTimeStats.missedFramesLegacy++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700343}
344
345void TimeStats::incrementClientCompositionFrames() {
346 if (!mEnabled.load()) return;
347
348 ATRACE_CALL();
349
350 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800351 mTimeStats.clientCompositionFramesLegacy++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700352}
353
Vishnu Nair9b079a22020-01-21 14:36:08 -0800354void TimeStats::incrementClientCompositionReusedFrames() {
355 if (!mEnabled.load()) return;
356
357 ATRACE_CALL();
358
359 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800360 mTimeStats.clientCompositionReusedFramesLegacy++;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800361}
362
Alec Mouri8de697e2020-03-19 10:52:01 -0700363void TimeStats::incrementRefreshRateSwitches() {
364 if (!mEnabled.load()) return;
365
366 ATRACE_CALL();
367
368 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800369 mTimeStats.refreshRateSwitchesLegacy++;
Alec Mouri8de697e2020-03-19 10:52:01 -0700370}
371
Alec Mouri8f7a0102020-04-15 12:11:10 -0700372void TimeStats::incrementCompositionStrategyChanges() {
373 if (!mEnabled.load()) return;
374
375 ATRACE_CALL();
376
377 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800378 mTimeStats.compositionStrategyChangesLegacy++;
Alec Mouri8f7a0102020-04-15 12:11:10 -0700379}
380
Alec Mouri717bcb62020-02-10 17:07:19 -0800381void TimeStats::recordDisplayEventConnectionCount(int32_t count) {
382 if (!mEnabled.load()) return;
383
384 ATRACE_CALL();
385
386 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800387 mTimeStats.displayEventConnectionsCountLegacy =
388 std::max(mTimeStats.displayEventConnectionsCountLegacy, count);
Alec Mouri717bcb62020-02-10 17:07:19 -0800389}
390
Alec Mouri9519bf12019-11-15 16:54:44 -0800391static int32_t msBetween(nsecs_t start, nsecs_t end) {
392 int64_t delta = std::chrono::duration_cast<std::chrono::milliseconds>(
393 std::chrono::nanoseconds(end - start))
394 .count();
395 delta = std::clamp(delta, int64_t(INT32_MIN), int64_t(INT32_MAX));
396 return static_cast<int32_t>(delta);
397}
398
399void TimeStats::recordFrameDuration(nsecs_t startTime, nsecs_t endTime) {
400 if (!mEnabled.load()) return;
401
402 std::lock_guard<std::mutex> lock(mMutex);
Peiyong Lin65248e02020-04-18 21:15:07 -0700403 if (mPowerTime.powerMode == PowerMode::ON) {
Alec Mouri7d436ec2021-01-27 20:40:50 -0800404 mTimeStats.frameDurationLegacy.insert(msBetween(startTime, endTime));
Alec Mouri9519bf12019-11-15 16:54:44 -0800405 }
406}
407
Alec Mourie4034bb2019-11-19 12:45:54 -0800408void TimeStats::recordRenderEngineDuration(nsecs_t startTime, nsecs_t endTime) {
409 if (!mEnabled.load()) return;
410
411 std::lock_guard<std::mutex> lock(mMutex);
412 if (mGlobalRecord.renderEngineDurations.size() == MAX_NUM_TIME_RECORDS) {
413 ALOGE("RenderEngineTimes are already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
414 mGlobalRecord.renderEngineDurations.pop_front();
415 }
416 mGlobalRecord.renderEngineDurations.push_back({startTime, endTime});
417}
418
419void TimeStats::recordRenderEngineDuration(nsecs_t startTime,
420 const std::shared_ptr<FenceTime>& endTime) {
421 if (!mEnabled.load()) return;
422
423 std::lock_guard<std::mutex> lock(mMutex);
424 if (mGlobalRecord.renderEngineDurations.size() == MAX_NUM_TIME_RECORDS) {
425 ALOGE("RenderEngineTimes are already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
426 mGlobalRecord.renderEngineDurations.pop_front();
427 }
428 mGlobalRecord.renderEngineDurations.push_back({startTime, endTime});
429}
430
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800431bool TimeStats::recordReadyLocked(int32_t layerId, TimeRecord* timeRecord) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700432 if (!timeRecord->ready) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800433 ALOGV("[%d]-[%" PRIu64 "]-presentFence is still not received", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700434 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700435 return false;
436 }
437
438 if (timeRecord->acquireFence != nullptr) {
439 if (timeRecord->acquireFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
440 return false;
441 }
442 if (timeRecord->acquireFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700443 timeRecord->frameTime.acquireTime = timeRecord->acquireFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700444 timeRecord->acquireFence = nullptr;
445 } else {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800446 ALOGV("[%d]-[%" PRIu64 "]-acquireFence signal time is invalid", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700447 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700448 }
449 }
450
451 if (timeRecord->presentFence != nullptr) {
452 if (timeRecord->presentFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
453 return false;
454 }
455 if (timeRecord->presentFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700456 timeRecord->frameTime.presentTime = timeRecord->presentFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700457 timeRecord->presentFence = nullptr;
458 } else {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800459 ALOGV("[%d]-[%" PRIu64 "]-presentFence signal time invalid", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700460 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700461 }
462 }
463
464 return true;
465}
466
Alec Mouri7d436ec2021-01-27 20:40:50 -0800467static int32_t clampToSmallestBucket(Fps fps, size_t bucketWidth) {
468 return (fps.getIntValue() / bucketWidth) * bucketWidth;
469}
470
471void TimeStats::flushAvailableRecordsToStatsLocked(int32_t layerId, Fps displayRefreshRate,
472 std::optional<Fps> renderRate) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700473 ATRACE_CALL();
474
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800475 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700476 TimeRecord& prevTimeRecord = layerRecord.prevTimeRecord;
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700477 std::deque<TimeRecord>& timeRecords = layerRecord.timeRecords;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800478 const int32_t refreshRateBucket =
479 clampToSmallestBucket(displayRefreshRate, REFRESH_RATE_BUCKET_WIDTH);
480 const int32_t renderRateBucket =
481 clampToSmallestBucket(renderRate ? *renderRate : displayRefreshRate,
482 RENDER_RATE_BUCKET_WIDTH);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700483 while (!timeRecords.empty()) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800484 if (!recordReadyLocked(layerId, &timeRecords[0])) break;
485 ALOGV("[%d]-[%" PRIu64 "]-presentFenceTime[%" PRId64 "]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700486 timeRecords[0].frameTime.frameNumber, timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700487
488 if (prevTimeRecord.ready) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700489 uid_t uid = layerRecord.uid;
Yiwei Zhangeafa5cc2019-07-26 15:06:25 -0700490 const std::string& layerName = layerRecord.layerName;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800491 TimeStatsHelper::TimelineStatsKey timelineKey = {refreshRateBucket, renderRateBucket};
492 if (!mTimeStats.stats.count(timelineKey)) {
493 mTimeStats.stats[timelineKey].key = timelineKey;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700494 }
Alec Mouri7d436ec2021-01-27 20:40:50 -0800495
496 TimeStatsHelper::TimelineStats& displayStats = mTimeStats.stats[timelineKey];
497
498 TimeStatsHelper::LayerStatsKey layerKey = {uid, layerName};
499 if (!displayStats.stats.count(layerKey)) {
500 displayStats.stats[layerKey].displayRefreshRateBucket = refreshRateBucket;
501 displayStats.stats[layerKey].renderRateBucket = renderRateBucket;
502 displayStats.stats[layerKey].uid = uid;
503 displayStats.stats[layerKey].layerName = layerName;
504 }
505 TimeStatsHelper::TimeStatsLayer& timeStatsLayer = displayStats.stats[layerKey];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700506 timeStatsLayer.totalFrames++;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700507 timeStatsLayer.droppedFrames += layerRecord.droppedFrames;
Alec Mouri91f6df32020-01-30 08:48:58 -0800508 timeStatsLayer.lateAcquireFrames += layerRecord.lateAcquireFrames;
509 timeStatsLayer.badDesiredPresentFrames += layerRecord.badDesiredPresentFrames;
510
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700511 layerRecord.droppedFrames = 0;
Alec Mouri91f6df32020-01-30 08:48:58 -0800512 layerRecord.lateAcquireFrames = 0;
513 layerRecord.badDesiredPresentFrames = 0;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700514
515 const int32_t postToAcquireMs = msBetween(timeRecords[0].frameTime.postTime,
516 timeRecords[0].frameTime.acquireTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800517 ALOGV("[%d]-[%" PRIu64 "]-post2acquire[%d]", layerId,
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700518 timeRecords[0].frameTime.frameNumber, postToAcquireMs);
519 timeStatsLayer.deltas["post2acquire"].insert(postToAcquireMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700520
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700521 const int32_t postToPresentMs = msBetween(timeRecords[0].frameTime.postTime,
522 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800523 ALOGV("[%d]-[%" PRIu64 "]-post2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700524 timeRecords[0].frameTime.frameNumber, postToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700525 timeStatsLayer.deltas["post2present"].insert(postToPresentMs);
526
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700527 const int32_t acquireToPresentMs = msBetween(timeRecords[0].frameTime.acquireTime,
528 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800529 ALOGV("[%d]-[%" PRIu64 "]-acquire2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700530 timeRecords[0].frameTime.frameNumber, acquireToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700531 timeStatsLayer.deltas["acquire2present"].insert(acquireToPresentMs);
532
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700533 const int32_t latchToPresentMs = msBetween(timeRecords[0].frameTime.latchTime,
534 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800535 ALOGV("[%d]-[%" PRIu64 "]-latch2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700536 timeRecords[0].frameTime.frameNumber, latchToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700537 timeStatsLayer.deltas["latch2present"].insert(latchToPresentMs);
538
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700539 const int32_t desiredToPresentMs = msBetween(timeRecords[0].frameTime.desiredTime,
540 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800541 ALOGV("[%d]-[%" PRIu64 "]-desired2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700542 timeRecords[0].frameTime.frameNumber, desiredToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700543 timeStatsLayer.deltas["desired2present"].insert(desiredToPresentMs);
544
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700545 const int32_t presentToPresentMs = msBetween(prevTimeRecord.frameTime.presentTime,
546 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800547 ALOGV("[%d]-[%" PRIu64 "]-present2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700548 timeRecords[0].frameTime.frameNumber, presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700549 timeStatsLayer.deltas["present2present"].insert(presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700550 }
551 prevTimeRecord = timeRecords[0];
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700552 timeRecords.pop_front();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700553 layerRecord.waitData--;
554 }
555}
556
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700557static constexpr const char* kPopupWindowPrefix = "PopupWindow";
558static const size_t kMinLenLayerName = std::strlen(kPopupWindowPrefix);
Yiwei Zhangbd408322018-10-15 18:31:53 -0700559
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700560// Avoid tracking the "PopupWindow:<random hash>#<number>" layers
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700561static bool layerNameIsValid(const std::string& layerName) {
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700562 return layerName.length() >= kMinLenLayerName &&
563 layerName.compare(0, kMinLenLayerName, kPopupWindowPrefix) != 0;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700564}
565
Alec Mouri9a29e672020-09-14 12:39:14 -0700566bool TimeStats::canAddNewAggregatedStats(uid_t uid, const std::string& layerName) {
Alec Mouri7d436ec2021-01-27 20:40:50 -0800567 uint32_t layerRecords = 0;
568 for (const auto& record : mTimeStats.stats) {
569 if (record.second.stats.count({uid, layerName}) > 0) {
570 return true;
571 }
572
573 layerRecords += record.second.stats.size();
574 }
575
576 return mTimeStats.stats.size() < MAX_NUM_LAYER_STATS;
Alec Mouri9a29e672020-09-14 12:39:14 -0700577}
578
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800579void TimeStats::setPostTime(int32_t layerId, uint64_t frameNumber, const std::string& layerName,
Alec Mouri9a29e672020-09-14 12:39:14 -0700580 uid_t uid, nsecs_t postTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700581 if (!mEnabled.load()) return;
582
583 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800584 ALOGV("[%d]-[%" PRIu64 "]-[%s]-PostTime[%" PRId64 "]", layerId, frameNumber, layerName.c_str(),
Yiwei Zhang8e8fe522018-11-02 18:34:07 -0700585 postTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700586
587 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri9a29e672020-09-14 12:39:14 -0700588 if (!canAddNewAggregatedStats(uid, layerName)) {
Yiwei Zhange926ab52019-08-14 15:16:00 -0700589 return;
590 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800591 if (!mTimeStatsTracker.count(layerId) && mTimeStatsTracker.size() < MAX_NUM_LAYER_RECORDS &&
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700592 layerNameIsValid(layerName)) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700593 mTimeStatsTracker[layerId].uid = uid;
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800594 mTimeStatsTracker[layerId].layerName = layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700595 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800596 if (!mTimeStatsTracker.count(layerId)) return;
597 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700598 if (layerRecord.timeRecords.size() == MAX_NUM_TIME_RECORDS) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800599 ALOGE("[%d]-[%s]-timeRecords is at its maximum size[%zu]. Ignore this when unittesting.",
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800600 layerId, layerRecord.layerName.c_str(), MAX_NUM_TIME_RECORDS);
601 mTimeStatsTracker.erase(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700602 return;
603 }
604 // For most media content, the acquireFence is invalid because the buffer is
605 // ready at the queueBuffer stage. In this case, acquireTime should be given
606 // a default value as postTime.
607 TimeRecord timeRecord = {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700608 .frameTime =
609 {
610 .frameNumber = frameNumber,
611 .postTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800612 .latchTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700613 .acquireTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800614 .desiredTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700615 },
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700616 };
617 layerRecord.timeRecords.push_back(timeRecord);
618 if (layerRecord.waitData < 0 ||
619 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
620 layerRecord.waitData = layerRecord.timeRecords.size() - 1;
621}
622
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800623void TimeStats::setLatchTime(int32_t layerId, uint64_t frameNumber, nsecs_t latchTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700624 if (!mEnabled.load()) return;
625
626 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800627 ALOGV("[%d]-[%" PRIu64 "]-LatchTime[%" PRId64 "]", layerId, frameNumber, latchTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700628
629 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800630 if (!mTimeStatsTracker.count(layerId)) return;
631 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700632 if (layerRecord.waitData < 0 ||
633 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
634 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700635 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700636 if (timeRecord.frameTime.frameNumber == frameNumber) {
637 timeRecord.frameTime.latchTime = latchTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700638 }
639}
640
Alec Mouri91f6df32020-01-30 08:48:58 -0800641void TimeStats::incrementLatchSkipped(int32_t layerId, LatchSkipReason reason) {
642 if (!mEnabled.load()) return;
643
644 ATRACE_CALL();
645 ALOGV("[%d]-LatchSkipped-Reason[%d]", layerId,
646 static_cast<std::underlying_type<LatchSkipReason>::type>(reason));
647
648 std::lock_guard<std::mutex> lock(mMutex);
649 if (!mTimeStatsTracker.count(layerId)) return;
650 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
651
652 switch (reason) {
653 case LatchSkipReason::LateAcquire:
654 layerRecord.lateAcquireFrames++;
655 break;
656 }
657}
658
659void TimeStats::incrementBadDesiredPresent(int32_t layerId) {
660 if (!mEnabled.load()) return;
661
662 ATRACE_CALL();
663 ALOGV("[%d]-BadDesiredPresent", layerId);
664
665 std::lock_guard<std::mutex> lock(mMutex);
666 if (!mTimeStatsTracker.count(layerId)) return;
667 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
668 layerRecord.badDesiredPresentFrames++;
669}
670
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800671void TimeStats::setDesiredTime(int32_t layerId, uint64_t frameNumber, nsecs_t desiredTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700672 if (!mEnabled.load()) return;
673
674 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800675 ALOGV("[%d]-[%" PRIu64 "]-DesiredTime[%" PRId64 "]", layerId, frameNumber, desiredTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700676
677 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800678 if (!mTimeStatsTracker.count(layerId)) return;
679 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700680 if (layerRecord.waitData < 0 ||
681 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
682 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700683 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700684 if (timeRecord.frameTime.frameNumber == frameNumber) {
685 timeRecord.frameTime.desiredTime = desiredTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700686 }
687}
688
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800689void TimeStats::setAcquireTime(int32_t layerId, uint64_t frameNumber, nsecs_t acquireTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700690 if (!mEnabled.load()) return;
691
692 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800693 ALOGV("[%d]-[%" PRIu64 "]-AcquireTime[%" PRId64 "]", layerId, frameNumber, acquireTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700694
695 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800696 if (!mTimeStatsTracker.count(layerId)) return;
697 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700698 if (layerRecord.waitData < 0 ||
699 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
700 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700701 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700702 if (timeRecord.frameTime.frameNumber == frameNumber) {
703 timeRecord.frameTime.acquireTime = acquireTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700704 }
705}
706
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800707void TimeStats::setAcquireFence(int32_t layerId, uint64_t frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700708 const std::shared_ptr<FenceTime>& acquireFence) {
709 if (!mEnabled.load()) return;
710
711 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800712 ALOGV("[%d]-[%" PRIu64 "]-AcquireFenceTime[%" PRId64 "]", layerId, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700713 acquireFence->getSignalTime());
714
715 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800716 if (!mTimeStatsTracker.count(layerId)) return;
717 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700718 if (layerRecord.waitData < 0 ||
719 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
720 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700721 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700722 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700723 timeRecord.acquireFence = acquireFence;
724 }
725}
726
Alec Mouri7d436ec2021-01-27 20:40:50 -0800727void TimeStats::setPresentTime(int32_t layerId, uint64_t frameNumber, nsecs_t presentTime,
728 Fps displayRefreshRate, std::optional<Fps> renderRate) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700729 if (!mEnabled.load()) return;
730
731 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800732 ALOGV("[%d]-[%" PRIu64 "]-PresentTime[%" PRId64 "]", layerId, frameNumber, presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700733
734 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800735 if (!mTimeStatsTracker.count(layerId)) return;
736 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700737 if (layerRecord.waitData < 0 ||
738 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
739 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700740 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700741 if (timeRecord.frameTime.frameNumber == frameNumber) {
742 timeRecord.frameTime.presentTime = presentTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700743 timeRecord.ready = true;
744 layerRecord.waitData++;
745 }
746
Alec Mouri7d436ec2021-01-27 20:40:50 -0800747 flushAvailableRecordsToStatsLocked(layerId, displayRefreshRate, renderRate);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700748}
749
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800750void TimeStats::setPresentFence(int32_t layerId, uint64_t frameNumber,
Alec Mouri7d436ec2021-01-27 20:40:50 -0800751 const std::shared_ptr<FenceTime>& presentFence,
752 Fps displayRefreshRate, std::optional<Fps> renderRate) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700753 if (!mEnabled.load()) return;
754
755 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800756 ALOGV("[%d]-[%" PRIu64 "]-PresentFenceTime[%" PRId64 "]", layerId, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700757 presentFence->getSignalTime());
758
759 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800760 if (!mTimeStatsTracker.count(layerId)) return;
761 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700762 if (layerRecord.waitData < 0 ||
763 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
764 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700765 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700766 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700767 timeRecord.presentFence = presentFence;
768 timeRecord.ready = true;
769 layerRecord.waitData++;
770 }
771
Alec Mouri7d436ec2021-01-27 20:40:50 -0800772 flushAvailableRecordsToStatsLocked(layerId, displayRefreshRate, renderRate);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700773}
774
Alec Mouri363faf02021-01-29 16:34:55 -0800775static const constexpr int32_t kValidJankyReason = JankType::SurfaceFlingerCpuDeadlineMissed |
776 JankType::SurfaceFlingerGpuDeadlineMissed | JankType::AppDeadlineMissed |
777 JankType::DisplayHAL;
778
Alec Mouri9a29e672020-09-14 12:39:14 -0700779template <class T>
780static void updateJankPayload(T& t, int32_t reasons) {
781 t.jankPayload.totalFrames++;
782
Alec Mouri9a29e672020-09-14 12:39:14 -0700783 if (reasons & kValidJankyReason) {
784 t.jankPayload.totalJankyFrames++;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800785 if ((reasons & JankType::SurfaceFlingerCpuDeadlineMissed) != 0) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700786 t.jankPayload.totalSFLongCpu++;
787 }
Jorim Jaggi5814ab82020-12-03 20:45:58 +0100788 if ((reasons & JankType::SurfaceFlingerGpuDeadlineMissed) != 0) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700789 t.jankPayload.totalSFLongGpu++;
790 }
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800791 if ((reasons & JankType::DisplayHAL) != 0) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700792 t.jankPayload.totalSFUnattributed++;
793 }
Jorim Jaggi5814ab82020-12-03 20:45:58 +0100794 if ((reasons & JankType::AppDeadlineMissed) != 0) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700795 t.jankPayload.totalAppUnattributed++;
796 }
797 }
798}
799
Alec Mouri363faf02021-01-29 16:34:55 -0800800void TimeStats::incrementJankyFrames(const JankyFramesInfo& info) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700801 if (!mEnabled.load()) return;
802
803 ATRACE_CALL();
804 std::lock_guard<std::mutex> lock(mMutex);
805
Alec Mouri542de112020-11-13 12:07:32 -0800806 // Only update layer stats if we're already tracking the layer in TimeStats.
807 // Otherwise, continue tracking the statistic but use a default layer name instead.
Alec Mouri9a29e672020-09-14 12:39:14 -0700808 // As an implementation detail, we do this because this method is expected to be
Alec Mouri542de112020-11-13 12:07:32 -0800809 // called from FrameTimeline, whose jank classification includes transaction jank
810 // that occurs without a buffer. But, in general those layer names are not suitable as
811 // aggregation keys: e.g., it's normal and expected for Window Manager to include the hash code
812 // for an animation leash. So while we can show that jank in dumpsys, aggregating based on the
813 // layer blows up the stats size, so as a workaround drop those stats. This assumes that
814 // TimeStats will flush the first present fence for a layer *before* FrameTimeline does so that
815 // the first jank record is not dropped.
Alec Mouri9a29e672020-09-14 12:39:14 -0700816
Alec Mouri542de112020-11-13 12:07:32 -0800817 static const std::string kDefaultLayerName = "none";
Alec Mouri7d436ec2021-01-27 20:40:50 -0800818
Alec Mouri363faf02021-01-29 16:34:55 -0800819 const int32_t refreshRateBucket =
820 clampToSmallestBucket(info.refreshRate, REFRESH_RATE_BUCKET_WIDTH);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800821 const int32_t renderRateBucket =
Alec Mouri363faf02021-01-29 16:34:55 -0800822 clampToSmallestBucket(info.renderRate ? *info.renderRate : info.refreshRate,
823 RENDER_RATE_BUCKET_WIDTH);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800824 const TimeStatsHelper::TimelineStatsKey timelineKey = {refreshRateBucket, renderRateBucket};
825
826 if (!mTimeStats.stats.count(timelineKey)) {
827 mTimeStats.stats[timelineKey].key = timelineKey;
Alec Mouri9a29e672020-09-14 12:39:14 -0700828 }
829
Alec Mouri7d436ec2021-01-27 20:40:50 -0800830 TimeStatsHelper::TimelineStats& timelineStats = mTimeStats.stats[timelineKey];
831
Alec Mouri363faf02021-01-29 16:34:55 -0800832 updateJankPayload<TimeStatsHelper::TimelineStats>(timelineStats, info.reasons);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800833
Alec Mouri363faf02021-01-29 16:34:55 -0800834 TimeStatsHelper::LayerStatsKey layerKey = {info.uid, info.layerName};
Alec Mouri7d436ec2021-01-27 20:40:50 -0800835 if (!timelineStats.stats.count(layerKey)) {
Alec Mouri363faf02021-01-29 16:34:55 -0800836 layerKey = {info.uid, kDefaultLayerName};
Alec Mouri7d436ec2021-01-27 20:40:50 -0800837 timelineStats.stats[layerKey].displayRefreshRateBucket = refreshRateBucket;
838 timelineStats.stats[layerKey].renderRateBucket = renderRateBucket;
Alec Mouri363faf02021-01-29 16:34:55 -0800839 timelineStats.stats[layerKey].uid = info.uid;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800840 timelineStats.stats[layerKey].layerName = kDefaultLayerName;
841 }
842
843 TimeStatsHelper::TimeStatsLayer& timeStatsLayer = timelineStats.stats[layerKey];
Alec Mouri363faf02021-01-29 16:34:55 -0800844 updateJankPayload<TimeStatsHelper::TimeStatsLayer>(timeStatsLayer, info.reasons);
845
846 if (info.reasons & kValidJankyReason) {
847 // TimeStats Histograms only retain positive values, so we don't need to check if these
848 // deadlines were really missed if we know that the frame had jank, since deadlines
849 // that were met will be dropped.
850 timelineStats.displayDeadlineDeltas.insert(static_cast<int32_t>(info.displayDeadlineDelta));
851 timelineStats.displayPresentDeltas.insert(static_cast<int32_t>(info.displayPresentJitter));
852 timeStatsLayer.deltas["appDeadlineDeltas"].insert(
853 static_cast<int32_t>(info.appDeadlineDelta));
854 }
Alec Mouri9a29e672020-09-14 12:39:14 -0700855}
856
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800857void TimeStats::onDestroy(int32_t layerId) {
Yiwei Zhangdc224042018-10-18 15:34:00 -0700858 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800859 ALOGV("[%d]-onDestroy", layerId);
Mikael Pessa90092f42019-08-26 17:22:04 -0700860 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800861 mTimeStatsTracker.erase(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700862}
863
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800864void TimeStats::removeTimeRecord(int32_t layerId, uint64_t frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700865 if (!mEnabled.load()) return;
866
867 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800868 ALOGV("[%d]-[%" PRIu64 "]-removeTimeRecord", layerId, frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700869
870 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800871 if (!mTimeStatsTracker.count(layerId)) return;
872 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700873 size_t removeAt = 0;
874 for (const TimeRecord& record : layerRecord.timeRecords) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700875 if (record.frameTime.frameNumber == frameNumber) break;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700876 removeAt++;
877 }
878 if (removeAt == layerRecord.timeRecords.size()) return;
879 layerRecord.timeRecords.erase(layerRecord.timeRecords.begin() + removeAt);
880 if (layerRecord.waitData > static_cast<int32_t>(removeAt)) {
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700881 layerRecord.waitData--;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700882 }
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700883 layerRecord.droppedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700884}
885
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700886void TimeStats::flushPowerTimeLocked() {
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700887 if (!mEnabled.load()) return;
888
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700889 nsecs_t curTime = systemTime();
890 // elapsedTime is in milliseconds.
891 int64_t elapsedTime = (curTime - mPowerTime.prevTime) / 1000000;
892
893 switch (mPowerTime.powerMode) {
Peiyong Lin65248e02020-04-18 21:15:07 -0700894 case PowerMode::ON:
Alec Mouri7d436ec2021-01-27 20:40:50 -0800895 mTimeStats.displayOnTimeLegacy += elapsedTime;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700896 break;
Peiyong Lin65248e02020-04-18 21:15:07 -0700897 case PowerMode::OFF:
898 case PowerMode::DOZE:
899 case PowerMode::DOZE_SUSPEND:
900 case PowerMode::ON_SUSPEND:
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700901 default:
902 break;
903 }
904
905 mPowerTime.prevTime = curTime;
906}
907
Peiyong Lin65248e02020-04-18 21:15:07 -0700908void TimeStats::setPowerMode(PowerMode powerMode) {
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700909 if (!mEnabled.load()) {
910 std::lock_guard<std::mutex> lock(mMutex);
911 mPowerTime.powerMode = powerMode;
912 return;
913 }
914
915 std::lock_guard<std::mutex> lock(mMutex);
916 if (powerMode == mPowerTime.powerMode) return;
917
918 flushPowerTimeLocked();
919 mPowerTime.powerMode = powerMode;
920}
921
Alec Mourifb571ea2019-01-24 18:42:10 -0800922void TimeStats::recordRefreshRate(uint32_t fps, nsecs_t duration) {
923 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800924 if (mTimeStats.refreshRateStatsLegacy.count(fps)) {
925 mTimeStats.refreshRateStatsLegacy[fps] += duration;
Alec Mourifb571ea2019-01-24 18:42:10 -0800926 } else {
Alec Mouri7d436ec2021-01-27 20:40:50 -0800927 mTimeStats.refreshRateStatsLegacy.insert({fps, duration});
Alec Mourifb571ea2019-01-24 18:42:10 -0800928 }
929}
930
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700931void TimeStats::flushAvailableGlobalRecordsToStatsLocked() {
932 ATRACE_CALL();
933
934 while (!mGlobalRecord.presentFences.empty()) {
935 const nsecs_t curPresentTime = mGlobalRecord.presentFences.front()->getSignalTime();
936 if (curPresentTime == Fence::SIGNAL_TIME_PENDING) break;
937
938 if (curPresentTime == Fence::SIGNAL_TIME_INVALID) {
939 ALOGE("GlobalPresentFence is invalid!");
940 mGlobalRecord.prevPresentTime = 0;
941 mGlobalRecord.presentFences.pop_front();
942 continue;
943 }
944
945 ALOGV("GlobalPresentFenceTime[%" PRId64 "]",
946 mGlobalRecord.presentFences.front()->getSignalTime());
947
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700948 if (mGlobalRecord.prevPresentTime != 0) {
949 const int32_t presentToPresentMs =
950 msBetween(mGlobalRecord.prevPresentTime, curPresentTime);
951 ALOGV("Global present2present[%d] prev[%" PRId64 "] curr[%" PRId64 "]",
952 presentToPresentMs, mGlobalRecord.prevPresentTime, curPresentTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800953 mTimeStats.presentToPresentLegacy.insert(presentToPresentMs);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700954 }
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700955
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700956 mGlobalRecord.prevPresentTime = curPresentTime;
957 mGlobalRecord.presentFences.pop_front();
958 }
Alec Mourie4034bb2019-11-19 12:45:54 -0800959 while (!mGlobalRecord.renderEngineDurations.empty()) {
960 const auto duration = mGlobalRecord.renderEngineDurations.front();
961 const auto& endTime = duration.endTime;
962
963 nsecs_t endNs = -1;
964
965 if (auto val = std::get_if<nsecs_t>(&endTime)) {
966 endNs = *val;
967 } else {
968 endNs = std::get<std::shared_ptr<FenceTime>>(endTime)->getSignalTime();
969 }
970
971 if (endNs == Fence::SIGNAL_TIME_PENDING) break;
972
973 if (endNs < 0) {
974 ALOGE("RenderEngineTiming is invalid!");
975 mGlobalRecord.renderEngineDurations.pop_front();
976 continue;
977 }
978
979 const int32_t renderEngineMs = msBetween(duration.startTime, endNs);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800980 mTimeStats.renderEngineTimingLegacy.insert(renderEngineMs);
Alec Mourie4034bb2019-11-19 12:45:54 -0800981
982 mGlobalRecord.renderEngineDurations.pop_front();
983 }
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700984}
985
986void TimeStats::setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) {
987 if (!mEnabled.load()) return;
988
989 ATRACE_CALL();
990 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700991 if (presentFence == nullptr || !presentFence->isValid()) {
992 mGlobalRecord.prevPresentTime = 0;
993 return;
994 }
995
Peiyong Lin65248e02020-04-18 21:15:07 -0700996 if (mPowerTime.powerMode != PowerMode::ON) {
997 // Try flushing the last present fence on PowerMode::ON.
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700998 flushAvailableGlobalRecordsToStatsLocked();
999 mGlobalRecord.presentFences.clear();
Yiwei Zhangce6ebc02018-10-20 12:42:38 -07001000 mGlobalRecord.prevPresentTime = 0;
1001 return;
1002 }
1003
1004 if (mGlobalRecord.presentFences.size() == MAX_NUM_TIME_RECORDS) {
1005 // The front presentFence must be trapped in pending status in this
1006 // case. Try dequeuing the front one to recover.
1007 ALOGE("GlobalPresentFences is already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
1008 mGlobalRecord.prevPresentTime = 0;
1009 mGlobalRecord.presentFences.pop_front();
1010 }
1011
1012 mGlobalRecord.presentFences.emplace_back(presentFence);
1013 flushAvailableGlobalRecordsToStatsLocked();
1014}
1015
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001016void TimeStats::enable() {
1017 if (mEnabled.load()) return;
1018
1019 ATRACE_CALL();
1020
1021 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001022 mEnabled.store(true);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001023 mTimeStats.statsStartLegacy = static_cast<int64_t>(std::time(0));
Yiwei Zhang3a226d22018-10-16 09:23:03 -07001024 mPowerTime.prevTime = systemTime();
Yiwei Zhange5c49d52018-10-29 00:15:31 -07001025 ALOGD("Enabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001026}
1027
1028void TimeStats::disable() {
1029 if (!mEnabled.load()) return;
1030
1031 ATRACE_CALL();
1032
1033 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -07001034 flushPowerTimeLocked();
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001035 mEnabled.store(false);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001036 mTimeStats.statsEndLegacy = static_cast<int64_t>(std::time(0));
Yiwei Zhange5c49d52018-10-29 00:15:31 -07001037 ALOGD("Disabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001038}
1039
Alec Mouri8e2f31b2020-01-16 22:04:35 +00001040void TimeStats::clearAll() {
1041 std::lock_guard<std::mutex> lock(mMutex);
1042 clearGlobalLocked();
1043 clearLayersLocked();
1044}
1045
1046void TimeStats::clearGlobalLocked() {
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001047 ATRACE_CALL();
1048
Alec Mouri7d436ec2021-01-27 20:40:50 -08001049 mTimeStats.statsStartLegacy = (mEnabled.load() ? static_cast<int64_t>(std::time(0)) : 0);
1050 mTimeStats.statsEndLegacy = 0;
1051 mTimeStats.totalFramesLegacy = 0;
1052 mTimeStats.missedFramesLegacy = 0;
1053 mTimeStats.clientCompositionFramesLegacy = 0;
1054 mTimeStats.clientCompositionReusedFramesLegacy = 0;
1055 mTimeStats.refreshRateSwitchesLegacy = 0;
1056 mTimeStats.compositionStrategyChangesLegacy = 0;
1057 mTimeStats.displayEventConnectionsCountLegacy = 0;
1058 mTimeStats.displayOnTimeLegacy = 0;
1059 mTimeStats.presentToPresentLegacy.hist.clear();
1060 mTimeStats.frameDurationLegacy.hist.clear();
1061 mTimeStats.renderEngineTimingLegacy.hist.clear();
1062 mTimeStats.refreshRateStatsLegacy.clear();
Yiwei Zhang3a226d22018-10-16 09:23:03 -07001063 mPowerTime.prevTime = systemTime();
Yiwei Zhange5c49d52018-10-29 00:15:31 -07001064 mGlobalRecord.prevPresentTime = 0;
1065 mGlobalRecord.presentFences.clear();
Alec Mouri8e2f31b2020-01-16 22:04:35 +00001066 ALOGD("Cleared global stats");
1067}
1068
1069void TimeStats::clearLayersLocked() {
1070 ATRACE_CALL();
1071
1072 mTimeStatsTracker.clear();
1073 mTimeStats.stats.clear();
1074 ALOGD("Cleared layer stats");
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001075}
1076
1077bool TimeStats::isEnabled() {
1078 return mEnabled.load();
1079}
1080
Yiwei Zhang5434a782018-12-05 18:06:32 -08001081void TimeStats::dump(bool asProto, std::optional<uint32_t> maxLayers, std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001082 ATRACE_CALL();
1083
1084 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001085 if (mTimeStats.statsStartLegacy == 0) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001086 return;
1087 }
1088
Alec Mouri7d436ec2021-01-27 20:40:50 -08001089 mTimeStats.statsEndLegacy = static_cast<int64_t>(std::time(0));
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001090
Yiwei Zhang3a226d22018-10-16 09:23:03 -07001091 flushPowerTimeLocked();
1092
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001093 if (asProto) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -07001094 ALOGD("Dumping TimeStats as proto");
Yiwei Zhangdc224042018-10-18 15:34:00 -07001095 SFTimeStatsGlobalProto timeStatsProto = mTimeStats.toProto(maxLayers);
Dominik Laskowski46470112019-08-02 13:13:11 -07001096 result.append(timeStatsProto.SerializeAsString());
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001097 } else {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -07001098 ALOGD("Dumping TimeStats as text");
Yiwei Zhang5434a782018-12-05 18:06:32 -08001099 result.append(mTimeStats.toString(maxLayers));
Yiwei Zhang8a4015c2018-05-08 16:03:47 -07001100 result.append("\n");
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001101 }
1102}
1103
Alec Mourifb571ea2019-01-24 18:42:10 -08001104} // namespace impl
1105
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001106} // namespace android