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