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