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