blob: c3f36712727006661fe39442835c53fb583d568e [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 =
150 histogramToProtoByteString(std::unordered_map<int32_t, int32_t>(),
151 mMaxPulledHistogramBuckets);
152 mStatsDelegate
153 ->statsEventWriteByteArray(event, (const uint8_t*)sfDeadlineMissedBytes.c_str(),
154 sfDeadlineMissedBytes.size()); // sf_deadline_misses
155 std::string sfPredictionErrorBytes =
156 histogramToProtoByteString(std::unordered_map<int32_t, int32_t>(),
157 mMaxPulledHistogramBuckets);
158 mStatsDelegate
159 ->statsEventWriteByteArray(event, (const uint8_t*)sfPredictionErrorBytes.c_str(),
160 sfPredictionErrorBytes.size()); // sf_prediction_errors
161 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
173 std::vector<TimeStatsHelper::TimeStatsLayer const*> 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
181 for (const auto& globalSlice : mTimeStats.stats) {
182 for (const auto& layerSlice : globalSlice.second.stats) {
183 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
197 for (const 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 =
237 histogramToProtoByteString(std::unordered_map<int32_t, int32_t>(),
238 mMaxPulledHistogramBuckets);
239 mStatsDelegate
240 ->statsEventWriteByteArray(event, (const uint8_t*)appDeadlineMissedBytes.c_str(),
241 appDeadlineMissedBytes.size()); // app_deadline_misses
242
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 Mouri9a29e672020-09-14 12:39:14 -0700775template <class T>
776static void updateJankPayload(T& t, int32_t reasons) {
777 t.jankPayload.totalFrames++;
778
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800779 static const constexpr int32_t kValidJankyReason = JankType::SurfaceFlingerCpuDeadlineMissed |
780 JankType::SurfaceFlingerGpuDeadlineMissed | JankType::AppDeadlineMissed |
781 JankType::DisplayHAL;
Alec Mouri9a29e672020-09-14 12:39:14 -0700782 if (reasons & kValidJankyReason) {
783 t.jankPayload.totalJankyFrames++;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800784 if ((reasons & JankType::SurfaceFlingerCpuDeadlineMissed) != 0) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700785 t.jankPayload.totalSFLongCpu++;
786 }
Jorim Jaggi5814ab82020-12-03 20:45:58 +0100787 if ((reasons & JankType::SurfaceFlingerGpuDeadlineMissed) != 0) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700788 t.jankPayload.totalSFLongGpu++;
789 }
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800790 if ((reasons & JankType::DisplayHAL) != 0) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700791 t.jankPayload.totalSFUnattributed++;
792 }
Jorim Jaggi5814ab82020-12-03 20:45:58 +0100793 if ((reasons & JankType::AppDeadlineMissed) != 0) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700794 t.jankPayload.totalAppUnattributed++;
795 }
796 }
797}
798
Alec Mouri7d436ec2021-01-27 20:40:50 -0800799void TimeStats::incrementJankyFrames(Fps refreshRate, std::optional<Fps> renderRate, uid_t uid,
800 const std::string& layerName, int32_t reasons) {
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
819 const int32_t refreshRateBucket = clampToSmallestBucket(refreshRate, REFRESH_RATE_BUCKET_WIDTH);
820 const int32_t renderRateBucket =
821 clampToSmallestBucket(renderRate ? *renderRate : refreshRate, RENDER_RATE_BUCKET_WIDTH);
822 const TimeStatsHelper::TimelineStatsKey timelineKey = {refreshRateBucket, renderRateBucket};
823
824 if (!mTimeStats.stats.count(timelineKey)) {
825 mTimeStats.stats[timelineKey].key = timelineKey;
Alec Mouri9a29e672020-09-14 12:39:14 -0700826 }
827
Alec Mouri7d436ec2021-01-27 20:40:50 -0800828 TimeStatsHelper::TimelineStats& timelineStats = mTimeStats.stats[timelineKey];
829
830 updateJankPayload<TimeStatsHelper::TimelineStats>(timelineStats, reasons);
831
832 TimeStatsHelper::LayerStatsKey layerKey = {uid, layerName};
833 if (!timelineStats.stats.count(layerKey)) {
834 layerKey = {uid, kDefaultLayerName};
835 timelineStats.stats[layerKey].displayRefreshRateBucket = refreshRateBucket;
836 timelineStats.stats[layerKey].renderRateBucket = renderRateBucket;
837 timelineStats.stats[layerKey].uid = uid;
838 timelineStats.stats[layerKey].layerName = kDefaultLayerName;
839 }
840
841 TimeStatsHelper::TimeStatsLayer& timeStatsLayer = timelineStats.stats[layerKey];
Alec Mouri9a29e672020-09-14 12:39:14 -0700842 updateJankPayload<TimeStatsHelper::TimeStatsLayer>(timeStatsLayer, reasons);
843}
844
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800845void TimeStats::onDestroy(int32_t layerId) {
Yiwei Zhangdc224042018-10-18 15:34:00 -0700846 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800847 ALOGV("[%d]-onDestroy", layerId);
Mikael Pessa90092f42019-08-26 17:22:04 -0700848 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800849 mTimeStatsTracker.erase(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700850}
851
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800852void TimeStats::removeTimeRecord(int32_t layerId, uint64_t frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700853 if (!mEnabled.load()) return;
854
855 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800856 ALOGV("[%d]-[%" PRIu64 "]-removeTimeRecord", layerId, frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700857
858 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800859 if (!mTimeStatsTracker.count(layerId)) return;
860 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700861 size_t removeAt = 0;
862 for (const TimeRecord& record : layerRecord.timeRecords) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700863 if (record.frameTime.frameNumber == frameNumber) break;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700864 removeAt++;
865 }
866 if (removeAt == layerRecord.timeRecords.size()) return;
867 layerRecord.timeRecords.erase(layerRecord.timeRecords.begin() + removeAt);
868 if (layerRecord.waitData > static_cast<int32_t>(removeAt)) {
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700869 layerRecord.waitData--;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700870 }
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700871 layerRecord.droppedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700872}
873
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700874void TimeStats::flushPowerTimeLocked() {
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700875 if (!mEnabled.load()) return;
876
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700877 nsecs_t curTime = systemTime();
878 // elapsedTime is in milliseconds.
879 int64_t elapsedTime = (curTime - mPowerTime.prevTime) / 1000000;
880
881 switch (mPowerTime.powerMode) {
Peiyong Lin65248e02020-04-18 21:15:07 -0700882 case PowerMode::ON:
Alec Mouri7d436ec2021-01-27 20:40:50 -0800883 mTimeStats.displayOnTimeLegacy += elapsedTime;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700884 break;
Peiyong Lin65248e02020-04-18 21:15:07 -0700885 case PowerMode::OFF:
886 case PowerMode::DOZE:
887 case PowerMode::DOZE_SUSPEND:
888 case PowerMode::ON_SUSPEND:
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700889 default:
890 break;
891 }
892
893 mPowerTime.prevTime = curTime;
894}
895
Peiyong Lin65248e02020-04-18 21:15:07 -0700896void TimeStats::setPowerMode(PowerMode powerMode) {
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700897 if (!mEnabled.load()) {
898 std::lock_guard<std::mutex> lock(mMutex);
899 mPowerTime.powerMode = powerMode;
900 return;
901 }
902
903 std::lock_guard<std::mutex> lock(mMutex);
904 if (powerMode == mPowerTime.powerMode) return;
905
906 flushPowerTimeLocked();
907 mPowerTime.powerMode = powerMode;
908}
909
Alec Mourifb571ea2019-01-24 18:42:10 -0800910void TimeStats::recordRefreshRate(uint32_t fps, nsecs_t duration) {
911 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800912 if (mTimeStats.refreshRateStatsLegacy.count(fps)) {
913 mTimeStats.refreshRateStatsLegacy[fps] += duration;
Alec Mourifb571ea2019-01-24 18:42:10 -0800914 } else {
Alec Mouri7d436ec2021-01-27 20:40:50 -0800915 mTimeStats.refreshRateStatsLegacy.insert({fps, duration});
Alec Mourifb571ea2019-01-24 18:42:10 -0800916 }
917}
918
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700919void TimeStats::flushAvailableGlobalRecordsToStatsLocked() {
920 ATRACE_CALL();
921
922 while (!mGlobalRecord.presentFences.empty()) {
923 const nsecs_t curPresentTime = mGlobalRecord.presentFences.front()->getSignalTime();
924 if (curPresentTime == Fence::SIGNAL_TIME_PENDING) break;
925
926 if (curPresentTime == Fence::SIGNAL_TIME_INVALID) {
927 ALOGE("GlobalPresentFence is invalid!");
928 mGlobalRecord.prevPresentTime = 0;
929 mGlobalRecord.presentFences.pop_front();
930 continue;
931 }
932
933 ALOGV("GlobalPresentFenceTime[%" PRId64 "]",
934 mGlobalRecord.presentFences.front()->getSignalTime());
935
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700936 if (mGlobalRecord.prevPresentTime != 0) {
937 const int32_t presentToPresentMs =
938 msBetween(mGlobalRecord.prevPresentTime, curPresentTime);
939 ALOGV("Global present2present[%d] prev[%" PRId64 "] curr[%" PRId64 "]",
940 presentToPresentMs, mGlobalRecord.prevPresentTime, curPresentTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800941 mTimeStats.presentToPresentLegacy.insert(presentToPresentMs);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700942 }
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700943
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700944 mGlobalRecord.prevPresentTime = curPresentTime;
945 mGlobalRecord.presentFences.pop_front();
946 }
Alec Mourie4034bb2019-11-19 12:45:54 -0800947 while (!mGlobalRecord.renderEngineDurations.empty()) {
948 const auto duration = mGlobalRecord.renderEngineDurations.front();
949 const auto& endTime = duration.endTime;
950
951 nsecs_t endNs = -1;
952
953 if (auto val = std::get_if<nsecs_t>(&endTime)) {
954 endNs = *val;
955 } else {
956 endNs = std::get<std::shared_ptr<FenceTime>>(endTime)->getSignalTime();
957 }
958
959 if (endNs == Fence::SIGNAL_TIME_PENDING) break;
960
961 if (endNs < 0) {
962 ALOGE("RenderEngineTiming is invalid!");
963 mGlobalRecord.renderEngineDurations.pop_front();
964 continue;
965 }
966
967 const int32_t renderEngineMs = msBetween(duration.startTime, endNs);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800968 mTimeStats.renderEngineTimingLegacy.insert(renderEngineMs);
Alec Mourie4034bb2019-11-19 12:45:54 -0800969
970 mGlobalRecord.renderEngineDurations.pop_front();
971 }
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700972}
973
974void TimeStats::setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) {
975 if (!mEnabled.load()) return;
976
977 ATRACE_CALL();
978 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700979 if (presentFence == nullptr || !presentFence->isValid()) {
980 mGlobalRecord.prevPresentTime = 0;
981 return;
982 }
983
Peiyong Lin65248e02020-04-18 21:15:07 -0700984 if (mPowerTime.powerMode != PowerMode::ON) {
985 // Try flushing the last present fence on PowerMode::ON.
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700986 flushAvailableGlobalRecordsToStatsLocked();
987 mGlobalRecord.presentFences.clear();
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700988 mGlobalRecord.prevPresentTime = 0;
989 return;
990 }
991
992 if (mGlobalRecord.presentFences.size() == MAX_NUM_TIME_RECORDS) {
993 // The front presentFence must be trapped in pending status in this
994 // case. Try dequeuing the front one to recover.
995 ALOGE("GlobalPresentFences is already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
996 mGlobalRecord.prevPresentTime = 0;
997 mGlobalRecord.presentFences.pop_front();
998 }
999
1000 mGlobalRecord.presentFences.emplace_back(presentFence);
1001 flushAvailableGlobalRecordsToStatsLocked();
1002}
1003
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001004void TimeStats::enable() {
1005 if (mEnabled.load()) return;
1006
1007 ATRACE_CALL();
1008
1009 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001010 mEnabled.store(true);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001011 mTimeStats.statsStartLegacy = static_cast<int64_t>(std::time(0));
Yiwei Zhang3a226d22018-10-16 09:23:03 -07001012 mPowerTime.prevTime = systemTime();
Yiwei Zhange5c49d52018-10-29 00:15:31 -07001013 ALOGD("Enabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001014}
1015
1016void TimeStats::disable() {
1017 if (!mEnabled.load()) return;
1018
1019 ATRACE_CALL();
1020
1021 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -07001022 flushPowerTimeLocked();
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001023 mEnabled.store(false);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001024 mTimeStats.statsEndLegacy = static_cast<int64_t>(std::time(0));
Yiwei Zhange5c49d52018-10-29 00:15:31 -07001025 ALOGD("Disabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001026}
1027
Alec Mouri8e2f31b2020-01-16 22:04:35 +00001028void TimeStats::clearAll() {
1029 std::lock_guard<std::mutex> lock(mMutex);
1030 clearGlobalLocked();
1031 clearLayersLocked();
1032}
1033
1034void TimeStats::clearGlobalLocked() {
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001035 ATRACE_CALL();
1036
Alec Mouri7d436ec2021-01-27 20:40:50 -08001037 mTimeStats.statsStartLegacy = (mEnabled.load() ? static_cast<int64_t>(std::time(0)) : 0);
1038 mTimeStats.statsEndLegacy = 0;
1039 mTimeStats.totalFramesLegacy = 0;
1040 mTimeStats.missedFramesLegacy = 0;
1041 mTimeStats.clientCompositionFramesLegacy = 0;
1042 mTimeStats.clientCompositionReusedFramesLegacy = 0;
1043 mTimeStats.refreshRateSwitchesLegacy = 0;
1044 mTimeStats.compositionStrategyChangesLegacy = 0;
1045 mTimeStats.displayEventConnectionsCountLegacy = 0;
1046 mTimeStats.displayOnTimeLegacy = 0;
1047 mTimeStats.presentToPresentLegacy.hist.clear();
1048 mTimeStats.frameDurationLegacy.hist.clear();
1049 mTimeStats.renderEngineTimingLegacy.hist.clear();
1050 mTimeStats.refreshRateStatsLegacy.clear();
Yiwei Zhang3a226d22018-10-16 09:23:03 -07001051 mPowerTime.prevTime = systemTime();
Yiwei Zhange5c49d52018-10-29 00:15:31 -07001052 mGlobalRecord.prevPresentTime = 0;
1053 mGlobalRecord.presentFences.clear();
Alec Mouri8e2f31b2020-01-16 22:04:35 +00001054 ALOGD("Cleared global stats");
1055}
1056
1057void TimeStats::clearLayersLocked() {
1058 ATRACE_CALL();
1059
1060 mTimeStatsTracker.clear();
1061 mTimeStats.stats.clear();
1062 ALOGD("Cleared layer stats");
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001063}
1064
1065bool TimeStats::isEnabled() {
1066 return mEnabled.load();
1067}
1068
Yiwei Zhang5434a782018-12-05 18:06:32 -08001069void TimeStats::dump(bool asProto, std::optional<uint32_t> maxLayers, std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001070 ATRACE_CALL();
1071
1072 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001073 if (mTimeStats.statsStartLegacy == 0) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001074 return;
1075 }
1076
Alec Mouri7d436ec2021-01-27 20:40:50 -08001077 mTimeStats.statsEndLegacy = static_cast<int64_t>(std::time(0));
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001078
Yiwei Zhang3a226d22018-10-16 09:23:03 -07001079 flushPowerTimeLocked();
1080
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001081 if (asProto) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -07001082 ALOGD("Dumping TimeStats as proto");
Yiwei Zhangdc224042018-10-18 15:34:00 -07001083 SFTimeStatsGlobalProto timeStatsProto = mTimeStats.toProto(maxLayers);
Dominik Laskowski46470112019-08-02 13:13:11 -07001084 result.append(timeStatsProto.SerializeAsString());
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001085 } else {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -07001086 ALOGD("Dumping TimeStats as text");
Yiwei Zhang5434a782018-12-05 18:06:32 -08001087 result.append(mTimeStats.toString(maxLayers));
Yiwei Zhang8a4015c2018-05-08 16:03:47 -07001088 result.append("\n");
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001089 }
1090}
1091
Alec Mourifb571ea2019-01-24 18:42:10 -08001092} // namespace impl
1093
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001094} // namespace android