You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.8 KiB

/*
* Copyright 2025 Rockchip Electronics S.LSI Co. LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "GpuSysfsReader.h"
#include <log/log.h>
#include <fstream>
#include <sstream>
#include "filesystem.h"
#undef LOG_TAG
#define LOG_TAG "memtrack-gpusysfsreader"
using namespace GpuSysfsReader;
namespace {
uint64_t readNode(const std::string node, pid_t pid) {
std::stringstream ss;
if (pid)
ss << kSysfsDevicePath << "/" << kProcessDir << "/" << pid << "/" << node;
else
ss << kSysfsDevicePath << "/" << node;
const std::string path = ss.str();
if (!filesystem::exists(filesystem::path(path))) {
ALOGV("File not found: %s", path.c_str());
return 0;
}
std::ifstream file(path.c_str());
if (!file.is_open()) {
ALOGW("Failed to open %s path", path.c_str());
return 0;
}
uint64_t out;
file >> out;
file.close();
return out;
}
} // namespace
uint64_t GpuSysfsReader::getPrivateGpuMem(pid_t pid) { return readNode(kPrivateGpuMemNode, pid); }
uint64_t GpuSysfsReader::getGpuMemTotal(pid_t pid) { return readNode(kTotalGpuMemNode, pid); }
uint64_t GpuSysfsReader::getDmaBufGpuMem(pid_t pid) {
auto private_size = getPrivateGpuMem(pid);
auto gpu_total_size = getGpuMemTotal(pid);
return gpu_total_size - private_size;
}