blob: eb483532a61d562cf40161e782f890c05ae0ec43 [file] [log] [blame]
Yiwei Zhang0102ad22018-05-02 17:37:17 -07001/*
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 Pessa2e1608f2019-07-19 11:25:35 -070019#include <hardware/hwcomposer_defs.h>
Alec Mouri8e2f31b2020-01-16 22:04:35 +000020#include <stats_event.h>
21#include <stats_pull_atom_callback.h>
22#include <statslog.h>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070023#include <timestatsproto/TimeStatsHelper.h>
24#include <timestatsproto/TimeStatsProtoHeader.h>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070025#include <ui/FenceTime.h>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070026#include <utils/String16.h>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070027#include <utils/Vector.h>
28
Yiwei Zhangc5f2c452018-05-08 16:31:56 -070029#include <deque>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070030#include <mutex>
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070031#include <optional>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070032#include <unordered_map>
Alec Mourie4034bb2019-11-19 12:45:54 -080033#include <variant>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070034
35using namespace android::surfaceflinger;
36
37namespace android {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070038
39class TimeStats {
Alec Mourifb571ea2019-01-24 18:42:10 -080040public:
41 virtual ~TimeStats() = default;
42
Alec Mouri8e2f31b2020-01-16 22:04:35 +000043 // Called once boot has been finished to perform additional capabilities,
44 // e.g. registration to statsd.
45 virtual void onBootFinished() = 0;
46
Alec Mourifb571ea2019-01-24 18:42:10 -080047 virtual void parseArgs(bool asProto, const Vector<String16>& args, std::string& result) = 0;
48 virtual bool isEnabled() = 0;
Oliver Nguyenf3b7c9c2019-05-07 13:28:07 -070049 virtual std::string miniDump() = 0;
Alec Mourifb571ea2019-01-24 18:42:10 -080050
51 virtual void incrementTotalFrames() = 0;
52 virtual void incrementMissedFrames() = 0;
53 virtual void incrementClientCompositionFrames() = 0;
Vishnu Nair9b079a22020-01-21 14:36:08 -080054 virtual void incrementClientCompositionReusedFrames() = 0;
Alec Mouri8de697e2020-03-19 10:52:01 -070055 // Increments the number of times the display refresh rate changed.
56 virtual void incrementRefreshRateSwitches() = 0;
Alec Mouri8f7a0102020-04-15 12:11:10 -070057 // Increments the number of changes in composition strategy
58 // The intention is to reflect the number of changes between hwc and gpu
59 // composition, where "gpu composition" may also include mixed composition.
60 virtual void incrementCompositionStrategyChanges() = 0;
Alec Mouri717bcb62020-02-10 17:07:19 -080061 // Records the most up-to-date count of display event connections.
62 // The stored count will be the maximum ever recoded.
63 virtual void recordDisplayEventConnectionCount(int32_t count) = 0;
Alec Mourifb571ea2019-01-24 18:42:10 -080064
Alec Mouri9519bf12019-11-15 16:54:44 -080065 // Records the start and end times for a frame.
66 // The start time is the same as the beginning of a SurfaceFlinger
67 // invalidate message.
68 // The end time corresponds to when SurfaceFlinger finishes submitting the
69 // request to HWC to present a frame.
70 virtual void recordFrameDuration(nsecs_t startTime, nsecs_t endTime) = 0;
Alec Mourie4034bb2019-11-19 12:45:54 -080071 // Records the start time and end times for when RenderEngine begins work.
72 // The start time corresponds to the beginning of RenderEngine::drawLayers.
73 // The end time corresponds to when RenderEngine finishes rendering.
74 virtual void recordRenderEngineDuration(nsecs_t startTime, nsecs_t endTime) = 0;
75 // Same as above, but passes in a fence representing the end time.
76 virtual void recordRenderEngineDuration(nsecs_t startTime,
77 const std::shared_ptr<FenceTime>& readyFence) = 0;
Alec Mouri9519bf12019-11-15 16:54:44 -080078
Yiwei Zhang1a88c402019-11-18 10:43:58 -080079 virtual void setPostTime(int32_t layerId, uint64_t frameNumber, const std::string& layerName,
Alec Mourifb571ea2019-01-24 18:42:10 -080080 nsecs_t postTime) = 0;
Yiwei Zhang1a88c402019-11-18 10:43:58 -080081 virtual void setLatchTime(int32_t layerId, uint64_t frameNumber, nsecs_t latchTime) = 0;
Alec Mouri91f6df32020-01-30 08:48:58 -080082 // Reasons why latching a particular buffer may be skipped
83 enum class LatchSkipReason {
84 // If the acquire fence did not fire on some devices we skip latching
85 // the buffer until the fence fires.
86 LateAcquire,
87 };
88 // Increments the counter of skipped latch buffers.
89 virtual void incrementLatchSkipped(int32_t layerId, LatchSkipReason reason) = 0;
90 // Increments the counter of bad desired present times for this layer.
91 // Bad desired present times are "implausible" and cause SurfaceFlinger to
92 // latch a buffer immediately to avoid stalling.
93 virtual void incrementBadDesiredPresent(int32_t layerId) = 0;
Yiwei Zhang1a88c402019-11-18 10:43:58 -080094 virtual void setDesiredTime(int32_t layerId, uint64_t frameNumber, nsecs_t desiredTime) = 0;
95 virtual void setAcquireTime(int32_t layerId, uint64_t frameNumber, nsecs_t acquireTime) = 0;
96 virtual void setAcquireFence(int32_t layerId, uint64_t frameNumber,
Alec Mourifb571ea2019-01-24 18:42:10 -080097 const std::shared_ptr<FenceTime>& acquireFence) = 0;
Alec Mourie4034bb2019-11-19 12:45:54 -080098 // SetPresent{Time, Fence} are not expected to be called in the critical
99 // rendering path, as they flush prior fences if those fences have fired.
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800100 virtual void setPresentTime(int32_t layerId, uint64_t frameNumber, nsecs_t presentTime) = 0;
101 virtual void setPresentFence(int32_t layerId, uint64_t frameNumber,
Alec Mourifb571ea2019-01-24 18:42:10 -0800102 const std::shared_ptr<FenceTime>& presentFence) = 0;
103 // Clean up the layer record
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800104 virtual void onDestroy(int32_t layerId) = 0;
Alec Mourifb571ea2019-01-24 18:42:10 -0800105 // If SF skips or rejects a buffer, remove the corresponding TimeRecord.
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800106 virtual void removeTimeRecord(int32_t layerId, uint64_t frameNumber) = 0;
Alec Mourifb571ea2019-01-24 18:42:10 -0800107
108 virtual void setPowerMode(int32_t powerMode) = 0;
109 // Source of truth is RefrehRateStats.
110 virtual void recordRefreshRate(uint32_t fps, nsecs_t duration) = 0;
111 virtual void setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) = 0;
112};
113
114namespace impl {
115
116class TimeStats : public android::TimeStats {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700117 struct FrameTime {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700118 uint64_t frameNumber = 0;
119 nsecs_t postTime = 0;
120 nsecs_t latchTime = 0;
121 nsecs_t acquireTime = 0;
122 nsecs_t desiredTime = 0;
123 nsecs_t presentTime = 0;
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700124 };
125
126 struct TimeRecord {
127 bool ready = false;
128 FrameTime frameTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700129 std::shared_ptr<FenceTime> acquireFence;
130 std::shared_ptr<FenceTime> presentFence;
131 };
132
133 struct LayerRecord {
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700134 std::string layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700135 // This is the index in timeRecords, at which the timestamps for that
136 // specific frame are still not fully received. This is not waiting for
137 // fences to signal, but rather waiting to receive those fences/timestamps.
138 int32_t waitData = -1;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700139 uint32_t droppedFrames = 0;
Alec Mouri91f6df32020-01-30 08:48:58 -0800140 uint32_t lateAcquireFrames = 0;
141 uint32_t badDesiredPresentFrames = 0;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700142 TimeRecord prevTimeRecord;
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700143 std::deque<TimeRecord> timeRecords;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700144 };
145
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700146 struct PowerTime {
147 int32_t powerMode = HWC_POWER_MODE_OFF;
148 nsecs_t prevTime = 0;
149 };
150
Alec Mourie4034bb2019-11-19 12:45:54 -0800151 struct RenderEngineDuration {
152 nsecs_t startTime;
153 std::variant<nsecs_t, std::shared_ptr<FenceTime>> endTime;
154 };
155
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700156 struct GlobalRecord {
157 nsecs_t prevPresentTime = 0;
158 std::deque<std::shared_ptr<FenceTime>> presentFences;
Alec Mourie4034bb2019-11-19 12:45:54 -0800159 std::deque<RenderEngineDuration> renderEngineDurations;
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700160 };
161
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700162public:
Alec Mourib3885ad2019-09-06 17:08:55 -0700163 TimeStats();
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800164
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000165 // Delegate to the statsd service and associated APIs.
166 // Production code may use this class directly, whereas unit test may define
167 // a subclass for ease of testing.
168 class StatsEventDelegate {
169 public:
170 virtual ~StatsEventDelegate() = default;
Tej Singh2a457b62020-01-31 16:16:10 -0800171 virtual AStatsEvent* addStatsEventToPullData(AStatsEventList* data) {
172 return AStatsEventList_addStatsEvent(data);
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000173 }
Tej Singh38a4b212020-03-13 19:04:51 -0700174 virtual void setStatsPullAtomCallback(int32_t atom_tag,
175 AStatsManager_PullAtomMetadata* metadata,
176 AStatsManager_PullAtomCallback callback,
177 void* cookie) {
178 return AStatsManager_setPullAtomCallback(atom_tag, metadata, callback, cookie);
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000179 }
180
Tej Singh38a4b212020-03-13 19:04:51 -0700181 virtual void clearStatsPullAtomCallback(int32_t atom_tag) {
182 return AStatsManager_clearPullAtomCallback(atom_tag);
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000183 }
184
Tej Singh2a457b62020-01-31 16:16:10 -0800185 virtual void statsEventSetAtomId(AStatsEvent* event, uint32_t atom_id) {
186 return AStatsEvent_setAtomId(event, atom_id);
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000187 }
188
Alec Mouri717bcb62020-02-10 17:07:19 -0800189 virtual void statsEventWriteInt32(AStatsEvent* event, int32_t field) {
190 return AStatsEvent_writeInt32(event, field);
191 }
192
Tej Singh2a457b62020-01-31 16:16:10 -0800193 virtual void statsEventWriteInt64(AStatsEvent* event, int64_t field) {
194 return AStatsEvent_writeInt64(event, field);
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000195 }
196
Tej Singh2a457b62020-01-31 16:16:10 -0800197 virtual void statsEventWriteString8(AStatsEvent* event, const char* field) {
198 return AStatsEvent_writeString(event, field);
Alec Mouri37384342020-01-02 17:23:37 -0800199 }
200
Tej Singh2a457b62020-01-31 16:16:10 -0800201 virtual void statsEventWriteByteArray(AStatsEvent* event, const uint8_t* buf,
Alec Mouri37384342020-01-02 17:23:37 -0800202 size_t numBytes) {
Tej Singh2a457b62020-01-31 16:16:10 -0800203 return AStatsEvent_writeByteArray(event, buf, numBytes);
Alec Mouri37384342020-01-02 17:23:37 -0800204 }
205
Tej Singh2a457b62020-01-31 16:16:10 -0800206 virtual void statsEventBuild(AStatsEvent* event) { return AStatsEvent_build(event); }
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000207 };
208 // For testing only for injecting custom dependencies.
Alec Mouri37384342020-01-02 17:23:37 -0800209 TimeStats(std::unique_ptr<StatsEventDelegate> statsDelegate,
210 std::optional<size_t> maxPulledLayers,
211 std::optional<size_t> maxPulledHistogramBuckets);
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000212
Alec Mouri3ecd5cd2020-01-29 12:53:07 -0800213 ~TimeStats() override;
214
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000215 void onBootFinished() override;
Alec Mourifb571ea2019-01-24 18:42:10 -0800216 void parseArgs(bool asProto, const Vector<String16>& args, std::string& result) override;
217 bool isEnabled() override;
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700218 std::string miniDump() override;
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800219
Alec Mourifb571ea2019-01-24 18:42:10 -0800220 void incrementTotalFrames() override;
221 void incrementMissedFrames() override;
222 void incrementClientCompositionFrames() override;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800223 void incrementClientCompositionReusedFrames() override;
Alec Mouri8de697e2020-03-19 10:52:01 -0700224 void incrementRefreshRateSwitches() override;
Alec Mouri8f7a0102020-04-15 12:11:10 -0700225 void incrementCompositionStrategyChanges() override;
Alec Mouri717bcb62020-02-10 17:07:19 -0800226 void recordDisplayEventConnectionCount(int32_t count) override;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700227
Alec Mouri9519bf12019-11-15 16:54:44 -0800228 void recordFrameDuration(nsecs_t startTime, nsecs_t endTime) override;
Alec Mourie4034bb2019-11-19 12:45:54 -0800229 void recordRenderEngineDuration(nsecs_t startTime, nsecs_t endTime) override;
230 void recordRenderEngineDuration(nsecs_t startTime,
231 const std::shared_ptr<FenceTime>& readyFence) override;
Alec Mouri9519bf12019-11-15 16:54:44 -0800232
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800233 void setPostTime(int32_t layerId, uint64_t frameNumber, const std::string& layerName,
Alec Mourifb571ea2019-01-24 18:42:10 -0800234 nsecs_t postTime) override;
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800235 void setLatchTime(int32_t layerId, uint64_t frameNumber, nsecs_t latchTime) override;
Alec Mouri91f6df32020-01-30 08:48:58 -0800236 void incrementLatchSkipped(int32_t layerId, LatchSkipReason reason) override;
237 void incrementBadDesiredPresent(int32_t layerId) override;
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800238 void setDesiredTime(int32_t layerId, uint64_t frameNumber, nsecs_t desiredTime) override;
239 void setAcquireTime(int32_t layerId, uint64_t frameNumber, nsecs_t acquireTime) override;
240 void setAcquireFence(int32_t layerId, uint64_t frameNumber,
Alec Mourifb571ea2019-01-24 18:42:10 -0800241 const std::shared_ptr<FenceTime>& acquireFence) override;
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800242 void setPresentTime(int32_t layerId, uint64_t frameNumber, nsecs_t presentTime) override;
243 void setPresentFence(int32_t layerId, uint64_t frameNumber,
Alec Mourifb571ea2019-01-24 18:42:10 -0800244 const std::shared_ptr<FenceTime>& presentFence) override;
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800245 // Clean up the layer record
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800246 void onDestroy(int32_t layerId) override;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700247 // If SF skips or rejects a buffer, remove the corresponding TimeRecord.
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800248 void removeTimeRecord(int32_t layerId, uint64_t frameNumber) override;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700249
Alec Mourifb571ea2019-01-24 18:42:10 -0800250 void setPowerMode(int32_t powerMode) override;
251 // Source of truth is RefrehRateStats.
252 void recordRefreshRate(uint32_t fps, nsecs_t duration) override;
253 void setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) override;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700254
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800255 static const size_t MAX_NUM_TIME_RECORDS = 64;
256
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700257private:
Tej Singh2a457b62020-01-31 16:16:10 -0800258 static AStatsManager_PullAtomCallbackReturn pullAtomCallback(int32_t atom_tag,
259 AStatsEventList* data,
260 void* cookie);
261 AStatsManager_PullAtomCallbackReturn populateGlobalAtom(AStatsEventList* data);
262 AStatsManager_PullAtomCallbackReturn populateLayerAtom(AStatsEventList* data);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800263 bool recordReadyLocked(int32_t layerId, TimeRecord* timeRecord);
264 void flushAvailableRecordsToStatsLocked(int32_t layerId);
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700265 void flushPowerTimeLocked();
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700266 void flushAvailableGlobalRecordsToStatsLocked();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700267
268 void enable();
269 void disable();
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000270 void clearAll();
271 void clearGlobalLocked();
272 void clearLayersLocked();
Yiwei Zhang5434a782018-12-05 18:06:32 -0800273 void dump(bool asProto, std::optional<uint32_t> maxLayers, std::string& result);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700274
275 std::atomic<bool> mEnabled = false;
276 std::mutex mMutex;
Yiwei Zhangdc224042018-10-18 15:34:00 -0700277 TimeStatsHelper::TimeStatsGlobal mTimeStats;
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800278 // Hashmap for LayerRecord with layerId as the hash key
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700279 std::unordered_map<int32_t, LayerRecord> mTimeStatsTracker;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700280 PowerTime mPowerTime;
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700281 GlobalRecord mGlobalRecord;
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700282
283 static const size_t MAX_NUM_LAYER_RECORDS = 200;
Yiwei Zhange926ab52019-08-14 15:16:00 -0700284 static const size_t MAX_NUM_LAYER_STATS = 200;
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000285 std::unique_ptr<StatsEventDelegate> mStatsDelegate = std::make_unique<StatsEventDelegate>();
Alec Mouri37384342020-01-02 17:23:37 -0800286 size_t mMaxPulledLayers = 8;
287 size_t mMaxPulledHistogramBuckets = 6;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700288};
289
Alec Mourifb571ea2019-01-24 18:42:10 -0800290} // namespace impl
291
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700292} // namespace android