blob: 974ae8484f5d56d95c4156171a5bc796955fb6b3 [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);
Adithya Srinivasanead17162021-02-18 02:17:37 +0000143 mStatsDelegate->statsEventWriteInt32(event,
144 globalSlice.second.jankPayload.totalSFScheduling);
145 mStatsDelegate->statsEventWriteInt32(event,
146 globalSlice.second.jankPayload.totalSFPredictionError);
147 mStatsDelegate->statsEventWriteInt32(event,
148 globalSlice.second.jankPayload.totalAppBufferStuffing);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800149 mStatsDelegate->statsEventWriteInt32(event, globalSlice.first.displayRefreshRateBucket);
150 std::string sfDeadlineMissedBytes =
Alec Mouri363faf02021-01-29 16:34:55 -0800151 histogramToProtoByteString(globalSlice.second.displayDeadlineDeltas.hist,
Alec Mouri7d436ec2021-01-27 20:40:50 -0800152 mMaxPulledHistogramBuckets);
Alec Mouri363faf02021-01-29 16:34:55 -0800153 mStatsDelegate->statsEventWriteByteArray(event,
154 (const uint8_t*)sfDeadlineMissedBytes.c_str(),
155 sfDeadlineMissedBytes.size());
Alec Mouri7d436ec2021-01-27 20:40:50 -0800156 std::string sfPredictionErrorBytes =
Alec Mouri363faf02021-01-29 16:34:55 -0800157 histogramToProtoByteString(globalSlice.second.displayPresentDeltas.hist,
Alec Mouri7d436ec2021-01-27 20:40:50 -0800158 mMaxPulledHistogramBuckets);
Alec Mouri363faf02021-01-29 16:34:55 -0800159 mStatsDelegate->statsEventWriteByteArray(event,
160 (const uint8_t*)sfPredictionErrorBytes.c_str(),
161 sfPredictionErrorBytes.size());
Alec Mouri7d436ec2021-01-27 20:40:50 -0800162 mStatsDelegate->statsEventWriteInt32(event, globalSlice.first.renderRateBucket);
163 mStatsDelegate->statsEventBuild(event);
164 }
165
Alec Mouridfad9002020-02-12 17:49:09 -0800166 clearGlobalLocked();
167
168 return AStatsManager_PULL_SUCCESS;
169}
170
Tej Singh2a457b62020-01-31 16:16:10 -0800171AStatsManager_PullAtomCallbackReturn TimeStats::populateLayerAtom(AStatsEventList* data) {
Alec Mouri37384342020-01-02 17:23:37 -0800172 std::lock_guard<std::mutex> lock(mMutex);
173
Alec Mouri363faf02021-01-29 16:34:55 -0800174 std::vector<TimeStatsHelper::TimeStatsLayer*> dumpStats;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800175 uint32_t numLayers = 0;
176 for (const auto& globalSlice : mTimeStats.stats) {
177 numLayers += globalSlice.second.stats.size();
178 }
179
180 dumpStats.reserve(numLayers);
181
Alec Mouri363faf02021-01-29 16:34:55 -0800182 for (auto& globalSlice : mTimeStats.stats) {
183 for (auto& layerSlice : globalSlice.second.stats) {
Alec Mouri7d436ec2021-01-27 20:40:50 -0800184 dumpStats.push_back(&layerSlice.second);
185 }
Alec Mouri37384342020-01-02 17:23:37 -0800186 }
187
188 std::sort(dumpStats.begin(), dumpStats.end(),
189 [](TimeStatsHelper::TimeStatsLayer const* l,
190 TimeStatsHelper::TimeStatsLayer const* r) {
191 return l->totalFrames > r->totalFrames;
192 });
193
194 if (mMaxPulledLayers < dumpStats.size()) {
195 dumpStats.resize(mMaxPulledLayers);
196 }
197
Alec Mouri363faf02021-01-29 16:34:55 -0800198 for (auto& layer : dumpStats) {
Tej Singh2a457b62020-01-31 16:16:10 -0800199 AStatsEvent* event = mStatsDelegate->addStatsEventToPullData(data);
Alec Mouri37384342020-01-02 17:23:37 -0800200 mStatsDelegate->statsEventSetAtomId(event, android::util::SURFACEFLINGER_STATS_LAYER_INFO);
201 mStatsDelegate->statsEventWriteString8(event, layer->layerName.c_str());
202 mStatsDelegate->statsEventWriteInt64(event, layer->totalFrames);
203 mStatsDelegate->statsEventWriteInt64(event, layer->droppedFrames);
204
205 for (const auto& name : kHistogramNames) {
206 const auto& histogram = layer->deltas.find(name);
207 if (histogram == layer->deltas.cend()) {
208 mStatsDelegate->statsEventWriteByteArray(event, nullptr, 0);
209 } else {
210 std::string bytes = histogramToProtoByteString(histogram->second.hist,
211 mMaxPulledHistogramBuckets);
212 mStatsDelegate->statsEventWriteByteArray(event, (const uint8_t*)bytes.c_str(),
213 bytes.size());
214 }
215 }
216
Yiwei Zhang7bfc75b2020-02-10 11:20:34 -0800217 mStatsDelegate->statsEventWriteInt64(event, layer->lateAcquireFrames);
218 mStatsDelegate->statsEventWriteInt64(event, layer->badDesiredPresentFrames);
Alec Mouri9a29e672020-09-14 12:39:14 -0700219 mStatsDelegate->statsEventWriteInt32(event, layer->uid);
220 mStatsDelegate->statsEventWriteInt32(event, layer->jankPayload.totalFrames);
221 mStatsDelegate->statsEventWriteInt32(event, layer->jankPayload.totalJankyFrames);
222 mStatsDelegate->statsEventWriteInt32(event, layer->jankPayload.totalSFLongCpu);
223 mStatsDelegate->statsEventWriteInt32(event, layer->jankPayload.totalSFLongGpu);
224 mStatsDelegate->statsEventWriteInt32(event, layer->jankPayload.totalSFUnattributed);
225 mStatsDelegate->statsEventWriteInt32(event, layer->jankPayload.totalAppUnattributed);
Adithya Srinivasanead17162021-02-18 02:17:37 +0000226 mStatsDelegate->statsEventWriteInt32(event, layer->jankPayload.totalSFScheduling);
227 mStatsDelegate->statsEventWriteInt32(event, layer->jankPayload.totalSFPredictionError);
228 mStatsDelegate->statsEventWriteInt32(event, layer->jankPayload.totalAppBufferStuffing);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800229 mStatsDelegate->statsEventWriteInt32(
230 event, layer->displayRefreshRateBucket); // display_refresh_rate_bucket
231 mStatsDelegate->statsEventWriteInt32(event, layer->renderRateBucket); // render_rate_bucket
Alec Mouri75de8f22021-01-20 14:53:44 -0800232 std::string frameRateVoteBytes = frameRateVoteToProtoByteString(0.0, 0, 0);
233 mStatsDelegate->statsEventWriteByteArray(event, (const uint8_t*)frameRateVoteBytes.c_str(),
234 frameRateVoteBytes.size()); // set_frame_rate_vote
235 std::string appDeadlineMissedBytes =
Alec Mouri363faf02021-01-29 16:34:55 -0800236 histogramToProtoByteString(layer->deltas["appDeadlineDeltas"].hist,
Alec Mouri75de8f22021-01-20 14:53:44 -0800237 mMaxPulledHistogramBuckets);
Alec Mouri363faf02021-01-29 16:34:55 -0800238 mStatsDelegate->statsEventWriteByteArray(event,
239 (const uint8_t*)appDeadlineMissedBytes.c_str(),
240 appDeadlineMissedBytes.size());
Alec Mouri75de8f22021-01-20 14:53:44 -0800241
Alec Mouri37384342020-01-02 17:23:37 -0800242 mStatsDelegate->statsEventBuild(event);
243 }
244 clearLayersLocked();
245
Tej Singh2a457b62020-01-31 16:16:10 -0800246 return AStatsManager_PULL_SUCCESS;
Alec Mouri37384342020-01-02 17:23:37 -0800247}
248
249TimeStats::TimeStats() : TimeStats(nullptr, std::nullopt, std::nullopt) {}
250
251TimeStats::TimeStats(std::unique_ptr<StatsEventDelegate> statsDelegate,
252 std::optional<size_t> maxPulledLayers,
253 std::optional<size_t> maxPulledHistogramBuckets) {
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000254 if (statsDelegate != nullptr) {
255 mStatsDelegate = std::move(statsDelegate);
256 }
Alec Mouri37384342020-01-02 17:23:37 -0800257
258 if (maxPulledLayers) {
259 mMaxPulledLayers = *maxPulledLayers;
260 }
261
262 if (maxPulledHistogramBuckets) {
263 mMaxPulledHistogramBuckets = *maxPulledHistogramBuckets;
264 }
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000265}
266
Alec Mouri3ecd5cd2020-01-29 12:53:07 -0800267TimeStats::~TimeStats() {
268 std::lock_guard<std::mutex> lock(mMutex);
Tej Singh38a4b212020-03-13 19:04:51 -0700269 mStatsDelegate->clearStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_GLOBAL_INFO);
270 mStatsDelegate->clearStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_LAYER_INFO);
Alec Mouri3ecd5cd2020-01-29 12:53:07 -0800271}
272
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000273void TimeStats::onBootFinished() {
Alec Mouri3ecd5cd2020-01-29 12:53:07 -0800274 std::lock_guard<std::mutex> lock(mMutex);
Tej Singh38a4b212020-03-13 19:04:51 -0700275 mStatsDelegate->setStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_GLOBAL_INFO,
276 nullptr, TimeStats::pullAtomCallback, this);
277 mStatsDelegate->setStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_LAYER_INFO,
278 nullptr, TimeStats::pullAtomCallback, this);
Alec Mourib3885ad2019-09-06 17:08:55 -0700279}
280
Dominik Laskowskic2867142019-01-21 11:33:38 -0800281void TimeStats::parseArgs(bool asProto, const Vector<String16>& args, std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700282 ATRACE_CALL();
283
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700284 std::unordered_map<std::string, int32_t> argsMap;
Dominik Laskowskic2867142019-01-21 11:33:38 -0800285 for (size_t index = 0; index < args.size(); ++index) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700286 argsMap[std::string(String8(args[index]).c_str())] = index;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700287 }
288
289 if (argsMap.count("-disable")) {
290 disable();
291 }
292
293 if (argsMap.count("-dump")) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700294 std::optional<uint32_t> maxLayers = std::nullopt;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700295 auto iter = argsMap.find("-maxlayers");
296 if (iter != argsMap.end() && iter->second + 1 < static_cast<int32_t>(args.size())) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700297 int64_t value = strtol(String8(args[iter->second + 1]).c_str(), nullptr, 10);
298 value = std::clamp(value, int64_t(0), int64_t(UINT32_MAX));
299 maxLayers = static_cast<uint32_t>(value);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700300 }
301
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700302 dump(asProto, maxLayers, result);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700303 }
304
305 if (argsMap.count("-clear")) {
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000306 clearAll();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700307 }
308
309 if (argsMap.count("-enable")) {
310 enable();
311 }
312}
313
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700314std::string TimeStats::miniDump() {
315 ATRACE_CALL();
316
317 std::string result = "TimeStats miniDump:\n";
318 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange926ab52019-08-14 15:16:00 -0700319 android::base::StringAppendF(&result, "Number of layers currently being tracked is %zu\n",
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700320 mTimeStatsTracker.size());
Yiwei Zhange926ab52019-08-14 15:16:00 -0700321 android::base::StringAppendF(&result, "Number of layers in the stats pool is %zu\n",
322 mTimeStats.stats.size());
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700323 return result;
324}
325
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700326void TimeStats::incrementTotalFrames() {
327 if (!mEnabled.load()) return;
328
329 ATRACE_CALL();
330
331 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800332 mTimeStats.totalFramesLegacy++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700333}
334
Yiwei Zhang621f9d42018-05-07 10:40:55 -0700335void TimeStats::incrementMissedFrames() {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700336 if (!mEnabled.load()) return;
337
338 ATRACE_CALL();
339
340 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800341 mTimeStats.missedFramesLegacy++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700342}
343
344void TimeStats::incrementClientCompositionFrames() {
345 if (!mEnabled.load()) return;
346
347 ATRACE_CALL();
348
349 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800350 mTimeStats.clientCompositionFramesLegacy++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700351}
352
Vishnu Nair9b079a22020-01-21 14:36:08 -0800353void TimeStats::incrementClientCompositionReusedFrames() {
354 if (!mEnabled.load()) return;
355
356 ATRACE_CALL();
357
358 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800359 mTimeStats.clientCompositionReusedFramesLegacy++;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800360}
361
Alec Mouri8de697e2020-03-19 10:52:01 -0700362void TimeStats::incrementRefreshRateSwitches() {
363 if (!mEnabled.load()) return;
364
365 ATRACE_CALL();
366
367 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800368 mTimeStats.refreshRateSwitchesLegacy++;
Alec Mouri8de697e2020-03-19 10:52:01 -0700369}
370
Alec Mouri8f7a0102020-04-15 12:11:10 -0700371void TimeStats::incrementCompositionStrategyChanges() {
372 if (!mEnabled.load()) return;
373
374 ATRACE_CALL();
375
376 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800377 mTimeStats.compositionStrategyChangesLegacy++;
Alec Mouri8f7a0102020-04-15 12:11:10 -0700378}
379
Alec Mouri717bcb62020-02-10 17:07:19 -0800380void TimeStats::recordDisplayEventConnectionCount(int32_t count) {
381 if (!mEnabled.load()) return;
382
383 ATRACE_CALL();
384
385 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800386 mTimeStats.displayEventConnectionsCountLegacy =
387 std::max(mTimeStats.displayEventConnectionsCountLegacy, count);
Alec Mouri717bcb62020-02-10 17:07:19 -0800388}
389
Alec Mouri9519bf12019-11-15 16:54:44 -0800390static int32_t msBetween(nsecs_t start, nsecs_t end) {
391 int64_t delta = std::chrono::duration_cast<std::chrono::milliseconds>(
392 std::chrono::nanoseconds(end - start))
393 .count();
394 delta = std::clamp(delta, int64_t(INT32_MIN), int64_t(INT32_MAX));
395 return static_cast<int32_t>(delta);
396}
397
398void TimeStats::recordFrameDuration(nsecs_t startTime, nsecs_t endTime) {
399 if (!mEnabled.load()) return;
400
401 std::lock_guard<std::mutex> lock(mMutex);
Peiyong Lin65248e02020-04-18 21:15:07 -0700402 if (mPowerTime.powerMode == PowerMode::ON) {
Alec Mouri7d436ec2021-01-27 20:40:50 -0800403 mTimeStats.frameDurationLegacy.insert(msBetween(startTime, endTime));
Alec Mouri9519bf12019-11-15 16:54:44 -0800404 }
405}
406
Alec Mourie4034bb2019-11-19 12:45:54 -0800407void TimeStats::recordRenderEngineDuration(nsecs_t startTime, nsecs_t endTime) {
408 if (!mEnabled.load()) return;
409
410 std::lock_guard<std::mutex> lock(mMutex);
411 if (mGlobalRecord.renderEngineDurations.size() == MAX_NUM_TIME_RECORDS) {
412 ALOGE("RenderEngineTimes are already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
413 mGlobalRecord.renderEngineDurations.pop_front();
414 }
415 mGlobalRecord.renderEngineDurations.push_back({startTime, endTime});
416}
417
418void TimeStats::recordRenderEngineDuration(nsecs_t startTime,
419 const std::shared_ptr<FenceTime>& endTime) {
420 if (!mEnabled.load()) return;
421
422 std::lock_guard<std::mutex> lock(mMutex);
423 if (mGlobalRecord.renderEngineDurations.size() == MAX_NUM_TIME_RECORDS) {
424 ALOGE("RenderEngineTimes are already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
425 mGlobalRecord.renderEngineDurations.pop_front();
426 }
427 mGlobalRecord.renderEngineDurations.push_back({startTime, endTime});
428}
429
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800430bool TimeStats::recordReadyLocked(int32_t layerId, TimeRecord* timeRecord) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700431 if (!timeRecord->ready) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800432 ALOGV("[%d]-[%" PRIu64 "]-presentFence is still not received", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700433 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700434 return false;
435 }
436
437 if (timeRecord->acquireFence != nullptr) {
438 if (timeRecord->acquireFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
439 return false;
440 }
441 if (timeRecord->acquireFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700442 timeRecord->frameTime.acquireTime = timeRecord->acquireFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700443 timeRecord->acquireFence = nullptr;
444 } else {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800445 ALOGV("[%d]-[%" PRIu64 "]-acquireFence signal time is invalid", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700446 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700447 }
448 }
449
450 if (timeRecord->presentFence != nullptr) {
451 if (timeRecord->presentFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
452 return false;
453 }
454 if (timeRecord->presentFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700455 timeRecord->frameTime.presentTime = timeRecord->presentFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700456 timeRecord->presentFence = nullptr;
457 } else {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800458 ALOGV("[%d]-[%" PRIu64 "]-presentFence signal time invalid", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700459 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700460 }
461 }
462
463 return true;
464}
465
Alec Mouri7d436ec2021-01-27 20:40:50 -0800466static int32_t clampToSmallestBucket(Fps fps, size_t bucketWidth) {
467 return (fps.getIntValue() / bucketWidth) * bucketWidth;
468}
469
470void TimeStats::flushAvailableRecordsToStatsLocked(int32_t layerId, Fps displayRefreshRate,
471 std::optional<Fps> renderRate) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700472 ATRACE_CALL();
473
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800474 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700475 TimeRecord& prevTimeRecord = layerRecord.prevTimeRecord;
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700476 std::deque<TimeRecord>& timeRecords = layerRecord.timeRecords;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800477 const int32_t refreshRateBucket =
478 clampToSmallestBucket(displayRefreshRate, REFRESH_RATE_BUCKET_WIDTH);
479 const int32_t renderRateBucket =
480 clampToSmallestBucket(renderRate ? *renderRate : displayRefreshRate,
481 RENDER_RATE_BUCKET_WIDTH);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700482 while (!timeRecords.empty()) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800483 if (!recordReadyLocked(layerId, &timeRecords[0])) break;
484 ALOGV("[%d]-[%" PRIu64 "]-presentFenceTime[%" PRId64 "]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700485 timeRecords[0].frameTime.frameNumber, timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700486
487 if (prevTimeRecord.ready) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700488 uid_t uid = layerRecord.uid;
Yiwei Zhangeafa5cc2019-07-26 15:06:25 -0700489 const std::string& layerName = layerRecord.layerName;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800490 TimeStatsHelper::TimelineStatsKey timelineKey = {refreshRateBucket, renderRateBucket};
491 if (!mTimeStats.stats.count(timelineKey)) {
492 mTimeStats.stats[timelineKey].key = timelineKey;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700493 }
Alec Mouri7d436ec2021-01-27 20:40:50 -0800494
495 TimeStatsHelper::TimelineStats& displayStats = mTimeStats.stats[timelineKey];
496
497 TimeStatsHelper::LayerStatsKey layerKey = {uid, layerName};
498 if (!displayStats.stats.count(layerKey)) {
499 displayStats.stats[layerKey].displayRefreshRateBucket = refreshRateBucket;
500 displayStats.stats[layerKey].renderRateBucket = renderRateBucket;
501 displayStats.stats[layerKey].uid = uid;
502 displayStats.stats[layerKey].layerName = layerName;
503 }
504 TimeStatsHelper::TimeStatsLayer& timeStatsLayer = displayStats.stats[layerKey];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700505 timeStatsLayer.totalFrames++;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700506 timeStatsLayer.droppedFrames += layerRecord.droppedFrames;
Alec Mouri91f6df32020-01-30 08:48:58 -0800507 timeStatsLayer.lateAcquireFrames += layerRecord.lateAcquireFrames;
508 timeStatsLayer.badDesiredPresentFrames += layerRecord.badDesiredPresentFrames;
509
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700510 layerRecord.droppedFrames = 0;
Alec Mouri91f6df32020-01-30 08:48:58 -0800511 layerRecord.lateAcquireFrames = 0;
512 layerRecord.badDesiredPresentFrames = 0;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700513
514 const int32_t postToAcquireMs = msBetween(timeRecords[0].frameTime.postTime,
515 timeRecords[0].frameTime.acquireTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800516 ALOGV("[%d]-[%" PRIu64 "]-post2acquire[%d]", layerId,
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700517 timeRecords[0].frameTime.frameNumber, postToAcquireMs);
518 timeStatsLayer.deltas["post2acquire"].insert(postToAcquireMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700519
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700520 const int32_t postToPresentMs = msBetween(timeRecords[0].frameTime.postTime,
521 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800522 ALOGV("[%d]-[%" PRIu64 "]-post2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700523 timeRecords[0].frameTime.frameNumber, postToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700524 timeStatsLayer.deltas["post2present"].insert(postToPresentMs);
525
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700526 const int32_t acquireToPresentMs = msBetween(timeRecords[0].frameTime.acquireTime,
527 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800528 ALOGV("[%d]-[%" PRIu64 "]-acquire2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700529 timeRecords[0].frameTime.frameNumber, acquireToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700530 timeStatsLayer.deltas["acquire2present"].insert(acquireToPresentMs);
531
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700532 const int32_t latchToPresentMs = msBetween(timeRecords[0].frameTime.latchTime,
533 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800534 ALOGV("[%d]-[%" PRIu64 "]-latch2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700535 timeRecords[0].frameTime.frameNumber, latchToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700536 timeStatsLayer.deltas["latch2present"].insert(latchToPresentMs);
537
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700538 const int32_t desiredToPresentMs = msBetween(timeRecords[0].frameTime.desiredTime,
539 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800540 ALOGV("[%d]-[%" PRIu64 "]-desired2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700541 timeRecords[0].frameTime.frameNumber, desiredToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700542 timeStatsLayer.deltas["desired2present"].insert(desiredToPresentMs);
543
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700544 const int32_t presentToPresentMs = msBetween(prevTimeRecord.frameTime.presentTime,
545 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800546 ALOGV("[%d]-[%" PRIu64 "]-present2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700547 timeRecords[0].frameTime.frameNumber, presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700548 timeStatsLayer.deltas["present2present"].insert(presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700549 }
550 prevTimeRecord = timeRecords[0];
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700551 timeRecords.pop_front();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700552 layerRecord.waitData--;
553 }
554}
555
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700556static constexpr const char* kPopupWindowPrefix = "PopupWindow";
557static const size_t kMinLenLayerName = std::strlen(kPopupWindowPrefix);
Yiwei Zhangbd408322018-10-15 18:31:53 -0700558
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700559// Avoid tracking the "PopupWindow:<random hash>#<number>" layers
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700560static bool layerNameIsValid(const std::string& layerName) {
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700561 return layerName.length() >= kMinLenLayerName &&
562 layerName.compare(0, kMinLenLayerName, kPopupWindowPrefix) != 0;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700563}
564
Alec Mouri9a29e672020-09-14 12:39:14 -0700565bool TimeStats::canAddNewAggregatedStats(uid_t uid, const std::string& layerName) {
Alec Mouri7d436ec2021-01-27 20:40:50 -0800566 uint32_t layerRecords = 0;
567 for (const auto& record : mTimeStats.stats) {
568 if (record.second.stats.count({uid, layerName}) > 0) {
569 return true;
570 }
571
572 layerRecords += record.second.stats.size();
573 }
574
575 return mTimeStats.stats.size() < MAX_NUM_LAYER_STATS;
Alec Mouri9a29e672020-09-14 12:39:14 -0700576}
577
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800578void TimeStats::setPostTime(int32_t layerId, uint64_t frameNumber, const std::string& layerName,
Alec Mouri9a29e672020-09-14 12:39:14 -0700579 uid_t uid, nsecs_t postTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700580 if (!mEnabled.load()) return;
581
582 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800583 ALOGV("[%d]-[%" PRIu64 "]-[%s]-PostTime[%" PRId64 "]", layerId, frameNumber, layerName.c_str(),
Yiwei Zhang8e8fe522018-11-02 18:34:07 -0700584 postTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700585
586 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri9a29e672020-09-14 12:39:14 -0700587 if (!canAddNewAggregatedStats(uid, layerName)) {
Yiwei Zhange926ab52019-08-14 15:16:00 -0700588 return;
589 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800590 if (!mTimeStatsTracker.count(layerId) && mTimeStatsTracker.size() < MAX_NUM_LAYER_RECORDS &&
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700591 layerNameIsValid(layerName)) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700592 mTimeStatsTracker[layerId].uid = uid;
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800593 mTimeStatsTracker[layerId].layerName = layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700594 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800595 if (!mTimeStatsTracker.count(layerId)) return;
596 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700597 if (layerRecord.timeRecords.size() == MAX_NUM_TIME_RECORDS) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800598 ALOGE("[%d]-[%s]-timeRecords is at its maximum size[%zu]. Ignore this when unittesting.",
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800599 layerId, layerRecord.layerName.c_str(), MAX_NUM_TIME_RECORDS);
600 mTimeStatsTracker.erase(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700601 return;
602 }
603 // For most media content, the acquireFence is invalid because the buffer is
604 // ready at the queueBuffer stage. In this case, acquireTime should be given
605 // a default value as postTime.
606 TimeRecord timeRecord = {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700607 .frameTime =
608 {
609 .frameNumber = frameNumber,
610 .postTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800611 .latchTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700612 .acquireTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800613 .desiredTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700614 },
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700615 };
616 layerRecord.timeRecords.push_back(timeRecord);
617 if (layerRecord.waitData < 0 ||
618 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
619 layerRecord.waitData = layerRecord.timeRecords.size() - 1;
620}
621
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800622void TimeStats::setLatchTime(int32_t layerId, uint64_t frameNumber, nsecs_t latchTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700623 if (!mEnabled.load()) return;
624
625 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800626 ALOGV("[%d]-[%" PRIu64 "]-LatchTime[%" PRId64 "]", layerId, frameNumber, latchTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700627
628 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800629 if (!mTimeStatsTracker.count(layerId)) return;
630 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700631 if (layerRecord.waitData < 0 ||
632 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
633 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700634 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700635 if (timeRecord.frameTime.frameNumber == frameNumber) {
636 timeRecord.frameTime.latchTime = latchTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700637 }
638}
639
Alec Mouri91f6df32020-01-30 08:48:58 -0800640void TimeStats::incrementLatchSkipped(int32_t layerId, LatchSkipReason reason) {
641 if (!mEnabled.load()) return;
642
643 ATRACE_CALL();
644 ALOGV("[%d]-LatchSkipped-Reason[%d]", layerId,
645 static_cast<std::underlying_type<LatchSkipReason>::type>(reason));
646
647 std::lock_guard<std::mutex> lock(mMutex);
648 if (!mTimeStatsTracker.count(layerId)) return;
649 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
650
651 switch (reason) {
652 case LatchSkipReason::LateAcquire:
653 layerRecord.lateAcquireFrames++;
654 break;
655 }
656}
657
658void TimeStats::incrementBadDesiredPresent(int32_t layerId) {
659 if (!mEnabled.load()) return;
660
661 ATRACE_CALL();
662 ALOGV("[%d]-BadDesiredPresent", layerId);
663
664 std::lock_guard<std::mutex> lock(mMutex);
665 if (!mTimeStatsTracker.count(layerId)) return;
666 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
667 layerRecord.badDesiredPresentFrames++;
668}
669
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800670void TimeStats::setDesiredTime(int32_t layerId, uint64_t frameNumber, nsecs_t desiredTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700671 if (!mEnabled.load()) return;
672
673 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800674 ALOGV("[%d]-[%" PRIu64 "]-DesiredTime[%" PRId64 "]", layerId, frameNumber, desiredTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700675
676 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800677 if (!mTimeStatsTracker.count(layerId)) return;
678 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700679 if (layerRecord.waitData < 0 ||
680 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
681 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700682 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700683 if (timeRecord.frameTime.frameNumber == frameNumber) {
684 timeRecord.frameTime.desiredTime = desiredTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700685 }
686}
687
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800688void TimeStats::setAcquireTime(int32_t layerId, uint64_t frameNumber, nsecs_t acquireTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700689 if (!mEnabled.load()) return;
690
691 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800692 ALOGV("[%d]-[%" PRIu64 "]-AcquireTime[%" PRId64 "]", layerId, frameNumber, acquireTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700693
694 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800695 if (!mTimeStatsTracker.count(layerId)) return;
696 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700697 if (layerRecord.waitData < 0 ||
698 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
699 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700700 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700701 if (timeRecord.frameTime.frameNumber == frameNumber) {
702 timeRecord.frameTime.acquireTime = acquireTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700703 }
704}
705
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800706void TimeStats::setAcquireFence(int32_t layerId, uint64_t frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700707 const std::shared_ptr<FenceTime>& acquireFence) {
708 if (!mEnabled.load()) return;
709
710 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800711 ALOGV("[%d]-[%" PRIu64 "]-AcquireFenceTime[%" PRId64 "]", layerId, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700712 acquireFence->getSignalTime());
713
714 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800715 if (!mTimeStatsTracker.count(layerId)) return;
716 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700717 if (layerRecord.waitData < 0 ||
718 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
719 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700720 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700721 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700722 timeRecord.acquireFence = acquireFence;
723 }
724}
725
Alec Mouri7d436ec2021-01-27 20:40:50 -0800726void TimeStats::setPresentTime(int32_t layerId, uint64_t frameNumber, nsecs_t presentTime,
727 Fps displayRefreshRate, std::optional<Fps> renderRate) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700728 if (!mEnabled.load()) return;
729
730 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800731 ALOGV("[%d]-[%" PRIu64 "]-PresentTime[%" PRId64 "]", layerId, frameNumber, presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700732
733 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800734 if (!mTimeStatsTracker.count(layerId)) return;
735 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700736 if (layerRecord.waitData < 0 ||
737 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
738 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700739 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700740 if (timeRecord.frameTime.frameNumber == frameNumber) {
741 timeRecord.frameTime.presentTime = presentTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700742 timeRecord.ready = true;
743 layerRecord.waitData++;
744 }
745
Alec Mouri7d436ec2021-01-27 20:40:50 -0800746 flushAvailableRecordsToStatsLocked(layerId, displayRefreshRate, renderRate);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700747}
748
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800749void TimeStats::setPresentFence(int32_t layerId, uint64_t frameNumber,
Alec Mouri7d436ec2021-01-27 20:40:50 -0800750 const std::shared_ptr<FenceTime>& presentFence,
751 Fps displayRefreshRate, std::optional<Fps> renderRate) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700752 if (!mEnabled.load()) return;
753
754 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800755 ALOGV("[%d]-[%" PRIu64 "]-PresentFenceTime[%" PRId64 "]", layerId, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700756 presentFence->getSignalTime());
757
758 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800759 if (!mTimeStatsTracker.count(layerId)) return;
760 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700761 if (layerRecord.waitData < 0 ||
762 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
763 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700764 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700765 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700766 timeRecord.presentFence = presentFence;
767 timeRecord.ready = true;
768 layerRecord.waitData++;
769 }
770
Alec Mouri7d436ec2021-01-27 20:40:50 -0800771 flushAvailableRecordsToStatsLocked(layerId, displayRefreshRate, renderRate);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700772}
773
Adithya Srinivasanead17162021-02-18 02:17:37 +0000774static const constexpr int32_t kValidJankyReason = JankType::DisplayHAL |
775 JankType::SurfaceFlingerCpuDeadlineMissed | JankType::SurfaceFlingerGpuDeadlineMissed |
776 JankType::AppDeadlineMissed | JankType::PredictionError |
777 JankType::SurfaceFlingerScheduling | JankType::BufferStuffing;
Alec Mouri363faf02021-01-29 16:34:55 -0800778
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 }
Adithya Srinivasanead17162021-02-18 02:17:37 +0000797 if ((reasons & JankType::PredictionError) != 0) {
798 t.jankPayload.totalSFPredictionError++;
799 }
800 if ((reasons & JankType::SurfaceFlingerScheduling) != 0) {
801 t.jankPayload.totalSFScheduling++;
802 }
803 if ((reasons & JankType::BufferStuffing) != 0) {
804 t.jankPayload.totalAppBufferStuffing++;
805 }
Alec Mouri9a29e672020-09-14 12:39:14 -0700806 }
807}
808
Alec Mouri363faf02021-01-29 16:34:55 -0800809void TimeStats::incrementJankyFrames(const JankyFramesInfo& info) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700810 if (!mEnabled.load()) return;
811
812 ATRACE_CALL();
813 std::lock_guard<std::mutex> lock(mMutex);
814
Alec Mouri542de112020-11-13 12:07:32 -0800815 // Only update layer stats if we're already tracking the layer in TimeStats.
816 // Otherwise, continue tracking the statistic but use a default layer name instead.
Alec Mouri9a29e672020-09-14 12:39:14 -0700817 // As an implementation detail, we do this because this method is expected to be
Alec Mouri542de112020-11-13 12:07:32 -0800818 // called from FrameTimeline, whose jank classification includes transaction jank
819 // that occurs without a buffer. But, in general those layer names are not suitable as
820 // aggregation keys: e.g., it's normal and expected for Window Manager to include the hash code
821 // for an animation leash. So while we can show that jank in dumpsys, aggregating based on the
822 // layer blows up the stats size, so as a workaround drop those stats. This assumes that
823 // TimeStats will flush the first present fence for a layer *before* FrameTimeline does so that
824 // the first jank record is not dropped.
Alec Mouri9a29e672020-09-14 12:39:14 -0700825
Alec Mouri542de112020-11-13 12:07:32 -0800826 static const std::string kDefaultLayerName = "none";
Alec Mouri7d436ec2021-01-27 20:40:50 -0800827
Alec Mouri363faf02021-01-29 16:34:55 -0800828 const int32_t refreshRateBucket =
829 clampToSmallestBucket(info.refreshRate, REFRESH_RATE_BUCKET_WIDTH);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800830 const int32_t renderRateBucket =
Alec Mouri363faf02021-01-29 16:34:55 -0800831 clampToSmallestBucket(info.renderRate ? *info.renderRate : info.refreshRate,
832 RENDER_RATE_BUCKET_WIDTH);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800833 const TimeStatsHelper::TimelineStatsKey timelineKey = {refreshRateBucket, renderRateBucket};
834
835 if (!mTimeStats.stats.count(timelineKey)) {
836 mTimeStats.stats[timelineKey].key = timelineKey;
Alec Mouri9a29e672020-09-14 12:39:14 -0700837 }
838
Alec Mouri7d436ec2021-01-27 20:40:50 -0800839 TimeStatsHelper::TimelineStats& timelineStats = mTimeStats.stats[timelineKey];
840
Alec Mouri363faf02021-01-29 16:34:55 -0800841 updateJankPayload<TimeStatsHelper::TimelineStats>(timelineStats, info.reasons);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800842
Alec Mouri363faf02021-01-29 16:34:55 -0800843 TimeStatsHelper::LayerStatsKey layerKey = {info.uid, info.layerName};
Alec Mouri7d436ec2021-01-27 20:40:50 -0800844 if (!timelineStats.stats.count(layerKey)) {
Alec Mouri363faf02021-01-29 16:34:55 -0800845 layerKey = {info.uid, kDefaultLayerName};
Alec Mouri7d436ec2021-01-27 20:40:50 -0800846 timelineStats.stats[layerKey].displayRefreshRateBucket = refreshRateBucket;
847 timelineStats.stats[layerKey].renderRateBucket = renderRateBucket;
Alec Mouri363faf02021-01-29 16:34:55 -0800848 timelineStats.stats[layerKey].uid = info.uid;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800849 timelineStats.stats[layerKey].layerName = kDefaultLayerName;
850 }
851
852 TimeStatsHelper::TimeStatsLayer& timeStatsLayer = timelineStats.stats[layerKey];
Alec Mouri363faf02021-01-29 16:34:55 -0800853 updateJankPayload<TimeStatsHelper::TimeStatsLayer>(timeStatsLayer, info.reasons);
854
855 if (info.reasons & kValidJankyReason) {
856 // TimeStats Histograms only retain positive values, so we don't need to check if these
857 // deadlines were really missed if we know that the frame had jank, since deadlines
858 // that were met will be dropped.
859 timelineStats.displayDeadlineDeltas.insert(static_cast<int32_t>(info.displayDeadlineDelta));
860 timelineStats.displayPresentDeltas.insert(static_cast<int32_t>(info.displayPresentJitter));
861 timeStatsLayer.deltas["appDeadlineDeltas"].insert(
862 static_cast<int32_t>(info.appDeadlineDelta));
863 }
Alec Mouri9a29e672020-09-14 12:39:14 -0700864}
865
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800866void TimeStats::onDestroy(int32_t layerId) {
Yiwei Zhangdc224042018-10-18 15:34:00 -0700867 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800868 ALOGV("[%d]-onDestroy", layerId);
Mikael Pessa90092f42019-08-26 17:22:04 -0700869 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800870 mTimeStatsTracker.erase(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700871}
872
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800873void TimeStats::removeTimeRecord(int32_t layerId, uint64_t frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700874 if (!mEnabled.load()) return;
875
876 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800877 ALOGV("[%d]-[%" PRIu64 "]-removeTimeRecord", layerId, frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700878
879 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800880 if (!mTimeStatsTracker.count(layerId)) return;
881 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700882 size_t removeAt = 0;
883 for (const TimeRecord& record : layerRecord.timeRecords) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700884 if (record.frameTime.frameNumber == frameNumber) break;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700885 removeAt++;
886 }
887 if (removeAt == layerRecord.timeRecords.size()) return;
888 layerRecord.timeRecords.erase(layerRecord.timeRecords.begin() + removeAt);
889 if (layerRecord.waitData > static_cast<int32_t>(removeAt)) {
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700890 layerRecord.waitData--;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700891 }
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700892 layerRecord.droppedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700893}
894
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700895void TimeStats::flushPowerTimeLocked() {
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700896 if (!mEnabled.load()) return;
897
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700898 nsecs_t curTime = systemTime();
899 // elapsedTime is in milliseconds.
900 int64_t elapsedTime = (curTime - mPowerTime.prevTime) / 1000000;
901
902 switch (mPowerTime.powerMode) {
Peiyong Lin65248e02020-04-18 21:15:07 -0700903 case PowerMode::ON:
Alec Mouri7d436ec2021-01-27 20:40:50 -0800904 mTimeStats.displayOnTimeLegacy += elapsedTime;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700905 break;
Peiyong Lin65248e02020-04-18 21:15:07 -0700906 case PowerMode::OFF:
907 case PowerMode::DOZE:
908 case PowerMode::DOZE_SUSPEND:
909 case PowerMode::ON_SUSPEND:
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700910 default:
911 break;
912 }
913
914 mPowerTime.prevTime = curTime;
915}
916
Peiyong Lin65248e02020-04-18 21:15:07 -0700917void TimeStats::setPowerMode(PowerMode powerMode) {
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700918 if (!mEnabled.load()) {
919 std::lock_guard<std::mutex> lock(mMutex);
920 mPowerTime.powerMode = powerMode;
921 return;
922 }
923
924 std::lock_guard<std::mutex> lock(mMutex);
925 if (powerMode == mPowerTime.powerMode) return;
926
927 flushPowerTimeLocked();
928 mPowerTime.powerMode = powerMode;
929}
930
Alec Mourifb571ea2019-01-24 18:42:10 -0800931void TimeStats::recordRefreshRate(uint32_t fps, nsecs_t duration) {
932 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800933 if (mTimeStats.refreshRateStatsLegacy.count(fps)) {
934 mTimeStats.refreshRateStatsLegacy[fps] += duration;
Alec Mourifb571ea2019-01-24 18:42:10 -0800935 } else {
Alec Mouri7d436ec2021-01-27 20:40:50 -0800936 mTimeStats.refreshRateStatsLegacy.insert({fps, duration});
Alec Mourifb571ea2019-01-24 18:42:10 -0800937 }
938}
939
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700940void TimeStats::flushAvailableGlobalRecordsToStatsLocked() {
941 ATRACE_CALL();
942
943 while (!mGlobalRecord.presentFences.empty()) {
944 const nsecs_t curPresentTime = mGlobalRecord.presentFences.front()->getSignalTime();
945 if (curPresentTime == Fence::SIGNAL_TIME_PENDING) break;
946
947 if (curPresentTime == Fence::SIGNAL_TIME_INVALID) {
948 ALOGE("GlobalPresentFence is invalid!");
949 mGlobalRecord.prevPresentTime = 0;
950 mGlobalRecord.presentFences.pop_front();
951 continue;
952 }
953
954 ALOGV("GlobalPresentFenceTime[%" PRId64 "]",
955 mGlobalRecord.presentFences.front()->getSignalTime());
956
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700957 if (mGlobalRecord.prevPresentTime != 0) {
958 const int32_t presentToPresentMs =
959 msBetween(mGlobalRecord.prevPresentTime, curPresentTime);
960 ALOGV("Global present2present[%d] prev[%" PRId64 "] curr[%" PRId64 "]",
961 presentToPresentMs, mGlobalRecord.prevPresentTime, curPresentTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800962 mTimeStats.presentToPresentLegacy.insert(presentToPresentMs);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700963 }
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700964
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700965 mGlobalRecord.prevPresentTime = curPresentTime;
966 mGlobalRecord.presentFences.pop_front();
967 }
Alec Mourie4034bb2019-11-19 12:45:54 -0800968 while (!mGlobalRecord.renderEngineDurations.empty()) {
969 const auto duration = mGlobalRecord.renderEngineDurations.front();
970 const auto& endTime = duration.endTime;
971
972 nsecs_t endNs = -1;
973
974 if (auto val = std::get_if<nsecs_t>(&endTime)) {
975 endNs = *val;
976 } else {
977 endNs = std::get<std::shared_ptr<FenceTime>>(endTime)->getSignalTime();
978 }
979
980 if (endNs == Fence::SIGNAL_TIME_PENDING) break;
981
982 if (endNs < 0) {
983 ALOGE("RenderEngineTiming is invalid!");
984 mGlobalRecord.renderEngineDurations.pop_front();
985 continue;
986 }
987
988 const int32_t renderEngineMs = msBetween(duration.startTime, endNs);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800989 mTimeStats.renderEngineTimingLegacy.insert(renderEngineMs);
Alec Mourie4034bb2019-11-19 12:45:54 -0800990
991 mGlobalRecord.renderEngineDurations.pop_front();
992 }
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700993}
994
995void TimeStats::setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) {
996 if (!mEnabled.load()) return;
997
998 ATRACE_CALL();
999 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -07001000 if (presentFence == nullptr || !presentFence->isValid()) {
1001 mGlobalRecord.prevPresentTime = 0;
1002 return;
1003 }
1004
Peiyong Lin65248e02020-04-18 21:15:07 -07001005 if (mPowerTime.powerMode != PowerMode::ON) {
1006 // Try flushing the last present fence on PowerMode::ON.
Yiwei Zhange5c49d52018-10-29 00:15:31 -07001007 flushAvailableGlobalRecordsToStatsLocked();
1008 mGlobalRecord.presentFences.clear();
Yiwei Zhangce6ebc02018-10-20 12:42:38 -07001009 mGlobalRecord.prevPresentTime = 0;
1010 return;
1011 }
1012
1013 if (mGlobalRecord.presentFences.size() == MAX_NUM_TIME_RECORDS) {
1014 // The front presentFence must be trapped in pending status in this
1015 // case. Try dequeuing the front one to recover.
1016 ALOGE("GlobalPresentFences is already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
1017 mGlobalRecord.prevPresentTime = 0;
1018 mGlobalRecord.presentFences.pop_front();
1019 }
1020
1021 mGlobalRecord.presentFences.emplace_back(presentFence);
1022 flushAvailableGlobalRecordsToStatsLocked();
1023}
1024
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001025void TimeStats::enable() {
1026 if (mEnabled.load()) return;
1027
1028 ATRACE_CALL();
1029
1030 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001031 mEnabled.store(true);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001032 mTimeStats.statsStartLegacy = static_cast<int64_t>(std::time(0));
Yiwei Zhang3a226d22018-10-16 09:23:03 -07001033 mPowerTime.prevTime = systemTime();
Yiwei Zhange5c49d52018-10-29 00:15:31 -07001034 ALOGD("Enabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001035}
1036
1037void TimeStats::disable() {
1038 if (!mEnabled.load()) return;
1039
1040 ATRACE_CALL();
1041
1042 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -07001043 flushPowerTimeLocked();
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001044 mEnabled.store(false);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001045 mTimeStats.statsEndLegacy = static_cast<int64_t>(std::time(0));
Yiwei Zhange5c49d52018-10-29 00:15:31 -07001046 ALOGD("Disabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001047}
1048
Alec Mouri8e2f31b2020-01-16 22:04:35 +00001049void TimeStats::clearAll() {
1050 std::lock_guard<std::mutex> lock(mMutex);
1051 clearGlobalLocked();
1052 clearLayersLocked();
1053}
1054
1055void TimeStats::clearGlobalLocked() {
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001056 ATRACE_CALL();
1057
Alec Mouri7d436ec2021-01-27 20:40:50 -08001058 mTimeStats.statsStartLegacy = (mEnabled.load() ? static_cast<int64_t>(std::time(0)) : 0);
1059 mTimeStats.statsEndLegacy = 0;
1060 mTimeStats.totalFramesLegacy = 0;
1061 mTimeStats.missedFramesLegacy = 0;
1062 mTimeStats.clientCompositionFramesLegacy = 0;
1063 mTimeStats.clientCompositionReusedFramesLegacy = 0;
1064 mTimeStats.refreshRateSwitchesLegacy = 0;
1065 mTimeStats.compositionStrategyChangesLegacy = 0;
1066 mTimeStats.displayEventConnectionsCountLegacy = 0;
1067 mTimeStats.displayOnTimeLegacy = 0;
1068 mTimeStats.presentToPresentLegacy.hist.clear();
1069 mTimeStats.frameDurationLegacy.hist.clear();
1070 mTimeStats.renderEngineTimingLegacy.hist.clear();
1071 mTimeStats.refreshRateStatsLegacy.clear();
Yiwei Zhang3a226d22018-10-16 09:23:03 -07001072 mPowerTime.prevTime = systemTime();
Alec Mouri56e63852021-03-09 18:17:25 -08001073 for (auto& globalRecord : mTimeStats.stats) {
1074 globalRecord.second.clearGlobals();
1075 }
Yiwei Zhange5c49d52018-10-29 00:15:31 -07001076 mGlobalRecord.prevPresentTime = 0;
1077 mGlobalRecord.presentFences.clear();
Alec Mouri8e2f31b2020-01-16 22:04:35 +00001078 ALOGD("Cleared global stats");
1079}
1080
1081void TimeStats::clearLayersLocked() {
1082 ATRACE_CALL();
1083
1084 mTimeStatsTracker.clear();
Alec Mouri56e63852021-03-09 18:17:25 -08001085
1086 for (auto& globalRecord : mTimeStats.stats) {
1087 globalRecord.second.stats.clear();
1088 }
Alec Mouri8e2f31b2020-01-16 22:04:35 +00001089 ALOGD("Cleared layer stats");
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001090}
1091
1092bool TimeStats::isEnabled() {
1093 return mEnabled.load();
1094}
1095
Yiwei Zhang5434a782018-12-05 18:06:32 -08001096void TimeStats::dump(bool asProto, std::optional<uint32_t> maxLayers, std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001097 ATRACE_CALL();
1098
1099 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001100 if (mTimeStats.statsStartLegacy == 0) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001101 return;
1102 }
1103
Alec Mouri7d436ec2021-01-27 20:40:50 -08001104 mTimeStats.statsEndLegacy = static_cast<int64_t>(std::time(0));
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001105
Yiwei Zhang3a226d22018-10-16 09:23:03 -07001106 flushPowerTimeLocked();
1107
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001108 if (asProto) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -07001109 ALOGD("Dumping TimeStats as proto");
Yiwei Zhangdc224042018-10-18 15:34:00 -07001110 SFTimeStatsGlobalProto timeStatsProto = mTimeStats.toProto(maxLayers);
Dominik Laskowski46470112019-08-02 13:13:11 -07001111 result.append(timeStatsProto.SerializeAsString());
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001112 } else {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -07001113 ALOGD("Dumping TimeStats as text");
Yiwei Zhang5434a782018-12-05 18:06:32 -08001114 result.append(mTimeStats.toString(maxLayers));
Yiwei Zhang8a4015c2018-05-08 16:03:47 -07001115 result.append("\n");
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001116 }
1117}
1118
Alec Mourifb571ea2019-01-24 18:42:10 -08001119} // namespace impl
1120
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001121} // namespace android