blob: 584498e981859a9926753cd912d83adbfa72ff14 [file] [log] [blame]
Adithya Srinivasanb9f62d62020-06-18 11:28:12 -07001/*
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 Srinivasanf031e972020-12-12 03:31:50 +000025#include <perfetto/trace/trace.pb.h>
Adithya Srinivasanb9f62d62020-06-18 11:28:12 -070026#include <unistd.h>
Adithya Srinivasanb9f62d62020-06-18 11:28:12 -070027
Adithya Srinivasanb9f62d62020-06-18 11:28:12 -070028#include <thread>
29
30PERFETTO_DEFINE_DATA_SOURCE_STATIC_MEMBERS(android::GpuMemTracer::GpuMemDataSource);
31
32namespace android {
33
34std::mutex GpuMemTracer::sTraceMutex;
35std::condition_variable GpuMemTracer::sCondition;
36bool GpuMemTracer::sTraceStarted;
37
38void 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 Srinivasanb9f62d62020-06-18 11:28:12 -070046 perfetto::Tracing::Initialize(args);
47 registerDataSource();
Adithya Srinivasanf031e972020-12-12 03:31:50 +000048 std::thread tracerThread(&GpuMemTracer::threadLoop, this, true);
Adithya Srinivasanb9f62d62020-06-18 11:28:12 -070049 pthread_setname_np(tracerThread.native_handle(), "GpuMemTracerThread");
50 tracerThread.detach();
Adithya Srinivasanf031e972020-12-12 03:31:50 +000051 tracerThreadCount++;
52}
53
54void 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
66std::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.
83std::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 Srinivasanb9f62d62020-06-18 11:28:12 -070093}
94
95void GpuMemTracer::registerDataSource() {
96 perfetto::DataSourceDescriptor dsd;
97 dsd.set_name(kGpuMemDataSource);
98 GpuMemDataSource::Register(dsd);
99}
100
Adithya Srinivasanf031e972020-12-12 03:31:50 +0000101void GpuMemTracer::threadLoop(bool infiniteLoop) {
102 do {
Adithya Srinivasanb9f62d62020-06-18 11:28:12 -0700103 {
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 Srinivasanf031e972020-12-12 03:31:50 +0000114 } 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 Srinivasanb9f62d62020-06-18 11:28:12 -0700119}
120
121void GpuMemTracer::traceInitialCounters() {
122 if (!mGpuMem->isInitialized()) {
123 // This should never happen.
124 ALOGE("Cannot trace without GpuMem initialization");
125 return;
126 }
Yiwei Zhang5cd71a22020-08-13 17:39:51 -0700127 mGpuMem->traverseGpuMemTotals([](int64_t ts, uint32_t gpuId, uint32_t pid, uint64_t size) {
Adithya Srinivasanb9f62d62020-06-18 11:28:12 -0700128 GpuMemDataSource::Trace([&](GpuMemDataSource::TraceContext ctx) {
129 auto packet = ctx.NewTracePacket();
Colin Cross0d5e1292020-08-27 04:12:26 +0000130 packet->set_timestamp_clock_id(perfetto::protos::pbzero::BUILTIN_CLOCK_MONOTONIC);
Yiwei Zhang5cd71a22020-08-13 17:39:51 -0700131 packet->set_timestamp(ts);
Adithya Srinivasanb9f62d62020-06-18 11:28:12 -0700132 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