Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 1 | /* |
| 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 Poddar | dd9a6cd | 2023-09-26 15:35:12 +0000 | [diff] [blame] | 19 | #include "../InputDeviceMetricsSource.h" |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 20 | |
| 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 Poddar | dd9a6cd | 2023-09-26 15:35:12 +0000 | [diff] [blame] | 27 | #include <input/InputDevice.h> |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 28 | #include <log/log.h> |
| 29 | |
| 30 | using android::base::HwTimeoutMultiplier; |
| 31 | using android::base::StringPrintf; |
| 32 | |
| 33 | namespace 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 | */ |
| 41 | const std::chrono::duration ANR_TIMEOUT = std::chrono::milliseconds( |
| 42 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 43 | HwTimeoutMultiplier()); |
| 44 | |
| 45 | static 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 | */ |
| 54 | template <typename K, typename V> |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 55 | static void eraseByValue(std::multimap<K, V>& map, const V& value) { |
| 56 | for (auto it = map.begin(); it != map.end();) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 57 | if (it->second == value) { |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 58 | it = map.erase(it); |
| 59 | } else { |
| 60 | it++; |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | LatencyTracker::LatencyTracker(InputEventTimelineProcessor* processor) |
| 66 | : mTimelineProcessor(processor) { |
| 67 | LOG_ALWAYS_FATAL_IF(processor == nullptr); |
| 68 | } |
| 69 | |
jioana | 0bdbea1 | 2024-08-10 19:26:04 +0000 | [diff] [blame] | 70 | void LatencyTracker::trackListener(int32_t inputEventId, nsecs_t eventTime, nsecs_t readTime, |
| 71 | DeviceId deviceId, |
| 72 | const std::set<InputDeviceUsageSource>& sources, |
jioana | 97cc8ac | 2024-09-09 15:01:43 +0000 | [diff] [blame] | 73 | int32_t inputEventAction, InputEventType inputEventType) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 74 | 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 Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 82 | eraseByValue(mEventTimes, inputEventId); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 83 | return; |
| 84 | } |
Asmita Poddar | dd9a6cd | 2023-09-26 15:35:12 +0000 | [diff] [blame] | 85 | |
| 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 | |
jioana | 0bdbea1 | 2024-08-10 19:26:04 +0000 | [diff] [blame] | 105 | const InputEventActionType inputEventActionType = [&]() { |
| 106 | switch (inputEventType) { |
| 107 | case InputEventType::MOTION: { |
jioana | 97cc8ac | 2024-09-09 15:01:43 +0000 | [diff] [blame] | 108 | switch (MotionEvent::getActionMasked(inputEventAction)) { |
jioana | 0bdbea1 | 2024-08-10 19:26:04 +0000 | [diff] [blame] | 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 | |
Asmita Poddar | dd9a6cd | 2023-09-26 15:35:12 +0000 | [diff] [blame] | 137 | mTimelines.emplace(inputEventId, |
jioana | 97cc8ac | 2024-09-09 15:01:43 +0000 | [diff] [blame] | 138 | InputEventTimeline(eventTime, readTime, identifier->vendor, |
jioana | 0bdbea1 | 2024-08-10 19:26:04 +0000 | [diff] [blame] | 139 | identifier->product, sources, inputEventActionType)); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 140 | mEventTimes.emplace(eventTime, inputEventId); |
| 141 | } |
| 142 | |
| 143 | void 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 Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 148 | // 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 Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 150 | // 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 | |
| 174 | void 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 Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 179 | // 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 Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 181 | // 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 | */ |
| 206 | void LatencyTracker::reportAndPruneMatureRecords(nsecs_t newEventTime) { |
| 207 | while (!mEventTimes.empty()) { |
| 208 | const auto& [oldestEventTime, oldestInputEventId] = *mEventTimes.begin(); |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 209 | if (isMatureEvent(oldestEventTime, /*now=*/newEventTime)) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 210 | // 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 Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 226 | std::string LatencyTracker::dump(const char* prefix) const { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 227 | 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 Poddar | dd9a6cd | 2023-09-26 15:35:12 +0000 | [diff] [blame] | 232 | void LatencyTracker::setInputDevices(const std::vector<InputDeviceInfo>& inputDevices) { |
| 233 | mInputDevices = inputDevices; |
| 234 | } |
| 235 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 236 | } // namespace android::inputdispatcher |