blob: 474fef89a0f8087aaa52549e7255dca98c3341b5 [file] [log] [blame]
Kean Mariotti639b54f2023-04-20 12:06:29 +00001/*
2 * Copyright 2023 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
20#include "LayerDataSource.h"
21
22#include <log/log.h>
23#include <perfetto/config/android/surfaceflinger_layers_config.pbzero.h>
24
25namespace android {
26
27void LayerDataSource::Initialize(LayerTracing& layerTracing) {
28 mLayerTracing.store(&layerTracing);
29
30 auto args = perfetto::TracingInitArgs{};
31 args.backends = perfetto::kSystemBackend;
32 // We are tracing ~50kb/entry and the default shmem buffer size (256kb) could be overrun.
33 // A shmem buffer overrun typically just stalls layer tracing, however when the stall
34 // lasts for too long perfetto assumes there is a deadlock and aborts surfaceflinger.
35 args.shmem_size_hint_kb = 1024;
36 perfetto::Tracing::Initialize(args);
37
38 perfetto::DataSourceDescriptor descriptor;
39 descriptor.set_name(android::LayerDataSource::kName);
40 LayerDataSource::Register(descriptor);
41}
42
43void LayerDataSource::UnregisterLayerTracing() {
44 mLayerTracing.store(nullptr);
45}
46
47void LayerDataSource::OnSetup(const LayerDataSource::SetupArgs& args) {
48 const auto configRaw = args.config->surfaceflinger_layers_config_raw();
49 const auto config = perfetto::protos::pbzero::SurfaceFlingerLayersConfig::Decoder{configRaw};
50
51 if (config.has_mode() && config.mode() != LayerTracing::Mode::MODE_UNSPECIFIED) {
52 mMode = static_cast<LayerTracing::Mode>(config.mode());
53 } else {
54 mMode = LayerTracing::Mode::MODE_GENERATED;
55 ALOGV("Received config with unspecified 'mode'. Using 'GENERATED' as default");
56 }
57
58 mFlags = 0;
59 for (auto it = config.trace_flags(); it; ++it) {
60 mFlags |= static_cast<uint32_t>(*it);
61 }
62}
63
64void LayerDataSource::OnStart(const LayerDataSource::StartArgs&) {
65 ALOGV("Received OnStart event (mode = 0x%02x, flags = 0x%02x)", mMode, mFlags);
66 if (auto* p = mLayerTracing.load()) {
67 p->onStart(mMode, mFlags);
68 }
69}
70
71void LayerDataSource::OnFlush(const LayerDataSource::FlushArgs&) {
72 ALOGV("Received OnFlush event (mode = 0x%02x, flags = 0x%02x)", mMode, mFlags);
73 if (auto* p = mLayerTracing.load()) {
74 p->onFlush(mMode, mFlags);
75 }
76}
77
78void LayerDataSource::OnStop(const LayerDataSource::StopArgs&) {
79 ALOGV("Received OnStop event (mode = 0x%02x, flags = 0x%02x)", mMode, mFlags);
80 if (auto* p = mLayerTracing.load()) {
81 p->onStop(mMode);
82 }
83}
84
85LayerTracing::Mode LayerDataSource::GetMode() const {
86 return mMode;
87}
88
89std::atomic<LayerTracing*> LayerDataSource::mLayerTracing = nullptr;
90
91} // namespace android
92
93PERFETTO_DEFINE_DATA_SOURCE_STATIC_MEMBERS(android::LayerDataSource);