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 #ifndef _UI_INPUT_INPUTDISPATCHER_LATENCYTRACKER_H
18 #define _UI_INPUT_INPUTDISPATCHER_LATENCYTRACKER_H
19 
20 #include <map>
21 #include <unordered_map>
22 
23 #include <binder/IBinder.h>
24 #include <input/Input.h>
25 
26 #include "InputEventTimeline.h"
27 
28 namespace android::inputdispatcher {
29 
30 /**
31  * Maintain a record for input events that are received by InputDispatcher, sent out to the apps,
32  * and processed by the apps. Once an event becomes "mature" (older than the ANR timeout), report
33  * the entire input event latency history to the reporting function.
34  *
35  * All calls to LatencyTracker should come from the same thread. It is not thread-safe.
36  */
37 class LatencyTracker {
38 public:
39     /**
40      * Create a LatencyTracker.
41      * param reportingFunction: the function that will be called in order to report full latency.
42      */
43     LatencyTracker(InputEventTimelineProcessor* processor);
44     /**
45      * Start keeping track of an event identified by inputEventId. This must be called first.
46      */
47     void trackListener(int32_t inputEventId, bool isDown, nsecs_t eventTime, nsecs_t readTime);
48     void trackFinishedEvent(int32_t inputEventId, const sp<IBinder>& connectionToken,
49                             nsecs_t deliveryTime, nsecs_t consumeTime, nsecs_t finishTime);
50     void trackGraphicsLatency(int32_t inputEventId, const sp<IBinder>& connectionToken,
51                               std::array<nsecs_t, GraphicsTimeline::SIZE> timeline);
52 
53     /**
54      * Report all collected events immediately, even if some of them are currently incomplete
55      * and may receive 'trackFinishedEvent' or 'trackGraphicsLatency' calls in the future.
56      * This is useful for tests. Otherwise, tests would have to inject additional "future" events,
57      * which is not convenient.
58      */
59     void reportNow();
60 
61     std::string dump(const char* prefix);
62 
63 private:
64     /**
65      * A collection of InputEventTimelines keyed by inputEventId. An InputEventTimeline is first
66      * created when 'trackListener' is called.
67      * When either 'trackFinishedEvent' or 'trackGraphicsLatency' is called for this input event,
68      * the corresponding InputEventTimeline will be updated for that token.
69      */
70     std::unordered_map<int32_t /*inputEventId*/, InputEventTimeline> mTimelines;
71     /**
72      * The collection of eventTimes will help us quickly find the events that we should prune
73      * from the 'mTimelines'. Since 'mTimelines' is keyed by inputEventId, it would be inefficient
74      * to walk through it directly to find the oldest input events to get rid of.
75      * There is a 1:1 mapping between 'mTimelines' and 'mEventTimes'.
76      * We are using 'multimap' instead of 'map' because there could be more than 1 event with the
77      * same eventTime.
78      */
79     std::multimap<nsecs_t /*eventTime*/, int32_t /*inputEventId*/> mEventTimes;
80 
81     InputEventTimelineProcessor* mTimelineProcessor;
82     void reportAndPruneMatureRecords(nsecs_t newEventTime);
83 };
84 
85 } // namespace android::inputdispatcher
86 
87 #endif // _UI_INPUT_INPUTDISPATCHER_LATENCYTRACKER_H
88