Skip to content

Commit 735e1da

Browse files
committed
8307766: Linux: Provide the option to override the timer slack
Backport-of: 7173c3009e0999f13eaa4bee5eedee7326f9d124
1 parent c486b9b commit 735e1da

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
@@ -86,7 +86,19 @@
8686
"Use CPU_ALLOC code path in os::active_processor_count ") \
8787
\
8888
product(bool, DumpPerfMapAtExit, false, DIAGNOSTIC, \
89-
"Write map file for Linux perf tool at exit")
89+
"Write map file for Linux perf tool at exit") \
90+
\
91+
product(intx, TimerSlack, -1, EXPERIMENTAL, \
92+
"Overrides the timer slack value to the given number of " \
93+
"nanoseconds. Lower value provides more accurate " \
94+
"high-precision timers, at the expense of (possibly) worse " \
95+
"power efficiency. In current Linux, 0 means using the " \
96+
"system-wide default, which would disable the override, but " \
97+
"VM would still print the current timer slack values. Use -1 "\
98+
"to disable both the override and the printouts." \
99+
"See prctl(PR_SET_TIMERSLACK) for more info.") \
100+
\
101+
90102

91103
// end of RUNTIME_OS_FLAGS
92104

src/hotspot/os/linux/os_linux.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
# include <inttypes.h>
109109
# include <sys/ioctl.h>
110110
# include <linux/elf-em.h>
111+
# include <sys/prctl.h>
111112
#ifdef __GLIBC__
112113
# include <malloc.h>
113114
#endif
@@ -913,6 +914,16 @@ bool os::create_thread(Thread* thread, ThreadType thr_type,
913914
if (ret == 0) {
914915
log_info(os, thread)("Thread \"%s\" started (pthread id: " UINTX_FORMAT ", attributes: %s). ",
915916
thread->name(), (uintx) tid, os::Posix::describe_pthread_attr(buf, sizeof(buf), &attr));
917+
918+
// Print current timer slack if override is enabled and timer slack value is available.
919+
// Avoid calling prctl otherwise for extra safety.
920+
if (TimerSlack >= 0) {
921+
int slack = prctl(PR_GET_TIMERSLACK);
922+
if (slack >= 0) {
923+
log_info(os, thread)("Thread \"%s\" (pthread id: " UINTX_FORMAT ") timer slack: %dns",
924+
thread->name(), (uintx) tid, slack);
925+
}
926+
}
916927
} else {
917928
log_warning(os, thread)("Failed to start thread \"%s\" - pthread_create failed (%s) for attributes: %s.",
918929
thread->name(), os::errno_name(ret), os::Posix::describe_pthread_attr(buf, sizeof(buf), &attr));
@@ -4684,6 +4695,15 @@ jint os::init_2(void) {
46844695
FLAG_SET_DEFAULT(UseCodeCacheFlushing, false);
46854696
}
46864697

4698+
// Override the timer slack value if needed. The adjustment for the main
4699+
// thread will establish the setting for child threads, which would be
4700+
// most threads in JDK/JVM.
4701+
if (TimerSlack >= 0) {
4702+
if (prctl(PR_SET_TIMERSLACK, TimerSlack) < 0) {
4703+
vm_exit_during_initialization("Setting timer slack failed: %s", os::strerror(errno));
4704+
}
4705+
}
4706+
46874707
return JNI_OK;
46884708
}
46894709

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)