Skip to content

Commit aae7077

Browse files
committed
BTRACE-55: Including features from java.lang.management.OperatingSystemMXBean
1 parent 1748a85 commit aae7077

8 files changed

Lines changed: 277 additions & 7 deletions

File tree

bin/btrace

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ if [ -f "${BTRACE_HOME}/build/btrace-client.jar" ] ; then
3636
TOOLS_JAR="${JAVA_HOME}/lib/tools.jar"
3737
;;
3838
esac
39-
${JAVA_HOME}/bin/java -Dcom.sun.btrace.probeDescPath=. -Dcom.sun.btrace.dumpClasses=false -Dcom.sun.btrace.debug=false -Dcom.sun.btrace.unsafe=false -cp ${BTRACE_HOME}/build/btrace-client.jar:${TOOLS_JAR}:/usr/share/lib/java/dtrace.jar com.sun.btrace.client.Main $*
39+
${JAVA_HOME}/bin/java -Dcom.sun.btrace.probeDescPath=. -Dcom.sun.btrace.dumpClasses=false -Dcom.sun.btrace.debug=true -Dcom.sun.btrace.unsafe=false -cp ${BTRACE_HOME}/build/btrace-client.jar:${TOOLS_JAR}:/usr/share/lib/java/dtrace.jar com.sun.btrace.client.Main $*
4040
else
4141
echo "Please set JAVA_HOME before running this script"
4242
exit 1

make/.ant-targets-build.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
all
2+
compile
3+
compile-src
4+
compile-tests
5+
create.javadocs
6+
create.sources
7+
jar
8+
javadoc
9+
upload.snapshots
10+
upload.staging

make/build.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,11 @@
274274
<arg value="-cp ${build.dir}/test"/>
275275
<arg value="-d ${tests.dir}"/>
276276
</apply>
277+
<echo>copying source traces</echo>
278+
<copy todir="${tests.dir}" >
279+
<fileset dir="${traces.dir}" includes="**/*.java"/>
280+
<globmapper from="*.btrace" to="*.java"/>
281+
</copy>
277282
</target>
278283

279284
<target name="compile-dtrace" if="libdtrace.jni.available" depends="prepare">

src/share/classes/com/sun/btrace/BTraceRuntime.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@
105105
import javax.management.NotificationListener;
106106
import javax.management.ObjectName;
107107
import javax.management.openmbean.CompositeData;
108+
109+
import java.lang.management.OperatingSystemMXBean;
110+
111+
import com.sun.management.UnixOperatingSystemMXBean;
108112
import sun.misc.Perf;
109113
import sun.misc.Unsafe;
110114
import sun.reflect.CallerSensitive;
@@ -194,6 +198,7 @@ public void run() {
194198
private static volatile ThreadMXBean threadMBean;
195199
private static volatile List<GarbageCollectorMXBean> gcBeanList;
196200
private static volatile List<MemoryPoolMXBean> memPoolList;
201+
private static volatile OperatingSystemMXBean operatingSystemMXBean;
197202

198203
// bytecode generator that generates Runnable implementations
199204
private static RunnableGenerator runnableGenerator;
@@ -1501,6 +1506,20 @@ static String getMemoryPoolUsage(String poolFormat) {
15011506
return membuffer.toString();
15021507
}
15031508

1509+
static double getSystemLoadAverage() {
1510+
initOperatingSystemBean();
1511+
return operatingSystemMXBean.getSystemLoadAverage();
1512+
}
1513+
1514+
static long getProcessCPUTime() {
1515+
initOperatingSystemBean();
1516+
if (operatingSystemMXBean instanceof com.sun.management.OperatingSystemMXBean) {
1517+
return ((com.sun.management.OperatingSystemMXBean)operatingSystemMXBean).getProcessCpuTime();
1518+
}
1519+
1520+
return -1;
1521+
}
1522+
15041523
static void serialize(Object obj, String fileName) {
15051524
try {
15061525
BufferedOutputStream bos = new BufferedOutputStream(
@@ -1992,6 +2011,29 @@ private static void initMemoryPoolList() {
19922011
}
19932012
}
19942013

2014+
private static OperatingSystemMXBean getOperatingSystemMXBean() {
2015+
try {
2016+
return AccessController.doPrivileged(
2017+
new PrivilegedExceptionAction<OperatingSystemMXBean>() {
2018+
public OperatingSystemMXBean run() throws Exception {
2019+
return ManagementFactory.getOperatingSystemMXBean();
2020+
}
2021+
});
2022+
} catch (Exception exp) {
2023+
throw new UnsupportedOperationException(exp);
2024+
}
2025+
}
2026+
2027+
private static void initOperatingSystemBean() {
2028+
if (operatingSystemMXBean == null) {
2029+
synchronized (BTraceRuntime.class) {
2030+
if (operatingSystemMXBean == null) {
2031+
operatingSystemMXBean = getOperatingSystemMXBean();
2032+
}
2033+
}
2034+
}
2035+
}
2036+
19952037
private static PerfReader getPerfReader() {
19962038
if (perfReader == null) {
19972039
throw new UnsupportedOperationException();

src/share/classes/com/sun/btrace/BTraceUtils.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3267,7 +3267,7 @@ public static void jstack(int numFrames) {
32673267
// passing '5' to skip our own frames to generate stack trace
32683268
jstack(1, numFrames);
32693269
}
3270-
3270+
32713271
private static void jstack(int strip, int numFrames) {
32723272
if (numFrames == 0) return;
32733273
StackTraceElement[] st = Thread.currentThread().getStackTrace();
@@ -3291,7 +3291,7 @@ public static void jstackAll() {
32913291
public static void jstackAll(int numFrames) {
32923292
jstackAll(1, numFrames);
32933293
}
3294-
3294+
32953295
private static void jstackAll(int strip, int numFrames) {
32963296
BTraceRuntime.stackTraceAll(numFrames);
32973297
}
@@ -3319,7 +3319,7 @@ public static String jstackStr(int numFrames) {
33193319
}
33203320
return jstackStr(1, numFrames);
33213321
}
3322-
3322+
33233323
private static String jstackStr(int strip, int numFrames) {
33243324
if (numFrames == 0) {
33253325
return "";
@@ -3351,7 +3351,7 @@ public static String jstackAllStr(int numFrames) {
33513351
}
33523352
return BTraceRuntime.stackTraceAllStr(numFrames);
33533353
}
3354-
3354+
33553355
/**
33563356
* Prints the stack trace of the given exception object.
33573357
*
@@ -4595,11 +4595,11 @@ public static boolean contains(Object[] array, Object value) {
45954595
}
45964596
return false;
45974597
}
4598-
4598+
45994599
public static <E> Object[] toArray(Collection<E> collection) {
46004600
return BTraceRuntime.toArray(collection);
46014601
}
4602-
4602+
46034603
// operations on Deque
46044604
public static <V> void push(Deque<V> queue, V value) {
46054605
BTraceRuntime.push(queue, value);
@@ -6275,6 +6275,25 @@ public static long daemonThreadCount() {
62756275
return BTraceRuntime.getDaemonThreadCount();
62766276
}
62776277

6278+
/**
6279+
* Returns the system load average for the last minute
6280+
*
6281+
* @return the system load average for the last minute
6282+
*/
6283+
public static double systemLoadAverage() {
6284+
return BTraceRuntime.getSystemLoadAverage();
6285+
}
6286+
6287+
/**
6288+
* Returns the CPU time used by the process on which the Java virtual machine is running in nanoseconds.
6289+
*
6290+
* @return the CPU time used by the process on which the JVM is running in nanoseconds. Returns -1 if
6291+
* this operation is not supported on this platform
6292+
*/
6293+
public static long processCPUTime() {
6294+
return BTraceRuntime.getProcessCPUTime();
6295+
}
6296+
62786297
/**
62796298
* Returns the start time of the Java virtual machine in milliseconds.
62806299
* This method returns the approximate time when the Java virtual
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright (c) 2013, Oracle and/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. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package com.sun.btrace;
27+
28+
import java.io.BufferedReader;
29+
import java.io.File;
30+
import java.io.InputStreamReader;
31+
import java.io.PrintWriter;
32+
import java.net.URL;
33+
import org.junit.Before;
34+
import org.junit.Test;
35+
36+
/**
37+
*
38+
* @author Jaroslav Bachorik
39+
*/
40+
public class TestBTraceUtils {
41+
private String cp = null;
42+
private String java = null;
43+
44+
@Before
45+
public void setup() {
46+
cp = System.getProperty("java.class.path");
47+
java = System.getProperty("java.home");
48+
}
49+
@Test
50+
public void testOSMBean() throws Exception {
51+
String cp = System.getProperty("java.class.path");
52+
String java = System.getProperty("java.home");
53+
54+
ProcessBuilder pb = new ProcessBuilder(
55+
java + "/bin/java",
56+
"-cp",
57+
cp,
58+
"resources.Main"
59+
);
60+
61+
Process p = pb.start();
62+
PrintWriter pw = new PrintWriter(p.getOutputStream());
63+
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
64+
65+
String l = br.readLine();
66+
if (l.startsWith("ready:")) {
67+
String pid = l.split("\\:")[1];
68+
System.out.println("Target process ready: " + pid);
69+
// attach(pid, "traces/OSMBeanTest.btrace");
70+
attach(pid, "traces/onmethod/Args.class");
71+
Thread.sleep(500);
72+
73+
pw.println("done");
74+
}
75+
}
76+
77+
private void attach(String pid, String trace) throws Exception {
78+
trace = trace.replace(".btrace", ".java");
79+
URL u = ClassLoader.getSystemResource(trace);
80+
System.out.println(trace);
81+
System.out.println(u);
82+
trace = new File(u.toURI()).getAbsolutePath();
83+
ProcessBuilder pb = new ProcessBuilder(
84+
java + "/bin/java",
85+
"-cp",
86+
cp,
87+
"com.sun.btrace.client.Main",
88+
pid,
89+
trace
90+
);
91+
pb.start();
92+
}
93+
}

src/test/resources/Main.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2013, Oracle and/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. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package resources;
26+
27+
import java.io.BufferedReader;
28+
import java.io.InputStreamReader;
29+
30+
/**
31+
*
32+
* @author Jaroslav Bachorik
33+
*/
34+
public class Main {
35+
36+
public static void main(String[] args) throws Exception {
37+
System.out.println("ready:" + getPID());
38+
System.out.flush();
39+
String resp = new BufferedReader(new InputStreamReader(System.in)).readLine();
40+
if (!"done".equals(resp)) {
41+
System.exit(1);
42+
}
43+
}
44+
45+
private static long getPID() {
46+
String processName
47+
= java.lang.management.ManagementFactory.getRuntimeMXBean().getName();
48+
return Long.parseLong(processName.split("@")[0]);
49+
}
50+
}

src/test/traces/OSMBeanTest.btrace

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2013, Oracle and/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. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package traces;
27+
28+
import com.sun.btrace.annotations.BTrace;
29+
import com.sun.btrace.BTraceUtils.*;
30+
import static com.sun.btrace.BTraceUtils.*;
31+
import com.sun.btrace.annotations.OnTimer;
32+
33+
/**
34+
*
35+
* @author Jaroslav Bachorik
36+
*/
37+
@BTrace(unsafe=true)
38+
public class OSMBeanTest {
39+
@OnTimer(200)
40+
public static void tester() {
41+
try {
42+
double la = Sys.VM.systemLoadAverage();
43+
long t = Sys.VM.processCPUTime();
44+
System.out.println(la + " # " + t);
45+
} catch (Throwable e) {
46+
System.out.println("FAILED");
47+
e.printStackTrace(System.out);
48+
System.out.flush();
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)