Skip to content

Commit

Permalink
5
Browse files Browse the repository at this point in the history
  • Loading branch information
xinyiZzz committed Aug 13, 2024
1 parent 086d027 commit c0f1a43
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 15 deletions.
18 changes: 10 additions & 8 deletions be/src/common/cgroup_memory_ctl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ namespace doris {
// Assumes that cgroupsv2_enable() is enabled.
bool cgroupsv2_memory_controller_enabled() {
#if defined(OS_LINUX)
assert(CGroupUtil::cgroupsv2_enable());
if (!CGroupUtil::cgroupsv2_enable()) {
throw doris::Exception(doris::ErrorCode::CGROUP_ERROR, "cgroupsv2_enable is false");
}
// According to https://docs.kernel.org/admin-guide/cgroup-v2.html, file "cgroup.controllers" defines which controllers are available
// for the current + child cgroups. The set of available controllers can be restricted from level to level using file
// "cgroups.subtree_control". It is therefore sufficient to check the bottom-most nested "cgroup.controllers" file.
Expand All @@ -62,7 +64,7 @@ struct CgroupsV1Reader : CGroupMemoryCtl::ICgroupsReader {
auto st = CGroupUtil::read_int_line_from_cgroup_file(
(_mount_file_dir / "memory.limit_in_bytes"), &value);
if (!st.ok()) {
throw doris::Exception(doris::ErrorCode::END_OF_FILE,
throw doris::Exception(doris::ErrorCode::CGROUP_ERROR,
"Cannot read cgroupv1 memory.limit_in_bytes, " + st.to_string());
}
return value;
Expand All @@ -88,7 +90,7 @@ struct CgroupsV2Reader : CGroupMemoryCtl::ICgroupsReader {
auto st = CGroupUtil::read_int_line_from_cgroup_file((_mount_file_dir / "memory.max"),
&value);
if (!st.ok()) {
throw doris::Exception(doris::ErrorCode::END_OF_FILE,
throw doris::Exception(doris::ErrorCode::CGROUP_ERROR,
"Cannot read cgroupv2 memory.max, " + st.to_string());
}
return value;
Expand All @@ -101,15 +103,15 @@ struct CgroupsV2Reader : CGroupMemoryCtl::ICgroupsReader {
auto st = CGroupUtil::read_int_line_from_cgroup_file((_mount_file_dir / "memory.current"),
&mem_usage);
if (!st.ok()) {
throw doris::Exception(doris::ErrorCode::END_OF_FILE,
throw doris::Exception(doris::ErrorCode::CGROUP_ERROR,
"Cannot read cgroupv2 memory.current, " + st.to_string());
}
std::unordered_map<std::string, int64_t> metrics_map;
CGroupUtil::read_int_metric_from_cgroup_file((_mount_file_dir / "memory.stat"),
metrics_map);
mem_usage -= metrics_map["inactive_file"];
if (mem_usage < 0) {
throw doris::Exception(doris::ErrorCode::END_OF_FILE, "Negative memory usage");
throw doris::Exception(doris::ErrorCode::CGROUP_ERROR, "Negative memory usage");
}
return mem_usage;
}
Expand Down Expand Up @@ -137,7 +139,7 @@ std::pair<std::string, CGroupUtil::CgroupsVersion> get_cgroups_path() {
}

throw doris::Exception(
doris::ErrorCode::END_OF_FILE,
doris::ErrorCode::CGROUP_ERROR,
fmt::format("Cannot find cgroups v1 or v2 current memory file, cgroupsv2_enable: {}, "
"cgroupsv2_memory_controller_enabled: {}, cgroupsv1_enable: {}",
CGroupUtil::cgroupsv2_enable(), cgroupsv2_memory_controller_enabled(),
Expand All @@ -160,7 +162,7 @@ Status CGroupMemoryCtl::find_cgroup_mem_limit(int64_t* bytes) {
return Status::OK();
} catch (const doris::Exception& e) {
LOG(WARNING) << "Cgroup find_cgroup_mem_limit failed, " << e.to_string();
return Status::EndOfFile(e.to_string());
return Status::CgroupError(e.to_string());
}
}

Expand All @@ -170,7 +172,7 @@ Status CGroupMemoryCtl::find_cgroup_mem_usage(int64_t* bytes) {
return Status::OK();
} catch (const doris::Exception& e) {
LOG(WARNING) << "Cgroup find_cgroup_mem_usage failed, " << e.to_string();
return Status::EndOfFile(e.to_string());
return Status::CgroupError(e.to_string());
}
}

Expand Down
2 changes: 2 additions & 0 deletions be/src/common/status.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ namespace ErrorCode {
TStatusError(HTTP_ERROR, true); \
TStatusError(TABLET_MISSING, true); \
TStatusError(NOT_MASTER, true); \
TStatusError(CGROUP_ERROR, false); \
TStatusError(DELETE_BITMAP_LOCK_ERROR, false);
// E error_name, error_code, print_stacktrace
#define APPLY_FOR_OLAP_ERROR_CODES(E) \
Expand Down Expand Up @@ -484,6 +485,7 @@ class [[nodiscard]] Status {
ERROR_CTOR_NOSTACK(NotAuthorized, NOT_AUTHORIZED)
ERROR_CTOR(HttpError, HTTP_ERROR)
ERROR_CTOR_NOSTACK(NeedSendAgain, NEED_SEND_AGAIN)
ERROR_CTOR_NOSTACK(CgroupError, CGROUP_ERROR)
#undef ERROR_CTOR

template <int code>
Expand Down
17 changes: 10 additions & 7 deletions be/src/util/cgroup_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ Status CGroupUtil::find_global_cgroupv1(const string& subsystem, string* path) {
string line;
while (true) {
if (proc_cgroups.fail()) {
return Status::EndOfFile("Error reading /proc/self/cgroup: {}", get_str_err_msg());
return Status::CgroupError("Error reading /proc/self/cgroup: {}", get_str_err_msg());
} else if (proc_cgroups.peek() == std::ifstream::traits_type::eof()) {
return Status::EndOfFile("Could not find subsystem {} in /proc/self/cgroup", subsystem);
return Status::CgroupError("Could not find subsystem {} in /proc/self/cgroup",
subsystem);
}
// The line format looks like this:
// 4:memory:/user.slice
Expand Down Expand Up @@ -102,10 +103,10 @@ Status CGroupUtil::find_cgroupv1_mounts(const string& subsystem, pair<string, st
string line;
while (true) {
if (mountinfo.fail() || mountinfo.bad()) {
return Status::EndOfFile("Error reading /proc/self/mountinfo: {}", get_str_err_msg());
return Status::CgroupError("Error reading /proc/self/mountinfo: {}", get_str_err_msg());
} else if (mountinfo.eof()) {
return Status::EndOfFile("Could not find subsystem {} in /proc/self/mountinfo",
subsystem);
return Status::CgroupError("Could not find subsystem {} in /proc/self/mountinfo",
subsystem);
}
// The relevant lines look like below (see proc manpage for full documentation). The
// first example is running outside of a container, the second example is running
Expand Down Expand Up @@ -167,7 +168,9 @@ Status CGroupUtil::find_abs_cgroupv1_path(const string& subsystem, string* path)

std::string CGroupUtil::cgroupv2_of_process() {
#if defined(OS_LINUX)
assert(cgroupsv2_enable());
if (!cgroupsv2_enable()) {
return "";
}
// All PIDs assigned to a cgroup are in /sys/fs/cgroups/{cgroup_name}/cgroup.procs
// A simpler way to get the membership is:
std::ifstream cgroup_name_file("/proc/self/cgroup");
Expand Down Expand Up @@ -214,7 +217,7 @@ Status CGroupUtil::read_int_line_from_cgroup_file(const std::filesystem::path& f
string line;
getline(file_stream, line);
if (file_stream.fail() || file_stream.bad()) {
return Status::EndOfFile("Error reading {}: {}", file_path.string(), get_str_err_msg());
return Status::CgroupError("Error reading {}: {}", file_path.string(), get_str_err_msg());
}
StringParser::ParseResult pr;
// Parse into an int64_t If it overflows, returning the max value of int64_t is ok because that
Expand Down
19 changes: 19 additions & 0 deletions be/test/util/cgroup_util_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

#include "common/cgroup_memory_ctl.h"
#include "gtest/gtest_pred_impl.h"
#include "testutil/test_util.h"
#include "util/cgroup_util.h"

namespace doris {

Expand All @@ -34,6 +36,23 @@ class CGroupUtilTest : public ::testing::Test {
~CGroupUtilTest() override = default;
};

TEST_F(CGroupUtilTest, ReadMetrics) {
std::string dir_path = GetCurrentRunningDir();
std::string memory_limit_in_bytes_path(dir_path);
memory_limit_in_bytes_path += "/util/test_data/memory.limit_in_bytes";
int64_t value;
auto st = CGroupUtil::read_int_line_from_cgroup_file(memory_limit_in_bytes_path, &value);
EXPECT_TRUE(st.ok());
EXPECT_EQ(6291456000, value);

std::string memory_stat_path(dir_path);
memory_stat_path += "/util/test_data/memory.stat";
std::unordered_map<std::string, int64_t> metrics_map;
CGroupUtil::read_int_metric_from_cgroup_file(memory_stat_path, metrics_map);
EXPECT_EQ(5443584, metrics_map["inactive_file"]);
EXPECT_EQ(0, metrics_map["rss"]);
}

TEST_F(CGroupUtilTest, memlimit) {
LOG(INFO) << CGroupMemoryCtl::debug_string();
int64_t mem_limit;
Expand Down

0 comments on commit c0f1a43

Please sign in to comment.