summaryrefslogtreecommitdiff
path: root/services/inputflinger/dispatcher/LatencyTracker.h
diff options
context:
space:
mode:
Diffstat (limited to 'services/inputflinger/dispatcher/LatencyTracker.h')
-rw-r--r--services/inputflinger/dispatcher/LatencyTracker.h87
1 files changed, 87 insertions, 0 deletions
diff --git a/services/inputflinger/dispatcher/LatencyTracker.h b/services/inputflinger/dispatcher/LatencyTracker.h
new file mode 100644
index 0000000000..289b8ed6c4
--- /dev/null
+++ b/services/inputflinger/dispatcher/LatencyTracker.h
@@ -0,0 +1,87 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _UI_INPUT_INPUTDISPATCHER_LATENCYTRACKER_H
+#define _UI_INPUT_INPUTDISPATCHER_LATENCYTRACKER_H
+
+#include <map>
+#include <unordered_map>
+
+#include <binder/IBinder.h>
+#include <input/Input.h>
+
+#include "InputEventTimeline.h"
+
+namespace android::inputdispatcher {
+
+/**
+ * Maintain a record for input events that are received by InputDispatcher, sent out to the apps,
+ * and processed by the apps. Once an event becomes "mature" (older than the ANR timeout), report
+ * the entire input event latency history to the reporting function.
+ *
+ * All calls to LatencyTracker should come from the same thread. It is not thread-safe.
+ */
+class LatencyTracker {
+public:
+ /**
+ * Create a LatencyTracker.
+ * param reportingFunction: the function that will be called in order to report full latency.
+ */
+ LatencyTracker(InputEventTimelineProcessor* processor);
+ /**
+ * Start keeping track of an event identified by inputEventId. This must be called first.
+ */
+ void trackListener(int32_t inputEventId, bool isDown, nsecs_t eventTime, nsecs_t readTime);
+ void trackFinishedEvent(int32_t inputEventId, const sp<IBinder>& connectionToken,
+ nsecs_t deliveryTime, nsecs_t consumeTime, nsecs_t finishTime);
+ void trackGraphicsLatency(int32_t inputEventId, const sp<IBinder>& connectionToken,
+ std::array<nsecs_t, GraphicsTimeline::SIZE> timeline);
+
+ /**
+ * Report all collected events immediately, even if some of them are currently incomplete
+ * and may receive 'trackFinishedEvent' or 'trackGraphicsLatency' calls in the future.
+ * This is useful for tests. Otherwise, tests would have to inject additional "future" events,
+ * which is not convenient.
+ */
+ void reportNow();
+
+ std::string dump(const char* prefix);
+
+private:
+ /**
+ * A collection of InputEventTimelines keyed by inputEventId. An InputEventTimeline is first
+ * created when 'trackListener' is called.
+ * When either 'trackFinishedEvent' or 'trackGraphicsLatency' is called for this input event,
+ * the corresponding InputEventTimeline will be updated for that token.
+ */
+ std::unordered_map<int32_t /*inputEventId*/, InputEventTimeline> mTimelines;
+ /**
+ * The collection of eventTimes will help us quickly find the events that we should prune
+ * from the 'mTimelines'. Since 'mTimelines' is keyed by inputEventId, it would be inefficient
+ * to walk through it directly to find the oldest input events to get rid of.
+ * There is a 1:1 mapping between 'mTimelines' and 'mEventTimes'.
+ * We are using 'multimap' instead of 'map' because there could be more than 1 event with the
+ * same eventTime.
+ */
+ std::multimap<nsecs_t /*eventTime*/, int32_t /*inputEventId*/> mEventTimes;
+
+ InputEventTimelineProcessor* mTimelineProcessor;
+ void reportAndPruneMatureRecords(nsecs_t newEventTime);
+};
+
+} // namespace android::inputdispatcher
+
+#endif // _UI_INPUT_INPUTDISPATCHER_LATENCYTRACKER_H