Skip to content

Commit 53aceba

Browse files
committed
8307766: Linux: Provide the option to override the timer slack
Reviewed-by: stuefe Backport-of: 7173c3009e0999f13eaa4bee5eedee7326f9d124
1 parent 6f76b65 commit 53aceba

File tree

3 files changed

+176
-1
lines changed

3 files changed

+176
-1
lines changed

src/hotspot/os/linux/globals_linux.hpp

+13-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,19 @@
7777
"Use CPU_ALLOC code path in os::active_processor_count ") \
7878
\
7979
product(bool, DumpPerfMapAtExit, false, DIAGNOSTIC, \
80-
"Write map file for Linux perf tool at exit")
80+
"Write map file for Linux perf tool at exit") \
81+
\
82+
product(intx, TimerSlack, -1, EXPERIMENTAL, \
83+
"Overrides the timer slack value to the given number of " \
84+
"nanoseconds. Lower value provides more accurate " \
85+
"high-precision timers, at the expense of (possibly) worse " \
86+
"power efficiency. In current Linux, 0 means using the " \
87+
"system-wide default, which would disable the override, but " \
88+
"VM would still print the current timer slack values. Use -1 "\
89+
"to disable both the override and the printouts." \
90+
"See prctl(PR_SET_TIMERSLACK) for more info.") \
91+
\
92+
8193

8294
// end of RUNTIME_OS_FLAGS
8395

src/hotspot/os/linux/os_linux.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
# include <inttypes.h>
114114
# include <sys/ioctl.h>
115115
# include <linux/elf-em.h>
116+
# include <sys/prctl.h>
116117
#ifdef __GLIBC__
117118
# include <malloc.h>
118119
#endif
@@ -967,6 +968,16 @@ bool os::create_thread(Thread* thread, ThreadType thr_type,
967968
if (ret == 0) {
968969
log_info(os, thread)("Thread \"%s\" started (pthread id: " UINTX_FORMAT ", attributes: %s). ",
969970
thread->name(), (uintx) tid, os::Posix::describe_pthread_attr(buf, sizeof(buf), &attr));
971+
972+
// Print current timer slack if override is enabled and timer slack value is available.
973+
// Avoid calling prctl otherwise for extra safety.
974+
if (TimerSlack >= 0) {
975+
int slack = prctl(PR_GET_TIMERSLACK);
976+
if (slack >= 0) {
977+
log_info(os, thread)("Thread \"%s\" (pthread id: " UINTX_FORMAT ") timer slack: %dns",
978+
thread->name(), (uintx) tid, slack);
979+
}
980+
}
970981
} else {
971982
log_warning(os, thread)("Failed to start thread \"%s\" - pthread_create failed (%s) for attributes: %s.",
972983
thread->name(), os::errno_name(ret), os::Posix::describe_pthread_attr(buf, sizeof(buf), &attr));
@@ -4701,6 +4712,15 @@ jint os::init_2(void) {
47014712
FLAG_SET_DEFAULT(UseCodeCacheFlushing, false);
47024713
}
47034714

4715+
// Override the timer slack value if needed. The adjustment for the main
4716+
// thread will establish the setting for child threads, which would be
4717+
// most threads in JDK/JVM.
4718+
if (TimerSlack >= 0) {
4719+
if (prctl(PR_SET_TIMERSLACK, TimerSlack) < 0) {
4720+
vm_exit_during_initialization("Setting timer slack failed: %s", os::strerror(errno));
4721+
}
4722+
}
4723+
47044724
return JNI_OK;
47054725
}
47064726

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
* Copyright Amazon.com Inc. or its affiliates. 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 it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 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 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import java.util.regex.Pattern;
25+
import java.util.regex.Matcher;
26+
27+
import jdk.test.lib.process.ProcessTools;
28+
import jdk.test.lib.process.OutputAnalyzer;
29+
30+
/**
31+
* @test
32+
* @summary Check that timer slack options work
33+
* @requires os.family == "linux"
34+
* @requires vm.flagless
35+
* @library /test/lib
36+
* @run driver TestTimerSlack
37+
*/
38+
public class TestTimerSlack {
39+
40+
public static void main(String[] args) throws Exception {
41+
int defaultSlack;
42+
43+
// Check the timer slack value is not printed by default
44+
{
45+
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-Xlog:os+thread",
46+
"TestTimerSlack$TestMain");
47+
48+
OutputAnalyzer output = new OutputAnalyzer(pb.start());
49+
output.shouldHaveExitValue(0);
50+
output.shouldNotContain("timer slack:");
51+
}
52+
53+
// Check the timer slack value is not printed when explicitly disabled
54+
{
55+
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-Xlog:os+thread",
56+
"-XX:+UnlockExperimentalVMOptions",
57+
"-XX:TimerSlack=-1",
58+
"TestTimerSlack$TestMain");
59+
60+
OutputAnalyzer output = new OutputAnalyzer(pb.start());
61+
output.shouldHaveExitValue(0);
62+
output.shouldNotContain("timer slack:");
63+
}
64+
65+
// Check the timer slack value is good when system-wide default is requested
66+
{
67+
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-Xlog:os+thread",
68+
"-XX:+UnlockExperimentalVMOptions",
69+
"-XX:TimerSlack=0",
70+
"TestTimerSlack$TestMain");
71+
72+
OutputAnalyzer output = new OutputAnalyzer(pb.start());
73+
output.shouldHaveExitValue(0);
74+
output.shouldContain("timer slack:");
75+
76+
defaultSlack = parseSlackValue(output);
77+
78+
if (defaultSlack == 0) {
79+
fail(output, "Default slack value (" + defaultSlack + ") is unexpected");
80+
}
81+
}
82+
83+
// Check the timer slack value is accepted by all threads
84+
for (int slack : new int[] {1, 10, 100, 1000, 10000, 100000, 1000000}) {
85+
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-Xlog:os+thread",
86+
"-XX:+UnlockExperimentalVMOptions",
87+
"-XX:TimerSlack=" + slack,
88+
"TestTimerSlack$TestMain");
89+
90+
OutputAnalyzer output = new OutputAnalyzer(pb.start());
91+
output.shouldHaveExitValue(0);
92+
output.shouldContain("timer slack:");
93+
94+
int actualSlack = parseSlackValue(output);
95+
if (actualSlack != slack) {
96+
fail(output, "Actual slack value (" + actualSlack + ") is not the requested one (" + slack + ")");
97+
}
98+
}
99+
}
100+
101+
static final Pattern EXTRACT_PATTERN = Pattern.compile("(.*)timer slack: ([0-9]+)ns(.*)");
102+
103+
public static int parseSlackValue(OutputAnalyzer output) {
104+
Integer value = null;
105+
for (String s : output.asLines()) {
106+
Matcher m = EXTRACT_PATTERN.matcher(s);
107+
if (m.matches()) {
108+
Integer parsedValue = Integer.parseInt(m.group(2));
109+
if (value == null) {
110+
value = parsedValue;
111+
} else if (!value.equals(parsedValue)) {
112+
fail(output, "Multiple timer slack values detected");
113+
}
114+
}
115+
}
116+
if (value == null) {
117+
fail(output, "No timer slack values detected");
118+
}
119+
return value;
120+
}
121+
122+
private static void fail(OutputAnalyzer output, String msg) {
123+
output.reportDiagnosticSummary();
124+
throw new IllegalStateException(msg);
125+
}
126+
127+
public static class TestMain {
128+
static final int THREADS = 8;
129+
130+
public static void main(String... args) throws Exception {
131+
Thread[] ts = new Thread[THREADS];
132+
for (int c = 0; c < THREADS; c++) {
133+
ts[c] = new Thread();
134+
ts[c].start();
135+
}
136+
137+
for (int c = 0; c < THREADS; c++) {
138+
ts[c].join();
139+
}
140+
}
141+
}
142+
143+
}

0 commit comments

Comments
 (0)