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