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 | |
Mikael Pessa | 2e1608f | 2019-07-19 11:25:35 -0700 | [diff] [blame] | 19 | #include <hardware/hwcomposer_defs.h> |
Alec Mouri | 8e2f31b | 2020-01-16 22:04:35 +0000 | [diff] [blame] | 20 | #include <stats_event.h> |
| 21 | #include <stats_pull_atom_callback.h> |
| 22 | #include <statslog.h> |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 23 | #include <timestatsproto/TimeStatsHelper.h> |
| 24 | #include <timestatsproto/TimeStatsProtoHeader.h> |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 25 | #include <ui/FenceTime.h> |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 26 | #include <utils/String16.h> |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 27 | #include <utils/Vector.h> |
| 28 | |
Yiwei Zhang | c5f2c45 | 2018-05-08 16:31:56 -0700 | [diff] [blame] | 29 | #include <deque> |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 30 | #include <mutex> |
Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 31 | #include <optional> |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 32 | #include <unordered_map> |
Alec Mouri | e4034bb | 2019-11-19 12:45:54 -0800 | [diff] [blame] | 33 | #include <variant> |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 34 | |
| 35 | using namespace android::surfaceflinger; |
| 36 | |
| 37 | namespace android { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 38 | |
| 39 | class TimeStats { |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 40 | public: |
| 41 | virtual ~TimeStats() = default; |
| 42 | |
Alec Mouri | 8e2f31b | 2020-01-16 22:04:35 +0000 | [diff] [blame] | 43 | // Called once boot has been finished to perform additional capabilities, |
| 44 | // e.g. registration to statsd. |
| 45 | virtual void onBootFinished() = 0; |
| 46 | |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 47 | virtual void parseArgs(bool asProto, const Vector<String16>& args, std::string& result) = 0; |
| 48 | virtual bool isEnabled() = 0; |
Oliver Nguyen | f3b7c9c | 2019-05-07 13:28:07 -0700 | [diff] [blame] | 49 | virtual std::string miniDump() = 0; |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 50 | |
| 51 | virtual void incrementTotalFrames() = 0; |
| 52 | virtual void incrementMissedFrames() = 0; |
| 53 | virtual void incrementClientCompositionFrames() = 0; |
Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame^] | 54 | virtual void incrementClientCompositionReusedFrames() = 0; |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 55 | |
Alec Mouri | 9519bf1 | 2019-11-15 16:54:44 -0800 | [diff] [blame] | 56 | // Records the start and end times for a frame. |
| 57 | // The start time is the same as the beginning of a SurfaceFlinger |
| 58 | // invalidate message. |
| 59 | // The end time corresponds to when SurfaceFlinger finishes submitting the |
| 60 | // request to HWC to present a frame. |
| 61 | virtual void recordFrameDuration(nsecs_t startTime, nsecs_t endTime) = 0; |
Alec Mouri | e4034bb | 2019-11-19 12:45:54 -0800 | [diff] [blame] | 62 | // Records the start time and end times for when RenderEngine begins work. |
| 63 | // The start time corresponds to the beginning of RenderEngine::drawLayers. |
| 64 | // The end time corresponds to when RenderEngine finishes rendering. |
| 65 | virtual void recordRenderEngineDuration(nsecs_t startTime, nsecs_t endTime) = 0; |
| 66 | // Same as above, but passes in a fence representing the end time. |
| 67 | virtual void recordRenderEngineDuration(nsecs_t startTime, |
| 68 | const std::shared_ptr<FenceTime>& readyFence) = 0; |
Alec Mouri | 9519bf1 | 2019-11-15 16:54:44 -0800 | [diff] [blame] | 69 | |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 70 | virtual void setPostTime(int32_t layerId, uint64_t frameNumber, const std::string& layerName, |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 71 | nsecs_t postTime) = 0; |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 72 | virtual void setLatchTime(int32_t layerId, uint64_t frameNumber, nsecs_t latchTime) = 0; |
| 73 | virtual void setDesiredTime(int32_t layerId, uint64_t frameNumber, nsecs_t desiredTime) = 0; |
| 74 | virtual void setAcquireTime(int32_t layerId, uint64_t frameNumber, nsecs_t acquireTime) = 0; |
| 75 | virtual void setAcquireFence(int32_t layerId, uint64_t frameNumber, |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 76 | const std::shared_ptr<FenceTime>& acquireFence) = 0; |
Alec Mouri | e4034bb | 2019-11-19 12:45:54 -0800 | [diff] [blame] | 77 | // SetPresent{Time, Fence} are not expected to be called in the critical |
| 78 | // rendering path, as they flush prior fences if those fences have fired. |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 79 | virtual void setPresentTime(int32_t layerId, uint64_t frameNumber, nsecs_t presentTime) = 0; |
| 80 | virtual void setPresentFence(int32_t layerId, uint64_t frameNumber, |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 81 | const std::shared_ptr<FenceTime>& presentFence) = 0; |
| 82 | // Clean up the layer record |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 83 | virtual void onDestroy(int32_t layerId) = 0; |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 84 | // If SF skips or rejects a buffer, remove the corresponding TimeRecord. |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 85 | virtual void removeTimeRecord(int32_t layerId, uint64_t frameNumber) = 0; |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 86 | |
| 87 | virtual void setPowerMode(int32_t powerMode) = 0; |
| 88 | // Source of truth is RefrehRateStats. |
| 89 | virtual void recordRefreshRate(uint32_t fps, nsecs_t duration) = 0; |
| 90 | virtual void setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) = 0; |
| 91 | }; |
| 92 | |
| 93 | namespace impl { |
| 94 | |
| 95 | class TimeStats : public android::TimeStats { |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 96 | struct FrameTime { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 97 | uint64_t frameNumber = 0; |
| 98 | nsecs_t postTime = 0; |
| 99 | nsecs_t latchTime = 0; |
| 100 | nsecs_t acquireTime = 0; |
| 101 | nsecs_t desiredTime = 0; |
| 102 | nsecs_t presentTime = 0; |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | struct TimeRecord { |
| 106 | bool ready = false; |
| 107 | FrameTime frameTime; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 108 | std::shared_ptr<FenceTime> acquireFence; |
| 109 | std::shared_ptr<FenceTime> presentFence; |
| 110 | }; |
| 111 | |
| 112 | struct LayerRecord { |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 113 | std::string layerName; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 114 | // This is the index in timeRecords, at which the timestamps for that |
| 115 | // specific frame are still not fully received. This is not waiting for |
| 116 | // fences to signal, but rather waiting to receive those fences/timestamps. |
| 117 | int32_t waitData = -1; |
Yiwei Zhang | eaeea06 | 2018-06-28 14:46:51 -0700 | [diff] [blame] | 118 | uint32_t droppedFrames = 0; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 119 | TimeRecord prevTimeRecord; |
Yiwei Zhang | c5f2c45 | 2018-05-08 16:31:56 -0700 | [diff] [blame] | 120 | std::deque<TimeRecord> timeRecords; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 121 | }; |
| 122 | |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 123 | struct PowerTime { |
| 124 | int32_t powerMode = HWC_POWER_MODE_OFF; |
| 125 | nsecs_t prevTime = 0; |
| 126 | }; |
| 127 | |
Alec Mouri | e4034bb | 2019-11-19 12:45:54 -0800 | [diff] [blame] | 128 | struct RenderEngineDuration { |
| 129 | nsecs_t startTime; |
| 130 | std::variant<nsecs_t, std::shared_ptr<FenceTime>> endTime; |
| 131 | }; |
| 132 | |
Yiwei Zhang | ce6ebc0 | 2018-10-20 12:42:38 -0700 | [diff] [blame] | 133 | struct GlobalRecord { |
| 134 | nsecs_t prevPresentTime = 0; |
| 135 | std::deque<std::shared_ptr<FenceTime>> presentFences; |
Alec Mouri | e4034bb | 2019-11-19 12:45:54 -0800 | [diff] [blame] | 136 | std::deque<RenderEngineDuration> renderEngineDurations; |
Yiwei Zhang | ce6ebc0 | 2018-10-20 12:42:38 -0700 | [diff] [blame] | 137 | }; |
| 138 | |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 139 | public: |
Alec Mouri | b3885ad | 2019-09-06 17:08:55 -0700 | [diff] [blame] | 140 | TimeStats(); |
Yiwei Zhang | 7e666a5 | 2018-11-15 13:33:42 -0800 | [diff] [blame] | 141 | |
Alec Mouri | 8e2f31b | 2020-01-16 22:04:35 +0000 | [diff] [blame] | 142 | // Delegate to the statsd service and associated APIs. |
| 143 | // Production code may use this class directly, whereas unit test may define |
| 144 | // a subclass for ease of testing. |
| 145 | class StatsEventDelegate { |
| 146 | public: |
| 147 | virtual ~StatsEventDelegate() = default; |
| 148 | virtual struct stats_event* addStatsEventToPullData(pulled_stats_event_list* data) { |
| 149 | return add_stats_event_to_pull_data(data); |
| 150 | } |
| 151 | virtual void registerStatsPullAtomCallback(int32_t atom_tag, |
| 152 | stats_pull_atom_callback_t callback, |
| 153 | pull_atom_metadata* metadata, void* cookie) { |
| 154 | return register_stats_pull_atom_callback(atom_tag, callback, metadata, cookie); |
| 155 | } |
| 156 | |
| 157 | virtual void unregisterStatsPullAtomCallback(int32_t atom_tag) { |
| 158 | return unregister_stats_pull_atom_callback(atom_tag); |
| 159 | } |
| 160 | |
| 161 | virtual void statsEventSetAtomId(struct stats_event* event, uint32_t atom_id) { |
| 162 | return stats_event_set_atom_id(event, atom_id); |
| 163 | } |
| 164 | |
| 165 | virtual void statsEventWriteInt64(struct stats_event* event, int64_t field) { |
| 166 | return stats_event_write_int64(event, field); |
| 167 | } |
| 168 | |
| 169 | virtual void statsEventBuild(struct stats_event* event) { return stats_event_build(event); } |
| 170 | }; |
| 171 | // For testing only for injecting custom dependencies. |
| 172 | TimeStats(std::unique_ptr<StatsEventDelegate> statsDelegate); |
| 173 | |
| 174 | void onBootFinished() override; |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 175 | void parseArgs(bool asProto, const Vector<String16>& args, std::string& result) override; |
| 176 | bool isEnabled() override; |
Yiwei Zhang | 7eb58b7 | 2019-04-22 19:00:02 -0700 | [diff] [blame] | 177 | std::string miniDump() override; |
Yiwei Zhang | 7e666a5 | 2018-11-15 13:33:42 -0800 | [diff] [blame] | 178 | |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 179 | void incrementTotalFrames() override; |
| 180 | void incrementMissedFrames() override; |
| 181 | void incrementClientCompositionFrames() override; |
Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame^] | 182 | void incrementClientCompositionReusedFrames() override; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 183 | |
Alec Mouri | 9519bf1 | 2019-11-15 16:54:44 -0800 | [diff] [blame] | 184 | void recordFrameDuration(nsecs_t startTime, nsecs_t endTime) override; |
Alec Mouri | e4034bb | 2019-11-19 12:45:54 -0800 | [diff] [blame] | 185 | void recordRenderEngineDuration(nsecs_t startTime, nsecs_t endTime) override; |
| 186 | void recordRenderEngineDuration(nsecs_t startTime, |
| 187 | const std::shared_ptr<FenceTime>& readyFence) override; |
Alec Mouri | 9519bf1 | 2019-11-15 16:54:44 -0800 | [diff] [blame] | 188 | |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 189 | void setPostTime(int32_t layerId, uint64_t frameNumber, const std::string& layerName, |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 190 | nsecs_t postTime) override; |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 191 | void setLatchTime(int32_t layerId, uint64_t frameNumber, nsecs_t latchTime) override; |
| 192 | void setDesiredTime(int32_t layerId, uint64_t frameNumber, nsecs_t desiredTime) override; |
| 193 | void setAcquireTime(int32_t layerId, uint64_t frameNumber, nsecs_t acquireTime) override; |
| 194 | void setAcquireFence(int32_t layerId, uint64_t frameNumber, |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 195 | const std::shared_ptr<FenceTime>& acquireFence) override; |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 196 | void setPresentTime(int32_t layerId, uint64_t frameNumber, nsecs_t presentTime) override; |
| 197 | void setPresentFence(int32_t layerId, uint64_t frameNumber, |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 198 | const std::shared_ptr<FenceTime>& presentFence) override; |
Yiwei Zhang | af8ee94 | 2018-11-22 00:15:23 -0800 | [diff] [blame] | 199 | // Clean up the layer record |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 200 | void onDestroy(int32_t layerId) override; |
Yiwei Zhang | eaeea06 | 2018-06-28 14:46:51 -0700 | [diff] [blame] | 201 | // If SF skips or rejects a buffer, remove the corresponding TimeRecord. |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 202 | void removeTimeRecord(int32_t layerId, uint64_t frameNumber) override; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 203 | |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 204 | void setPowerMode(int32_t powerMode) override; |
| 205 | // Source of truth is RefrehRateStats. |
| 206 | void recordRefreshRate(uint32_t fps, nsecs_t duration) override; |
| 207 | void setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) override; |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 208 | |
Yiwei Zhang | af8ee94 | 2018-11-22 00:15:23 -0800 | [diff] [blame] | 209 | static const size_t MAX_NUM_TIME_RECORDS = 64; |
| 210 | |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 211 | private: |
Alec Mouri | 8e2f31b | 2020-01-16 22:04:35 +0000 | [diff] [blame] | 212 | static status_pull_atom_return_t pullGlobalAtomCallback(int32_t atom_tag, |
| 213 | pulled_stats_event_list* data, |
| 214 | void* cookie); |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 215 | bool recordReadyLocked(int32_t layerId, TimeRecord* timeRecord); |
| 216 | void flushAvailableRecordsToStatsLocked(int32_t layerId); |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 217 | void flushPowerTimeLocked(); |
Yiwei Zhang | ce6ebc0 | 2018-10-20 12:42:38 -0700 | [diff] [blame] | 218 | void flushAvailableGlobalRecordsToStatsLocked(); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 219 | |
| 220 | void enable(); |
| 221 | void disable(); |
Alec Mouri | 8e2f31b | 2020-01-16 22:04:35 +0000 | [diff] [blame] | 222 | void clearAll(); |
| 223 | void clearGlobalLocked(); |
| 224 | void clearLayersLocked(); |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 225 | void dump(bool asProto, std::optional<uint32_t> maxLayers, std::string& result); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 226 | |
| 227 | std::atomic<bool> mEnabled = false; |
| 228 | std::mutex mMutex; |
Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 229 | TimeStatsHelper::TimeStatsGlobal mTimeStats; |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 230 | // Hashmap for LayerRecord with layerId as the hash key |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 231 | std::unordered_map<int32_t, LayerRecord> mTimeStatsTracker; |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 232 | PowerTime mPowerTime; |
Yiwei Zhang | ce6ebc0 | 2018-10-20 12:42:38 -0700 | [diff] [blame] | 233 | GlobalRecord mGlobalRecord; |
Yiwei Zhang | 7eb58b7 | 2019-04-22 19:00:02 -0700 | [diff] [blame] | 234 | |
| 235 | static const size_t MAX_NUM_LAYER_RECORDS = 200; |
Yiwei Zhang | e926ab5 | 2019-08-14 15:16:00 -0700 | [diff] [blame] | 236 | static const size_t MAX_NUM_LAYER_STATS = 200; |
Alec Mouri | 8e2f31b | 2020-01-16 22:04:35 +0000 | [diff] [blame] | 237 | std::unique_ptr<StatsEventDelegate> mStatsDelegate = std::make_unique<StatsEventDelegate>(); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 238 | }; |
| 239 | |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 240 | } // namespace impl |
| 241 | |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 242 | } // namespace android |