blob: 69024b326acffc57e53bce081b2e2cc5f8186c48 [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
65LatencyTracker::LatencyTracker(InputEventTimelineProcessor* processor)
66 : mTimelineProcessor(processor) {
67 LOG_ALWAYS_FATAL_IF(processor == nullptr);
68}
69
jioana0bdbea12024-08-10 19:26:04 +000070void LatencyTracker::trackListener(int32_t inputEventId, nsecs_t eventTime, nsecs_t readTime,
71 DeviceId deviceId,
72 const std::set<InputDeviceUsageSource>& sources,
jioana97cc8ac2024-09-09 15:01:43 +000073 int32_t inputEventAction, InputEventType inputEventType) {
Siarhei Vishniakouf2652122021-03-05 21:39:46 +000074 reportAndPruneMatureRecords(eventTime);
75 const auto it = mTimelines.find(inputEventId);
76 if (it != mTimelines.end()) {
77 // Input event ids are randomly generated, so it's possible that two events have the same
78 // event id. Drop this event, and also drop the existing event because the apps would
79 // confuse us by reporting the rest of the timeline for one of them. This should happen
80 // rarely, so we won't lose much data
81 mTimelines.erase(it);
Siarhei Vishniakou363e7292021-07-09 03:22:42 +000082 eraseByValue(mEventTimes, inputEventId);
Siarhei Vishniakouf2652122021-03-05 21:39:46 +000083 return;
84 }
Asmita Poddardd9a6cd2023-09-26 15:35:12 +000085
86 // Create an InputEventTimeline for the device ID. The vendorId and productId
87 // can be obtained from the InputDeviceIdentifier of the particular device.
88 const InputDeviceIdentifier* identifier = nullptr;
89 for (auto& inputDevice : mInputDevices) {
90 if (deviceId == inputDevice.getId()) {
91 identifier = &inputDevice.getIdentifier();
92 break;
93 }
94 }
95
96 // If no matching ids can be found for the device from among the input devices connected,
97 // the call to trackListener will be dropped.
98 // Note: there generally isn't expected to be a situation where we can't find an InputDeviceInfo
99 // but a possibility of it is handled in case of race conditions
100 if (identifier == nullptr) {
101 ALOGE("Could not find input device identifier. Dropping call to LatencyTracker.");
102 return;
103 }
104
jioana0bdbea12024-08-10 19:26:04 +0000105 const InputEventActionType inputEventActionType = [&]() {
106 switch (inputEventType) {
107 case InputEventType::MOTION: {
jioana97cc8ac2024-09-09 15:01:43 +0000108 switch (MotionEvent::getActionMasked(inputEventAction)) {
jioana0bdbea12024-08-10 19:26:04 +0000109 case AMOTION_EVENT_ACTION_DOWN:
110 return InputEventActionType::MOTION_ACTION_DOWN;
111 case AMOTION_EVENT_ACTION_MOVE:
112 return InputEventActionType::MOTION_ACTION_MOVE;
113 case AMOTION_EVENT_ACTION_UP:
114 return InputEventActionType::MOTION_ACTION_UP;
115 case AMOTION_EVENT_ACTION_HOVER_MOVE:
116 return InputEventActionType::MOTION_ACTION_HOVER_MOVE;
117 case AMOTION_EVENT_ACTION_SCROLL:
118 return InputEventActionType::MOTION_ACTION_SCROLL;
119 default:
120 return InputEventActionType::UNKNOWN_INPUT_EVENT;
121 }
122 }
123 case InputEventType::KEY: {
124 switch (inputEventAction) {
125 case AKEY_EVENT_ACTION_DOWN:
126 case AKEY_EVENT_ACTION_UP:
127 return InputEventActionType::KEY;
128 default:
129 return InputEventActionType::UNKNOWN_INPUT_EVENT;
130 }
131 }
132 default:
133 return InputEventActionType::UNKNOWN_INPUT_EVENT;
134 }
135 }();
136
Asmita Poddardd9a6cd2023-09-26 15:35:12 +0000137 mTimelines.emplace(inputEventId,
jioana97cc8ac2024-09-09 15:01:43 +0000138 InputEventTimeline(eventTime, readTime, identifier->vendor,
jioana0bdbea12024-08-10 19:26:04 +0000139 identifier->product, sources, inputEventActionType));
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000140 mEventTimes.emplace(eventTime, inputEventId);
141}
142
143void LatencyTracker::trackFinishedEvent(int32_t inputEventId, const sp<IBinder>& connectionToken,
144 nsecs_t deliveryTime, nsecs_t consumeTime,
145 nsecs_t finishTime) {
146 const auto it = mTimelines.find(inputEventId);
147 if (it == mTimelines.end()) {
Siarhei Vishniakou363e7292021-07-09 03:22:42 +0000148 // This could happen if we erased this event when duplicate events were detected. It's
149 // 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 +0000150 // anything in its process. Just drop the report and move on.
151 return;
152 }
153
154 InputEventTimeline& timeline = it->second;
155 const auto connectionIt = timeline.connectionTimelines.find(connectionToken);
156 if (connectionIt == timeline.connectionTimelines.end()) {
157 // Most likely case: app calls 'finishInputEvent' before it reports the graphics timeline
158 timeline.connectionTimelines.emplace(connectionToken,
159 ConnectionTimeline{deliveryTime, consumeTime,
160 finishTime});
161 } else {
162 // Already have a record for this connectionToken
163 ConnectionTimeline& connectionTimeline = connectionIt->second;
164 const bool success =
165 connectionTimeline.setDispatchTimeline(deliveryTime, consumeTime, finishTime);
166 if (!success) {
167 // We are receiving unreliable data from the app. Just delete the entire connection
168 // timeline for this event
169 timeline.connectionTimelines.erase(connectionIt);
170 }
171 }
172}
173
174void LatencyTracker::trackGraphicsLatency(
175 int32_t inputEventId, const sp<IBinder>& connectionToken,
176 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline) {
177 const auto it = mTimelines.find(inputEventId);
178 if (it == mTimelines.end()) {
Siarhei Vishniakou363e7292021-07-09 03:22:42 +0000179 // This could happen if we erased this event when duplicate events were detected. It's
180 // 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 +0000181 // anything in its process. Just drop the report and move on.
182 return;
183 }
184
185 InputEventTimeline& timeline = it->second;
186 const auto connectionIt = timeline.connectionTimelines.find(connectionToken);
187 if (connectionIt == timeline.connectionTimelines.end()) {
188 timeline.connectionTimelines.emplace(connectionToken, std::move(graphicsTimeline));
189 } else {
190 // Most likely case
191 ConnectionTimeline& connectionTimeline = connectionIt->second;
192 const bool success = connectionTimeline.setGraphicsTimeline(std::move(graphicsTimeline));
193 if (!success) {
194 // We are receiving unreliable data from the app. Just delete the entire connection
195 // timeline for this event
196 timeline.connectionTimelines.erase(connectionIt);
197 }
198 }
199}
200
201/**
202 * We should use the current time 'now()' here to determine the age of the event, but instead we
203 * are using the latest 'eventTime' for efficiency since this time is already acquired, and
204 * 'trackListener' should happen soon after the event occurs.
205 */
206void LatencyTracker::reportAndPruneMatureRecords(nsecs_t newEventTime) {
207 while (!mEventTimes.empty()) {
208 const auto& [oldestEventTime, oldestInputEventId] = *mEventTimes.begin();
Harry Cutts33476232023-01-30 19:57:29 +0000209 if (isMatureEvent(oldestEventTime, /*now=*/newEventTime)) {
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000210 // Report and drop this event
211 const auto it = mTimelines.find(oldestInputEventId);
212 LOG_ALWAYS_FATAL_IF(it == mTimelines.end(),
213 "Event %" PRId32 " is in mEventTimes, but not in mTimelines",
214 oldestInputEventId);
215 const InputEventTimeline& timeline = it->second;
216 mTimelineProcessor->processTimeline(timeline);
217 mTimelines.erase(it);
218 mEventTimes.erase(mEventTimes.begin());
219 } else {
220 // If the oldest event does not need to be pruned, no events should be pruned.
221 return;
222 }
223 }
224}
225
Siarhei Vishniakou4c9d6ff2023-04-18 11:23:20 -0700226std::string LatencyTracker::dump(const char* prefix) const {
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000227 return StringPrintf("%sLatencyTracker:\n", prefix) +
228 StringPrintf("%s mTimelines.size() = %zu\n", prefix, mTimelines.size()) +
229 StringPrintf("%s mEventTimes.size() = %zu\n", prefix, mEventTimes.size());
230}
231
Asmita Poddardd9a6cd2023-09-26 15:35:12 +0000232void LatencyTracker::setInputDevices(const std::vector<InputDeviceInfo>& inputDevices) {
233 mInputDevices = inputDevices;
234}
235
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000236} // namespace android::inputdispatcher