11/*
22 * Copyright (c) 2023, Red Hat, Inc.
3- * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+ * Copyright (c) 2024, 2026, Oracle and/or its affiliates. All rights reserved.
44 *
55 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
66 *
4848import jdk .test .lib .containers .docker .DockerRunOptions ;
4949import jdk .test .lib .containers .docker .DockerTestUtils ;
5050import jdk .test .lib .process .OutputAnalyzer ;
51+ import jtreg .SkippedException ;
5152
5253public class TestLimitsUpdating {
5354 private static final long M = 1024 * 1024 ;
55+ private static final int CPU_PERIOD = 100_000 ;
56+ private static final int INITIAL_CPU_COUNT = 1 ;
57+ private static final int UPDATED_CPU_COUNT = 2 ;
5458 private static final String TARGET_CONTAINER = "limitsUpdatingJDK_" + Runtime .getRuntime ().version ().major ();
5559 private static final String imageName = Common .imageName ("limitsUpdatingJDK" );
5660
@@ -67,6 +71,10 @@ public static void main(String[] args) throws Exception {
6771 }
6872
6973 private static void testLimitUpdates () throws Exception {
74+ if (Runtime .getRuntime ().availableProcessors () < UPDATED_CPU_COUNT ) {
75+ throw new SkippedException ("Need at least " + UPDATED_CPU_COUNT
76+ + " available CPUs to test CPU limit updates" );
77+ }
7078 File sharedtmpdir = new File ("jdk-sharedtmp" );
7179 File flag = new File (sharedtmpdir , "limitsUpdated" ); // shared with LimitUpdateChecker
7280 File started = new File (sharedtmpdir , "started" ); // shared with LimitUpdateChecker
@@ -76,8 +84,8 @@ private static void testLimitUpdates() throws Exception {
7684 DockerRunOptions opts = new DockerRunOptions (imageName , "/jdk/bin/java" , "LimitUpdateChecker" );
7785 opts .addDockerOpts ("--volume" , Utils .TEST_CLASSES + ":/test-classes/" );
7886 opts .addDockerOpts ("--volume" , sharedtmpdir .getAbsolutePath () + ":/tmp" );
79- opts .addDockerOpts ("--cpu-period" , "100000" );
80- opts .addDockerOpts ("--cpu-quota" , "200000" );
87+ opts .addDockerOpts ("--cpu-period" , Integer . toString ( CPU_PERIOD ) );
88+ opts .addDockerOpts ("--cpu-quota" , Integer . toString ( INITIAL_CPU_COUNT * CPU_PERIOD ) );
8189 opts .addDockerOpts ("--memory" , "500m" );
8290 opts .addDockerOpts ("--memory-swap" , "500m" );
8391 opts .addDockerOpts ("--name" , TARGET_CONTAINER );
@@ -106,7 +114,7 @@ public void run() {
106114 Thread .sleep (100 );
107115 }
108116
109- final List <String > containerCommand = getContainerUpdate (300_000 , 100_000 , "300m" );
117+ final List <String > containerCommand = getContainerUpdate (UPDATED_CPU_COUNT * CPU_PERIOD , CPU_PERIOD , "300m" );
110118 // Run the update command so as to increase resources once the container signaled it has started.
111119 Thread t2 = new Thread () {
112120 public void run () {
@@ -129,10 +137,10 @@ public void run() {
129137
130138 // Do assertions based on the output in target container
131139 OutputAnalyzer targetOut = out [0 ];
132- targetOut .shouldContain ("Runtime.availableProcessors: 2 " ); // initial value
133- targetOut .shouldContain ("OperatingSystemMXBean.getAvailableProcessors: 2 " ); // initial value
134- targetOut .shouldContain ("Runtime.availableProcessors: 3 " ); // updated value
135- targetOut .shouldContain ("OperatingSystemMXBean.getAvailableProcessors: 3 " ); // updated value
140+ targetOut .shouldContain ("Runtime.availableProcessors: 1 " ); // initial value
141+ targetOut .shouldContain ("OperatingSystemMXBean.getAvailableProcessors: 1 " ); // initial value
142+ targetOut .shouldContain ("Runtime.availableProcessors: 2 " ); // updated value
143+ targetOut .shouldContain ("OperatingSystemMXBean.getAvailableProcessors: 2 " ); // updated value
136144 long memoryInBytes = 500 * M ;
137145 targetOut .shouldContain ("Metrics.getMemoryLimit() == " + memoryInBytes ); // initial value
138146 targetOut .shouldContain ("OperatingSystemMXBean.getTotalMemorySize: " + memoryInBytes ); // initial value
0 commit comments