Skip to content

Commit

Permalink
feat: linux control group version 2 API support cgroup v2 (#1262)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChangxinDong committed Oct 10, 2022
1 parent 3981030 commit 4f9c07d
Show file tree
Hide file tree
Showing 5 changed files with 501 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

package com.aws.greengrass.util.platforms.unix.linux;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import java.nio.file.Path;
import java.nio.file.Paths;

/**
* Represents Linux cgroup v2.
*/
@SuppressFBWarnings(value = "DMI_HARDCODED_ABSOLUTE_FILENAME", justification = "Cgroup virtual filesystem path "
+ "cannot be relative")
public final class CgroupV2 {

private static final String CGROUP_ROOT = "/sys/fs/cgroup";
private static final String GG_NAMESPACE = "greengrass";
private static final String CPU_MAX = "cpu.max";
private static final String MEMORY_MAX = "memory.max";
private static final String CGROUP_PROCS = "cgroup.procs";
private static final String CGROUP_SUBTREE_CONTROL = "cgroup.subtree_control";
private static final String CGROUP_FREEZE = "cgroup.freeze";

private CgroupV2() {
}

public static Path getRootPath() {
return Paths.get(CGROUP_ROOT);
}

public static String rootMountCmd() {
return String.format("mount -t cgroup2 none %s", CGROUP_ROOT);
}

public static Path getSubsystemRootPath() {
return Paths.get(CGROUP_ROOT);
}

public static Path getRootSubTreeControlPath() {
return getSubsystemRootPath().resolve(CGROUP_SUBTREE_CONTROL);
}

public static Path getSubsystemGGPath() {
return getSubsystemRootPath().resolve(GG_NAMESPACE);
}

public static Path getGGSubTreeControlPath() {
return getSubsystemGGPath().resolve(CGROUP_SUBTREE_CONTROL);
}

public static Path getSubsystemComponentPath(String componentName) {
return getSubsystemGGPath().resolve(componentName);
}

public static Path getComponentCpuMaxPath(String componentName) {
return getSubsystemComponentPath(componentName).resolve(CPU_MAX);
}

public static Path getComponentMemoryMaxPath(String componentName) {
return getSubsystemComponentPath(componentName).resolve(MEMORY_MAX);
}

public static Path getCgroupProcsPath(String componentName) {
return getSubsystemComponentPath(componentName).resolve(CGROUP_PROCS);
}

public static Path getCgroupFreezePath(String componentName) {
return getSubsystemComponentPath(componentName).resolve(CGROUP_FREEZE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

package com.aws.greengrass.util.platforms.unix.linux;

public enum CgroupV2FreezerState {
THAWED(0),
FROZEN(1);

private int index;

CgroupV2FreezerState(int index) {
this.index = index;
}

/**
* Get the index value associated with this CgroupV2FreezerState.
*
* @return the integer index value associated with this CgroupV2FreezerState.
*/
public int getIndex() {
return index;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,29 @@
import com.aws.greengrass.util.platforms.SystemResourceController;
import com.aws.greengrass.util.platforms.unix.UnixPlatform;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class LinuxPlatform extends UnixPlatform {
SystemResourceController systemResourceController = new LinuxSystemResourceController(this);
private static final String CGROUP_ROOT = "/sys/fs/cgroup";
private static final String CGROUP_CONTROLLERS = "cgroup.controllers";

SystemResourceController systemResourceController;

@Override
public SystemResourceController getSystemResourceController() {
//if the path exists, identify it as cgroupv1, otherwise identify it as cgroupv2
if (Files.exists(getControllersRootPath())) {
systemResourceController = new LinuxSystemResourceControllerV2(this);
} else {
systemResourceController = new LinuxSystemResourceController(this);
}

return systemResourceController;
}

private Path getControllersRootPath() {
return Paths.get(CGROUP_ROOT).resolve(CGROUP_CONTROLLERS);
}
}
Loading

0 comments on commit 4f9c07d

Please sign in to comment.