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" |
| 19 | |
| 20 | #include <inttypes.h> |
| 21 | |
| 22 | #include <android-base/properties.h> |
| 23 | #include <android-base/stringprintf.h> |
| 24 | #include <android/os/IInputConstants.h> |
| 25 | #include <input/Input.h> |
| 26 | #include <log/log.h> |
| 27 | |
| 28 | using android::base::HwTimeoutMultiplier; |
| 29 | using android::base::StringPrintf; |
| 30 | |
| 31 | namespace android::inputdispatcher { |
| 32 | |
| 33 | /** |
| 34 | * Events that are older than this time will be considered mature, at which point we will stop |
| 35 | * waiting for the apps to provide further information about them. |
| 36 | * It's likely that the apps will ANR if the events are not received by this deadline, and we |
| 37 | * already track ANR metrics separately. |
| 38 | */ |
| 39 | const std::chrono::duration ANR_TIMEOUT = std::chrono::milliseconds( |
| 40 | android::os::IInputConstants::UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS * |
| 41 | HwTimeoutMultiplier()); |
| 42 | |
| 43 | static bool isMatureEvent(nsecs_t eventTime, nsecs_t now) { |
| 44 | std::chrono::duration age = std::chrono::nanoseconds(now) - std::chrono::nanoseconds(eventTime); |
| 45 | return age > ANR_TIMEOUT; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * A multimap allows to have several entries with the same key. This function just erases a specific |
| 50 | * key-value pair. Equivalent to the imaginary std api std::multimap::erase(key, value). |
| 51 | */ |
| 52 | template <typename K, typename V> |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 53 | static void eraseByValue(std::multimap<K, V>& map, const V& value) { |
| 54 | for (auto it = map.begin(); it != map.end();) { |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 55 | if (it->second == value) { |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 56 | it = map.erase(it); |
| 57 | } else { |
| 58 | it++; |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | LatencyTracker::LatencyTracker(InputEventTimelineProcessor* processor) |
| 64 | : mTimelineProcessor(processor) { |
| 65 | LOG_ALWAYS_FATAL_IF(processor == nullptr); |
| 66 | } |
| 67 | |
| 68 | void LatencyTracker::trackListener(int32_t inputEventId, bool isDown, nsecs_t eventTime, |
| 69 | nsecs_t readTime) { |
| 70 | reportAndPruneMatureRecords(eventTime); |
| 71 | const auto it = mTimelines.find(inputEventId); |
| 72 | if (it != mTimelines.end()) { |
| 73 | // Input event ids are randomly generated, so it's possible that two events have the same |
| 74 | // event id. Drop this event, and also drop the existing event because the apps would |
| 75 | // confuse us by reporting the rest of the timeline for one of them. This should happen |
| 76 | // rarely, so we won't lose much data |
| 77 | mTimelines.erase(it); |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 78 | eraseByValue(mEventTimes, inputEventId); |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 79 | return; |
| 80 | } |
| 81 | mTimelines.emplace(inputEventId, InputEventTimeline(isDown, eventTime, readTime)); |
| 82 | mEventTimes.emplace(eventTime, inputEventId); |
| 83 | } |
| 84 | |
| 85 | void LatencyTracker::trackFinishedEvent(int32_t inputEventId, const sp<IBinder>& connectionToken, |
| 86 | nsecs_t deliveryTime, nsecs_t consumeTime, |
| 87 | nsecs_t finishTime) { |
| 88 | const auto it = mTimelines.find(inputEventId); |
| 89 | if (it == mTimelines.end()) { |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 90 | // This could happen if we erased this event when duplicate events were detected. It's |
| 91 | // 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] | 92 | // anything in its process. Just drop the report and move on. |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | InputEventTimeline& timeline = it->second; |
| 97 | const auto connectionIt = timeline.connectionTimelines.find(connectionToken); |
| 98 | if (connectionIt == timeline.connectionTimelines.end()) { |
| 99 | // Most likely case: app calls 'finishInputEvent' before it reports the graphics timeline |
| 100 | timeline.connectionTimelines.emplace(connectionToken, |
| 101 | ConnectionTimeline{deliveryTime, consumeTime, |
| 102 | finishTime}); |
| 103 | } else { |
| 104 | // Already have a record for this connectionToken |
| 105 | ConnectionTimeline& connectionTimeline = connectionIt->second; |
| 106 | const bool success = |
| 107 | connectionTimeline.setDispatchTimeline(deliveryTime, consumeTime, finishTime); |
| 108 | if (!success) { |
| 109 | // We are receiving unreliable data from the app. Just delete the entire connection |
| 110 | // timeline for this event |
| 111 | timeline.connectionTimelines.erase(connectionIt); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | void LatencyTracker::trackGraphicsLatency( |
| 117 | int32_t inputEventId, const sp<IBinder>& connectionToken, |
| 118 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline) { |
| 119 | const auto it = mTimelines.find(inputEventId); |
| 120 | if (it == mTimelines.end()) { |
Siarhei Vishniakou | 363e729 | 2021-07-09 03:22:42 +0000 | [diff] [blame] | 121 | // This could happen if we erased this event when duplicate events were detected. It's |
| 122 | // 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] | 123 | // anything in its process. Just drop the report and move on. |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | InputEventTimeline& timeline = it->second; |
| 128 | const auto connectionIt = timeline.connectionTimelines.find(connectionToken); |
| 129 | if (connectionIt == timeline.connectionTimelines.end()) { |
| 130 | timeline.connectionTimelines.emplace(connectionToken, std::move(graphicsTimeline)); |
| 131 | } else { |
| 132 | // Most likely case |
| 133 | ConnectionTimeline& connectionTimeline = connectionIt->second; |
| 134 | const bool success = connectionTimeline.setGraphicsTimeline(std::move(graphicsTimeline)); |
| 135 | if (!success) { |
| 136 | // We are receiving unreliable data from the app. Just delete the entire connection |
| 137 | // timeline for this event |
| 138 | timeline.connectionTimelines.erase(connectionIt); |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * We should use the current time 'now()' here to determine the age of the event, but instead we |
| 145 | * are using the latest 'eventTime' for efficiency since this time is already acquired, and |
| 146 | * 'trackListener' should happen soon after the event occurs. |
| 147 | */ |
| 148 | void LatencyTracker::reportAndPruneMatureRecords(nsecs_t newEventTime) { |
| 149 | while (!mEventTimes.empty()) { |
| 150 | const auto& [oldestEventTime, oldestInputEventId] = *mEventTimes.begin(); |
| 151 | if (isMatureEvent(oldestEventTime, newEventTime /*now*/)) { |
| 152 | // Report and drop this event |
| 153 | const auto it = mTimelines.find(oldestInputEventId); |
| 154 | LOG_ALWAYS_FATAL_IF(it == mTimelines.end(), |
| 155 | "Event %" PRId32 " is in mEventTimes, but not in mTimelines", |
| 156 | oldestInputEventId); |
| 157 | const InputEventTimeline& timeline = it->second; |
| 158 | mTimelineProcessor->processTimeline(timeline); |
| 159 | mTimelines.erase(it); |
| 160 | mEventTimes.erase(mEventTimes.begin()); |
| 161 | } else { |
| 162 | // If the oldest event does not need to be pruned, no events should be pruned. |
| 163 | return; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
Siarhei Vishniakou | f265212 | 2021-03-05 21:39:46 +0000 | [diff] [blame] | 168 | std::string LatencyTracker::dump(const char* prefix) { |
| 169 | return StringPrintf("%sLatencyTracker:\n", prefix) + |
| 170 | StringPrintf("%s mTimelines.size() = %zu\n", prefix, mTimelines.size()) + |
| 171 | StringPrintf("%s mEventTimes.size() = %zu\n", prefix, mEventTimes.size()); |
| 172 | } |
| 173 | |
| 174 | } // namespace android::inputdispatcher |