Skip to content
Merged
35 changes: 15 additions & 20 deletions src/ray/common/cgroup2/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ ray_cc_library(
":is_linux": ["cgroup_manager.cc"],
"//conditions:default": ["noop_cgroup_manager.cc"],
}),
hdrs = ["cgroup_manager.h"],
hdrs = [
"cgroup_manager.h",
"scoped_cgroup_operation.h",
],
visibility = ["//visibility:public"],
deps = [
":cgroup_driver_interface",
Expand All @@ -21,7 +24,6 @@ ray_cc_library(
"//src/ray/common:status_or",
] + select({
":is_linux": [
":scoped_cgroup_operation",
"//src/ray/util:logging",
"@com_google_absl//absl/strings",
],
Expand Down Expand Up @@ -55,35 +57,28 @@ ray_cc_library(

ray_cc_library(
name = "sysfs_cgroup_driver",
srcs = ["sysfs_cgroup_driver.cc"],
srcs = select({
":is_linux": ["sysfs_cgroup_driver.cc"],
"//conditions:default": ["noop_sysfs_cgroup_driver.cc"],
}),
hdrs = [
"sysfs_cgroup_driver.h",
],
target_compatible_with = [
"@platforms//os:linux",
],
visibility = ["//visibility:public"],
deps = [
":cgroup_driver_interface",
"//src/ray/common:status",
"//src/ray/common:status_or",
"//src/ray/util:logging",
"@com_google_absl//absl/strings",
],
] + select({
":is_linux": [
"//src/ray/util:logging",
"@com_google_absl//absl/strings",
],
"//conditions:default": [],
}),
)

# Private Targets.
ray_cc_library(
name = "scoped_cgroup_operation",
hdrs = [
"scoped_cgroup_operation.h",
],
target_compatible_with = [
"@platforms//os:linux",
],
visibility = [":__subpackages__"],
)

ray_cc_library(
name = "fake_cgroup_driver",
hdrs = [
Expand Down
74 changes: 74 additions & 0 deletions src/ray/common/cgroup2/noop_sysfs_cgroup_driver.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2025 The Ray Authors.
//
// Licensed 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.

#include <string>
#include <unordered_set>

#include "ray/common/cgroup2/sysfs_cgroup_driver.h"
#include "ray/common/status.h"
#include "ray/common/status_or.h"

namespace ray {
Status SysFsCgroupDriver::CheckCgroupv2Enabled() { return Status::OK(); }

Status SysFsCgroupDriver::CheckCgroup(const std::string &cgroup_path) {
return Status::OK();
}

Status SysFsCgroupDriver::CreateCgroup(const std::string &cgroup_path) {
return Status::OK();
}

Status SysFsCgroupDriver::DeleteCgroup(const std::string &cgroup_path) {
return Status::OK();
}

StatusOr<std::unordered_set<std::string>> SysFsCgroupDriver::GetAvailableControllers(
const std::string &cgroup_dir) {
return std::unordered_set<std::string>{};
}

StatusOr<std::unordered_set<std::string>> SysFsCgroupDriver::GetEnabledControllers(
const std::string &cgroup_dir) {
return std::unordered_set<std::string>{};
}

Status SysFsCgroupDriver::MoveAllProcesses(const std::string &from,
const std::string &to) {
return Status::OK();
}

Status SysFsCgroupDriver::EnableController(const std::string &cgroup_path,
const std::string &controller) {
return Status::OK();
}

Status SysFsCgroupDriver::DisableController(const std::string &cgroup_path,
const std::string &controller) {
return Status::OK();
}

Status SysFsCgroupDriver::AddConstraint(const std::string &cgroup_path,
const std::string &controller,
const std::string &constraint,
const std::string &constraint_value) {
return Status::OK();
}

StatusOr<std::unordered_set<std::string>> SysFsCgroupDriver::ReadControllerFile(
const std::string &controller_file_path) {
return std::unordered_set<std::string>{};
}

} // namespace ray
6 changes: 6 additions & 0 deletions src/ray/common/cgroup2/sysfs_cgroup_driver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
#include "ray/common/status.h"
#include "ray/common/status_or.h"

// Used to identify if a filesystem is mounted using cgroupv2.
// See: https://docs.kernel.org/admin-guide/cgroup-v2.html#mounting
#ifndef CGROUP2_SUPER_MAGIC
#define CGROUP2_SUPER_MAGIC 0x63677270
#endif

Comment on lines +40 to +45
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved here to make the header file support non-linux systems.

namespace ray {
Status SysFsCgroupDriver::CheckCgroupv2Enabled() {
FILE *fp = setmntent(mount_file_path_.c_str(), "r");
Expand Down
18 changes: 6 additions & 12 deletions src/ray/common/cgroup2/sysfs_cgroup_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
// limitations under the License.
#pragma once

#include <linux/magic.h>
#include <mntent.h>
// TODO(#54703): SysFsCgroupDriver should not be a public target.
// It will be hidden behind a CgroupManagerFactory which will create
// an appropriate depending on configuration and platform.
// #include <mntent.h>

#include <string>
#include <unordered_set>
Expand All @@ -24,12 +26,6 @@
#include "ray/common/status.h"
#include "ray/common/status_or.h"

// Used to identify if a filesystem is mounted using cgroupv2.
// See: https://docs.kernel.org/admin-guide/cgroup-v2.html#mounting
#ifndef CGROUP2_SUPER_MAGIC
#define CGROUP2_SUPER_MAGIC 0x63677270
#endif

namespace ray {

/**
Expand All @@ -46,12 +42,9 @@ namespace ray {
class SysFsCgroupDriver : public CgroupDriverInterface {
public:
/**
* MOUNTED is defined in mntent.h (and typically refers to /etc/mtab)
* @see https://www.gnu.org/software/libc/manual/2.24/html_node/Mount-Information.html
*
* @param mount_file_path only used for testing.
*/
explicit SysFsCgroupDriver(std::string mount_file_path = MOUNTED)
explicit SysFsCgroupDriver(std::string mount_file_path = kMountFilePath)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was /etc/mtab on line 45 and now it's /mnt/mtab? - also some comments about MOUNTED are not longer valid

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I've deleted the comments and changed it to /etc/mtab. I need to figure out why the cgroup tests passed with the wrong mount path.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, the relevant integration tests are introduced in a later PR. This would break tests in #56352.

: mount_file_path_(std::move(mount_file_path)) {}

~SysFsCgroupDriver() override = default;
Expand Down Expand Up @@ -280,5 +273,6 @@ class SysFsCgroupDriver : public CgroupDriverInterface {
static constexpr std::string_view kCgroupSubtreeControlFilename =
"cgroup.subtree_control";
static constexpr std::string_view kCgroupControllersFilename = "cgroup.controllers";
static inline std::string kMountFilePath = "/etc/mtab";
};
} // namespace ray