Path: blob/master/test/hotspot/jtreg/containers/docker/TestPids.java
64493 views
/*1* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.2* Copyright (c) 2021 SAP SE. All rights reserved.3* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.4*5* This code is free software; you can redistribute it and/or modify it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/232425/*26* @test27* @key cgroups28* @summary Test JVM's awareness of pids controller29* @requires docker.support30* @library /test/lib31* @modules java.base/jdk.internal.misc32* java.management33* @build sun.hotspot.WhiteBox PrintContainerInfo34* @run driver jdk.test.lib.helpers.ClassFileInstaller -jar whitebox.jar sun.hotspot.WhiteBox35* @run driver TestPids36*/37import java.util.List;38import jdk.test.lib.containers.docker.Common;39import jdk.test.lib.containers.docker.DockerRunOptions;40import jdk.test.lib.containers.docker.DockerTestUtils;41import jdk.test.lib.Asserts;42import jdk.test.lib.Container;43import jdk.test.lib.Platform;44import jdk.test.lib.Utils;4546public class TestPids {47private static final String imageName = Common.imageName("pids");48private static final boolean IS_PODMAN = Container.ENGINE_COMMAND.contains("podman");49private static final int UNLIMITED_PIDS_PODMAN = 0;50private static final int UNLIMITED_PIDS_DOCKER = -1;5152static final String warning_kernel_no_pids_support = "WARNING: Your kernel does not support pids limit capabilities";5354public static void main(String[] args) throws Exception {55if (!DockerTestUtils.canTestDocker()) {56return;57}5859Common.prepareWhiteBox();60DockerTestUtils.buildJdkContainerImage(imageName);6162try {63testPids();64} finally {65if (!DockerTestUtils.RETAIN_IMAGE_AFTER_TEST) {66DockerTestUtils.removeDockerImage(imageName);67}68}69}7071private static void testPids() throws Exception {72System.out.println("Testing pids controller ...");73testPids("400");74testPids("800");75testPids("2000");76testPids("Unlimited");77}7879private static DockerRunOptions commonOpts() {80DockerRunOptions opts = new DockerRunOptions(imageName, "/jdk/bin/java", "PrintContainerInfo");81opts.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/");82opts.addJavaOpts("-Xlog:os+container=trace", "-cp", "/test-classes/");83Common.addWhiteBoxOpts(opts);84return opts;85}8687private static void checkResult(List<String> lines, String lineMarker, String expectedValue) {88boolean lineMarkerFound = false;8990for (String line : lines) {91if (line.contains(warning_kernel_no_pids_support)) {92System.out.println("Docker pids limitation seems not to work, avoiding check");93return;94}9596if (line.contains(lineMarker)) {97lineMarkerFound = true;98String[] parts = line.split(":");99System.out.println("DEBUG: line = " + line);100System.out.println("DEBUG: parts.length = " + parts.length);101if (expectedValue.equals("any_integer")) {102Asserts.assertEquals(parts.length, 2);103String ivalue = parts[1].replaceAll("\\s","");104try {105int ai = Integer.parseInt(ivalue);106System.out.println("Found " + lineMarker + " with value: " + ai + ". PASS.");107} catch (NumberFormatException ex) {108throw new RuntimeException("Could not convert " + ivalue + " to an integer, log line was " + line);109}110break;111}112113Asserts.assertEquals(parts.length, 2);114String actual = parts[1].replaceAll("\\s","");115// Unlimited pids leads on some setups not to "max" in the output, but to a high number116if (expectedValue.equals("max")) {117if (actual.equals("max")) {118System.out.println("Found expected max for unlimited pids value.");119} else {120try {121int ai = Integer.parseInt(actual);122if (ai > 20000) {123System.out.println("Limit value " + ai + " got accepted as unlimited, log line was " + line);124} else {125throw new RuntimeException("Limit value " + ai + " is not accepted as unlimited, log line was " + line);126}127} catch (NumberFormatException ex) {128throw new RuntimeException("Could not convert " + actual + " to an integer, log line was " + line);129}130}131} else {132Asserts.assertEquals(actual, expectedValue);133}134break;135}136}137Asserts.assertTrue(lineMarkerFound);138}139140private static void testPids(String value) throws Exception {141Common.logNewTestCase("pids controller test, limiting value = " + value);142143DockerRunOptions opts = commonOpts();144if (value.equals("Unlimited")) {145int unlimited = IS_PODMAN ? UNLIMITED_PIDS_PODMAN : UNLIMITED_PIDS_DOCKER;146opts.addDockerOpts("--pids-limit=" + unlimited);147} else {148opts.addDockerOpts("--pids-limit="+value);149}150151List<String> lines = Common.run(opts).asLines();152if (value.equals("Unlimited")) {153checkResult(lines, "Maximum number of tasks is: ", "max");154} else {155checkResult(lines, "Maximum number of tasks is: ", value);156}157// current number of tasks value is hard to predict, so better expect no value158checkResult(lines, "Current number of tasks is: ", "any_integer");159}160161}162163164