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 cbfdbd802 [cgroups2] Introduces the cpu namespace and cpu::weight() API
cbfdbd802 is described below
commit cbfdbd8020c68baf78b4d9c0a87d0653fc1e953f
Author: Devin Leamy <[email protected]>
AuthorDate: Wed Mar 20 12:05:44 2024 -0400
[cgroups2] Introduces the cpu namespace and cpu::weight() API
Introduces the `cpu` namespace for cgroups2, and expose an API to set
the CPU weight for a cgroup.
In cgroups v2, CPU cycles are allocated based on weights that are assigned
to cgroups, using the "cpu.weight" control. Specifically, a cgroup has
access
to 100*(weight/total_weight)% of CPU access.
This closes #522
---
src/linux/cgroups2.cpp | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++
src/linux/cgroups2.hpp | 13 +++++++++++
2 files changed, 73 insertions(+)
diff --git a/src/linux/cgroups2.cpp b/src/linux/cgroups2.cpp
index 88060e02b..ee3013373 100644
--- a/src/linux/cgroups2.cpp
+++ b/src/linux/cgroups2.cpp
@@ -22,6 +22,7 @@
#include <string>
#include <vector>
+#include <stout/numify.hpp>
#include <stout/os.hpp>
#include <stout/path.hpp>
#include <stout/stringify.hpp>
@@ -150,6 +151,18 @@ Try<string> read(const string& cgroup, const string&
control)
}
+template <>
+Try<uint64_t> read(const string& cgroup, const string& control)
+{
+ Try<string> content = read<string>(cgroup, control);
+ if (content.isError()) {
+ return Error(content.error());
+ }
+
+ return numify<uint64_t>(strings::trim(*content));
+}
+
+
template <>
Try<Nothing> write(
const string& cgroup,
@@ -160,6 +173,16 @@ Try<Nothing> write(
}
+template <>
+Try<Nothing> write(
+ const string& cgroup,
+ const string& control,
+ const uint64_t& value)
+{
+ return write(cgroup, control, stringify(value));
+}
+
+
bool enabled()
{
Try<bool> supported = mesos::internal::fs::supported(cgroups2::FILE_SYSTEM);
@@ -382,4 +405,41 @@ Try<set<string>> enabled(const string& cgroup)
} // namespace controllers {
+namespace cpu {
+
+namespace control {
+
+const std::string IDLE = "cpu.idle";
+const std::string MAX = "cpu.max";
+const std::string MAX_BURST = "cpu.max.burst";
+const std::string PRESSURE = "cpu.pressure";
+const std::string STATS = "cpu.stat";
+const std::string UCLAMP_MAX = "cpu.uclamp.max";
+const std::string UCLAMP_MIN = "cpu.uclamp.min";
+const std::string WEIGHT = "cpu.weight";
+const std::string WEIGHT_NICE = "cpu.weight.nice";
+
+} // namespace control {
+
+Try<Nothing> weight(const string& cgroup, uint64_t weight)
+{
+ if (cgroup == ROOT_CGROUP) {
+ return Error("Operation not supported for the root cgroup");
+ }
+
+ return cgroups2::write(cgroup, cpu::control::WEIGHT, weight);
+}
+
+
+Try<uint64_t> weight(const string& cgroup)
+{
+ if (cgroup == ROOT_CGROUP) {
+ return Error("Operation not supported for the root cgroup");
+ }
+
+ return cgroups2::read<uint64_t>(cgroup, cpu::control::WEIGHT);
+}
+
+} // namespace cpu {
+
} // namespace cgroups2 {
diff --git a/src/linux/cgroups2.hpp b/src/linux/cgroups2.hpp
index 35519cc60..b24e8c75d 100644
--- a/src/linux/cgroups2.hpp
+++ b/src/linux/cgroups2.hpp
@@ -95,6 +95,19 @@ Try<std::set<std::string>> enabled(const std::string&
cgroup);
} // namespace controllers {
+namespace cpu {
+
+// Set the cpu weight for a cgroup, weight must be in the [1, 10000] range.
+// Cannot be used for the root cgroup.
+Try<Nothing> weight(const std::string& cgroup, uint64_t weight);
+
+
+// Retrieve the cpu weight for a cgroup, weight will be in the [1, 10000]
range.
+// Cannot be used for the root cgroup.
+Try<uint64_t> weight(const std::string& cgroup);
+
+} // namespace cpu {
+
} // namespace cgroups2 {
#endif // __CGROUPS_V2_HPP__