blob: 3d82afa43a09f9cec24b4f5b2cf6aea809c49208 [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
Yiwei Zhang0102ad22018-05-02 17:37:17 -070022#include <android-base/stringprintf.h>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070023#include <log/log.h>
Tej Singhe2751772021-04-06 22:05:29 -070024#include <timestatsatomsproto/TimeStatsAtomsProtoHeader.h>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070025#include <utils/String8.h>
Yiwei Zhang3a226d22018-10-16 09:23:03 -070026#include <utils/Timers.h>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070027#include <utils/Trace.h>
28
29#include <algorithm>
Alec Mouri9519bf12019-11-15 16:54:44 -080030#include <chrono>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070031
Tej Singhe2751772021-04-06 22:05:29 -070032#include "TimeStats.h"
Alec Mouri9a29e672020-09-14 12:39:14 -070033#include "timestatsproto/TimeStatsHelper.h"
34
Yiwei Zhang0102ad22018-05-02 17:37:17 -070035namespace android {
36
Alec Mourifb571ea2019-01-24 18:42:10 -080037namespace impl {
38
Alec Mouri37384342020-01-02 17:23:37 -080039namespace {
Alec Mouri8e2f31b2020-01-16 22:04:35 +000040
Tej Singhe2751772021-04-06 22:05:29 -070041FrameTimingHistogram histogramToProto(const std::unordered_map<int32_t, int32_t>& histogram,
42 size_t maxPulledHistogramBuckets) {
Alec Mouri37384342020-01-02 17:23:37 -080043 auto buckets = std::vector<std::pair<int32_t, int32_t>>(histogram.begin(), histogram.end());
44 std::sort(buckets.begin(), buckets.end(),
45 [](std::pair<int32_t, int32_t>& left, std::pair<int32_t, int32_t>& right) {
46 return left.second > right.second;
47 });
48
Tej Singhe2751772021-04-06 22:05:29 -070049 FrameTimingHistogram histogramProto;
Alec Mouri37384342020-01-02 17:23:37 -080050 int histogramSize = 0;
51 for (const auto& bucket : buckets) {
52 if (++histogramSize > maxPulledHistogramBuckets) {
53 break;
54 }
Tej Singhe2751772021-04-06 22:05:29 -070055 histogramProto.add_time_millis_buckets((int32_t)bucket.first);
56 histogramProto.add_frame_counts((int64_t)bucket.second);
Alec Mouri37384342020-01-02 17:23:37 -080057 }
Tej Singhe2751772021-04-06 22:05:29 -070058 return histogramProto;
Alec Mouri37384342020-01-02 17:23:37 -080059}
Alec Mouri75de8f22021-01-20 14:53:44 -080060
Tej Singhe2751772021-04-06 22:05:29 -070061SurfaceflingerStatsLayerInfo_SetFrameRateVote frameRateVoteToProto(
62 const TimeStats::SetFrameRateVote& setFrameRateVote) {
63 using FrameRateCompatibilityEnum =
64 SurfaceflingerStatsLayerInfo::SetFrameRateVote::FrameRateCompatibility;
65 using SeamlessnessEnum = SurfaceflingerStatsLayerInfo::SetFrameRateVote::Seamlessness;
Alec Mouri75de8f22021-01-20 14:53:44 -080066
Tej Singhe2751772021-04-06 22:05:29 -070067 SurfaceflingerStatsLayerInfo_SetFrameRateVote proto;
68 proto.set_frame_rate(setFrameRateVote.frameRate);
69 proto.set_frame_rate_compatibility(
70 static_cast<FrameRateCompatibilityEnum>(setFrameRateVote.frameRateCompatibility));
71 proto.set_seamlessness(static_cast<SeamlessnessEnum>(setFrameRateVote.seamlessness));
72 return proto;
Alec Mouri75de8f22021-01-20 14:53:44 -080073}
Alec Mouri37384342020-01-02 17:23:37 -080074} // namespace
75
Tej Singhe2751772021-04-06 22:05:29 -070076bool TimeStats::populateGlobalAtom(std::string* pulledData) {
Alec Mouridfad9002020-02-12 17:49:09 -080077 std::lock_guard<std::mutex> lock(mMutex);
78
Alec Mouri7d436ec2021-01-27 20:40:50 -080079 if (mTimeStats.statsStartLegacy == 0) {
Tej Singhe2751772021-04-06 22:05:29 -070080 return false;
Alec Mouridfad9002020-02-12 17:49:09 -080081 }
82 flushPowerTimeLocked();
Tej Singhe2751772021-04-06 22:05:29 -070083 SurfaceflingerStatsGlobalInfoWrapper atomList;
Alec Mouri7d436ec2021-01-27 20:40:50 -080084 for (const auto& globalSlice : mTimeStats.stats) {
Tej Singhe2751772021-04-06 22:05:29 -070085 SurfaceflingerStatsGlobalInfo* atom = atomList.add_atom();
86 atom->set_total_frames(mTimeStats.totalFramesLegacy);
87 atom->set_missed_frames(mTimeStats.missedFramesLegacy);
88 atom->set_client_composition_frames(mTimeStats.clientCompositionFramesLegacy);
89 atom->set_display_on_millis(mTimeStats.displayOnTimeLegacy);
90 atom->set_animation_millis(mTimeStats.presentToPresentLegacy.totalTime());
91 atom->set_event_connection_count(mTimeStats.displayEventConnectionsCountLegacy);
92 *atom->mutable_frame_duration() =
93 histogramToProto(mTimeStats.frameDurationLegacy.hist, mMaxPulledHistogramBuckets);
94 *atom->mutable_render_engine_timing() =
95 histogramToProto(mTimeStats.renderEngineTimingLegacy.hist,
96 mMaxPulledHistogramBuckets);
97 atom->set_total_timeline_frames(globalSlice.second.jankPayload.totalFrames);
98 atom->set_total_janky_frames(globalSlice.second.jankPayload.totalJankyFrames);
99 atom->set_total_janky_frames_with_long_cpu(globalSlice.second.jankPayload.totalSFLongCpu);
100 atom->set_total_janky_frames_with_long_gpu(globalSlice.second.jankPayload.totalSFLongGpu);
101 atom->set_total_janky_frames_sf_unattributed(
102 globalSlice.second.jankPayload.totalSFUnattributed);
103 atom->set_total_janky_frames_app_unattributed(
104 globalSlice.second.jankPayload.totalAppUnattributed);
105 atom->set_total_janky_frames_sf_scheduling(
106 globalSlice.second.jankPayload.totalSFScheduling);
107 atom->set_total_jank_frames_sf_prediction_error(
108 globalSlice.second.jankPayload.totalSFPredictionError);
109 atom->set_total_jank_frames_app_buffer_stuffing(
110 globalSlice.second.jankPayload.totalAppBufferStuffing);
111 atom->set_display_refresh_rate_bucket(globalSlice.first.displayRefreshRateBucket);
112 *atom->mutable_sf_deadline_misses() =
113 histogramToProto(globalSlice.second.displayDeadlineDeltas.hist,
114 mMaxPulledHistogramBuckets);
115 *atom->mutable_sf_prediction_errors() =
116 histogramToProto(globalSlice.second.displayPresentDeltas.hist,
117 mMaxPulledHistogramBuckets);
118 atom->set_render_rate_bucket(globalSlice.first.renderRateBucket);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800119 }
120
Tej Singhe2751772021-04-06 22:05:29 -0700121 // Always clear data.
Alec Mouridfad9002020-02-12 17:49:09 -0800122 clearGlobalLocked();
123
Tej Singhe2751772021-04-06 22:05:29 -0700124 return atomList.SerializeToString(pulledData);
Alec Mouridfad9002020-02-12 17:49:09 -0800125}
126
Tej Singhe2751772021-04-06 22:05:29 -0700127bool TimeStats::populateLayerAtom(std::string* pulledData) {
Alec Mouri37384342020-01-02 17:23:37 -0800128 std::lock_guard<std::mutex> lock(mMutex);
129
Alec Mouri363faf02021-01-29 16:34:55 -0800130 std::vector<TimeStatsHelper::TimeStatsLayer*> dumpStats;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800131 uint32_t numLayers = 0;
132 for (const auto& globalSlice : mTimeStats.stats) {
133 numLayers += globalSlice.second.stats.size();
134 }
135
136 dumpStats.reserve(numLayers);
137
Alec Mouri363faf02021-01-29 16:34:55 -0800138 for (auto& globalSlice : mTimeStats.stats) {
139 for (auto& layerSlice : globalSlice.second.stats) {
Alec Mouri7d436ec2021-01-27 20:40:50 -0800140 dumpStats.push_back(&layerSlice.second);
141 }
Alec Mouri37384342020-01-02 17:23:37 -0800142 }
143
144 std::sort(dumpStats.begin(), dumpStats.end(),
145 [](TimeStatsHelper::TimeStatsLayer const* l,
146 TimeStatsHelper::TimeStatsLayer const* r) {
147 return l->totalFrames > r->totalFrames;
148 });
149
150 if (mMaxPulledLayers < dumpStats.size()) {
151 dumpStats.resize(mMaxPulledLayers);
152 }
153
Tej Singhe2751772021-04-06 22:05:29 -0700154 SurfaceflingerStatsLayerInfoWrapper atomList;
Alec Mouri363faf02021-01-29 16:34:55 -0800155 for (auto& layer : dumpStats) {
Tej Singhe2751772021-04-06 22:05:29 -0700156 SurfaceflingerStatsLayerInfo* atom = atomList.add_atom();
157 atom->set_layer_name(layer->layerName);
158 atom->set_total_frames(layer->totalFrames);
159 atom->set_dropped_frames(layer->droppedFrames);
160 const auto& present2PresentHist = layer->deltas.find("present2present");
161 if (present2PresentHist != layer->deltas.cend()) {
162 *atom->mutable_present_to_present() =
163 histogramToProto(present2PresentHist->second.hist, mMaxPulledHistogramBuckets);
164 }
165 const auto& post2presentHist = layer->deltas.find("post2present");
166 if (post2presentHist != layer->deltas.cend()) {
167 *atom->mutable_post_to_present() =
168 histogramToProto(post2presentHist->second.hist, mMaxPulledHistogramBuckets);
169 }
170 const auto& acquire2presentHist = layer->deltas.find("acquire2present");
171 if (acquire2presentHist != layer->deltas.cend()) {
172 *atom->mutable_acquire_to_present() =
173 histogramToProto(acquire2presentHist->second.hist, mMaxPulledHistogramBuckets);
174 }
175 const auto& latch2presentHist = layer->deltas.find("latch2present");
176 if (latch2presentHist != layer->deltas.cend()) {
177 *atom->mutable_latch_to_present() =
178 histogramToProto(latch2presentHist->second.hist, mMaxPulledHistogramBuckets);
179 }
180 const auto& desired2presentHist = layer->deltas.find("desired2present");
181 if (desired2presentHist != layer->deltas.cend()) {
182 *atom->mutable_desired_to_present() =
183 histogramToProto(desired2presentHist->second.hist, mMaxPulledHistogramBuckets);
184 }
185 const auto& post2acquireHist = layer->deltas.find("post2acquire");
186 if (post2acquireHist != layer->deltas.cend()) {
187 *atom->mutable_post_to_acquire() =
188 histogramToProto(post2acquireHist->second.hist, mMaxPulledHistogramBuckets);
Alec Mouri37384342020-01-02 17:23:37 -0800189 }
190
Tej Singhe2751772021-04-06 22:05:29 -0700191 atom->set_late_acquire_frames(layer->lateAcquireFrames);
192 atom->set_bad_desired_present_frames(layer->badDesiredPresentFrames);
193 atom->set_uid(layer->uid);
194 atom->set_total_timeline_frames(layer->jankPayload.totalFrames);
195 atom->set_total_janky_frames(layer->jankPayload.totalJankyFrames);
196 atom->set_total_janky_frames_with_long_cpu(layer->jankPayload.totalSFLongCpu);
197 atom->set_total_janky_frames_with_long_gpu(layer->jankPayload.totalSFLongGpu);
198 atom->set_total_janky_frames_sf_unattributed(layer->jankPayload.totalSFUnattributed);
199 atom->set_total_janky_frames_app_unattributed(layer->jankPayload.totalAppUnattributed);
200 atom->set_total_janky_frames_sf_scheduling(layer->jankPayload.totalSFScheduling);
201 atom->set_total_jank_frames_sf_prediction_error(layer->jankPayload.totalSFPredictionError);
202 atom->set_total_jank_frames_app_buffer_stuffing(layer->jankPayload.totalAppBufferStuffing);
203 atom->set_display_refresh_rate_bucket(layer->displayRefreshRateBucket);
204 atom->set_render_rate_bucket(layer->renderRateBucket);
205 *atom->mutable_set_frame_rate_vote() = frameRateVoteToProto(layer->setFrameRateVote);
206 *atom->mutable_app_deadline_misses() =
207 histogramToProto(layer->deltas["appDeadlineDeltas"].hist,
208 mMaxPulledHistogramBuckets);
Alec Mouri37384342020-01-02 17:23:37 -0800209 }
Tej Singhe2751772021-04-06 22:05:29 -0700210
211 // Always clear data.
Alec Mouri37384342020-01-02 17:23:37 -0800212 clearLayersLocked();
213
Tej Singhe2751772021-04-06 22:05:29 -0700214 return atomList.SerializeToString(pulledData);
Alec Mouri37384342020-01-02 17:23:37 -0800215}
216
Tej Singhe2751772021-04-06 22:05:29 -0700217TimeStats::TimeStats() : TimeStats(std::nullopt, std::nullopt) {}
Alec Mouri37384342020-01-02 17:23:37 -0800218
Tej Singhe2751772021-04-06 22:05:29 -0700219TimeStats::TimeStats(std::optional<size_t> maxPulledLayers,
Alec Mouri37384342020-01-02 17:23:37 -0800220 std::optional<size_t> maxPulledHistogramBuckets) {
Alec Mouri37384342020-01-02 17:23:37 -0800221 if (maxPulledLayers) {
222 mMaxPulledLayers = *maxPulledLayers;
223 }
224
225 if (maxPulledHistogramBuckets) {
226 mMaxPulledHistogramBuckets = *maxPulledHistogramBuckets;
227 }
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000228}
229
Tej Singhe2751772021-04-06 22:05:29 -0700230bool TimeStats::onPullAtom(const int atomId, std::string* pulledData) {
231 bool success = false;
232 if (atomId == 10062) { // SURFACEFLINGER_STATS_GLOBAL_INFO
233 success = populateGlobalAtom(pulledData);
234 } else if (atomId == 10063) { // SURFACEFLINGER_STATS_LAYER_INFO
235 success = populateLayerAtom(pulledData);
236 }
Alec Mouri3ecd5cd2020-01-29 12:53:07 -0800237
Tej Singhe2751772021-04-06 22:05:29 -0700238 // Enable timestats now. The first full pull for a given build is expected to
239 // have empty or very little stats, as stats are first enabled after the
240 // first pull is completed for either the global or layer stats.
241 enable();
242 return success;
Alec Mourib3885ad2019-09-06 17:08:55 -0700243}
244
Dominik Laskowskic2867142019-01-21 11:33:38 -0800245void TimeStats::parseArgs(bool asProto, const Vector<String16>& args, std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700246 ATRACE_CALL();
247
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700248 std::unordered_map<std::string, int32_t> argsMap;
Dominik Laskowskic2867142019-01-21 11:33:38 -0800249 for (size_t index = 0; index < args.size(); ++index) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700250 argsMap[std::string(String8(args[index]).c_str())] = index;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700251 }
252
253 if (argsMap.count("-disable")) {
254 disable();
255 }
256
257 if (argsMap.count("-dump")) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700258 std::optional<uint32_t> maxLayers = std::nullopt;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700259 auto iter = argsMap.find("-maxlayers");
260 if (iter != argsMap.end() && iter->second + 1 < static_cast<int32_t>(args.size())) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700261 int64_t value = strtol(String8(args[iter->second + 1]).c_str(), nullptr, 10);
262 value = std::clamp(value, int64_t(0), int64_t(UINT32_MAX));
263 maxLayers = static_cast<uint32_t>(value);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700264 }
265
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700266 dump(asProto, maxLayers, result);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700267 }
268
269 if (argsMap.count("-clear")) {
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000270 clearAll();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700271 }
272
273 if (argsMap.count("-enable")) {
274 enable();
275 }
276}
277
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700278std::string TimeStats::miniDump() {
279 ATRACE_CALL();
280
281 std::string result = "TimeStats miniDump:\n";
282 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange926ab52019-08-14 15:16:00 -0700283 android::base::StringAppendF(&result, "Number of layers currently being tracked is %zu\n",
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700284 mTimeStatsTracker.size());
Yiwei Zhange926ab52019-08-14 15:16:00 -0700285 android::base::StringAppendF(&result, "Number of layers in the stats pool is %zu\n",
286 mTimeStats.stats.size());
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700287 return result;
288}
289
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700290void TimeStats::incrementTotalFrames() {
291 if (!mEnabled.load()) return;
292
293 ATRACE_CALL();
294
295 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800296 mTimeStats.totalFramesLegacy++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700297}
298
Yiwei Zhang621f9d42018-05-07 10:40:55 -0700299void TimeStats::incrementMissedFrames() {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700300 if (!mEnabled.load()) return;
301
302 ATRACE_CALL();
303
304 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800305 mTimeStats.missedFramesLegacy++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700306}
307
308void TimeStats::incrementClientCompositionFrames() {
309 if (!mEnabled.load()) return;
310
311 ATRACE_CALL();
312
313 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800314 mTimeStats.clientCompositionFramesLegacy++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700315}
316
Vishnu Nair9b079a22020-01-21 14:36:08 -0800317void TimeStats::incrementClientCompositionReusedFrames() {
318 if (!mEnabled.load()) return;
319
320 ATRACE_CALL();
321
322 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800323 mTimeStats.clientCompositionReusedFramesLegacy++;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800324}
325
Alec Mouri8de697e2020-03-19 10:52:01 -0700326void TimeStats::incrementRefreshRateSwitches() {
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.refreshRateSwitchesLegacy++;
Alec Mouri8de697e2020-03-19 10:52:01 -0700333}
334
Alec Mouri8f7a0102020-04-15 12:11:10 -0700335void TimeStats::incrementCompositionStrategyChanges() {
336 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.compositionStrategyChangesLegacy++;
Alec Mouri8f7a0102020-04-15 12:11:10 -0700342}
343
Alec Mouri717bcb62020-02-10 17:07:19 -0800344void TimeStats::recordDisplayEventConnectionCount(int32_t count) {
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.displayEventConnectionsCountLegacy =
351 std::max(mTimeStats.displayEventConnectionsCountLegacy, count);
Alec Mouri717bcb62020-02-10 17:07:19 -0800352}
353
Alec Mouri9519bf12019-11-15 16:54:44 -0800354static int32_t msBetween(nsecs_t start, nsecs_t end) {
355 int64_t delta = std::chrono::duration_cast<std::chrono::milliseconds>(
356 std::chrono::nanoseconds(end - start))
357 .count();
358 delta = std::clamp(delta, int64_t(INT32_MIN), int64_t(INT32_MAX));
359 return static_cast<int32_t>(delta);
360}
361
362void TimeStats::recordFrameDuration(nsecs_t startTime, nsecs_t endTime) {
363 if (!mEnabled.load()) return;
364
365 std::lock_guard<std::mutex> lock(mMutex);
Peiyong Lin65248e02020-04-18 21:15:07 -0700366 if (mPowerTime.powerMode == PowerMode::ON) {
Alec Mouri7d436ec2021-01-27 20:40:50 -0800367 mTimeStats.frameDurationLegacy.insert(msBetween(startTime, endTime));
Alec Mouri9519bf12019-11-15 16:54:44 -0800368 }
369}
370
Alec Mourie4034bb2019-11-19 12:45:54 -0800371void TimeStats::recordRenderEngineDuration(nsecs_t startTime, nsecs_t endTime) {
372 if (!mEnabled.load()) return;
373
374 std::lock_guard<std::mutex> lock(mMutex);
375 if (mGlobalRecord.renderEngineDurations.size() == MAX_NUM_TIME_RECORDS) {
376 ALOGE("RenderEngineTimes are already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
377 mGlobalRecord.renderEngineDurations.pop_front();
378 }
379 mGlobalRecord.renderEngineDurations.push_back({startTime, endTime});
380}
381
382void TimeStats::recordRenderEngineDuration(nsecs_t startTime,
383 const std::shared_ptr<FenceTime>& endTime) {
384 if (!mEnabled.load()) return;
385
386 std::lock_guard<std::mutex> lock(mMutex);
387 if (mGlobalRecord.renderEngineDurations.size() == MAX_NUM_TIME_RECORDS) {
388 ALOGE("RenderEngineTimes are already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
389 mGlobalRecord.renderEngineDurations.pop_front();
390 }
391 mGlobalRecord.renderEngineDurations.push_back({startTime, endTime});
392}
393
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800394bool TimeStats::recordReadyLocked(int32_t layerId, TimeRecord* timeRecord) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700395 if (!timeRecord->ready) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800396 ALOGV("[%d]-[%" PRIu64 "]-presentFence is still not received", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700397 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700398 return false;
399 }
400
401 if (timeRecord->acquireFence != nullptr) {
402 if (timeRecord->acquireFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
403 return false;
404 }
405 if (timeRecord->acquireFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700406 timeRecord->frameTime.acquireTime = timeRecord->acquireFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700407 timeRecord->acquireFence = nullptr;
408 } else {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800409 ALOGV("[%d]-[%" PRIu64 "]-acquireFence signal time is invalid", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700410 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700411 }
412 }
413
414 if (timeRecord->presentFence != nullptr) {
415 if (timeRecord->presentFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
416 return false;
417 }
418 if (timeRecord->presentFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700419 timeRecord->frameTime.presentTime = timeRecord->presentFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700420 timeRecord->presentFence = nullptr;
421 } else {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800422 ALOGV("[%d]-[%" PRIu64 "]-presentFence signal time invalid", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700423 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700424 }
425 }
426
427 return true;
428}
429
Alec Mouri7d436ec2021-01-27 20:40:50 -0800430static int32_t clampToSmallestBucket(Fps fps, size_t bucketWidth) {
431 return (fps.getIntValue() / bucketWidth) * bucketWidth;
432}
433
434void TimeStats::flushAvailableRecordsToStatsLocked(int32_t layerId, Fps displayRefreshRate,
Ady Abraham8b9e6122021-01-26 19:11:45 -0800435 std::optional<Fps> renderRate,
436 SetFrameRateVote frameRateVote) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700437 ATRACE_CALL();
Ady Abraham8b9e6122021-01-26 19:11:45 -0800438 ALOGV("[%d]-flushAvailableRecordsToStatsLocked", layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700439
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800440 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700441 TimeRecord& prevTimeRecord = layerRecord.prevTimeRecord;
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700442 std::deque<TimeRecord>& timeRecords = layerRecord.timeRecords;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800443 const int32_t refreshRateBucket =
444 clampToSmallestBucket(displayRefreshRate, REFRESH_RATE_BUCKET_WIDTH);
445 const int32_t renderRateBucket =
446 clampToSmallestBucket(renderRate ? *renderRate : displayRefreshRate,
447 RENDER_RATE_BUCKET_WIDTH);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700448 while (!timeRecords.empty()) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800449 if (!recordReadyLocked(layerId, &timeRecords[0])) break;
450 ALOGV("[%d]-[%" PRIu64 "]-presentFenceTime[%" PRId64 "]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700451 timeRecords[0].frameTime.frameNumber, timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700452
453 if (prevTimeRecord.ready) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700454 uid_t uid = layerRecord.uid;
Yiwei Zhangeafa5cc2019-07-26 15:06:25 -0700455 const std::string& layerName = layerRecord.layerName;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800456 TimeStatsHelper::TimelineStatsKey timelineKey = {refreshRateBucket, renderRateBucket};
457 if (!mTimeStats.stats.count(timelineKey)) {
458 mTimeStats.stats[timelineKey].key = timelineKey;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700459 }
Alec Mouri7d436ec2021-01-27 20:40:50 -0800460
461 TimeStatsHelper::TimelineStats& displayStats = mTimeStats.stats[timelineKey];
462
463 TimeStatsHelper::LayerStatsKey layerKey = {uid, layerName};
464 if (!displayStats.stats.count(layerKey)) {
465 displayStats.stats[layerKey].displayRefreshRateBucket = refreshRateBucket;
466 displayStats.stats[layerKey].renderRateBucket = renderRateBucket;
467 displayStats.stats[layerKey].uid = uid;
468 displayStats.stats[layerKey].layerName = layerName;
469 }
Ady Abraham8b9e6122021-01-26 19:11:45 -0800470 if (frameRateVote.frameRate > 0.0f) {
471 displayStats.stats[layerKey].setFrameRateVote = frameRateVote;
472 }
Alec Mouri7d436ec2021-01-27 20:40:50 -0800473 TimeStatsHelper::TimeStatsLayer& timeStatsLayer = displayStats.stats[layerKey];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700474 timeStatsLayer.totalFrames++;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700475 timeStatsLayer.droppedFrames += layerRecord.droppedFrames;
Alec Mouri91f6df32020-01-30 08:48:58 -0800476 timeStatsLayer.lateAcquireFrames += layerRecord.lateAcquireFrames;
477 timeStatsLayer.badDesiredPresentFrames += layerRecord.badDesiredPresentFrames;
478
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700479 layerRecord.droppedFrames = 0;
Alec Mouri91f6df32020-01-30 08:48:58 -0800480 layerRecord.lateAcquireFrames = 0;
481 layerRecord.badDesiredPresentFrames = 0;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700482
483 const int32_t postToAcquireMs = msBetween(timeRecords[0].frameTime.postTime,
484 timeRecords[0].frameTime.acquireTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800485 ALOGV("[%d]-[%" PRIu64 "]-post2acquire[%d]", layerId,
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700486 timeRecords[0].frameTime.frameNumber, postToAcquireMs);
487 timeStatsLayer.deltas["post2acquire"].insert(postToAcquireMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700488
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700489 const int32_t postToPresentMs = msBetween(timeRecords[0].frameTime.postTime,
490 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800491 ALOGV("[%d]-[%" PRIu64 "]-post2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700492 timeRecords[0].frameTime.frameNumber, postToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700493 timeStatsLayer.deltas["post2present"].insert(postToPresentMs);
494
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700495 const int32_t acquireToPresentMs = msBetween(timeRecords[0].frameTime.acquireTime,
496 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800497 ALOGV("[%d]-[%" PRIu64 "]-acquire2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700498 timeRecords[0].frameTime.frameNumber, acquireToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700499 timeStatsLayer.deltas["acquire2present"].insert(acquireToPresentMs);
500
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700501 const int32_t latchToPresentMs = msBetween(timeRecords[0].frameTime.latchTime,
502 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800503 ALOGV("[%d]-[%" PRIu64 "]-latch2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700504 timeRecords[0].frameTime.frameNumber, latchToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700505 timeStatsLayer.deltas["latch2present"].insert(latchToPresentMs);
506
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700507 const int32_t desiredToPresentMs = msBetween(timeRecords[0].frameTime.desiredTime,
508 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800509 ALOGV("[%d]-[%" PRIu64 "]-desired2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700510 timeRecords[0].frameTime.frameNumber, desiredToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700511 timeStatsLayer.deltas["desired2present"].insert(desiredToPresentMs);
512
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700513 const int32_t presentToPresentMs = msBetween(prevTimeRecord.frameTime.presentTime,
514 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800515 ALOGV("[%d]-[%" PRIu64 "]-present2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700516 timeRecords[0].frameTime.frameNumber, presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700517 timeStatsLayer.deltas["present2present"].insert(presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700518 }
519 prevTimeRecord = timeRecords[0];
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700520 timeRecords.pop_front();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700521 layerRecord.waitData--;
522 }
523}
524
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700525static constexpr const char* kPopupWindowPrefix = "PopupWindow";
526static const size_t kMinLenLayerName = std::strlen(kPopupWindowPrefix);
Yiwei Zhangbd408322018-10-15 18:31:53 -0700527
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700528// Avoid tracking the "PopupWindow:<random hash>#<number>" layers
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700529static bool layerNameIsValid(const std::string& layerName) {
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700530 return layerName.length() >= kMinLenLayerName &&
531 layerName.compare(0, kMinLenLayerName, kPopupWindowPrefix) != 0;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700532}
533
Alec Mouri9a29e672020-09-14 12:39:14 -0700534bool TimeStats::canAddNewAggregatedStats(uid_t uid, const std::string& layerName) {
Alec Mouri7d436ec2021-01-27 20:40:50 -0800535 uint32_t layerRecords = 0;
536 for (const auto& record : mTimeStats.stats) {
537 if (record.second.stats.count({uid, layerName}) > 0) {
538 return true;
539 }
540
541 layerRecords += record.second.stats.size();
542 }
543
544 return mTimeStats.stats.size() < MAX_NUM_LAYER_STATS;
Alec Mouri9a29e672020-09-14 12:39:14 -0700545}
546
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800547void TimeStats::setPostTime(int32_t layerId, uint64_t frameNumber, const std::string& layerName,
Alec Mouri9a29e672020-09-14 12:39:14 -0700548 uid_t uid, nsecs_t postTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700549 if (!mEnabled.load()) return;
550
551 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800552 ALOGV("[%d]-[%" PRIu64 "]-[%s]-PostTime[%" PRId64 "]", layerId, frameNumber, layerName.c_str(),
Yiwei Zhang8e8fe522018-11-02 18:34:07 -0700553 postTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700554
555 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri9a29e672020-09-14 12:39:14 -0700556 if (!canAddNewAggregatedStats(uid, layerName)) {
Yiwei Zhange926ab52019-08-14 15:16:00 -0700557 return;
558 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800559 if (!mTimeStatsTracker.count(layerId) && mTimeStatsTracker.size() < MAX_NUM_LAYER_RECORDS &&
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700560 layerNameIsValid(layerName)) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700561 mTimeStatsTracker[layerId].uid = uid;
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800562 mTimeStatsTracker[layerId].layerName = layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700563 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800564 if (!mTimeStatsTracker.count(layerId)) return;
565 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700566 if (layerRecord.timeRecords.size() == MAX_NUM_TIME_RECORDS) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800567 ALOGE("[%d]-[%s]-timeRecords is at its maximum size[%zu]. Ignore this when unittesting.",
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800568 layerId, layerRecord.layerName.c_str(), MAX_NUM_TIME_RECORDS);
569 mTimeStatsTracker.erase(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700570 return;
571 }
572 // For most media content, the acquireFence is invalid because the buffer is
573 // ready at the queueBuffer stage. In this case, acquireTime should be given
574 // a default value as postTime.
575 TimeRecord timeRecord = {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700576 .frameTime =
577 {
578 .frameNumber = frameNumber,
579 .postTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800580 .latchTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700581 .acquireTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800582 .desiredTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700583 },
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700584 };
585 layerRecord.timeRecords.push_back(timeRecord);
586 if (layerRecord.waitData < 0 ||
587 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
588 layerRecord.waitData = layerRecord.timeRecords.size() - 1;
589}
590
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800591void TimeStats::setLatchTime(int32_t layerId, uint64_t frameNumber, nsecs_t latchTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700592 if (!mEnabled.load()) return;
593
594 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800595 ALOGV("[%d]-[%" PRIu64 "]-LatchTime[%" PRId64 "]", layerId, frameNumber, latchTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700596
597 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800598 if (!mTimeStatsTracker.count(layerId)) return;
599 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700600 if (layerRecord.waitData < 0 ||
601 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
602 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700603 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700604 if (timeRecord.frameTime.frameNumber == frameNumber) {
605 timeRecord.frameTime.latchTime = latchTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700606 }
607}
608
Alec Mouri91f6df32020-01-30 08:48:58 -0800609void TimeStats::incrementLatchSkipped(int32_t layerId, LatchSkipReason reason) {
610 if (!mEnabled.load()) return;
611
612 ATRACE_CALL();
613 ALOGV("[%d]-LatchSkipped-Reason[%d]", layerId,
614 static_cast<std::underlying_type<LatchSkipReason>::type>(reason));
615
616 std::lock_guard<std::mutex> lock(mMutex);
617 if (!mTimeStatsTracker.count(layerId)) return;
618 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
619
620 switch (reason) {
621 case LatchSkipReason::LateAcquire:
622 layerRecord.lateAcquireFrames++;
623 break;
624 }
625}
626
627void TimeStats::incrementBadDesiredPresent(int32_t layerId) {
628 if (!mEnabled.load()) return;
629
630 ATRACE_CALL();
631 ALOGV("[%d]-BadDesiredPresent", layerId);
632
633 std::lock_guard<std::mutex> lock(mMutex);
634 if (!mTimeStatsTracker.count(layerId)) return;
635 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
636 layerRecord.badDesiredPresentFrames++;
637}
638
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800639void TimeStats::setDesiredTime(int32_t layerId, uint64_t frameNumber, nsecs_t desiredTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700640 if (!mEnabled.load()) return;
641
642 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800643 ALOGV("[%d]-[%" PRIu64 "]-DesiredTime[%" PRId64 "]", layerId, frameNumber, desiredTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700644
645 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800646 if (!mTimeStatsTracker.count(layerId)) return;
647 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700648 if (layerRecord.waitData < 0 ||
649 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
650 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700651 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700652 if (timeRecord.frameTime.frameNumber == frameNumber) {
653 timeRecord.frameTime.desiredTime = desiredTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700654 }
655}
656
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800657void TimeStats::setAcquireTime(int32_t layerId, uint64_t frameNumber, nsecs_t acquireTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700658 if (!mEnabled.load()) return;
659
660 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800661 ALOGV("[%d]-[%" PRIu64 "]-AcquireTime[%" PRId64 "]", layerId, frameNumber, acquireTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700662
663 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800664 if (!mTimeStatsTracker.count(layerId)) return;
665 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700666 if (layerRecord.waitData < 0 ||
667 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
668 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700669 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700670 if (timeRecord.frameTime.frameNumber == frameNumber) {
671 timeRecord.frameTime.acquireTime = acquireTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700672 }
673}
674
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800675void TimeStats::setAcquireFence(int32_t layerId, uint64_t frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700676 const std::shared_ptr<FenceTime>& acquireFence) {
677 if (!mEnabled.load()) return;
678
679 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800680 ALOGV("[%d]-[%" PRIu64 "]-AcquireFenceTime[%" PRId64 "]", layerId, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700681 acquireFence->getSignalTime());
682
683 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800684 if (!mTimeStatsTracker.count(layerId)) return;
685 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700686 if (layerRecord.waitData < 0 ||
687 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
688 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700689 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700690 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700691 timeRecord.acquireFence = acquireFence;
692 }
693}
694
Alec Mouri7d436ec2021-01-27 20:40:50 -0800695void TimeStats::setPresentTime(int32_t layerId, uint64_t frameNumber, nsecs_t presentTime,
Ady Abraham8b9e6122021-01-26 19:11:45 -0800696 Fps displayRefreshRate, std::optional<Fps> renderRate,
697 SetFrameRateVote frameRateVote) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700698 if (!mEnabled.load()) return;
699
700 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800701 ALOGV("[%d]-[%" PRIu64 "]-PresentTime[%" PRId64 "]", layerId, frameNumber, presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700702
703 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800704 if (!mTimeStatsTracker.count(layerId)) return;
705 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700706 if (layerRecord.waitData < 0 ||
707 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
708 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700709 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700710 if (timeRecord.frameTime.frameNumber == frameNumber) {
711 timeRecord.frameTime.presentTime = presentTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700712 timeRecord.ready = true;
713 layerRecord.waitData++;
714 }
715
Ady Abraham8b9e6122021-01-26 19:11:45 -0800716 flushAvailableRecordsToStatsLocked(layerId, displayRefreshRate, renderRate, frameRateVote);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700717}
718
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800719void TimeStats::setPresentFence(int32_t layerId, uint64_t frameNumber,
Alec Mouri7d436ec2021-01-27 20:40:50 -0800720 const std::shared_ptr<FenceTime>& presentFence,
Ady Abraham8b9e6122021-01-26 19:11:45 -0800721 Fps displayRefreshRate, std::optional<Fps> renderRate,
722 SetFrameRateVote frameRateVote) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700723 if (!mEnabled.load()) return;
724
725 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800726 ALOGV("[%d]-[%" PRIu64 "]-PresentFenceTime[%" PRId64 "]", layerId, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700727 presentFence->getSignalTime());
728
729 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800730 if (!mTimeStatsTracker.count(layerId)) return;
731 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700732 if (layerRecord.waitData < 0 ||
733 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
734 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700735 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700736 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700737 timeRecord.presentFence = presentFence;
738 timeRecord.ready = true;
739 layerRecord.waitData++;
740 }
741
Ady Abraham8b9e6122021-01-26 19:11:45 -0800742 flushAvailableRecordsToStatsLocked(layerId, displayRefreshRate, renderRate, frameRateVote);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700743}
744
Adithya Srinivasanead17162021-02-18 02:17:37 +0000745static const constexpr int32_t kValidJankyReason = JankType::DisplayHAL |
746 JankType::SurfaceFlingerCpuDeadlineMissed | JankType::SurfaceFlingerGpuDeadlineMissed |
747 JankType::AppDeadlineMissed | JankType::PredictionError |
Adithya Srinivasan53e5c402021-04-16 17:34:30 +0000748 JankType::SurfaceFlingerScheduling;
Alec Mouri363faf02021-01-29 16:34:55 -0800749
Alec Mouri9a29e672020-09-14 12:39:14 -0700750template <class T>
751static void updateJankPayload(T& t, int32_t reasons) {
752 t.jankPayload.totalFrames++;
753
Alec Mouri9a29e672020-09-14 12:39:14 -0700754 if (reasons & kValidJankyReason) {
755 t.jankPayload.totalJankyFrames++;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800756 if ((reasons & JankType::SurfaceFlingerCpuDeadlineMissed) != 0) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700757 t.jankPayload.totalSFLongCpu++;
758 }
Jorim Jaggi5814ab82020-12-03 20:45:58 +0100759 if ((reasons & JankType::SurfaceFlingerGpuDeadlineMissed) != 0) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700760 t.jankPayload.totalSFLongGpu++;
761 }
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800762 if ((reasons & JankType::DisplayHAL) != 0) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700763 t.jankPayload.totalSFUnattributed++;
764 }
Jorim Jaggi5814ab82020-12-03 20:45:58 +0100765 if ((reasons & JankType::AppDeadlineMissed) != 0) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700766 t.jankPayload.totalAppUnattributed++;
767 }
Adithya Srinivasanead17162021-02-18 02:17:37 +0000768 if ((reasons & JankType::PredictionError) != 0) {
769 t.jankPayload.totalSFPredictionError++;
770 }
771 if ((reasons & JankType::SurfaceFlingerScheduling) != 0) {
772 t.jankPayload.totalSFScheduling++;
773 }
Adithya Srinivasan53e5c402021-04-16 17:34:30 +0000774 }
775
776 // We want to track BufferStuffing separately as it can provide info on latency issues
777 if (reasons & JankType::BufferStuffing) {
778 t.jankPayload.totalAppBufferStuffing++;
Alec Mouri9a29e672020-09-14 12:39:14 -0700779 }
780}
781
Alec Mouri363faf02021-01-29 16:34:55 -0800782void TimeStats::incrementJankyFrames(const JankyFramesInfo& info) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700783 if (!mEnabled.load()) return;
784
785 ATRACE_CALL();
786 std::lock_guard<std::mutex> lock(mMutex);
787
Alec Mouri542de112020-11-13 12:07:32 -0800788 // Only update layer stats if we're already tracking the layer in TimeStats.
789 // Otherwise, continue tracking the statistic but use a default layer name instead.
Alec Mouri9a29e672020-09-14 12:39:14 -0700790 // As an implementation detail, we do this because this method is expected to be
Alec Mouri542de112020-11-13 12:07:32 -0800791 // called from FrameTimeline, whose jank classification includes transaction jank
792 // that occurs without a buffer. But, in general those layer names are not suitable as
793 // aggregation keys: e.g., it's normal and expected for Window Manager to include the hash code
794 // for an animation leash. So while we can show that jank in dumpsys, aggregating based on the
795 // layer blows up the stats size, so as a workaround drop those stats. This assumes that
796 // TimeStats will flush the first present fence for a layer *before* FrameTimeline does so that
797 // the first jank record is not dropped.
Alec Mouri9a29e672020-09-14 12:39:14 -0700798
Alec Mouri542de112020-11-13 12:07:32 -0800799 static const std::string kDefaultLayerName = "none";
Alec Mouri7d436ec2021-01-27 20:40:50 -0800800
Alec Mouri363faf02021-01-29 16:34:55 -0800801 const int32_t refreshRateBucket =
802 clampToSmallestBucket(info.refreshRate, REFRESH_RATE_BUCKET_WIDTH);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800803 const int32_t renderRateBucket =
Alec Mouri363faf02021-01-29 16:34:55 -0800804 clampToSmallestBucket(info.renderRate ? *info.renderRate : info.refreshRate,
805 RENDER_RATE_BUCKET_WIDTH);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800806 const TimeStatsHelper::TimelineStatsKey timelineKey = {refreshRateBucket, renderRateBucket};
807
808 if (!mTimeStats.stats.count(timelineKey)) {
809 mTimeStats.stats[timelineKey].key = timelineKey;
Alec Mouri9a29e672020-09-14 12:39:14 -0700810 }
811
Alec Mouri7d436ec2021-01-27 20:40:50 -0800812 TimeStatsHelper::TimelineStats& timelineStats = mTimeStats.stats[timelineKey];
813
Alec Mouri363faf02021-01-29 16:34:55 -0800814 updateJankPayload<TimeStatsHelper::TimelineStats>(timelineStats, info.reasons);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800815
Alec Mouri363faf02021-01-29 16:34:55 -0800816 TimeStatsHelper::LayerStatsKey layerKey = {info.uid, info.layerName};
Alec Mouri7d436ec2021-01-27 20:40:50 -0800817 if (!timelineStats.stats.count(layerKey)) {
Alec Mouri363faf02021-01-29 16:34:55 -0800818 layerKey = {info.uid, kDefaultLayerName};
Alec Mouri7d436ec2021-01-27 20:40:50 -0800819 timelineStats.stats[layerKey].displayRefreshRateBucket = refreshRateBucket;
820 timelineStats.stats[layerKey].renderRateBucket = renderRateBucket;
Alec Mouri363faf02021-01-29 16:34:55 -0800821 timelineStats.stats[layerKey].uid = info.uid;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800822 timelineStats.stats[layerKey].layerName = kDefaultLayerName;
823 }
824
825 TimeStatsHelper::TimeStatsLayer& timeStatsLayer = timelineStats.stats[layerKey];
Alec Mouri363faf02021-01-29 16:34:55 -0800826 updateJankPayload<TimeStatsHelper::TimeStatsLayer>(timeStatsLayer, info.reasons);
827
828 if (info.reasons & kValidJankyReason) {
829 // TimeStats Histograms only retain positive values, so we don't need to check if these
830 // deadlines were really missed if we know that the frame had jank, since deadlines
831 // that were met will be dropped.
832 timelineStats.displayDeadlineDeltas.insert(static_cast<int32_t>(info.displayDeadlineDelta));
833 timelineStats.displayPresentDeltas.insert(static_cast<int32_t>(info.displayPresentJitter));
834 timeStatsLayer.deltas["appDeadlineDeltas"].insert(
835 static_cast<int32_t>(info.appDeadlineDelta));
836 }
Alec Mouri9a29e672020-09-14 12:39:14 -0700837}
838
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800839void TimeStats::onDestroy(int32_t layerId) {
Yiwei Zhangdc224042018-10-18 15:34:00 -0700840 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800841 ALOGV("[%d]-onDestroy", layerId);
Mikael Pessa90092f42019-08-26 17:22:04 -0700842 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800843 mTimeStatsTracker.erase(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700844}
845
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800846void TimeStats::removeTimeRecord(int32_t layerId, uint64_t frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700847 if (!mEnabled.load()) return;
848
849 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800850 ALOGV("[%d]-[%" PRIu64 "]-removeTimeRecord", layerId, frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700851
852 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800853 if (!mTimeStatsTracker.count(layerId)) return;
854 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700855 size_t removeAt = 0;
856 for (const TimeRecord& record : layerRecord.timeRecords) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700857 if (record.frameTime.frameNumber == frameNumber) break;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700858 removeAt++;
859 }
860 if (removeAt == layerRecord.timeRecords.size()) return;
861 layerRecord.timeRecords.erase(layerRecord.timeRecords.begin() + removeAt);
862 if (layerRecord.waitData > static_cast<int32_t>(removeAt)) {
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700863 layerRecord.waitData--;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700864 }
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700865 layerRecord.droppedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700866}
867
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700868void TimeStats::flushPowerTimeLocked() {
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700869 if (!mEnabled.load()) return;
870
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700871 nsecs_t curTime = systemTime();
872 // elapsedTime is in milliseconds.
873 int64_t elapsedTime = (curTime - mPowerTime.prevTime) / 1000000;
874
875 switch (mPowerTime.powerMode) {
Peiyong Lin65248e02020-04-18 21:15:07 -0700876 case PowerMode::ON:
Alec Mouri7d436ec2021-01-27 20:40:50 -0800877 mTimeStats.displayOnTimeLegacy += elapsedTime;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700878 break;
Peiyong Lin65248e02020-04-18 21:15:07 -0700879 case PowerMode::OFF:
880 case PowerMode::DOZE:
881 case PowerMode::DOZE_SUSPEND:
882 case PowerMode::ON_SUSPEND:
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700883 default:
884 break;
885 }
886
887 mPowerTime.prevTime = curTime;
888}
889
Peiyong Lin65248e02020-04-18 21:15:07 -0700890void TimeStats::setPowerMode(PowerMode powerMode) {
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700891 if (!mEnabled.load()) {
892 std::lock_guard<std::mutex> lock(mMutex);
893 mPowerTime.powerMode = powerMode;
894 return;
895 }
896
897 std::lock_guard<std::mutex> lock(mMutex);
898 if (powerMode == mPowerTime.powerMode) return;
899
900 flushPowerTimeLocked();
901 mPowerTime.powerMode = powerMode;
902}
903
Alec Mourifb571ea2019-01-24 18:42:10 -0800904void TimeStats::recordRefreshRate(uint32_t fps, nsecs_t duration) {
905 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800906 if (mTimeStats.refreshRateStatsLegacy.count(fps)) {
907 mTimeStats.refreshRateStatsLegacy[fps] += duration;
Alec Mourifb571ea2019-01-24 18:42:10 -0800908 } else {
Alec Mouri7d436ec2021-01-27 20:40:50 -0800909 mTimeStats.refreshRateStatsLegacy.insert({fps, duration});
Alec Mourifb571ea2019-01-24 18:42:10 -0800910 }
911}
912
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700913void TimeStats::flushAvailableGlobalRecordsToStatsLocked() {
914 ATRACE_CALL();
915
916 while (!mGlobalRecord.presentFences.empty()) {
917 const nsecs_t curPresentTime = mGlobalRecord.presentFences.front()->getSignalTime();
918 if (curPresentTime == Fence::SIGNAL_TIME_PENDING) break;
919
920 if (curPresentTime == Fence::SIGNAL_TIME_INVALID) {
921 ALOGE("GlobalPresentFence is invalid!");
922 mGlobalRecord.prevPresentTime = 0;
923 mGlobalRecord.presentFences.pop_front();
924 continue;
925 }
926
927 ALOGV("GlobalPresentFenceTime[%" PRId64 "]",
928 mGlobalRecord.presentFences.front()->getSignalTime());
929
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700930 if (mGlobalRecord.prevPresentTime != 0) {
931 const int32_t presentToPresentMs =
932 msBetween(mGlobalRecord.prevPresentTime, curPresentTime);
933 ALOGV("Global present2present[%d] prev[%" PRId64 "] curr[%" PRId64 "]",
934 presentToPresentMs, mGlobalRecord.prevPresentTime, curPresentTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800935 mTimeStats.presentToPresentLegacy.insert(presentToPresentMs);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700936 }
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700937
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700938 mGlobalRecord.prevPresentTime = curPresentTime;
939 mGlobalRecord.presentFences.pop_front();
940 }
Alec Mourie4034bb2019-11-19 12:45:54 -0800941 while (!mGlobalRecord.renderEngineDurations.empty()) {
942 const auto duration = mGlobalRecord.renderEngineDurations.front();
943 const auto& endTime = duration.endTime;
944
945 nsecs_t endNs = -1;
946
947 if (auto val = std::get_if<nsecs_t>(&endTime)) {
948 endNs = *val;
949 } else {
950 endNs = std::get<std::shared_ptr<FenceTime>>(endTime)->getSignalTime();
951 }
952
953 if (endNs == Fence::SIGNAL_TIME_PENDING) break;
954
955 if (endNs < 0) {
956 ALOGE("RenderEngineTiming is invalid!");
957 mGlobalRecord.renderEngineDurations.pop_front();
958 continue;
959 }
960
961 const int32_t renderEngineMs = msBetween(duration.startTime, endNs);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800962 mTimeStats.renderEngineTimingLegacy.insert(renderEngineMs);
Alec Mourie4034bb2019-11-19 12:45:54 -0800963
964 mGlobalRecord.renderEngineDurations.pop_front();
965 }
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700966}
967
968void TimeStats::setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) {
969 if (!mEnabled.load()) return;
970
971 ATRACE_CALL();
972 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700973 if (presentFence == nullptr || !presentFence->isValid()) {
974 mGlobalRecord.prevPresentTime = 0;
975 return;
976 }
977
Peiyong Lin65248e02020-04-18 21:15:07 -0700978 if (mPowerTime.powerMode != PowerMode::ON) {
979 // Try flushing the last present fence on PowerMode::ON.
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700980 flushAvailableGlobalRecordsToStatsLocked();
981 mGlobalRecord.presentFences.clear();
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700982 mGlobalRecord.prevPresentTime = 0;
983 return;
984 }
985
986 if (mGlobalRecord.presentFences.size() == MAX_NUM_TIME_RECORDS) {
987 // The front presentFence must be trapped in pending status in this
988 // case. Try dequeuing the front one to recover.
989 ALOGE("GlobalPresentFences is already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
990 mGlobalRecord.prevPresentTime = 0;
991 mGlobalRecord.presentFences.pop_front();
992 }
993
994 mGlobalRecord.presentFences.emplace_back(presentFence);
995 flushAvailableGlobalRecordsToStatsLocked();
996}
997
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700998void TimeStats::enable() {
999 if (mEnabled.load()) return;
1000
1001 ATRACE_CALL();
1002
1003 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001004 mEnabled.store(true);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001005 mTimeStats.statsStartLegacy = static_cast<int64_t>(std::time(0));
Yiwei Zhang3a226d22018-10-16 09:23:03 -07001006 mPowerTime.prevTime = systemTime();
Yiwei Zhange5c49d52018-10-29 00:15:31 -07001007 ALOGD("Enabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001008}
1009
1010void TimeStats::disable() {
1011 if (!mEnabled.load()) return;
1012
1013 ATRACE_CALL();
1014
1015 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -07001016 flushPowerTimeLocked();
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001017 mEnabled.store(false);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001018 mTimeStats.statsEndLegacy = static_cast<int64_t>(std::time(0));
Yiwei Zhange5c49d52018-10-29 00:15:31 -07001019 ALOGD("Disabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001020}
1021
Alec Mouri8e2f31b2020-01-16 22:04:35 +00001022void TimeStats::clearAll() {
1023 std::lock_guard<std::mutex> lock(mMutex);
1024 clearGlobalLocked();
1025 clearLayersLocked();
1026}
1027
1028void TimeStats::clearGlobalLocked() {
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001029 ATRACE_CALL();
1030
Alec Mouri7d436ec2021-01-27 20:40:50 -08001031 mTimeStats.statsStartLegacy = (mEnabled.load() ? static_cast<int64_t>(std::time(0)) : 0);
1032 mTimeStats.statsEndLegacy = 0;
1033 mTimeStats.totalFramesLegacy = 0;
1034 mTimeStats.missedFramesLegacy = 0;
1035 mTimeStats.clientCompositionFramesLegacy = 0;
1036 mTimeStats.clientCompositionReusedFramesLegacy = 0;
1037 mTimeStats.refreshRateSwitchesLegacy = 0;
1038 mTimeStats.compositionStrategyChangesLegacy = 0;
1039 mTimeStats.displayEventConnectionsCountLegacy = 0;
1040 mTimeStats.displayOnTimeLegacy = 0;
1041 mTimeStats.presentToPresentLegacy.hist.clear();
1042 mTimeStats.frameDurationLegacy.hist.clear();
1043 mTimeStats.renderEngineTimingLegacy.hist.clear();
1044 mTimeStats.refreshRateStatsLegacy.clear();
Yiwei Zhang3a226d22018-10-16 09:23:03 -07001045 mPowerTime.prevTime = systemTime();
Alec Mouri56e63852021-03-09 18:17:25 -08001046 for (auto& globalRecord : mTimeStats.stats) {
1047 globalRecord.second.clearGlobals();
1048 }
Yiwei Zhange5c49d52018-10-29 00:15:31 -07001049 mGlobalRecord.prevPresentTime = 0;
1050 mGlobalRecord.presentFences.clear();
Alec Mouri8e2f31b2020-01-16 22:04:35 +00001051 ALOGD("Cleared global stats");
1052}
1053
1054void TimeStats::clearLayersLocked() {
1055 ATRACE_CALL();
1056
1057 mTimeStatsTracker.clear();
Alec Mouri56e63852021-03-09 18:17:25 -08001058
1059 for (auto& globalRecord : mTimeStats.stats) {
1060 globalRecord.second.stats.clear();
1061 }
Alec Mouri8e2f31b2020-01-16 22:04:35 +00001062 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