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> |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 20 | #include <timestatsproto/TimeStatsHelper.h> |
| 21 | #include <timestatsproto/TimeStatsProtoHeader.h> |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 22 | #include <ui/FenceTime.h> |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 23 | #include <utils/String16.h> |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 24 | #include <utils/Vector.h> |
| 25 | |
Yiwei Zhang | c5f2c45 | 2018-05-08 16:31:56 -0700 | [diff] [blame] | 26 | #include <deque> |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 27 | #include <mutex> |
Yiwei Zhang | 8a4015c | 2018-05-08 16:03:47 -0700 | [diff] [blame] | 28 | #include <optional> |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 29 | #include <unordered_map> |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 30 | |
| 31 | using namespace android::surfaceflinger; |
| 32 | |
| 33 | namespace android { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 34 | |
| 35 | class TimeStats { |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 36 | public: |
| 37 | virtual ~TimeStats() = default; |
| 38 | |
| 39 | virtual void parseArgs(bool asProto, const Vector<String16>& args, std::string& result) = 0; |
| 40 | virtual bool isEnabled() = 0; |
Oliver Nguyen | f3b7c9c | 2019-05-07 13:28:07 -0700 | [diff] [blame] | 41 | virtual std::string miniDump() = 0; |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 42 | |
| 43 | virtual void incrementTotalFrames() = 0; |
| 44 | virtual void incrementMissedFrames() = 0; |
| 45 | virtual void incrementClientCompositionFrames() = 0; |
| 46 | |
Alec Mouri | 9519bf1 | 2019-11-15 16:54:44 -0800 | [diff] [blame^] | 47 | // Records the start and end times for a frame. |
| 48 | // The start time is the same as the beginning of a SurfaceFlinger |
| 49 | // invalidate message. |
| 50 | // The end time corresponds to when SurfaceFlinger finishes submitting the |
| 51 | // request to HWC to present a frame. |
| 52 | virtual void recordFrameDuration(nsecs_t startTime, nsecs_t endTime) = 0; |
| 53 | |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 54 | 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] | 55 | nsecs_t postTime) = 0; |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 56 | virtual void setLatchTime(int32_t layerId, uint64_t frameNumber, nsecs_t latchTime) = 0; |
| 57 | virtual void setDesiredTime(int32_t layerId, uint64_t frameNumber, nsecs_t desiredTime) = 0; |
| 58 | virtual void setAcquireTime(int32_t layerId, uint64_t frameNumber, nsecs_t acquireTime) = 0; |
| 59 | virtual void setAcquireFence(int32_t layerId, uint64_t frameNumber, |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 60 | const std::shared_ptr<FenceTime>& acquireFence) = 0; |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 61 | virtual void setPresentTime(int32_t layerId, uint64_t frameNumber, nsecs_t presentTime) = 0; |
| 62 | virtual void setPresentFence(int32_t layerId, uint64_t frameNumber, |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 63 | const std::shared_ptr<FenceTime>& presentFence) = 0; |
| 64 | // Clean up the layer record |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 65 | virtual void onDestroy(int32_t layerId) = 0; |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 66 | // If SF skips or rejects a buffer, remove the corresponding TimeRecord. |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 67 | virtual void removeTimeRecord(int32_t layerId, uint64_t frameNumber) = 0; |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 68 | |
| 69 | virtual void setPowerMode(int32_t powerMode) = 0; |
| 70 | // Source of truth is RefrehRateStats. |
| 71 | virtual void recordRefreshRate(uint32_t fps, nsecs_t duration) = 0; |
| 72 | virtual void setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) = 0; |
| 73 | }; |
| 74 | |
| 75 | namespace impl { |
| 76 | |
| 77 | class TimeStats : public android::TimeStats { |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 78 | struct FrameTime { |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 79 | uint64_t frameNumber = 0; |
| 80 | nsecs_t postTime = 0; |
| 81 | nsecs_t latchTime = 0; |
| 82 | nsecs_t acquireTime = 0; |
| 83 | nsecs_t desiredTime = 0; |
| 84 | nsecs_t presentTime = 0; |
Yiwei Zhang | cf50ab9 | 2018-06-14 10:50:12 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | struct TimeRecord { |
| 88 | bool ready = false; |
| 89 | FrameTime frameTime; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 90 | std::shared_ptr<FenceTime> acquireFence; |
| 91 | std::shared_ptr<FenceTime> presentFence; |
| 92 | }; |
| 93 | |
| 94 | struct LayerRecord { |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 95 | std::string layerName; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 96 | // This is the index in timeRecords, at which the timestamps for that |
| 97 | // specific frame are still not fully received. This is not waiting for |
| 98 | // fences to signal, but rather waiting to receive those fences/timestamps. |
| 99 | int32_t waitData = -1; |
Yiwei Zhang | eaeea06 | 2018-06-28 14:46:51 -0700 | [diff] [blame] | 100 | uint32_t droppedFrames = 0; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 101 | TimeRecord prevTimeRecord; |
Yiwei Zhang | c5f2c45 | 2018-05-08 16:31:56 -0700 | [diff] [blame] | 102 | std::deque<TimeRecord> timeRecords; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 103 | }; |
| 104 | |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 105 | struct PowerTime { |
| 106 | int32_t powerMode = HWC_POWER_MODE_OFF; |
| 107 | nsecs_t prevTime = 0; |
| 108 | }; |
| 109 | |
Yiwei Zhang | ce6ebc0 | 2018-10-20 12:42:38 -0700 | [diff] [blame] | 110 | struct GlobalRecord { |
| 111 | nsecs_t prevPresentTime = 0; |
| 112 | std::deque<std::shared_ptr<FenceTime>> presentFences; |
| 113 | }; |
| 114 | |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 115 | public: |
Alec Mouri | b3885ad | 2019-09-06 17:08:55 -0700 | [diff] [blame] | 116 | TimeStats(); |
Yiwei Zhang | 7e666a5 | 2018-11-15 13:33:42 -0800 | [diff] [blame] | 117 | |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 118 | void parseArgs(bool asProto, const Vector<String16>& args, std::string& result) override; |
| 119 | bool isEnabled() override; |
Yiwei Zhang | 7eb58b7 | 2019-04-22 19:00:02 -0700 | [diff] [blame] | 120 | std::string miniDump() override; |
Yiwei Zhang | 7e666a5 | 2018-11-15 13:33:42 -0800 | [diff] [blame] | 121 | |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 122 | void incrementTotalFrames() override; |
| 123 | void incrementMissedFrames() override; |
| 124 | void incrementClientCompositionFrames() override; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 125 | |
Alec Mouri | 9519bf1 | 2019-11-15 16:54:44 -0800 | [diff] [blame^] | 126 | void recordFrameDuration(nsecs_t startTime, nsecs_t endTime) override; |
| 127 | |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 128 | void setPostTime(int32_t layerId, uint64_t frameNumber, const std::string& layerName, |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 129 | nsecs_t postTime) override; |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 130 | void setLatchTime(int32_t layerId, uint64_t frameNumber, nsecs_t latchTime) override; |
| 131 | void setDesiredTime(int32_t layerId, uint64_t frameNumber, nsecs_t desiredTime) override; |
| 132 | void setAcquireTime(int32_t layerId, uint64_t frameNumber, nsecs_t acquireTime) override; |
| 133 | void setAcquireFence(int32_t layerId, uint64_t frameNumber, |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 134 | const std::shared_ptr<FenceTime>& acquireFence) override; |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 135 | void setPresentTime(int32_t layerId, uint64_t frameNumber, nsecs_t presentTime) override; |
| 136 | void setPresentFence(int32_t layerId, uint64_t frameNumber, |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 137 | const std::shared_ptr<FenceTime>& presentFence) override; |
Yiwei Zhang | af8ee94 | 2018-11-22 00:15:23 -0800 | [diff] [blame] | 138 | // Clean up the layer record |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 139 | void onDestroy(int32_t layerId) override; |
Yiwei Zhang | eaeea06 | 2018-06-28 14:46:51 -0700 | [diff] [blame] | 140 | // If SF skips or rejects a buffer, remove the corresponding TimeRecord. |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 141 | void removeTimeRecord(int32_t layerId, uint64_t frameNumber) override; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 142 | |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 143 | void setPowerMode(int32_t powerMode) override; |
| 144 | // Source of truth is RefrehRateStats. |
| 145 | void recordRefreshRate(uint32_t fps, nsecs_t duration) override; |
| 146 | void setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) override; |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 147 | |
Yiwei Zhang | af8ee94 | 2018-11-22 00:15:23 -0800 | [diff] [blame] | 148 | static const size_t MAX_NUM_TIME_RECORDS = 64; |
| 149 | |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 150 | private: |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 151 | bool recordReadyLocked(int32_t layerId, TimeRecord* timeRecord); |
| 152 | void flushAvailableRecordsToStatsLocked(int32_t layerId); |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 153 | void flushPowerTimeLocked(); |
Yiwei Zhang | ce6ebc0 | 2018-10-20 12:42:38 -0700 | [diff] [blame] | 154 | void flushAvailableGlobalRecordsToStatsLocked(); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 155 | |
| 156 | void enable(); |
| 157 | void disable(); |
| 158 | void clear(); |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 159 | void dump(bool asProto, std::optional<uint32_t> maxLayers, std::string& result); |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 160 | |
| 161 | std::atomic<bool> mEnabled = false; |
| 162 | std::mutex mMutex; |
Yiwei Zhang | dc22404 | 2018-10-18 15:34:00 -0700 | [diff] [blame] | 163 | TimeStatsHelper::TimeStatsGlobal mTimeStats; |
Yiwei Zhang | 1a88c40 | 2019-11-18 10:43:58 -0800 | [diff] [blame] | 164 | // Hashmap for LayerRecord with layerId as the hash key |
Yiwei Zhang | 9689e2f | 2018-05-11 12:33:23 -0700 | [diff] [blame] | 165 | std::unordered_map<int32_t, LayerRecord> mTimeStatsTracker; |
Yiwei Zhang | 3a226d2 | 2018-10-16 09:23:03 -0700 | [diff] [blame] | 166 | PowerTime mPowerTime; |
Yiwei Zhang | ce6ebc0 | 2018-10-20 12:42:38 -0700 | [diff] [blame] | 167 | GlobalRecord mGlobalRecord; |
Yiwei Zhang | 7eb58b7 | 2019-04-22 19:00:02 -0700 | [diff] [blame] | 168 | |
| 169 | static const size_t MAX_NUM_LAYER_RECORDS = 200; |
Yiwei Zhang | e926ab5 | 2019-08-14 15:16:00 -0700 | [diff] [blame] | 170 | static const size_t MAX_NUM_LAYER_STATS = 200; |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 171 | }; |
| 172 | |
Alec Mouri | fb571ea | 2019-01-24 18:42:10 -0800 | [diff] [blame] | 173 | } // namespace impl |
| 174 | |
Yiwei Zhang | 0102ad2 | 2018-05-02 17:37:17 -0700 | [diff] [blame] | 175 | } // namespace android |