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 #pragma once 18 19 #include <bpf/BpfMap.h> 20 #include <utils/String16.h> 21 #include <utils/Vector.h> 22 23 #include <functional> 24 25 namespace android { 26 27 class GpuMem { 28 public: 29 GpuMem() = default; 30 ~GpuMem(); 31 32 // initialize eBPF program and map 33 void initialize(); 34 // dumpsys interface 35 void dump(const Vector<String16>& args, std::string* result); isInitialized()36 bool isInitialized() { return mInitialized.load(); } stop()37 void stop() { mStop.store(true); } 38 39 // Traverse the gpu memory total map to feed the callback function. 40 void traverseGpuMemTotals(const std::function<void(int64_t ts, uint32_t gpuId, uint32_t pid, 41 uint64_t size)>& callback); 42 43 private: 44 // Friend class for testing. 45 friend class TestableGpuMem; 46 47 // set gpu memory total map 48 void setGpuMemTotalMap(bpf::BpfMapRO<uint64_t, uint64_t>& map); 49 50 // indicate whether ebpf has been initialized 51 std::atomic<bool> mInitialized = false; 52 53 // whether initialization should be stopped 54 std::atomic<bool> mStop = false; 55 56 // bpf map for GPU memory total data 57 android::bpf::BpfMapRO<uint64_t, uint64_t> mGpuMemTotalMap; 58 59 // gpu memory tracepoint event category 60 static constexpr char kGpuMemTraceGroup[] = "gpu_mem"; 61 // gpu memory total tracepoint 62 static constexpr char kGpuMemTotalTracepoint[] = "gpu_mem_total"; 63 // pinned gpu memory total bpf c program path in bpf sysfs 64 static constexpr char kGpuMemTotalProgPath[] = 65 "/sys/fs/bpf/prog_gpuMem_tracepoint_gpu_mem_gpu_mem_total"; 66 // pinned gpu memory total bpf map path in bpf sysfs 67 static constexpr char kGpuMemTotalMapPath[] = "/sys/fs/bpf/map_gpuMem_gpu_mem_total_map"; 68 // 30 seconds timeout for trying to attach bpf program to tracepoint 69 static constexpr int kGpuWaitTimeout = 30; 70 }; 71 72 } // namespace android 73