1*38e8c45fSAndroid Build Coastguard Worker /* 2*38e8c45fSAndroid Build Coastguard Worker * Copyright (C) 2020 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 <binder/IBinder.h> 20*38e8c45fSAndroid Build Coastguard Worker #include <utils/Timers.h> 21*38e8c45fSAndroid Build Coastguard Worker #include <set> 22*38e8c45fSAndroid Build Coastguard Worker 23*38e8c45fSAndroid Build Coastguard Worker namespace android::inputdispatcher { 24*38e8c45fSAndroid Build Coastguard Worker 25*38e8c45fSAndroid Build Coastguard Worker /** 26*38e8c45fSAndroid Build Coastguard Worker * Keeps track of the times when each connection is going to ANR. 27*38e8c45fSAndroid Build Coastguard Worker * Provides the ability to quickly find the connection that is going to cause ANR next. 28*38e8c45fSAndroid Build Coastguard Worker */ 29*38e8c45fSAndroid Build Coastguard Worker class AnrTracker { 30*38e8c45fSAndroid Build Coastguard Worker public: 31*38e8c45fSAndroid Build Coastguard Worker void insert(nsecs_t timeoutTime, sp<IBinder> token); 32*38e8c45fSAndroid Build Coastguard Worker void erase(nsecs_t timeoutTime, const sp<IBinder>& token); 33*38e8c45fSAndroid Build Coastguard Worker void eraseToken(const sp<IBinder>& token); 34*38e8c45fSAndroid Build Coastguard Worker void clear(); 35*38e8c45fSAndroid Build Coastguard Worker 36*38e8c45fSAndroid Build Coastguard Worker bool empty() const; 37*38e8c45fSAndroid Build Coastguard Worker // If empty() is false, return the time at which the next connection should cause an ANR 38*38e8c45fSAndroid Build Coastguard Worker // If empty() is true, return LLONG_MAX 39*38e8c45fSAndroid Build Coastguard Worker nsecs_t firstTimeout() const; 40*38e8c45fSAndroid Build Coastguard Worker // Return the token of the next connection that should cause an ANR. 41*38e8c45fSAndroid Build Coastguard Worker // Do not call this unless empty() is false, you will encounter undefined behaviour. 42*38e8c45fSAndroid Build Coastguard Worker const sp<IBinder>& firstToken() const; 43*38e8c45fSAndroid Build Coastguard Worker 44*38e8c45fSAndroid Build Coastguard Worker private: 45*38e8c45fSAndroid Build Coastguard Worker // Optimization: use a multiset to keep track of the event timeouts. When an event is sent 46*38e8c45fSAndroid Build Coastguard Worker // to the InputConsumer, we add an entry to this structure. We look at the smallest value to 47*38e8c45fSAndroid Build Coastguard Worker // determine if any of the connections is unresponsive, and to determine when we should wake 48*38e8c45fSAndroid Build Coastguard Worker // next for the future ANR check. 49*38e8c45fSAndroid Build Coastguard Worker // Using a multiset helps quickly look up the next timeout due. 50*38e8c45fSAndroid Build Coastguard Worker // 51*38e8c45fSAndroid Build Coastguard Worker // We must use a multi-set, because it is plausible (although highly unlikely) to have entries 52*38e8c45fSAndroid Build Coastguard Worker // from the same connection and same timestamp, but different sequence numbers. 53*38e8c45fSAndroid Build Coastguard Worker // We are not tracking sequence numbers, and just allow duplicates to exist. 54*38e8c45fSAndroid Build Coastguard Worker std::multiset<std::pair<nsecs_t /*timeoutTime*/, sp<IBinder> /*connectionToken*/>> mAnrTimeouts; 55*38e8c45fSAndroid Build Coastguard Worker }; 56*38e8c45fSAndroid Build Coastguard Worker 57*38e8c45fSAndroid Build Coastguard Worker } // namespace android::inputdispatcher 58