From b8cd78190f95b485503d820ecdb1384608e7cfbc Mon Sep 17 00:00:00 2001 From: "changxin.dong" Date: Fri, 4 Nov 2022 16:13:36 +0800 Subject: [PATCH] feat: linux control group version 2 API support cgroup v2 --- .../ipc/IPCHibernateTest.java | 5 +- .../GenericExternalServiceIntegTest.java | 87 ++----------- ...temPath.java => CGroupSubSystemPaths.java} | 16 ++- .../{CgroupSubSystem.java => CGroupV1.java} | 23 +--- .../{CgroupSubSystemV2.java => CGroupV2.java} | 22 +--- .../util/platforms/unix/linux/Cgroup.java | 121 ------------------ .../linux/LinuxSystemResourceController.java | 36 +++--- .../LinuxSystemResourceControllerV2Test.java | 6 +- 8 files changed, 52 insertions(+), 264 deletions(-) rename src/main/java/com/aws/greengrass/util/platforms/unix/linux/{CGroupSubSystemPath.java => CGroupSubSystemPaths.java} (88%) rename src/main/java/com/aws/greengrass/util/platforms/unix/linux/{CgroupSubSystem.java => CGroupV1.java} (89%) rename src/main/java/com/aws/greengrass/util/platforms/unix/linux/{CgroupSubSystemV2.java => CGroupV2.java} (88%) delete mode 100644 src/main/java/com/aws/greengrass/util/platforms/unix/linux/Cgroup.java diff --git a/src/integrationtests/java/com/aws/greengrass/integrationtests/ipc/IPCHibernateTest.java b/src/integrationtests/java/com/aws/greengrass/integrationtests/ipc/IPCHibernateTest.java index 597749aabe..c6ea202df4 100644 --- a/src/integrationtests/java/com/aws/greengrass/integrationtests/ipc/IPCHibernateTest.java +++ b/src/integrationtests/java/com/aws/greengrass/integrationtests/ipc/IPCHibernateTest.java @@ -9,8 +9,7 @@ import com.aws.greengrass.lifecyclemanager.Kernel; import com.aws.greengrass.testcommons.testutilities.GGExtension; import com.aws.greengrass.testcommons.testutilities.TestUtils; -import com.aws.greengrass.util.platforms.unix.linux.Cgroup; -import com.aws.greengrass.util.platforms.unix.linux.CgroupSubSystem; +import com.aws.greengrass.util.platforms.unix.linux.CGroupV1; import com.aws.greengrass.util.platforms.unix.linux.LinuxSystemResourceController; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -116,7 +115,7 @@ void GIVEN_LifeCycleEventStreamClient_WHEN_pause_resume_component_THEN_target_se private LinuxSystemResourceController.CgroupFreezerState getCgroupFreezerState(String serviceName) throws IOException { return LinuxSystemResourceController.CgroupFreezerState.valueOf( - new String(Files.readAllBytes(new Cgroup(CgroupSubSystem.Freezer).getCgroupFreezerStateFilePath(serviceName)), + new String(Files.readAllBytes(CGroupV1.Freezer.getCgroupFreezerStateFilePath(serviceName)), StandardCharsets.UTF_8).trim()); } } diff --git a/src/integrationtests/java/com/aws/greengrass/integrationtests/lifecyclemanager/GenericExternalServiceIntegTest.java b/src/integrationtests/java/com/aws/greengrass/integrationtests/lifecyclemanager/GenericExternalServiceIntegTest.java index 7ad911722a..ad0f8bde3c 100644 --- a/src/integrationtests/java/com/aws/greengrass/integrationtests/lifecyclemanager/GenericExternalServiceIntegTest.java +++ b/src/integrationtests/java/com/aws/greengrass/integrationtests/lifecyclemanager/GenericExternalServiceIntegTest.java @@ -21,15 +21,9 @@ import com.aws.greengrass.testcommons.testutilities.GGExtension; import com.aws.greengrass.testcommons.testutilities.NoOpPathOwnershipHandler; import com.aws.greengrass.util.Pair; -import com.aws.greengrass.util.platforms.SystemResourceController; -import com.aws.greengrass.util.platforms.unix.linux.Cgroup; -import com.aws.greengrass.util.platforms.unix.linux.CgroupSubSystem; -import com.aws.greengrass.util.platforms.unix.linux.CgroupSubSystemV2; -import com.aws.greengrass.util.platforms.unix.linux.LinuxPlatform; +import com.aws.greengrass.util.platforms.unix.linux.CGroupV1; import com.aws.greengrass.util.platforms.unix.linux.LinuxSystemResourceController; -import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.SystemUtils; -import org.apache.commons.lang3.reflect.FieldUtils; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -40,11 +34,9 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; -import org.mockito.Spy; import org.mockito.junit.jupiter.MockitoExtension; import java.io.IOException; -import java.lang.reflect.Field; import java.nio.charset.StandardCharsets; import java.nio.file.FileSystemException; import java.nio.file.Files; @@ -95,14 +87,9 @@ class GenericExternalServiceIntegTest extends BaseITCase { private Kernel kernel; - @Spy - LinuxPlatform linuxPlatform; - - private final static String ROOT_PATH_STRING = "/systest21/fs/cgroup"; + private final static String ROOT_PATH_STRING = "/sys/fs/cgroup"; private final static String GG_PATH_STRING = "greengrass"; - SystemResourceController systemResourceController; - static Stream posixTestUserConfig() { return Stream.of( arguments("config_run_with_user.yaml", "nobody", "nobody"), @@ -551,7 +538,7 @@ void GIVEN_posix_default_user_WHEN_runs_THEN_runs_with_default_user(String file, String messageOnStdout = m.getMessage(); if (STDOUT.equals(m.getEventType()) && messageOnStdout != null && (messageOnStdout.contains("run as") - || messageOnStdout.contains("install as") )) { + || messageOnStdout.contains("install as") )) { stdouts.add(messageOnStdout); countDownLatch.countDown(); } @@ -642,10 +629,9 @@ void GIVEN_linux_resource_limits_WHEN_it_changes_THEN_component_runs_with_new_re @EnabledOnOs({OS.LINUX}) @Test void GIVEN_linux_resource_limits_WHEN_it_changes_THEN_component_runs_with_new_resource_limits_V2() throws Exception { + assumeTrue(ifCgroupV2(), "skip this test case if v1 is enabled."); + String echoComponentName = "echo_service"; - String mainComponentName = "main"; - String rootGGPathString = ROOT_PATH_STRING + "/" + GG_PATH_STRING; - String componentPathString = rootGGPathString + "/" + echoComponentName; // Run with no resource limit ConfigPlatformResolver.initKernelWithMultiPlatformConfig(kernel, getClass().getResource("config_run_with_user.yaml")); @@ -656,37 +642,6 @@ void GIVEN_linux_resource_limits_WHEN_it_changes_THEN_component_runs_with_new_re } }); - linuxPlatform = spy(kernel.getContext().get(LinuxPlatform.class)); - - createComponentData(echoComponentName); - createComponentData(mainComponentName); - - // Due to cgroup v1 is active by default (in test platform), and the directories of cgroup v1 are read-only - // therefore, here create some directories and files as fake cgroup v2 files to support testing - Field controllerField = LinuxPlatform.class.getDeclaredField("CGROUP_CONTROLLERS"); - setFinalStatic(controllerField, Paths.get(componentPathString + "/memory.max")); - systemResourceController = linuxPlatform.getSystemResourceController(); - LinuxSystemResourceController controllerV2 = (LinuxSystemResourceController) systemResourceController; - Field memoryCgroupField = LinuxSystemResourceController.class.getSuperclass().getDeclaredField("memoryCgroup"); - memoryCgroupField.setAccessible(true); - Cgroup memoryCgroup = (Cgroup) memoryCgroupField.get(controllerV2); - Field subsystem = memoryCgroup.getClass().getDeclaredField("subSystem"); - subsystem.setAccessible(true); - CgroupSubSystemV2 cg = (CgroupSubSystemV2) subsystem.get(memoryCgroup); - Field f = cg.getClass().getInterfaces()[0].getDeclaredField("CGROUP_ROOT"); - setFinalStatic(f, Paths.get(ROOT_PATH_STRING)); - - Field mountsField = LinuxSystemResourceController.class.getSuperclass().getDeclaredField("MOUNT_PATH"); - mountsField.setAccessible(true); - String mountPathFile = rootGGPathString + "/mountPath.txt"; - final Path mountPathFilePath = Paths.get(mountPathFile); - if (!Files.exists(mountPathFilePath)) { - Files.createFile(mountPathFilePath); - Files.write(mountPathFilePath, String.format("test1 %s test2 test3 test4 test5", ROOT_PATH_STRING).getBytes(StandardCharsets.UTF_8)); - - } - setFinalStatic(mountsField, mountPathFile); - kernel.launch(); assertResourceLimits_V2(10240l * 1024, 1.5); @@ -707,14 +662,10 @@ void GIVEN_linux_resource_limits_WHEN_it_changes_THEN_component_runs_with_new_re kernel.getContext().waitForPublishQueueToClear(); assertResourceLimits_V2(10240l * 1024, 1.5); - - FileUtils.deleteDirectory(Paths.get("/systest21").toFile()); } - private void setFinalStatic(Field field, Object newValue) throws Exception { - field.setAccessible(true); - FieldUtils.removeFinalModifier(field, true); - field.set(null, newValue); + private boolean ifCgroupV2() { + return Files.exists(Paths.get("/sys/fs/cgroup/cgroup.controllers")); } @Test @@ -848,11 +799,11 @@ void GIVEN_service_starts_up_WHEN_startup_times_out_THEN_timeout_error_code_pers } private void assertResourceLimits(String componentName, long memory, double cpus) throws Exception { - byte[] buf1 = Files.readAllBytes(new Cgroup(CgroupSubSystem.Memory).getComponentMemoryLimitPath(componentName)); + byte[] buf1 = Files.readAllBytes(CGroupV1.Memory.getComponentMemoryLimitPath(componentName)); assertThat(memory, equalTo(Long.parseLong(new String(buf1, StandardCharsets.UTF_8).trim()))); - byte[] buf2 = Files.readAllBytes(new Cgroup(CgroupSubSystem.CPU).getComponentCpuQuotaPath(componentName)); - byte[] buf3 = Files.readAllBytes(new Cgroup(CgroupSubSystem.CPU).getComponentCpuPeriodPath(componentName)); + byte[] buf2 = Files.readAllBytes(CGroupV1.CPU.getComponentCpuQuotaPath(componentName)); + byte[] buf3 = Files.readAllBytes(CGroupV1.CPU.getComponentCpuPeriodPath(componentName)); int quota = Integer.parseInt(new String(buf2, StandardCharsets.UTF_8).trim()); int period = Integer.parseInt(new String(buf3, StandardCharsets.UTF_8).trim()); @@ -860,22 +811,6 @@ private void assertResourceLimits(String componentName, long memory, double cpus assertThat(expectedQuota, equalTo(quota)); } - private void createComponentData(String componentName) throws IOException { - Path path = Paths.get(ROOT_PATH_STRING).resolve(GG_PATH_STRING).resolve(componentName); - Files.createDirectories(path); - - if (!Files.exists(path.resolve("memory.max"))) { - Files.createFile(path.resolve("memory.max")); - } - if (!Files.exists(path.resolve("cpu.max"))) { - Files.createFile(path.resolve("cpu.max")); - } - Files.write(path.resolve("cpu.max"), "max 100000".getBytes(StandardCharsets.UTF_8)); - if (!Files.exists(path.resolve("cgroup.procs"))) { - Files.createFile(path.resolve("cgroup.procs")); - } - } - private void assertResourceLimits_V2(long memory, double cpus) throws Exception { byte[] buf1 = Files.readAllBytes(Paths.get(String.format("%s/%s/echo_service/memory.max", ROOT_PATH_STRING, GG_PATH_STRING))); assertThat(memory, equalTo(Long.parseLong(new String(buf1, StandardCharsets.UTF_8).trim()))); @@ -928,7 +863,7 @@ void GIVEN_running_service_WHEN_pause_resume_requested_THEN_pause_resume_Service private LinuxSystemResourceController.CgroupFreezerState getCgroupFreezerState(String serviceName) throws IOException { return LinuxSystemResourceController.CgroupFreezerState - .valueOf(new String(Files.readAllBytes(new Cgroup(CgroupSubSystem.Freezer).getCgroupFreezerStateFilePath(serviceName)) + .valueOf(new String(Files.readAllBytes(CGroupV1.Freezer.getCgroupFreezerStateFilePath(serviceName)) , StandardCharsets.UTF_8).trim()); } } diff --git a/src/main/java/com/aws/greengrass/util/platforms/unix/linux/CGroupSubSystemPath.java b/src/main/java/com/aws/greengrass/util/platforms/unix/linux/CGroupSubSystemPaths.java similarity index 88% rename from src/main/java/com/aws/greengrass/util/platforms/unix/linux/CGroupSubSystemPath.java rename to src/main/java/com/aws/greengrass/util/platforms/unix/linux/CGroupSubSystemPaths.java index e99c3130a0..61ffd1dd21 100644 --- a/src/main/java/com/aws/greengrass/util/platforms/unix/linux/CGroupSubSystemPath.java +++ b/src/main/java/com/aws/greengrass/util/platforms/unix/linux/CGroupSubSystemPaths.java @@ -18,7 +18,7 @@ @SuppressFBWarnings(value = "DMI_HARDCODED_ABSOLUTE_FILENAME", justification = "CGroupSubSystemPath virtual filesystem path cannot be relative") -public interface CGroupSubSystemPath { +public interface CGroupSubSystemPaths { Path CGROUP_ROOT = Paths.get("/sys/fs/cgroup"); String GG_NAMESPACE = "greengrass"; String CGROUP_MEMORY_LIMITS = "memory.limit_in_bytes"; @@ -45,9 +45,13 @@ default String subsystemMountCmd() { Path getSubsystemRootPath(); - Path getSubsystemGGPath(); + default Path getSubsystemGGPath() { + return getSubsystemRootPath().resolve(GG_NAMESPACE); + } - Path getSubsystemComponentPath(String componentName); + default Path getSubsystemComponentPath(String componentName) { + return getSubsystemGGPath().resolve(componentName); + } Path getComponentMemoryLimitPath(String componentName); @@ -59,7 +63,9 @@ default Path getComponentCpuQuotaPath(String componentName) { return null; } - Path getCgroupProcsPath(String componentName); + default Path getCgroupProcsPath(String componentName) { + return getSubsystemComponentPath(componentName).resolve(CGROUP_PROCS); + } Path getCgroupFreezerStateFilePath(String componentName); @@ -83,7 +89,7 @@ default Path getCgroupFreezePath(String componentName) { void handleCpuLimits(GreengrassService component, double cpu) throws IOException; - void pauseComponentProcessesCore(GreengrassService component, List processes) throws IOException; + void pauseComponentProcessesCore(GreengrassService component) throws IOException; void resumeComponentProcesses(GreengrassService component) throws IOException; diff --git a/src/main/java/com/aws/greengrass/util/platforms/unix/linux/CgroupSubSystem.java b/src/main/java/com/aws/greengrass/util/platforms/unix/linux/CGroupV1.java similarity index 89% rename from src/main/java/com/aws/greengrass/util/platforms/unix/linux/CgroupSubSystem.java rename to src/main/java/com/aws/greengrass/util/platforms/unix/linux/CGroupV1.java index 3dbaac81f9..73d2a3cf78 100644 --- a/src/main/java/com/aws/greengrass/util/platforms/unix/linux/CgroupSubSystem.java +++ b/src/main/java/com/aws/greengrass/util/platforms/unix/linux/CGroupV1.java @@ -18,14 +18,14 @@ import java.util.Set; @SuppressFBWarnings(value = "DMI_HARDCODED_ABSOLUTE_FILENAME", - justification = "CgroupSubSystem virtual filesystem path cannot be relative") -public enum CgroupSubSystem implements CGroupSubSystemPath { + justification = "CGroupV1 virtual filesystem path cannot be relative") +public enum CGroupV1 implements CGroupSubSystemPaths { Memory("memory", ""), CPU("cpu,cpuacct", ""), Freezer("freezer", "freezer"); private String osString; private String mountSrc; - CgroupSubSystem(String osString, String mountSrc) { + CGroupV1(String osString, String mountSrc) { this.osString = osString; this.mountSrc = mountSrc; } @@ -63,16 +63,6 @@ public Path getSubsystemRootPath() { return CGROUP_ROOT.resolve(osString); } - @Override - public Path getSubsystemGGPath() { - return getSubsystemRootPath().resolve(GG_NAMESPACE); - } - - @Override - public Path getSubsystemComponentPath(String componentName) { - return getSubsystemGGPath().resolve(componentName); - } - @Override public Path getComponentMemoryLimitPath(String componentName) { return getSubsystemComponentPath(componentName).resolve(CGROUP_MEMORY_LIMITS); @@ -88,11 +78,6 @@ public Path getComponentCpuQuotaPath(String componentName) { return getSubsystemComponentPath(componentName).resolve(CPU_CFS_QUOTA_US); } - @Override - public Path getCgroupProcsPath(String componentName) { - return getSubsystemComponentPath(componentName).resolve(CGROUP_PROCS); - } - @Override public Path getCgroupFreezerStateFilePath(String componentName) { return getSubsystemComponentPath(componentName).resolve(FREEZER_STATE_FILE); @@ -134,7 +119,7 @@ public void handleCpuLimits(GreengrassService component, double cpu) throws IOEx } @Override - public void pauseComponentProcessesCore(GreengrassService component, List processes) + public void pauseComponentProcessesCore(GreengrassService component) throws IOException { if (LinuxSystemResourceController.CgroupFreezerState.FROZEN.equals( currentFreezerCgroupState(component.getServiceName()))) { diff --git a/src/main/java/com/aws/greengrass/util/platforms/unix/linux/CgroupSubSystemV2.java b/src/main/java/com/aws/greengrass/util/platforms/unix/linux/CGroupV2.java similarity index 88% rename from src/main/java/com/aws/greengrass/util/platforms/unix/linux/CgroupSubSystemV2.java rename to src/main/java/com/aws/greengrass/util/platforms/unix/linux/CGroupV2.java index 3aef0931cb..d1efd2872b 100644 --- a/src/main/java/com/aws/greengrass/util/platforms/unix/linux/CgroupSubSystemV2.java +++ b/src/main/java/com/aws/greengrass/util/platforms/unix/linux/CGroupV2.java @@ -14,12 +14,11 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardOpenOption; -import java.util.List; import java.util.Set; @SuppressFBWarnings(value = "DMI_HARDCODED_ABSOLUTE_FILENAME", - justification = "CgroupSubSystemV2 virtual filesystem path cannot be relative") -public enum CgroupSubSystemV2 implements CGroupSubSystemPath { + justification = "CGroupV2 virtual filesystem path cannot be relative") +public enum CGroupV2 implements CGroupSubSystemPaths { Memory, CPU, Freezer, Unified; private static final String CGROUP_SUBTREE_CONTROL_CONTENT = "+cpuset +cpu +io +memory +pids"; @@ -33,26 +32,11 @@ public Path getSubsystemRootPath() { return CGROUP_ROOT; } - @Override - public Path getSubsystemGGPath() { - return getSubsystemRootPath().resolve(GG_NAMESPACE); - } - - @Override - public Path getSubsystemComponentPath(String componentName) { - return getSubsystemGGPath().resolve(componentName); - } - @Override public Path getComponentMemoryLimitPath(String componentName) { return getSubsystemComponentPath(componentName).resolve(MEMORY_MAX); } - @Override - public Path getCgroupProcsPath(String componentName) { - return getSubsystemComponentPath(componentName).resolve(CGROUP_PROCS); - } - @Override public Path getCgroupFreezerStateFilePath(String componentName) { return getSubsystemComponentPath(componentName).resolve(CGROUP_FREEZE); @@ -129,7 +113,7 @@ public void handleCpuLimits(GreengrassService component, double cpu) throws IOEx } @Override - public void pauseComponentProcessesCore(GreengrassService component, List processes) + public void pauseComponentProcessesCore(GreengrassService component) throws IOException { Files.write(getCgroupFreezerStateFilePath(component.getServiceName()), String.valueOf(CgroupV2FreezerState.FROZEN.getIndex()).getBytes(StandardCharsets.UTF_8), diff --git a/src/main/java/com/aws/greengrass/util/platforms/unix/linux/Cgroup.java b/src/main/java/com/aws/greengrass/util/platforms/unix/linux/Cgroup.java deleted file mode 100644 index d28dece13b..0000000000 --- a/src/main/java/com/aws/greengrass/util/platforms/unix/linux/Cgroup.java +++ /dev/null @@ -1,121 +0,0 @@ -/* - * 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 com.aws.greengrass.lifecyclemanager.GreengrassService; - -import java.io.IOException; -import java.nio.file.Path; -import java.util.List; - -/** - * Represents Linux cgroup subsystems. - */ -public class Cgroup { - private final CGroupSubSystemPath subSystem; - - public Cgroup(CGroupSubSystemPath subSystem) { - this.subSystem = subSystem; - } - - public Path getRootPath() { - return subSystem.getRootPath(); - } - - /** - * root mount cmd. - * - * @return mount command string - */ - public String rootMountCmd() { - return subSystem.rootMountCmd(); - } - - public String subsystemMountCmd() { - return subSystem.subsystemMountCmd(); - } - - public Path getSubsystemRootPath() { - return subSystem.getSubsystemRootPath(); - } - - public Path getSubsystemGGPath() { - return subSystem.getSubsystemGGPath(); - } - - public Path getSubsystemComponentPath(String componentName) { - return subSystem.getSubsystemComponentPath(componentName); - } - - /** - * get component memory limit path. - * - * @param componentName componentName - * @return memory limit Path - */ - public Path getComponentMemoryLimitPath(String componentName) { - return subSystem.getComponentMemoryLimitPath(componentName); - } - - public Path getComponentCpuPeriodPath(String componentName) { - return subSystem.getComponentCpuPeriodPath(componentName); - } - - public Path getComponentCpuQuotaPath(String componentName) { - return subSystem.getComponentCpuQuotaPath(componentName); - } - - public Path getCgroupProcsPath(String componentName) { - return subSystem.getCgroupProcsPath(componentName); - } - - /** - * get cgroup freezer path. - * - * @param componentName componentName - * @return cgroup freezer path - */ - public Path getCgroupFreezerStateFilePath(String componentName) { - return subSystem.getCgroupFreezerStateFilePath(componentName); - } - - public Path getRootSubTreeControlPath() { - return subSystem.getRootSubTreeControlPath(); - } - - public Path getGGSubTreeControlPath() { - return subSystem.getGGSubTreeControlPath(); - } - - public Path getComponentCpuMaxPath(String componentName) { - return subSystem.getComponentCpuMaxPath(componentName); - } - - public Path getCgroupFreezePath(String componentName) { - return subSystem.getCgroupFreezePath(componentName); - } - - public void initializeCgroup(GreengrassService component, LinuxPlatform platform) throws IOException { - subSystem.initializeCgroup(component, platform); - } - - public void handleCpuLimits(GreengrassService component, double cpu) throws IOException { - subSystem.handleCpuLimits(component, cpu); - } - - public void pauseComponentProcessesCore(GreengrassService component, List processes) - throws IOException { - subSystem.pauseComponentProcessesCore(component, processes); - } - - public void resumeComponentProcesses(GreengrassService component) throws IOException { - subSystem.resumeComponentProcesses(component); - } - - protected Path freezerCgroupStateFile(String component) { - return getCgroupFreezerStateFilePath(component); - } -} diff --git a/src/main/java/com/aws/greengrass/util/platforms/unix/linux/LinuxSystemResourceController.java b/src/main/java/com/aws/greengrass/util/platforms/unix/linux/LinuxSystemResourceController.java index bf49b53367..3252a58827 100644 --- a/src/main/java/com/aws/greengrass/util/platforms/unix/linux/LinuxSystemResourceController.java +++ b/src/main/java/com/aws/greengrass/util/platforms/unix/linux/LinuxSystemResourceController.java @@ -36,12 +36,12 @@ public class LinuxSystemResourceController implements SystemResourceController { private static final String COMPONENT_NAME = "componentName"; private static final String MEMORY_KEY = "memory"; private static final String CPUS_KEY = "cpus"; - private Cgroup memoryCgroup; - private Cgroup cpuCgroup; - private Cgroup freezerCgroup; - private Cgroup unifiedCgroup; - private List resourceLimitCgroups; - protected CopyOnWriteArrayList usedCgroups = new CopyOnWriteArrayList<>(); + private CGroupSubSystemPaths memoryCgroup; + private CGroupSubSystemPaths cpuCgroup; + private CGroupSubSystemPaths freezerCgroup; + private CGroupSubSystemPaths unifiedCgroup; + private List resourceLimitCgroups; + protected CopyOnWriteArrayList usedCgroups = new CopyOnWriteArrayList<>(); protected LinuxPlatform platform; @@ -55,16 +55,16 @@ public LinuxSystemResourceController(LinuxPlatform platform, boolean isV1Used) { this.platform = platform; if (isV1Used) { - this.memoryCgroup = new Cgroup(CgroupSubSystem.Memory); - this.cpuCgroup = new Cgroup(CgroupSubSystem.CPU); - this.freezerCgroup = new Cgroup(CgroupSubSystem.Freezer); + this.memoryCgroup = CGroupV1.Memory; + this.cpuCgroup = CGroupV1.CPU; + this.freezerCgroup = CGroupV1.Freezer; resourceLimitCgroups = Arrays.asList( memoryCgroup, cpuCgroup); } else { - this.unifiedCgroup = new Cgroup(CgroupSubSystemV2.Unified); - this.memoryCgroup = new Cgroup(CgroupSubSystemV2.Memory); - this.cpuCgroup = new Cgroup(CgroupSubSystemV2.CPU); - this.freezerCgroup = new Cgroup(CgroupSubSystemV2.Freezer); + this.unifiedCgroup = CGroupV2.Unified; + this.memoryCgroup = CGroupV2.Memory; + this.cpuCgroup = CGroupV2.CPU; + this.freezerCgroup = CGroupV2.Freezer; resourceLimitCgroups = Arrays.asList(unifiedCgroup); } } @@ -129,7 +129,7 @@ protected void updateMemoryResourceLimits(GreengrassService component, @Override public void resetResourceLimits(GreengrassService component) { - for (Cgroup cg : resourceLimitCgroups) { + for (CGroupSubSystemPaths cg : resourceLimitCgroups) { try { if (Files.exists(cg.getSubsystemComponentPath(component.getServiceName()))) { Files.delete(cg.getSubsystemComponentPath(component.getServiceName())); @@ -169,7 +169,7 @@ public void addComponentProcess(GreengrassService component, Process process) { @Override public void pauseComponentProcesses(GreengrassService component, List processes) throws IOException { prePauseComponentProcesses(component, processes); - freezerCgroup.pauseComponentProcessesCore(component, processes); + freezerCgroup.pauseComponentProcessesCore(component); } @@ -178,7 +178,7 @@ public void resumeComponentProcesses(GreengrassService component) throws IOExcep freezerCgroup.resumeComponentProcesses(component); } - protected void addComponentProcessToCgroup(String component, Process process, Cgroup cg) + protected void addComponentProcessToCgroup(String component, Process process, CGroupSubSystemPaths cg) throws IOException { if (!Files.exists(cg.getSubsystemComponentPath(component))) { @@ -227,12 +227,12 @@ private void handleErrorAddingPidToCgroup(IOException e, String component) { } } - protected void initializeCgroup(GreengrassService component, Cgroup cgroup) throws IOException { + protected void initializeCgroup(GreengrassService component, CGroupSubSystemPaths cgroup) throws IOException { cgroup.initializeCgroup(component, platform); usedCgroups.add(cgroup); } - private Set pidsInComponentCgroup(Cgroup cgroup, String component) throws IOException { + private Set pidsInComponentCgroup(CGroupSubSystemPaths cgroup, String component) throws IOException { return Files.readAllLines(cgroup.getCgroupProcsPath(component)) .stream().map(Integer::parseInt).collect(Collectors.toSet()); } diff --git a/src/test/java/com/aws/greengrass/util/platforms/unix/linux/LinuxSystemResourceControllerV2Test.java b/src/test/java/com/aws/greengrass/util/platforms/unix/linux/LinuxSystemResourceControllerV2Test.java index a9e02e016b..58dff9ed12 100644 --- a/src/test/java/com/aws/greengrass/util/platforms/unix/linux/LinuxSystemResourceControllerV2Test.java +++ b/src/test/java/com/aws/greengrass/util/platforms/unix/linux/LinuxSystemResourceControllerV2Test.java @@ -43,9 +43,9 @@ class LinuxSystemResourceControllerV2Test { @Mock LinuxPlatform platform; @Spy - Cgroup cpuCgroup = new Cgroup(CgroupSubSystemV2.CPU); + CGroupSubSystemPaths cpuCgroup = CGroupV2.CPU; @Spy - Cgroup memoryCgroup = new Cgroup(CgroupSubSystemV2.Memory); + CGroupSubSystemPaths memoryCgroup = CGroupV2.Memory; @InjectMocks @Spy LinuxSystemResourceController linuxSystemResourceControllerV2 = new LinuxSystemResourceController( @@ -101,7 +101,7 @@ void GIVEN_cgroupv2_WHEN_cpu_limit_updated_THEN_cpu_limit_file_updated() throws Files.write(path, "max 100000".getBytes(StandardCharsets.UTF_8)); - CGroupSubSystemPath cpuSystemV2 = spy(CgroupSubSystemV2.CPU); + CGroupSubSystemPaths cpuSystemV2 = spy(CGroupV2.CPU); lenient().when(cpuSystemV2.getComponentCpuMaxPath(COMPONENT_NAME)).thenReturn(path); cpuSystemV2.handleCpuLimits(component, 0.5);