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 | #undef LOG_TAG |
| 18 | #define LOG_TAG "LayerTracing" |
| 19 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 20 | |
Diwas Sharma | 6975834 | 2023-09-01 00:40:19 +0000 | [diff] [blame] | 21 | #include "LayerTracing.h" |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 22 | |
| 23 | #include "LayerDataSource.h" |
| 24 | #include "Tracing/tools/LayerTraceGenerator.h" |
| 25 | #include "TransactionTracing.h" |
| 26 | |
| 27 | #include <log/log.h> |
| 28 | #include <perfetto/tracing.h> |
| 29 | #include <utils/Timers.h> |
| 30 | #include <utils/Trace.h> |
Diwas Sharma | 6975834 | 2023-09-01 00:40:19 +0000 | [diff] [blame] | 31 | |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 32 | namespace android { |
| 33 | |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 34 | LayerTracing::LayerTracing() { |
| 35 | mTakeLayersSnapshotProto = [](uint32_t) { return perfetto::protos::LayersSnapshotProto{}; }; |
| 36 | LayerDataSource::Initialize(*this); |
John Reck | 2a3d29d | 2023-08-17 17:45:01 -0400 | [diff] [blame] | 37 | } |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 38 | |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 39 | LayerTracing::~LayerTracing() { |
| 40 | LayerDataSource::UnregisterLayerTracing(); |
| 41 | } |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 42 | |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 43 | void LayerTracing::setTakeLayersSnapshotProtoFunction( |
| 44 | const std::function<perfetto::protos::LayersSnapshotProto(uint32_t)>& callback) { |
| 45 | mTakeLayersSnapshotProto = callback; |
| 46 | } |
| 47 | |
| 48 | void LayerTracing::setTransactionTracing(TransactionTracing& transactionTracing) { |
| 49 | mTransactionTracing = &transactionTracing; |
| 50 | } |
| 51 | |
| 52 | void LayerTracing::setOutputStream(std::ostream& outStream) { |
| 53 | mOutStream = std::ref(outStream); |
| 54 | } |
| 55 | |
| 56 | void LayerTracing::onStart(Mode mode, uint32_t flags) { |
| 57 | switch (mode) { |
| 58 | case Mode::MODE_ACTIVE: { |
| 59 | mActiveTracingFlags.store(flags); |
| 60 | mIsActiveTracingStarted.store(true); |
| 61 | ALOGV("Starting active tracing (waiting for initial snapshot)"); |
| 62 | // It might take a while before a layers change occurs and a "spontaneous" snapshot is |
| 63 | // taken. Let's manually take a snapshot, so that the trace's first entry will contain |
| 64 | // the current layers state. |
| 65 | addProtoSnapshotToOstream(mTakeLayersSnapshotProto(flags), Mode::MODE_ACTIVE); |
Kean Mariotti | 8c5abc8 | 2023-09-20 09:28:07 +0000 | [diff] [blame^] | 66 | ALOGD("Started active tracing (traced initial snapshot)"); |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 67 | break; |
| 68 | } |
| 69 | case Mode::MODE_GENERATED: { |
Kean Mariotti | 8c5abc8 | 2023-09-20 09:28:07 +0000 | [diff] [blame^] | 70 | ALOGD("Started generated tracing (waiting for OnFlush event to generated layers)"); |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 71 | break; |
| 72 | } |
| 73 | case Mode::MODE_DUMP: { |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 74 | auto snapshot = mTakeLayersSnapshotProto(flags); |
| 75 | addProtoSnapshotToOstream(std::move(snapshot), Mode::MODE_DUMP); |
Kean Mariotti | 8c5abc8 | 2023-09-20 09:28:07 +0000 | [diff] [blame^] | 76 | ALOGD("Started dump tracing (dumped single snapshot)"); |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 77 | break; |
| 78 | } |
| 79 | default: { |
| 80 | ALOGE("Started unknown tracing mode (0x%02x)", mode); |
| 81 | } |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 82 | } |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 85 | void LayerTracing::onFlush(Mode mode, uint32_t flags) { |
| 86 | // In "generated" mode process the buffer of transactions (owned by TransactionTracing), |
| 87 | // generate a sequence of layers snapshots and write them to perfetto. |
| 88 | if (mode != Mode::MODE_GENERATED) { |
| 89 | return; |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 90 | } |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 91 | |
| 92 | if (!mTransactionTracing) { |
Kean Mariotti | 8c5abc8 | 2023-09-20 09:28:07 +0000 | [diff] [blame^] | 93 | ALOGD("Skipping layers trace generation (transactions tracing disabled)"); |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 94 | return; |
Vishnu Nair | cb56533 | 2023-03-14 21:10:55 -0700 | [diff] [blame] | 95 | } |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 96 | |
| 97 | auto transactionTrace = mTransactionTracing->writeToProto(); |
| 98 | LayerTraceGenerator{}.generate(transactionTrace, flags); |
Kean Mariotti | 8c5abc8 | 2023-09-20 09:28:07 +0000 | [diff] [blame^] | 99 | ALOGD("Flushed generated tracing"); |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 102 | void LayerTracing::onStop(Mode mode) { |
| 103 | if (mode == Mode::MODE_ACTIVE) { |
| 104 | mIsActiveTracingStarted.store(false); |
Kean Mariotti | 8c5abc8 | 2023-09-20 09:28:07 +0000 | [diff] [blame^] | 105 | ALOGD("Stopped active tracing"); |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 106 | } |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 109 | void LayerTracing::addProtoSnapshotToOstream(perfetto::protos::LayersSnapshotProto&& snapshot, |
| 110 | Mode mode) { |
| 111 | ATRACE_CALL(); |
| 112 | if (mOutStream) { |
| 113 | writeSnapshotToStream(std::move(snapshot)); |
| 114 | } else { |
| 115 | writeSnapshotToPerfetto(snapshot, mode); |
| 116 | } |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 119 | bool LayerTracing::isActiveTracingStarted() const { |
| 120 | return mIsActiveTracingStarted.load(); |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 123 | uint32_t LayerTracing::getActiveTracingFlags() const { |
| 124 | return mActiveTracingFlags.load(); |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 125 | } |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 126 | |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 127 | bool LayerTracing::isActiveTracingFlagSet(Flag flag) const { |
| 128 | return (mActiveTracingFlags.load() & flag) != 0; |
| 129 | } |
| 130 | |
| 131 | perfetto::protos::LayersTraceFileProto LayerTracing::createTraceFileProto() { |
| 132 | perfetto::protos::LayersTraceFileProto fileProto; |
| 133 | fileProto.set_magic_number( |
| 134 | static_cast<uint64_t>(perfetto::protos::LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_H) |
| 135 | << 32 | |
| 136 | perfetto::protos::LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_L); |
| 137 | auto timeOffsetNs = static_cast<uint64_t>(systemTime(SYSTEM_TIME_REALTIME) - |
| 138 | systemTime(SYSTEM_TIME_MONOTONIC)); |
Kean Mariotti | c44fdaf | 2022-07-29 14:20:39 +0000 | [diff] [blame] | 139 | fileProto.set_real_to_elapsed_time_offset_nanos(timeOffsetNs); |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 140 | return fileProto; |
| 141 | } |
| 142 | |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 143 | void LayerTracing::writeSnapshotToStream(perfetto::protos::LayersSnapshotProto&& snapshot) const { |
| 144 | auto fileProto = createTraceFileProto(); |
| 145 | *fileProto.add_entry() = std::move(snapshot); |
| 146 | mOutStream->get() << fileProto.SerializeAsString(); |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 149 | void LayerTracing::writeSnapshotToPerfetto(const perfetto::protos::LayersSnapshotProto& snapshot, |
| 150 | Mode mode) { |
| 151 | const auto snapshotBytes = snapshot.SerializeAsString(); |
| 152 | |
| 153 | LayerDataSource::Trace([&](LayerDataSource::TraceContext context) { |
| 154 | if (mode != context.GetCustomTlsState()->mMode) { |
| 155 | return; |
| 156 | } |
| 157 | if (!checkAndUpdateLastVsyncIdWrittenToPerfetto(mode, snapshot.vsync_id())) { |
| 158 | return; |
| 159 | } |
| 160 | { |
| 161 | auto packet = context.NewTracePacket(); |
| 162 | packet->set_timestamp(static_cast<uint64_t>(snapshot.elapsed_realtime_nanos())); |
| 163 | packet->set_timestamp_clock_id(perfetto::protos::pbzero::BUILTIN_CLOCK_MONOTONIC); |
| 164 | auto* snapshotProto = packet->set_surfaceflinger_layers_snapshot(); |
| 165 | snapshotProto->AppendRawProtoBytes(snapshotBytes.data(), snapshotBytes.size()); |
| 166 | } |
| 167 | { |
| 168 | // TODO (b/162206162): remove empty packet when perfetto bug is fixed. |
| 169 | // It is currently needed in order not to lose the last trace entry. |
| 170 | context.NewTracePacket(); |
| 171 | } |
| 172 | }); |
| 173 | } |
| 174 | |
| 175 | bool LayerTracing::checkAndUpdateLastVsyncIdWrittenToPerfetto(Mode mode, std::int64_t vsyncId) { |
| 176 | // In some situations (e.g. two bugreports taken shortly one after the other) the generated |
| 177 | // sequence of layers snapshots might overlap. Here we check the snapshot's vsyncid to make |
| 178 | // sure that in generated tracing mode a given snapshot is written only once to perfetto. |
| 179 | if (mode != Mode::MODE_GENERATED) { |
| 180 | return true; |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 183 | auto lastVsyncId = mLastVsyncIdWrittenToPerfetto.load(); |
| 184 | while (lastVsyncId < vsyncId) { |
| 185 | if (mLastVsyncIdWrittenToPerfetto.compare_exchange_strong(lastVsyncId, vsyncId)) { |
| 186 | return true; |
| 187 | } |
Vishnu Nair | b64a3b4 | 2022-01-13 15:29:32 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Kean Mariotti | 639b54f | 2023-04-20 12:06:29 +0000 | [diff] [blame] | 190 | return false; |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | } // namespace android |