Skip to content

Commit

Permalink
8
Browse files Browse the repository at this point in the history
  • Loading branch information
xinyiZzz committed Aug 13, 2024
1 parent 01551e8 commit 6f81b7e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 24 deletions.
22 changes: 11 additions & 11 deletions be/src/common/cgroup_memory_ctl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ struct CgroupsV1Reader : CGroupMemoryCtl::ICgroupsReader {
explicit CgroupsV1Reader(std::filesystem::path mount_file_dir)
: _mount_file_dir(std::move(mount_file_dir)) {}

Status read_memory_limit(uint64_t* value) override {
Status read_memory_limit(int64_t* value) override {
RETURN_IF_ERROR(CGroupUtil::read_int_line_from_cgroup_file(
(_mount_file_dir / "memory.limit_in_bytes"), (int64_t*)value));
(_mount_file_dir / "memory.limit_in_bytes"), value));
return Status::OK();
}

Status read_memory_usage(uint64_t* value) override {
Status read_memory_usage(int64_t* value) override {
std::unordered_map<std::string, int64_t> metrics_map;
CGroupUtil::read_int_metric_from_cgroup_file((_mount_file_dir / "memory.stat"),
metrics_map);
Expand All @@ -83,17 +83,17 @@ struct CgroupsV2Reader : CGroupMemoryCtl::ICgroupsReader {
explicit CgroupsV2Reader(std::filesystem::path mount_file_dir)
: _mount_file_dir(std::move(mount_file_dir)) {}

Status read_memory_limit(uint64_t* value) override {
Status read_memory_limit(int64_t* value) override {
RETURN_IF_ERROR(CGroupUtil::read_int_line_from_cgroup_file((_mount_file_dir / "memory.max"),
(int64_t*)value));
value));
return Status::OK();
}

Status read_memory_usage(uint64_t* value) override {
Status read_memory_usage(int64_t* value) override {
// memory.current contains a single number
// the reason why we subtract it described here: https://github.com/ClickHouse/ClickHouse/issues/64652#issuecomment-2149630667
RETURN_IF_ERROR(CGroupUtil::read_int_line_from_cgroup_file(
(_mount_file_dir / "memory.current"), (int64_t*)value));
(_mount_file_dir / "memory.current"), value));
std::unordered_map<std::string, int64_t> metrics_map;
CGroupUtil::read_int_metric_from_cgroup_file((_mount_file_dir / "memory.stat"),
metrics_map);
Expand Down Expand Up @@ -152,14 +152,14 @@ Status get_cgroups_reader(std::shared_ptr<CGroupMemoryCtl::ICgroupsReader>& read
return Status::OK();
}

Status CGroupMemoryCtl::find_cgroup_mem_limit(uint64_t* bytes) {
Status CGroupMemoryCtl::find_cgroup_mem_limit(int64_t* bytes) {
std::shared_ptr<CGroupMemoryCtl::ICgroupsReader> reader;
RETURN_IF_ERROR(get_cgroups_reader(reader));
RETURN_IF_ERROR(reader->read_memory_limit(bytes));
return Status::OK();
}

Status CGroupMemoryCtl::find_cgroup_mem_usage(uint64_t* bytes) {
Status CGroupMemoryCtl::find_cgroup_mem_usage(int64_t* bytes) {
std::shared_ptr<CGroupMemoryCtl::ICgroupsReader> reader;
RETURN_IF_ERROR(get_cgroups_reader(reader));
RETURN_IF_ERROR(reader->read_memory_usage(bytes));
Expand All @@ -178,10 +178,10 @@ std::string CGroupMemoryCtl::debug_string() {
CGroupUtil::cgroupsv1_enable());
}

uint64_t mem_limit;
int64_t mem_limit;
auto mem_limit_st = find_cgroup_mem_limit(&mem_limit);

uint64_t mem_usage;
int64_t mem_usage;
auto mem_usage_st = find_cgroup_mem_usage(&mem_usage);

return fmt::format(
Expand Down
8 changes: 4 additions & 4 deletions be/src/common/cgroup_memory_ctl.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ class CGroupMemoryCtl {
struct ICgroupsReader {
virtual ~ICgroupsReader() = default;

virtual Status read_memory_limit(uint64_t* value) = 0;
virtual Status read_memory_limit(int64_t* value) = 0;

virtual Status read_memory_usage(uint64_t* value) = 0;
virtual Status read_memory_usage(int64_t* value) = 0;
};

// Determines the CGroup memory limit from the current processes' cgroup.
// If the limit is more than INT64_MAX, INT64_MAX is returned (since that is
// effectively unlimited anyway). Does not take into account memory limits
// set on any ancestor CGroups.
static Status find_cgroup_mem_limit(uint64_t* bytes);
static Status find_cgroup_mem_limit(int64_t* bytes);

// https://serverfault.com/questions/902009/the-memory-usage-reported-in-cgroup-differs-from-the-free-command
static Status find_cgroup_mem_usage(uint64_t* bytes);
static Status find_cgroup_mem_usage(int64_t* bytes);

// Returns a human-readable string with information about CGroups.
static std::string debug_string();
Expand Down
13 changes: 6 additions & 7 deletions be/src/util/mem_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ std::atomic<int64_t> MemInfo::_s_je_dirty_pages_mem = std::numeric_limits<int64_
std::atomic<int64_t> MemInfo::_s_je_dirty_pages_mem_limit = std::numeric_limits<int64_t>::max();
std::atomic<int64_t> MemInfo::_s_virtual_memory_used = 0;

uint64_t MemInfo::_s_cgroup_mem_limit = std::numeric_limits<uint64_t>::max();
uint64_t MemInfo::_s_cgroup_mem_usage = std::numeric_limits<uint64_t>::min();
int64_t MemInfo::_s_cgroup_mem_limit = std::numeric_limits<int64_t>::max();
int64_t MemInfo::_s_cgroup_mem_usage = std::numeric_limits<int64_t>::min();
bool MemInfo::_s_cgroup_mem_refresh_state = false;
int64_t MemInfo::_s_cgroup_mem_refresh_wait_times = 0;

Expand Down Expand Up @@ -190,8 +190,8 @@ void MemInfo::refresh_proc_meminfo() {

// refresh cgroup memory
if (_s_cgroup_mem_refresh_wait_times >= 0 && config::enable_use_cgroup_memory_info) {
uint64_t cgroup_mem_limit = -1;
uint64_t cgroup_mem_usage = -1;
int64_t cgroup_mem_limit = -1;
int64_t cgroup_mem_usage = -1;
std::string cgroup_mem_info_file_path;
_s_cgroup_mem_refresh_state = true;
Status status = CGroupMemoryCtl::find_cgroup_mem_limit(&cgroup_mem_limit);
Expand Down Expand Up @@ -234,7 +234,7 @@ void MemInfo::refresh_proc_meminfo() {
if (physical_mem < 0) {
physical_mem = _s_cgroup_mem_limit;
} else {
physical_mem = std::min(physical_mem, (int64_t)_s_cgroup_mem_limit);
physical_mem = std::min(physical_mem, _s_cgroup_mem_limit);
}
}

Expand Down Expand Up @@ -280,8 +280,7 @@ void MemInfo::refresh_proc_meminfo() {
if (mem_available < 0) {
mem_available = _s_cgroup_mem_limit - _s_cgroup_mem_usage;
} else {
mem_available =
std::min(mem_available, (int64_t)(_s_cgroup_mem_limit - _s_cgroup_mem_usage));
mem_available = std::min(mem_available, (_s_cgroup_mem_limit - _s_cgroup_mem_usage));
}
}
if (mem_available < 0) {
Expand Down
4 changes: 2 additions & 2 deletions be/src/util/mem_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ class MemInfo {
static std::atomic<int64_t> _s_je_dirty_pages_mem_limit;
static std::atomic<int64_t> _s_virtual_memory_used;

static uint64_t _s_cgroup_mem_limit;
static uint64_t _s_cgroup_mem_usage;
static int64_t _s_cgroup_mem_limit;
static int64_t _s_cgroup_mem_usage;
static bool _s_cgroup_mem_refresh_state;
static int64_t _s_cgroup_mem_refresh_wait_times;

Expand Down

0 comments on commit 6f81b7e

Please sign in to comment.