xref: /aosp_15_r20/frameworks/native/services/inputflinger/dispatcher/LatencyTracker.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright (C) 2021 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #pragma once
18*38e8c45fSAndroid Build Coastguard Worker 
19*38e8c45fSAndroid Build Coastguard Worker #include "../InputDeviceMetricsSource.h"
20*38e8c45fSAndroid Build Coastguard Worker 
21*38e8c45fSAndroid Build Coastguard Worker #include <map>
22*38e8c45fSAndroid Build Coastguard Worker #include <unordered_map>
23*38e8c45fSAndroid Build Coastguard Worker 
24*38e8c45fSAndroid Build Coastguard Worker #include <binder/IBinder.h>
25*38e8c45fSAndroid Build Coastguard Worker #include <input/Input.h>
26*38e8c45fSAndroid Build Coastguard Worker 
27*38e8c45fSAndroid Build Coastguard Worker #include "InputEventTimeline.h"
28*38e8c45fSAndroid Build Coastguard Worker #include "NotifyArgs.h"
29*38e8c45fSAndroid Build Coastguard Worker 
30*38e8c45fSAndroid Build Coastguard Worker namespace android::inputdispatcher {
31*38e8c45fSAndroid Build Coastguard Worker 
32*38e8c45fSAndroid Build Coastguard Worker /**
33*38e8c45fSAndroid Build Coastguard Worker  * Maintain a record for input events that are received by InputDispatcher, sent out to the apps,
34*38e8c45fSAndroid Build Coastguard Worker  * and processed by the apps. Once an event becomes "mature" (older than the ANR timeout), report
35*38e8c45fSAndroid Build Coastguard Worker  * the entire input event latency history to the reporting function.
36*38e8c45fSAndroid Build Coastguard Worker  *
37*38e8c45fSAndroid Build Coastguard Worker  * All calls to LatencyTracker should come from the same thread. It is not thread-safe.
38*38e8c45fSAndroid Build Coastguard Worker  */
39*38e8c45fSAndroid Build Coastguard Worker class LatencyTracker {
40*38e8c45fSAndroid Build Coastguard Worker public:
41*38e8c45fSAndroid Build Coastguard Worker     /**
42*38e8c45fSAndroid Build Coastguard Worker      * Create a LatencyTracker.
43*38e8c45fSAndroid Build Coastguard Worker      * param reportingFunction: the function that will be called in order to report full latency.
44*38e8c45fSAndroid Build Coastguard Worker      */
45*38e8c45fSAndroid Build Coastguard Worker     LatencyTracker(InputEventTimelineProcessor& processor);
46*38e8c45fSAndroid Build Coastguard Worker     /**
47*38e8c45fSAndroid Build Coastguard Worker      * Start keeping track of an event identified by the args. This must be called first.
48*38e8c45fSAndroid Build Coastguard Worker      * If duplicate events are encountered (events that have the same eventId), none of them will be
49*38e8c45fSAndroid Build Coastguard Worker      * tracked. This is because there is not enough information to correctly track them. It is
50*38e8c45fSAndroid Build Coastguard Worker      * always possible that two different events are generated with the same inputEventId and the
51*38e8c45fSAndroid Build Coastguard Worker      * same eventTime, so there aren't ways to distinguish those. Therefore, we must drop all
52*38e8c45fSAndroid Build Coastguard Worker      * duplicate data.
53*38e8c45fSAndroid Build Coastguard Worker      * For that reason, the APIs 'trackFinishedEvent' and 'trackGraphicsLatency' only receive the
54*38e8c45fSAndroid Build Coastguard Worker      * inputEventId as input.
55*38e8c45fSAndroid Build Coastguard Worker      */
56*38e8c45fSAndroid Build Coastguard Worker     void trackListener(const NotifyArgs& args);
57*38e8c45fSAndroid Build Coastguard Worker     void trackFinishedEvent(int32_t inputEventId, const sp<IBinder>& connectionToken,
58*38e8c45fSAndroid Build Coastguard Worker                             nsecs_t deliveryTime, nsecs_t consumeTime, nsecs_t finishTime);
59*38e8c45fSAndroid Build Coastguard Worker     void trackGraphicsLatency(int32_t inputEventId, const sp<IBinder>& connectionToken,
60*38e8c45fSAndroid Build Coastguard Worker                               std::array<nsecs_t, GraphicsTimeline::SIZE> timeline);
61*38e8c45fSAndroid Build Coastguard Worker 
62*38e8c45fSAndroid Build Coastguard Worker     std::string dump(const char* prefix) const;
63*38e8c45fSAndroid Build Coastguard Worker     void setInputDevices(const std::vector<InputDeviceInfo>& inputDevices);
64*38e8c45fSAndroid Build Coastguard Worker 
65*38e8c45fSAndroid Build Coastguard Worker private:
66*38e8c45fSAndroid Build Coastguard Worker     /**
67*38e8c45fSAndroid Build Coastguard Worker      * A collection of InputEventTimelines keyed by inputEventId. An InputEventTimeline is first
68*38e8c45fSAndroid Build Coastguard Worker      * created when 'trackListener' is called.
69*38e8c45fSAndroid Build Coastguard Worker      * When either 'trackFinishedEvent' or 'trackGraphicsLatency' is called for this input event,
70*38e8c45fSAndroid Build Coastguard Worker      * the corresponding InputEventTimeline will be updated for that token.
71*38e8c45fSAndroid Build Coastguard Worker      */
72*38e8c45fSAndroid Build Coastguard Worker     std::unordered_map<int32_t /*inputEventId*/, InputEventTimeline> mTimelines;
73*38e8c45fSAndroid Build Coastguard Worker     /**
74*38e8c45fSAndroid Build Coastguard Worker      * The collection of eventTimes will help us quickly find the events that we should prune
75*38e8c45fSAndroid Build Coastguard Worker      * from the 'mTimelines'. Since 'mTimelines' is keyed by inputEventId, it would be inefficient
76*38e8c45fSAndroid Build Coastguard Worker      * to walk through it directly to find the oldest input events to get rid of.
77*38e8c45fSAndroid Build Coastguard Worker      * There is a 1:1 mapping between 'mTimelines' and 'mEventTimes'.
78*38e8c45fSAndroid Build Coastguard Worker      * We are using 'multimap' instead of 'map' because there could be more than 1 event with the
79*38e8c45fSAndroid Build Coastguard Worker      * same eventTime.
80*38e8c45fSAndroid Build Coastguard Worker      */
81*38e8c45fSAndroid Build Coastguard Worker     std::multimap<nsecs_t /*eventTime*/, int32_t /*inputEventId*/> mEventTimes;
82*38e8c45fSAndroid Build Coastguard Worker 
83*38e8c45fSAndroid Build Coastguard Worker     InputEventTimelineProcessor* mTimelineProcessor;
84*38e8c45fSAndroid Build Coastguard Worker     std::vector<InputDeviceInfo> mInputDevices;
85*38e8c45fSAndroid Build Coastguard Worker 
86*38e8c45fSAndroid Build Coastguard Worker     void trackListener(int32_t inputEventId, nsecs_t eventTime, nsecs_t readTime, DeviceId deviceId,
87*38e8c45fSAndroid Build Coastguard Worker                        const std::set<InputDeviceUsageSource>& sources, int32_t inputEventAction,
88*38e8c45fSAndroid Build Coastguard Worker                        InputEventType inputEventType);
89*38e8c45fSAndroid Build Coastguard Worker     void reportAndPruneMatureRecords(nsecs_t newEventTime);
90*38e8c45fSAndroid Build Coastguard Worker };
91*38e8c45fSAndroid Build Coastguard Worker 
92*38e8c45fSAndroid Build Coastguard Worker } // namespace android::inputdispatcher
93