Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 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 | |
Prabir Pradhan | 4810866 | 2022-09-09 21:22:04 +0000 | [diff] [blame] | 17 | #pragma once |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 18 | |
Siarhei Vishniakou | ad16bf2 | 2023-09-05 18:24:44 -0700 | [diff] [blame] | 19 | #include <android-base/thread_annotations.h> |
Siarhei Vishniakou | 99b9d38 | 2021-04-01 08:03:41 +0000 | [diff] [blame] | 20 | #include <kll.h> |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 21 | #include <statslog.h> |
| 22 | #include <utils/Timers.h> |
| 23 | |
| 24 | #include "InputEventTimeline.h" |
| 25 | |
| 26 | namespace android::inputdispatcher { |
| 27 | |
| 28 | enum SketchIndex : size_t { |
| 29 | EVENT_TO_READ = 0, |
| 30 | READ_TO_DELIVER = 1, |
| 31 | DELIVER_TO_CONSUME = 2, |
| 32 | CONSUME_TO_FINISH = 3, |
| 33 | CONSUME_TO_GPU_COMPLETE = 4, |
| 34 | GPU_COMPLETE_TO_PRESENT = 5, |
| 35 | END_TO_END = 6, // EVENT_TO_PRESENT |
| 36 | SIZE = 7, // Must be last |
| 37 | }; |
| 38 | |
| 39 | // Let's create a full timeline here: |
| 40 | // eventTime |
| 41 | // readTime |
| 42 | // <---- after this point, the data becomes per-connection |
| 43 | // deliveryTime // time at which the event was sent to the receiver |
| 44 | // consumeTime // time at which the receiver read the event |
| 45 | // finishTime // time at which the finish event was received |
| 46 | // GraphicsTimeline::GPU_COMPLETED_TIME |
| 47 | // GraphicsTimeline::PRESENT_TIME |
| 48 | |
| 49 | /** |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 50 | * Keep sketches of the provided events and report slow events |
| 51 | */ |
| 52 | class LatencyAggregator final : public InputEventTimelineProcessor { |
| 53 | public: |
| 54 | LatencyAggregator(); |
| 55 | /** |
| 56 | * Record a complete event timeline |
| 57 | */ |
| 58 | void processTimeline(const InputEventTimeline& timeline) override; |
| 59 | |
Siarhei Vishniakou | 4c9d6ff | 2023-04-18 11:23:20 -0700 | [diff] [blame] | 60 | std::string dump(const char* prefix) const; |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 61 | |
| 62 | ~LatencyAggregator(); |
| 63 | |
| 64 | private: |
Siarhei Vishniakou | ad16bf2 | 2023-09-05 18:24:44 -0700 | [diff] [blame] | 65 | // Binder call -- called on a binder thread. This is different from the thread where the rest of |
| 66 | // the public API is called. |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 67 | static AStatsManager_PullAtomCallbackReturn pullAtomCallback(int32_t atom_tag, |
| 68 | AStatsEventList* data, |
| 69 | void* cookie); |
| 70 | AStatsManager_PullAtomCallbackReturn pullData(AStatsEventList* data); |
Siarhei Vishniakou | ad16bf2 | 2023-09-05 18:24:44 -0700 | [diff] [blame] | 71 | |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 72 | // ---------- Slow event handling ---------- |
| 73 | void processSlowEvent(const InputEventTimeline& timeline); |
| 74 | nsecs_t mLastSlowEventTime = 0; |
| 75 | // How many slow events have been skipped due to rate limiting |
| 76 | size_t mNumSkippedSlowEvents = 0; |
| 77 | // How many events have been received since the last time we reported a slow event |
| 78 | size_t mNumEventsSinceLastSlowEventReport = 0; |
| 79 | |
| 80 | // ---------- Statistics handling ---------- |
Siarhei Vishniakou | ad16bf2 | 2023-09-05 18:24:44 -0700 | [diff] [blame] | 81 | // Statistics is pulled rather than pushed. It's pulled on a binder thread, and therefore will |
| 82 | // be accessed by two different threads. The lock is needed to protect the pulled data. |
| 83 | mutable std::mutex mLock; |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 84 | void processStatistics(const InputEventTimeline& timeline); |
| 85 | // Sketches |
Siarhei Vishniakou | 99b9d38 | 2021-04-01 08:03:41 +0000 | [diff] [blame] | 86 | std::array<std::unique_ptr<dist_proc::aggregation::KllQuantile>, SketchIndex::SIZE> |
Siarhei Vishniakou | ad16bf2 | 2023-09-05 18:24:44 -0700 | [diff] [blame] | 87 | mDownSketches GUARDED_BY(mLock); |
Siarhei Vishniakou | 99b9d38 | 2021-04-01 08:03:41 +0000 | [diff] [blame] | 88 | std::array<std::unique_ptr<dist_proc::aggregation::KllQuantile>, SketchIndex::SIZE> |
Siarhei Vishniakou | ad16bf2 | 2023-09-05 18:24:44 -0700 | [diff] [blame] | 89 | mMoveSketches GUARDED_BY(mLock); |
Siarhei Vishniakou | 99b9d38 | 2021-04-01 08:03:41 +0000 | [diff] [blame] | 90 | // How many events have been processed so far |
Siarhei Vishniakou | ad16bf2 | 2023-09-05 18:24:44 -0700 | [diff] [blame] | 91 | size_t mNumSketchEventsProcessed GUARDED_BY(mLock) = 0; |
Siarhei Vishniakou | a04181f | 2021-03-26 05:56:49 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | } // namespace android::inputdispatcher |