blob: 8de5d0c3e8a6410dc8de671c1185a1e466c7131e [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
Peiyong Lin65248e02020-04-18 21:15:07 -070019// TODO(b/129481165): remove the #pragma below and fix conversion issues
20#pragma clang diagnostic push
21#pragma clang diagnostic ignored "-Wconversion"
22
23#include <android/hardware/graphics/composer/2.4/IComposerClient.h>
24
25// TODO(b/129481165): remove the #pragma below and fix conversion issues
26#pragma clang diagnostic pop // ignored "-Wconversion"
27
Alec Mouri8e2f31b2020-01-16 22:04:35 +000028#include <stats_event.h>
29#include <stats_pull_atom_callback.h>
30#include <statslog.h>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070031#include <timestatsproto/TimeStatsHelper.h>
32#include <timestatsproto/TimeStatsProtoHeader.h>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070033#include <ui/FenceTime.h>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070034#include <utils/String16.h>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070035#include <utils/Vector.h>
36
Yiwei Zhangc5f2c452018-05-08 16:31:56 -070037#include <deque>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070038#include <mutex>
Yiwei Zhang8a4015c2018-05-08 16:03:47 -070039#include <optional>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070040#include <unordered_map>
Alec Mourie4034bb2019-11-19 12:45:54 -080041#include <variant>
Yiwei Zhang0102ad22018-05-02 17:37:17 -070042
43using namespace android::surfaceflinger;
44
45namespace android {
Yiwei Zhang0102ad22018-05-02 17:37:17 -070046
47class TimeStats {
Alec Mourifb571ea2019-01-24 18:42:10 -080048public:
49 virtual ~TimeStats() = default;
50
Alec Mouri8e2f31b2020-01-16 22:04:35 +000051 // Called once boot has been finished to perform additional capabilities,
52 // e.g. registration to statsd.
53 virtual void onBootFinished() = 0;
54
Alec Mourifb571ea2019-01-24 18:42:10 -080055 virtual void parseArgs(bool asProto, const Vector<String16>& args, std::string& result) = 0;
56 virtual bool isEnabled() = 0;
Oliver Nguyenf3b7c9c2019-05-07 13:28:07 -070057 virtual std::string miniDump() = 0;
Alec Mourifb571ea2019-01-24 18:42:10 -080058
59 virtual void incrementTotalFrames() = 0;
60 virtual void incrementMissedFrames() = 0;
61 virtual void incrementClientCompositionFrames() = 0;
Vishnu Nair9b079a22020-01-21 14:36:08 -080062 virtual void incrementClientCompositionReusedFrames() = 0;
Alec Mouri8de697e2020-03-19 10:52:01 -070063 // Increments the number of times the display refresh rate changed.
64 virtual void incrementRefreshRateSwitches() = 0;
Alec Mouri8f7a0102020-04-15 12:11:10 -070065 // Increments the number of changes in composition strategy
66 // The intention is to reflect the number of changes between hwc and gpu
67 // composition, where "gpu composition" may also include mixed composition.
68 virtual void incrementCompositionStrategyChanges() = 0;
Alec Mouri717bcb62020-02-10 17:07:19 -080069 // Records the most up-to-date count of display event connections.
70 // The stored count will be the maximum ever recoded.
71 virtual void recordDisplayEventConnectionCount(int32_t count) = 0;
Alec Mourifb571ea2019-01-24 18:42:10 -080072
Alec Mouri9519bf12019-11-15 16:54:44 -080073 // Records the start and end times for a frame.
74 // The start time is the same as the beginning of a SurfaceFlinger
75 // invalidate message.
76 // The end time corresponds to when SurfaceFlinger finishes submitting the
77 // request to HWC to present a frame.
78 virtual void recordFrameDuration(nsecs_t startTime, nsecs_t endTime) = 0;
Alec Mourie4034bb2019-11-19 12:45:54 -080079 // Records the start time and end times for when RenderEngine begins work.
80 // The start time corresponds to the beginning of RenderEngine::drawLayers.
81 // The end time corresponds to when RenderEngine finishes rendering.
82 virtual void recordRenderEngineDuration(nsecs_t startTime, nsecs_t endTime) = 0;
83 // Same as above, but passes in a fence representing the end time.
84 virtual void recordRenderEngineDuration(nsecs_t startTime,
85 const std::shared_ptr<FenceTime>& readyFence) = 0;
Alec Mouri9519bf12019-11-15 16:54:44 -080086
Yiwei Zhang1a88c402019-11-18 10:43:58 -080087 virtual void setPostTime(int32_t layerId, uint64_t frameNumber, const std::string& layerName,
Alec Mourifb571ea2019-01-24 18:42:10 -080088 nsecs_t postTime) = 0;
Yiwei Zhang1a88c402019-11-18 10:43:58 -080089 virtual void setLatchTime(int32_t layerId, uint64_t frameNumber, nsecs_t latchTime) = 0;
Alec Mouri91f6df32020-01-30 08:48:58 -080090 // Reasons why latching a particular buffer may be skipped
91 enum class LatchSkipReason {
92 // If the acquire fence did not fire on some devices we skip latching
93 // the buffer until the fence fires.
94 LateAcquire,
95 };
96 // Increments the counter of skipped latch buffers.
97 virtual void incrementLatchSkipped(int32_t layerId, LatchSkipReason reason) = 0;
98 // Increments the counter of bad desired present times for this layer.
99 // Bad desired present times are "implausible" and cause SurfaceFlinger to
100 // latch a buffer immediately to avoid stalling.
101 virtual void incrementBadDesiredPresent(int32_t layerId) = 0;
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800102 virtual void setDesiredTime(int32_t layerId, uint64_t frameNumber, nsecs_t desiredTime) = 0;
103 virtual void setAcquireTime(int32_t layerId, uint64_t frameNumber, nsecs_t acquireTime) = 0;
104 virtual void setAcquireFence(int32_t layerId, uint64_t frameNumber,
Alec Mourifb571ea2019-01-24 18:42:10 -0800105 const std::shared_ptr<FenceTime>& acquireFence) = 0;
Alec Mourie4034bb2019-11-19 12:45:54 -0800106 // SetPresent{Time, Fence} are not expected to be called in the critical
107 // rendering path, as they flush prior fences if those fences have fired.
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800108 virtual void setPresentTime(int32_t layerId, uint64_t frameNumber, nsecs_t presentTime) = 0;
109 virtual void setPresentFence(int32_t layerId, uint64_t frameNumber,
Alec Mourifb571ea2019-01-24 18:42:10 -0800110 const std::shared_ptr<FenceTime>& presentFence) = 0;
111 // Clean up the layer record
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800112 virtual void onDestroy(int32_t layerId) = 0;
Alec Mourifb571ea2019-01-24 18:42:10 -0800113 // If SF skips or rejects a buffer, remove the corresponding TimeRecord.
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800114 virtual void removeTimeRecord(int32_t layerId, uint64_t frameNumber) = 0;
Alec Mourifb571ea2019-01-24 18:42:10 -0800115
Peiyong Lin65248e02020-04-18 21:15:07 -0700116 virtual void setPowerMode(
117 hardware::graphics::composer::V2_4::IComposerClient::PowerMode powerMode) = 0;
Alec Mourifb571ea2019-01-24 18:42:10 -0800118 // Source of truth is RefrehRateStats.
119 virtual void recordRefreshRate(uint32_t fps, nsecs_t duration) = 0;
120 virtual void setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) = 0;
121};
122
123namespace impl {
124
125class TimeStats : public android::TimeStats {
Peiyong Lin65248e02020-04-18 21:15:07 -0700126 using PowerMode = android::hardware::graphics::composer::V2_4::IComposerClient::PowerMode;
127
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700128 struct FrameTime {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700129 uint64_t frameNumber = 0;
130 nsecs_t postTime = 0;
131 nsecs_t latchTime = 0;
132 nsecs_t acquireTime = 0;
133 nsecs_t desiredTime = 0;
134 nsecs_t presentTime = 0;
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700135 };
136
137 struct TimeRecord {
138 bool ready = false;
139 FrameTime frameTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700140 std::shared_ptr<FenceTime> acquireFence;
141 std::shared_ptr<FenceTime> presentFence;
142 };
143
144 struct LayerRecord {
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700145 std::string layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700146 // This is the index in timeRecords, at which the timestamps for that
147 // specific frame are still not fully received. This is not waiting for
148 // fences to signal, but rather waiting to receive those fences/timestamps.
149 int32_t waitData = -1;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700150 uint32_t droppedFrames = 0;
Alec Mouri91f6df32020-01-30 08:48:58 -0800151 uint32_t lateAcquireFrames = 0;
152 uint32_t badDesiredPresentFrames = 0;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700153 TimeRecord prevTimeRecord;
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700154 std::deque<TimeRecord> timeRecords;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700155 };
156
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700157 struct PowerTime {
Peiyong Lin65248e02020-04-18 21:15:07 -0700158 PowerMode powerMode = PowerMode::OFF;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700159 nsecs_t prevTime = 0;
160 };
161
Alec Mourie4034bb2019-11-19 12:45:54 -0800162 struct RenderEngineDuration {
163 nsecs_t startTime;
164 std::variant<nsecs_t, std::shared_ptr<FenceTime>> endTime;
165 };
166
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700167 struct GlobalRecord {
168 nsecs_t prevPresentTime = 0;
169 std::deque<std::shared_ptr<FenceTime>> presentFences;
Alec Mourie4034bb2019-11-19 12:45:54 -0800170 std::deque<RenderEngineDuration> renderEngineDurations;
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700171 };
172
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700173public:
Alec Mourib3885ad2019-09-06 17:08:55 -0700174 TimeStats();
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800175
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000176 // Delegate to the statsd service and associated APIs.
177 // Production code may use this class directly, whereas unit test may define
178 // a subclass for ease of testing.
179 class StatsEventDelegate {
180 public:
181 virtual ~StatsEventDelegate() = default;
Tej Singh2a457b62020-01-31 16:16:10 -0800182 virtual AStatsEvent* addStatsEventToPullData(AStatsEventList* data) {
183 return AStatsEventList_addStatsEvent(data);
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000184 }
Tej Singh38a4b212020-03-13 19:04:51 -0700185 virtual void setStatsPullAtomCallback(int32_t atom_tag,
186 AStatsManager_PullAtomMetadata* metadata,
187 AStatsManager_PullAtomCallback callback,
188 void* cookie) {
189 return AStatsManager_setPullAtomCallback(atom_tag, metadata, callback, cookie);
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000190 }
191
Tej Singh38a4b212020-03-13 19:04:51 -0700192 virtual void clearStatsPullAtomCallback(int32_t atom_tag) {
193 return AStatsManager_clearPullAtomCallback(atom_tag);
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000194 }
195
Tej Singh2a457b62020-01-31 16:16:10 -0800196 virtual void statsEventSetAtomId(AStatsEvent* event, uint32_t atom_id) {
197 return AStatsEvent_setAtomId(event, atom_id);
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000198 }
199
Alec Mouri717bcb62020-02-10 17:07:19 -0800200 virtual void statsEventWriteInt32(AStatsEvent* event, int32_t field) {
201 return AStatsEvent_writeInt32(event, field);
202 }
203
Tej Singh2a457b62020-01-31 16:16:10 -0800204 virtual void statsEventWriteInt64(AStatsEvent* event, int64_t field) {
205 return AStatsEvent_writeInt64(event, field);
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000206 }
207
Tej Singh2a457b62020-01-31 16:16:10 -0800208 virtual void statsEventWriteString8(AStatsEvent* event, const char* field) {
209 return AStatsEvent_writeString(event, field);
Alec Mouri37384342020-01-02 17:23:37 -0800210 }
211
Tej Singh2a457b62020-01-31 16:16:10 -0800212 virtual void statsEventWriteByteArray(AStatsEvent* event, const uint8_t* buf,
Alec Mouri37384342020-01-02 17:23:37 -0800213 size_t numBytes) {
Tej Singh2a457b62020-01-31 16:16:10 -0800214 return AStatsEvent_writeByteArray(event, buf, numBytes);
Alec Mouri37384342020-01-02 17:23:37 -0800215 }
216
Tej Singh2a457b62020-01-31 16:16:10 -0800217 virtual void statsEventBuild(AStatsEvent* event) { return AStatsEvent_build(event); }
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000218 };
219 // For testing only for injecting custom dependencies.
Alec Mouri37384342020-01-02 17:23:37 -0800220 TimeStats(std::unique_ptr<StatsEventDelegate> statsDelegate,
221 std::optional<size_t> maxPulledLayers,
222 std::optional<size_t> maxPulledHistogramBuckets);
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000223
Alec Mouri3ecd5cd2020-01-29 12:53:07 -0800224 ~TimeStats() override;
225
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000226 void onBootFinished() override;
Alec Mourifb571ea2019-01-24 18:42:10 -0800227 void parseArgs(bool asProto, const Vector<String16>& args, std::string& result) override;
228 bool isEnabled() override;
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700229 std::string miniDump() override;
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800230
Alec Mourifb571ea2019-01-24 18:42:10 -0800231 void incrementTotalFrames() override;
232 void incrementMissedFrames() override;
233 void incrementClientCompositionFrames() override;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800234 void incrementClientCompositionReusedFrames() override;
Alec Mouri8de697e2020-03-19 10:52:01 -0700235 void incrementRefreshRateSwitches() override;
Alec Mouri8f7a0102020-04-15 12:11:10 -0700236 void incrementCompositionStrategyChanges() override;
Alec Mouri717bcb62020-02-10 17:07:19 -0800237 void recordDisplayEventConnectionCount(int32_t count) override;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700238
Alec Mouri9519bf12019-11-15 16:54:44 -0800239 void recordFrameDuration(nsecs_t startTime, nsecs_t endTime) override;
Alec Mourie4034bb2019-11-19 12:45:54 -0800240 void recordRenderEngineDuration(nsecs_t startTime, nsecs_t endTime) override;
241 void recordRenderEngineDuration(nsecs_t startTime,
242 const std::shared_ptr<FenceTime>& readyFence) override;
Alec Mouri9519bf12019-11-15 16:54:44 -0800243
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800244 void setPostTime(int32_t layerId, uint64_t frameNumber, const std::string& layerName,
Alec Mourifb571ea2019-01-24 18:42:10 -0800245 nsecs_t postTime) override;
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800246 void setLatchTime(int32_t layerId, uint64_t frameNumber, nsecs_t latchTime) override;
Alec Mouri91f6df32020-01-30 08:48:58 -0800247 void incrementLatchSkipped(int32_t layerId, LatchSkipReason reason) override;
248 void incrementBadDesiredPresent(int32_t layerId) override;
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800249 void setDesiredTime(int32_t layerId, uint64_t frameNumber, nsecs_t desiredTime) override;
250 void setAcquireTime(int32_t layerId, uint64_t frameNumber, nsecs_t acquireTime) override;
251 void setAcquireFence(int32_t layerId, uint64_t frameNumber,
Alec Mourifb571ea2019-01-24 18:42:10 -0800252 const std::shared_ptr<FenceTime>& acquireFence) override;
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800253 void setPresentTime(int32_t layerId, uint64_t frameNumber, nsecs_t presentTime) override;
254 void setPresentFence(int32_t layerId, uint64_t frameNumber,
Alec Mourifb571ea2019-01-24 18:42:10 -0800255 const std::shared_ptr<FenceTime>& presentFence) override;
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800256 // Clean up the layer record
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800257 void onDestroy(int32_t layerId) override;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700258 // If SF skips or rejects a buffer, remove the corresponding TimeRecord.
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800259 void removeTimeRecord(int32_t layerId, uint64_t frameNumber) override;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700260
Peiyong Lin65248e02020-04-18 21:15:07 -0700261 void setPowerMode(
262 hardware::graphics::composer::V2_4::IComposerClient::PowerMode powerMode) override;
Alec Mourifb571ea2019-01-24 18:42:10 -0800263 // Source of truth is RefrehRateStats.
264 void recordRefreshRate(uint32_t fps, nsecs_t duration) override;
265 void setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) override;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700266
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800267 static const size_t MAX_NUM_TIME_RECORDS = 64;
268
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700269private:
Tej Singh2a457b62020-01-31 16:16:10 -0800270 static AStatsManager_PullAtomCallbackReturn pullAtomCallback(int32_t atom_tag,
271 AStatsEventList* data,
272 void* cookie);
273 AStatsManager_PullAtomCallbackReturn populateGlobalAtom(AStatsEventList* data);
274 AStatsManager_PullAtomCallbackReturn populateLayerAtom(AStatsEventList* data);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800275 bool recordReadyLocked(int32_t layerId, TimeRecord* timeRecord);
276 void flushAvailableRecordsToStatsLocked(int32_t layerId);
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700277 void flushPowerTimeLocked();
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700278 void flushAvailableGlobalRecordsToStatsLocked();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700279
280 void enable();
281 void disable();
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000282 void clearAll();
283 void clearGlobalLocked();
284 void clearLayersLocked();
Yiwei Zhang5434a782018-12-05 18:06:32 -0800285 void dump(bool asProto, std::optional<uint32_t> maxLayers, std::string& result);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700286
287 std::atomic<bool> mEnabled = false;
288 std::mutex mMutex;
Yiwei Zhangdc224042018-10-18 15:34:00 -0700289 TimeStatsHelper::TimeStatsGlobal mTimeStats;
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800290 // Hashmap for LayerRecord with layerId as the hash key
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700291 std::unordered_map<int32_t, LayerRecord> mTimeStatsTracker;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700292 PowerTime mPowerTime;
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700293 GlobalRecord mGlobalRecord;
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700294
295 static const size_t MAX_NUM_LAYER_RECORDS = 200;
Yiwei Zhange926ab52019-08-14 15:16:00 -0700296 static const size_t MAX_NUM_LAYER_STATS = 200;
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000297 std::unique_ptr<StatsEventDelegate> mStatsDelegate = std::make_unique<StatsEventDelegate>();
Alec Mouri37384342020-01-02 17:23:37 -0800298 size_t mMaxPulledLayers = 8;
299 size_t mMaxPulledHistogramBuckets = 6;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700300};
301
Alec Mourifb571ea2019-01-24 18:42:10 -0800302} // namespace impl
303
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700304} // namespace android