Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 1 | /* |
| 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 Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 16 | |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 17 | #undef LOG_TAG |
| 18 | #define LOG_TAG "TimeStats" |
| 19 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 20 | |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 21 | #include <android-base/stringprintf.h> |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 22 | #include <log/log.h> |
Tej Singh | e275177 | 2021-04-06 22:05:29 -0700 | [diff] [blame] | 23 | #include <timestatsatomsproto/TimeStatsAtomsProtoHeader.h> |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 24 | #include <utils/String8.h> |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 25 | #include <utils/Timers.h> |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 26 | #include <utils/Trace.h> |
| 27 | |
| 28 | #include <algorithm> |
Alec Mouri | 9519bf1 | 2019-11-15 16:54:44 -0800 | [diff] [blame] | 29 | #include <chrono> |
Dominik Laskowski | f5d0ea5 | 2021-09-26 17:27:01 -0700 | [diff] [blame] | 30 | #include <unordered_map> |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 31 | |
Tej Singh | e275177 | 2021-04-06 22:05:29 -0700 | [diff] [blame] | 32 | #include "TimeStats.h" |
Alec Mouri | 9a29e67 | 2020-09-14 12:39:14 -0700 | [diff] [blame] | 33 | #include "timestatsproto/TimeStatsHelper.h" |
| 34 | |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 35 | namespace android { |
| 36 | |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 37 | namespace impl { |
| 38 | |
Alec Mouri | 3738434 | 2020-01-02 17:23:37 -0800 | [diff] [blame] | 39 | namespace { |
Alec Mouri | 8e2f31b | 2020-01-16 22:04:35 +0000 | [diff] [blame] | 40 | |
Tej Singh | e275177 | 2021-04-06 22:05:29 -0700 | [diff] [blame] | 41 | FrameTimingHistogram histogramToProto(const std::unordered_map<int32_t, int32_t>& histogram, |
| 42 | size_t maxPulledHistogramBuckets) { |
Alec Mouri | 3738434 | 2020-01-02 17:23:37 -0800 | [diff] [blame] | 43 | 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 Singh | e275177 | 2021-04-06 22:05:29 -0700 | [diff] [blame] | 49 | FrameTimingHistogram histogramProto; |
Alec Mouri | 3738434 | 2020-01-02 17:23:37 -0800 | [diff] [blame] | 50 | int histogramSize = 0; |
| 51 | for (const auto& bucket : buckets) { |
| 52 | if (++histogramSize > maxPulledHistogramBuckets) { |
| 53 | break; |
| 54 | } |
Tej Singh | e275177 | 2021-04-06 22:05:29 -0700 | [diff] [blame] | 55 | histogramProto.add_time_millis_buckets((int32_t)bucket.first); |
| 56 | histogramProto.add_frame_counts((int64_t)bucket.second); |
Alec Mouri | 3738434 | 2020-01-02 17:23:37 -0800 | [diff] [blame] | 57 | } |
Tej Singh | e275177 | 2021-04-06 22:05:29 -0700 | [diff] [blame] | 58 | return histogramProto; |
Alec Mouri | 3738434 | 2020-01-02 17:23:37 -0800 | [diff] [blame] | 59 | } |
Alec Mouri | 75de8f2 | 2021-01-20 14:53:44 -0800 | [diff] [blame] | 60 | |
Dominik Laskowski | f5d0ea5 | 2021-09-26 17:27:01 -0700 | [diff] [blame] | 61 | SurfaceflingerStatsLayerInfo_GameMode gameModeToProto(GameMode gameMode) { |
Adithya Srinivasan | 58069dc | 2021-06-04 20:37:02 +0000 | [diff] [blame] | 62 | switch (gameMode) { |
Dominik Laskowski | f5d0ea5 | 2021-09-26 17:27:01 -0700 | [diff] [blame] | 63 | case GameMode::Unsupported: |
Adithya Srinivasan | 58069dc | 2021-06-04 20:37:02 +0000 | [diff] [blame] | 64 | return SurfaceflingerStatsLayerInfo::GAME_MODE_UNSUPPORTED; |
Dominik Laskowski | f5d0ea5 | 2021-09-26 17:27:01 -0700 | [diff] [blame] | 65 | case GameMode::Standard: |
Adithya Srinivasan | 58069dc | 2021-06-04 20:37:02 +0000 | [diff] [blame] | 66 | return SurfaceflingerStatsLayerInfo::GAME_MODE_STANDARD; |
Dominik Laskowski | f5d0ea5 | 2021-09-26 17:27:01 -0700 | [diff] [blame] | 67 | case GameMode::Performance: |
Adithya Srinivasan | 58069dc | 2021-06-04 20:37:02 +0000 | [diff] [blame] | 68 | return SurfaceflingerStatsLayerInfo::GAME_MODE_PERFORMANCE; |
Dominik Laskowski | f5d0ea5 | 2021-09-26 17:27:01 -0700 | [diff] [blame] | 69 | case GameMode::Battery: |
Adithya Srinivasan | 58069dc | 2021-06-04 20:37:02 +0000 | [diff] [blame] | 70 | return SurfaceflingerStatsLayerInfo::GAME_MODE_BATTERY; |
Xiang Wang | f0c5cca | 2022-11-09 18:03:09 -0800 | [diff] [blame^] | 71 | case GameMode::Custom: |
| 72 | return SurfaceflingerStatsLayerInfo::GAME_MODE_CUSTOM; |
Adithya Srinivasan | 58069dc | 2021-06-04 20:37:02 +0000 | [diff] [blame] | 73 | default: |
| 74 | return SurfaceflingerStatsLayerInfo::GAME_MODE_UNSPECIFIED; |
| 75 | } |
| 76 | } |
| 77 | |
Tej Singh | e275177 | 2021-04-06 22:05:29 -0700 | [diff] [blame] | 78 | SurfaceflingerStatsLayerInfo_SetFrameRateVote frameRateVoteToProto( |
| 79 | const TimeStats::SetFrameRateVote& setFrameRateVote) { |
| 80 | using FrameRateCompatibilityEnum = |
| 81 | SurfaceflingerStatsLayerInfo::SetFrameRateVote::FrameRateCompatibility; |
| 82 | using SeamlessnessEnum = SurfaceflingerStatsLayerInfo::SetFrameRateVote::Seamlessness; |
Alec Mouri | 75de8f2 | 2021-01-20 14:53:44 -0800 | [diff] [blame] | 83 | |
Tej Singh | e275177 | 2021-04-06 22:05:29 -0700 | [diff] [blame] | 84 | SurfaceflingerStatsLayerInfo_SetFrameRateVote proto; |
| 85 | proto.set_frame_rate(setFrameRateVote.frameRate); |
| 86 | proto.set_frame_rate_compatibility( |
| 87 | static_cast<FrameRateCompatibilityEnum>(setFrameRateVote.frameRateCompatibility)); |
| 88 | proto.set_seamlessness(static_cast<SeamlessnessEnum>(setFrameRateVote.seamlessness)); |
| 89 | return proto; |
Alec Mouri | 75de8f2 | 2021-01-20 14:53:44 -0800 | [diff] [blame] | 90 | } |
Alec Mouri | 3738434 | 2020-01-02 17:23:37 -0800 | [diff] [blame] | 91 | } // namespace |
| 92 | |
Tej Singh | e275177 | 2021-04-06 22:05:29 -0700 | [diff] [blame] | 93 | bool TimeStats::populateGlobalAtom(std::string* pulledData) { |
Alec Mouri | dfad900 | 2020-02-12 17:49:09 -0800 | [diff] [blame] | 94 | std::lock_guard<std::mutex> lock(mMutex); |
| 95 | |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 96 | if (mTimeStats.statsStartLegacy == 0) { |
Tej Singh | e275177 | 2021-04-06 22:05:29 -0700 | [diff] [blame] | 97 | return false; |
Alec Mouri | dfad900 | 2020-02-12 17:49:09 -0800 | [diff] [blame] | 98 | } |
| 99 | flushPowerTimeLocked(); |
Tej Singh | e275177 | 2021-04-06 22:05:29 -0700 | [diff] [blame] | 100 | SurfaceflingerStatsGlobalInfoWrapper atomList; |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 101 | for (const auto& globalSlice : mTimeStats.stats) { |
Tej Singh | e275177 | 2021-04-06 22:05:29 -0700 | [diff] [blame] | 102 | SurfaceflingerStatsGlobalInfo* atom = atomList.add_atom(); |
| 103 | atom->set_total_frames(mTimeStats.totalFramesLegacy); |
| 104 | atom->set_missed_frames(mTimeStats.missedFramesLegacy); |
| 105 | atom->set_client_composition_frames(mTimeStats.clientCompositionFramesLegacy); |
| 106 | atom->set_display_on_millis(mTimeStats.displayOnTimeLegacy); |
| 107 | atom->set_animation_millis(mTimeStats.presentToPresentLegacy.totalTime()); |
| 108 | atom->set_event_connection_count(mTimeStats.displayEventConnectionsCountLegacy); |
| 109 | *atom->mutable_frame_duration() = |
| 110 | histogramToProto(mTimeStats.frameDurationLegacy.hist, mMaxPulledHistogramBuckets); |
| 111 | *atom->mutable_render_engine_timing() = |
| 112 | histogramToProto(mTimeStats.renderEngineTimingLegacy.hist, |
| 113 | mMaxPulledHistogramBuckets); |
| 114 | atom->set_total_timeline_frames(globalSlice.second.jankPayload.totalFrames); |
| 115 | atom->set_total_janky_frames(globalSlice.second.jankPayload.totalJankyFrames); |
| 116 | atom->set_total_janky_frames_with_long_cpu(globalSlice.second.jankPayload.totalSFLongCpu); |
| 117 | atom->set_total_janky_frames_with_long_gpu(globalSlice.second.jankPayload.totalSFLongGpu); |
| 118 | atom->set_total_janky_frames_sf_unattributed( |
| 119 | globalSlice.second.jankPayload.totalSFUnattributed); |
| 120 | atom->set_total_janky_frames_app_unattributed( |
| 121 | globalSlice.second.jankPayload.totalAppUnattributed); |
| 122 | atom->set_total_janky_frames_sf_scheduling( |
| 123 | globalSlice.second.jankPayload.totalSFScheduling); |
| 124 | atom->set_total_jank_frames_sf_prediction_error( |
| 125 | globalSlice.second.jankPayload.totalSFPredictionError); |
| 126 | atom->set_total_jank_frames_app_buffer_stuffing( |
| 127 | globalSlice.second.jankPayload.totalAppBufferStuffing); |
| 128 | atom->set_display_refresh_rate_bucket(globalSlice.first.displayRefreshRateBucket); |
| 129 | *atom->mutable_sf_deadline_misses() = |
| 130 | histogramToProto(globalSlice.second.displayDeadlineDeltas.hist, |
| 131 | mMaxPulledHistogramBuckets); |
| 132 | *atom->mutable_sf_prediction_errors() = |
| 133 | histogramToProto(globalSlice.second.displayPresentDeltas.hist, |
| 134 | mMaxPulledHistogramBuckets); |
| 135 | atom->set_render_rate_bucket(globalSlice.first.renderRateBucket); |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Tej Singh | e275177 | 2021-04-06 22:05:29 -0700 | [diff] [blame] | 138 | // Always clear data. |
Alec Mouri | dfad900 | 2020-02-12 17:49:09 -0800 | [diff] [blame] | 139 | clearGlobalLocked(); |
| 140 | |
Tej Singh | e275177 | 2021-04-06 22:05:29 -0700 | [diff] [blame] | 141 | return atomList.SerializeToString(pulledData); |
Alec Mouri | dfad900 | 2020-02-12 17:49:09 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Tej Singh | e275177 | 2021-04-06 22:05:29 -0700 | [diff] [blame] | 144 | bool TimeStats::populateLayerAtom(std::string* pulledData) { |
Alec Mouri | 3738434 | 2020-01-02 17:23:37 -0800 | [diff] [blame] | 145 | std::lock_guard<std::mutex> lock(mMutex); |
| 146 | |
Alec Mouri | 363faf0 | 2021-01-29 16:34:55 -0800 | [diff] [blame] | 147 | std::vector<TimeStatsHelper::TimeStatsLayer*> dumpStats; |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 148 | uint32_t numLayers = 0; |
| 149 | for (const auto& globalSlice : mTimeStats.stats) { |
| 150 | numLayers += globalSlice.second.stats.size(); |
| 151 | } |
| 152 | |
| 153 | dumpStats.reserve(numLayers); |
| 154 | |
Alec Mouri | 363faf0 | 2021-01-29 16:34:55 -0800 | [diff] [blame] | 155 | for (auto& globalSlice : mTimeStats.stats) { |
| 156 | for (auto& layerSlice : globalSlice.second.stats) { |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 157 | dumpStats.push_back(&layerSlice.second); |
| 158 | } |
Alec Mouri | 3738434 | 2020-01-02 17:23:37 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | std::sort(dumpStats.begin(), dumpStats.end(), |
| 162 | [](TimeStatsHelper::TimeStatsLayer const* l, |
| 163 | TimeStatsHelper::TimeStatsLayer const* r) { |
| 164 | return l->totalFrames > r->totalFrames; |
| 165 | }); |
| 166 | |
| 167 | if (mMaxPulledLayers < dumpStats.size()) { |
| 168 | dumpStats.resize(mMaxPulledLayers); |
| 169 | } |
| 170 | |
Tej Singh | e275177 | 2021-04-06 22:05:29 -0700 | [diff] [blame] | 171 | SurfaceflingerStatsLayerInfoWrapper atomList; |
Alec Mouri | 363faf0 | 2021-01-29 16:34:55 -0800 | [diff] [blame] | 172 | for (auto& layer : dumpStats) { |
Tej Singh | e275177 | 2021-04-06 22:05:29 -0700 | [diff] [blame] | 173 | SurfaceflingerStatsLayerInfo* atom = atomList.add_atom(); |
| 174 | atom->set_layer_name(layer->layerName); |
| 175 | atom->set_total_frames(layer->totalFrames); |
| 176 | atom->set_dropped_frames(layer->droppedFrames); |
| 177 | const auto& present2PresentHist = layer->deltas.find("present2present"); |
| 178 | if (present2PresentHist != layer->deltas.cend()) { |
| 179 | *atom->mutable_present_to_present() = |
| 180 | histogramToProto(present2PresentHist->second.hist, mMaxPulledHistogramBuckets); |
| 181 | } |
| 182 | const auto& post2presentHist = layer->deltas.find("post2present"); |
| 183 | if (post2presentHist != layer->deltas.cend()) { |
| 184 | *atom->mutable_post_to_present() = |
| 185 | histogramToProto(post2presentHist->second.hist, mMaxPulledHistogramBuckets); |
| 186 | } |
| 187 | const auto& acquire2presentHist = layer->deltas.find("acquire2present"); |
| 188 | if (acquire2presentHist != layer->deltas.cend()) { |
| 189 | *atom->mutable_acquire_to_present() = |
| 190 | histogramToProto(acquire2presentHist->second.hist, mMaxPulledHistogramBuckets); |
| 191 | } |
| 192 | const auto& latch2presentHist = layer->deltas.find("latch2present"); |
| 193 | if (latch2presentHist != layer->deltas.cend()) { |
| 194 | *atom->mutable_latch_to_present() = |
| 195 | histogramToProto(latch2presentHist->second.hist, mMaxPulledHistogramBuckets); |
| 196 | } |
| 197 | const auto& desired2presentHist = layer->deltas.find("desired2present"); |
| 198 | if (desired2presentHist != layer->deltas.cend()) { |
| 199 | *atom->mutable_desired_to_present() = |
| 200 | histogramToProto(desired2presentHist->second.hist, mMaxPulledHistogramBuckets); |
| 201 | } |
| 202 | const auto& post2acquireHist = layer->deltas.find("post2acquire"); |
| 203 | if (post2acquireHist != layer->deltas.cend()) { |
| 204 | *atom->mutable_post_to_acquire() = |
| 205 | histogramToProto(post2acquireHist->second.hist, mMaxPulledHistogramBuckets); |
Alec Mouri | 3738434 | 2020-01-02 17:23:37 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Tej Singh | e275177 | 2021-04-06 22:05:29 -0700 | [diff] [blame] | 208 | atom->set_late_acquire_frames(layer->lateAcquireFrames); |
| 209 | atom->set_bad_desired_present_frames(layer->badDesiredPresentFrames); |
| 210 | atom->set_uid(layer->uid); |
| 211 | atom->set_total_timeline_frames(layer->jankPayload.totalFrames); |
| 212 | atom->set_total_janky_frames(layer->jankPayload.totalJankyFrames); |
| 213 | atom->set_total_janky_frames_with_long_cpu(layer->jankPayload.totalSFLongCpu); |
| 214 | atom->set_total_janky_frames_with_long_gpu(layer->jankPayload.totalSFLongGpu); |
| 215 | atom->set_total_janky_frames_sf_unattributed(layer->jankPayload.totalSFUnattributed); |
| 216 | atom->set_total_janky_frames_app_unattributed(layer->jankPayload.totalAppUnattributed); |
| 217 | atom->set_total_janky_frames_sf_scheduling(layer->jankPayload.totalSFScheduling); |
| 218 | atom->set_total_jank_frames_sf_prediction_error(layer->jankPayload.totalSFPredictionError); |
| 219 | atom->set_total_jank_frames_app_buffer_stuffing(layer->jankPayload.totalAppBufferStuffing); |
| 220 | atom->set_display_refresh_rate_bucket(layer->displayRefreshRateBucket); |
| 221 | atom->set_render_rate_bucket(layer->renderRateBucket); |
| 222 | *atom->mutable_set_frame_rate_vote() = frameRateVoteToProto(layer->setFrameRateVote); |
| 223 | *atom->mutable_app_deadline_misses() = |
| 224 | histogramToProto(layer->deltas["appDeadlineDeltas"].hist, |
| 225 | mMaxPulledHistogramBuckets); |
Adithya Srinivasan | 58069dc | 2021-06-04 20:37:02 +0000 | [diff] [blame] | 226 | atom->set_game_mode(gameModeToProto(layer->gameMode)); |
Alec Mouri | 3738434 | 2020-01-02 17:23:37 -0800 | [diff] [blame] | 227 | } |
Tej Singh | e275177 | 2021-04-06 22:05:29 -0700 | [diff] [blame] | 228 | |
| 229 | // Always clear data. |
Alec Mouri | 3738434 | 2020-01-02 17:23:37 -0800 | [diff] [blame] | 230 | clearLayersLocked(); |
| 231 | |
Tej Singh | e275177 | 2021-04-06 22:05:29 -0700 | [diff] [blame] | 232 | return atomList.SerializeToString(pulledData); |
Alec Mouri | 3738434 | 2020-01-02 17:23:37 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Tej Singh | e275177 | 2021-04-06 22:05:29 -0700 | [diff] [blame] | 235 | TimeStats::TimeStats() : TimeStats(std::nullopt, std::nullopt) {} |
Alec Mouri | 3738434 | 2020-01-02 17:23:37 -0800 | [diff] [blame] | 236 | |
Tej Singh | e275177 | 2021-04-06 22:05:29 -0700 | [diff] [blame] | 237 | TimeStats::TimeStats(std::optional<size_t> maxPulledLayers, |
Alec Mouri | 3738434 | 2020-01-02 17:23:37 -0800 | [diff] [blame] | 238 | std::optional<size_t> maxPulledHistogramBuckets) { |
Alec Mouri | 3738434 | 2020-01-02 17:23:37 -0800 | [diff] [blame] | 239 | if (maxPulledLayers) { |
| 240 | mMaxPulledLayers = *maxPulledLayers; |
| 241 | } |
| 242 | |
| 243 | if (maxPulledHistogramBuckets) { |
| 244 | mMaxPulledHistogramBuckets = *maxPulledHistogramBuckets; |
| 245 | } |
Alec Mouri | 8e2f31b | 2020-01-16 22:04:35 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Tej Singh | e275177 | 2021-04-06 22:05:29 -0700 | [diff] [blame] | 248 | bool TimeStats::onPullAtom(const int atomId, std::string* pulledData) { |
| 249 | bool success = false; |
| 250 | if (atomId == 10062) { // SURFACEFLINGER_STATS_GLOBAL_INFO |
| 251 | success = populateGlobalAtom(pulledData); |
| 252 | } else if (atomId == 10063) { // SURFACEFLINGER_STATS_LAYER_INFO |
| 253 | success = populateLayerAtom(pulledData); |
| 254 | } |
Alec Mouri | 3ecd5cd | 2020-01-29 12:53:07 -0800 | [diff] [blame] | 255 | |
Tej Singh | e275177 | 2021-04-06 22:05:29 -0700 | [diff] [blame] | 256 | // Enable timestats now. The first full pull for a given build is expected to |
| 257 | // have empty or very little stats, as stats are first enabled after the |
| 258 | // first pull is completed for either the global or layer stats. |
| 259 | enable(); |
| 260 | return success; |
Alec Mouri | b3885ad | 2019-09-06 17:08:55 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Dominik Laskowski | c286714 | 2019-01-21 11:33:38 -0800 | [diff] [blame] | 263 | void TimeStats::parseArgs(bool asProto, const Vector<String16>& args, std::string& result) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 264 | ATRACE_CALL(); |
| 265 | |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 266 | std::unordered_map<std::string, int32_t> argsMap; |
Dominik Laskowski | c286714 | 2019-01-21 11:33:38 -0800 | [diff] [blame] | 267 | for (size_t index = 0; index < args.size(); ++index) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 268 | argsMap[std::string(String8(args[index]).c_str())] = index; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | if (argsMap.count("-disable")) { |
| 272 | disable(); |
| 273 | } |
| 274 | |
| 275 | if (argsMap.count("-dump")) { |
Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 276 | std::optional<uint32_t> maxLayers = std::nullopt; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 277 | auto iter = argsMap.find("-maxlayers"); |
| 278 | if (iter != argsMap.end() && iter->second + 1 < static_cast<int32_t>(args.size())) { |
Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 279 | int64_t value = strtol(String8(args[iter->second + 1]).c_str(), nullptr, 10); |
| 280 | value = std::clamp(value, int64_t(0), int64_t(UINT32_MAX)); |
| 281 | maxLayers = static_cast<uint32_t>(value); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 284 | dump(asProto, maxLayers, result); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | if (argsMap.count("-clear")) { |
Alec Mouri | 8e2f31b | 2020-01-16 22:04:35 +0000 | [diff] [blame] | 288 | clearAll(); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | if (argsMap.count("-enable")) { |
| 292 | enable(); |
| 293 | } |
| 294 | } |
| 295 | |
Yiwei Zhang | 7eb58b7 | 2019-04-22 19:00:02 -0700 | [diff] [blame] | 296 | std::string TimeStats::miniDump() { |
| 297 | ATRACE_CALL(); |
| 298 | |
| 299 | std::string result = "TimeStats miniDump:\n"; |
| 300 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | e926ab5 | 2019-08-14 15:16:00 -0700 | [diff] [blame] | 301 | android::base::StringAppendF(&result, "Number of layers currently being tracked is %zu\n", |
Yiwei Zhang | 7eb58b7 | 2019-04-22 19:00:02 -0700 | [diff] [blame] | 302 | mTimeStatsTracker.size()); |
Yiwei Zhang | e926ab5 | 2019-08-14 15:16:00 -0700 | [diff] [blame] | 303 | android::base::StringAppendF(&result, "Number of layers in the stats pool is %zu\n", |
| 304 | mTimeStats.stats.size()); |
Yiwei Zhang | 7eb58b7 | 2019-04-22 19:00:02 -0700 | [diff] [blame] | 305 | return result; |
| 306 | } |
| 307 | |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 308 | void TimeStats::incrementTotalFrames() { |
| 309 | if (!mEnabled.load()) return; |
| 310 | |
| 311 | ATRACE_CALL(); |
| 312 | |
| 313 | std::lock_guard<std::mutex> lock(mMutex); |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 314 | mTimeStats.totalFramesLegacy++; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Yiwei Zhang | 621f9d4 | 2018-05-07 10:40:55 -0700 | [diff] [blame] | 317 | void TimeStats::incrementMissedFrames() { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 318 | if (!mEnabled.load()) return; |
| 319 | |
| 320 | ATRACE_CALL(); |
| 321 | |
| 322 | std::lock_guard<std::mutex> lock(mMutex); |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 323 | mTimeStats.missedFramesLegacy++; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Vishnu Nair | 9cf8926 | 2022-02-26 09:17:49 -0800 | [diff] [blame] | 326 | void TimeStats::pushCompositionStrategyState(const TimeStats::ClientCompositionRecord& record) { |
| 327 | if (!mEnabled.load() || !record.hasInterestingData()) { |
| 328 | return; |
| 329 | } |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 330 | |
| 331 | ATRACE_CALL(); |
| 332 | |
| 333 | std::lock_guard<std::mutex> lock(mMutex); |
Vishnu Nair | 9cf8926 | 2022-02-26 09:17:49 -0800 | [diff] [blame] | 334 | if (record.changed) mTimeStats.compositionStrategyChangesLegacy++; |
| 335 | if (record.hadClientComposition) mTimeStats.clientCompositionFramesLegacy++; |
| 336 | if (record.reused) mTimeStats.clientCompositionReusedFramesLegacy++; |
| 337 | if (record.predicted) mTimeStats.compositionStrategyPredictedLegacy++; |
| 338 | if (record.predictionSucceeded) mTimeStats.compositionStrategyPredictionSucceededLegacy++; |
Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 339 | } |
| 340 | |
Alec Mouri | 8de697e | 2020-03-19 10:52:01 -0700 | [diff] [blame] | 341 | void TimeStats::incrementRefreshRateSwitches() { |
| 342 | if (!mEnabled.load()) return; |
| 343 | |
| 344 | ATRACE_CALL(); |
| 345 | |
| 346 | std::lock_guard<std::mutex> lock(mMutex); |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 347 | mTimeStats.refreshRateSwitchesLegacy++; |
Alec Mouri | 8de697e | 2020-03-19 10:52:01 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 350 | void TimeStats::recordDisplayEventConnectionCount(int32_t count) { |
| 351 | if (!mEnabled.load()) return; |
| 352 | |
| 353 | ATRACE_CALL(); |
| 354 | |
| 355 | std::lock_guard<std::mutex> lock(mMutex); |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 356 | mTimeStats.displayEventConnectionsCountLegacy = |
| 357 | std::max(mTimeStats.displayEventConnectionsCountLegacy, count); |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 358 | } |
| 359 | |
Ady Abraham | 3e8cc07 | 2021-05-11 16:29:54 -0700 | [diff] [blame] | 360 | static int32_t toMs(nsecs_t nanos) { |
| 361 | int64_t millis = |
| 362 | std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::nanoseconds(nanos)) |
| 363 | .count(); |
| 364 | millis = std::clamp(millis, int64_t(INT32_MIN), int64_t(INT32_MAX)); |
| 365 | return static_cast<int32_t>(millis); |
| 366 | } |
| 367 | |
Alec Mouri | 9519bf1 | 2019-11-15 16:54:44 -0800 | [diff] [blame] | 368 | static int32_t msBetween(nsecs_t start, nsecs_t end) { |
Ady Abraham | 3e8cc07 | 2021-05-11 16:29:54 -0700 | [diff] [blame] | 369 | return toMs(end - start); |
Alec Mouri | 9519bf1 | 2019-11-15 16:54:44 -0800 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | void TimeStats::recordFrameDuration(nsecs_t startTime, nsecs_t endTime) { |
| 373 | if (!mEnabled.load()) return; |
| 374 | |
| 375 | std::lock_guard<std::mutex> lock(mMutex); |
Peiyong Lin | 65248e0 | 2020-04-18 21:15:07 -0700 | [diff] [blame] | 376 | if (mPowerTime.powerMode == PowerMode::ON) { |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 377 | mTimeStats.frameDurationLegacy.insert(msBetween(startTime, endTime)); |
Alec Mouri | 9519bf1 | 2019-11-15 16:54:44 -0800 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | |
Alec Mouri | e4034bb | 2019-11-19 12:45:54 -0800 | [diff] [blame] | 381 | void TimeStats::recordRenderEngineDuration(nsecs_t startTime, nsecs_t endTime) { |
| 382 | if (!mEnabled.load()) return; |
| 383 | |
| 384 | std::lock_guard<std::mutex> lock(mMutex); |
| 385 | if (mGlobalRecord.renderEngineDurations.size() == MAX_NUM_TIME_RECORDS) { |
| 386 | ALOGE("RenderEngineTimes are already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS); |
| 387 | mGlobalRecord.renderEngineDurations.pop_front(); |
| 388 | } |
| 389 | mGlobalRecord.renderEngineDurations.push_back({startTime, endTime}); |
| 390 | } |
| 391 | |
| 392 | void TimeStats::recordRenderEngineDuration(nsecs_t startTime, |
| 393 | const std::shared_ptr<FenceTime>& endTime) { |
| 394 | if (!mEnabled.load()) return; |
| 395 | |
| 396 | std::lock_guard<std::mutex> lock(mMutex); |
| 397 | if (mGlobalRecord.renderEngineDurations.size() == MAX_NUM_TIME_RECORDS) { |
| 398 | ALOGE("RenderEngineTimes are already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS); |
| 399 | mGlobalRecord.renderEngineDurations.pop_front(); |
| 400 | } |
| 401 | mGlobalRecord.renderEngineDurations.push_back({startTime, endTime}); |
| 402 | } |
| 403 | |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 404 | bool TimeStats::recordReadyLocked(int32_t layerId, TimeRecord* timeRecord) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 405 | if (!timeRecord->ready) { |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 406 | ALOGV("[%d]-[%" PRIu64 "]-presentFence is still not received", layerId, |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 407 | timeRecord->frameTime.frameNumber); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 408 | return false; |
| 409 | } |
| 410 | |
| 411 | if (timeRecord->acquireFence != nullptr) { |
| 412 | if (timeRecord->acquireFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) { |
| 413 | return false; |
| 414 | } |
| 415 | if (timeRecord->acquireFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) { |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 416 | timeRecord->frameTime.acquireTime = timeRecord->acquireFence->getSignalTime(); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 417 | timeRecord->acquireFence = nullptr; |
| 418 | } else { |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 419 | ALOGV("[%d]-[%" PRIu64 "]-acquireFence signal time is invalid", layerId, |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 420 | timeRecord->frameTime.frameNumber); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
| 424 | if (timeRecord->presentFence != nullptr) { |
| 425 | if (timeRecord->presentFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) { |
| 426 | return false; |
| 427 | } |
| 428 | if (timeRecord->presentFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) { |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 429 | timeRecord->frameTime.presentTime = timeRecord->presentFence->getSignalTime(); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 430 | timeRecord->presentFence = nullptr; |
| 431 | } else { |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 432 | ALOGV("[%d]-[%" PRIu64 "]-presentFence signal time invalid", layerId, |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 433 | timeRecord->frameTime.frameNumber); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
| 437 | return true; |
| 438 | } |
| 439 | |
Ady Abraham | 3403a3f | 2021-04-27 16:58:40 -0700 | [diff] [blame] | 440 | static int32_t clampToNearestBucket(Fps fps, size_t bucketWidth) { |
| 441 | return std::round(fps.getValue() / bucketWidth) * bucketWidth; |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | void TimeStats::flushAvailableRecordsToStatsLocked(int32_t layerId, Fps displayRefreshRate, |
Ady Abraham | 8b9e612 | 2021-01-26 19:11:45 -0800 | [diff] [blame] | 445 | std::optional<Fps> renderRate, |
Adithya Srinivasan | 58069dc | 2021-06-04 20:37:02 +0000 | [diff] [blame] | 446 | SetFrameRateVote frameRateVote, |
Dominik Laskowski | f5d0ea5 | 2021-09-26 17:27:01 -0700 | [diff] [blame] | 447 | GameMode gameMode) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 448 | ATRACE_CALL(); |
Ady Abraham | 8b9e612 | 2021-01-26 19:11:45 -0800 | [diff] [blame] | 449 | ALOGV("[%d]-flushAvailableRecordsToStatsLocked", layerId); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 450 | |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 451 | LayerRecord& layerRecord = mTimeStatsTracker[layerId]; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 452 | TimeRecord& prevTimeRecord = layerRecord.prevTimeRecord; |
Yiwei Zhang | c5f2c45 | 2018-05-08 16:31:56 -0700 | [diff] [blame] | 453 | std::deque<TimeRecord>& timeRecords = layerRecord.timeRecords; |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 454 | const int32_t refreshRateBucket = |
Ady Abraham | 3403a3f | 2021-04-27 16:58:40 -0700 | [diff] [blame] | 455 | clampToNearestBucket(displayRefreshRate, REFRESH_RATE_BUCKET_WIDTH); |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 456 | const int32_t renderRateBucket = |
Ady Abraham | 3403a3f | 2021-04-27 16:58:40 -0700 | [diff] [blame] | 457 | clampToNearestBucket(renderRate ? *renderRate : displayRefreshRate, |
| 458 | RENDER_RATE_BUCKET_WIDTH); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 459 | while (!timeRecords.empty()) { |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 460 | if (!recordReadyLocked(layerId, &timeRecords[0])) break; |
| 461 | ALOGV("[%d]-[%" PRIu64 "]-presentFenceTime[%" PRId64 "]", layerId, |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 462 | timeRecords[0].frameTime.frameNumber, timeRecords[0].frameTime.presentTime); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 463 | |
| 464 | if (prevTimeRecord.ready) { |
Alec Mouri | 9a29e67 | 2020-09-14 12:39:14 -0700 | [diff] [blame] | 465 | uid_t uid = layerRecord.uid; |
Yiwei Zhang | eafa5cc | 2019-07-26 15:06:25 -0700 | [diff] [blame] | 466 | const std::string& layerName = layerRecord.layerName; |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 467 | TimeStatsHelper::TimelineStatsKey timelineKey = {refreshRateBucket, renderRateBucket}; |
| 468 | if (!mTimeStats.stats.count(timelineKey)) { |
| 469 | mTimeStats.stats[timelineKey].key = timelineKey; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 470 | } |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 471 | |
| 472 | TimeStatsHelper::TimelineStats& displayStats = mTimeStats.stats[timelineKey]; |
| 473 | |
Adithya Srinivasan | 58069dc | 2021-06-04 20:37:02 +0000 | [diff] [blame] | 474 | TimeStatsHelper::LayerStatsKey layerKey = {uid, layerName, gameMode}; |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 475 | if (!displayStats.stats.count(layerKey)) { |
| 476 | displayStats.stats[layerKey].displayRefreshRateBucket = refreshRateBucket; |
| 477 | displayStats.stats[layerKey].renderRateBucket = renderRateBucket; |
| 478 | displayStats.stats[layerKey].uid = uid; |
| 479 | displayStats.stats[layerKey].layerName = layerName; |
Adithya Srinivasan | 58069dc | 2021-06-04 20:37:02 +0000 | [diff] [blame] | 480 | displayStats.stats[layerKey].gameMode = gameMode; |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 481 | } |
Ady Abraham | 8b9e612 | 2021-01-26 19:11:45 -0800 | [diff] [blame] | 482 | if (frameRateVote.frameRate > 0.0f) { |
| 483 | displayStats.stats[layerKey].setFrameRateVote = frameRateVote; |
| 484 | } |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 485 | TimeStatsHelper::TimeStatsLayer& timeStatsLayer = displayStats.stats[layerKey]; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 486 | timeStatsLayer.totalFrames++; |
Yiwei Zhang | eaeea06 | 2018-06-28 14:46:51 -0700 | [diff] [blame] | 487 | timeStatsLayer.droppedFrames += layerRecord.droppedFrames; |
Alec Mouri | 91f6df3 | 2020-01-30 08:48:58 -0800 | [diff] [blame] | 488 | timeStatsLayer.lateAcquireFrames += layerRecord.lateAcquireFrames; |
| 489 | timeStatsLayer.badDesiredPresentFrames += layerRecord.badDesiredPresentFrames; |
| 490 | |
Yiwei Zhang | eaeea06 | 2018-06-28 14:46:51 -0700 | [diff] [blame] | 491 | layerRecord.droppedFrames = 0; |
Alec Mouri | 91f6df3 | 2020-01-30 08:48:58 -0800 | [diff] [blame] | 492 | layerRecord.lateAcquireFrames = 0; |
| 493 | layerRecord.badDesiredPresentFrames = 0; |
Yiwei Zhang | eaeea06 | 2018-06-28 14:46:51 -0700 | [diff] [blame] | 494 | |
| 495 | const int32_t postToAcquireMs = msBetween(timeRecords[0].frameTime.postTime, |
| 496 | timeRecords[0].frameTime.acquireTime); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 497 | ALOGV("[%d]-[%" PRIu64 "]-post2acquire[%d]", layerId, |
Yiwei Zhang | eaeea06 | 2018-06-28 14:46:51 -0700 | [diff] [blame] | 498 | timeRecords[0].frameTime.frameNumber, postToAcquireMs); |
| 499 | timeStatsLayer.deltas["post2acquire"].insert(postToAcquireMs); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 500 | |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 501 | const int32_t postToPresentMs = msBetween(timeRecords[0].frameTime.postTime, |
| 502 | timeRecords[0].frameTime.presentTime); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 503 | ALOGV("[%d]-[%" PRIu64 "]-post2present[%d]", layerId, |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 504 | timeRecords[0].frameTime.frameNumber, postToPresentMs); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 505 | timeStatsLayer.deltas["post2present"].insert(postToPresentMs); |
| 506 | |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 507 | const int32_t acquireToPresentMs = msBetween(timeRecords[0].frameTime.acquireTime, |
| 508 | timeRecords[0].frameTime.presentTime); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 509 | ALOGV("[%d]-[%" PRIu64 "]-acquire2present[%d]", layerId, |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 510 | timeRecords[0].frameTime.frameNumber, acquireToPresentMs); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 511 | timeStatsLayer.deltas["acquire2present"].insert(acquireToPresentMs); |
| 512 | |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 513 | const int32_t latchToPresentMs = msBetween(timeRecords[0].frameTime.latchTime, |
| 514 | timeRecords[0].frameTime.presentTime); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 515 | ALOGV("[%d]-[%" PRIu64 "]-latch2present[%d]", layerId, |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 516 | timeRecords[0].frameTime.frameNumber, latchToPresentMs); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 517 | timeStatsLayer.deltas["latch2present"].insert(latchToPresentMs); |
| 518 | |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 519 | const int32_t desiredToPresentMs = msBetween(timeRecords[0].frameTime.desiredTime, |
| 520 | timeRecords[0].frameTime.presentTime); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 521 | ALOGV("[%d]-[%" PRIu64 "]-desired2present[%d]", layerId, |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 522 | timeRecords[0].frameTime.frameNumber, desiredToPresentMs); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 523 | timeStatsLayer.deltas["desired2present"].insert(desiredToPresentMs); |
| 524 | |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 525 | const int32_t presentToPresentMs = msBetween(prevTimeRecord.frameTime.presentTime, |
| 526 | timeRecords[0].frameTime.presentTime); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 527 | ALOGV("[%d]-[%" PRIu64 "]-present2present[%d]", layerId, |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 528 | timeRecords[0].frameTime.frameNumber, presentToPresentMs); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 529 | timeStatsLayer.deltas["present2present"].insert(presentToPresentMs); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 530 | } |
| 531 | prevTimeRecord = timeRecords[0]; |
Yiwei Zhang | c5f2c45 | 2018-05-08 16:31:56 -0700 | [diff] [blame] | 532 | timeRecords.pop_front(); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 533 | layerRecord.waitData--; |
| 534 | } |
| 535 | } |
| 536 | |
Yiwei Zhang | 8bec7e8 | 2019-10-07 18:08:26 -0700 | [diff] [blame] | 537 | static constexpr const char* kPopupWindowPrefix = "PopupWindow"; |
| 538 | static const size_t kMinLenLayerName = std::strlen(kPopupWindowPrefix); |
Yiwei Zhang | bd40832 | 2018-10-15 18:31:53 -0700 | [diff] [blame] | 539 | |
Yiwei Zhang | 8bec7e8 | 2019-10-07 18:08:26 -0700 | [diff] [blame] | 540 | // Avoid tracking the "PopupWindow:<random hash>#<number>" layers |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 541 | static bool layerNameIsValid(const std::string& layerName) { |
Yiwei Zhang | 8bec7e8 | 2019-10-07 18:08:26 -0700 | [diff] [blame] | 542 | return layerName.length() >= kMinLenLayerName && |
| 543 | layerName.compare(0, kMinLenLayerName, kPopupWindowPrefix) != 0; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 544 | } |
| 545 | |
Adithya Srinivasan | 58069dc | 2021-06-04 20:37:02 +0000 | [diff] [blame] | 546 | bool TimeStats::canAddNewAggregatedStats(uid_t uid, const std::string& layerName, |
Dominik Laskowski | f5d0ea5 | 2021-09-26 17:27:01 -0700 | [diff] [blame] | 547 | GameMode gameMode) { |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 548 | uint32_t layerRecords = 0; |
| 549 | for (const auto& record : mTimeStats.stats) { |
Adithya Srinivasan | 58069dc | 2021-06-04 20:37:02 +0000 | [diff] [blame] | 550 | if (record.second.stats.count({uid, layerName, gameMode}) > 0) { |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 551 | return true; |
| 552 | } |
| 553 | |
| 554 | layerRecords += record.second.stats.size(); |
| 555 | } |
| 556 | |
Dominik Laskowski | b4ba8f5 | 2021-09-27 18:20:58 -0700 | [diff] [blame] | 557 | return layerRecords < MAX_NUM_LAYER_STATS; |
Alec Mouri | 9a29e67 | 2020-09-14 12:39:14 -0700 | [diff] [blame] | 558 | } |
| 559 | |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 560 | void TimeStats::setPostTime(int32_t layerId, uint64_t frameNumber, const std::string& layerName, |
Dominik Laskowski | f5d0ea5 | 2021-09-26 17:27:01 -0700 | [diff] [blame] | 561 | uid_t uid, nsecs_t postTime, GameMode gameMode) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 562 | if (!mEnabled.load()) return; |
| 563 | |
| 564 | ATRACE_CALL(); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 565 | ALOGV("[%d]-[%" PRIu64 "]-[%s]-PostTime[%" PRId64 "]", layerId, frameNumber, layerName.c_str(), |
Yiwei Zhang | 8e8fe52 | 2018-11-02 18:34:07 -0700 | [diff] [blame] | 566 | postTime); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 567 | |
| 568 | std::lock_guard<std::mutex> lock(mMutex); |
Adithya Srinivasan | 58069dc | 2021-06-04 20:37:02 +0000 | [diff] [blame] | 569 | if (!canAddNewAggregatedStats(uid, layerName, gameMode)) { |
Yiwei Zhang | e926ab5 | 2019-08-14 15:16:00 -0700 | [diff] [blame] | 570 | return; |
| 571 | } |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 572 | if (!mTimeStatsTracker.count(layerId) && mTimeStatsTracker.size() < MAX_NUM_LAYER_RECORDS && |
Yiwei Zhang | 7eb58b7 | 2019-04-22 19:00:02 -0700 | [diff] [blame] | 573 | layerNameIsValid(layerName)) { |
Alec Mouri | 9a29e67 | 2020-09-14 12:39:14 -0700 | [diff] [blame] | 574 | mTimeStatsTracker[layerId].uid = uid; |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 575 | mTimeStatsTracker[layerId].layerName = layerName; |
Adithya Srinivasan | 58069dc | 2021-06-04 20:37:02 +0000 | [diff] [blame] | 576 | mTimeStatsTracker[layerId].gameMode = gameMode; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 577 | } |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 578 | if (!mTimeStatsTracker.count(layerId)) return; |
| 579 | LayerRecord& layerRecord = mTimeStatsTracker[layerId]; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 580 | if (layerRecord.timeRecords.size() == MAX_NUM_TIME_RECORDS) { |
Yiwei Zhang | af8ee94 | 2018-11-22 00:15:23 -0800 | [diff] [blame] | 581 | ALOGE("[%d]-[%s]-timeRecords is at its maximum size[%zu]. Ignore this when unittesting.", |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 582 | layerId, layerRecord.layerName.c_str(), MAX_NUM_TIME_RECORDS); |
| 583 | mTimeStatsTracker.erase(layerId); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 584 | return; |
| 585 | } |
| 586 | // For most media content, the acquireFence is invalid because the buffer is |
| 587 | // ready at the queueBuffer stage. In this case, acquireTime should be given |
| 588 | // a default value as postTime. |
| 589 | TimeRecord timeRecord = { |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 590 | .frameTime = |
| 591 | { |
| 592 | .frameNumber = frameNumber, |
| 593 | .postTime = postTime, |
Yiwei Zhang | af8ee94 | 2018-11-22 00:15:23 -0800 | [diff] [blame] | 594 | .latchTime = postTime, |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 595 | .acquireTime = postTime, |
Yiwei Zhang | af8ee94 | 2018-11-22 00:15:23 -0800 | [diff] [blame] | 596 | .desiredTime = postTime, |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 597 | }, |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 598 | }; |
| 599 | layerRecord.timeRecords.push_back(timeRecord); |
| 600 | if (layerRecord.waitData < 0 || |
| 601 | layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size())) |
| 602 | layerRecord.waitData = layerRecord.timeRecords.size() - 1; |
| 603 | } |
| 604 | |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 605 | void TimeStats::setLatchTime(int32_t layerId, uint64_t frameNumber, nsecs_t latchTime) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 606 | if (!mEnabled.load()) return; |
| 607 | |
| 608 | ATRACE_CALL(); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 609 | ALOGV("[%d]-[%" PRIu64 "]-LatchTime[%" PRId64 "]", layerId, frameNumber, latchTime); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 610 | |
| 611 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 612 | if (!mTimeStatsTracker.count(layerId)) return; |
| 613 | LayerRecord& layerRecord = mTimeStatsTracker[layerId]; |
Yiwei Zhang | cb7dd00 | 2019-04-16 11:03:01 -0700 | [diff] [blame] | 614 | if (layerRecord.waitData < 0 || |
| 615 | layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size())) |
| 616 | return; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 617 | TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData]; |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 618 | if (timeRecord.frameTime.frameNumber == frameNumber) { |
| 619 | timeRecord.frameTime.latchTime = latchTime; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 620 | } |
| 621 | } |
| 622 | |
Alec Mouri | 91f6df3 | 2020-01-30 08:48:58 -0800 | [diff] [blame] | 623 | void TimeStats::incrementLatchSkipped(int32_t layerId, LatchSkipReason reason) { |
| 624 | if (!mEnabled.load()) return; |
| 625 | |
| 626 | ATRACE_CALL(); |
| 627 | ALOGV("[%d]-LatchSkipped-Reason[%d]", layerId, |
| 628 | static_cast<std::underlying_type<LatchSkipReason>::type>(reason)); |
| 629 | |
| 630 | std::lock_guard<std::mutex> lock(mMutex); |
| 631 | if (!mTimeStatsTracker.count(layerId)) return; |
| 632 | LayerRecord& layerRecord = mTimeStatsTracker[layerId]; |
| 633 | |
| 634 | switch (reason) { |
| 635 | case LatchSkipReason::LateAcquire: |
| 636 | layerRecord.lateAcquireFrames++; |
| 637 | break; |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | void TimeStats::incrementBadDesiredPresent(int32_t layerId) { |
| 642 | if (!mEnabled.load()) return; |
| 643 | |
| 644 | ATRACE_CALL(); |
| 645 | ALOGV("[%d]-BadDesiredPresent", layerId); |
| 646 | |
| 647 | std::lock_guard<std::mutex> lock(mMutex); |
| 648 | if (!mTimeStatsTracker.count(layerId)) return; |
| 649 | LayerRecord& layerRecord = mTimeStatsTracker[layerId]; |
| 650 | layerRecord.badDesiredPresentFrames++; |
| 651 | } |
| 652 | |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 653 | void TimeStats::setDesiredTime(int32_t layerId, uint64_t frameNumber, nsecs_t desiredTime) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 654 | if (!mEnabled.load()) return; |
| 655 | |
| 656 | ATRACE_CALL(); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 657 | ALOGV("[%d]-[%" PRIu64 "]-DesiredTime[%" PRId64 "]", layerId, frameNumber, desiredTime); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 658 | |
| 659 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 660 | if (!mTimeStatsTracker.count(layerId)) return; |
| 661 | LayerRecord& layerRecord = mTimeStatsTracker[layerId]; |
Yiwei Zhang | cb7dd00 | 2019-04-16 11:03:01 -0700 | [diff] [blame] | 662 | if (layerRecord.waitData < 0 || |
| 663 | layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size())) |
| 664 | return; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 665 | TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData]; |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 666 | if (timeRecord.frameTime.frameNumber == frameNumber) { |
| 667 | timeRecord.frameTime.desiredTime = desiredTime; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 668 | } |
| 669 | } |
| 670 | |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 671 | void TimeStats::setAcquireTime(int32_t layerId, uint64_t frameNumber, nsecs_t acquireTime) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 672 | if (!mEnabled.load()) return; |
| 673 | |
| 674 | ATRACE_CALL(); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 675 | ALOGV("[%d]-[%" PRIu64 "]-AcquireTime[%" PRId64 "]", layerId, frameNumber, acquireTime); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 676 | |
| 677 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 678 | if (!mTimeStatsTracker.count(layerId)) return; |
| 679 | LayerRecord& layerRecord = mTimeStatsTracker[layerId]; |
Yiwei Zhang | cb7dd00 | 2019-04-16 11:03:01 -0700 | [diff] [blame] | 680 | if (layerRecord.waitData < 0 || |
| 681 | layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size())) |
| 682 | return; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 683 | TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData]; |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 684 | if (timeRecord.frameTime.frameNumber == frameNumber) { |
| 685 | timeRecord.frameTime.acquireTime = acquireTime; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 689 | void TimeStats::setAcquireFence(int32_t layerId, uint64_t frameNumber, |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 690 | const std::shared_ptr<FenceTime>& acquireFence) { |
| 691 | if (!mEnabled.load()) return; |
| 692 | |
| 693 | ATRACE_CALL(); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 694 | ALOGV("[%d]-[%" PRIu64 "]-AcquireFenceTime[%" PRId64 "]", layerId, frameNumber, |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 695 | acquireFence->getSignalTime()); |
| 696 | |
| 697 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 698 | if (!mTimeStatsTracker.count(layerId)) return; |
| 699 | LayerRecord& layerRecord = mTimeStatsTracker[layerId]; |
Yiwei Zhang | cb7dd00 | 2019-04-16 11:03:01 -0700 | [diff] [blame] | 700 | if (layerRecord.waitData < 0 || |
| 701 | layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size())) |
| 702 | return; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 703 | TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData]; |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 704 | if (timeRecord.frameTime.frameNumber == frameNumber) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 705 | timeRecord.acquireFence = acquireFence; |
| 706 | } |
| 707 | } |
| 708 | |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 709 | void TimeStats::setPresentTime(int32_t layerId, uint64_t frameNumber, nsecs_t presentTime, |
Ady Abraham | 8b9e612 | 2021-01-26 19:11:45 -0800 | [diff] [blame] | 710 | Fps displayRefreshRate, std::optional<Fps> renderRate, |
Dominik Laskowski | f5d0ea5 | 2021-09-26 17:27:01 -0700 | [diff] [blame] | 711 | SetFrameRateVote frameRateVote, GameMode gameMode) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 712 | if (!mEnabled.load()) return; |
| 713 | |
| 714 | ATRACE_CALL(); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 715 | ALOGV("[%d]-[%" PRIu64 "]-PresentTime[%" PRId64 "]", layerId, frameNumber, presentTime); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 716 | |
| 717 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 718 | if (!mTimeStatsTracker.count(layerId)) return; |
| 719 | LayerRecord& layerRecord = mTimeStatsTracker[layerId]; |
Yiwei Zhang | cb7dd00 | 2019-04-16 11:03:01 -0700 | [diff] [blame] | 720 | if (layerRecord.waitData < 0 || |
| 721 | layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size())) |
| 722 | return; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 723 | TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData]; |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 724 | if (timeRecord.frameTime.frameNumber == frameNumber) { |
| 725 | timeRecord.frameTime.presentTime = presentTime; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 726 | timeRecord.ready = true; |
| 727 | layerRecord.waitData++; |
| 728 | } |
| 729 | |
Adithya Srinivasan | 58069dc | 2021-06-04 20:37:02 +0000 | [diff] [blame] | 730 | flushAvailableRecordsToStatsLocked(layerId, displayRefreshRate, renderRate, frameRateVote, |
| 731 | gameMode); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 732 | } |
| 733 | |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 734 | void TimeStats::setPresentFence(int32_t layerId, uint64_t frameNumber, |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 735 | const std::shared_ptr<FenceTime>& presentFence, |
Ady Abraham | 8b9e612 | 2021-01-26 19:11:45 -0800 | [diff] [blame] | 736 | Fps displayRefreshRate, std::optional<Fps> renderRate, |
Dominik Laskowski | f5d0ea5 | 2021-09-26 17:27:01 -0700 | [diff] [blame] | 737 | SetFrameRateVote frameRateVote, GameMode gameMode) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 738 | if (!mEnabled.load()) return; |
| 739 | |
| 740 | ATRACE_CALL(); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 741 | ALOGV("[%d]-[%" PRIu64 "]-PresentFenceTime[%" PRId64 "]", layerId, frameNumber, |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 742 | presentFence->getSignalTime()); |
| 743 | |
| 744 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 745 | if (!mTimeStatsTracker.count(layerId)) return; |
| 746 | LayerRecord& layerRecord = mTimeStatsTracker[layerId]; |
Yiwei Zhang | cb7dd00 | 2019-04-16 11:03:01 -0700 | [diff] [blame] | 747 | if (layerRecord.waitData < 0 || |
| 748 | layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size())) |
| 749 | return; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 750 | TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData]; |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 751 | if (timeRecord.frameTime.frameNumber == frameNumber) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 752 | timeRecord.presentFence = presentFence; |
| 753 | timeRecord.ready = true; |
| 754 | layerRecord.waitData++; |
| 755 | } |
| 756 | |
Adithya Srinivasan | 58069dc | 2021-06-04 20:37:02 +0000 | [diff] [blame] | 757 | flushAvailableRecordsToStatsLocked(layerId, displayRefreshRate, renderRate, frameRateVote, |
| 758 | gameMode); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 759 | } |
| 760 | |
Adithya Srinivasan | ead1716 | 2021-02-18 02:17:37 +0000 | [diff] [blame] | 761 | static const constexpr int32_t kValidJankyReason = JankType::DisplayHAL | |
| 762 | JankType::SurfaceFlingerCpuDeadlineMissed | JankType::SurfaceFlingerGpuDeadlineMissed | |
| 763 | JankType::AppDeadlineMissed | JankType::PredictionError | |
Adithya Srinivasan | 53e5c40 | 2021-04-16 17:34:30 +0000 | [diff] [blame] | 764 | JankType::SurfaceFlingerScheduling; |
Alec Mouri | 363faf0 | 2021-01-29 16:34:55 -0800 | [diff] [blame] | 765 | |
Alec Mouri | 9a29e67 | 2020-09-14 12:39:14 -0700 | [diff] [blame] | 766 | template <class T> |
| 767 | static void updateJankPayload(T& t, int32_t reasons) { |
| 768 | t.jankPayload.totalFrames++; |
| 769 | |
Alec Mouri | 9a29e67 | 2020-09-14 12:39:14 -0700 | [diff] [blame] | 770 | if (reasons & kValidJankyReason) { |
| 771 | t.jankPayload.totalJankyFrames++; |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 772 | if ((reasons & JankType::SurfaceFlingerCpuDeadlineMissed) != 0) { |
Alec Mouri | 9a29e67 | 2020-09-14 12:39:14 -0700 | [diff] [blame] | 773 | t.jankPayload.totalSFLongCpu++; |
| 774 | } |
Jorim Jaggi | 5814ab8 | 2020-12-03 20:45:58 +0100 | [diff] [blame] | 775 | if ((reasons & JankType::SurfaceFlingerGpuDeadlineMissed) != 0) { |
Alec Mouri | 9a29e67 | 2020-09-14 12:39:14 -0700 | [diff] [blame] | 776 | t.jankPayload.totalSFLongGpu++; |
| 777 | } |
Adithya Srinivasan | 9b2ca3e | 2020-11-10 10:14:17 -0800 | [diff] [blame] | 778 | if ((reasons & JankType::DisplayHAL) != 0) { |
Alec Mouri | 9a29e67 | 2020-09-14 12:39:14 -0700 | [diff] [blame] | 779 | t.jankPayload.totalSFUnattributed++; |
| 780 | } |
Jorim Jaggi | 5814ab8 | 2020-12-03 20:45:58 +0100 | [diff] [blame] | 781 | if ((reasons & JankType::AppDeadlineMissed) != 0) { |
Alec Mouri | 9a29e67 | 2020-09-14 12:39:14 -0700 | [diff] [blame] | 782 | t.jankPayload.totalAppUnattributed++; |
| 783 | } |
Adithya Srinivasan | ead1716 | 2021-02-18 02:17:37 +0000 | [diff] [blame] | 784 | if ((reasons & JankType::PredictionError) != 0) { |
| 785 | t.jankPayload.totalSFPredictionError++; |
| 786 | } |
| 787 | if ((reasons & JankType::SurfaceFlingerScheduling) != 0) { |
| 788 | t.jankPayload.totalSFScheduling++; |
| 789 | } |
Adithya Srinivasan | 53e5c40 | 2021-04-16 17:34:30 +0000 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | // We want to track BufferStuffing separately as it can provide info on latency issues |
| 793 | if (reasons & JankType::BufferStuffing) { |
| 794 | t.jankPayload.totalAppBufferStuffing++; |
Alec Mouri | 9a29e67 | 2020-09-14 12:39:14 -0700 | [diff] [blame] | 795 | } |
| 796 | } |
| 797 | |
Alec Mouri | 363faf0 | 2021-01-29 16:34:55 -0800 | [diff] [blame] | 798 | void TimeStats::incrementJankyFrames(const JankyFramesInfo& info) { |
Alec Mouri | 9a29e67 | 2020-09-14 12:39:14 -0700 | [diff] [blame] | 799 | if (!mEnabled.load()) return; |
| 800 | |
| 801 | ATRACE_CALL(); |
| 802 | std::lock_guard<std::mutex> lock(mMutex); |
| 803 | |
Alec Mouri | 542de11 | 2020-11-13 12:07:32 -0800 | [diff] [blame] | 804 | // Only update layer stats if we're already tracking the layer in TimeStats. |
| 805 | // Otherwise, continue tracking the statistic but use a default layer name instead. |
Alec Mouri | 9a29e67 | 2020-09-14 12:39:14 -0700 | [diff] [blame] | 806 | // As an implementation detail, we do this because this method is expected to be |
Alec Mouri | 542de11 | 2020-11-13 12:07:32 -0800 | [diff] [blame] | 807 | // called from FrameTimeline, whose jank classification includes transaction jank |
| 808 | // that occurs without a buffer. But, in general those layer names are not suitable as |
| 809 | // aggregation keys: e.g., it's normal and expected for Window Manager to include the hash code |
| 810 | // for an animation leash. So while we can show that jank in dumpsys, aggregating based on the |
| 811 | // layer blows up the stats size, so as a workaround drop those stats. This assumes that |
| 812 | // TimeStats will flush the first present fence for a layer *before* FrameTimeline does so that |
| 813 | // the first jank record is not dropped. |
Alec Mouri | 9a29e67 | 2020-09-14 12:39:14 -0700 | [diff] [blame] | 814 | |
Alec Mouri | 542de11 | 2020-11-13 12:07:32 -0800 | [diff] [blame] | 815 | static const std::string kDefaultLayerName = "none"; |
Dominik Laskowski | f5d0ea5 | 2021-09-26 17:27:01 -0700 | [diff] [blame] | 816 | constexpr GameMode kDefaultGameMode = GameMode::Unsupported; |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 817 | |
Alec Mouri | 363faf0 | 2021-01-29 16:34:55 -0800 | [diff] [blame] | 818 | const int32_t refreshRateBucket = |
Ady Abraham | 3403a3f | 2021-04-27 16:58:40 -0700 | [diff] [blame] | 819 | clampToNearestBucket(info.refreshRate, REFRESH_RATE_BUCKET_WIDTH); |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 820 | const int32_t renderRateBucket = |
Ady Abraham | 3403a3f | 2021-04-27 16:58:40 -0700 | [diff] [blame] | 821 | clampToNearestBucket(info.renderRate ? *info.renderRate : info.refreshRate, |
| 822 | RENDER_RATE_BUCKET_WIDTH); |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 823 | const TimeStatsHelper::TimelineStatsKey timelineKey = {refreshRateBucket, renderRateBucket}; |
| 824 | |
| 825 | if (!mTimeStats.stats.count(timelineKey)) { |
| 826 | mTimeStats.stats[timelineKey].key = timelineKey; |
Alec Mouri | 9a29e67 | 2020-09-14 12:39:14 -0700 | [diff] [blame] | 827 | } |
| 828 | |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 829 | TimeStatsHelper::TimelineStats& timelineStats = mTimeStats.stats[timelineKey]; |
| 830 | |
Alec Mouri | 363faf0 | 2021-01-29 16:34:55 -0800 | [diff] [blame] | 831 | updateJankPayload<TimeStatsHelper::TimelineStats>(timelineStats, info.reasons); |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 832 | |
Adithya Srinivasan | 58069dc | 2021-06-04 20:37:02 +0000 | [diff] [blame] | 833 | TimeStatsHelper::LayerStatsKey layerKey = {info.uid, info.layerName, info.gameMode}; |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 834 | if (!timelineStats.stats.count(layerKey)) { |
Adithya Srinivasan | 58069dc | 2021-06-04 20:37:02 +0000 | [diff] [blame] | 835 | layerKey = {info.uid, kDefaultLayerName, kDefaultGameMode}; |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 836 | timelineStats.stats[layerKey].displayRefreshRateBucket = refreshRateBucket; |
| 837 | timelineStats.stats[layerKey].renderRateBucket = renderRateBucket; |
Alec Mouri | 363faf0 | 2021-01-29 16:34:55 -0800 | [diff] [blame] | 838 | timelineStats.stats[layerKey].uid = info.uid; |
Adithya Srinivasan | f427f76 | 2021-06-15 19:46:26 +0000 | [diff] [blame] | 839 | timelineStats.stats[layerKey].layerName = kDefaultLayerName; |
| 840 | timelineStats.stats[layerKey].gameMode = kDefaultGameMode; |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | TimeStatsHelper::TimeStatsLayer& timeStatsLayer = timelineStats.stats[layerKey]; |
Alec Mouri | 363faf0 | 2021-01-29 16:34:55 -0800 | [diff] [blame] | 844 | updateJankPayload<TimeStatsHelper::TimeStatsLayer>(timeStatsLayer, info.reasons); |
| 845 | |
| 846 | if (info.reasons & kValidJankyReason) { |
| 847 | // TimeStats Histograms only retain positive values, so we don't need to check if these |
| 848 | // deadlines were really missed if we know that the frame had jank, since deadlines |
| 849 | // that were met will be dropped. |
Ady Abraham | 3e8cc07 | 2021-05-11 16:29:54 -0700 | [diff] [blame] | 850 | timelineStats.displayDeadlineDeltas.insert(toMs(info.displayDeadlineDelta)); |
| 851 | timelineStats.displayPresentDeltas.insert(toMs(info.displayPresentJitter)); |
| 852 | timeStatsLayer.deltas["appDeadlineDeltas"].insert(toMs(info.appDeadlineDelta)); |
Alec Mouri | 363faf0 | 2021-01-29 16:34:55 -0800 | [diff] [blame] | 853 | } |
Alec Mouri | 9a29e67 | 2020-09-14 12:39:14 -0700 | [diff] [blame] | 854 | } |
| 855 | |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 856 | void TimeStats::onDestroy(int32_t layerId) { |
Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 857 | ATRACE_CALL(); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 858 | ALOGV("[%d]-onDestroy", layerId); |
Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 859 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 860 | mTimeStatsTracker.erase(layerId); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 861 | } |
| 862 | |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 863 | void TimeStats::removeTimeRecord(int32_t layerId, uint64_t frameNumber) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 864 | if (!mEnabled.load()) return; |
| 865 | |
| 866 | ATRACE_CALL(); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 867 | ALOGV("[%d]-[%" PRIu64 "]-removeTimeRecord", layerId, frameNumber); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 868 | |
| 869 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 870 | if (!mTimeStatsTracker.count(layerId)) return; |
| 871 | LayerRecord& layerRecord = mTimeStatsTracker[layerId]; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 872 | size_t removeAt = 0; |
| 873 | for (const TimeRecord& record : layerRecord.timeRecords) { |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 874 | if (record.frameTime.frameNumber == frameNumber) break; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 875 | removeAt++; |
| 876 | } |
| 877 | if (removeAt == layerRecord.timeRecords.size()) return; |
| 878 | layerRecord.timeRecords.erase(layerRecord.timeRecords.begin() + removeAt); |
| 879 | if (layerRecord.waitData > static_cast<int32_t>(removeAt)) { |
Yiwei Zhang | eaeea06 | 2018-06-28 14:46:51 -0700 | [diff] [blame] | 880 | layerRecord.waitData--; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 881 | } |
Yiwei Zhang | eaeea06 | 2018-06-28 14:46:51 -0700 | [diff] [blame] | 882 | layerRecord.droppedFrames++; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 883 | } |
| 884 | |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 885 | void TimeStats::flushPowerTimeLocked() { |
Yiwei Zhang | e5c49d5 | 2018-10-29 00:15:31 -0700 | [diff] [blame] | 886 | if (!mEnabled.load()) return; |
| 887 | |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 888 | nsecs_t curTime = systemTime(); |
| 889 | // elapsedTime is in milliseconds. |
| 890 | int64_t elapsedTime = (curTime - mPowerTime.prevTime) / 1000000; |
| 891 | |
| 892 | switch (mPowerTime.powerMode) { |
Peiyong Lin | 65248e0 | 2020-04-18 21:15:07 -0700 | [diff] [blame] | 893 | case PowerMode::ON: |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 894 | mTimeStats.displayOnTimeLegacy += elapsedTime; |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 895 | break; |
Peiyong Lin | 65248e0 | 2020-04-18 21:15:07 -0700 | [diff] [blame] | 896 | case PowerMode::OFF: |
| 897 | case PowerMode::DOZE: |
| 898 | case PowerMode::DOZE_SUSPEND: |
| 899 | case PowerMode::ON_SUSPEND: |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 900 | default: |
| 901 | break; |
| 902 | } |
| 903 | |
| 904 | mPowerTime.prevTime = curTime; |
| 905 | } |
| 906 | |
Peiyong Lin | 65248e0 | 2020-04-18 21:15:07 -0700 | [diff] [blame] | 907 | void TimeStats::setPowerMode(PowerMode powerMode) { |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 908 | if (!mEnabled.load()) { |
| 909 | std::lock_guard<std::mutex> lock(mMutex); |
| 910 | mPowerTime.powerMode = powerMode; |
| 911 | return; |
| 912 | } |
| 913 | |
| 914 | std::lock_guard<std::mutex> lock(mMutex); |
| 915 | if (powerMode == mPowerTime.powerMode) return; |
| 916 | |
| 917 | flushPowerTimeLocked(); |
| 918 | mPowerTime.powerMode = powerMode; |
| 919 | } |
| 920 | |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 921 | void TimeStats::recordRefreshRate(uint32_t fps, nsecs_t duration) { |
| 922 | std::lock_guard<std::mutex> lock(mMutex); |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 923 | if (mTimeStats.refreshRateStatsLegacy.count(fps)) { |
| 924 | mTimeStats.refreshRateStatsLegacy[fps] += duration; |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 925 | } else { |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 926 | mTimeStats.refreshRateStatsLegacy.insert({fps, duration}); |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 927 | } |
| 928 | } |
| 929 | |
Yiwei Zhang | ce6ebc0 | 2018-10-20 12:42:38 -0700 | [diff] [blame] | 930 | void TimeStats::flushAvailableGlobalRecordsToStatsLocked() { |
| 931 | ATRACE_CALL(); |
| 932 | |
| 933 | while (!mGlobalRecord.presentFences.empty()) { |
| 934 | const nsecs_t curPresentTime = mGlobalRecord.presentFences.front()->getSignalTime(); |
| 935 | if (curPresentTime == Fence::SIGNAL_TIME_PENDING) break; |
| 936 | |
| 937 | if (curPresentTime == Fence::SIGNAL_TIME_INVALID) { |
| 938 | ALOGE("GlobalPresentFence is invalid!"); |
| 939 | mGlobalRecord.prevPresentTime = 0; |
| 940 | mGlobalRecord.presentFences.pop_front(); |
| 941 | continue; |
| 942 | } |
| 943 | |
| 944 | ALOGV("GlobalPresentFenceTime[%" PRId64 "]", |
| 945 | mGlobalRecord.presentFences.front()->getSignalTime()); |
| 946 | |
Yiwei Zhang | e5c49d5 | 2018-10-29 00:15:31 -0700 | [diff] [blame] | 947 | if (mGlobalRecord.prevPresentTime != 0) { |
| 948 | const int32_t presentToPresentMs = |
| 949 | msBetween(mGlobalRecord.prevPresentTime, curPresentTime); |
| 950 | ALOGV("Global present2present[%d] prev[%" PRId64 "] curr[%" PRId64 "]", |
| 951 | presentToPresentMs, mGlobalRecord.prevPresentTime, curPresentTime); |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 952 | mTimeStats.presentToPresentLegacy.insert(presentToPresentMs); |
Yiwei Zhang | e5c49d5 | 2018-10-29 00:15:31 -0700 | [diff] [blame] | 953 | } |
Yiwei Zhang | ce6ebc0 | 2018-10-20 12:42:38 -0700 | [diff] [blame] | 954 | |
Yiwei Zhang | ce6ebc0 | 2018-10-20 12:42:38 -0700 | [diff] [blame] | 955 | mGlobalRecord.prevPresentTime = curPresentTime; |
| 956 | mGlobalRecord.presentFences.pop_front(); |
| 957 | } |
Alec Mouri | e4034bb | 2019-11-19 12:45:54 -0800 | [diff] [blame] | 958 | while (!mGlobalRecord.renderEngineDurations.empty()) { |
| 959 | const auto duration = mGlobalRecord.renderEngineDurations.front(); |
| 960 | const auto& endTime = duration.endTime; |
| 961 | |
| 962 | nsecs_t endNs = -1; |
| 963 | |
| 964 | if (auto val = std::get_if<nsecs_t>(&endTime)) { |
| 965 | endNs = *val; |
| 966 | } else { |
| 967 | endNs = std::get<std::shared_ptr<FenceTime>>(endTime)->getSignalTime(); |
| 968 | } |
| 969 | |
| 970 | if (endNs == Fence::SIGNAL_TIME_PENDING) break; |
| 971 | |
| 972 | if (endNs < 0) { |
| 973 | ALOGE("RenderEngineTiming is invalid!"); |
| 974 | mGlobalRecord.renderEngineDurations.pop_front(); |
| 975 | continue; |
| 976 | } |
| 977 | |
| 978 | const int32_t renderEngineMs = msBetween(duration.startTime, endNs); |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 979 | mTimeStats.renderEngineTimingLegacy.insert(renderEngineMs); |
Alec Mouri | e4034bb | 2019-11-19 12:45:54 -0800 | [diff] [blame] | 980 | |
| 981 | mGlobalRecord.renderEngineDurations.pop_front(); |
| 982 | } |
Yiwei Zhang | ce6ebc0 | 2018-10-20 12:42:38 -0700 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | void TimeStats::setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) { |
| 986 | if (!mEnabled.load()) return; |
| 987 | |
| 988 | ATRACE_CALL(); |
| 989 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | e5c49d5 | 2018-10-29 00:15:31 -0700 | [diff] [blame] | 990 | if (presentFence == nullptr || !presentFence->isValid()) { |
| 991 | mGlobalRecord.prevPresentTime = 0; |
| 992 | return; |
| 993 | } |
| 994 | |
Peiyong Lin | 65248e0 | 2020-04-18 21:15:07 -0700 | [diff] [blame] | 995 | if (mPowerTime.powerMode != PowerMode::ON) { |
| 996 | // Try flushing the last present fence on PowerMode::ON. |
Yiwei Zhang | e5c49d5 | 2018-10-29 00:15:31 -0700 | [diff] [blame] | 997 | flushAvailableGlobalRecordsToStatsLocked(); |
| 998 | mGlobalRecord.presentFences.clear(); |
Yiwei Zhang | ce6ebc0 | 2018-10-20 12:42:38 -0700 | [diff] [blame] | 999 | mGlobalRecord.prevPresentTime = 0; |
| 1000 | return; |
| 1001 | } |
| 1002 | |
| 1003 | if (mGlobalRecord.presentFences.size() == MAX_NUM_TIME_RECORDS) { |
| 1004 | // The front presentFence must be trapped in pending status in this |
| 1005 | // case. Try dequeuing the front one to recover. |
| 1006 | ALOGE("GlobalPresentFences is already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS); |
| 1007 | mGlobalRecord.prevPresentTime = 0; |
| 1008 | mGlobalRecord.presentFences.pop_front(); |
| 1009 | } |
| 1010 | |
| 1011 | mGlobalRecord.presentFences.emplace_back(presentFence); |
| 1012 | flushAvailableGlobalRecordsToStatsLocked(); |
| 1013 | } |
| 1014 | |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 1015 | void TimeStats::enable() { |
| 1016 | if (mEnabled.load()) return; |
| 1017 | |
| 1018 | ATRACE_CALL(); |
| 1019 | |
| 1020 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 1021 | mEnabled.store(true); |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 1022 | mTimeStats.statsStartLegacy = static_cast<int64_t>(std::time(0)); |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 1023 | mPowerTime.prevTime = systemTime(); |
Yiwei Zhang | e5c49d5 | 2018-10-29 00:15:31 -0700 | [diff] [blame] | 1024 | ALOGD("Enabled"); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | void TimeStats::disable() { |
| 1028 | if (!mEnabled.load()) return; |
| 1029 | |
| 1030 | ATRACE_CALL(); |
| 1031 | |
| 1032 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | e5c49d5 | 2018-10-29 00:15:31 -0700 | [diff] [blame] | 1033 | flushPowerTimeLocked(); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 1034 | mEnabled.store(false); |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 1035 | mTimeStats.statsEndLegacy = static_cast<int64_t>(std::time(0)); |
Yiwei Zhang | e5c49d5 | 2018-10-29 00:15:31 -0700 | [diff] [blame] | 1036 | ALOGD("Disabled"); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 1037 | } |
| 1038 | |
Alec Mouri | 8e2f31b | 2020-01-16 22:04:35 +0000 | [diff] [blame] | 1039 | void TimeStats::clearAll() { |
| 1040 | std::lock_guard<std::mutex> lock(mMutex); |
Ady Abraham | 3403a3f | 2021-04-27 16:58:40 -0700 | [diff] [blame] | 1041 | mTimeStats.stats.clear(); |
Alec Mouri | 8e2f31b | 2020-01-16 22:04:35 +0000 | [diff] [blame] | 1042 | clearGlobalLocked(); |
| 1043 | clearLayersLocked(); |
| 1044 | } |
| 1045 | |
| 1046 | void TimeStats::clearGlobalLocked() { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 1047 | ATRACE_CALL(); |
| 1048 | |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 1049 | mTimeStats.statsStartLegacy = (mEnabled.load() ? static_cast<int64_t>(std::time(0)) : 0); |
| 1050 | mTimeStats.statsEndLegacy = 0; |
| 1051 | mTimeStats.totalFramesLegacy = 0; |
| 1052 | mTimeStats.missedFramesLegacy = 0; |
| 1053 | mTimeStats.clientCompositionFramesLegacy = 0; |
| 1054 | mTimeStats.clientCompositionReusedFramesLegacy = 0; |
Robert Carr | a00eb14 | 2022-03-09 13:49:30 -0800 | [diff] [blame] | 1055 | mTimeStats.compositionStrategyChangesLegacy = 0; |
Vishnu Nair | 9cf8926 | 2022-02-26 09:17:49 -0800 | [diff] [blame] | 1056 | mTimeStats.compositionStrategyPredictedLegacy = 0; |
| 1057 | mTimeStats.compositionStrategyPredictionSucceededLegacy = 0; |
| 1058 | mTimeStats.refreshRateSwitchesLegacy = 0; |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 1059 | mTimeStats.displayEventConnectionsCountLegacy = 0; |
| 1060 | mTimeStats.displayOnTimeLegacy = 0; |
| 1061 | mTimeStats.presentToPresentLegacy.hist.clear(); |
| 1062 | mTimeStats.frameDurationLegacy.hist.clear(); |
| 1063 | mTimeStats.renderEngineTimingLegacy.hist.clear(); |
| 1064 | mTimeStats.refreshRateStatsLegacy.clear(); |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 1065 | mPowerTime.prevTime = systemTime(); |
Alec Mouri | 56e6385 | 2021-03-09 18:17:25 -0800 | [diff] [blame] | 1066 | for (auto& globalRecord : mTimeStats.stats) { |
| 1067 | globalRecord.second.clearGlobals(); |
| 1068 | } |
Yiwei Zhang | e5c49d5 | 2018-10-29 00:15:31 -0700 | [diff] [blame] | 1069 | mGlobalRecord.prevPresentTime = 0; |
| 1070 | mGlobalRecord.presentFences.clear(); |
Alec Mouri | 8e2f31b | 2020-01-16 22:04:35 +0000 | [diff] [blame] | 1071 | ALOGD("Cleared global stats"); |
| 1072 | } |
| 1073 | |
| 1074 | void TimeStats::clearLayersLocked() { |
| 1075 | ATRACE_CALL(); |
| 1076 | |
| 1077 | mTimeStatsTracker.clear(); |
Alec Mouri | 56e6385 | 2021-03-09 18:17:25 -0800 | [diff] [blame] | 1078 | |
| 1079 | for (auto& globalRecord : mTimeStats.stats) { |
| 1080 | globalRecord.second.stats.clear(); |
| 1081 | } |
Alec Mouri | 8e2f31b | 2020-01-16 22:04:35 +0000 | [diff] [blame] | 1082 | ALOGD("Cleared layer stats"); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | bool TimeStats::isEnabled() { |
| 1086 | return mEnabled.load(); |
| 1087 | } |
| 1088 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 1089 | void TimeStats::dump(bool asProto, std::optional<uint32_t> maxLayers, std::string& result) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 1090 | ATRACE_CALL(); |
| 1091 | |
| 1092 | std::lock_guard<std::mutex> lock(mMutex); |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 1093 | if (mTimeStats.statsStartLegacy == 0) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 1094 | return; |
| 1095 | } |
| 1096 | |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 1097 | mTimeStats.statsEndLegacy = static_cast<int64_t>(std::time(0)); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 1098 | |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 1099 | flushPowerTimeLocked(); |
| 1100 | |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 1101 | if (asProto) { |
Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 1102 | ALOGD("Dumping TimeStats as proto"); |
Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 1103 | SFTimeStatsGlobalProto timeStatsProto = mTimeStats.toProto(maxLayers); |
Dominik Laskowski | 4647011 | 2019-08-02 13:13:11 -0700 | [diff] [blame] | 1104 | result.append(timeStatsProto.SerializeAsString()); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 1105 | } else { |
Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 1106 | ALOGD("Dumping TimeStats as text"); |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 1107 | result.append(mTimeStats.toString(maxLayers)); |
Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 1108 | result.append("\n"); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 1109 | } |
| 1110 | } |
| 1111 | |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 1112 | } // namespace impl |
| 1113 | |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 1114 | } // namespace android |