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 | |
| 17 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 18 | #pragma clang diagnostic push |
| 19 | #pragma clang diagnostic ignored "-Wconversion" |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 20 | #undef LOG_TAG |
| 21 | #define LOG_TAG "TimeStats" |
| 22 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 23 | |
| 24 | #include "TimeStats.h" |
| 25 | |
| 26 | #include <android-base/stringprintf.h> |
Alec Mouri | 3738434 | 2020-01-02 17:23:37 -0800 | [diff] [blame] | 27 | #include <android/util/ProtoOutputStream.h> |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 28 | #include <log/log.h> |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 29 | #include <utils/String8.h> |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 30 | #include <utils/Timers.h> |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 31 | #include <utils/Trace.h> |
| 32 | |
| 33 | #include <algorithm> |
Alec Mouri | 9519bf1 | 2019-11-15 16:54:44 -0800 | [diff] [blame] | 34 | #include <chrono> |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 35 | |
| 36 | namespace android { |
| 37 | |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 38 | namespace impl { |
| 39 | |
Alec Mouri | 3738434 | 2020-01-02 17:23:37 -0800 | [diff] [blame] | 40 | status_pull_atom_return_t TimeStats::pullAtomCallback(int32_t atom_tag, |
| 41 | pulled_stats_event_list* data, void* cookie) { |
Alec Mouri | 8e2f31b | 2020-01-16 22:04:35 +0000 | [diff] [blame] | 42 | impl::TimeStats* timeStats = reinterpret_cast<impl::TimeStats*>(cookie); |
Alec Mouri | 3ecd5cd | 2020-01-29 12:53:07 -0800 | [diff] [blame] | 43 | status_pull_atom_return_t result = STATS_PULL_SKIP; |
Alec Mouri | 3738434 | 2020-01-02 17:23:37 -0800 | [diff] [blame] | 44 | if (atom_tag == android::util::SURFACEFLINGER_STATS_GLOBAL_INFO) { |
Alec Mouri | 3ecd5cd | 2020-01-29 12:53:07 -0800 | [diff] [blame] | 45 | result = timeStats->populateGlobalAtom(data); |
Alec Mouri | 3738434 | 2020-01-02 17:23:37 -0800 | [diff] [blame] | 46 | } else if (atom_tag == android::util::SURFACEFLINGER_STATS_LAYER_INFO) { |
Alec Mouri | 3ecd5cd | 2020-01-29 12:53:07 -0800 | [diff] [blame] | 47 | result = timeStats->populateLayerAtom(data); |
Alec Mouri | 8e2f31b | 2020-01-16 22:04:35 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Alec Mouri | 3ecd5cd | 2020-01-29 12:53:07 -0800 | [diff] [blame] | 50 | // Enable timestats now. The first full pull for a given build is expected to |
| 51 | // have empty or very little stats, as stats are first enabled after the |
| 52 | // first pull is completed for either the global or layer stats. |
| 53 | timeStats->enable(); |
| 54 | return result; |
Alec Mouri | 3738434 | 2020-01-02 17:23:37 -0800 | [diff] [blame] | 55 | } |
Alec Mouri | 8e2f31b | 2020-01-16 22:04:35 +0000 | [diff] [blame] | 56 | |
Alec Mouri | 3738434 | 2020-01-02 17:23:37 -0800 | [diff] [blame] | 57 | status_pull_atom_return_t TimeStats::populateGlobalAtom(pulled_stats_event_list* data) { |
| 58 | std::lock_guard<std::mutex> lock(mMutex); |
| 59 | |
| 60 | if (mTimeStats.statsStart == 0) { |
Alec Mouri | 8e2f31b | 2020-01-16 22:04:35 +0000 | [diff] [blame] | 61 | return STATS_PULL_SKIP; |
| 62 | } |
Alec Mouri | 3738434 | 2020-01-02 17:23:37 -0800 | [diff] [blame] | 63 | flushPowerTimeLocked(); |
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 | struct stats_event* event = mStatsDelegate->addStatsEventToPullData(data); |
| 66 | mStatsDelegate->statsEventSetAtomId(event, android::util::SURFACEFLINGER_STATS_GLOBAL_INFO); |
| 67 | mStatsDelegate->statsEventWriteInt64(event, mTimeStats.totalFrames); |
| 68 | mStatsDelegate->statsEventWriteInt64(event, mTimeStats.missedFrames); |
| 69 | mStatsDelegate->statsEventWriteInt64(event, mTimeStats.clientCompositionFrames); |
| 70 | mStatsDelegate->statsEventWriteInt64(event, mTimeStats.displayOnTime); |
| 71 | mStatsDelegate->statsEventWriteInt64(event, mTimeStats.presentToPresent.totalTime()); |
| 72 | mStatsDelegate->statsEventBuild(event); |
| 73 | clearGlobalLocked(); |
Alec Mouri | 8e2f31b | 2020-01-16 22:04:35 +0000 | [diff] [blame] | 74 | |
| 75 | return STATS_PULL_SUCCESS; |
| 76 | } |
| 77 | |
Alec Mouri | 3738434 | 2020-01-02 17:23:37 -0800 | [diff] [blame] | 78 | namespace { |
| 79 | // Histograms align with the order of fields in SurfaceflingerStatsLayerInfo. |
| 80 | const std::array<std::string, 6> kHistogramNames = { |
| 81 | "present2present", "post2present", "acquire2present", |
| 82 | "latch2present", "desired2present", "post2acquire", |
| 83 | }; |
Alec Mouri | 8e2f31b | 2020-01-16 22:04:35 +0000 | [diff] [blame] | 84 | |
Alec Mouri | 3738434 | 2020-01-02 17:23:37 -0800 | [diff] [blame] | 85 | std::string histogramToProtoByteString(const std::unordered_map<int32_t, int32_t>& histogram, |
| 86 | size_t maxPulledHistogramBuckets) { |
| 87 | auto buckets = std::vector<std::pair<int32_t, int32_t>>(histogram.begin(), histogram.end()); |
| 88 | std::sort(buckets.begin(), buckets.end(), |
| 89 | [](std::pair<int32_t, int32_t>& left, std::pair<int32_t, int32_t>& right) { |
| 90 | return left.second > right.second; |
| 91 | }); |
| 92 | |
| 93 | util::ProtoOutputStream proto; |
| 94 | int histogramSize = 0; |
| 95 | for (const auto& bucket : buckets) { |
| 96 | if (++histogramSize > maxPulledHistogramBuckets) { |
| 97 | break; |
| 98 | } |
| 99 | proto.write(android::util::FIELD_TYPE_INT32 | android::util::FIELD_COUNT_REPEATED | |
| 100 | 1 /* field id */, |
| 101 | (int32_t)bucket.first); |
| 102 | proto.write(android::util::FIELD_TYPE_INT64 | android::util::FIELD_COUNT_REPEATED | |
| 103 | 2 /* field id */, |
| 104 | (int64_t)bucket.second); |
| 105 | } |
| 106 | |
| 107 | std::string byteString; |
| 108 | proto.serializeToString(&byteString); |
| 109 | return byteString; |
| 110 | } |
| 111 | } // namespace |
| 112 | |
| 113 | status_pull_atom_return_t TimeStats::populateLayerAtom(pulled_stats_event_list* data) { |
| 114 | std::lock_guard<std::mutex> lock(mMutex); |
| 115 | |
| 116 | std::vector<TimeStatsHelper::TimeStatsLayer const*> dumpStats; |
| 117 | for (const auto& ele : mTimeStats.stats) { |
| 118 | dumpStats.push_back(&ele.second); |
| 119 | } |
| 120 | |
| 121 | std::sort(dumpStats.begin(), dumpStats.end(), |
| 122 | [](TimeStatsHelper::TimeStatsLayer const* l, |
| 123 | TimeStatsHelper::TimeStatsLayer const* r) { |
| 124 | return l->totalFrames > r->totalFrames; |
| 125 | }); |
| 126 | |
| 127 | if (mMaxPulledLayers < dumpStats.size()) { |
| 128 | dumpStats.resize(mMaxPulledLayers); |
| 129 | } |
| 130 | |
| 131 | for (const auto& layer : dumpStats) { |
| 132 | struct stats_event* event = mStatsDelegate->addStatsEventToPullData(data); |
| 133 | mStatsDelegate->statsEventSetAtomId(event, android::util::SURFACEFLINGER_STATS_LAYER_INFO); |
| 134 | mStatsDelegate->statsEventWriteString8(event, layer->layerName.c_str()); |
| 135 | mStatsDelegate->statsEventWriteInt64(event, layer->totalFrames); |
| 136 | mStatsDelegate->statsEventWriteInt64(event, layer->droppedFrames); |
| 137 | |
| 138 | for (const auto& name : kHistogramNames) { |
| 139 | const auto& histogram = layer->deltas.find(name); |
| 140 | if (histogram == layer->deltas.cend()) { |
| 141 | mStatsDelegate->statsEventWriteByteArray(event, nullptr, 0); |
| 142 | } else { |
| 143 | std::string bytes = histogramToProtoByteString(histogram->second.hist, |
| 144 | mMaxPulledHistogramBuckets); |
| 145 | mStatsDelegate->statsEventWriteByteArray(event, (const uint8_t*)bytes.c_str(), |
| 146 | bytes.size()); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | mStatsDelegate->statsEventBuild(event); |
| 151 | } |
| 152 | clearLayersLocked(); |
| 153 | |
| 154 | return STATS_PULL_SUCCESS; |
| 155 | } |
| 156 | |
| 157 | TimeStats::TimeStats() : TimeStats(nullptr, std::nullopt, std::nullopt) {} |
| 158 | |
| 159 | TimeStats::TimeStats(std::unique_ptr<StatsEventDelegate> statsDelegate, |
| 160 | std::optional<size_t> maxPulledLayers, |
| 161 | std::optional<size_t> maxPulledHistogramBuckets) { |
Alec Mouri | 8e2f31b | 2020-01-16 22:04:35 +0000 | [diff] [blame] | 162 | if (statsDelegate != nullptr) { |
| 163 | mStatsDelegate = std::move(statsDelegate); |
| 164 | } |
Alec Mouri | 3738434 | 2020-01-02 17:23:37 -0800 | [diff] [blame] | 165 | |
| 166 | if (maxPulledLayers) { |
| 167 | mMaxPulledLayers = *maxPulledLayers; |
| 168 | } |
| 169 | |
| 170 | if (maxPulledHistogramBuckets) { |
| 171 | mMaxPulledHistogramBuckets = *maxPulledHistogramBuckets; |
| 172 | } |
Alec Mouri | 8e2f31b | 2020-01-16 22:04:35 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Alec Mouri | 3ecd5cd | 2020-01-29 12:53:07 -0800 | [diff] [blame] | 175 | TimeStats::~TimeStats() { |
| 176 | std::lock_guard<std::mutex> lock(mMutex); |
| 177 | mStatsDelegate->unregisterStatsPullAtomCallback( |
| 178 | android::util::SURFACEFLINGER_STATS_GLOBAL_INFO); |
| 179 | mStatsDelegate->unregisterStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_LAYER_INFO); |
| 180 | } |
| 181 | |
Alec Mouri | 8e2f31b | 2020-01-16 22:04:35 +0000 | [diff] [blame] | 182 | void TimeStats::onBootFinished() { |
Alec Mouri | 3ecd5cd | 2020-01-29 12:53:07 -0800 | [diff] [blame] | 183 | std::lock_guard<std::mutex> lock(mMutex); |
| 184 | mStatsDelegate->registerStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_GLOBAL_INFO, |
| 185 | TimeStats::pullAtomCallback, nullptr, this); |
| 186 | mStatsDelegate->registerStatsPullAtomCallback(android::util::SURFACEFLINGER_STATS_LAYER_INFO, |
| 187 | TimeStats::pullAtomCallback, nullptr, this); |
Alec Mouri | b3885ad | 2019-09-06 17:08:55 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Dominik Laskowski | c286714 | 2019-01-21 11:33:38 -0800 | [diff] [blame] | 190 | void TimeStats::parseArgs(bool asProto, const Vector<String16>& args, std::string& result) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 191 | ATRACE_CALL(); |
| 192 | |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 193 | std::unordered_map<std::string, int32_t> argsMap; |
Dominik Laskowski | c286714 | 2019-01-21 11:33:38 -0800 | [diff] [blame] | 194 | for (size_t index = 0; index < args.size(); ++index) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 195 | argsMap[std::string(String8(args[index]).c_str())] = index; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | if (argsMap.count("-disable")) { |
| 199 | disable(); |
| 200 | } |
| 201 | |
| 202 | if (argsMap.count("-dump")) { |
Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 203 | std::optional<uint32_t> maxLayers = std::nullopt; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 204 | auto iter = argsMap.find("-maxlayers"); |
| 205 | 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] | 206 | int64_t value = strtol(String8(args[iter->second + 1]).c_str(), nullptr, 10); |
| 207 | value = std::clamp(value, int64_t(0), int64_t(UINT32_MAX)); |
| 208 | maxLayers = static_cast<uint32_t>(value); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 211 | dump(asProto, maxLayers, result); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | if (argsMap.count("-clear")) { |
Alec Mouri | 8e2f31b | 2020-01-16 22:04:35 +0000 | [diff] [blame] | 215 | clearAll(); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | if (argsMap.count("-enable")) { |
| 219 | enable(); |
| 220 | } |
| 221 | } |
| 222 | |
Yiwei Zhang | 7eb58b7 | 2019-04-22 19:00:02 -0700 | [diff] [blame] | 223 | std::string TimeStats::miniDump() { |
| 224 | ATRACE_CALL(); |
| 225 | |
| 226 | std::string result = "TimeStats miniDump:\n"; |
| 227 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | e926ab5 | 2019-08-14 15:16:00 -0700 | [diff] [blame] | 228 | 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] | 229 | mTimeStatsTracker.size()); |
Yiwei Zhang | e926ab5 | 2019-08-14 15:16:00 -0700 | [diff] [blame] | 230 | android::base::StringAppendF(&result, "Number of layers in the stats pool is %zu\n", |
| 231 | mTimeStats.stats.size()); |
Yiwei Zhang | 7eb58b7 | 2019-04-22 19:00:02 -0700 | [diff] [blame] | 232 | return result; |
| 233 | } |
| 234 | |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 235 | void TimeStats::incrementTotalFrames() { |
| 236 | if (!mEnabled.load()) return; |
| 237 | |
| 238 | ATRACE_CALL(); |
| 239 | |
| 240 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 241 | mTimeStats.totalFrames++; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Yiwei Zhang | 621f9d4 | 2018-05-07 10:40:55 -0700 | [diff] [blame] | 244 | void TimeStats::incrementMissedFrames() { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 245 | if (!mEnabled.load()) return; |
| 246 | |
| 247 | ATRACE_CALL(); |
| 248 | |
| 249 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 250 | mTimeStats.missedFrames++; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | void TimeStats::incrementClientCompositionFrames() { |
| 254 | if (!mEnabled.load()) return; |
| 255 | |
| 256 | ATRACE_CALL(); |
| 257 | |
| 258 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 259 | mTimeStats.clientCompositionFrames++; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 262 | void TimeStats::incrementClientCompositionReusedFrames() { |
| 263 | if (!mEnabled.load()) return; |
| 264 | |
| 265 | ATRACE_CALL(); |
| 266 | |
| 267 | std::lock_guard<std::mutex> lock(mMutex); |
| 268 | mTimeStats.clientCompositionReusedFrames++; |
| 269 | } |
| 270 | |
Alec Mouri | 9519bf1 | 2019-11-15 16:54:44 -0800 | [diff] [blame] | 271 | static int32_t msBetween(nsecs_t start, nsecs_t end) { |
| 272 | int64_t delta = std::chrono::duration_cast<std::chrono::milliseconds>( |
| 273 | std::chrono::nanoseconds(end - start)) |
| 274 | .count(); |
| 275 | delta = std::clamp(delta, int64_t(INT32_MIN), int64_t(INT32_MAX)); |
| 276 | return static_cast<int32_t>(delta); |
| 277 | } |
| 278 | |
| 279 | void TimeStats::recordFrameDuration(nsecs_t startTime, nsecs_t endTime) { |
| 280 | if (!mEnabled.load()) return; |
| 281 | |
| 282 | std::lock_guard<std::mutex> lock(mMutex); |
| 283 | if (mPowerTime.powerMode == HWC_POWER_MODE_NORMAL) { |
| 284 | mTimeStats.frameDuration.insert(msBetween(startTime, endTime)); |
| 285 | } |
| 286 | } |
| 287 | |
Alec Mouri | e4034bb | 2019-11-19 12:45:54 -0800 | [diff] [blame] | 288 | void TimeStats::recordRenderEngineDuration(nsecs_t startTime, nsecs_t endTime) { |
| 289 | if (!mEnabled.load()) return; |
| 290 | |
| 291 | std::lock_guard<std::mutex> lock(mMutex); |
| 292 | if (mGlobalRecord.renderEngineDurations.size() == MAX_NUM_TIME_RECORDS) { |
| 293 | ALOGE("RenderEngineTimes are already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS); |
| 294 | mGlobalRecord.renderEngineDurations.pop_front(); |
| 295 | } |
| 296 | mGlobalRecord.renderEngineDurations.push_back({startTime, endTime}); |
| 297 | } |
| 298 | |
| 299 | void TimeStats::recordRenderEngineDuration(nsecs_t startTime, |
| 300 | const std::shared_ptr<FenceTime>& endTime) { |
| 301 | if (!mEnabled.load()) return; |
| 302 | |
| 303 | std::lock_guard<std::mutex> lock(mMutex); |
| 304 | if (mGlobalRecord.renderEngineDurations.size() == MAX_NUM_TIME_RECORDS) { |
| 305 | ALOGE("RenderEngineTimes are already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS); |
| 306 | mGlobalRecord.renderEngineDurations.pop_front(); |
| 307 | } |
| 308 | mGlobalRecord.renderEngineDurations.push_back({startTime, endTime}); |
| 309 | } |
| 310 | |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 311 | bool TimeStats::recordReadyLocked(int32_t layerId, TimeRecord* timeRecord) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 312 | if (!timeRecord->ready) { |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 313 | ALOGV("[%d]-[%" PRIu64 "]-presentFence is still not received", layerId, |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 314 | timeRecord->frameTime.frameNumber); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 315 | return false; |
| 316 | } |
| 317 | |
| 318 | if (timeRecord->acquireFence != nullptr) { |
| 319 | if (timeRecord->acquireFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) { |
| 320 | return false; |
| 321 | } |
| 322 | if (timeRecord->acquireFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) { |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 323 | timeRecord->frameTime.acquireTime = timeRecord->acquireFence->getSignalTime(); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 324 | timeRecord->acquireFence = nullptr; |
| 325 | } else { |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 326 | ALOGV("[%d]-[%" PRIu64 "]-acquireFence signal time is invalid", layerId, |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 327 | timeRecord->frameTime.frameNumber); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | |
| 331 | if (timeRecord->presentFence != nullptr) { |
| 332 | if (timeRecord->presentFence->getSignalTime() == Fence::SIGNAL_TIME_PENDING) { |
| 333 | return false; |
| 334 | } |
| 335 | if (timeRecord->presentFence->getSignalTime() != Fence::SIGNAL_TIME_INVALID) { |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 336 | timeRecord->frameTime.presentTime = timeRecord->presentFence->getSignalTime(); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 337 | timeRecord->presentFence = nullptr; |
| 338 | } else { |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 339 | ALOGV("[%d]-[%" PRIu64 "]-presentFence signal time invalid", layerId, |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 340 | timeRecord->frameTime.frameNumber); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | |
| 344 | return true; |
| 345 | } |
| 346 | |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 347 | void TimeStats::flushAvailableRecordsToStatsLocked(int32_t layerId) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 348 | ATRACE_CALL(); |
| 349 | |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 350 | LayerRecord& layerRecord = mTimeStatsTracker[layerId]; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 351 | TimeRecord& prevTimeRecord = layerRecord.prevTimeRecord; |
Yiwei Zhang | c5f2c45 | 2018-05-08 16:31:56 -0700 | [diff] [blame] | 352 | std::deque<TimeRecord>& timeRecords = layerRecord.timeRecords; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 353 | while (!timeRecords.empty()) { |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 354 | if (!recordReadyLocked(layerId, &timeRecords[0])) break; |
| 355 | ALOGV("[%d]-[%" PRIu64 "]-presentFenceTime[%" PRId64 "]", layerId, |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 356 | timeRecords[0].frameTime.frameNumber, timeRecords[0].frameTime.presentTime); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 357 | |
| 358 | if (prevTimeRecord.ready) { |
Yiwei Zhang | eafa5cc | 2019-07-26 15:06:25 -0700 | [diff] [blame] | 359 | const std::string& layerName = layerRecord.layerName; |
Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 360 | if (!mTimeStats.stats.count(layerName)) { |
| 361 | mTimeStats.stats[layerName].layerName = layerName; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 362 | } |
Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 363 | TimeStatsHelper::TimeStatsLayer& timeStatsLayer = mTimeStats.stats[layerName]; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 364 | timeStatsLayer.totalFrames++; |
Yiwei Zhang | eaeea06 | 2018-06-28 14:46:51 -0700 | [diff] [blame] | 365 | timeStatsLayer.droppedFrames += layerRecord.droppedFrames; |
Alec Mouri | 91f6df3 | 2020-01-30 08:48:58 -0800 | [diff] [blame] | 366 | timeStatsLayer.lateAcquireFrames += layerRecord.lateAcquireFrames; |
| 367 | timeStatsLayer.badDesiredPresentFrames += layerRecord.badDesiredPresentFrames; |
| 368 | |
Yiwei Zhang | eaeea06 | 2018-06-28 14:46:51 -0700 | [diff] [blame] | 369 | layerRecord.droppedFrames = 0; |
Alec Mouri | 91f6df3 | 2020-01-30 08:48:58 -0800 | [diff] [blame] | 370 | layerRecord.lateAcquireFrames = 0; |
| 371 | layerRecord.badDesiredPresentFrames = 0; |
Yiwei Zhang | eaeea06 | 2018-06-28 14:46:51 -0700 | [diff] [blame] | 372 | |
| 373 | const int32_t postToAcquireMs = msBetween(timeRecords[0].frameTime.postTime, |
| 374 | timeRecords[0].frameTime.acquireTime); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 375 | ALOGV("[%d]-[%" PRIu64 "]-post2acquire[%d]", layerId, |
Yiwei Zhang | eaeea06 | 2018-06-28 14:46:51 -0700 | [diff] [blame] | 376 | timeRecords[0].frameTime.frameNumber, postToAcquireMs); |
| 377 | timeStatsLayer.deltas["post2acquire"].insert(postToAcquireMs); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 378 | |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 379 | const int32_t postToPresentMs = msBetween(timeRecords[0].frameTime.postTime, |
| 380 | timeRecords[0].frameTime.presentTime); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 381 | ALOGV("[%d]-[%" PRIu64 "]-post2present[%d]", layerId, |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 382 | timeRecords[0].frameTime.frameNumber, postToPresentMs); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 383 | timeStatsLayer.deltas["post2present"].insert(postToPresentMs); |
| 384 | |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 385 | const int32_t acquireToPresentMs = msBetween(timeRecords[0].frameTime.acquireTime, |
| 386 | timeRecords[0].frameTime.presentTime); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 387 | ALOGV("[%d]-[%" PRIu64 "]-acquire2present[%d]", layerId, |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 388 | timeRecords[0].frameTime.frameNumber, acquireToPresentMs); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 389 | timeStatsLayer.deltas["acquire2present"].insert(acquireToPresentMs); |
| 390 | |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 391 | const int32_t latchToPresentMs = msBetween(timeRecords[0].frameTime.latchTime, |
| 392 | timeRecords[0].frameTime.presentTime); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 393 | ALOGV("[%d]-[%" PRIu64 "]-latch2present[%d]", layerId, |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 394 | timeRecords[0].frameTime.frameNumber, latchToPresentMs); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 395 | timeStatsLayer.deltas["latch2present"].insert(latchToPresentMs); |
| 396 | |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 397 | const int32_t desiredToPresentMs = msBetween(timeRecords[0].frameTime.desiredTime, |
| 398 | timeRecords[0].frameTime.presentTime); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 399 | ALOGV("[%d]-[%" PRIu64 "]-desired2present[%d]", layerId, |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 400 | timeRecords[0].frameTime.frameNumber, desiredToPresentMs); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 401 | timeStatsLayer.deltas["desired2present"].insert(desiredToPresentMs); |
| 402 | |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 403 | const int32_t presentToPresentMs = msBetween(prevTimeRecord.frameTime.presentTime, |
| 404 | timeRecords[0].frameTime.presentTime); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 405 | ALOGV("[%d]-[%" PRIu64 "]-present2present[%d]", layerId, |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 406 | timeRecords[0].frameTime.frameNumber, presentToPresentMs); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 407 | timeStatsLayer.deltas["present2present"].insert(presentToPresentMs); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 408 | } |
| 409 | prevTimeRecord = timeRecords[0]; |
Yiwei Zhang | c5f2c45 | 2018-05-08 16:31:56 -0700 | [diff] [blame] | 410 | timeRecords.pop_front(); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 411 | layerRecord.waitData--; |
| 412 | } |
| 413 | } |
| 414 | |
Yiwei Zhang | 8bec7e8 | 2019-10-07 18:08:26 -0700 | [diff] [blame] | 415 | static constexpr const char* kPopupWindowPrefix = "PopupWindow"; |
| 416 | static const size_t kMinLenLayerName = std::strlen(kPopupWindowPrefix); |
Yiwei Zhang | bd40832 | 2018-10-15 18:31:53 -0700 | [diff] [blame] | 417 | |
Yiwei Zhang | 8bec7e8 | 2019-10-07 18:08:26 -0700 | [diff] [blame] | 418 | // Avoid tracking the "PopupWindow:<random hash>#<number>" layers |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 419 | static bool layerNameIsValid(const std::string& layerName) { |
Yiwei Zhang | 8bec7e8 | 2019-10-07 18:08:26 -0700 | [diff] [blame] | 420 | return layerName.length() >= kMinLenLayerName && |
| 421 | layerName.compare(0, kMinLenLayerName, kPopupWindowPrefix) != 0; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 424 | void TimeStats::setPostTime(int32_t layerId, uint64_t frameNumber, const std::string& layerName, |
Yiwei Zhang | 8e8fe52 | 2018-11-02 18:34:07 -0700 | [diff] [blame] | 425 | nsecs_t postTime) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 426 | if (!mEnabled.load()) return; |
| 427 | |
| 428 | ATRACE_CALL(); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 429 | ALOGV("[%d]-[%" PRIu64 "]-[%s]-PostTime[%" PRId64 "]", layerId, frameNumber, layerName.c_str(), |
Yiwei Zhang | 8e8fe52 | 2018-11-02 18:34:07 -0700 | [diff] [blame] | 430 | postTime); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 431 | |
| 432 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | e926ab5 | 2019-08-14 15:16:00 -0700 | [diff] [blame] | 433 | if (!mTimeStats.stats.count(layerName) && mTimeStats.stats.size() >= MAX_NUM_LAYER_STATS) { |
| 434 | return; |
| 435 | } |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 436 | if (!mTimeStatsTracker.count(layerId) && mTimeStatsTracker.size() < MAX_NUM_LAYER_RECORDS && |
Yiwei Zhang | 7eb58b7 | 2019-04-22 19:00:02 -0700 | [diff] [blame] | 437 | layerNameIsValid(layerName)) { |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 438 | mTimeStatsTracker[layerId].layerName = layerName; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 439 | } |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 440 | if (!mTimeStatsTracker.count(layerId)) return; |
| 441 | LayerRecord& layerRecord = mTimeStatsTracker[layerId]; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 442 | if (layerRecord.timeRecords.size() == MAX_NUM_TIME_RECORDS) { |
Yiwei Zhang | af8ee94 | 2018-11-22 00:15:23 -0800 | [diff] [blame] | 443 | 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] | 444 | layerId, layerRecord.layerName.c_str(), MAX_NUM_TIME_RECORDS); |
| 445 | mTimeStatsTracker.erase(layerId); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 446 | return; |
| 447 | } |
| 448 | // For most media content, the acquireFence is invalid because the buffer is |
| 449 | // ready at the queueBuffer stage. In this case, acquireTime should be given |
| 450 | // a default value as postTime. |
| 451 | TimeRecord timeRecord = { |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 452 | .frameTime = |
| 453 | { |
| 454 | .frameNumber = frameNumber, |
| 455 | .postTime = postTime, |
Yiwei Zhang | af8ee94 | 2018-11-22 00:15:23 -0800 | [diff] [blame] | 456 | .latchTime = postTime, |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 457 | .acquireTime = postTime, |
Yiwei Zhang | af8ee94 | 2018-11-22 00:15:23 -0800 | [diff] [blame] | 458 | .desiredTime = postTime, |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 459 | }, |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 460 | }; |
| 461 | layerRecord.timeRecords.push_back(timeRecord); |
| 462 | if (layerRecord.waitData < 0 || |
| 463 | layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size())) |
| 464 | layerRecord.waitData = layerRecord.timeRecords.size() - 1; |
| 465 | } |
| 466 | |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 467 | void TimeStats::setLatchTime(int32_t layerId, uint64_t frameNumber, nsecs_t latchTime) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 468 | if (!mEnabled.load()) return; |
| 469 | |
| 470 | ATRACE_CALL(); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 471 | ALOGV("[%d]-[%" PRIu64 "]-LatchTime[%" PRId64 "]", layerId, frameNumber, latchTime); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 472 | |
| 473 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 474 | if (!mTimeStatsTracker.count(layerId)) return; |
| 475 | LayerRecord& layerRecord = mTimeStatsTracker[layerId]; |
Yiwei Zhang | cb7dd00 | 2019-04-16 11:03:01 -0700 | [diff] [blame] | 476 | if (layerRecord.waitData < 0 || |
| 477 | layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size())) |
| 478 | return; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 479 | TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData]; |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 480 | if (timeRecord.frameTime.frameNumber == frameNumber) { |
| 481 | timeRecord.frameTime.latchTime = latchTime; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | |
Alec Mouri | 91f6df3 | 2020-01-30 08:48:58 -0800 | [diff] [blame] | 485 | void TimeStats::incrementLatchSkipped(int32_t layerId, LatchSkipReason reason) { |
| 486 | if (!mEnabled.load()) return; |
| 487 | |
| 488 | ATRACE_CALL(); |
| 489 | ALOGV("[%d]-LatchSkipped-Reason[%d]", layerId, |
| 490 | static_cast<std::underlying_type<LatchSkipReason>::type>(reason)); |
| 491 | |
| 492 | std::lock_guard<std::mutex> lock(mMutex); |
| 493 | if (!mTimeStatsTracker.count(layerId)) return; |
| 494 | LayerRecord& layerRecord = mTimeStatsTracker[layerId]; |
| 495 | |
| 496 | switch (reason) { |
| 497 | case LatchSkipReason::LateAcquire: |
| 498 | layerRecord.lateAcquireFrames++; |
| 499 | break; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | void TimeStats::incrementBadDesiredPresent(int32_t layerId) { |
| 504 | if (!mEnabled.load()) return; |
| 505 | |
| 506 | ATRACE_CALL(); |
| 507 | ALOGV("[%d]-BadDesiredPresent", layerId); |
| 508 | |
| 509 | std::lock_guard<std::mutex> lock(mMutex); |
| 510 | if (!mTimeStatsTracker.count(layerId)) return; |
| 511 | LayerRecord& layerRecord = mTimeStatsTracker[layerId]; |
| 512 | layerRecord.badDesiredPresentFrames++; |
| 513 | } |
| 514 | |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 515 | void TimeStats::setDesiredTime(int32_t layerId, uint64_t frameNumber, nsecs_t desiredTime) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 516 | if (!mEnabled.load()) return; |
| 517 | |
| 518 | ATRACE_CALL(); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 519 | ALOGV("[%d]-[%" PRIu64 "]-DesiredTime[%" PRId64 "]", layerId, frameNumber, desiredTime); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 520 | |
| 521 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 522 | if (!mTimeStatsTracker.count(layerId)) return; |
| 523 | LayerRecord& layerRecord = mTimeStatsTracker[layerId]; |
Yiwei Zhang | cb7dd00 | 2019-04-16 11:03:01 -0700 | [diff] [blame] | 524 | if (layerRecord.waitData < 0 || |
| 525 | layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size())) |
| 526 | return; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 527 | TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData]; |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 528 | if (timeRecord.frameTime.frameNumber == frameNumber) { |
| 529 | timeRecord.frameTime.desiredTime = desiredTime; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 533 | void TimeStats::setAcquireTime(int32_t layerId, uint64_t frameNumber, nsecs_t acquireTime) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 534 | if (!mEnabled.load()) return; |
| 535 | |
| 536 | ATRACE_CALL(); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 537 | ALOGV("[%d]-[%" PRIu64 "]-AcquireTime[%" PRId64 "]", layerId, frameNumber, acquireTime); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 538 | |
| 539 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 540 | if (!mTimeStatsTracker.count(layerId)) return; |
| 541 | LayerRecord& layerRecord = mTimeStatsTracker[layerId]; |
Yiwei Zhang | cb7dd00 | 2019-04-16 11:03:01 -0700 | [diff] [blame] | 542 | if (layerRecord.waitData < 0 || |
| 543 | layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size())) |
| 544 | return; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 545 | TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData]; |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 546 | if (timeRecord.frameTime.frameNumber == frameNumber) { |
| 547 | timeRecord.frameTime.acquireTime = acquireTime; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 548 | } |
| 549 | } |
| 550 | |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 551 | void TimeStats::setAcquireFence(int32_t layerId, uint64_t frameNumber, |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 552 | const std::shared_ptr<FenceTime>& acquireFence) { |
| 553 | if (!mEnabled.load()) return; |
| 554 | |
| 555 | ATRACE_CALL(); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 556 | ALOGV("[%d]-[%" PRIu64 "]-AcquireFenceTime[%" PRId64 "]", layerId, frameNumber, |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 557 | acquireFence->getSignalTime()); |
| 558 | |
| 559 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 560 | if (!mTimeStatsTracker.count(layerId)) return; |
| 561 | LayerRecord& layerRecord = mTimeStatsTracker[layerId]; |
Yiwei Zhang | cb7dd00 | 2019-04-16 11:03:01 -0700 | [diff] [blame] | 562 | if (layerRecord.waitData < 0 || |
| 563 | layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size())) |
| 564 | return; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 565 | TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData]; |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 566 | if (timeRecord.frameTime.frameNumber == frameNumber) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 567 | timeRecord.acquireFence = acquireFence; |
| 568 | } |
| 569 | } |
| 570 | |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 571 | void TimeStats::setPresentTime(int32_t layerId, uint64_t frameNumber, nsecs_t presentTime) { |
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 "]-PresentTime[%" PRId64 "]", layerId, frameNumber, presentTime); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 576 | |
| 577 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 578 | if (!mTimeStatsTracker.count(layerId)) return; |
| 579 | LayerRecord& layerRecord = mTimeStatsTracker[layerId]; |
Yiwei Zhang | cb7dd00 | 2019-04-16 11:03:01 -0700 | [diff] [blame] | 580 | if (layerRecord.waitData < 0 || |
| 581 | layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size())) |
| 582 | return; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 583 | TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData]; |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 584 | if (timeRecord.frameTime.frameNumber == frameNumber) { |
| 585 | timeRecord.frameTime.presentTime = presentTime; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 586 | timeRecord.ready = true; |
| 587 | layerRecord.waitData++; |
| 588 | } |
| 589 | |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 590 | flushAvailableRecordsToStatsLocked(layerId); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 593 | void TimeStats::setPresentFence(int32_t layerId, uint64_t frameNumber, |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 594 | const std::shared_ptr<FenceTime>& presentFence) { |
| 595 | if (!mEnabled.load()) return; |
| 596 | |
| 597 | ATRACE_CALL(); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 598 | ALOGV("[%d]-[%" PRIu64 "]-PresentFenceTime[%" PRId64 "]", layerId, frameNumber, |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 599 | presentFence->getSignalTime()); |
| 600 | |
| 601 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 602 | if (!mTimeStatsTracker.count(layerId)) return; |
| 603 | LayerRecord& layerRecord = mTimeStatsTracker[layerId]; |
Yiwei Zhang | cb7dd00 | 2019-04-16 11:03:01 -0700 | [diff] [blame] | 604 | if (layerRecord.waitData < 0 || |
| 605 | layerRecord.waitData >= static_cast<int32_t>(layerRecord.timeRecords.size())) |
| 606 | return; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 607 | TimeRecord& timeRecord = layerRecord.timeRecords[layerRecord.waitData]; |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 608 | if (timeRecord.frameTime.frameNumber == frameNumber) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 609 | timeRecord.presentFence = presentFence; |
| 610 | timeRecord.ready = true; |
| 611 | layerRecord.waitData++; |
| 612 | } |
| 613 | |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 614 | flushAvailableRecordsToStatsLocked(layerId); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 615 | } |
| 616 | |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 617 | void TimeStats::onDestroy(int32_t layerId) { |
Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 618 | ATRACE_CALL(); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 619 | ALOGV("[%d]-onDestroy", layerId); |
Mikael Pessa | 90092f4 | 2019-08-26 17:22:04 -0700 | [diff] [blame] | 620 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 621 | mTimeStatsTracker.erase(layerId); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 622 | } |
| 623 | |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 624 | void TimeStats::removeTimeRecord(int32_t layerId, uint64_t frameNumber) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 625 | if (!mEnabled.load()) return; |
| 626 | |
| 627 | ATRACE_CALL(); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 628 | ALOGV("[%d]-[%" PRIu64 "]-removeTimeRecord", layerId, frameNumber); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 629 | |
| 630 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 631 | if (!mTimeStatsTracker.count(layerId)) return; |
| 632 | LayerRecord& layerRecord = mTimeStatsTracker[layerId]; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 633 | size_t removeAt = 0; |
| 634 | for (const TimeRecord& record : layerRecord.timeRecords) { |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 635 | if (record.frameTime.frameNumber == frameNumber) break; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 636 | removeAt++; |
| 637 | } |
| 638 | if (removeAt == layerRecord.timeRecords.size()) return; |
| 639 | layerRecord.timeRecords.erase(layerRecord.timeRecords.begin() + removeAt); |
| 640 | if (layerRecord.waitData > static_cast<int32_t>(removeAt)) { |
Yiwei Zhang | eaeea06 | 2018-06-28 14:46:51 -0700 | [diff] [blame] | 641 | layerRecord.waitData--; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 642 | } |
Yiwei Zhang | eaeea06 | 2018-06-28 14:46:51 -0700 | [diff] [blame] | 643 | layerRecord.droppedFrames++; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 644 | } |
| 645 | |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 646 | void TimeStats::flushPowerTimeLocked() { |
Yiwei Zhang | e5c49d5 | 2018-10-29 00:15:31 -0700 | [diff] [blame] | 647 | if (!mEnabled.load()) return; |
| 648 | |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 649 | nsecs_t curTime = systemTime(); |
| 650 | // elapsedTime is in milliseconds. |
| 651 | int64_t elapsedTime = (curTime - mPowerTime.prevTime) / 1000000; |
| 652 | |
| 653 | switch (mPowerTime.powerMode) { |
| 654 | case HWC_POWER_MODE_NORMAL: |
| 655 | mTimeStats.displayOnTime += elapsedTime; |
| 656 | break; |
| 657 | case HWC_POWER_MODE_OFF: |
| 658 | case HWC_POWER_MODE_DOZE: |
| 659 | case HWC_POWER_MODE_DOZE_SUSPEND: |
| 660 | default: |
| 661 | break; |
| 662 | } |
| 663 | |
| 664 | mPowerTime.prevTime = curTime; |
| 665 | } |
| 666 | |
| 667 | void TimeStats::setPowerMode(int32_t powerMode) { |
| 668 | if (!mEnabled.load()) { |
| 669 | std::lock_guard<std::mutex> lock(mMutex); |
| 670 | mPowerTime.powerMode = powerMode; |
| 671 | return; |
| 672 | } |
| 673 | |
| 674 | std::lock_guard<std::mutex> lock(mMutex); |
| 675 | if (powerMode == mPowerTime.powerMode) return; |
| 676 | |
| 677 | flushPowerTimeLocked(); |
| 678 | mPowerTime.powerMode = powerMode; |
| 679 | } |
| 680 | |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 681 | void TimeStats::recordRefreshRate(uint32_t fps, nsecs_t duration) { |
| 682 | std::lock_guard<std::mutex> lock(mMutex); |
| 683 | if (mTimeStats.refreshRateStats.count(fps)) { |
| 684 | mTimeStats.refreshRateStats[fps] += duration; |
| 685 | } else { |
| 686 | mTimeStats.refreshRateStats.insert({fps, duration}); |
| 687 | } |
| 688 | } |
| 689 | |
Yiwei Zhang | ce6ebc0 | 2018-10-20 12:42:38 -0700 | [diff] [blame] | 690 | void TimeStats::flushAvailableGlobalRecordsToStatsLocked() { |
| 691 | ATRACE_CALL(); |
| 692 | |
| 693 | while (!mGlobalRecord.presentFences.empty()) { |
| 694 | const nsecs_t curPresentTime = mGlobalRecord.presentFences.front()->getSignalTime(); |
| 695 | if (curPresentTime == Fence::SIGNAL_TIME_PENDING) break; |
| 696 | |
| 697 | if (curPresentTime == Fence::SIGNAL_TIME_INVALID) { |
| 698 | ALOGE("GlobalPresentFence is invalid!"); |
| 699 | mGlobalRecord.prevPresentTime = 0; |
| 700 | mGlobalRecord.presentFences.pop_front(); |
| 701 | continue; |
| 702 | } |
| 703 | |
| 704 | ALOGV("GlobalPresentFenceTime[%" PRId64 "]", |
| 705 | mGlobalRecord.presentFences.front()->getSignalTime()); |
| 706 | |
Yiwei Zhang | e5c49d5 | 2018-10-29 00:15:31 -0700 | [diff] [blame] | 707 | if (mGlobalRecord.prevPresentTime != 0) { |
| 708 | const int32_t presentToPresentMs = |
| 709 | msBetween(mGlobalRecord.prevPresentTime, curPresentTime); |
| 710 | ALOGV("Global present2present[%d] prev[%" PRId64 "] curr[%" PRId64 "]", |
| 711 | presentToPresentMs, mGlobalRecord.prevPresentTime, curPresentTime); |
| 712 | mTimeStats.presentToPresent.insert(presentToPresentMs); |
| 713 | } |
Yiwei Zhang | ce6ebc0 | 2018-10-20 12:42:38 -0700 | [diff] [blame] | 714 | |
Yiwei Zhang | ce6ebc0 | 2018-10-20 12:42:38 -0700 | [diff] [blame] | 715 | mGlobalRecord.prevPresentTime = curPresentTime; |
| 716 | mGlobalRecord.presentFences.pop_front(); |
| 717 | } |
Alec Mouri | e4034bb | 2019-11-19 12:45:54 -0800 | [diff] [blame] | 718 | while (!mGlobalRecord.renderEngineDurations.empty()) { |
| 719 | const auto duration = mGlobalRecord.renderEngineDurations.front(); |
| 720 | const auto& endTime = duration.endTime; |
| 721 | |
| 722 | nsecs_t endNs = -1; |
| 723 | |
| 724 | if (auto val = std::get_if<nsecs_t>(&endTime)) { |
| 725 | endNs = *val; |
| 726 | } else { |
| 727 | endNs = std::get<std::shared_ptr<FenceTime>>(endTime)->getSignalTime(); |
| 728 | } |
| 729 | |
| 730 | if (endNs == Fence::SIGNAL_TIME_PENDING) break; |
| 731 | |
| 732 | if (endNs < 0) { |
| 733 | ALOGE("RenderEngineTiming is invalid!"); |
| 734 | mGlobalRecord.renderEngineDurations.pop_front(); |
| 735 | continue; |
| 736 | } |
| 737 | |
| 738 | const int32_t renderEngineMs = msBetween(duration.startTime, endNs); |
| 739 | mTimeStats.renderEngineTiming.insert(renderEngineMs); |
| 740 | |
| 741 | mGlobalRecord.renderEngineDurations.pop_front(); |
| 742 | } |
Yiwei Zhang | ce6ebc0 | 2018-10-20 12:42:38 -0700 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | void TimeStats::setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) { |
| 746 | if (!mEnabled.load()) return; |
| 747 | |
| 748 | ATRACE_CALL(); |
| 749 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | e5c49d5 | 2018-10-29 00:15:31 -0700 | [diff] [blame] | 750 | if (presentFence == nullptr || !presentFence->isValid()) { |
| 751 | mGlobalRecord.prevPresentTime = 0; |
| 752 | return; |
| 753 | } |
| 754 | |
| 755 | if (mPowerTime.powerMode != HWC_POWER_MODE_NORMAL) { |
| 756 | // Try flushing the last present fence on HWC_POWER_MODE_NORMAL. |
| 757 | flushAvailableGlobalRecordsToStatsLocked(); |
| 758 | mGlobalRecord.presentFences.clear(); |
Yiwei Zhang | ce6ebc0 | 2018-10-20 12:42:38 -0700 | [diff] [blame] | 759 | mGlobalRecord.prevPresentTime = 0; |
| 760 | return; |
| 761 | } |
| 762 | |
| 763 | if (mGlobalRecord.presentFences.size() == MAX_NUM_TIME_RECORDS) { |
| 764 | // The front presentFence must be trapped in pending status in this |
| 765 | // case. Try dequeuing the front one to recover. |
| 766 | ALOGE("GlobalPresentFences is already at its maximum size[%zu]", MAX_NUM_TIME_RECORDS); |
| 767 | mGlobalRecord.prevPresentTime = 0; |
| 768 | mGlobalRecord.presentFences.pop_front(); |
| 769 | } |
| 770 | |
| 771 | mGlobalRecord.presentFences.emplace_back(presentFence); |
| 772 | flushAvailableGlobalRecordsToStatsLocked(); |
| 773 | } |
| 774 | |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 775 | void TimeStats::enable() { |
| 776 | if (mEnabled.load()) return; |
| 777 | |
| 778 | ATRACE_CALL(); |
| 779 | |
| 780 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 781 | mEnabled.store(true); |
Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 782 | mTimeStats.statsStart = static_cast<int64_t>(std::time(0)); |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 783 | mPowerTime.prevTime = systemTime(); |
Yiwei Zhang | e5c49d5 | 2018-10-29 00:15:31 -0700 | [diff] [blame] | 784 | ALOGD("Enabled"); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | void TimeStats::disable() { |
| 788 | if (!mEnabled.load()) return; |
| 789 | |
| 790 | ATRACE_CALL(); |
| 791 | |
| 792 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | e5c49d5 | 2018-10-29 00:15:31 -0700 | [diff] [blame] | 793 | flushPowerTimeLocked(); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 794 | mEnabled.store(false); |
Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 795 | mTimeStats.statsEnd = static_cast<int64_t>(std::time(0)); |
Yiwei Zhang | e5c49d5 | 2018-10-29 00:15:31 -0700 | [diff] [blame] | 796 | ALOGD("Disabled"); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 797 | } |
| 798 | |
Alec Mouri | 8e2f31b | 2020-01-16 22:04:35 +0000 | [diff] [blame] | 799 | void TimeStats::clearAll() { |
| 800 | std::lock_guard<std::mutex> lock(mMutex); |
| 801 | clearGlobalLocked(); |
| 802 | clearLayersLocked(); |
| 803 | } |
| 804 | |
| 805 | void TimeStats::clearGlobalLocked() { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 806 | ATRACE_CALL(); |
| 807 | |
Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 808 | mTimeStats.statsStart = (mEnabled.load() ? static_cast<int64_t>(std::time(0)) : 0); |
| 809 | mTimeStats.statsEnd = 0; |
| 810 | mTimeStats.totalFrames = 0; |
| 811 | mTimeStats.missedFrames = 0; |
| 812 | mTimeStats.clientCompositionFrames = 0; |
Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 813 | mTimeStats.clientCompositionReusedFrames = 0; |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 814 | mTimeStats.displayOnTime = 0; |
Yiwei Zhang | ce6ebc0 | 2018-10-20 12:42:38 -0700 | [diff] [blame] | 815 | mTimeStats.presentToPresent.hist.clear(); |
Alec Mouri | 31ac64a | 2020-01-09 09:26:22 -0800 | [diff] [blame] | 816 | mTimeStats.frameDuration.hist.clear(); |
| 817 | mTimeStats.renderEngineTiming.hist.clear(); |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 818 | mTimeStats.refreshRateStats.clear(); |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 819 | mPowerTime.prevTime = systemTime(); |
Yiwei Zhang | e5c49d5 | 2018-10-29 00:15:31 -0700 | [diff] [blame] | 820 | mGlobalRecord.prevPresentTime = 0; |
| 821 | mGlobalRecord.presentFences.clear(); |
Alec Mouri | 8e2f31b | 2020-01-16 22:04:35 +0000 | [diff] [blame] | 822 | ALOGD("Cleared global stats"); |
| 823 | } |
| 824 | |
| 825 | void TimeStats::clearLayersLocked() { |
| 826 | ATRACE_CALL(); |
| 827 | |
| 828 | mTimeStatsTracker.clear(); |
| 829 | mTimeStats.stats.clear(); |
| 830 | ALOGD("Cleared layer stats"); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | bool TimeStats::isEnabled() { |
| 834 | return mEnabled.load(); |
| 835 | } |
| 836 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 837 | 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] | 838 | ATRACE_CALL(); |
| 839 | |
| 840 | std::lock_guard<std::mutex> lock(mMutex); |
Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 841 | if (mTimeStats.statsStart == 0) { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 842 | return; |
| 843 | } |
| 844 | |
Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 845 | mTimeStats.statsEnd = static_cast<int64_t>(std::time(0)); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 846 | |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 847 | flushPowerTimeLocked(); |
| 848 | |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 849 | if (asProto) { |
Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 850 | ALOGD("Dumping TimeStats as proto"); |
Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 851 | SFTimeStatsGlobalProto timeStatsProto = mTimeStats.toProto(maxLayers); |
Dominik Laskowski | 4647011 | 2019-08-02 13:13:11 -0700 | [diff] [blame] | 852 | result.append(timeStatsProto.SerializeAsString()); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 853 | } else { |
Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 854 | ALOGD("Dumping TimeStats as text"); |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 855 | result.append(mTimeStats.toString(maxLayers)); |
Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 856 | result.append("\n"); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 857 | } |
| 858 | } |
| 859 | |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 860 | } // namespace impl |
| 861 | |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 862 | } // namespace android |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 863 | |
| 864 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 865 | #pragma clang diagnostic pop // ignored "-Wconversion" |