xref: /aosp_15_r20/frameworks/native/services/inputflinger/dispatcher/AnrTracker.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
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 #include "AnrTracker.h"
18*38e8c45fSAndroid Build Coastguard Worker 
19*38e8c45fSAndroid Build Coastguard Worker namespace android::inputdispatcher {
20*38e8c45fSAndroid Build Coastguard Worker 
21*38e8c45fSAndroid Build Coastguard Worker template <typename T>
max(const T & a,const T & b)22*38e8c45fSAndroid Build Coastguard Worker static T max(const T& a, const T& b) {
23*38e8c45fSAndroid Build Coastguard Worker     return a < b ? b : a;
24*38e8c45fSAndroid Build Coastguard Worker }
25*38e8c45fSAndroid Build Coastguard Worker 
insert(nsecs_t timeoutTime,sp<IBinder> token)26*38e8c45fSAndroid Build Coastguard Worker void AnrTracker::insert(nsecs_t timeoutTime, sp<IBinder> token) {
27*38e8c45fSAndroid Build Coastguard Worker     mAnrTimeouts.insert(std::make_pair(timeoutTime, std::move(token)));
28*38e8c45fSAndroid Build Coastguard Worker }
29*38e8c45fSAndroid Build Coastguard Worker 
30*38e8c45fSAndroid Build Coastguard Worker /**
31*38e8c45fSAndroid Build Coastguard Worker  * Erase a single entry only. If there are multiple duplicate entries
32*38e8c45fSAndroid Build Coastguard Worker  * (same time, same connection), then only remove one of them.
33*38e8c45fSAndroid Build Coastguard Worker  */
erase(nsecs_t timeoutTime,const sp<IBinder> & token)34*38e8c45fSAndroid Build Coastguard Worker void AnrTracker::erase(nsecs_t timeoutTime, const sp<IBinder>& token) {
35*38e8c45fSAndroid Build Coastguard Worker     auto pair = std::make_pair(timeoutTime, token);
36*38e8c45fSAndroid Build Coastguard Worker     auto it = mAnrTimeouts.find(pair);
37*38e8c45fSAndroid Build Coastguard Worker     if (it != mAnrTimeouts.end()) {
38*38e8c45fSAndroid Build Coastguard Worker         mAnrTimeouts.erase(it);
39*38e8c45fSAndroid Build Coastguard Worker     }
40*38e8c45fSAndroid Build Coastguard Worker }
41*38e8c45fSAndroid Build Coastguard Worker 
eraseToken(const sp<IBinder> & token)42*38e8c45fSAndroid Build Coastguard Worker void AnrTracker::eraseToken(const sp<IBinder>& token) {
43*38e8c45fSAndroid Build Coastguard Worker     for (auto it = mAnrTimeouts.begin(); it != mAnrTimeouts.end();) {
44*38e8c45fSAndroid Build Coastguard Worker         if (it->second == token) {
45*38e8c45fSAndroid Build Coastguard Worker             it = mAnrTimeouts.erase(it);
46*38e8c45fSAndroid Build Coastguard Worker         } else {
47*38e8c45fSAndroid Build Coastguard Worker             ++it;
48*38e8c45fSAndroid Build Coastguard Worker         }
49*38e8c45fSAndroid Build Coastguard Worker     }
50*38e8c45fSAndroid Build Coastguard Worker }
51*38e8c45fSAndroid Build Coastguard Worker 
empty() const52*38e8c45fSAndroid Build Coastguard Worker bool AnrTracker::empty() const {
53*38e8c45fSAndroid Build Coastguard Worker     return mAnrTimeouts.empty();
54*38e8c45fSAndroid Build Coastguard Worker }
55*38e8c45fSAndroid Build Coastguard Worker 
56*38e8c45fSAndroid Build Coastguard Worker // If empty() is false, return the time at which the next connection should cause an ANR
57*38e8c45fSAndroid Build Coastguard Worker // If empty() is true, return LLONG_MAX
firstTimeout() const58*38e8c45fSAndroid Build Coastguard Worker nsecs_t AnrTracker::firstTimeout() const {
59*38e8c45fSAndroid Build Coastguard Worker     if (mAnrTimeouts.empty()) {
60*38e8c45fSAndroid Build Coastguard Worker         return std::numeric_limits<nsecs_t>::max();
61*38e8c45fSAndroid Build Coastguard Worker     }
62*38e8c45fSAndroid Build Coastguard Worker     return mAnrTimeouts.begin()->first;
63*38e8c45fSAndroid Build Coastguard Worker }
64*38e8c45fSAndroid Build Coastguard Worker 
firstToken() const65*38e8c45fSAndroid Build Coastguard Worker const sp<IBinder>& AnrTracker::firstToken() const {
66*38e8c45fSAndroid Build Coastguard Worker     return mAnrTimeouts.begin()->second;
67*38e8c45fSAndroid Build Coastguard Worker }
68*38e8c45fSAndroid Build Coastguard Worker 
clear()69*38e8c45fSAndroid Build Coastguard Worker void AnrTracker::clear() {
70*38e8c45fSAndroid Build Coastguard Worker     mAnrTimeouts.clear();
71*38e8c45fSAndroid Build Coastguard Worker }
72*38e8c45fSAndroid Build Coastguard Worker 
73*38e8c45fSAndroid Build Coastguard Worker } // namespace android::inputdispatcher
74