Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration test: cgroup v1 relative-cpus tests #2898

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
78 changes: 78 additions & 0 deletions tests/contest/contest/src/tests/cgroups/cpu/v1.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use std::fs;
use std::path::Path;
use std::string::ToString;

use anyhow::Result;
use libcgroups::common;
use libcgroups::v1::{util, ControllerType};
use num_cpus;
use test_framework::{test_result, ConditionalTest, TestGroup, TestResult};

Expand Down Expand Up @@ -219,6 +223,74 @@ fn test_cpu_cgroups() -> TestResult {
TestResult::Passed
}

fn check_cgroup_subsystem(
cgroup_name: &str,
subsystem: &ControllerType,
filename: &str,
expected: &dyn ToString,
) -> Result<()> {
let mount_point = util::get_subsystem_mount_point(subsystem)?;
let cgroup_path = mount_point
.join("runtime-test")
.join(cgroup_name)
.join(filename);

let content = fs::read_to_string(&cgroup_path)?;
let trimmed = content.trim();
assert_eq!(trimmed, expected.to_string());
Ok(())
}

fn test_relative_cpus() -> TestResult {
let case = test_result!(create_cpu_spec(
1024,
100000,
50000,
None,
"0-1",
"0",
get_realtime_period(),
get_realtime_runtime(),
));
let spec = test_result!(create_spec("test_relative_cpus", case.clone()));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please correct me if I am wrong. After checking the definition of the create_spec function, seems like I already specify the cgroups_path by calling it.

fn create_spec(cgroup_name: &str, case: LinuxCpu) -> Result<Spec> {
    let spec = SpecBuilder::default()
        .linux(
            LinuxBuilder::default()
                .cgroups_path(Path::new("/runtime-test").join(cgroup_name))
                .resources(
                    LinuxResourcesBuilder::default()
                        .cpu(case)
                        .build()
                        .context("failed to build resource spec")?,
                )
                .build()
                .context("failed to build linux spec")?,
        )
        .build()
        .context("failed to build spec")?;

    Ok(spec)
}

If that's the case, I guess I have no need to modify create_cpu_spec interface. Am I right?

Copy link
Member

Choose a reason for hiding this comment

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

Is your intention to edit Spec after calling create_spec? I prefer passing a cgroup path to create_cpu_spec.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for reply. I totally miss the point. I plan to modify create_spec and add an optional argument, since the original code already calls the builder there. Is it better than modify create_cpu_spec?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, it'd be better because create_cpu_spec should only be editing related CPU field,s not the cgroup 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.

I've add a is_relative argument in create_spec. But I have no idea why the CI fail.

Copy link
Member

Choose a reason for hiding this comment

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

@posutsai Could you manually create config.json to confirm if runc supports a relative cgroup 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.

I've tried to run a container with following config.json which specified specific relative cgroupsPath and everything works fine.

{
	"ociVersion": "1.2.0",
	"linux": {
		"resources": {
			"devices": [
				{
					"allow": false,
					"access": "rwm"
				}
			]
		},
		"cgroupsPath": "testdir/runtime-test",
		"namespaces": [
			{
				"type": "pid"
			},
			{
				"type": "network"
			},
			{
				"type": "ipc"
			},
			{
				"type": "uts"
			},
			{
				"type": "mount"
			}
		],
		"maskedPaths": [
			"/proc/acpi",
			"/proc/asound",
			"/proc/kcore",
			"/proc/keys",
			"/proc/latency_stats",
			"/proc/timer_list",
			"/proc/timer_stats",
			"/proc/sched_debug",
			"/sys/firmware",
			"/proc/scsi"
		],
		"readonlyPaths": [
			"/proc/bus",
			"/proc/fs",
			"/proc/irq",
			"/proc/sys",
			"/proc/sysrq-trigger"
		]
	}
}

Also, my runc is version 1.2.4

Copy link
Member

Choose a reason for hiding this comment

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

testdir/runtime-test has not been created in advance, right? Is that okay?

Also, there isn't enough error message, so why not add an error message in the ? part?

Copy link
Contributor Author

@posutsai posutsai Jan 21, 2025

Choose a reason for hiding this comment

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

Does the issue come from the user.slice part? Cause my actual cgroup path is /sys/fs/cgroup/cpu,cpuacct/user.slice/testdir/runtime-test/

Copy link
Member

Choose a reason for hiding this comment

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

Does the issue come from the user.slice part?

I'm not sure, but there is a possibility.
OCI Runtime said that so, /sys/fs/cgroup/cpu,cpuacct/user.slice/$relative-path should be okay to pass.

In the case of a relative path (not starting with /), the runtime MAY interpret the path relative to a runtime-determined location in the cgroups hierarchy.

https://github.com/opencontainers/runtime-spec/blob/main/config-linux.md#cgroups-path


test_outside_container(spec, &|data| {
test_result!(check_container_created(&data));
let cgroup_name = "test_relative_cpus";
test_result!(check_cgroup_subsystem(
cgroup_name,
&ControllerType::CpuAcct,
"cpu.shares",
&case.shares().unwrap(),
));
test_result!(check_cgroup_subsystem(
cgroup_name,
&ControllerType::CpuAcct,
"cpu.cfs_period_us",
&case.period().unwrap(),
));
test_result!(check_cgroup_subsystem(
cgroup_name,
&ControllerType::CpuAcct,
"cpu.cfs_quota_us",
&case.quota().unwrap(),
));
test_result!(check_cgroup_subsystem(
cgroup_name,
&ControllerType::CpuSet,
"cpuset.cpus",
&case.cpus().to_owned().unwrap(),
));
test_result!(check_cgroup_subsystem(
cgroup_name,
&ControllerType::CpuSet,
"cpuset.mems",
&case.mems().to_owned().unwrap()
));
TestResult::Passed
})
}

fn test_empty_cpu() -> TestResult {
let cgroup_name = "test_empty_cpu";
let spec = test_result!(create_empty_spec(cgroup_name));
Expand Down Expand Up @@ -317,12 +389,18 @@ pub fn get_test_group() -> TestGroup {
Box::new(can_run_idle),
Box::new(test_cpu_idle_set),
);
let relative_cpus = ConditionalTest::new(
"test_relative_cpus",
Box::new(can_run),
Box::new(test_relative_cpus),
);

test_group.add(vec![
Box::new(linux_cgroups_cpus),
Box::new(empty_cpu),
Box::new(cpu_idle_set),
Box::new(cpu_idle_default),
Box::new(relative_cpus),
]);

test_group
Expand Down
Loading