blob: 6366e1d8e211f7525361cd4e98d01aa7736ea327 [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>
25#include <unistd.h>
Adithya Srinivasanb9f62d62020-06-18 11:28:12 -070026
Adithya Srinivasanb9f62d62020-06-18 11:28:12 -070027#include <thread>
28
29PERFETTO_DEFINE_DATA_SOURCE_STATIC_MEMBERS(android::GpuMemTracer::GpuMemDataSource);
30
31namespace android {
32
33std::mutex GpuMemTracer::sTraceMutex;
34std::condition_variable GpuMemTracer::sCondition;
35bool GpuMemTracer::sTraceStarted;
36
37void 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 Srinivasanb9f62d62020-06-18 11:28:12 -070045 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
52void GpuMemTracer::registerDataSource() {
53 perfetto::DataSourceDescriptor dsd;
54 dsd.set_name(kGpuMemDataSource);
55 GpuMemDataSource::Register(dsd);
56}
57
58void 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
74void GpuMemTracer::traceInitialCounters() {
75 if (!mGpuMem->isInitialized()) {
76 // This should never happen.
77 ALOGE("Cannot trace without GpuMem initialization");
78 return;
79 }
Yiwei Zhang5cd71a22020-08-13 17:39:51 -070080 mGpuMem->traverseGpuMemTotals([](int64_t ts, uint32_t gpuId, uint32_t pid, uint64_t size) {
Adithya Srinivasanb9f62d62020-06-18 11:28:12 -070081 GpuMemDataSource::Trace([&](GpuMemDataSource::TraceContext ctx) {
82 auto packet = ctx.NewTracePacket();
Yiwei Zhang5cd71a22020-08-13 17:39:51 -070083 packet->set_timestamp(ts);
Adithya Srinivasanb9f62d62020-06-18 11:28:12 -070084 auto* event = packet->set_gpu_mem_total_event();
85 event->set_gpu_id(gpuId);
86 event->set_pid(pid);
87 event->set_size(size);
88 });
89 });
90 // Flush the TraceContext. The last packet in the above loop will go
91 // missing without this flush.
92 GpuMemDataSource::Trace([](GpuMemDataSource::TraceContext ctx) { ctx.Flush(); });
93}
94
95} // namespace android