1 /*
2 * Copyright 2019 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 // TODO(b/129481165): remove the #pragma below and fix conversion issues
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wconversion"
20
21 #undef LOG_TAG
22 #define LOG_TAG "FrameTracer"
23 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
24
25 #include "FrameTracer.h"
26
27 #include <android-base/stringprintf.h>
28 #include <perfetto/trace/clock_snapshot.pbzero.h>
29
30 #include <algorithm>
31 #include <mutex>
32
33 PERFETTO_DEFINE_DATA_SOURCE_STATIC_MEMBERS(android::FrameTracer::FrameTracerDataSource);
34
35 namespace android {
36
37 using Clock = perfetto::protos::pbzero::ClockSnapshot::Clock;
initialize()38 void FrameTracer::initialize() {
39 std::call_once(mInitializationFlag, [this]() {
40 perfetto::TracingInitArgs args;
41 args.backends = perfetto::kSystemBackend;
42 perfetto::Tracing::Initialize(args);
43 registerDataSource();
44 });
45 }
46
registerDataSource()47 void FrameTracer::registerDataSource() {
48 perfetto::DataSourceDescriptor dsd;
49 dsd.set_name(kFrameTracerDataSource);
50 FrameTracerDataSource::Register(dsd);
51 }
52
traceNewLayer(int32_t layerId,const std::string & layerName)53 void FrameTracer::traceNewLayer(int32_t layerId, const std::string& layerName) {
54 FrameTracerDataSource::Trace([this, layerId, &layerName](FrameTracerDataSource::TraceContext) {
55 if (mTraceTracker.find(layerId) == mTraceTracker.end()) {
56 std::lock_guard<std::mutex> lock(mTraceMutex);
57 mTraceTracker[layerId].layerName = layerName;
58 }
59 });
60 }
61
traceTimestamp(int32_t layerId,uint64_t bufferID,uint64_t frameNumber,nsecs_t timestamp,FrameEvent::BufferEventType type,nsecs_t duration)62 void FrameTracer::traceTimestamp(int32_t layerId, uint64_t bufferID, uint64_t frameNumber,
63 nsecs_t timestamp, FrameEvent::BufferEventType type,
64 nsecs_t duration) {
65 FrameTracerDataSource::Trace([this, layerId, bufferID, frameNumber, timestamp, type,
66 duration](FrameTracerDataSource::TraceContext ctx) {
67 std::lock_guard<std::mutex> lock(mTraceMutex);
68 if (mTraceTracker.find(layerId) == mTraceTracker.end()) {
69 return;
70 }
71
72 // Handle any pending fences for this buffer.
73 tracePendingFencesLocked(ctx, layerId, bufferID);
74
75 // Complete current trace.
76 traceLocked(ctx, layerId, bufferID, frameNumber, timestamp, type, duration);
77 });
78 }
79
traceFence(int32_t layerId,uint64_t bufferID,uint64_t frameNumber,const std::shared_ptr<FenceTime> & fence,FrameEvent::BufferEventType type,nsecs_t startTime)80 void FrameTracer::traceFence(int32_t layerId, uint64_t bufferID, uint64_t frameNumber,
81 const std::shared_ptr<FenceTime>& fence,
82 FrameEvent::BufferEventType type, nsecs_t startTime) {
83 FrameTracerDataSource::Trace([this, layerId, bufferID, frameNumber, &fence, type,
84 startTime](FrameTracerDataSource::TraceContext ctx) {
85 const nsecs_t signalTime = fence->getSignalTime();
86 if (signalTime != Fence::SIGNAL_TIME_INVALID) {
87 std::lock_guard<std::mutex> lock(mTraceMutex);
88 if (mTraceTracker.find(layerId) == mTraceTracker.end()) {
89 return;
90 }
91
92 // Handle any pending fences for this buffer.
93 tracePendingFencesLocked(ctx, layerId, bufferID);
94
95 if (signalTime != Fence::SIGNAL_TIME_PENDING) {
96 traceSpanLocked(ctx, layerId, bufferID, frameNumber, type, startTime, signalTime);
97 } else {
98 mTraceTracker[layerId].pendingFences[bufferID].push_back(
99 {.frameNumber = frameNumber,
100 .type = type,
101 .fence = fence,
102 .startTime = startTime});
103 }
104 }
105 });
106 }
107
tracePendingFencesLocked(FrameTracerDataSource::TraceContext & ctx,int32_t layerId,uint64_t bufferID)108 void FrameTracer::tracePendingFencesLocked(FrameTracerDataSource::TraceContext& ctx,
109 int32_t layerId, uint64_t bufferID) {
110 if (mTraceTracker[layerId].pendingFences.count(bufferID)) {
111 auto& pendingFences = mTraceTracker[layerId].pendingFences[bufferID];
112 for (size_t i = 0; i < pendingFences.size(); ++i) {
113 auto& pendingFence = pendingFences[i];
114
115 nsecs_t signalTime = Fence::SIGNAL_TIME_INVALID;
116 if (pendingFence.fence && pendingFence.fence->isValid()) {
117 signalTime = pendingFence.fence->getSignalTime();
118 if (signalTime == Fence::SIGNAL_TIME_PENDING) {
119 continue;
120 }
121 }
122
123 if (signalTime != Fence::SIGNAL_TIME_INVALID &&
124 systemTime() - signalTime < kFenceSignallingDeadline) {
125 traceSpanLocked(ctx, layerId, bufferID, pendingFence.frameNumber, pendingFence.type,
126 pendingFence.startTime, signalTime);
127 }
128
129 pendingFences.erase(pendingFences.begin() + i);
130 --i;
131 }
132 }
133 }
134
traceLocked(FrameTracerDataSource::TraceContext & ctx,int32_t layerId,uint64_t bufferID,uint64_t frameNumber,nsecs_t timestamp,FrameEvent::BufferEventType type,nsecs_t duration)135 void FrameTracer::traceLocked(FrameTracerDataSource::TraceContext& ctx, int32_t layerId,
136 uint64_t bufferID, uint64_t frameNumber, nsecs_t timestamp,
137 FrameEvent::BufferEventType type, nsecs_t duration) {
138 auto packet = ctx.NewTracePacket();
139 packet->set_timestamp_clock_id(Clock::MONOTONIC);
140 packet->set_timestamp(timestamp);
141 auto* event = packet->set_graphics_frame_event()->set_buffer_event();
142 event->set_buffer_id(static_cast<uint32_t>(bufferID));
143 if (frameNumber != UNSPECIFIED_FRAME_NUMBER) {
144 event->set_frame_number(frameNumber);
145 }
146 event->set_type(type);
147
148 if (mTraceTracker.find(layerId) != mTraceTracker.end() &&
149 !mTraceTracker[layerId].layerName.empty()) {
150 const std::string& layerName = mTraceTracker[layerId].layerName;
151 event->set_layer_name(layerName.c_str(), layerName.size());
152 }
153
154 if (duration > 0) {
155 event->set_duration_ns(duration);
156 }
157 }
158
traceSpanLocked(FrameTracerDataSource::TraceContext & ctx,int32_t layerId,uint64_t bufferID,uint64_t frameNumber,FrameEvent::BufferEventType type,nsecs_t startTime,nsecs_t endTime)159 void FrameTracer::traceSpanLocked(FrameTracerDataSource::TraceContext& ctx, int32_t layerId,
160 uint64_t bufferID, uint64_t frameNumber,
161 FrameEvent::BufferEventType type, nsecs_t startTime,
162 nsecs_t endTime) {
163 nsecs_t timestamp = endTime;
164 nsecs_t duration = 0;
165 if (startTime > 0 && startTime < endTime) {
166 timestamp = startTime;
167 duration = endTime - startTime;
168 }
169 traceLocked(ctx, layerId, bufferID, frameNumber, timestamp, type, duration);
170 }
171
onDestroy(int32_t layerId)172 void FrameTracer::onDestroy(int32_t layerId) {
173 std::lock_guard<std::mutex> traceLock(mTraceMutex);
174 mTraceTracker.erase(layerId);
175 }
176
miniDump()177 std::string FrameTracer::miniDump() {
178 std::string result = "FrameTracer miniDump:\n";
179 std::lock_guard<std::mutex> lock(mTraceMutex);
180 android::base::StringAppendF(&result, "Number of layers currently being traced is %zu\n",
181 mTraceTracker.size());
182 return result;
183 }
184
185 } // namespace android
186
187 // TODO(b/129481165): remove the #pragma below and fix conversion issues
188 #pragma clang diagnostic pop // ignored "-Wconversion"
189