/* * Copyright 2021 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include #include #include namespace android { class TransactionTracing; /* * LayerTracing records layer states during surface flinging. Manages tracing state and * configuration. * * The traced data can then be collected with Perfetto. * * The Perfetto custom data source LayerDataSource is registered with perfetto. The data source * is used to listen to perfetto events (setup, start, stop, flush) and to write trace packets * to perfetto. * * The user can configure/start/stop tracing via /system/bin/perfetto. * * Tracing can operate in the following modes. * * ACTIVE mode: * A layers snapshot is taken and written to perfetto for each vsyncid commit. * * GENERATED mode: * Listens to the perfetto 'flush' event (e.g. when a bugreport is taken). * When a 'flush' event is received, the ring buffer of transactions (hold by TransactionTracing) * is processed by LayerTraceGenerator, a sequence of layers snapshots is generated * and written to perfetto. * * DUMP mode: * When the 'start' event is received a single layers snapshot is taken * and written to perfetto. * * * E.g. start active mode tracing * (replace mode value with MODE_DUMP, MODE_GENERATED or MODE_GENERATED_BUGREPORT_ONLY to enable * different tracing modes): * adb shell -t perfetto \ -c - --txt \ -o /data/misc/perfetto-traces/trace \ <&); void setTransactionTracing(TransactionTracing&); // Start event from perfetto data source void onStart(Mode mode, uint32_t flags); // Flush event from perfetto data source void onFlush(Mode mode, uint32_t flags, bool isBugreport); // Stop event from perfetto data source void onStop(Mode mode); void addProtoSnapshotToOstream(perfetto::protos::LayersSnapshotProto&& snapshot, Mode mode); bool isActiveTracingStarted() const; uint32_t getActiveTracingFlags() const; bool isActiveTracingFlagSet(Flag flag) const; static perfetto::protos::LayersTraceFileProto createTraceFileProto(); private: void writeSnapshotToStream(perfetto::protos::LayersSnapshotProto&& snapshot) const; void writeSnapshotToPerfetto(const perfetto::protos::LayersSnapshotProto& snapshot, Mode mode); bool checkAndUpdateLastVsyncIdWrittenToPerfetto(Mode mode, std::int64_t vsyncId); std::function mTakeLayersSnapshotProto; TransactionTracing* mTransactionTracing; std::atomic mIsActiveTracingStarted{false}; std::atomic mActiveTracingFlags{0}; std::atomic mLastVsyncIdWrittenToPerfetto{-1}; std::optional> mOutStream; }; } // namespace android