blob: d203fbabf6941e4bb864ade929a28d0bea2430d3 [file] [log] [blame]
Siarhei Vishniakouf2652122021-03-05 21:39:46 +00001/*
2 * Copyright (C) 2021 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#define LOG_TAG "LatencyTracker"
18#include "LatencyTracker.h"
Asmita Poddardd9a6cd2023-09-26 15:35:12 +000019#include "../InputDeviceMetricsSource.h"
Siarhei Vishniakouf2652122021-03-05 21:39:46 +000020
21#include <inttypes.h>
22
23#include <android-base/properties.h>
24#include <android-base/stringprintf.h>
25#include <android/os/IInputConstants.h>
26#include <input/Input.h>
Asmita Poddardd9a6cd2023-09-26 15:35:12 +000027#include <input/InputDevice.h>
Siarhei Vishniakouf2652122021-03-05 21:39:46 +000028#include <log/log.h>
29
30using android::base::HwTimeoutMultiplier;
31using android::base::StringPrintf;
32
33namespace android::inputdispatcher {
34
35/**
36 * Events that are older than this time will be considered mature, at which point we will stop
37 * waiting for the apps to provide further information about them.
38 * It's likely that the apps will ANR if the events are not received by this deadline, and we
39 * already track ANR metrics separately.
40 */
41const std::chrono::duration ANR_TIMEOUT = std::chrono::milliseconds(
42 android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS *
43 HwTimeoutMultiplier());
44
45static bool isMatureEvent(nsecs_t eventTime, nsecs_t now) {
46 std::chrono::duration age = std::chrono::nanoseconds(now) - std::chrono::nanoseconds(eventTime);
47 return age > ANR_TIMEOUT;
48}
49
50/**
51 * A multimap allows to have several entries with the same key. This function just erases a specific
52 * key-value pair. Equivalent to the imaginary std api std::multimap::erase(key, value).
53 */
54template <typename K, typename V>
Siarhei Vishniakou363e7292021-07-09 03:22:42 +000055static void eraseByValue(std::multimap<K, V>& map, const V& value) {
56 for (auto it = map.begin(); it != map.end();) {
Siarhei Vishniakouf2652122021-03-05 21:39:46 +000057 if (it->second == value) {
Siarhei Vishniakou363e7292021-07-09 03:22:42 +000058 it = map.erase(it);
59 } else {
60 it++;
Siarhei Vishniakouf2652122021-03-05 21:39:46 +000061 }
62 }
63}
64
jioana24878b52024-09-10 10:13:27 +000065LatencyTracker::LatencyTracker(InputEventTimelineProcessor& processor)
66 : mTimelineProcessor(&processor) {}
Siarhei Vishniakouf2652122021-03-05 21:39:46 +000067
jioana0bdbea12024-08-10 19:26:04 +000068void LatencyTracker::trackListener(int32_t inputEventId, nsecs_t eventTime, nsecs_t readTime,
69 DeviceId deviceId,
70 const std::set<InputDeviceUsageSource>& sources,
jioana97cc8ac2024-09-09 15:01:43 +000071 int32_t inputEventAction, InputEventType inputEventType) {
Siarhei Vishniakouf2652122021-03-05 21:39:46 +000072 reportAndPruneMatureRecords(eventTime);
73 const auto it = mTimelines.find(inputEventId);
74 if (it != mTimelines.end()) {
75 // Input event ids are randomly generated, so it's possible that two events have the same
76 // event id. Drop this event, and also drop the existing event because the apps would
77 // confuse us by reporting the rest of the timeline for one of them. This should happen
78 // rarely, so we won't lose much data
79 mTimelines.erase(it);
Siarhei Vishniakou363e7292021-07-09 03:22:42 +000080 eraseByValue(mEventTimes, inputEventId);
Siarhei Vishniakouf2652122021-03-05 21:39:46 +000081 return;
82 }
Asmita Poddardd9a6cd2023-09-26 15:35:12 +000083
84 // Create an InputEventTimeline for the device ID. The vendorId and productId
85 // can be obtained from the InputDeviceIdentifier of the particular device.
86 const InputDeviceIdentifier* identifier = nullptr;
87 for (auto& inputDevice : mInputDevices) {
88 if (deviceId == inputDevice.getId()) {
89 identifier = &inputDevice.getIdentifier();
90 break;
91 }
92 }
93
94 // If no matching ids can be found for the device from among the input devices connected,
95 // the call to trackListener will be dropped.
96 // Note: there generally isn't expected to be a situation where we can't find an InputDeviceInfo
97 // but a possibility of it is handled in case of race conditions
98 if (identifier == nullptr) {
99 ALOGE("Could not find input device identifier. Dropping call to LatencyTracker.");
100 return;
101 }
102
jioana0bdbea12024-08-10 19:26:04 +0000103 const InputEventActionType inputEventActionType = [&]() {
104 switch (inputEventType) {
105 case InputEventType::MOTION: {
jioana97cc8ac2024-09-09 15:01:43 +0000106 switch (MotionEvent::getActionMasked(inputEventAction)) {
jioana0bdbea12024-08-10 19:26:04 +0000107 case AMOTION_EVENT_ACTION_DOWN:
108 return InputEventActionType::MOTION_ACTION_DOWN;
109 case AMOTION_EVENT_ACTION_MOVE:
110 return InputEventActionType::MOTION_ACTION_MOVE;
111 case AMOTION_EVENT_ACTION_UP:
112 return InputEventActionType::MOTION_ACTION_UP;
113 case AMOTION_EVENT_ACTION_HOVER_MOVE:
114 return InputEventActionType::MOTION_ACTION_HOVER_MOVE;
115 case AMOTION_EVENT_ACTION_SCROLL:
116 return InputEventActionType::MOTION_ACTION_SCROLL;
117 default:
118 return InputEventActionType::UNKNOWN_INPUT_EVENT;
119 }
120 }
121 case InputEventType::KEY: {
122 switch (inputEventAction) {
123 case AKEY_EVENT_ACTION_DOWN:
124 case AKEY_EVENT_ACTION_UP:
125 return InputEventActionType::KEY;
126 default:
127 return InputEventActionType::UNKNOWN_INPUT_EVENT;
128 }
129 }
130 default:
131 return InputEventActionType::UNKNOWN_INPUT_EVENT;
132 }
133 }();
134
Asmita Poddardd9a6cd2023-09-26 15:35:12 +0000135 mTimelines.emplace(inputEventId,
jioana97cc8ac2024-09-09 15:01:43 +0000136 InputEventTimeline(eventTime, readTime, identifier->vendor,
jioana0bdbea12024-08-10 19:26:04 +0000137 identifier->product, sources, inputEventActionType));
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000138 mEventTimes.emplace(eventTime, inputEventId);
139}
140
141void LatencyTracker::trackFinishedEvent(int32_t inputEventId, const sp<IBinder>& connectionToken,
142 nsecs_t deliveryTime, nsecs_t consumeTime,
143 nsecs_t finishTime) {
144 const auto it = mTimelines.find(inputEventId);
145 if (it == mTimelines.end()) {
Siarhei Vishniakou363e7292021-07-09 03:22:42 +0000146 // This could happen if we erased this event when duplicate events were detected. It's
147 // also possible that an app sent a bad (or late) 'Finish' signal, since it's free to do
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000148 // anything in its process. Just drop the report and move on.
149 return;
150 }
151
152 InputEventTimeline& timeline = it->second;
153 const auto connectionIt = timeline.connectionTimelines.find(connectionToken);
154 if (connectionIt == timeline.connectionTimelines.end()) {
155 // Most likely case: app calls 'finishInputEvent' before it reports the graphics timeline
156 timeline.connectionTimelines.emplace(connectionToken,
157 ConnectionTimeline{deliveryTime, consumeTime,
158 finishTime});
159 } else {
160 // Already have a record for this connectionToken
161 ConnectionTimeline& connectionTimeline = connectionIt->second;
162 const bool success =
163 connectionTimeline.setDispatchTimeline(deliveryTime, consumeTime, finishTime);
164 if (!success) {
165 // We are receiving unreliable data from the app. Just delete the entire connection
166 // timeline for this event
167 timeline.connectionTimelines.erase(connectionIt);
168 }
169 }
170}
171
172void LatencyTracker::trackGraphicsLatency(
173 int32_t inputEventId, const sp<IBinder>& connectionToken,
174 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline) {
175 const auto it = mTimelines.find(inputEventId);
176 if (it == mTimelines.end()) {
Siarhei Vishniakou363e7292021-07-09 03:22:42 +0000177 // This could happen if we erased this event when duplicate events were detected. It's
178 // also possible that an app sent a bad (or late) 'Timeline' signal, since it's free to do
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000179 // anything in its process. Just drop the report and move on.
180 return;
181 }
182
183 InputEventTimeline& timeline = it->second;
184 const auto connectionIt = timeline.connectionTimelines.find(connectionToken);
185 if (connectionIt == timeline.connectionTimelines.end()) {
186 timeline.connectionTimelines.emplace(connectionToken, std::move(graphicsTimeline));
187 } else {
188 // Most likely case
189 ConnectionTimeline& connectionTimeline = connectionIt->second;
190 const bool success = connectionTimeline.setGraphicsTimeline(std::move(graphicsTimeline));
191 if (!success) {
192 // We are receiving unreliable data from the app. Just delete the entire connection
193 // timeline for this event
194 timeline.connectionTimelines.erase(connectionIt);
195 }
196 }
197}
198
199/**
200 * We should use the current time 'now()' here to determine the age of the event, but instead we
201 * are using the latest 'eventTime' for efficiency since this time is already acquired, and
202 * 'trackListener' should happen soon after the event occurs.
203 */
204void LatencyTracker::reportAndPruneMatureRecords(nsecs_t newEventTime) {
205 while (!mEventTimes.empty()) {
206 const auto& [oldestEventTime, oldestInputEventId] = *mEventTimes.begin();
Harry Cutts33476232023-01-30 19:57:29 +0000207 if (isMatureEvent(oldestEventTime, /*now=*/newEventTime)) {
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000208 // Report and drop this event
209 const auto it = mTimelines.find(oldestInputEventId);
210 LOG_ALWAYS_FATAL_IF(it == mTimelines.end(),
211 "Event %" PRId32 " is in mEventTimes, but not in mTimelines",
212 oldestInputEventId);
213 const InputEventTimeline& timeline = it->second;
214 mTimelineProcessor->processTimeline(timeline);
215 mTimelines.erase(it);
216 mEventTimes.erase(mEventTimes.begin());
217 } else {
218 // If the oldest event does not need to be pruned, no events should be pruned.
219 return;
220 }
221 }
222}
223
Siarhei Vishniakou4c9d6ff2023-04-18 11:23:20 -0700224std::string LatencyTracker::dump(const char* prefix) const {
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000225 return StringPrintf("%sLatencyTracker:\n", prefix) +
226 StringPrintf("%s mTimelines.size() = %zu\n", prefix, mTimelines.size()) +
227 StringPrintf("%s mEventTimes.size() = %zu\n", prefix, mEventTimes.size());
228}
229
Asmita Poddardd9a6cd2023-09-26 15:35:12 +0000230void LatencyTracker::setInputDevices(const std::vector<InputDeviceInfo>& inputDevices) {
231 mInputDevices = inputDevices;
232}
233
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000234} // namespace android::inputdispatcher