Adithya Srinivasan | b9f62d6 | 2020-06-18 11:28:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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 "GpuMemTracer" |
| 19 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 20 | |
| 21 | #include "tracing/GpuMemTracer.h" |
| 22 | |
| 23 | #include <gpumem/GpuMem.h> |
| 24 | #include <perfetto/trace/android/gpu_mem_event.pbzero.h> |
Adithya Srinivasan | f031e97 | 2020-12-12 03:31:50 +0000 | [diff] [blame^] | 25 | #include <perfetto/trace/trace.pb.h> |
Adithya Srinivasan | b9f62d6 | 2020-06-18 11:28:12 -0700 | [diff] [blame] | 26 | #include <unistd.h> |
Adithya Srinivasan | b9f62d6 | 2020-06-18 11:28:12 -0700 | [diff] [blame] | 27 | |
Adithya Srinivasan | b9f62d6 | 2020-06-18 11:28:12 -0700 | [diff] [blame] | 28 | #include <thread> |
| 29 | |
| 30 | PERFETTO_DEFINE_DATA_SOURCE_STATIC_MEMBERS(android::GpuMemTracer::GpuMemDataSource); |
| 31 | |
| 32 | namespace android { |
| 33 | |
| 34 | std::mutex GpuMemTracer::sTraceMutex; |
| 35 | std::condition_variable GpuMemTracer::sCondition; |
| 36 | bool GpuMemTracer::sTraceStarted; |
| 37 | |
| 38 | void GpuMemTracer::initialize(std::shared_ptr<GpuMem> gpuMem) { |
| 39 | if (!gpuMem->isInitialized()) { |
| 40 | ALOGE("Cannot initialize GpuMemTracer before GpuMem"); |
| 41 | return; |
| 42 | } |
| 43 | mGpuMem = gpuMem; |
| 44 | perfetto::TracingInitArgs args; |
| 45 | args.backends = perfetto::kSystemBackend; |
Adithya Srinivasan | b9f62d6 | 2020-06-18 11:28:12 -0700 | [diff] [blame] | 46 | perfetto::Tracing::Initialize(args); |
| 47 | registerDataSource(); |
Adithya Srinivasan | f031e97 | 2020-12-12 03:31:50 +0000 | [diff] [blame^] | 48 | std::thread tracerThread(&GpuMemTracer::threadLoop, this, true); |
Adithya Srinivasan | b9f62d6 | 2020-06-18 11:28:12 -0700 | [diff] [blame] | 49 | pthread_setname_np(tracerThread.native_handle(), "GpuMemTracerThread"); |
| 50 | tracerThread.detach(); |
Adithya Srinivasan | f031e97 | 2020-12-12 03:31:50 +0000 | [diff] [blame^] | 51 | tracerThreadCount++; |
| 52 | } |
| 53 | |
| 54 | void GpuMemTracer::initializeForTest(std::shared_ptr<GpuMem> gpuMem) { |
| 55 | mGpuMem = gpuMem; |
| 56 | perfetto::TracingInitArgs args; |
| 57 | args.backends = perfetto::kInProcessBackend; |
| 58 | perfetto::Tracing::Initialize(args); |
| 59 | registerDataSource(); |
| 60 | std::thread tracerThread(&GpuMemTracer::threadLoop, this, false); |
| 61 | pthread_setname_np(tracerThread.native_handle(), "GpuMemTracerThreadForTest"); |
| 62 | tracerThread.detach(); |
| 63 | tracerThreadCount++; |
| 64 | } |
| 65 | |
| 66 | std::vector<perfetto::protos::TracePacket> GpuMemTracer::readGpuMemTotalPacketsForTestBlocking( |
| 67 | perfetto::TracingSession* tracingSession) { |
| 68 | std::vector<char> raw_trace = tracingSession->ReadTraceBlocking(); |
| 69 | perfetto::protos::Trace trace; |
| 70 | trace.ParseFromArray(raw_trace.data(), int(raw_trace.size())); |
| 71 | |
| 72 | std::vector<perfetto::protos::TracePacket> packets; |
| 73 | for (const auto& packet : trace.packet()) { |
| 74 | if (!packet.has_gpu_mem_total_event()) { |
| 75 | continue; |
| 76 | } |
| 77 | packets.emplace_back(packet); |
| 78 | } |
| 79 | return packets; |
| 80 | } |
| 81 | |
| 82 | // Each tracing session can be used for a single block of Start -> Stop. |
| 83 | std::unique_ptr<perfetto::TracingSession> GpuMemTracer::getTracingSessionForTest() { |
| 84 | perfetto::TraceConfig cfg; |
| 85 | cfg.set_duration_ms(500); |
| 86 | cfg.add_buffers()->set_size_kb(1024); |
| 87 | auto* ds_cfg = cfg.add_data_sources()->mutable_config(); |
| 88 | ds_cfg->set_name(GpuMemTracer::kGpuMemDataSource); |
| 89 | |
| 90 | auto tracingSession = perfetto::Tracing::NewTrace(perfetto::kInProcessBackend); |
| 91 | tracingSession->Setup(cfg); |
| 92 | return tracingSession; |
Adithya Srinivasan | b9f62d6 | 2020-06-18 11:28:12 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void GpuMemTracer::registerDataSource() { |
| 96 | perfetto::DataSourceDescriptor dsd; |
| 97 | dsd.set_name(kGpuMemDataSource); |
| 98 | GpuMemDataSource::Register(dsd); |
| 99 | } |
| 100 | |
Adithya Srinivasan | f031e97 | 2020-12-12 03:31:50 +0000 | [diff] [blame^] | 101 | void GpuMemTracer::threadLoop(bool infiniteLoop) { |
| 102 | do { |
Adithya Srinivasan | b9f62d6 | 2020-06-18 11:28:12 -0700 | [diff] [blame] | 103 | { |
| 104 | std::unique_lock<std::mutex> lock(GpuMemTracer::sTraceMutex); |
| 105 | while (!sTraceStarted) { |
| 106 | sCondition.wait(lock); |
| 107 | } |
| 108 | } |
| 109 | traceInitialCounters(); |
| 110 | { |
| 111 | std::lock_guard<std::mutex> lock(GpuMemTracer::sTraceMutex); |
| 112 | sTraceStarted = false; |
| 113 | } |
Adithya Srinivasan | f031e97 | 2020-12-12 03:31:50 +0000 | [diff] [blame^] | 114 | } while (infiniteLoop); |
| 115 | |
| 116 | // Thread loop is exiting. Reduce the tracerThreadCount to reflect the number of active threads |
| 117 | // in the wait loop. |
| 118 | tracerThreadCount--; |
Adithya Srinivasan | b9f62d6 | 2020-06-18 11:28:12 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void GpuMemTracer::traceInitialCounters() { |
| 122 | if (!mGpuMem->isInitialized()) { |
| 123 | // This should never happen. |
| 124 | ALOGE("Cannot trace without GpuMem initialization"); |
| 125 | return; |
| 126 | } |
Yiwei Zhang | 5cd71a2 | 2020-08-13 17:39:51 -0700 | [diff] [blame] | 127 | mGpuMem->traverseGpuMemTotals([](int64_t ts, uint32_t gpuId, uint32_t pid, uint64_t size) { |
Adithya Srinivasan | b9f62d6 | 2020-06-18 11:28:12 -0700 | [diff] [blame] | 128 | GpuMemDataSource::Trace([&](GpuMemDataSource::TraceContext ctx) { |
| 129 | auto packet = ctx.NewTracePacket(); |
Colin Cross | 0d5e129 | 2020-08-27 04:12:26 +0000 | [diff] [blame] | 130 | packet->set_timestamp_clock_id(perfetto::protos::pbzero::BUILTIN_CLOCK_MONOTONIC); |
Yiwei Zhang | 5cd71a2 | 2020-08-13 17:39:51 -0700 | [diff] [blame] | 131 | packet->set_timestamp(ts); |
Adithya Srinivasan | b9f62d6 | 2020-06-18 11:28:12 -0700 | [diff] [blame] | 132 | auto* event = packet->set_gpu_mem_total_event(); |
| 133 | event->set_gpu_id(gpuId); |
| 134 | event->set_pid(pid); |
| 135 | event->set_size(size); |
| 136 | }); |
| 137 | }); |
| 138 | // Flush the TraceContext. The last packet in the above loop will go |
| 139 | // missing without this flush. |
| 140 | GpuMemDataSource::Trace([](GpuMemDataSource::TraceContext ctx) { ctx.Flush(); }); |
| 141 | } |
| 142 | |
| 143 | } // namespace android |