1 #include "GpuSysfsReader.h" 2 3 #include <log/log.h> 4 5 #include <fstream> 6 #include <sstream> 7 8 #include "filesystem.h" 9 10 #undef LOG_TAG 11 #define LOG_TAG "memtrack-gpusysfsreader" 12 13 using namespace GpuSysfsReader; 14 15 namespace { readNode(const std::string node,pid_t pid)16uint64_t readNode(const std::string node, pid_t pid) { 17 std::stringstream ss; 18 if (pid) 19 ss << kSysfsDevicePath << "/" << kProcessDir << "/" << pid << "/" << node; 20 else 21 ss << kSysfsDevicePath << "/" << node; 22 const std::string path = ss.str(); 23 24 if (!filesystem::exists(filesystem::path(path))) { 25 ALOGV("File not found: %s", path.c_str()); 26 return 0; 27 } 28 29 std::ifstream file(path.c_str()); 30 if (!file.is_open()) { 31 ALOGW("Failed to open %s path", path.c_str()); 32 return 0; 33 } 34 35 uint64_t out; 36 file >> out; 37 file.close(); 38 39 return out; 40 } 41 } // namespace 42 getDmaBufGpuMem(pid_t pid)43uint64_t GpuSysfsReader::getDmaBufGpuMem(pid_t pid) { return readNode(kDmaBufGpuMemNode, pid); } 44 getGpuMemTotal(pid_t pid)45uint64_t GpuSysfsReader::getGpuMemTotal(pid_t pid) { return readNode(kTotalGpuMemNode, pid); } 46 getPrivateGpuMem(pid_t pid)47uint64_t GpuSysfsReader::getPrivateGpuMem(pid_t pid) { 48 auto dma_buf_size = getDmaBufGpuMem(pid); 49 auto gpu_total_size = getGpuMemTotal(pid); 50 51 if (dma_buf_size > gpu_total_size) { 52 ALOGE("Bug in reader, dma-buf size (%" PRIu64 ") is higher than total gpu size (%" PRIu64 53 ")", 54 dma_buf_size, gpu_total_size); 55 return 0; 56 } 57 58 return gpu_total_size - dma_buf_size; 59 } 60