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