Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 | |
| 17 | #pragma once |
| 18 | |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 19 | #include <layerproto/LayerProtoHeader.h> |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 20 | |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame^] | 21 | #include <atomic> |
| 22 | #include <functional> |
| 23 | #include <optional> |
| 24 | #include <ostream> |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame^] | 28 | class TransactionTracing; |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 29 | |
| 30 | /* |
| 31 | * LayerTracing records layer states during surface flinging. Manages tracing state and |
| 32 | * configuration. |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame^] | 33 | * |
| 34 | * The traced data can then be collected with Perfetto. |
| 35 | * |
| 36 | * The Perfetto custom data source LayerDataSource is registered with perfetto. The data source |
| 37 | * is used to listen to perfetto events (setup, start, stop, flush) and to write trace packets |
| 38 | * to perfetto. |
| 39 | * |
| 40 | * The user can configure/start/stop tracing via /system/bin/perfetto. |
| 41 | * |
| 42 | * Tracing can operate in the following modes. |
| 43 | * |
| 44 | * ACTIVE mode: |
| 45 | * A layers snapshot is taken and written to perfetto for each vsyncid commit. |
| 46 | * |
| 47 | * GENERATED mode: |
| 48 | * Listens to the perfetto 'flush' event (e.g. when a bugreport is taken). |
| 49 | * When a 'flush' event is received, the ring buffer of transactions (hold by TransactionTracing) |
| 50 | * is processed by LayerTraceGenerator, a sequence of layers snapshots is generated |
| 51 | * and written to perfetto. |
| 52 | * |
| 53 | * DUMP mode: |
| 54 | * When the 'start' event is received a single layers snapshot is taken |
| 55 | * and written to perfetto. |
| 56 | * |
| 57 | * |
| 58 | * E.g. start active mode tracing: |
| 59 | * |
| 60 | adb shell -t perfetto \ |
| 61 | -c - --txt \ |
| 62 | -o /data/misc/perfetto-traces/trace \ |
| 63 | <<EOF |
| 64 | unique_session_name: "surfaceflinger_layers_active" |
| 65 | buffers: { |
| 66 | size_kb: 63488 |
| 67 | fill_policy: RING_BUFFER |
| 68 | } |
| 69 | data_sources: { |
| 70 | config { |
| 71 | name: "android.surfaceflinger.layers" |
| 72 | surfaceflinger_layers_config: { |
| 73 | mode: MODE_ACTIVE |
| 74 | trace_flags: TRACE_FLAG_INPUT |
| 75 | trace_flags: TRACE_FLAG_COMPOSITION |
| 76 | trace_flags: TRACE_FLAG_HWC |
| 77 | trace_flags: TRACE_FLAG_BUFFERS |
| 78 | trace_flags: TRACE_FLAG_VIRTUAL_DISPLAYS |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | EOF |
| 83 | * |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 84 | */ |
| 85 | class LayerTracing { |
| 86 | public: |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame^] | 87 | using Mode = perfetto::protos::pbzero::SurfaceFlingerLayersConfig::Mode; |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 88 | |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame^] | 89 | enum Flag : uint32_t { |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 90 | TRACE_INPUT = 1 << 1, |
| 91 | TRACE_COMPOSITION = 1 << 2, |
| 92 | TRACE_EXTRA = 1 << 3, |
| 93 | TRACE_HWC = 1 << 4, |
| 94 | TRACE_BUFFERS = 1 << 5, |
Nataniel Borges | 63b3c87 | 2022-11-03 16:22:24 +0000 | [diff] [blame] | 95 | TRACE_VIRTUAL_DISPLAYS = 1 << 6, |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 96 | TRACE_ALL = TRACE_INPUT | TRACE_COMPOSITION | TRACE_EXTRA, |
| 97 | }; |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame^] | 98 | |
| 99 | LayerTracing(); |
| 100 | ~LayerTracing(); |
| 101 | void setTakeLayersSnapshotProtoFunction( |
| 102 | const std::function<perfetto::protos::LayersSnapshotProto(uint32_t)>&); |
| 103 | void setTransactionTracing(TransactionTracing&); |
| 104 | void setOutputStream(std::ostream&); |
| 105 | |
| 106 | // Start event from perfetto data source |
| 107 | void onStart(Mode mode, uint32_t flags); |
| 108 | // Flush event from perfetto data source |
| 109 | void onFlush(Mode mode, uint32_t flags); |
| 110 | // Stop event from perfetto data source |
| 111 | void onStop(Mode mode); |
| 112 | |
| 113 | void addProtoSnapshotToOstream(perfetto::protos::LayersSnapshotProto&& snapshot, Mode mode); |
| 114 | bool isActiveTracingStarted() const; |
| 115 | uint32_t getActiveTracingFlags() const; |
| 116 | bool isActiveTracingFlagSet(Flag flag) const; |
| 117 | static perfetto::protos::LayersTraceFileProto createTraceFileProto(); |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 118 | |
| 119 | private: |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame^] | 120 | void writeSnapshotToStream(perfetto::protos::LayersSnapshotProto&& snapshot) const; |
| 121 | void writeSnapshotToPerfetto(const perfetto::protos::LayersSnapshotProto& snapshot, Mode mode); |
| 122 | bool checkAndUpdateLastVsyncIdWrittenToPerfetto(Mode mode, std::int64_t vsyncId); |
| 123 | |
| 124 | std::function<perfetto::protos::LayersSnapshotProto(uint32_t)> mTakeLayersSnapshotProto; |
| 125 | TransactionTracing* mTransactionTracing; |
| 126 | |
| 127 | std::atomic<bool> mIsActiveTracingStarted{false}; |
| 128 | std::atomic<uint32_t> mActiveTracingFlags{0}; |
| 129 | std::atomic<std::int64_t> mLastVsyncIdWrittenToPerfetto{-1}; |
| 130 | std::optional<std::reference_wrapper<std::ostream>> mOutStream; |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | } // namespace android |