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> |
| 25 | #include <unistd.h> |
Adithya Srinivasan | b9f62d6 | 2020-06-18 11:28:12 -0700 | [diff] [blame] | 26 | |
Adithya Srinivasan | b9f62d6 | 2020-06-18 11:28:12 -0700 | [diff] [blame] | 27 | #include <thread> |
| 28 | |
| 29 | PERFETTO_DEFINE_DATA_SOURCE_STATIC_MEMBERS(android::GpuMemTracer::GpuMemDataSource); |
| 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | std::mutex GpuMemTracer::sTraceMutex; |
| 34 | std::condition_variable GpuMemTracer::sCondition; |
| 35 | bool GpuMemTracer::sTraceStarted; |
| 36 | |
| 37 | void GpuMemTracer::initialize(std::shared_ptr<GpuMem> gpuMem) { |
| 38 | if (!gpuMem->isInitialized()) { |
| 39 | ALOGE("Cannot initialize GpuMemTracer before GpuMem"); |
| 40 | return; |
| 41 | } |
| 42 | mGpuMem = gpuMem; |
| 43 | perfetto::TracingInitArgs args; |
| 44 | args.backends = perfetto::kSystemBackend; |
Adithya Srinivasan | b9f62d6 | 2020-06-18 11:28:12 -0700 | [diff] [blame] | 45 | perfetto::Tracing::Initialize(args); |
| 46 | registerDataSource(); |
| 47 | std::thread tracerThread(&GpuMemTracer::threadLoop, this); |
| 48 | pthread_setname_np(tracerThread.native_handle(), "GpuMemTracerThread"); |
| 49 | tracerThread.detach(); |
| 50 | } |
| 51 | |
| 52 | void GpuMemTracer::registerDataSource() { |
| 53 | perfetto::DataSourceDescriptor dsd; |
| 54 | dsd.set_name(kGpuMemDataSource); |
| 55 | GpuMemDataSource::Register(dsd); |
| 56 | } |
| 57 | |
| 58 | void GpuMemTracer::threadLoop() { |
| 59 | while (true) { |
| 60 | { |
| 61 | std::unique_lock<std::mutex> lock(GpuMemTracer::sTraceMutex); |
| 62 | while (!sTraceStarted) { |
| 63 | sCondition.wait(lock); |
| 64 | } |
| 65 | } |
| 66 | traceInitialCounters(); |
| 67 | { |
| 68 | std::lock_guard<std::mutex> lock(GpuMemTracer::sTraceMutex); |
| 69 | sTraceStarted = false; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void GpuMemTracer::traceInitialCounters() { |
| 75 | if (!mGpuMem->isInitialized()) { |
| 76 | // This should never happen. |
| 77 | ALOGE("Cannot trace without GpuMem initialization"); |
| 78 | return; |
| 79 | } |
Yiwei Zhang | 5cd71a2 | 2020-08-13 17:39:51 -0700 | [diff] [blame] | 80 | 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] | 81 | GpuMemDataSource::Trace([&](GpuMemDataSource::TraceContext ctx) { |
| 82 | auto packet = ctx.NewTracePacket(); |
Colin Cross | 0d5e129 | 2020-08-27 04:12:26 +0000 | [diff] [blame] | 83 | packet->set_timestamp_clock_id(perfetto::protos::pbzero::BUILTIN_CLOCK_MONOTONIC); |
Yiwei Zhang | 5cd71a2 | 2020-08-13 17:39:51 -0700 | [diff] [blame] | 84 | packet->set_timestamp(ts); |
Adithya Srinivasan | b9f62d6 | 2020-06-18 11:28:12 -0700 | [diff] [blame] | 85 | auto* event = packet->set_gpu_mem_total_event(); |
| 86 | event->set_gpu_id(gpuId); |
| 87 | event->set_pid(pid); |
| 88 | event->set_size(size); |
| 89 | }); |
| 90 | }); |
| 91 | // Flush the TraceContext. The last packet in the above loop will go |
| 92 | // missing without this flush. |
| 93 | GpuMemDataSource::Trace([](GpuMemDataSource::TraceContext ctx) { ctx.Flush(); }); |
| 94 | } |
| 95 | |
| 96 | } // namespace android |