blob: d6a0787ddcc379b73adcf2ef0bfc5c3d1d442c00 [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
Ady Abraham3e8cc072021-05-11 16:29:54 -0700354static int32_t toMs(nsecs_t nanos) {
355 int64_t millis =
356 std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::nanoseconds(nanos))
357 .count();
358 millis = std::clamp(millis, int64_t(INT32_MIN), int64_t(INT32_MAX));
359 return static_cast<int32_t>(millis);
360}
361
Alec Mouri9519bf12019-11-15 16:54:44 -0800362static int32_t msBetween(nsecs_t start, nsecs_t end) {
Ady Abraham3e8cc072021-05-11 16:29:54 -0700363 return toMs(end - start);
Alec Mouri9519bf12019-11-15 16:54:44 -0800364}
365
366void TimeStats::recordFrameDuration(nsecs_t startTime, nsecs_t endTime) {
367 if (!mEnabled.load()) return;
368
369 std::lock_guard<std::mutex> lock(mMutex);
Peiyong Lin65248e02020-04-18 21:15:07 -0700370 if (mPowerTime.powerMode == PowerMode::ON) {
Alec Mouri7d436ec2021-01-27 20:40:50 -0800371 mTimeStats.frameDurationLegacy.insert(msBetween(startTime, endTime));
Alec Mouri9519bf12019-11-15 16:54:44 -0800372 }
373}
374
Alec Mourie4034bb2019-11-19 12:45:54 -0800375void TimeStats::recordRenderEngineDuration(nsecs_t startTime, nsecs_t endTime) {
376 if (!mEnabled.load()) return;
377
378 std::lock_guard<std::mutex> lock(mMutex);
379 if (mGlobalRecord.renderEngineDurations.size() == MAX_NUM_TIME_RECORDS) {
380 ALOGE("RenderEngineTimes are already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
381 mGlobalRecord.renderEngineDurations.pop_front();
382 }
383 mGlobalRecord.renderEngineDurations.push_back({startTime, endTime});
384}
385
386void TimeStats::recordRenderEngineDuration(nsecs_t startTime,
387 const std::shared_ptr<FenceTime>& endTime) {
388 if (!mEnabled.load()) return;
389
390 std::lock_guard<std::mutex> lock(mMutex);
391 if (mGlobalRecord.renderEngineDurations.size() == MAX_NUM_TIME_RECORDS) {
392 ALOGE("RenderEngineTimes are already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
393 mGlobalRecord.renderEngineDurations.pop_front();
394 }
395 mGlobalRecord.renderEngineDurations.push_back({startTime, endTime});
396}
397
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800398bool TimeStats::recordReadyLocked(int32_t layerId, TimeRecord* timeRecord) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700399 if (!timeRecord->ready) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800400 ALOGV("[%d]-[%" PRIu64 "]-presentFence is still not received", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700401 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700402 return false;
403 }
404
405 if (timeRecord->acquireFence != nullptr) {
406 if (timeRecord->acquireFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
407 return false;
408 }
409 if (timeRecord->acquireFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700410 timeRecord->frameTime.acquireTime = timeRecord->acquireFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700411 timeRecord->acquireFence = nullptr;
412 } else {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800413 ALOGV("[%d]-[%" PRIu64 "]-acquireFence signal time is invalid", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700414 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700415 }
416 }
417
418 if (timeRecord->presentFence != nullptr) {
419 if (timeRecord->presentFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
420 return false;
421 }
422 if (timeRecord->presentFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700423 timeRecord->frameTime.presentTime = timeRecord->presentFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700424 timeRecord->presentFence = nullptr;
425 } else {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800426 ALOGV("[%d]-[%" PRIu64 "]-presentFence signal time invalid", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700427 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700428 }
429 }
430
431 return true;
432}
433
Ady Abraham3403a3f2021-04-27 16:58:40 -0700434static int32_t clampToNearestBucket(Fps fps, size_t bucketWidth) {
435 return std::round(fps.getValue() / bucketWidth) * bucketWidth;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800436}
437
438void TimeStats::flushAvailableRecordsToStatsLocked(int32_t layerId, Fps displayRefreshRate,
Ady Abraham8b9e6122021-01-26 19:11:45 -0800439 std::optional<Fps> renderRate,
440 SetFrameRateVote frameRateVote) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700441 ATRACE_CALL();
Ady Abraham8b9e6122021-01-26 19:11:45 -0800442 ALOGV("[%d]-flushAvailableRecordsToStatsLocked", layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700443
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800444 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700445 TimeRecord& prevTimeRecord = layerRecord.prevTimeRecord;
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700446 std::deque<TimeRecord>& timeRecords = layerRecord.timeRecords;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800447 const int32_t refreshRateBucket =
Ady Abraham3403a3f2021-04-27 16:58:40 -0700448 clampToNearestBucket(displayRefreshRate, REFRESH_RATE_BUCKET_WIDTH);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800449 const int32_t renderRateBucket =
Ady Abraham3403a3f2021-04-27 16:58:40 -0700450 clampToNearestBucket(renderRate ? *renderRate : displayRefreshRate,
451 RENDER_RATE_BUCKET_WIDTH);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700452 while (!timeRecords.empty()) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800453 if (!recordReadyLocked(layerId, &timeRecords[0])) break;
454 ALOGV("[%d]-[%" PRIu64 "]-presentFenceTime[%" PRId64 "]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700455 timeRecords[0].frameTime.frameNumber, timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700456
457 if (prevTimeRecord.ready) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700458 uid_t uid = layerRecord.uid;
Yiwei Zhangeafa5cc2019-07-26 15:06:25 -0700459 const std::string& layerName = layerRecord.layerName;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800460 TimeStatsHelper::TimelineStatsKey timelineKey = {refreshRateBucket, renderRateBucket};
461 if (!mTimeStats.stats.count(timelineKey)) {
462 mTimeStats.stats[timelineKey].key = timelineKey;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700463 }
Alec Mouri7d436ec2021-01-27 20:40:50 -0800464
465 TimeStatsHelper::TimelineStats& displayStats = mTimeStats.stats[timelineKey];
466
467 TimeStatsHelper::LayerStatsKey layerKey = {uid, layerName};
468 if (!displayStats.stats.count(layerKey)) {
469 displayStats.stats[layerKey].displayRefreshRateBucket = refreshRateBucket;
470 displayStats.stats[layerKey].renderRateBucket = renderRateBucket;
471 displayStats.stats[layerKey].uid = uid;
472 displayStats.stats[layerKey].layerName = layerName;
473 }
Ady Abraham8b9e6122021-01-26 19:11:45 -0800474 if (frameRateVote.frameRate > 0.0f) {
475 displayStats.stats[layerKey].setFrameRateVote = frameRateVote;
476 }
Alec Mouri7d436ec2021-01-27 20:40:50 -0800477 TimeStatsHelper::TimeStatsLayer& timeStatsLayer = displayStats.stats[layerKey];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700478 timeStatsLayer.totalFrames++;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700479 timeStatsLayer.droppedFrames += layerRecord.droppedFrames;
Alec Mouri91f6df32020-01-30 08:48:58 -0800480 timeStatsLayer.lateAcquireFrames += layerRecord.lateAcquireFrames;
481 timeStatsLayer.badDesiredPresentFrames += layerRecord.badDesiredPresentFrames;
482
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700483 layerRecord.droppedFrames = 0;
Alec Mouri91f6df32020-01-30 08:48:58 -0800484 layerRecord.lateAcquireFrames = 0;
485 layerRecord.badDesiredPresentFrames = 0;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700486
487 const int32_t postToAcquireMs = msBetween(timeRecords[0].frameTime.postTime,
488 timeRecords[0].frameTime.acquireTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800489 ALOGV("[%d]-[%" PRIu64 "]-post2acquire[%d]", layerId,
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700490 timeRecords[0].frameTime.frameNumber, postToAcquireMs);
491 timeStatsLayer.deltas["post2acquire"].insert(postToAcquireMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700492
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700493 const int32_t postToPresentMs = msBetween(timeRecords[0].frameTime.postTime,
494 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800495 ALOGV("[%d]-[%" PRIu64 "]-post2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700496 timeRecords[0].frameTime.frameNumber, postToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700497 timeStatsLayer.deltas["post2present"].insert(postToPresentMs);
498
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700499 const int32_t acquireToPresentMs = msBetween(timeRecords[0].frameTime.acquireTime,
500 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800501 ALOGV("[%d]-[%" PRIu64 "]-acquire2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700502 timeRecords[0].frameTime.frameNumber, acquireToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700503 timeStatsLayer.deltas["acquire2present"].insert(acquireToPresentMs);
504
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700505 const int32_t latchToPresentMs = msBetween(timeRecords[0].frameTime.latchTime,
506 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800507 ALOGV("[%d]-[%" PRIu64 "]-latch2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700508 timeRecords[0].frameTime.frameNumber, latchToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700509 timeStatsLayer.deltas["latch2present"].insert(latchToPresentMs);
510
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700511 const int32_t desiredToPresentMs = msBetween(timeRecords[0].frameTime.desiredTime,
512 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800513 ALOGV("[%d]-[%" PRIu64 "]-desired2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700514 timeRecords[0].frameTime.frameNumber, desiredToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700515 timeStatsLayer.deltas["desired2present"].insert(desiredToPresentMs);
516
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700517 const int32_t presentToPresentMs = msBetween(prevTimeRecord.frameTime.presentTime,
518 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800519 ALOGV("[%d]-[%" PRIu64 "]-present2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700520 timeRecords[0].frameTime.frameNumber, presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700521 timeStatsLayer.deltas["present2present"].insert(presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700522 }
523 prevTimeRecord = timeRecords[0];
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700524 timeRecords.pop_front();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700525 layerRecord.waitData--;
526 }
527}
528
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700529static constexpr const char* kPopupWindowPrefix = "PopupWindow";
530static const size_t kMinLenLayerName = std::strlen(kPopupWindowPrefix);
Yiwei Zhangbd408322018-10-15 18:31:53 -0700531
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700532// Avoid tracking the "PopupWindow:<random hash>#<number>" layers
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700533static bool layerNameIsValid(const std::string& layerName) {
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700534 return layerName.length() >= kMinLenLayerName &&
535 layerName.compare(0, kMinLenLayerName, kPopupWindowPrefix) != 0;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700536}
537
Alec Mouri9a29e672020-09-14 12:39:14 -0700538bool TimeStats::canAddNewAggregatedStats(uid_t uid, const std::string& layerName) {
Alec Mouri7d436ec2021-01-27 20:40:50 -0800539 uint32_t layerRecords = 0;
540 for (const auto& record : mTimeStats.stats) {
541 if (record.second.stats.count({uid, layerName}) > 0) {
542 return true;
543 }
544
545 layerRecords += record.second.stats.size();
546 }
547
548 return mTimeStats.stats.size() < MAX_NUM_LAYER_STATS;
Alec Mouri9a29e672020-09-14 12:39:14 -0700549}
550
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800551void TimeStats::setPostTime(int32_t layerId, uint64_t frameNumber, const std::string& layerName,
Alec Mouri9a29e672020-09-14 12:39:14 -0700552 uid_t uid, nsecs_t postTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700553 if (!mEnabled.load()) return;
554
555 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800556 ALOGV("[%d]-[%" PRIu64 "]-[%s]-PostTime[%" PRId64 "]", layerId, frameNumber, layerName.c_str(),
Yiwei Zhang8e8fe522018-11-02 18:34:07 -0700557 postTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700558
559 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri9a29e672020-09-14 12:39:14 -0700560 if (!canAddNewAggregatedStats(uid, layerName)) {
Yiwei Zhange926ab52019-08-14 15:16:00 -0700561 return;
562 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800563 if (!mTimeStatsTracker.count(layerId) && mTimeStatsTracker.size() < MAX_NUM_LAYER_RECORDS &&
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700564 layerNameIsValid(layerName)) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700565 mTimeStatsTracker[layerId].uid = uid;
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800566 mTimeStatsTracker[layerId].layerName = layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700567 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800568 if (!mTimeStatsTracker.count(layerId)) return;
569 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700570 if (layerRecord.timeRecords.size() == MAX_NUM_TIME_RECORDS) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800571 ALOGE("[%d]-[%s]-timeRecords is at its maximum size[%zu]. Ignore this when unittesting.",
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800572 layerId, layerRecord.layerName.c_str(), MAX_NUM_TIME_RECORDS);
573 mTimeStatsTracker.erase(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700574 return;
575 }
576 // For most media content, the acquireFence is invalid because the buffer is
577 // ready at the queueBuffer stage. In this case, acquireTime should be given
578 // a default value as postTime.
579 TimeRecord timeRecord = {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700580 .frameTime =
581 {
582 .frameNumber = frameNumber,
583 .postTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800584 .latchTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700585 .acquireTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800586 .desiredTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700587 },
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700588 };
589 layerRecord.timeRecords.push_back(timeRecord);
590 if (layerRecord.waitData < 0 ||
591 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
592 layerRecord.waitData = layerRecord.timeRecords.size() - 1;
593}
594
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800595void TimeStats::setLatchTime(int32_t layerId, uint64_t frameNumber, nsecs_t latchTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700596 if (!mEnabled.load()) return;
597
598 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800599 ALOGV("[%d]-[%" PRIu64 "]-LatchTime[%" PRId64 "]", layerId, frameNumber, latchTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700600
601 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800602 if (!mTimeStatsTracker.count(layerId)) return;
603 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700604 if (layerRecord.waitData < 0 ||
605 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
606 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700607 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700608 if (timeRecord.frameTime.frameNumber == frameNumber) {
609 timeRecord.frameTime.latchTime = latchTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700610 }
611}
612
Alec Mouri91f6df32020-01-30 08:48:58 -0800613void TimeStats::incrementLatchSkipped(int32_t layerId, LatchSkipReason reason) {
614 if (!mEnabled.load()) return;
615
616 ATRACE_CALL();
617 ALOGV("[%d]-LatchSkipped-Reason[%d]", layerId,
618 static_cast<std::underlying_type<LatchSkipReason>::type>(reason));
619
620 std::lock_guard<std::mutex> lock(mMutex);
621 if (!mTimeStatsTracker.count(layerId)) return;
622 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
623
624 switch (reason) {
625 case LatchSkipReason::LateAcquire:
626 layerRecord.lateAcquireFrames++;
627 break;
628 }
629}
630
631void TimeStats::incrementBadDesiredPresent(int32_t layerId) {
632 if (!mEnabled.load()) return;
633
634 ATRACE_CALL();
635 ALOGV("[%d]-BadDesiredPresent", layerId);
636
637 std::lock_guard<std::mutex> lock(mMutex);
638 if (!mTimeStatsTracker.count(layerId)) return;
639 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
640 layerRecord.badDesiredPresentFrames++;
641}
642
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800643void TimeStats::setDesiredTime(int32_t layerId, uint64_t frameNumber, nsecs_t desiredTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700644 if (!mEnabled.load()) return;
645
646 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800647 ALOGV("[%d]-[%" PRIu64 "]-DesiredTime[%" PRId64 "]", layerId, frameNumber, desiredTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700648
649 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800650 if (!mTimeStatsTracker.count(layerId)) return;
651 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700652 if (layerRecord.waitData < 0 ||
653 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
654 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700655 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700656 if (timeRecord.frameTime.frameNumber == frameNumber) {
657 timeRecord.frameTime.desiredTime = desiredTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700658 }
659}
660
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800661void TimeStats::setAcquireTime(int32_t layerId, uint64_t frameNumber, nsecs_t acquireTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700662 if (!mEnabled.load()) return;
663
664 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800665 ALOGV("[%d]-[%" PRIu64 "]-AcquireTime[%" PRId64 "]", layerId, frameNumber, acquireTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700666
667 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800668 if (!mTimeStatsTracker.count(layerId)) return;
669 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700670 if (layerRecord.waitData < 0 ||
671 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
672 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700673 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700674 if (timeRecord.frameTime.frameNumber == frameNumber) {
675 timeRecord.frameTime.acquireTime = acquireTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700676 }
677}
678
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800679void TimeStats::setAcquireFence(int32_t layerId, uint64_t frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700680 const std::shared_ptr<FenceTime>& acquireFence) {
681 if (!mEnabled.load()) return;
682
683 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800684 ALOGV("[%d]-[%" PRIu64 "]-AcquireFenceTime[%" PRId64 "]", layerId, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700685 acquireFence->getSignalTime());
686
687 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800688 if (!mTimeStatsTracker.count(layerId)) return;
689 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700690 if (layerRecord.waitData < 0 ||
691 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
692 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700693 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700694 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700695 timeRecord.acquireFence = acquireFence;
696 }
697}
698
Alec Mouri7d436ec2021-01-27 20:40:50 -0800699void TimeStats::setPresentTime(int32_t layerId, uint64_t frameNumber, nsecs_t presentTime,
Ady Abraham8b9e6122021-01-26 19:11:45 -0800700 Fps displayRefreshRate, std::optional<Fps> renderRate,
701 SetFrameRateVote frameRateVote) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700702 if (!mEnabled.load()) return;
703
704 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800705 ALOGV("[%d]-[%" PRIu64 "]-PresentTime[%" PRId64 "]", layerId, frameNumber, presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700706
707 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800708 if (!mTimeStatsTracker.count(layerId)) return;
709 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700710 if (layerRecord.waitData < 0 ||
711 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
712 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700713 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700714 if (timeRecord.frameTime.frameNumber == frameNumber) {
715 timeRecord.frameTime.presentTime = presentTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700716 timeRecord.ready = true;
717 layerRecord.waitData++;
718 }
719
Ady Abraham8b9e6122021-01-26 19:11:45 -0800720 flushAvailableRecordsToStatsLocked(layerId, displayRefreshRate, renderRate, frameRateVote);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700721}
722
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800723void TimeStats::setPresentFence(int32_t layerId, uint64_t frameNumber,
Alec Mouri7d436ec2021-01-27 20:40:50 -0800724 const std::shared_ptr<FenceTime>& presentFence,
Ady Abraham8b9e6122021-01-26 19:11:45 -0800725 Fps displayRefreshRate, std::optional<Fps> renderRate,
726 SetFrameRateVote frameRateVote) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700727 if (!mEnabled.load()) return;
728
729 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800730 ALOGV("[%d]-[%" PRIu64 "]-PresentFenceTime[%" PRId64 "]", layerId, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700731 presentFence->getSignalTime());
732
733 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800734 if (!mTimeStatsTracker.count(layerId)) return;
735 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700736 if (layerRecord.waitData < 0 ||
737 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
738 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700739 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700740 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700741 timeRecord.presentFence = presentFence;
742 timeRecord.ready = true;
743 layerRecord.waitData++;
744 }
745
Ady Abraham8b9e6122021-01-26 19:11:45 -0800746 flushAvailableRecordsToStatsLocked(layerId, displayRefreshRate, renderRate, frameRateVote);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700747}
748
Adithya Srinivasanead17162021-02-18 02:17:37 +0000749static const constexpr int32_t kValidJankyReason = JankType::DisplayHAL |
750 JankType::SurfaceFlingerCpuDeadlineMissed | JankType::SurfaceFlingerGpuDeadlineMissed |
751 JankType::AppDeadlineMissed | JankType::PredictionError |
Adithya Srinivasan53e5c402021-04-16 17:34:30 +0000752 JankType::SurfaceFlingerScheduling;
Alec Mouri363faf02021-01-29 16:34:55 -0800753
Alec Mouri9a29e672020-09-14 12:39:14 -0700754template <class T>
755static void updateJankPayload(T& t, int32_t reasons) {
756 t.jankPayload.totalFrames++;
757
Alec Mouri9a29e672020-09-14 12:39:14 -0700758 if (reasons & kValidJankyReason) {
759 t.jankPayload.totalJankyFrames++;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800760 if ((reasons & JankType::SurfaceFlingerCpuDeadlineMissed) != 0) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700761 t.jankPayload.totalSFLongCpu++;
762 }
Jorim Jaggi5814ab82020-12-03 20:45:58 +0100763 if ((reasons & JankType::SurfaceFlingerGpuDeadlineMissed) != 0) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700764 t.jankPayload.totalSFLongGpu++;
765 }
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800766 if ((reasons & JankType::DisplayHAL) != 0) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700767 t.jankPayload.totalSFUnattributed++;
768 }
Jorim Jaggi5814ab82020-12-03 20:45:58 +0100769 if ((reasons & JankType::AppDeadlineMissed) != 0) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700770 t.jankPayload.totalAppUnattributed++;
771 }
Adithya Srinivasanead17162021-02-18 02:17:37 +0000772 if ((reasons & JankType::PredictionError) != 0) {
773 t.jankPayload.totalSFPredictionError++;
774 }
775 if ((reasons & JankType::SurfaceFlingerScheduling) != 0) {
776 t.jankPayload.totalSFScheduling++;
777 }
Adithya Srinivasan53e5c402021-04-16 17:34:30 +0000778 }
779
780 // We want to track BufferStuffing separately as it can provide info on latency issues
781 if (reasons & JankType::BufferStuffing) {
782 t.jankPayload.totalAppBufferStuffing++;
Alec Mouri9a29e672020-09-14 12:39:14 -0700783 }
784}
785
Alec Mouri363faf02021-01-29 16:34:55 -0800786void TimeStats::incrementJankyFrames(const JankyFramesInfo& info) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700787 if (!mEnabled.load()) return;
788
789 ATRACE_CALL();
790 std::lock_guard<std::mutex> lock(mMutex);
791
Alec Mouri542de112020-11-13 12:07:32 -0800792 // Only update layer stats if we're already tracking the layer in TimeStats.
793 // Otherwise, continue tracking the statistic but use a default layer name instead.
Alec Mouri9a29e672020-09-14 12:39:14 -0700794 // As an implementation detail, we do this because this method is expected to be
Alec Mouri542de112020-11-13 12:07:32 -0800795 // called from FrameTimeline, whose jank classification includes transaction jank
796 // that occurs without a buffer. But, in general those layer names are not suitable as
797 // aggregation keys: e.g., it's normal and expected for Window Manager to include the hash code
798 // for an animation leash. So while we can show that jank in dumpsys, aggregating based on the
799 // layer blows up the stats size, so as a workaround drop those stats. This assumes that
800 // TimeStats will flush the first present fence for a layer *before* FrameTimeline does so that
801 // the first jank record is not dropped.
Alec Mouri9a29e672020-09-14 12:39:14 -0700802
Alec Mouri542de112020-11-13 12:07:32 -0800803 static const std::string kDefaultLayerName = "none";
Alec Mouri7d436ec2021-01-27 20:40:50 -0800804
Alec Mouri363faf02021-01-29 16:34:55 -0800805 const int32_t refreshRateBucket =
Ady Abraham3403a3f2021-04-27 16:58:40 -0700806 clampToNearestBucket(info.refreshRate, REFRESH_RATE_BUCKET_WIDTH);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800807 const int32_t renderRateBucket =
Ady Abraham3403a3f2021-04-27 16:58:40 -0700808 clampToNearestBucket(info.renderRate ? *info.renderRate : info.refreshRate,
809 RENDER_RATE_BUCKET_WIDTH);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800810 const TimeStatsHelper::TimelineStatsKey timelineKey = {refreshRateBucket, renderRateBucket};
811
812 if (!mTimeStats.stats.count(timelineKey)) {
813 mTimeStats.stats[timelineKey].key = timelineKey;
Alec Mouri9a29e672020-09-14 12:39:14 -0700814 }
815
Alec Mouri7d436ec2021-01-27 20:40:50 -0800816 TimeStatsHelper::TimelineStats& timelineStats = mTimeStats.stats[timelineKey];
817
Alec Mouri363faf02021-01-29 16:34:55 -0800818 updateJankPayload<TimeStatsHelper::TimelineStats>(timelineStats, info.reasons);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800819
Alec Mouri363faf02021-01-29 16:34:55 -0800820 TimeStatsHelper::LayerStatsKey layerKey = {info.uid, info.layerName};
Alec Mouri7d436ec2021-01-27 20:40:50 -0800821 if (!timelineStats.stats.count(layerKey)) {
Alec Mouri363faf02021-01-29 16:34:55 -0800822 layerKey = {info.uid, kDefaultLayerName};
Alec Mouri7d436ec2021-01-27 20:40:50 -0800823 timelineStats.stats[layerKey].displayRefreshRateBucket = refreshRateBucket;
824 timelineStats.stats[layerKey].renderRateBucket = renderRateBucket;
Alec Mouri363faf02021-01-29 16:34:55 -0800825 timelineStats.stats[layerKey].uid = info.uid;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800826 timelineStats.stats[layerKey].layerName = kDefaultLayerName;
827 }
828
829 TimeStatsHelper::TimeStatsLayer& timeStatsLayer = timelineStats.stats[layerKey];
Alec Mouri363faf02021-01-29 16:34:55 -0800830 updateJankPayload<TimeStatsHelper::TimeStatsLayer>(timeStatsLayer, info.reasons);
831
832 if (info.reasons & kValidJankyReason) {
833 // TimeStats Histograms only retain positive values, so we don't need to check if these
834 // deadlines were really missed if we know that the frame had jank, since deadlines
835 // that were met will be dropped.
Ady Abraham3e8cc072021-05-11 16:29:54 -0700836 timelineStats.displayDeadlineDeltas.insert(toMs(info.displayDeadlineDelta));
837 timelineStats.displayPresentDeltas.insert(toMs(info.displayPresentJitter));
838 timeStatsLayer.deltas["appDeadlineDeltas"].insert(toMs(info.appDeadlineDelta));
Alec Mouri363faf02021-01-29 16:34:55 -0800839 }
Alec Mouri9a29e672020-09-14 12:39:14 -0700840}
841
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800842void TimeStats::onDestroy(int32_t layerId) {
Yiwei Zhangdc224042018-10-18 15:34:00 -0700843 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800844 ALOGV("[%d]-onDestroy", layerId);
Mikael Pessa90092f42019-08-26 17:22:04 -0700845 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800846 mTimeStatsTracker.erase(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700847}
848
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800849void TimeStats::removeTimeRecord(int32_t layerId, uint64_t frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700850 if (!mEnabled.load()) return;
851
852 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800853 ALOGV("[%d]-[%" PRIu64 "]-removeTimeRecord", layerId, frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700854
855 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800856 if (!mTimeStatsTracker.count(layerId)) return;
857 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700858 size_t removeAt = 0;
859 for (const TimeRecord& record : layerRecord.timeRecords) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700860 if (record.frameTime.frameNumber == frameNumber) break;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700861 removeAt++;
862 }
863 if (removeAt == layerRecord.timeRecords.size()) return;
864 layerRecord.timeRecords.erase(layerRecord.timeRecords.begin() + removeAt);
865 if (layerRecord.waitData > static_cast<int32_t>(removeAt)) {
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700866 layerRecord.waitData--;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700867 }
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700868 layerRecord.droppedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700869}
870
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700871void TimeStats::flushPowerTimeLocked() {
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700872 if (!mEnabled.load()) return;
873
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700874 nsecs_t curTime = systemTime();
875 // elapsedTime is in milliseconds.
876 int64_t elapsedTime = (curTime - mPowerTime.prevTime) / 1000000;
877
878 switch (mPowerTime.powerMode) {
Peiyong Lin65248e02020-04-18 21:15:07 -0700879 case PowerMode::ON:
Alec Mouri7d436ec2021-01-27 20:40:50 -0800880 mTimeStats.displayOnTimeLegacy += elapsedTime;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700881 break;
Peiyong Lin65248e02020-04-18 21:15:07 -0700882 case PowerMode::OFF:
883 case PowerMode::DOZE:
884 case PowerMode::DOZE_SUSPEND:
885 case PowerMode::ON_SUSPEND:
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700886 default:
887 break;
888 }
889
890 mPowerTime.prevTime = curTime;
891}
892
Peiyong Lin65248e02020-04-18 21:15:07 -0700893void TimeStats::setPowerMode(PowerMode powerMode) {
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700894 if (!mEnabled.load()) {
895 std::lock_guard<std::mutex> lock(mMutex);
896 mPowerTime.powerMode = powerMode;
897 return;
898 }
899
900 std::lock_guard<std::mutex> lock(mMutex);
901 if (powerMode == mPowerTime.powerMode) return;
902
903 flushPowerTimeLocked();
904 mPowerTime.powerMode = powerMode;
905}
906
Alec Mourifb571ea2019-01-24 18:42:10 -0800907void TimeStats::recordRefreshRate(uint32_t fps, nsecs_t duration) {
908 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800909 if (mTimeStats.refreshRateStatsLegacy.count(fps)) {
910 mTimeStats.refreshRateStatsLegacy[fps] += duration;
Alec Mourifb571ea2019-01-24 18:42:10 -0800911 } else {
Alec Mouri7d436ec2021-01-27 20:40:50 -0800912 mTimeStats.refreshRateStatsLegacy.insert({fps, duration});
Alec Mourifb571ea2019-01-24 18:42:10 -0800913 }
914}
915
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700916void TimeStats::flushAvailableGlobalRecordsToStatsLocked() {
917 ATRACE_CALL();
918
919 while (!mGlobalRecord.presentFences.empty()) {
920 const nsecs_t curPresentTime = mGlobalRecord.presentFences.front()->getSignalTime();
921 if (curPresentTime == Fence::SIGNAL_TIME_PENDING) break;
922
923 if (curPresentTime == Fence::SIGNAL_TIME_INVALID) {
924 ALOGE("GlobalPresentFence is invalid!");
925 mGlobalRecord.prevPresentTime = 0;
926 mGlobalRecord.presentFences.pop_front();
927 continue;
928 }
929
930 ALOGV("GlobalPresentFenceTime[%" PRId64 "]",
931 mGlobalRecord.presentFences.front()->getSignalTime());
932
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700933 if (mGlobalRecord.prevPresentTime != 0) {
934 const int32_t presentToPresentMs =
935 msBetween(mGlobalRecord.prevPresentTime, curPresentTime);
936 ALOGV("Global present2present[%d] prev[%" PRId64 "] curr[%" PRId64 "]",
937 presentToPresentMs, mGlobalRecord.prevPresentTime, curPresentTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800938 mTimeStats.presentToPresentLegacy.insert(presentToPresentMs);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700939 }
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700940
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700941 mGlobalRecord.prevPresentTime = curPresentTime;
942 mGlobalRecord.presentFences.pop_front();
943 }
Alec Mourie4034bb2019-11-19 12:45:54 -0800944 while (!mGlobalRecord.renderEngineDurations.empty()) {
945 const auto duration = mGlobalRecord.renderEngineDurations.front();
946 const auto& endTime = duration.endTime;
947
948 nsecs_t endNs = -1;
949
950 if (auto val = std::get_if<nsecs_t>(&endTime)) {
951 endNs = *val;
952 } else {
953 endNs = std::get<std::shared_ptr<FenceTime>>(endTime)->getSignalTime();
954 }
955
956 if (endNs == Fence::SIGNAL_TIME_PENDING) break;
957
958 if (endNs < 0) {
959 ALOGE("RenderEngineTiming is invalid!");
960 mGlobalRecord.renderEngineDurations.pop_front();
961 continue;
962 }
963
964 const int32_t renderEngineMs = msBetween(duration.startTime, endNs);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800965 mTimeStats.renderEngineTimingLegacy.insert(renderEngineMs);
Alec Mourie4034bb2019-11-19 12:45:54 -0800966
967 mGlobalRecord.renderEngineDurations.pop_front();
968 }
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700969}
970
971void TimeStats::setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) {
972 if (!mEnabled.load()) return;
973
974 ATRACE_CALL();
975 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700976 if (presentFence == nullptr || !presentFence->isValid()) {
977 mGlobalRecord.prevPresentTime = 0;
978 return;
979 }
980
Peiyong Lin65248e02020-04-18 21:15:07 -0700981 if (mPowerTime.powerMode != PowerMode::ON) {
982 // Try flushing the last present fence on PowerMode::ON.
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700983 flushAvailableGlobalRecordsToStatsLocked();
984 mGlobalRecord.presentFences.clear();
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700985 mGlobalRecord.prevPresentTime = 0;
986 return;
987 }
988
989 if (mGlobalRecord.presentFences.size() == MAX_NUM_TIME_RECORDS) {
990 // The front presentFence must be trapped in pending status in this
991 // case. Try dequeuing the front one to recover.
992 ALOGE("GlobalPresentFences is already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
993 mGlobalRecord.prevPresentTime = 0;
994 mGlobalRecord.presentFences.pop_front();
995 }
996
997 mGlobalRecord.presentFences.emplace_back(presentFence);
998 flushAvailableGlobalRecordsToStatsLocked();
999}
1000
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001001void TimeStats::enable() {
1002 if (mEnabled.load()) return;
1003
1004 ATRACE_CALL();
1005
1006 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001007 mEnabled.store(true);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001008 mTimeStats.statsStartLegacy = static_cast<int64_t>(std::time(0));
Yiwei Zhang3a226d22018-10-16 09:23:03 -07001009 mPowerTime.prevTime = systemTime();
Yiwei Zhange5c49d52018-10-29 00:15:31 -07001010 ALOGD("Enabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001011}
1012
1013void TimeStats::disable() {
1014 if (!mEnabled.load()) return;
1015
1016 ATRACE_CALL();
1017
1018 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -07001019 flushPowerTimeLocked();
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001020 mEnabled.store(false);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001021 mTimeStats.statsEndLegacy = static_cast<int64_t>(std::time(0));
Yiwei Zhange5c49d52018-10-29 00:15:31 -07001022 ALOGD("Disabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001023}
1024
Alec Mouri8e2f31b2020-01-16 22:04:35 +00001025void TimeStats::clearAll() {
1026 std::lock_guard<std::mutex> lock(mMutex);
Ady Abraham3403a3f2021-04-27 16:58:40 -07001027 mTimeStats.stats.clear();
Alec Mouri8e2f31b2020-01-16 22:04:35 +00001028 clearGlobalLocked();
1029 clearLayersLocked();
1030}
1031
1032void TimeStats::clearGlobalLocked() {
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001033 ATRACE_CALL();
1034
Alec Mouri7d436ec2021-01-27 20:40:50 -08001035 mTimeStats.statsStartLegacy = (mEnabled.load() ? static_cast<int64_t>(std::time(0)) : 0);
1036 mTimeStats.statsEndLegacy = 0;
1037 mTimeStats.totalFramesLegacy = 0;
1038 mTimeStats.missedFramesLegacy = 0;
1039 mTimeStats.clientCompositionFramesLegacy = 0;
1040 mTimeStats.clientCompositionReusedFramesLegacy = 0;
1041 mTimeStats.refreshRateSwitchesLegacy = 0;
1042 mTimeStats.compositionStrategyChangesLegacy = 0;
1043 mTimeStats.displayEventConnectionsCountLegacy = 0;
1044 mTimeStats.displayOnTimeLegacy = 0;
1045 mTimeStats.presentToPresentLegacy.hist.clear();
1046 mTimeStats.frameDurationLegacy.hist.clear();
1047 mTimeStats.renderEngineTimingLegacy.hist.clear();
1048 mTimeStats.refreshRateStatsLegacy.clear();
Yiwei Zhang3a226d22018-10-16 09:23:03 -07001049 mPowerTime.prevTime = systemTime();
Alec Mouri56e63852021-03-09 18:17:25 -08001050 for (auto& globalRecord : mTimeStats.stats) {
1051 globalRecord.second.clearGlobals();
1052 }
Yiwei Zhange5c49d52018-10-29 00:15:31 -07001053 mGlobalRecord.prevPresentTime = 0;
1054 mGlobalRecord.presentFences.clear();
Alec Mouri8e2f31b2020-01-16 22:04:35 +00001055 ALOGD("Cleared global stats");
1056}
1057
1058void TimeStats::clearLayersLocked() {
1059 ATRACE_CALL();
1060
1061 mTimeStatsTracker.clear();
Alec Mouri56e63852021-03-09 18:17:25 -08001062
1063 for (auto& globalRecord : mTimeStats.stats) {
1064 globalRecord.second.stats.clear();
1065 }
Alec Mouri8e2f31b2020-01-16 22:04:35 +00001066 ALOGD("Cleared layer stats");
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001067}
1068
1069bool TimeStats::isEnabled() {
1070 return mEnabled.load();
1071}
1072
Yiwei Zhang5434a782018-12-05 18:06:32 -08001073void TimeStats::dump(bool asProto, std::optional<uint32_t> maxLayers, std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001074 ATRACE_CALL();
1075
1076 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001077 if (mTimeStats.statsStartLegacy == 0) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001078 return;
1079 }
1080
Alec Mouri7d436ec2021-01-27 20:40:50 -08001081 mTimeStats.statsEndLegacy = static_cast<int64_t>(std::time(0));
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001082
Yiwei Zhang3a226d22018-10-16 09:23:03 -07001083 flushPowerTimeLocked();
1084
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001085 if (asProto) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -07001086 ALOGD("Dumping TimeStats as proto");
Yiwei Zhangdc224042018-10-18 15:34:00 -07001087 SFTimeStatsGlobalProto timeStatsProto = mTimeStats.toProto(maxLayers);
Dominik Laskowski46470112019-08-02 13:13:11 -07001088 result.append(timeStatsProto.SerializeAsString());
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001089 } else {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -07001090 ALOGD("Dumping TimeStats as text");
Yiwei Zhang5434a782018-12-05 18:06:32 -08001091 result.append(mTimeStats.toString(maxLayers));
Yiwei Zhang8a4015c2018-05-08 16:03:47 -07001092 result.append("\n");
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001093 }
1094}
1095
Alec Mourifb571ea2019-01-24 18:42:10 -08001096} // namespace impl
1097
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001098} // namespace android