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 | |
Vishnu Nair | cb56533 | 2023-03-14 21:10:55 -0700 | [diff] [blame] | 21 | #include <filesystem> |
| 22 | |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 23 | #include <SurfaceFlinger.h> |
| 24 | #include <android-base/stringprintf.h> |
| 25 | #include <log/log.h> |
| 26 | #include <utils/SystemClock.h> |
| 27 | #include <utils/Trace.h> |
| 28 | |
| 29 | #include "LayerTracing.h" |
John Reck | 2a3d29d | 2023-08-17 17:45:01 -0400 | [diff] [blame^] | 30 | #include "TransactionRingBuffer.h" |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 31 | |
| 32 | namespace android { |
| 33 | |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 34 | LayerTracing::LayerTracing() |
John Reck | 2a3d29d | 2023-08-17 17:45:01 -0400 | [diff] [blame^] | 35 | : mBuffer(std::make_unique<TransactionRingBuffer<LayersTraceFileProto, LayersTraceProto>>()) { |
| 36 | } |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 37 | |
| 38 | LayerTracing::~LayerTracing() = default; |
| 39 | |
| 40 | bool LayerTracing::enable() { |
| 41 | std::scoped_lock lock(mTraceLock); |
| 42 | if (mEnabled) { |
| 43 | return false; |
| 44 | } |
| 45 | mBuffer->setSize(mBufferSizeInBytes); |
| 46 | mEnabled = true; |
| 47 | return true; |
| 48 | } |
| 49 | |
Vishnu Nair | cb56533 | 2023-03-14 21:10:55 -0700 | [diff] [blame] | 50 | bool LayerTracing::disable(std::string filename, bool writeToFile) { |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 51 | std::scoped_lock lock(mTraceLock); |
| 52 | if (!mEnabled) { |
| 53 | return false; |
| 54 | } |
| 55 | mEnabled = false; |
Vishnu Nair | cb56533 | 2023-03-14 21:10:55 -0700 | [diff] [blame] | 56 | if (writeToFile) { |
| 57 | LayersTraceFileProto fileProto = createTraceFileProto(); |
| 58 | mBuffer->writeToFile(fileProto, filename); |
| 59 | } |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 60 | mBuffer->reset(); |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 61 | return true; |
| 62 | } |
| 63 | |
Vishnu Nair | cb56533 | 2023-03-14 21:10:55 -0700 | [diff] [blame] | 64 | void LayerTracing::appendToStream(std::ofstream& out) { |
| 65 | std::scoped_lock lock(mTraceLock); |
| 66 | LayersTraceFileProto fileProto = createTraceFileProto(); |
| 67 | mBuffer->appendToStream(fileProto, out); |
| 68 | mBuffer->reset(); |
| 69 | } |
| 70 | |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 71 | bool LayerTracing::isEnabled() const { |
| 72 | std::scoped_lock lock(mTraceLock); |
| 73 | return mEnabled; |
| 74 | } |
| 75 | |
Vishnu Nair | cb56533 | 2023-03-14 21:10:55 -0700 | [diff] [blame] | 76 | status_t LayerTracing::writeToFile(std::string filename) { |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 77 | std::scoped_lock lock(mTraceLock); |
| 78 | if (!mEnabled) { |
| 79 | return STATUS_OK; |
| 80 | } |
| 81 | LayersTraceFileProto fileProto = createTraceFileProto(); |
Vishnu Nair | cb56533 | 2023-03-14 21:10:55 -0700 | [diff] [blame] | 82 | return mBuffer->writeToFile(fileProto, filename); |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void LayerTracing::setTraceFlags(uint32_t flags) { |
| 86 | std::scoped_lock lock(mTraceLock); |
| 87 | mFlags = flags; |
| 88 | } |
| 89 | |
| 90 | void LayerTracing::setBufferSize(size_t bufferSizeInBytes) { |
| 91 | std::scoped_lock lock(mTraceLock); |
| 92 | mBufferSizeInBytes = bufferSizeInBytes; |
| 93 | } |
| 94 | |
| 95 | bool LayerTracing::flagIsSet(uint32_t flags) const { |
| 96 | return (mFlags & flags) == flags; |
| 97 | } |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 98 | uint32_t LayerTracing::getFlags() const { |
| 99 | return mFlags; |
| 100 | } |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 101 | |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 102 | LayersTraceFileProto LayerTracing::createTraceFileProto() { |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 103 | LayersTraceFileProto fileProto; |
| 104 | fileProto.set_magic_number(uint64_t(LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_H) << 32 | |
| 105 | LayersTraceFileProto_MagicNumber_MAGIC_NUMBER_L); |
Kean Mariotti | c44fdaf | 2022-07-29 14:20:39 +0000 | [diff] [blame] | 106 | auto timeOffsetNs = static_cast<std::uint64_t>(systemTime(SYSTEM_TIME_REALTIME) - |
| 107 | systemTime(SYSTEM_TIME_MONOTONIC)); |
| 108 | fileProto.set_real_to_elapsed_time_offset_nanos(timeOffsetNs); |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 109 | return fileProto; |
| 110 | } |
| 111 | |
| 112 | void LayerTracing::dump(std::string& result) const { |
| 113 | std::scoped_lock lock(mTraceLock); |
| 114 | base::StringAppendF(&result, "Tracing state: %s\n", mEnabled ? "enabled" : "disabled"); |
| 115 | mBuffer->dump(result); |
| 116 | } |
| 117 | |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 118 | void LayerTracing::notify(bool visibleRegionDirty, int64_t time, int64_t vsyncId, |
| 119 | LayersProto* layers, std::string hwcDump, |
| 120 | google::protobuf::RepeatedPtrField<DisplayProto>* displays) { |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 121 | std::scoped_lock lock(mTraceLock); |
| 122 | if (!mEnabled) { |
| 123 | return; |
| 124 | } |
| 125 | |
Vishnu Nair | b64a3b4 | 2022-01-13 15:29:32 -0800 | [diff] [blame] | 126 | if (!visibleRegionDirty && !flagIsSet(LayerTracing::TRACE_BUFFERS)) { |
| 127 | return; |
| 128 | } |
| 129 | |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 130 | ATRACE_CALL(); |
| 131 | LayersTraceProto entry; |
Vishnu Nair | b64a3b4 | 2022-01-13 15:29:32 -0800 | [diff] [blame] | 132 | entry.set_elapsed_realtime_nanos(time); |
| 133 | const char* where = visibleRegionDirty ? "visibleRegionsDirty" : "bufferLatched"; |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 134 | entry.set_where(where); |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 135 | entry.mutable_layers()->Swap(layers); |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 136 | |
| 137 | if (flagIsSet(LayerTracing::TRACE_HWC)) { |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 138 | entry.set_hwc_blob(hwcDump); |
| 139 | } |
| 140 | if (!flagIsSet(LayerTracing::TRACE_COMPOSITION)) { |
| 141 | entry.set_excludes_composition_state(true); |
| 142 | } |
Vishnu Nair | 8175062 | 2023-03-08 15:02:06 -0800 | [diff] [blame] | 143 | entry.mutable_displays()->Swap(displays); |
Pablo Gamito | b877579 | 2022-06-10 15:02:42 +0000 | [diff] [blame] | 144 | entry.set_vsync_id(vsyncId); |
Greg Kaiser | ddefa7b | 2022-06-13 06:19:26 -0700 | [diff] [blame] | 145 | mBuffer->emplace(std::move(entry)); |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | } // namespace android |