yiguolei commented on code in PR #39256:
URL: https://github.com/apache/doris/pull/39256#discussion_r1715047507


##########
be/src/common/cgroup_memory_ctl.cpp:
##########
@@ -0,0 +1,200 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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.
+// This file is copied from
+// 
https://github.com/ClickHouse/ClickHouse/blob/master/src/Common/CgroupsMemoryUsageObserver.cpp
+// and modified by Doris
+
+#include "common/cgroup_memory_ctl.h"
+
+#include <filesystem>
+#include <fstream>
+#include <memory>
+#include <utility>
+
+#include "common/exception.h"
+#include "common/status.h"
+#include "util/cgroup_util.h"
+
+namespace doris {
+
+// Is the memory controller of cgroups v2 enabled on the system?
+// Assumes that cgroupsv2_enable() is enabled.
+bool cgroupsv2_memory_controller_enabled() {
+#if defined(OS_LINUX)
+    assert(CGroupUtil::cgroupsv2_enable());
+    // 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.
+    std::string cgroup = CGroupUtil::cgroupv2_of_process();
+    auto cgroup_dir = cgroup.empty() ? default_cgroups_mount : 
(default_cgroups_mount / cgroup);
+    std::ifstream controllers_file(cgroup_dir / "cgroup.controllers");
+    if (!controllers_file.is_open()) {
+        return false;
+    }
+    std::string controllers;
+    std::getline(controllers_file, controllers);
+    return controllers.find("memory") != std::string::npos;
+#else
+    return false;
+#endif
+}
+
+struct CgroupsV1Reader : CGroupMemoryCtl::ICgroupsReader {
+    explicit CgroupsV1Reader(std::filesystem::path mount_file_dir)
+            : _mount_file_dir(std::move(mount_file_dir)) {}
+
+    uint64_t read_memory_limit() override {
+        int64_t value;
+        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,
+                                   "Cannot read cgroupv1 
memory.limit_in_bytes, " + st.to_string());
+        }
+        return value;
+    }
+
+    uint64_t read_memory_usage() override {
+        std::unordered_map<std::string, int64_t> metrics_map;
+        CGroupUtil::read_int_metric_from_cgroup_file((_mount_file_dir / 
"memory.stat"),
+                                                     metrics_map);
+        return metrics_map["rss"];
+    }
+
+private:
+    std::filesystem::path _mount_file_dir;
+};
+
+struct CgroupsV2Reader : CGroupMemoryCtl::ICgroupsReader {
+    explicit CgroupsV2Reader(std::filesystem::path mount_file_dir)
+            : _mount_file_dir(std::move(mount_file_dir)) {}
+
+    uint64_t read_memory_limit() override {
+        int64_t value;
+        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,

Review Comment:
   同上



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to