mirror of
https://github.com/Lizonghang/prima.cpp.git
synced 2025-09-09 12:24:33 +00:00
fix device_cgroup_physical_memory in docker container
This commit is contained in:
parent
d78fa427e7
commit
a11e0dd0fa
1 changed files with 85 additions and 35 deletions
|
@ -44,6 +44,7 @@
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <random>
|
#include <random>
|
||||||
#include <regex>
|
#include <regex>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
|
||||||
const char * device_name() {
|
const char * device_name() {
|
||||||
|
@ -477,49 +478,97 @@ static uint64_t device_host_physical_memory(bool available) {
|
||||||
return memory;
|
return memory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static uint64_t read_value_from_file(const char * path) {
|
||||||
|
std::ifstream file(path);
|
||||||
|
if (!file.is_open()) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
std::string line;
|
||||||
|
if (!std::getline(file, line)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return std::stoull(line);
|
||||||
|
} catch (...) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::unordered_map<std::string, uint64_t> read_memory_stat() {
|
||||||
|
std::unordered_map<std::string, uint64_t> stats;
|
||||||
|
std::ifstream file("/sys/fs/cgroup/memory.stat");
|
||||||
|
if (!file.is_open()) {
|
||||||
|
return stats;
|
||||||
|
}
|
||||||
|
std::string line;
|
||||||
|
while (std::getline(file, line)) {
|
||||||
|
size_t space_pos = line.find(' ');
|
||||||
|
if (space_pos != std::string::npos) {
|
||||||
|
std::string key = line.substr(0, space_pos);
|
||||||
|
std::string val_str = line.substr(space_pos + 1);
|
||||||
|
try {
|
||||||
|
uint64_t val = std::stoull(val_str);
|
||||||
|
stats[key] = val;
|
||||||
|
} catch (...) {
|
||||||
|
return stats;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return stats;
|
||||||
|
}
|
||||||
|
|
||||||
static uint64_t device_cgroup_physical_memory(bool available) {
|
static uint64_t device_cgroup_physical_memory(bool available) {
|
||||||
uint64_t memory_info = 0;
|
|
||||||
const char * file_path = nullptr;
|
const char * file_path = nullptr;
|
||||||
|
|
||||||
std::ifstream cgroup_file("/proc/cgroups");
|
|
||||||
bool is_cgroup_v2 = false;
|
bool is_cgroup_v2 = false;
|
||||||
if (cgroup_file.is_open()) {
|
{
|
||||||
std::string line;
|
std::ifstream cgroup_file("/proc/cgroups");
|
||||||
while (std::getline(cgroup_file, line)) {
|
if (cgroup_file.is_open()) {
|
||||||
if (line.find("0") != std::string::npos) {
|
std::string line;
|
||||||
is_cgroup_v2 = true;
|
while (std::getline(cgroup_file, line)) {
|
||||||
break;
|
if (line.find("0") != std::string::npos) {
|
||||||
|
is_cgroup_v2 = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cgroup_file.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_cgroup_v2) {
|
if (!available) {
|
||||||
file_path = available
|
if (is_cgroup_v2) {
|
||||||
? "/sys/fs/cgroup/memory.current"
|
file_path = "/sys/fs/cgroup/memory.max";
|
||||||
: "/sys/fs/cgroup/memory.max";
|
} else {
|
||||||
} else {
|
file_path = "/sys/fs/cgroup/memory/memory.limit_in_bytes";
|
||||||
file_path = available
|
|
||||||
? "/sys/fs/cgroup/memory/memory.usage_in_bytes"
|
|
||||||
: "/sys/fs/cgroup/memory/memory.limit_in_bytes";
|
|
||||||
}
|
|
||||||
|
|
||||||
std::ifstream file(file_path);
|
|
||||||
if (file.is_open()) {
|
|
||||||
std::string line;
|
|
||||||
if (std::getline(file, line)) {
|
|
||||||
try {
|
|
||||||
memory_info = std::stoull(line);
|
|
||||||
} catch (const std::exception &e) {
|
|
||||||
memory_info = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
file.close();
|
return read_value_from_file(file_path);
|
||||||
} else {
|
} else {
|
||||||
memory_info = 0;
|
if (is_cgroup_v2) {
|
||||||
}
|
uint64_t mem_max = read_value_from_file("/sys/fs/cgroup/memory.max");
|
||||||
|
uint64_t mem_current = read_value_from_file("/sys/fs/cgroup/memory.current");
|
||||||
|
|
||||||
return memory_info;
|
auto stats = read_memory_stat();
|
||||||
|
|
||||||
|
uint64_t slab_reclaimable = 0;
|
||||||
|
uint64_t inactive_file = 0;
|
||||||
|
|
||||||
|
if (stats.find("slab_reclaimable") != stats.end()) {
|
||||||
|
slab_reclaimable = stats["slab_reclaimable"];
|
||||||
|
}
|
||||||
|
if (stats.find("inactive_file") != stats.end()) {
|
||||||
|
inactive_file = stats["inactive_file"];
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t available_memory = std::max(mem_max - mem_current, 0);
|
||||||
|
available_memory += slab_reclaimable;
|
||||||
|
available_memory += inactive_file;
|
||||||
|
return available_memory;
|
||||||
|
} else {
|
||||||
|
LOG_WRN("Using cgroup v1, the available memory could be error, will be addressed later\n");
|
||||||
|
uint64_t mem_limit = read_value_from_file("/sys/fs/cgroup/memory/memory.limit_in_bytes");
|
||||||
|
uint64_t mem_usage = read_value_from_file("/sys/fs/cgroup/memory/memory.usage_in_bytes");
|
||||||
|
return std::max(mem_limit - mem_usage, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t device_physical_memory(bool available) {
|
uint64_t device_physical_memory(bool available) {
|
||||||
|
@ -1408,6 +1457,7 @@ static float device_mem_copy_delay(struct llama_model * model, const struct llam
|
||||||
float layer_delay_cuda = device_cuda_mem_copy(model);
|
float layer_delay_cuda = device_cuda_mem_copy(model);
|
||||||
return layer_delay_cuda * n_gpu_layers + layer_delay_cpu * (n_layers - n_gpu_layers);
|
return layer_delay_cuda * n_gpu_layers + layer_delay_cpu * (n_layers - n_gpu_layers);
|
||||||
#else
|
#else
|
||||||
|
(void)n_gpu_layers;
|
||||||
return layer_delay_cpu * n_layers;
|
return layer_delay_cpu * n_layers;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue