You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by bm...@apache.org on 2024/04/11 21:15:55 UTC

(mesos) branch master updated: [cgroups2] Introduce `memory` controller.

This is an automated email from the ASF dual-hosted git repository.

bmahler pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mesos.git


The following commit(s) were added to refs/heads/master by this push:
     new 117e6e373 [cgroups2] Introduce `memory` controller.
117e6e373 is described below

commit 117e6e3735504a73fe4d1ad9d8eb2d59bf45143c
Author: Devin Leamy <dl...@twitter.com>
AuthorDate: Thu Apr 11 17:12:35 2024 -0400

    [cgroups2] Introduce `memory` controller.
    
    Introduces the cgroups v2 `memory` controller, the `cgroups2::memory::usage`
    function, to obtain the memory usage of a cgroup and its descendants', and a test.
    
    This closes #553
---
 src/linux/cgroups2.cpp                     | 21 +++++++++++++++++++++
 src/linux/cgroups2.hpp                     |  8 ++++++++
 src/tests/containerizer/cgroups2_tests.cpp | 14 ++++++++++++++
 3 files changed, 43 insertions(+)

diff --git a/src/linux/cgroups2.cpp b/src/linux/cgroups2.cpp
index 00534d755..50e4285f2 100644
--- a/src/linux/cgroups2.cpp
+++ b/src/linux/cgroups2.cpp
@@ -767,6 +767,27 @@ Try<cpu::BandwidthLimit> max(const string& cgroup)
 
 } // namespace cpu {
 
+namespace memory {
+
+namespace control {
+
+const string CURRENT = "memory.current";
+
+} // namespace control {
+
+Try<Bytes> usage(const string& cgroup)
+{
+  Try<uint64_t> contents = cgroups2::read<uint64_t>(
+      cgroup, memory::control::CURRENT);
+  if (contents.isError()) {
+    return Error("Failed to read 'memory.current': " + contents.error());
+  }
+
+  return Bytes(*contents);
+}
+
+} // namespace memory {
+
 namespace devices {
 
 // Utility class to construct an eBPF program to whitelist or blacklist
diff --git a/src/linux/cgroups2.hpp b/src/linux/cgroups2.hpp
index e4f482405..02fbc1cf7 100644
--- a/src/linux/cgroups2.hpp
+++ b/src/linux/cgroups2.hpp
@@ -21,6 +21,7 @@
 #include <string>
 #include <vector>
 
+#include <stout/bytes.hpp>
 #include <stout/duration.hpp>
 #include <stout/nothing.hpp>
 #include <stout/option.hpp>
@@ -214,6 +215,13 @@ Try<BandwidthLimit> max(const std::string& cgroup);
 
 } // namespace cpu {
 
+namespace memory {
+
+// Current memory usage of a cgroup and its descendants in bytes.
+Try<Bytes> usage(const std::string& cgroup);
+
+} // namespace memory {
+
 namespace devices {
 
 using cgroups::devices::Entry;
diff --git a/src/tests/containerizer/cgroups2_tests.cpp b/src/tests/containerizer/cgroups2_tests.cpp
index 5c0a3e7b2..bf6e86c56 100644
--- a/src/tests/containerizer/cgroups2_tests.cpp
+++ b/src/tests/containerizer/cgroups2_tests.cpp
@@ -311,6 +311,20 @@ TEST_F(Cgroups2Test, ROOT_CGROUPS2_CpuBandwidthLimit)
 }
 
 
+TEST_F(Cgroups2Test, ROOT_CGROUPS2_MemoryUsage)
+{
+  ASSERT_SOME(enable_controllers({"memory"}));
+
+  ASSERT_SOME(cgroups2::create(TEST_CGROUP));
+  ASSERT_SOME(cgroups2::controllers::enable(TEST_CGROUP, {"memory"}));
+
+  // Does not exist for the root cgroup.
+  EXPECT_ERROR(cgroups2::memory::usage(cgroups2::ROOT_CGROUP));
+
+  EXPECT_SOME(cgroups2::memory::usage(TEST_CGROUP));
+}
+
+
 TEST_F(Cgroups2Test, ROOT_CGROUPS2_GetCgroups)
 {
   vector<string> cgroups = {