blob: a2843c8e598dc53c8f1ae39dd8780323fc69b849 [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 Mourifb571ea2019-01-24 18:42:10 -080055
Alec Mouri9519bf12019-11-15 16:54:44 -080056 // 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 Mourie4034bb2019-11-19 12:45:54 -080062 // 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 Mouri9519bf12019-11-15 16:54:44 -080069
Yiwei Zhang1a88c402019-11-18 10:43:58 -080070 virtual void setPostTime(int32_t layerId, uint64_t frameNumber, const std::string& layerName,
Alec Mourifb571ea2019-01-24 18:42:10 -080071 nsecs_t postTime) = 0;
Yiwei Zhang1a88c402019-11-18 10:43:58 -080072 virtual void setLatchTime(int32_t layerId, uint64_t frameNumber, nsecs_t latchTime) = 0;
Alec Mouri91f6df32020-01-30 08:48:58 -080073 // Reasons why latching a particular buffer may be skipped
74 enum class LatchSkipReason {
75 // If the acquire fence did not fire on some devices we skip latching
76 // the buffer until the fence fires.
77 LateAcquire,
78 };
79 // Increments the counter of skipped latch buffers.
80 virtual void incrementLatchSkipped(int32_t layerId, LatchSkipReason reason) = 0;
81 // Increments the counter of bad desired present times for this layer.
82 // Bad desired present times are "implausible" and cause SurfaceFlinger to
83 // latch a buffer immediately to avoid stalling.
84 virtual void incrementBadDesiredPresent(int32_t layerId) = 0;
Yiwei Zhang1a88c402019-11-18 10:43:58 -080085 virtual void setDesiredTime(int32_t layerId, uint64_t frameNumber, nsecs_t desiredTime) = 0;
86 virtual void setAcquireTime(int32_t layerId, uint64_t frameNumber, nsecs_t acquireTime) = 0;
87 virtual void setAcquireFence(int32_t layerId, uint64_t frameNumber,
Alec Mourifb571ea2019-01-24 18:42:10 -080088 const std::shared_ptr<FenceTime>& acquireFence) = 0;
Alec Mourie4034bb2019-11-19 12:45:54 -080089 // SetPresent{Time, Fence} are not expected to be called in the critical
90 // rendering path, as they flush prior fences if those fences have fired.
Yiwei Zhang1a88c402019-11-18 10:43:58 -080091 virtual void setPresentTime(int32_t layerId, uint64_t frameNumber, nsecs_t presentTime) = 0;
92 virtual void setPresentFence(int32_t layerId, uint64_t frameNumber,
Alec Mourifb571ea2019-01-24 18:42:10 -080093 const std::shared_ptr<FenceTime>& presentFence) = 0;
94 // Clean up the layer record
Yiwei Zhang1a88c402019-11-18 10:43:58 -080095 virtual void onDestroy(int32_t layerId) = 0;
Alec Mourifb571ea2019-01-24 18:42:10 -080096 // If SF skips or rejects a buffer, remove the corresponding TimeRecord.
Yiwei Zhang1a88c402019-11-18 10:43:58 -080097 virtual void removeTimeRecord(int32_t layerId, uint64_t frameNumber) = 0;
Alec Mourifb571ea2019-01-24 18:42:10 -080098
99 virtual void setPowerMode(int32_t powerMode) = 0;
100 // Source of truth is RefrehRateStats.
101 virtual void recordRefreshRate(uint32_t fps, nsecs_t duration) = 0;
102 virtual void setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) = 0;
103};
104
105namespace impl {
106
107class TimeStats : public android::TimeStats {
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700108 struct FrameTime {
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700109 uint64_t frameNumber = 0;
110 nsecs_t postTime = 0;
111 nsecs_t latchTime = 0;
112 nsecs_t acquireTime = 0;
113 nsecs_t desiredTime = 0;
114 nsecs_t presentTime = 0;
Yiwei Zhangcf50ab92018-06-14 10:50:12 -0700115 };
116
117 struct TimeRecord {
118 bool ready = false;
119 FrameTime frameTime;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700120 std::shared_ptr<FenceTime> acquireFence;
121 std::shared_ptr<FenceTime> presentFence;
122 };
123
124 struct LayerRecord {
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700125 std::string layerName;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700126 // This is the index in timeRecords, at which the timestamps for that
127 // specific frame are still not fully received. This is not waiting for
128 // fences to signal, but rather waiting to receive those fences/timestamps.
129 int32_t waitData = -1;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700130 uint32_t droppedFrames = 0;
Alec Mouri91f6df32020-01-30 08:48:58 -0800131 uint32_t lateAcquireFrames = 0;
132 uint32_t badDesiredPresentFrames = 0;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700133 TimeRecord prevTimeRecord;
Yiwei Zhangc5f2c452018-05-08 16:31:56 -0700134 std::deque<TimeRecord> timeRecords;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700135 };
136
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700137 struct PowerTime {
138 int32_t powerMode = HWC_POWER_MODE_OFF;
139 nsecs_t prevTime = 0;
140 };
141
Alec Mourie4034bb2019-11-19 12:45:54 -0800142 struct RenderEngineDuration {
143 nsecs_t startTime;
144 std::variant<nsecs_t, std::shared_ptr<FenceTime>> endTime;
145 };
146
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700147 struct GlobalRecord {
148 nsecs_t prevPresentTime = 0;
149 std::deque<std::shared_ptr<FenceTime>> presentFences;
Alec Mourie4034bb2019-11-19 12:45:54 -0800150 std::deque<RenderEngineDuration> renderEngineDurations;
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700151 };
152
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700153public:
Alec Mourib3885ad2019-09-06 17:08:55 -0700154 TimeStats();
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800155
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000156 // Delegate to the statsd service and associated APIs.
157 // Production code may use this class directly, whereas unit test may define
158 // a subclass for ease of testing.
159 class StatsEventDelegate {
160 public:
161 virtual ~StatsEventDelegate() = default;
162 virtual struct stats_event* addStatsEventToPullData(pulled_stats_event_list* data) {
163 return add_stats_event_to_pull_data(data);
164 }
165 virtual void registerStatsPullAtomCallback(int32_t atom_tag,
166 stats_pull_atom_callback_t callback,
167 pull_atom_metadata* metadata, void* cookie) {
168 return register_stats_pull_atom_callback(atom_tag, callback, metadata, cookie);
169 }
170
171 virtual void unregisterStatsPullAtomCallback(int32_t atom_tag) {
172 return unregister_stats_pull_atom_callback(atom_tag);
173 }
174
175 virtual void statsEventSetAtomId(struct stats_event* event, uint32_t atom_id) {
176 return stats_event_set_atom_id(event, atom_id);
177 }
178
179 virtual void statsEventWriteInt64(struct stats_event* event, int64_t field) {
180 return stats_event_write_int64(event, field);
181 }
182
Alec Mouri37384342020-01-02 17:23:37 -0800183 virtual void statsEventWriteString8(struct stats_event* event, const char* field) {
184 return stats_event_write_string8(event, field);
185 }
186
187 virtual void statsEventWriteByteArray(struct stats_event* event, const uint8_t* buf,
188 size_t numBytes) {
189 return stats_event_write_byte_array(event, buf, numBytes);
190 }
191
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000192 virtual void statsEventBuild(struct stats_event* event) { return stats_event_build(event); }
193 };
194 // For testing only for injecting custom dependencies.
Alec Mouri37384342020-01-02 17:23:37 -0800195 TimeStats(std::unique_ptr<StatsEventDelegate> statsDelegate,
196 std::optional<size_t> maxPulledLayers,
197 std::optional<size_t> maxPulledHistogramBuckets);
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000198
199 void onBootFinished() override;
Alec Mourifb571ea2019-01-24 18:42:10 -0800200 void parseArgs(bool asProto, const Vector<String16>& args, std::string& result) override;
201 bool isEnabled() override;
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700202 std::string miniDump() override;
Yiwei Zhang7e666a52018-11-15 13:33:42 -0800203
Alec Mourifb571ea2019-01-24 18:42:10 -0800204 void incrementTotalFrames() override;
205 void incrementMissedFrames() override;
206 void incrementClientCompositionFrames() override;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800207 void incrementClientCompositionReusedFrames() override;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700208
Alec Mouri9519bf12019-11-15 16:54:44 -0800209 void recordFrameDuration(nsecs_t startTime, nsecs_t endTime) override;
Alec Mourie4034bb2019-11-19 12:45:54 -0800210 void recordRenderEngineDuration(nsecs_t startTime, nsecs_t endTime) override;
211 void recordRenderEngineDuration(nsecs_t startTime,
212 const std::shared_ptr<FenceTime>& readyFence) override;
Alec Mouri9519bf12019-11-15 16:54:44 -0800213
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800214 void setPostTime(int32_t layerId, uint64_t frameNumber, const std::string& layerName,
Alec Mourifb571ea2019-01-24 18:42:10 -0800215 nsecs_t postTime) override;
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800216 void setLatchTime(int32_t layerId, uint64_t frameNumber, nsecs_t latchTime) override;
Alec Mouri91f6df32020-01-30 08:48:58 -0800217 void incrementLatchSkipped(int32_t layerId, LatchSkipReason reason) override;
218 void incrementBadDesiredPresent(int32_t layerId) override;
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800219 void setDesiredTime(int32_t layerId, uint64_t frameNumber, nsecs_t desiredTime) override;
220 void setAcquireTime(int32_t layerId, uint64_t frameNumber, nsecs_t acquireTime) override;
221 void setAcquireFence(int32_t layerId, uint64_t frameNumber,
Alec Mourifb571ea2019-01-24 18:42:10 -0800222 const std::shared_ptr<FenceTime>& acquireFence) override;
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800223 void setPresentTime(int32_t layerId, uint64_t frameNumber, nsecs_t presentTime) override;
224 void setPresentFence(int32_t layerId, uint64_t frameNumber,
Alec Mourifb571ea2019-01-24 18:42:10 -0800225 const std::shared_ptr<FenceTime>& presentFence) override;
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800226 // Clean up the layer record
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800227 void onDestroy(int32_t layerId) override;
Yiwei Zhangeaeea062018-06-28 14:46:51 -0700228 // If SF skips or rejects a buffer, remove the corresponding TimeRecord.
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800229 void removeTimeRecord(int32_t layerId, uint64_t frameNumber) override;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700230
Alec Mourifb571ea2019-01-24 18:42:10 -0800231 void setPowerMode(int32_t powerMode) override;
232 // Source of truth is RefrehRateStats.
233 void recordRefreshRate(uint32_t fps, nsecs_t duration) override;
234 void setPresentFenceGlobal(const std::shared_ptr<FenceTime>& presentFence) override;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700235
Yiwei Zhangaf8ee942018-11-22 00:15:23 -0800236 static const size_t MAX_NUM_TIME_RECORDS = 64;
237
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700238private:
Alec Mouri37384342020-01-02 17:23:37 -0800239 static status_pull_atom_return_t pullAtomCallback(int32_t atom_tag,
240 pulled_stats_event_list* data, void* cookie);
241 status_pull_atom_return_t populateGlobalAtom(pulled_stats_event_list* data);
242 status_pull_atom_return_t populateLayerAtom(pulled_stats_event_list* data);
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800243 bool recordReadyLocked(int32_t layerId, TimeRecord* timeRecord);
244 void flushAvailableRecordsToStatsLocked(int32_t layerId);
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700245 void flushPowerTimeLocked();
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700246 void flushAvailableGlobalRecordsToStatsLocked();
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700247
248 void enable();
249 void disable();
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000250 void clearAll();
251 void clearGlobalLocked();
252 void clearLayersLocked();
Yiwei Zhang5434a782018-12-05 18:06:32 -0800253 void dump(bool asProto, std::optional<uint32_t> maxLayers, std::string& result);
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700254
255 std::atomic<bool> mEnabled = false;
256 std::mutex mMutex;
Yiwei Zhangdc224042018-10-18 15:34:00 -0700257 TimeStatsHelper::TimeStatsGlobal mTimeStats;
Yiwei Zhang1a88c402019-11-18 10:43:58 -0800258 // Hashmap for LayerRecord with layerId as the hash key
Yiwei Zhang9689e2f2018-05-11 12:33:23 -0700259 std::unordered_map<int32_t, LayerRecord> mTimeStatsTracker;
Yiwei Zhang3a226d22018-10-16 09:23:03 -0700260 PowerTime mPowerTime;
Yiwei Zhangce6ebc02018-10-20 12:42:38 -0700261 GlobalRecord mGlobalRecord;
Yiwei Zhang7eb58b72019-04-22 19:00:02 -0700262
263 static const size_t MAX_NUM_LAYER_RECORDS = 200;
Yiwei Zhange926ab52019-08-14 15:16:00 -0700264 static const size_t MAX_NUM_LAYER_STATS = 200;
Alec Mouri8e2f31b2020-01-16 22:04:35 +0000265 std::unique_ptr<StatsEventDelegate> mStatsDelegate = std::make_unique<StatsEventDelegate>();
Alec Mouri37384342020-01-02 17:23:37 -0800266 size_t mMaxPulledLayers = 8;
267 size_t mMaxPulledHistogramBuckets = 6;
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700268};
269
Alec Mourifb571ea2019-01-24 18:42:10 -0800270} // namespace impl
271
Yiwei Zhang0102ad22018-05-02 17:37:17 -0700272} // namespace android