blob: e5a9dd47c3c75dd3c13ecc45d5bedd098fba9a53 [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
Yiwei Zhang0102ad22018-05-02 17:37:17 -070017#undef LOG_TAG
18#define LOG_TAG "TimeStats"
19#define ATRACE_TAG ATRACE_TAG_GRAPHICS
20
Yiwei Zhang0102ad22018-05-02 17:37:17 -070021#include <android-base/stringprintf.h>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070022#include <log/log.h>
Tej Singhe2751772021-04-06 22:05:29 -070023#include <timestatsatomsproto/TimeStatsAtomsProtoHeader.h>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070024#include <utils/String8.h>
Yiwei Zhang3a226d22018-10-16 09:23:03 -070025#include <utils/Timers.h>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070026#include <utils/Trace.h>
27
28#include <algorithm>
Alec Mouri9519bf12019-11-15 16:54:44 -080029#include <chrono>
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -070030#include <unordered_map>
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
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -070061SurfaceflingerStatsLayerInfo_GameMode gameModeToProto(GameMode gameMode) {
Adithya Srinivasan58069dc2021-06-04 20:37:02 +000062 switch (gameMode) {
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -070063 case GameMode::Unsupported:
Adithya Srinivasan58069dc2021-06-04 20:37:02 +000064 return SurfaceflingerStatsLayerInfo::GAME_MODE_UNSUPPORTED;
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -070065 case GameMode::Standard:
Adithya Srinivasan58069dc2021-06-04 20:37:02 +000066 return SurfaceflingerStatsLayerInfo::GAME_MODE_STANDARD;
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -070067 case GameMode::Performance:
Adithya Srinivasan58069dc2021-06-04 20:37:02 +000068 return SurfaceflingerStatsLayerInfo::GAME_MODE_PERFORMANCE;
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -070069 case GameMode::Battery:
Adithya Srinivasan58069dc2021-06-04 20:37:02 +000070 return SurfaceflingerStatsLayerInfo::GAME_MODE_BATTERY;
71 default:
72 return SurfaceflingerStatsLayerInfo::GAME_MODE_UNSPECIFIED;
73 }
74}
75
Tej Singhe2751772021-04-06 22:05:29 -070076SurfaceflingerStatsLayerInfo_SetFrameRateVote frameRateVoteToProto(
77 const TimeStats::SetFrameRateVote& setFrameRateVote) {
78 using FrameRateCompatibilityEnum =
79 SurfaceflingerStatsLayerInfo::SetFrameRateVote::FrameRateCompatibility;
80 using SeamlessnessEnum = SurfaceflingerStatsLayerInfo::SetFrameRateVote::Seamlessness;
Alec Mouri75de8f22021-01-20 14:53:44 -080081
Tej Singhe2751772021-04-06 22:05:29 -070082 SurfaceflingerStatsLayerInfo_SetFrameRateVote proto;
83 proto.set_frame_rate(setFrameRateVote.frameRate);
84 proto.set_frame_rate_compatibility(
85 static_cast<FrameRateCompatibilityEnum>(setFrameRateVote.frameRateCompatibility));
86 proto.set_seamlessness(static_cast<SeamlessnessEnum>(setFrameRateVote.seamlessness));
87 return proto;
Alec Mouri75de8f22021-01-20 14:53:44 -080088}
Alec Mouri37384342020-01-02 17:23:37 -080089} // namespace
90
Tej Singhe2751772021-04-06 22:05:29 -070091bool TimeStats::populateGlobalAtom(std::string* pulledData) {
Alec Mouridfad9002020-02-12 17:49:09 -080092 std::lock_guard<std::mutex> lock(mMutex);
93
Alec Mouri7d436ec2021-01-27 20:40:50 -080094 if (mTimeStats.statsStartLegacy == 0) {
Tej Singhe2751772021-04-06 22:05:29 -070095 return false;
Alec Mouridfad9002020-02-12 17:49:09 -080096 }
97 flushPowerTimeLocked();
Tej Singhe2751772021-04-06 22:05:29 -070098 SurfaceflingerStatsGlobalInfoWrapper atomList;
Alec Mouri7d436ec2021-01-27 20:40:50 -080099 for (const auto& globalSlice : mTimeStats.stats) {
Tej Singhe2751772021-04-06 22:05:29 -0700100 SurfaceflingerStatsGlobalInfo* atom = atomList.add_atom();
101 atom->set_total_frames(mTimeStats.totalFramesLegacy);
102 atom->set_missed_frames(mTimeStats.missedFramesLegacy);
103 atom->set_client_composition_frames(mTimeStats.clientCompositionFramesLegacy);
104 atom->set_display_on_millis(mTimeStats.displayOnTimeLegacy);
105 atom->set_animation_millis(mTimeStats.presentToPresentLegacy.totalTime());
106 atom->set_event_connection_count(mTimeStats.displayEventConnectionsCountLegacy);
107 *atom->mutable_frame_duration() =
108 histogramToProto(mTimeStats.frameDurationLegacy.hist, mMaxPulledHistogramBuckets);
109 *atom->mutable_render_engine_timing() =
110 histogramToProto(mTimeStats.renderEngineTimingLegacy.hist,
111 mMaxPulledHistogramBuckets);
112 atom->set_total_timeline_frames(globalSlice.second.jankPayload.totalFrames);
113 atom->set_total_janky_frames(globalSlice.second.jankPayload.totalJankyFrames);
114 atom->set_total_janky_frames_with_long_cpu(globalSlice.second.jankPayload.totalSFLongCpu);
115 atom->set_total_janky_frames_with_long_gpu(globalSlice.second.jankPayload.totalSFLongGpu);
116 atom->set_total_janky_frames_sf_unattributed(
117 globalSlice.second.jankPayload.totalSFUnattributed);
118 atom->set_total_janky_frames_app_unattributed(
119 globalSlice.second.jankPayload.totalAppUnattributed);
120 atom->set_total_janky_frames_sf_scheduling(
121 globalSlice.second.jankPayload.totalSFScheduling);
122 atom->set_total_jank_frames_sf_prediction_error(
123 globalSlice.second.jankPayload.totalSFPredictionError);
124 atom->set_total_jank_frames_app_buffer_stuffing(
125 globalSlice.second.jankPayload.totalAppBufferStuffing);
126 atom->set_display_refresh_rate_bucket(globalSlice.first.displayRefreshRateBucket);
127 *atom->mutable_sf_deadline_misses() =
128 histogramToProto(globalSlice.second.displayDeadlineDeltas.hist,
129 mMaxPulledHistogramBuckets);
130 *atom->mutable_sf_prediction_errors() =
131 histogramToProto(globalSlice.second.displayPresentDeltas.hist,
132 mMaxPulledHistogramBuckets);
133 atom->set_render_rate_bucket(globalSlice.first.renderRateBucket);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800134 }
135
Tej Singhe2751772021-04-06 22:05:29 -0700136 // Always clear data.
Alec Mouridfad9002020-02-12 17:49:09 -0800137 clearGlobalLocked();
138
Tej Singhe2751772021-04-06 22:05:29 -0700139 return atomList.SerializeToString(pulledData);
Alec Mouridfad9002020-02-12 17:49:09 -0800140}
141
Tej Singhe2751772021-04-06 22:05:29 -0700142bool TimeStats::populateLayerAtom(std::string* pulledData) {
Alec Mouri37384342020-01-02 17:23:37 -0800143 std::lock_guard<std::mutex> lock(mMutex);
144
Alec Mouri363faf02021-01-29 16:34:55 -0800145 std::vector<TimeStatsHelper::TimeStatsLayer*> dumpStats;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800146 uint32_t numLayers = 0;
147 for (const auto& globalSlice : mTimeStats.stats) {
148 numLayers += globalSlice.second.stats.size();
149 }
150
151 dumpStats.reserve(numLayers);
152
Alec Mouri363faf02021-01-29 16:34:55 -0800153 for (auto& globalSlice : mTimeStats.stats) {
154 for (auto& layerSlice : globalSlice.second.stats) {
Alec Mouri7d436ec2021-01-27 20:40:50 -0800155 dumpStats.push_back(&layerSlice.second);
156 }
Alec Mouri37384342020-01-02 17:23:37 -0800157 }
158
159 std::sort(dumpStats.begin(), dumpStats.end(),
160 [](TimeStatsHelper::TimeStatsLayer const* l,
161 TimeStatsHelper::TimeStatsLayer const* r) {
162 return l->totalFrames > r->totalFrames;
163 });
164
165 if (mMaxPulledLayers < dumpStats.size()) {
166 dumpStats.resize(mMaxPulledLayers);
167 }
168
Tej Singhe2751772021-04-06 22:05:29 -0700169 SurfaceflingerStatsLayerInfoWrapper atomList;
Alec Mouri363faf02021-01-29 16:34:55 -0800170 for (auto& layer : dumpStats) {
Tej Singhe2751772021-04-06 22:05:29 -0700171 SurfaceflingerStatsLayerInfo* atom = atomList.add_atom();
172 atom->set_layer_name(layer->layerName);
173 atom->set_total_frames(layer->totalFrames);
174 atom->set_dropped_frames(layer->droppedFrames);
175 const auto& present2PresentHist = layer->deltas.find("present2present");
176 if (present2PresentHist != layer->deltas.cend()) {
177 *atom->mutable_present_to_present() =
178 histogramToProto(present2PresentHist->second.hist, mMaxPulledHistogramBuckets);
179 }
180 const auto& post2presentHist = layer->deltas.find("post2present");
181 if (post2presentHist != layer->deltas.cend()) {
182 *atom->mutable_post_to_present() =
183 histogramToProto(post2presentHist->second.hist, mMaxPulledHistogramBuckets);
184 }
185 const auto& acquire2presentHist = layer->deltas.find("acquire2present");
186 if (acquire2presentHist != layer->deltas.cend()) {
187 *atom->mutable_acquire_to_present() =
188 histogramToProto(acquire2presentHist->second.hist, mMaxPulledHistogramBuckets);
189 }
190 const auto& latch2presentHist = layer->deltas.find("latch2present");
191 if (latch2presentHist != layer->deltas.cend()) {
192 *atom->mutable_latch_to_present() =
193 histogramToProto(latch2presentHist->second.hist, mMaxPulledHistogramBuckets);
194 }
195 const auto& desired2presentHist = layer->deltas.find("desired2present");
196 if (desired2presentHist != layer->deltas.cend()) {
197 *atom->mutable_desired_to_present() =
198 histogramToProto(desired2presentHist->second.hist, mMaxPulledHistogramBuckets);
199 }
200 const auto& post2acquireHist = layer->deltas.find("post2acquire");
201 if (post2acquireHist != layer->deltas.cend()) {
202 *atom->mutable_post_to_acquire() =
203 histogramToProto(post2acquireHist->second.hist, mMaxPulledHistogramBuckets);
Alec Mouri37384342020-01-02 17:23:37 -0800204 }
205
Tej Singhe2751772021-04-06 22:05:29 -0700206 atom->set_late_acquire_frames(layer->lateAcquireFrames);
207 atom->set_bad_desired_present_frames(layer->badDesiredPresentFrames);
208 atom->set_uid(layer->uid);
209 atom->set_total_timeline_frames(layer->jankPayload.totalFrames);
210 atom->set_total_janky_frames(layer->jankPayload.totalJankyFrames);
211 atom->set_total_janky_frames_with_long_cpu(layer->jankPayload.totalSFLongCpu);
212 atom->set_total_janky_frames_with_long_gpu(layer->jankPayload.totalSFLongGpu);
213 atom->set_total_janky_frames_sf_unattributed(layer->jankPayload.totalSFUnattributed);
214 atom->set_total_janky_frames_app_unattributed(layer->jankPayload.totalAppUnattributed);
215 atom->set_total_janky_frames_sf_scheduling(layer->jankPayload.totalSFScheduling);
216 atom->set_total_jank_frames_sf_prediction_error(layer->jankPayload.totalSFPredictionError);
217 atom->set_total_jank_frames_app_buffer_stuffing(layer->jankPayload.totalAppBufferStuffing);
218 atom->set_display_refresh_rate_bucket(layer->displayRefreshRateBucket);
219 atom->set_render_rate_bucket(layer->renderRateBucket);
220 *atom->mutable_set_frame_rate_vote() = frameRateVoteToProto(layer->setFrameRateVote);
221 *atom->mutable_app_deadline_misses() =
222 histogramToProto(layer->deltas["appDeadlineDeltas"].hist,
223 mMaxPulledHistogramBuckets);
Adithya Srinivasan58069dc2021-06-04 20:37:02 +0000224 atom->set_game_mode(gameModeToProto(layer->gameMode));
Alec Mouri37384342020-01-02 17:23:37 -0800225 }
Tej Singhe2751772021-04-06 22:05:29 -0700226
227 // Always clear data.
Alec Mouri37384342020-01-02 17:23:37 -0800228 clearLayersLocked();
229
Tej Singhe2751772021-04-06 22:05:29 -0700230 return atomList.SerializeToString(pulledData);
Alec Mouri37384342020-01-02 17:23:37 -0800231}
232
Tej Singhe2751772021-04-06 22:05:29 -0700233TimeStats::TimeStats() : TimeStats(std::nullopt, std::nullopt) {}
Alec Mouri37384342020-01-02 17:23:37 -0800234
Tej Singhe2751772021-04-06 22:05:29 -0700235TimeStats::TimeStats(std::optional<size_t> maxPulledLayers,
Alec Mouri37384342020-01-02 17:23:37 -0800236 std::optional<size_t> maxPulledHistogramBuckets) {
Alec Mouri37384342020-01-02 17:23:37 -0800237 if (maxPulledLayers) {
238 mMaxPulledLayers = *maxPulledLayers;
239 }
240
241 if (maxPulledHistogramBuckets) {
242 mMaxPulledHistogramBuckets = *maxPulledHistogramBuckets;
243 }
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000244}
245
Tej Singhe2751772021-04-06 22:05:29 -0700246bool TimeStats::onPullAtom(const int atomId, std::string* pulledData) {
247 bool success = false;
248 if (atomId == 10062) { // SURFACEFLINGER_STATS_GLOBAL_INFO
249 success = populateGlobalAtom(pulledData);
250 } else if (atomId == 10063) { // SURFACEFLINGER_STATS_LAYER_INFO
251 success = populateLayerAtom(pulledData);
252 }
Alec Mouri3ecd5cd2020-01-29 12:53:07 -0800253
Tej Singhe2751772021-04-06 22:05:29 -0700254 // Enable timestats now. The first full pull for a given build is expected to
255 // have empty or very little stats, as stats are first enabled after the
256 // first pull is completed for either the global or layer stats.
257 enable();
258 return success;
Alec Mourib3885ad2019-09-06 17:08:55 -0700259}
260
Dominik Laskowskic2867142019-01-21 11:33:38 -0800261void TimeStats::parseArgs(bool asProto, const Vector<String16>& args, std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700262 ATRACE_CALL();
263
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700264 std::unordered_map<std::string, int32_t> argsMap;
Dominik Laskowskic2867142019-01-21 11:33:38 -0800265 for (size_t index = 0; index < args.size(); ++index) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700266 argsMap[std::string(String8(args[index]).c_str())] = index;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700267 }
268
269 if (argsMap.count("-disable")) {
270 disable();
271 }
272
273 if (argsMap.count("-dump")) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700274 std::optional<uint32_t> maxLayers = std::nullopt;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700275 auto iter = argsMap.find("-maxlayers");
276 if (iter != argsMap.end() && iter->second + 1 < static_cast<int32_t>(args.size())) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700277 int64_t value = strtol(String8(args[iter->second + 1]).c_str(), nullptr, 10);
278 value = std::clamp(value, int64_t(0), int64_t(UINT32_MAX));
279 maxLayers = static_cast<uint32_t>(value);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700280 }
281
Yiwei Zhang8a4015c2018-05-08 16:03:47 -0700282 dump(asProto, maxLayers, result);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700283 }
284
285 if (argsMap.count("-clear")) {
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000286 clearAll();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700287 }
288
289 if (argsMap.count("-enable")) {
290 enable();
291 }
292}
293
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700294std::string TimeStats::miniDump() {
295 ATRACE_CALL();
296
297 std::string result = "TimeStats miniDump:\n";
298 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange926ab52019-08-14 15:16:00 -0700299 android::base::StringAppendF(&result, "Number of layers currently being tracked is %zu\n",
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700300 mTimeStatsTracker.size());
Yiwei Zhange926ab52019-08-14 15:16:00 -0700301 android::base::StringAppendF(&result, "Number of layers in the stats pool is %zu\n",
302 mTimeStats.stats.size());
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700303 return result;
304}
305
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700306void TimeStats::incrementTotalFrames() {
307 if (!mEnabled.load()) return;
308
309 ATRACE_CALL();
310
311 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800312 mTimeStats.totalFramesLegacy++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700313}
314
Yiwei Zhang621f9d42018-05-07 10:40:55 -0700315void TimeStats::incrementMissedFrames() {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700316 if (!mEnabled.load()) return;
317
318 ATRACE_CALL();
319
320 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800321 mTimeStats.missedFramesLegacy++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700322}
323
Vishnu Nair9cf89262022-02-26 09:17:49 -0800324void TimeStats::pushCompositionStrategyState(const TimeStats::ClientCompositionRecord& record) {
325 if (!mEnabled.load() || !record.hasInterestingData()) {
326 return;
327 }
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700328
329 ATRACE_CALL();
330
331 std::lock_guard<std::mutex> lock(mMutex);
Vishnu Nair9cf89262022-02-26 09:17:49 -0800332 if (record.changed) mTimeStats.compositionStrategyChangesLegacy++;
333 if (record.hadClientComposition) mTimeStats.clientCompositionFramesLegacy++;
334 if (record.reused) mTimeStats.clientCompositionReusedFramesLegacy++;
335 if (record.predicted) mTimeStats.compositionStrategyPredictedLegacy++;
336 if (record.predictionSucceeded) mTimeStats.compositionStrategyPredictionSucceededLegacy++;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800337}
338
Alec Mouri8de697e2020-03-19 10:52:01 -0700339void TimeStats::incrementRefreshRateSwitches() {
340 if (!mEnabled.load()) return;
341
342 ATRACE_CALL();
343
344 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800345 mTimeStats.refreshRateSwitchesLegacy++;
Alec Mouri8de697e2020-03-19 10:52:01 -0700346}
347
Alec Mouri717bcb62020-02-10 17:07:19 -0800348void TimeStats::recordDisplayEventConnectionCount(int32_t count) {
349 if (!mEnabled.load()) return;
350
351 ATRACE_CALL();
352
353 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800354 mTimeStats.displayEventConnectionsCountLegacy =
355 std::max(mTimeStats.displayEventConnectionsCountLegacy, count);
Alec Mouri717bcb62020-02-10 17:07:19 -0800356}
357
Ady Abraham3e8cc072021-05-11 16:29:54 -0700358static int32_t toMs(nsecs_t nanos) {
359 int64_t millis =
360 std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::nanoseconds(nanos))
361 .count();
362 millis = std::clamp(millis, int64_t(INT32_MIN), int64_t(INT32_MAX));
363 return static_cast<int32_t>(millis);
364}
365
Alec Mouri9519bf12019-11-15 16:54:44 -0800366static int32_t msBetween(nsecs_t start, nsecs_t end) {
Ady Abraham3e8cc072021-05-11 16:29:54 -0700367 return toMs(end - start);
Alec Mouri9519bf12019-11-15 16:54:44 -0800368}
369
370void TimeStats::recordFrameDuration(nsecs_t startTime, nsecs_t endTime) {
371 if (!mEnabled.load()) return;
372
373 std::lock_guard<std::mutex> lock(mMutex);
Peiyong Lin65248e02020-04-18 21:15:07 -0700374 if (mPowerTime.powerMode == PowerMode::ON) {
Alec Mouri7d436ec2021-01-27 20:40:50 -0800375 mTimeStats.frameDurationLegacy.insert(msBetween(startTime, endTime));
Alec Mouri9519bf12019-11-15 16:54:44 -0800376 }
377}
378
Alec Mourie4034bb2019-11-19 12:45:54 -0800379void TimeStats::recordRenderEngineDuration(nsecs_t startTime, nsecs_t endTime) {
380 if (!mEnabled.load()) return;
381
382 std::lock_guard<std::mutex> lock(mMutex);
383 if (mGlobalRecord.renderEngineDurations.size() == MAX_NUM_TIME_RECORDS) {
384 ALOGE("RenderEngineTimes are already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
385 mGlobalRecord.renderEngineDurations.pop_front();
386 }
387 mGlobalRecord.renderEngineDurations.push_back({startTime, endTime});
388}
389
390void TimeStats::recordRenderEngineDuration(nsecs_t startTime,
391 const std::shared_ptr<FenceTime>& endTime) {
392 if (!mEnabled.load()) return;
393
394 std::lock_guard<std::mutex> lock(mMutex);
395 if (mGlobalRecord.renderEngineDurations.size() == MAX_NUM_TIME_RECORDS) {
396 ALOGE("RenderEngineTimes are already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
397 mGlobalRecord.renderEngineDurations.pop_front();
398 }
399 mGlobalRecord.renderEngineDurations.push_back({startTime, endTime});
400}
401
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800402bool TimeStats::recordReadyLocked(int32_t layerId, TimeRecord* timeRecord) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700403 if (!timeRecord->ready) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800404 ALOGV("[%d]-[%" PRIu64 "]-presentFence is still not received", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700405 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700406 return false;
407 }
408
409 if (timeRecord->acquireFence != nullptr) {
410 if (timeRecord->acquireFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
411 return false;
412 }
413 if (timeRecord->acquireFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700414 timeRecord->frameTime.acquireTime = timeRecord->acquireFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700415 timeRecord->acquireFence = nullptr;
416 } else {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800417 ALOGV("[%d]-[%" PRIu64 "]-acquireFence signal time is invalid", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700418 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700419 }
420 }
421
422 if (timeRecord->presentFence != nullptr) {
423 if (timeRecord->presentFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) {
424 return false;
425 }
426 if (timeRecord->presentFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700427 timeRecord->frameTime.presentTime = timeRecord->presentFence->getSignalTime();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700428 timeRecord->presentFence = nullptr;
429 } else {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800430 ALOGV("[%d]-[%" PRIu64 "]-presentFence signal time invalid", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700431 timeRecord->frameTime.frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700432 }
433 }
434
435 return true;
436}
437
Ady Abraham3403a3f2021-04-27 16:58:40 -0700438static int32_t clampToNearestBucket(Fps fps, size_t bucketWidth) {
439 return std::round(fps.getValue() / bucketWidth) * bucketWidth;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800440}
441
442void TimeStats::flushAvailableRecordsToStatsLocked(int32_t layerId, Fps displayRefreshRate,
Ady Abraham8b9e6122021-01-26 19:11:45 -0800443 std::optional<Fps> renderRate,
Adithya Srinivasan58069dc2021-06-04 20:37:02 +0000444 SetFrameRateVote frameRateVote,
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700445 GameMode gameMode) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700446 ATRACE_CALL();
Ady Abraham8b9e6122021-01-26 19:11:45 -0800447 ALOGV("[%d]-flushAvailableRecordsToStatsLocked", layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700448
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800449 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700450 TimeRecord& prevTimeRecord = layerRecord.prevTimeRecord;
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700451 std::deque<TimeRecord>& timeRecords = layerRecord.timeRecords;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800452 const int32_t refreshRateBucket =
Ady Abraham3403a3f2021-04-27 16:58:40 -0700453 clampToNearestBucket(displayRefreshRate, REFRESH_RATE_BUCKET_WIDTH);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800454 const int32_t renderRateBucket =
Ady Abraham3403a3f2021-04-27 16:58:40 -0700455 clampToNearestBucket(renderRate ? *renderRate : displayRefreshRate,
456 RENDER_RATE_BUCKET_WIDTH);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700457 while (!timeRecords.empty()) {
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800458 if (!recordReadyLocked(layerId, &timeRecords[0])) break;
459 ALOGV("[%d]-[%" PRIu64 "]-presentFenceTime[%" PRId64 "]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700460 timeRecords[0].frameTime.frameNumber, timeRecords[0].frameTime.presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700461
462 if (prevTimeRecord.ready) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700463 uid_t uid = layerRecord.uid;
Yiwei Zhangeafa5cc2019-07-26 15:06:25 -0700464 const std::string& layerName = layerRecord.layerName;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800465 TimeStatsHelper::TimelineStatsKey timelineKey = {refreshRateBucket, renderRateBucket};
466 if (!mTimeStats.stats.count(timelineKey)) {
467 mTimeStats.stats[timelineKey].key = timelineKey;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700468 }
Alec Mouri7d436ec2021-01-27 20:40:50 -0800469
470 TimeStatsHelper::TimelineStats& displayStats = mTimeStats.stats[timelineKey];
471
Adithya Srinivasan58069dc2021-06-04 20:37:02 +0000472 TimeStatsHelper::LayerStatsKey layerKey = {uid, layerName, gameMode};
Alec Mouri7d436ec2021-01-27 20:40:50 -0800473 if (!displayStats.stats.count(layerKey)) {
474 displayStats.stats[layerKey].displayRefreshRateBucket = refreshRateBucket;
475 displayStats.stats[layerKey].renderRateBucket = renderRateBucket;
476 displayStats.stats[layerKey].uid = uid;
477 displayStats.stats[layerKey].layerName = layerName;
Adithya Srinivasan58069dc2021-06-04 20:37:02 +0000478 displayStats.stats[layerKey].gameMode = gameMode;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800479 }
Ady Abraham8b9e6122021-01-26 19:11:45 -0800480 if (frameRateVote.frameRate > 0.0f) {
481 displayStats.stats[layerKey].setFrameRateVote = frameRateVote;
482 }
Alec Mouri7d436ec2021-01-27 20:40:50 -0800483 TimeStatsHelper::TimeStatsLayer& timeStatsLayer = displayStats.stats[layerKey];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700484 timeStatsLayer.totalFrames++;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700485 timeStatsLayer.droppedFrames += layerRecord.droppedFrames;
Alec Mouri91f6df32020-01-30 08:48:58 -0800486 timeStatsLayer.lateAcquireFrames += layerRecord.lateAcquireFrames;
487 timeStatsLayer.badDesiredPresentFrames += layerRecord.badDesiredPresentFrames;
488
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700489 layerRecord.droppedFrames = 0;
Alec Mouri91f6df32020-01-30 08:48:58 -0800490 layerRecord.lateAcquireFrames = 0;
491 layerRecord.badDesiredPresentFrames = 0;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700492
493 const int32_t postToAcquireMs = msBetween(timeRecords[0].frameTime.postTime,
494 timeRecords[0].frameTime.acquireTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800495 ALOGV("[%d]-[%" PRIu64 "]-post2acquire[%d]", layerId,
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700496 timeRecords[0].frameTime.frameNumber, postToAcquireMs);
497 timeStatsLayer.deltas["post2acquire"].insert(postToAcquireMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700498
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700499 const int32_t postToPresentMs = msBetween(timeRecords[0].frameTime.postTime,
500 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800501 ALOGV("[%d]-[%" PRIu64 "]-post2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700502 timeRecords[0].frameTime.frameNumber, postToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700503 timeStatsLayer.deltas["post2present"].insert(postToPresentMs);
504
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700505 const int32_t acquireToPresentMs = msBetween(timeRecords[0].frameTime.acquireTime,
506 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800507 ALOGV("[%d]-[%" PRIu64 "]-acquire2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700508 timeRecords[0].frameTime.frameNumber, acquireToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700509 timeStatsLayer.deltas["acquire2present"].insert(acquireToPresentMs);
510
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700511 const int32_t latchToPresentMs = msBetween(timeRecords[0].frameTime.latchTime,
512 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800513 ALOGV("[%d]-[%" PRIu64 "]-latch2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700514 timeRecords[0].frameTime.frameNumber, latchToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700515 timeStatsLayer.deltas["latch2present"].insert(latchToPresentMs);
516
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700517 const int32_t desiredToPresentMs = msBetween(timeRecords[0].frameTime.desiredTime,
518 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800519 ALOGV("[%d]-[%" PRIu64 "]-desired2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700520 timeRecords[0].frameTime.frameNumber, desiredToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700521 timeStatsLayer.deltas["desired2present"].insert(desiredToPresentMs);
522
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700523 const int32_t presentToPresentMs = msBetween(prevTimeRecord.frameTime.presentTime,
524 timeRecords[0].frameTime.presentTime);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800525 ALOGV("[%d]-[%" PRIu64 "]-present2present[%d]", layerId,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700526 timeRecords[0].frameTime.frameNumber, presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700527 timeStatsLayer.deltas["present2present"].insert(presentToPresentMs);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700528 }
529 prevTimeRecord = timeRecords[0];
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700530 timeRecords.pop_front();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700531 layerRecord.waitData--;
532 }
533}
534
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700535static constexpr const char* kPopupWindowPrefix = "PopupWindow";
536static const size_t kMinLenLayerName = std::strlen(kPopupWindowPrefix);
Yiwei Zhangbd408322018-10-15 18:31:53 -0700537
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700538// Avoid tracking the "PopupWindow:<random hash>#<number>" layers
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700539static bool layerNameIsValid(const std::string& layerName) {
Yiwei Zhang8bec7e82019-10-07 18:08:26 -0700540 return layerName.length() >= kMinLenLayerName &&
541 layerName.compare(0, kMinLenLayerName, kPopupWindowPrefix) != 0;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700542}
543
Adithya Srinivasan58069dc2021-06-04 20:37:02 +0000544bool TimeStats::canAddNewAggregatedStats(uid_t uid, const std::string& layerName,
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700545 GameMode gameMode) {
Alec Mouri7d436ec2021-01-27 20:40:50 -0800546 uint32_t layerRecords = 0;
547 for (const auto& record : mTimeStats.stats) {
Adithya Srinivasan58069dc2021-06-04 20:37:02 +0000548 if (record.second.stats.count({uid, layerName, gameMode}) > 0) {
Alec Mouri7d436ec2021-01-27 20:40:50 -0800549 return true;
550 }
551
552 layerRecords += record.second.stats.size();
553 }
554
Dominik Laskowskib4ba8f52021-09-27 18:20:58 -0700555 return layerRecords < MAX_NUM_LAYER_STATS;
Alec Mouri9a29e672020-09-14 12:39:14 -0700556}
557
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800558void TimeStats::setPostTime(int32_t layerId, uint64_t frameNumber, const std::string& layerName,
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700559 uid_t uid, nsecs_t postTime, GameMode gameMode) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700560 if (!mEnabled.load()) return;
561
562 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800563 ALOGV("[%d]-[%" PRIu64 "]-[%s]-PostTime[%" PRId64 "]", layerId, frameNumber, layerName.c_str(),
Yiwei Zhang8e8fe522018-11-02 18:34:07 -0700564 postTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700565
566 std::lock_guard<std::mutex> lock(mMutex);
Adithya Srinivasan58069dc2021-06-04 20:37:02 +0000567 if (!canAddNewAggregatedStats(uid, layerName, gameMode)) {
Yiwei Zhange926ab52019-08-14 15:16:00 -0700568 return;
569 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800570 if (!mTimeStatsTracker.count(layerId) && mTimeStatsTracker.size() < MAX_NUM_LAYER_RECORDS &&
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700571 layerNameIsValid(layerName)) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700572 mTimeStatsTracker[layerId].uid = uid;
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800573 mTimeStatsTracker[layerId].layerName = layerName;
Adithya Srinivasan58069dc2021-06-04 20:37:02 +0000574 mTimeStatsTracker[layerId].gameMode = gameMode;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700575 }
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800576 if (!mTimeStatsTracker.count(layerId)) return;
577 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700578 if (layerRecord.timeRecords.size() == MAX_NUM_TIME_RECORDS) {
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800579 ALOGE("[%d]-[%s]-timeRecords is at its maximum size[%zu]. Ignore this when unittesting.",
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800580 layerId, layerRecord.layerName.c_str(), MAX_NUM_TIME_RECORDS);
581 mTimeStatsTracker.erase(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700582 return;
583 }
584 // For most media content, the acquireFence is invalid because the buffer is
585 // ready at the queueBuffer stage. In this case, acquireTime should be given
586 // a default value as postTime.
587 TimeRecord timeRecord = {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700588 .frameTime =
589 {
590 .frameNumber = frameNumber,
591 .postTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800592 .latchTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700593 .acquireTime = postTime,
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800594 .desiredTime = postTime,
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700595 },
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700596 };
597 layerRecord.timeRecords.push_back(timeRecord);
598 if (layerRecord.waitData < 0 ||
599 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
600 layerRecord.waitData = layerRecord.timeRecords.size() - 1;
601}
602
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800603void TimeStats::setLatchTime(int32_t layerId, uint64_t frameNumber, nsecs_t latchTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700604 if (!mEnabled.load()) return;
605
606 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800607 ALOGV("[%d]-[%" PRIu64 "]-LatchTime[%" PRId64 "]", layerId, frameNumber, latchTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700608
609 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800610 if (!mTimeStatsTracker.count(layerId)) return;
611 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700612 if (layerRecord.waitData < 0 ||
613 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
614 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700615 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700616 if (timeRecord.frameTime.frameNumber == frameNumber) {
617 timeRecord.frameTime.latchTime = latchTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700618 }
619}
620
Alec Mouri91f6df32020-01-30 08:48:58 -0800621void TimeStats::incrementLatchSkipped(int32_t layerId, LatchSkipReason reason) {
622 if (!mEnabled.load()) return;
623
624 ATRACE_CALL();
625 ALOGV("[%d]-LatchSkipped-Reason[%d]", layerId,
626 static_cast<std::underlying_type<LatchSkipReason>::type>(reason));
627
628 std::lock_guard<std::mutex> lock(mMutex);
629 if (!mTimeStatsTracker.count(layerId)) return;
630 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
631
632 switch (reason) {
633 case LatchSkipReason::LateAcquire:
634 layerRecord.lateAcquireFrames++;
635 break;
636 }
637}
638
639void TimeStats::incrementBadDesiredPresent(int32_t layerId) {
640 if (!mEnabled.load()) return;
641
642 ATRACE_CALL();
643 ALOGV("[%d]-BadDesiredPresent", layerId);
644
645 std::lock_guard<std::mutex> lock(mMutex);
646 if (!mTimeStatsTracker.count(layerId)) return;
647 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
648 layerRecord.badDesiredPresentFrames++;
649}
650
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800651void TimeStats::setDesiredTime(int32_t layerId, uint64_t frameNumber, nsecs_t desiredTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700652 if (!mEnabled.load()) return;
653
654 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800655 ALOGV("[%d]-[%" PRIu64 "]-DesiredTime[%" PRId64 "]", layerId, frameNumber, desiredTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700656
657 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800658 if (!mTimeStatsTracker.count(layerId)) return;
659 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700660 if (layerRecord.waitData < 0 ||
661 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
662 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700663 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700664 if (timeRecord.frameTime.frameNumber == frameNumber) {
665 timeRecord.frameTime.desiredTime = desiredTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700666 }
667}
668
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800669void TimeStats::setAcquireTime(int32_t layerId, uint64_t frameNumber, nsecs_t acquireTime) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700670 if (!mEnabled.load()) return;
671
672 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800673 ALOGV("[%d]-[%" PRIu64 "]-AcquireTime[%" PRId64 "]", layerId, frameNumber, acquireTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700674
675 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800676 if (!mTimeStatsTracker.count(layerId)) return;
677 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700678 if (layerRecord.waitData < 0 ||
679 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
680 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700681 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700682 if (timeRecord.frameTime.frameNumber == frameNumber) {
683 timeRecord.frameTime.acquireTime = acquireTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700684 }
685}
686
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800687void TimeStats::setAcquireFence(int32_t layerId, uint64_t frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700688 const std::shared_ptr<FenceTime>& acquireFence) {
689 if (!mEnabled.load()) return;
690
691 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800692 ALOGV("[%d]-[%" PRIu64 "]-AcquireFenceTime[%" PRId64 "]", layerId, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700693 acquireFence->getSignalTime());
694
695 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800696 if (!mTimeStatsTracker.count(layerId)) return;
697 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700698 if (layerRecord.waitData < 0 ||
699 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
700 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700701 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700702 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700703 timeRecord.acquireFence = acquireFence;
704 }
705}
706
Alec Mouri7d436ec2021-01-27 20:40:50 -0800707void TimeStats::setPresentTime(int32_t layerId, uint64_t frameNumber, nsecs_t presentTime,
Ady Abraham8b9e6122021-01-26 19:11:45 -0800708 Fps displayRefreshRate, std::optional<Fps> renderRate,
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700709 SetFrameRateVote frameRateVote, GameMode gameMode) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700710 if (!mEnabled.load()) return;
711
712 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800713 ALOGV("[%d]-[%" PRIu64 "]-PresentTime[%" PRId64 "]", layerId, frameNumber, presentTime);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700714
715 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800716 if (!mTimeStatsTracker.count(layerId)) return;
717 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700718 if (layerRecord.waitData < 0 ||
719 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
720 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700721 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700722 if (timeRecord.frameTime.frameNumber == frameNumber) {
723 timeRecord.frameTime.presentTime = presentTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700724 timeRecord.ready = true;
725 layerRecord.waitData++;
726 }
727
Adithya Srinivasan58069dc2021-06-04 20:37:02 +0000728 flushAvailableRecordsToStatsLocked(layerId, displayRefreshRate, renderRate, frameRateVote,
729 gameMode);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700730}
731
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800732void TimeStats::setPresentFence(int32_t layerId, uint64_t frameNumber,
Alec Mouri7d436ec2021-01-27 20:40:50 -0800733 const std::shared_ptr<FenceTime>& presentFence,
Ady Abraham8b9e6122021-01-26 19:11:45 -0800734 Fps displayRefreshRate, std::optional<Fps> renderRate,
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700735 SetFrameRateVote frameRateVote, GameMode gameMode) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700736 if (!mEnabled.load()) return;
737
738 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800739 ALOGV("[%d]-[%" PRIu64 "]-PresentFenceTime[%" PRId64 "]", layerId, frameNumber,
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700740 presentFence->getSignalTime());
741
742 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800743 if (!mTimeStatsTracker.count(layerId)) return;
744 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhangcb7dd002019-04-16 11:03:01 -0700745 if (layerRecord.waitData < 0 ||
746 layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size()))
747 return;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700748 TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData];
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700749 if (timeRecord.frameTime.frameNumber == frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700750 timeRecord.presentFence = presentFence;
751 timeRecord.ready = true;
752 layerRecord.waitData++;
753 }
754
Adithya Srinivasan58069dc2021-06-04 20:37:02 +0000755 flushAvailableRecordsToStatsLocked(layerId, displayRefreshRate, renderRate, frameRateVote,
756 gameMode);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700757}
758
Adithya Srinivasanead17162021-02-18 02:17:37 +0000759static const constexpr int32_t kValidJankyReason = JankType::DisplayHAL |
760 JankType::SurfaceFlingerCpuDeadlineMissed | JankType::SurfaceFlingerGpuDeadlineMissed |
761 JankType::AppDeadlineMissed | JankType::PredictionError |
Adithya Srinivasan53e5c402021-04-16 17:34:30 +0000762 JankType::SurfaceFlingerScheduling;
Alec Mouri363faf02021-01-29 16:34:55 -0800763
Alec Mouri9a29e672020-09-14 12:39:14 -0700764template <class T>
765static void updateJankPayload(T& t, int32_t reasons) {
766 t.jankPayload.totalFrames++;
767
Alec Mouri9a29e672020-09-14 12:39:14 -0700768 if (reasons & kValidJankyReason) {
769 t.jankPayload.totalJankyFrames++;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800770 if ((reasons & JankType::SurfaceFlingerCpuDeadlineMissed) != 0) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700771 t.jankPayload.totalSFLongCpu++;
772 }
Jorim Jaggi5814ab82020-12-03 20:45:58 +0100773 if ((reasons & JankType::SurfaceFlingerGpuDeadlineMissed) != 0) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700774 t.jankPayload.totalSFLongGpu++;
775 }
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800776 if ((reasons & JankType::DisplayHAL) != 0) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700777 t.jankPayload.totalSFUnattributed++;
778 }
Jorim Jaggi5814ab82020-12-03 20:45:58 +0100779 if ((reasons & JankType::AppDeadlineMissed) != 0) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700780 t.jankPayload.totalAppUnattributed++;
781 }
Adithya Srinivasanead17162021-02-18 02:17:37 +0000782 if ((reasons & JankType::PredictionError) != 0) {
783 t.jankPayload.totalSFPredictionError++;
784 }
785 if ((reasons & JankType::SurfaceFlingerScheduling) != 0) {
786 t.jankPayload.totalSFScheduling++;
787 }
Adithya Srinivasan53e5c402021-04-16 17:34:30 +0000788 }
789
790 // We want to track BufferStuffing separately as it can provide info on latency issues
791 if (reasons & JankType::BufferStuffing) {
792 t.jankPayload.totalAppBufferStuffing++;
Alec Mouri9a29e672020-09-14 12:39:14 -0700793 }
794}
795
Alec Mouri363faf02021-01-29 16:34:55 -0800796void TimeStats::incrementJankyFrames(const JankyFramesInfo& info) {
Alec Mouri9a29e672020-09-14 12:39:14 -0700797 if (!mEnabled.load()) return;
798
799 ATRACE_CALL();
800 std::lock_guard<std::mutex> lock(mMutex);
801
Alec Mouri542de112020-11-13 12:07:32 -0800802 // Only update layer stats if we're already tracking the layer in TimeStats.
803 // Otherwise, continue tracking the statistic but use a default layer name instead.
Alec Mouri9a29e672020-09-14 12:39:14 -0700804 // As an implementation detail, we do this because this method is expected to be
Alec Mouri542de112020-11-13 12:07:32 -0800805 // called from FrameTimeline, whose jank classification includes transaction jank
806 // that occurs without a buffer. But, in general those layer names are not suitable as
807 // aggregation keys: e.g., it's normal and expected for Window Manager to include the hash code
808 // for an animation leash. So while we can show that jank in dumpsys, aggregating based on the
809 // layer blows up the stats size, so as a workaround drop those stats. This assumes that
810 // TimeStats will flush the first present fence for a layer *before* FrameTimeline does so that
811 // the first jank record is not dropped.
Alec Mouri9a29e672020-09-14 12:39:14 -0700812
Alec Mouri542de112020-11-13 12:07:32 -0800813 static const std::string kDefaultLayerName = "none";
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700814 constexpr GameMode kDefaultGameMode = GameMode::Unsupported;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800815
Alec Mouri363faf02021-01-29 16:34:55 -0800816 const int32_t refreshRateBucket =
Ady Abraham3403a3f2021-04-27 16:58:40 -0700817 clampToNearestBucket(info.refreshRate, REFRESH_RATE_BUCKET_WIDTH);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800818 const int32_t renderRateBucket =
Ady Abraham3403a3f2021-04-27 16:58:40 -0700819 clampToNearestBucket(info.renderRate ? *info.renderRate : info.refreshRate,
820 RENDER_RATE_BUCKET_WIDTH);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800821 const TimeStatsHelper::TimelineStatsKey timelineKey = {refreshRateBucket, renderRateBucket};
822
823 if (!mTimeStats.stats.count(timelineKey)) {
824 mTimeStats.stats[timelineKey].key = timelineKey;
Alec Mouri9a29e672020-09-14 12:39:14 -0700825 }
826
Alec Mouri7d436ec2021-01-27 20:40:50 -0800827 TimeStatsHelper::TimelineStats& timelineStats = mTimeStats.stats[timelineKey];
828
Alec Mouri363faf02021-01-29 16:34:55 -0800829 updateJankPayload<TimeStatsHelper::TimelineStats>(timelineStats, info.reasons);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800830
Adithya Srinivasan58069dc2021-06-04 20:37:02 +0000831 TimeStatsHelper::LayerStatsKey layerKey = {info.uid, info.layerName, info.gameMode};
Alec Mouri7d436ec2021-01-27 20:40:50 -0800832 if (!timelineStats.stats.count(layerKey)) {
Adithya Srinivasan58069dc2021-06-04 20:37:02 +0000833 layerKey = {info.uid, kDefaultLayerName, kDefaultGameMode};
Alec Mouri7d436ec2021-01-27 20:40:50 -0800834 timelineStats.stats[layerKey].displayRefreshRateBucket = refreshRateBucket;
835 timelineStats.stats[layerKey].renderRateBucket = renderRateBucket;
Alec Mouri363faf02021-01-29 16:34:55 -0800836 timelineStats.stats[layerKey].uid = info.uid;
Adithya Srinivasanf427f762021-06-15 19:46:26 +0000837 timelineStats.stats[layerKey].layerName = kDefaultLayerName;
838 timelineStats.stats[layerKey].gameMode = kDefaultGameMode;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800839 }
840
841 TimeStatsHelper::TimeStatsLayer& timeStatsLayer = timelineStats.stats[layerKey];
Alec Mouri363faf02021-01-29 16:34:55 -0800842 updateJankPayload<TimeStatsHelper::TimeStatsLayer>(timeStatsLayer, info.reasons);
843
844 if (info.reasons & kValidJankyReason) {
845 // TimeStats Histograms only retain positive values, so we don't need to check if these
846 // deadlines were really missed if we know that the frame had jank, since deadlines
847 // that were met will be dropped.
Ady Abraham3e8cc072021-05-11 16:29:54 -0700848 timelineStats.displayDeadlineDeltas.insert(toMs(info.displayDeadlineDelta));
849 timelineStats.displayPresentDeltas.insert(toMs(info.displayPresentJitter));
850 timeStatsLayer.deltas["appDeadlineDeltas"].insert(toMs(info.appDeadlineDelta));
Alec Mouri363faf02021-01-29 16:34:55 -0800851 }
Alec Mouri9a29e672020-09-14 12:39:14 -0700852}
853
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800854void TimeStats::onDestroy(int32_t layerId) {
Yiwei Zhangdc224042018-10-18 15:34:00 -0700855 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800856 ALOGV("[%d]-onDestroy", layerId);
Mikael Pessa90092f42019-08-26 17:22:04 -0700857 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800858 mTimeStatsTracker.erase(layerId);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700859}
860
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800861void TimeStats::removeTimeRecord(int32_t layerId, uint64_t frameNumber) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700862 if (!mEnabled.load()) return;
863
864 ATRACE_CALL();
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800865 ALOGV("[%d]-[%" PRIu64 "]-removeTimeRecord", layerId, frameNumber);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700866
867 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800868 if (!mTimeStatsTracker.count(layerId)) return;
869 LayerRecord& layerRecord = mTimeStatsTracker[layerId];
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700870 size_t removeAt = 0;
871 for (const TimeRecord& record : layerRecord.timeRecords) {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700872 if (record.frameTime.frameNumber == frameNumber) break;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700873 removeAt++;
874 }
875 if (removeAt == layerRecord.timeRecords.size()) return;
876 layerRecord.timeRecords.erase(layerRecord.timeRecords.begin() + removeAt);
877 if (layerRecord.waitData > static_cast<int32_t>(removeAt)) {
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700878 layerRecord.waitData--;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700879 }
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700880 layerRecord.droppedFrames++;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700881}
882
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700883void TimeStats::flushPowerTimeLocked() {
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700884 if (!mEnabled.load()) return;
885
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700886 nsecs_t curTime = systemTime();
887 // elapsedTime is in milliseconds.
888 int64_t elapsedTime = (curTime - mPowerTime.prevTime) / 1000000;
889
890 switch (mPowerTime.powerMode) {
Peiyong Lin65248e02020-04-18 21:15:07 -0700891 case PowerMode::ON:
Alec Mouri7d436ec2021-01-27 20:40:50 -0800892 mTimeStats.displayOnTimeLegacy += elapsedTime;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700893 break;
Peiyong Lin65248e02020-04-18 21:15:07 -0700894 case PowerMode::OFF:
895 case PowerMode::DOZE:
896 case PowerMode::DOZE_SUSPEND:
897 case PowerMode::ON_SUSPEND:
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700898 default:
899 break;
900 }
901
902 mPowerTime.prevTime = curTime;
903}
904
Peiyong Lin65248e02020-04-18 21:15:07 -0700905void TimeStats::setPowerMode(PowerMode powerMode) {
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700906 if (!mEnabled.load()) {
907 std::lock_guard<std::mutex> lock(mMutex);
908 mPowerTime.powerMode = powerMode;
909 return;
910 }
911
912 std::lock_guard<std::mutex> lock(mMutex);
913 if (powerMode == mPowerTime.powerMode) return;
914
915 flushPowerTimeLocked();
916 mPowerTime.powerMode = powerMode;
917}
918
Alec Mourifb571ea2019-01-24 18:42:10 -0800919void TimeStats::recordRefreshRate(uint32_t fps, nsecs_t duration) {
920 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800921 if (mTimeStats.refreshRateStatsLegacy.count(fps)) {
922 mTimeStats.refreshRateStatsLegacy[fps] += duration;
Alec Mourifb571ea2019-01-24 18:42:10 -0800923 } else {
Alec Mouri7d436ec2021-01-27 20:40:50 -0800924 mTimeStats.refreshRateStatsLegacy.insert({fps, duration});
Alec Mourifb571ea2019-01-24 18:42:10 -0800925 }
926}
927
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700928void TimeStats::flushAvailableGlobalRecordsToStatsLocked() {
929 ATRACE_CALL();
930
931 while (!mGlobalRecord.presentFences.empty()) {
932 const nsecs_t curPresentTime = mGlobalRecord.presentFences.front()->getSignalTime();
933 if (curPresentTime == Fence::SIGNAL_TIME_PENDING) break;
934
935 if (curPresentTime == Fence::SIGNAL_TIME_INVALID) {
936 ALOGE("GlobalPresentFence is invalid!");
937 mGlobalRecord.prevPresentTime = 0;
938 mGlobalRecord.presentFences.pop_front();
939 continue;
940 }
941
942 ALOGV("GlobalPresentFenceTime[%" PRId64 "]",
943 mGlobalRecord.presentFences.front()->getSignalTime());
944
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700945 if (mGlobalRecord.prevPresentTime != 0) {
946 const int32_t presentToPresentMs =
947 msBetween(mGlobalRecord.prevPresentTime, curPresentTime);
948 ALOGV("Global present2present[%d] prev[%" PRId64 "] curr[%" PRId64 "]",
949 presentToPresentMs, mGlobalRecord.prevPresentTime, curPresentTime);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800950 mTimeStats.presentToPresentLegacy.insert(presentToPresentMs);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700951 }
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700952
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700953 mGlobalRecord.prevPresentTime = curPresentTime;
954 mGlobalRecord.presentFences.pop_front();
955 }
Alec Mourie4034bb2019-11-19 12:45:54 -0800956 while (!mGlobalRecord.renderEngineDurations.empty()) {
957 const auto duration = mGlobalRecord.renderEngineDurations.front();
958 const auto& endTime = duration.endTime;
959
960 nsecs_t endNs = -1;
961
962 if (auto val = std::get_if<nsecs_t>(&endTime)) {
963 endNs = *val;
964 } else {
965 endNs = std::get<std::shared_ptr<FenceTime>>(endTime)->getSignalTime();
966 }
967
968 if (endNs == Fence::SIGNAL_TIME_PENDING) break;
969
970 if (endNs < 0) {
971 ALOGE("RenderEngineTiming is invalid!");
972 mGlobalRecord.renderEngineDurations.pop_front();
973 continue;
974 }
975
976 const int32_t renderEngineMs = msBetween(duration.startTime, endNs);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800977 mTimeStats.renderEngineTimingLegacy.insert(renderEngineMs);
Alec Mourie4034bb2019-11-19 12:45:54 -0800978
979 mGlobalRecord.renderEngineDurations.pop_front();
980 }
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700981}
982
983void TimeStats::setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) {
984 if (!mEnabled.load()) return;
985
986 ATRACE_CALL();
987 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700988 if (presentFence == nullptr || !presentFence->isValid()) {
989 mGlobalRecord.prevPresentTime = 0;
990 return;
991 }
992
Peiyong Lin65248e02020-04-18 21:15:07 -0700993 if (mPowerTime.powerMode != PowerMode::ON) {
994 // Try flushing the last present fence on PowerMode::ON.
Yiwei Zhange5c49d52018-10-29 00:15:31 -0700995 flushAvailableGlobalRecordsToStatsLocked();
996 mGlobalRecord.presentFences.clear();
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700997 mGlobalRecord.prevPresentTime = 0;
998 return;
999 }
1000
1001 if (mGlobalRecord.presentFences.size() == MAX_NUM_TIME_RECORDS) {
1002 // The front presentFence must be trapped in pending status in this
1003 // case. Try dequeuing the front one to recover.
1004 ALOGE("GlobalPresentFences is already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS);
1005 mGlobalRecord.prevPresentTime = 0;
1006 mGlobalRecord.presentFences.pop_front();
1007 }
1008
1009 mGlobalRecord.presentFences.emplace_back(presentFence);
1010 flushAvailableGlobalRecordsToStatsLocked();
1011}
1012
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001013void TimeStats::enable() {
1014 if (mEnabled.load()) return;
1015
1016 ATRACE_CALL();
1017
1018 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001019 mEnabled.store(true);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001020 mTimeStats.statsStartLegacy = static_cast<int64_t>(std::time(0));
Yiwei Zhang3a226d22018-10-16 09:23:03 -07001021 mPowerTime.prevTime = systemTime();
Yiwei Zhange5c49d52018-10-29 00:15:31 -07001022 ALOGD("Enabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001023}
1024
1025void TimeStats::disable() {
1026 if (!mEnabled.load()) return;
1027
1028 ATRACE_CALL();
1029
1030 std::lock_guard<std::mutex> lock(mMutex);
Yiwei Zhange5c49d52018-10-29 00:15:31 -07001031 flushPowerTimeLocked();
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001032 mEnabled.store(false);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001033 mTimeStats.statsEndLegacy = static_cast<int64_t>(std::time(0));
Yiwei Zhange5c49d52018-10-29 00:15:31 -07001034 ALOGD("Disabled");
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001035}
1036
Alec Mouri8e2f31b2020-01-16 22:04:35 +00001037void TimeStats::clearAll() {
1038 std::lock_guard<std::mutex> lock(mMutex);
Ady Abraham3403a3f2021-04-27 16:58:40 -07001039 mTimeStats.stats.clear();
Alec Mouri8e2f31b2020-01-16 22:04:35 +00001040 clearGlobalLocked();
1041 clearLayersLocked();
1042}
1043
1044void TimeStats::clearGlobalLocked() {
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001045 ATRACE_CALL();
1046
Alec Mouri7d436ec2021-01-27 20:40:50 -08001047 mTimeStats.statsStartLegacy = (mEnabled.load() ? static_cast<int64_t>(std::time(0)) : 0);
1048 mTimeStats.statsEndLegacy = 0;
1049 mTimeStats.totalFramesLegacy = 0;
1050 mTimeStats.missedFramesLegacy = 0;
1051 mTimeStats.clientCompositionFramesLegacy = 0;
1052 mTimeStats.clientCompositionReusedFramesLegacy = 0;
Robert Carra00eb142022-03-09 13:49:30 -08001053 mTimeStats.compositionStrategyChangesLegacy = 0;
Vishnu Nair9cf89262022-02-26 09:17:49 -08001054 mTimeStats.compositionStrategyPredictedLegacy = 0;
1055 mTimeStats.compositionStrategyPredictionSucceededLegacy = 0;
1056 mTimeStats.refreshRateSwitchesLegacy = 0;
Alec Mouri7d436ec2021-01-27 20:40:50 -08001057 mTimeStats.displayEventConnectionsCountLegacy = 0;
1058 mTimeStats.displayOnTimeLegacy = 0;
1059 mTimeStats.presentToPresentLegacy.hist.clear();
1060 mTimeStats.frameDurationLegacy.hist.clear();
1061 mTimeStats.renderEngineTimingLegacy.hist.clear();
1062 mTimeStats.refreshRateStatsLegacy.clear();
Yiwei Zhang3a226d22018-10-16 09:23:03 -07001063 mPowerTime.prevTime = systemTime();
Alec Mouri56e63852021-03-09 18:17:25 -08001064 for (auto& globalRecord : mTimeStats.stats) {
1065 globalRecord.second.clearGlobals();
1066 }
Yiwei Zhange5c49d52018-10-29 00:15:31 -07001067 mGlobalRecord.prevPresentTime = 0;
1068 mGlobalRecord.presentFences.clear();
Alec Mouri8e2f31b2020-01-16 22:04:35 +00001069 ALOGD("Cleared global stats");
1070}
1071
1072void TimeStats::clearLayersLocked() {
1073 ATRACE_CALL();
1074
1075 mTimeStatsTracker.clear();
Alec Mouri56e63852021-03-09 18:17:25 -08001076
1077 for (auto& globalRecord : mTimeStats.stats) {
1078 globalRecord.second.stats.clear();
1079 }
Alec Mouri8e2f31b2020-01-16 22:04:35 +00001080 ALOGD("Cleared layer stats");
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001081}
1082
1083bool TimeStats::isEnabled() {
1084 return mEnabled.load();
1085}
1086
Yiwei Zhang5434a782018-12-05 18:06:32 -08001087void TimeStats::dump(bool asProto, std::optional<uint32_t> maxLayers, std::string& result) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001088 ATRACE_CALL();
1089
1090 std::lock_guard<std::mutex> lock(mMutex);
Alec Mouri7d436ec2021-01-27 20:40:50 -08001091 if (mTimeStats.statsStartLegacy == 0) {
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001092 return;
1093 }
1094
Alec Mouri7d436ec2021-01-27 20:40:50 -08001095 mTimeStats.statsEndLegacy = static_cast<int64_t>(std::time(0));
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001096
Yiwei Zhang3a226d22018-10-16 09:23:03 -07001097 flushPowerTimeLocked();
1098
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001099 if (asProto) {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -07001100 ALOGD("Dumping TimeStats as proto");
Yiwei Zhangdc224042018-10-18 15:34:00 -07001101 SFTimeStatsGlobalProto timeStatsProto = mTimeStats.toProto(maxLayers);
Dominik Laskowski46470112019-08-02 13:13:11 -07001102 result.append(timeStatsProto.SerializeAsString());
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001103 } else {
Yiwei Zhang8a4015c2018-05-08 16:03:47 -07001104 ALOGD("Dumping TimeStats as text");
Yiwei Zhang5434a782018-12-05 18:06:32 -08001105 result.append(mTimeStats.toString(maxLayers));
Yiwei Zhang8a4015c2018-05-08 16:03:47 -07001106 result.append("\n");
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001107 }
1108}
1109
Alec Mourifb571ea2019-01-24 18:42:10 -08001110} // namespace impl
1111
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001112} // namespace android