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 | */ |
| 16 | |
| 17 | #pragma once |
| 18 | |
Peiyong Lin | 65248e0 | 2020-04-18 21:15:07 -0700 | [diff] [blame] | 19 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
Alec Mouri | 9a29e67 | 2020-09-14 12:39:14 -0700 | [diff] [blame] | 20 | #include <cstdint> |
Peiyong Lin | 65248e0 | 2020-04-18 21:15:07 -0700 | [diff] [blame] | 21 | #pragma clang diagnostic push |
| 22 | #pragma clang diagnostic ignored "-Wconversion" |
Marin Shalamanov | bed7fd3 | 2020-12-21 20:02:20 +0100 | [diff] [blame] | 23 | #pragma clang diagnostic ignored "-Wextra" |
Peiyong Lin | 65248e0 | 2020-04-18 21:15:07 -0700 | [diff] [blame] | 24 | |
| 25 | #include <android/hardware/graphics/composer/2.4/IComposerClient.h> |
| 26 | |
| 27 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
Marin Shalamanov | bed7fd3 | 2020-12-21 20:02:20 +0100 | [diff] [blame] | 28 | #pragma clang diagnostic pop // ignored "-Wconversion -Wextra" |
Peiyong Lin | 65248e0 | 2020-04-18 21:15:07 -0700 | [diff] [blame] | 29 | |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 30 | #include <../Fps.h> |
| 31 | #include <gui/JankInfo.h> |
Alec Mouri | 8e2f31b | 2020-01-16 22:04:35 +0000 | [diff] [blame] | 32 | #include <stats_event.h> |
| 33 | #include <stats_pull_atom_callback.h> |
| 34 | #include <statslog.h> |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 35 | #include <timestatsproto/TimeStatsHelper.h> |
| 36 | #include <timestatsproto/TimeStatsProtoHeader.h> |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 37 | #include <ui/FenceTime.h> |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 38 | #include <utils/String16.h> |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 39 | #include <utils/Vector.h> |
| 40 | |
Yiwei Zhang | c5f2c45 | 2018-05-08 16:31:56 -0700 | [diff] [blame] | 41 | #include <deque> |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 42 | #include <mutex> |
Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 43 | #include <optional> |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 44 | #include <unordered_map> |
Alec Mouri | e4034bb | 2019-11-19 12:45:54 -0800 | [diff] [blame] | 45 | #include <variant> |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 46 | |
| 47 | using namespace android::surfaceflinger; |
| 48 | |
| 49 | namespace android { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 50 | |
| 51 | class TimeStats { |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 52 | public: |
Ady Abraham | 8b9e612 | 2021-01-26 19:11:45 -0800 | [diff] [blame] | 53 | using SetFrameRateVote = TimeStatsHelper::SetFrameRateVote; |
| 54 | |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 55 | virtual ~TimeStats() = default; |
| 56 | |
Alec Mouri | 8e2f31b | 2020-01-16 22:04:35 +0000 | [diff] [blame] | 57 | // Called once boot has been finished to perform additional capabilities, |
| 58 | // e.g. registration to statsd. |
| 59 | virtual void onBootFinished() = 0; |
| 60 | |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 61 | virtual void parseArgs(bool asProto, const Vector<String16>& args, std::string& result) = 0; |
| 62 | virtual bool isEnabled() = 0; |
Oliver Nguyen | f3b7c9c | 2019-05-07 13:28:07 -0700 | [diff] [blame] | 63 | virtual std::string miniDump() = 0; |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 64 | |
| 65 | virtual void incrementTotalFrames() = 0; |
| 66 | virtual void incrementMissedFrames() = 0; |
| 67 | virtual void incrementClientCompositionFrames() = 0; |
Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 68 | virtual void incrementClientCompositionReusedFrames() = 0; |
Alec Mouri | 8de697e | 2020-03-19 10:52:01 -0700 | [diff] [blame] | 69 | // Increments the number of times the display refresh rate changed. |
| 70 | virtual void incrementRefreshRateSwitches() = 0; |
Alec Mouri | 8f7a010 | 2020-04-15 12:11:10 -0700 | [diff] [blame] | 71 | // Increments the number of changes in composition strategy |
| 72 | // The intention is to reflect the number of changes between hwc and gpu |
| 73 | // composition, where "gpu composition" may also include mixed composition. |
| 74 | virtual void incrementCompositionStrategyChanges() = 0; |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 75 | // Records the most up-to-date count of display event connections. |
| 76 | // The stored count will be the maximum ever recoded. |
| 77 | virtual void recordDisplayEventConnectionCount(int32_t count) = 0; |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 78 | |
Alec Mouri | 9519bf1 | 2019-11-15 16:54:44 -0800 | [diff] [blame] | 79 | // Records the start and end times for a frame. |
| 80 | // The start time is the same as the beginning of a SurfaceFlinger |
| 81 | // invalidate message. |
| 82 | // The end time corresponds to when SurfaceFlinger finishes submitting the |
| 83 | // request to HWC to present a frame. |
| 84 | virtual void recordFrameDuration(nsecs_t startTime, nsecs_t endTime) = 0; |
Alec Mouri | e4034bb | 2019-11-19 12:45:54 -0800 | [diff] [blame] | 85 | // Records the start time and end times for when RenderEngine begins work. |
| 86 | // The start time corresponds to the beginning of RenderEngine::drawLayers. |
| 87 | // The end time corresponds to when RenderEngine finishes rendering. |
| 88 | virtual void recordRenderEngineDuration(nsecs_t startTime, nsecs_t endTime) = 0; |
| 89 | // Same as above, but passes in a fence representing the end time. |
| 90 | virtual void recordRenderEngineDuration(nsecs_t startTime, |
| 91 | const std::shared_ptr<FenceTime>& readyFence) = 0; |
Alec Mouri | 9519bf1 | 2019-11-15 16:54:44 -0800 | [diff] [blame] | 92 | |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 93 | virtual void setPostTime(int32_t layerId, uint64_t frameNumber, const std::string& layerName, |
Alec Mouri | 9a29e67 | 2020-09-14 12:39:14 -0700 | [diff] [blame] | 94 | uid_t uid, nsecs_t postTime) = 0; |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 95 | virtual void setLatchTime(int32_t layerId, uint64_t frameNumber, nsecs_t latchTime) = 0; |
Alec Mouri | 91f6df3 | 2020-01-30 08:48:58 -0800 | [diff] [blame] | 96 | // Reasons why latching a particular buffer may be skipped |
| 97 | enum class LatchSkipReason { |
| 98 | // If the acquire fence did not fire on some devices we skip latching |
| 99 | // the buffer until the fence fires. |
| 100 | LateAcquire, |
| 101 | }; |
| 102 | // Increments the counter of skipped latch buffers. |
| 103 | virtual void incrementLatchSkipped(int32_t layerId, LatchSkipReason reason) = 0; |
| 104 | // Increments the counter of bad desired present times for this layer. |
| 105 | // Bad desired present times are "implausible" and cause SurfaceFlinger to |
| 106 | // latch a buffer immediately to avoid stalling. |
| 107 | virtual void incrementBadDesiredPresent(int32_t layerId) = 0; |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 108 | virtual void setDesiredTime(int32_t layerId, uint64_t frameNumber, nsecs_t desiredTime) = 0; |
| 109 | virtual void setAcquireTime(int32_t layerId, uint64_t frameNumber, nsecs_t acquireTime) = 0; |
| 110 | virtual void setAcquireFence(int32_t layerId, uint64_t frameNumber, |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 111 | const std::shared_ptr<FenceTime>& acquireFence) = 0; |
Alec Mouri | e4034bb | 2019-11-19 12:45:54 -0800 | [diff] [blame] | 112 | // SetPresent{Time, Fence} are not expected to be called in the critical |
| 113 | // rendering path, as they flush prior fences if those fences have fired. |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 114 | virtual void setPresentTime(int32_t layerId, uint64_t frameNumber, nsecs_t presentTime, |
Ady Abraham | 8b9e612 | 2021-01-26 19:11:45 -0800 | [diff] [blame] | 115 | Fps displayRefreshRate, std::optional<Fps> renderRate, |
| 116 | SetFrameRateVote frameRateVote) = 0; |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 117 | virtual void setPresentFence(int32_t layerId, uint64_t frameNumber, |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 118 | const std::shared_ptr<FenceTime>& presentFence, |
Ady Abraham | 8b9e612 | 2021-01-26 19:11:45 -0800 | [diff] [blame] | 119 | Fps displayRefreshRate, std::optional<Fps> renderRate, |
| 120 | SetFrameRateVote frameRateVote) = 0; |
Alec Mouri | 9a29e67 | 2020-09-14 12:39:14 -0700 | [diff] [blame] | 121 | |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 122 | // Increments janky frames, blamed to the provided {refreshRate, renderRate, uid, layerName} |
| 123 | // key, with JankMetadata as supplementary reasons for the jank. Because FrameTimeline is the |
| 124 | // infrastructure responsible for computing jank in the system, this is expected to be called |
| 125 | // from FrameTimeline, rather than directly from SurfaceFlinger or individual layers. If there |
| 126 | // are no jank reasons, then total frames are incremented but jank is not, for accurate |
Alec Mouri | 9a29e67 | 2020-09-14 12:39:14 -0700 | [diff] [blame] | 127 | // accounting of janky frames. |
Alec Mouri | 363faf0 | 2021-01-29 16:34:55 -0800 | [diff] [blame] | 128 | // displayDeadlineDelta, displayPresentJitter, and appDeadlineDelta are also provided in order |
| 129 | // to provide contextual information about a janky frame. These values may only be uploaded if |
| 130 | // there was an associated valid jank reason, and they must be positive. When these frame counts |
| 131 | // are incremented, these are also aggregated into a global reporting packet to help with data |
| 132 | // validation and assessing of overall device health. |
| 133 | struct JankyFramesInfo { |
| 134 | Fps refreshRate; |
| 135 | std::optional<Fps> renderRate; |
| 136 | uid_t uid = 0; |
| 137 | std::string layerName; |
| 138 | int32_t reasons = 0; |
| 139 | nsecs_t displayDeadlineDelta = 0; |
| 140 | nsecs_t displayPresentJitter = 0; |
| 141 | nsecs_t appDeadlineDelta = 0; |
| 142 | |
| 143 | bool operator==(const JankyFramesInfo& o) const { |
| 144 | return Fps::EqualsInBuckets{}(refreshRate, o.refreshRate) && |
| 145 | ((renderRate == std::nullopt && o.renderRate == std::nullopt) || |
| 146 | (renderRate != std::nullopt && o.renderRate != std::nullopt && |
| 147 | Fps::EqualsInBuckets{}(*renderRate, *o.renderRate))) && |
| 148 | uid == o.uid && layerName == o.layerName && reasons == o.reasons && |
| 149 | displayDeadlineDelta == o.displayDeadlineDelta && |
| 150 | displayPresentJitter == o.displayPresentJitter && |
| 151 | appDeadlineDelta == o.appDeadlineDelta; |
| 152 | } |
| 153 | |
| 154 | friend std::ostream& operator<<(std::ostream& os, const JankyFramesInfo& info) { |
| 155 | os << "JankyFramesInfo {"; |
| 156 | os << "\n .refreshRate = " << info.refreshRate; |
| 157 | os << "\n .renderRate = " |
| 158 | << (info.renderRate ? to_string(*info.renderRate) : "nullopt"); |
| 159 | os << "\n .uid = " << info.uid; |
| 160 | os << "\n .layerName = " << info.layerName; |
| 161 | os << "\n .reasons = " << info.reasons; |
| 162 | os << "\n .displayDeadlineDelta = " << info.displayDeadlineDelta; |
| 163 | os << "\n .displayPresentJitter = " << info.displayPresentJitter; |
| 164 | os << "\n .appDeadlineDelta = " << info.appDeadlineDelta; |
| 165 | return os << "\n}"; |
| 166 | } |
| 167 | }; |
| 168 | |
| 169 | virtual void incrementJankyFrames(const JankyFramesInfo& info) = 0; |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 170 | // Clean up the layer record |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 171 | virtual void onDestroy(int32_t layerId) = 0; |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 172 | // If SF skips or rejects a buffer, remove the corresponding TimeRecord. |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 173 | virtual void removeTimeRecord(int32_t layerId, uint64_t frameNumber) = 0; |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 174 | |
Peiyong Lin | 65248e0 | 2020-04-18 21:15:07 -0700 | [diff] [blame] | 175 | virtual void setPowerMode( |
| 176 | hardware::graphics::composer::V2_4::IComposerClient::PowerMode powerMode) = 0; |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 177 | // Source of truth is RefrehRateStats. |
| 178 | virtual void recordRefreshRate(uint32_t fps, nsecs_t duration) = 0; |
| 179 | virtual void setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) = 0; |
| 180 | }; |
| 181 | |
| 182 | namespace impl { |
| 183 | |
| 184 | class TimeStats : public android::TimeStats { |
Peiyong Lin | 65248e0 | 2020-04-18 21:15:07 -0700 | [diff] [blame] | 185 | using PowerMode = android::hardware::graphics::composer::V2_4::IComposerClient::PowerMode; |
| 186 | |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 187 | struct FrameTime { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 188 | uint64_t frameNumber = 0; |
| 189 | nsecs_t postTime = 0; |
| 190 | nsecs_t latchTime = 0; |
| 191 | nsecs_t acquireTime = 0; |
| 192 | nsecs_t desiredTime = 0; |
| 193 | nsecs_t presentTime = 0; |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 194 | }; |
| 195 | |
| 196 | struct TimeRecord { |
| 197 | bool ready = false; |
| 198 | FrameTime frameTime; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 199 | std::shared_ptr<FenceTime> acquireFence; |
| 200 | std::shared_ptr<FenceTime> presentFence; |
| 201 | }; |
| 202 | |
| 203 | struct LayerRecord { |
Alec Mouri | 9a29e67 | 2020-09-14 12:39:14 -0700 | [diff] [blame] | 204 | uid_t uid; |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 205 | std::string layerName; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 206 | // This is the index in timeRecords, at which the timestamps for that |
| 207 | // specific frame are still not fully received. This is not waiting for |
| 208 | // fences to signal, but rather waiting to receive those fences/timestamps. |
| 209 | int32_t waitData = -1; |
Yiwei Zhang | eaeea06 | 2018-06-28 14:46:51 -0700 | [diff] [blame] | 210 | uint32_t droppedFrames = 0; |
Alec Mouri | 91f6df3 | 2020-01-30 08:48:58 -0800 | [diff] [blame] | 211 | uint32_t lateAcquireFrames = 0; |
| 212 | uint32_t badDesiredPresentFrames = 0; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 213 | TimeRecord prevTimeRecord; |
Yiwei Zhang | c5f2c45 | 2018-05-08 16:31:56 -0700 | [diff] [blame] | 214 | std::deque<TimeRecord> timeRecords; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 215 | }; |
| 216 | |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 217 | struct PowerTime { |
Peiyong Lin | 65248e0 | 2020-04-18 21:15:07 -0700 | [diff] [blame] | 218 | PowerMode powerMode = PowerMode::OFF; |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 219 | nsecs_t prevTime = 0; |
| 220 | }; |
| 221 | |
Alec Mouri | e4034bb | 2019-11-19 12:45:54 -0800 | [diff] [blame] | 222 | struct RenderEngineDuration { |
| 223 | nsecs_t startTime; |
| 224 | std::variant<nsecs_t, std::shared_ptr<FenceTime>> endTime; |
| 225 | }; |
| 226 | |
Yiwei Zhang | ce6ebc0 | 2018-10-20 12:42:38 -0700 | [diff] [blame] | 227 | struct GlobalRecord { |
| 228 | nsecs_t prevPresentTime = 0; |
| 229 | std::deque<std::shared_ptr<FenceTime>> presentFences; |
Alec Mouri | e4034bb | 2019-11-19 12:45:54 -0800 | [diff] [blame] | 230 | std::deque<RenderEngineDuration> renderEngineDurations; |
Yiwei Zhang | ce6ebc0 | 2018-10-20 12:42:38 -0700 | [diff] [blame] | 231 | }; |
| 232 | |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 233 | public: |
Alec Mouri | b3885ad | 2019-09-06 17:08:55 -0700 | [diff] [blame] | 234 | TimeStats(); |
Yiwei Zhang | 7e666a5 | 2018-11-15 13:33:42 -0800 | [diff] [blame] | 235 | |
Alec Mouri | 8e2f31b | 2020-01-16 22:04:35 +0000 | [diff] [blame] | 236 | // Delegate to the statsd service and associated APIs. |
| 237 | // Production code may use this class directly, whereas unit test may define |
| 238 | // a subclass for ease of testing. |
| 239 | class StatsEventDelegate { |
| 240 | public: |
| 241 | virtual ~StatsEventDelegate() = default; |
Tej Singh | 2a457b6 | 2020-01-31 16:16:10 -0800 | [diff] [blame] | 242 | virtual AStatsEvent* addStatsEventToPullData(AStatsEventList* data) { |
| 243 | return AStatsEventList_addStatsEvent(data); |
Alec Mouri | 8e2f31b | 2020-01-16 22:04:35 +0000 | [diff] [blame] | 244 | } |
Tej Singh | 38a4b21 | 2020-03-13 19:04:51 -0700 | [diff] [blame] | 245 | virtual void setStatsPullAtomCallback(int32_t atom_tag, |
| 246 | AStatsManager_PullAtomMetadata* metadata, |
| 247 | AStatsManager_PullAtomCallback callback, |
| 248 | void* cookie) { |
| 249 | return AStatsManager_setPullAtomCallback(atom_tag, metadata, callback, cookie); |
Alec Mouri | 8e2f31b | 2020-01-16 22:04:35 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Tej Singh | 38a4b21 | 2020-03-13 19:04:51 -0700 | [diff] [blame] | 252 | virtual void clearStatsPullAtomCallback(int32_t atom_tag) { |
| 253 | return AStatsManager_clearPullAtomCallback(atom_tag); |
Alec Mouri | 8e2f31b | 2020-01-16 22:04:35 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Tej Singh | 2a457b6 | 2020-01-31 16:16:10 -0800 | [diff] [blame] | 256 | virtual void statsEventSetAtomId(AStatsEvent* event, uint32_t atom_id) { |
| 257 | return AStatsEvent_setAtomId(event, atom_id); |
Alec Mouri | 8e2f31b | 2020-01-16 22:04:35 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 260 | virtual void statsEventWriteInt32(AStatsEvent* event, int32_t field) { |
| 261 | return AStatsEvent_writeInt32(event, field); |
| 262 | } |
| 263 | |
Tej Singh | 2a457b6 | 2020-01-31 16:16:10 -0800 | [diff] [blame] | 264 | virtual void statsEventWriteInt64(AStatsEvent* event, int64_t field) { |
| 265 | return AStatsEvent_writeInt64(event, field); |
Alec Mouri | 8e2f31b | 2020-01-16 22:04:35 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Tej Singh | 2a457b6 | 2020-01-31 16:16:10 -0800 | [diff] [blame] | 268 | virtual void statsEventWriteString8(AStatsEvent* event, const char* field) { |
| 269 | return AStatsEvent_writeString(event, field); |
Alec Mouri | 3738434 | 2020-01-02 17:23:37 -0800 | [diff] [blame] | 270 | } |
| 271 | |
Tej Singh | 2a457b6 | 2020-01-31 16:16:10 -0800 | [diff] [blame] | 272 | virtual void statsEventWriteByteArray(AStatsEvent* event, const uint8_t* buf, |
Alec Mouri | 3738434 | 2020-01-02 17:23:37 -0800 | [diff] [blame] | 273 | size_t numBytes) { |
Tej Singh | 2a457b6 | 2020-01-31 16:16:10 -0800 | [diff] [blame] | 274 | return AStatsEvent_writeByteArray(event, buf, numBytes); |
Alec Mouri | 3738434 | 2020-01-02 17:23:37 -0800 | [diff] [blame] | 275 | } |
| 276 | |
Tej Singh | 2a457b6 | 2020-01-31 16:16:10 -0800 | [diff] [blame] | 277 | virtual void statsEventBuild(AStatsEvent* event) { return AStatsEvent_build(event); } |
Alec Mouri | 8e2f31b | 2020-01-16 22:04:35 +0000 | [diff] [blame] | 278 | }; |
| 279 | // For testing only for injecting custom dependencies. |
Alec Mouri | 3738434 | 2020-01-02 17:23:37 -0800 | [diff] [blame] | 280 | TimeStats(std::unique_ptr<StatsEventDelegate> statsDelegate, |
| 281 | std::optional<size_t> maxPulledLayers, |
| 282 | std::optional<size_t> maxPulledHistogramBuckets); |
Alec Mouri | 8e2f31b | 2020-01-16 22:04:35 +0000 | [diff] [blame] | 283 | |
Alec Mouri | 3ecd5cd | 2020-01-29 12:53:07 -0800 | [diff] [blame] | 284 | ~TimeStats() override; |
| 285 | |
Alec Mouri | 8e2f31b | 2020-01-16 22:04:35 +0000 | [diff] [blame] | 286 | void onBootFinished() override; |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 287 | void parseArgs(bool asProto, const Vector<String16>& args, std::string& result) override; |
| 288 | bool isEnabled() override; |
Yiwei Zhang | 7eb58b7 | 2019-04-22 19:00:02 -0700 | [diff] [blame] | 289 | std::string miniDump() override; |
Yiwei Zhang | 7e666a5 | 2018-11-15 13:33:42 -0800 | [diff] [blame] | 290 | |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 291 | void incrementTotalFrames() override; |
| 292 | void incrementMissedFrames() override; |
| 293 | void incrementClientCompositionFrames() override; |
Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 294 | void incrementClientCompositionReusedFrames() override; |
Alec Mouri | 8de697e | 2020-03-19 10:52:01 -0700 | [diff] [blame] | 295 | void incrementRefreshRateSwitches() override; |
Alec Mouri | 8f7a010 | 2020-04-15 12:11:10 -0700 | [diff] [blame] | 296 | void incrementCompositionStrategyChanges() override; |
Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 297 | void recordDisplayEventConnectionCount(int32_t count) override; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 298 | |
Alec Mouri | 9519bf1 | 2019-11-15 16:54:44 -0800 | [diff] [blame] | 299 | void recordFrameDuration(nsecs_t startTime, nsecs_t endTime) override; |
Alec Mouri | e4034bb | 2019-11-19 12:45:54 -0800 | [diff] [blame] | 300 | void recordRenderEngineDuration(nsecs_t startTime, nsecs_t endTime) override; |
| 301 | void recordRenderEngineDuration(nsecs_t startTime, |
| 302 | const std::shared_ptr<FenceTime>& readyFence) override; |
Alec Mouri | 9519bf1 | 2019-11-15 16:54:44 -0800 | [diff] [blame] | 303 | |
Alec Mouri | 9a29e67 | 2020-09-14 12:39:14 -0700 | [diff] [blame] | 304 | void setPostTime(int32_t layerId, uint64_t frameNumber, const std::string& layerName, uid_t uid, |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 305 | nsecs_t postTime) override; |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 306 | void setLatchTime(int32_t layerId, uint64_t frameNumber, nsecs_t latchTime) override; |
Alec Mouri | 91f6df3 | 2020-01-30 08:48:58 -0800 | [diff] [blame] | 307 | void incrementLatchSkipped(int32_t layerId, LatchSkipReason reason) override; |
| 308 | void incrementBadDesiredPresent(int32_t layerId) override; |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 309 | void setDesiredTime(int32_t layerId, uint64_t frameNumber, nsecs_t desiredTime) override; |
| 310 | void setAcquireTime(int32_t layerId, uint64_t frameNumber, nsecs_t acquireTime) override; |
| 311 | void setAcquireFence(int32_t layerId, uint64_t frameNumber, |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 312 | const std::shared_ptr<FenceTime>& acquireFence) override; |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 313 | void setPresentTime(int32_t layerId, uint64_t frameNumber, nsecs_t presentTime, |
Ady Abraham | 8b9e612 | 2021-01-26 19:11:45 -0800 | [diff] [blame] | 314 | Fps displayRefreshRate, std::optional<Fps> renderRate, |
| 315 | SetFrameRateVote frameRateVote) override; |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 316 | void setPresentFence(int32_t layerId, uint64_t frameNumber, |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 317 | const std::shared_ptr<FenceTime>& presentFence, Fps displayRefreshRate, |
Ady Abraham | 8b9e612 | 2021-01-26 19:11:45 -0800 | [diff] [blame] | 318 | std::optional<Fps> renderRate, SetFrameRateVote frameRateVote) override; |
Alec Mouri | 363faf0 | 2021-01-29 16:34:55 -0800 | [diff] [blame] | 319 | |
| 320 | void incrementJankyFrames(const JankyFramesInfo& info) override; |
Yiwei Zhang | af8ee94 | 2018-11-22 00:15:23 -0800 | [diff] [blame] | 321 | // Clean up the layer record |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 322 | void onDestroy(int32_t layerId) override; |
Yiwei Zhang | eaeea06 | 2018-06-28 14:46:51 -0700 | [diff] [blame] | 323 | // If SF skips or rejects a buffer, remove the corresponding TimeRecord. |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 324 | void removeTimeRecord(int32_t layerId, uint64_t frameNumber) override; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 325 | |
Peiyong Lin | 65248e0 | 2020-04-18 21:15:07 -0700 | [diff] [blame] | 326 | void setPowerMode( |
| 327 | hardware::graphics::composer::V2_4::IComposerClient::PowerMode powerMode) override; |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 328 | // Source of truth is RefrehRateStats. |
| 329 | void recordRefreshRate(uint32_t fps, nsecs_t duration) override; |
| 330 | void setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) override; |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 331 | |
Yiwei Zhang | af8ee94 | 2018-11-22 00:15:23 -0800 | [diff] [blame] | 332 | static const size_t MAX_NUM_TIME_RECORDS = 64; |
| 333 | |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 334 | private: |
Tej Singh | 2a457b6 | 2020-01-31 16:16:10 -0800 | [diff] [blame] | 335 | static AStatsManager_PullAtomCallbackReturn pullAtomCallback(int32_t atom_tag, |
| 336 | AStatsEventList* data, |
| 337 | void* cookie); |
| 338 | AStatsManager_PullAtomCallbackReturn populateGlobalAtom(AStatsEventList* data); |
| 339 | AStatsManager_PullAtomCallbackReturn populateLayerAtom(AStatsEventList* data); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 340 | bool recordReadyLocked(int32_t layerId, TimeRecord* timeRecord); |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 341 | void flushAvailableRecordsToStatsLocked(int32_t layerId, Fps displayRefreshRate, |
Ady Abraham | 8b9e612 | 2021-01-26 19:11:45 -0800 | [diff] [blame] | 342 | std::optional<Fps> renderRate, |
| 343 | SetFrameRateVote frameRateVote); |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 344 | void flushPowerTimeLocked(); |
Yiwei Zhang | ce6ebc0 | 2018-10-20 12:42:38 -0700 | [diff] [blame] | 345 | void flushAvailableGlobalRecordsToStatsLocked(); |
Alec Mouri | 9a29e67 | 2020-09-14 12:39:14 -0700 | [diff] [blame] | 346 | bool canAddNewAggregatedStats(uid_t uid, const std::string& layerName); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 347 | |
| 348 | void enable(); |
| 349 | void disable(); |
Alec Mouri | 8e2f31b | 2020-01-16 22:04:35 +0000 | [diff] [blame] | 350 | void clearAll(); |
| 351 | void clearGlobalLocked(); |
| 352 | void clearLayersLocked(); |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 353 | void dump(bool asProto, std::optional<uint32_t> maxLayers, std::string& result); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 354 | |
| 355 | std::atomic<bool> mEnabled = false; |
| 356 | std::mutex mMutex; |
Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 357 | TimeStatsHelper::TimeStatsGlobal mTimeStats; |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 358 | // Hashmap for LayerRecord with layerId as the hash key |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 359 | std::unordered_map<int32_t, LayerRecord> mTimeStatsTracker; |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 360 | PowerTime mPowerTime; |
Yiwei Zhang | ce6ebc0 | 2018-10-20 12:42:38 -0700 | [diff] [blame] | 361 | GlobalRecord mGlobalRecord; |
Yiwei Zhang | 7eb58b7 | 2019-04-22 19:00:02 -0700 | [diff] [blame] | 362 | |
| 363 | static const size_t MAX_NUM_LAYER_RECORDS = 200; |
Alec Mouri | 7d436ec | 2021-01-27 20:40:50 -0800 | [diff] [blame] | 364 | |
| 365 | static const size_t REFRESH_RATE_BUCKET_WIDTH = 30; |
| 366 | static const size_t RENDER_RATE_BUCKET_WIDTH = REFRESH_RATE_BUCKET_WIDTH; |
Yiwei Zhang | e926ab5 | 2019-08-14 15:16:00 -0700 | [diff] [blame] | 367 | static const size_t MAX_NUM_LAYER_STATS = 200; |
Alec Mouri | 52bf780 | 2021-01-20 08:20:50 -0800 | [diff] [blame] | 368 | static const size_t MAX_NUM_PULLED_LAYERS = MAX_NUM_LAYER_STATS; |
Alec Mouri | 8e2f31b | 2020-01-16 22:04:35 +0000 | [diff] [blame] | 369 | std::unique_ptr<StatsEventDelegate> mStatsDelegate = std::make_unique<StatsEventDelegate>(); |
Alec Mouri | 52bf780 | 2021-01-20 08:20:50 -0800 | [diff] [blame] | 370 | size_t mMaxPulledLayers = MAX_NUM_PULLED_LAYERS; |
Alec Mouri | 3738434 | 2020-01-02 17:23:37 -0800 | [diff] [blame] | 371 | size_t mMaxPulledHistogramBuckets = 6; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 372 | }; |
| 373 | |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 374 | } // namespace impl |
| 375 | |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 376 | } // namespace android |