blob: 721d009570cc6d8d51272637198cd85fb77da654 [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,
73 int 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: {
108 switch (inputEventAction) {
109 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
137 bool isDown = inputEventType == InputEventType::MOTION &&
138 inputEventAction == AMOTION_EVENT_ACTION_DOWN;
Asmita Poddardd9a6cd2023-09-26 15:35:12 +0000139 mTimelines.emplace(inputEventId,
140 InputEventTimeline(isDown, eventTime, readTime, identifier->vendor,
jioana0bdbea12024-08-10 19:26:04 +0000141 identifier->product, sources, inputEventActionType));
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000142 mEventTimes.emplace(eventTime, inputEventId);
143}
144
145void LatencyTracker::trackFinishedEvent(int32_t inputEventId, const sp<IBinder>& connectionToken,
146 nsecs_t deliveryTime, nsecs_t consumeTime,
147 nsecs_t finishTime) {
148 const auto it = mTimelines.find(inputEventId);
149 if (it == mTimelines.end()) {
Siarhei Vishniakou363e7292021-07-09 03:22:42 +0000150 // This could happen if we erased this event when duplicate events were detected. It's
151 // 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 +0000152 // anything in its process. Just drop the report and move on.
153 return;
154 }
155
156 InputEventTimeline& timeline = it->second;
157 const auto connectionIt = timeline.connectionTimelines.find(connectionToken);
158 if (connectionIt == timeline.connectionTimelines.end()) {
159 // Most likely case: app calls 'finishInputEvent' before it reports the graphics timeline
160 timeline.connectionTimelines.emplace(connectionToken,
161 ConnectionTimeline{deliveryTime, consumeTime,
162 finishTime});
163 } else {
164 // Already have a record for this connectionToken
165 ConnectionTimeline& connectionTimeline = connectionIt->second;
166 const bool success =
167 connectionTimeline.setDispatchTimeline(deliveryTime, consumeTime, finishTime);
168 if (!success) {
169 // We are receiving unreliable data from the app. Just delete the entire connection
170 // timeline for this event
171 timeline.connectionTimelines.erase(connectionIt);
172 }
173 }
174}
175
176void LatencyTracker::trackGraphicsLatency(
177 int32_t inputEventId, const sp<IBinder>& connectionToken,
178 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline) {
179 const auto it = mTimelines.find(inputEventId);
180 if (it == mTimelines.end()) {
Siarhei Vishniakou363e7292021-07-09 03:22:42 +0000181 // This could happen if we erased this event when duplicate events were detected. It's
182 // 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 +0000183 // anything in its process. Just drop the report and move on.
184 return;
185 }
186
187 InputEventTimeline& timeline = it->second;
188 const auto connectionIt = timeline.connectionTimelines.find(connectionToken);
189 if (connectionIt == timeline.connectionTimelines.end()) {
190 timeline.connectionTimelines.emplace(connectionToken, std::move(graphicsTimeline));
191 } else {
192 // Most likely case
193 ConnectionTimeline& connectionTimeline = connectionIt->second;
194 const bool success = connectionTimeline.setGraphicsTimeline(std::move(graphicsTimeline));
195 if (!success) {
196 // We are receiving unreliable data from the app. Just delete the entire connection
197 // timeline for this event
198 timeline.connectionTimelines.erase(connectionIt);
199 }
200 }
201}
202
203/**
204 * We should use the current time 'now()' here to determine the age of the event, but instead we
205 * are using the latest 'eventTime' for efficiency since this time is already acquired, and
206 * 'trackListener' should happen soon after the event occurs.
207 */
208void LatencyTracker::reportAndPruneMatureRecords(nsecs_t newEventTime) {
209 while (!mEventTimes.empty()) {
210 const auto& [oldestEventTime, oldestInputEventId] = *mEventTimes.begin();
Harry Cutts33476232023-01-30 19:57:29 +0000211 if (isMatureEvent(oldestEventTime, /*now=*/newEventTime)) {
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000212 // Report and drop this event
213 const auto it = mTimelines.find(oldestInputEventId);
214 LOG_ALWAYS_FATAL_IF(it == mTimelines.end(),
215 "Event %" PRId32 " is in mEventTimes, but not in mTimelines",
216 oldestInputEventId);
217 const InputEventTimeline& timeline = it->second;
218 mTimelineProcessor->processTimeline(timeline);
219 mTimelines.erase(it);
220 mEventTimes.erase(mEventTimes.begin());
221 } else {
222 // If the oldest event does not need to be pruned, no events should be pruned.
223 return;
224 }
225 }
226}
227
Siarhei Vishniakou4c9d6ff2023-04-18 11:23:20 -0700228std::string LatencyTracker::dump(const char* prefix) const {
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000229 return StringPrintf("%sLatencyTracker:\n", prefix) +
230 StringPrintf("%s mTimelines.size() = %zu\n", prefix, mTimelines.size()) +
231 StringPrintf("%s mEventTimes.size() = %zu\n", prefix, mEventTimes.size());
232}
233
Asmita Poddardd9a6cd2023-09-26 15:35:12 +0000234void LatencyTracker::setInputDevices(const std::vector<InputDeviceInfo>& inputDevices) {
235 mInputDevices = inputDevices;
236}
237
Siarhei Vishniakouf2652122021-03-05 21:39:46 +0000238} // namespace android::inputdispatcher