Skip to content

Commit 9f458e2

Browse files
author
Jaroslav Bachorik
committed
Fix btraceio#95 : Allow standard string concatenation in BTrace scripts
1 parent 81acbaa commit 9f458e2

24 files changed

Lines changed: 245 additions & 90 deletions
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright (c) 2014, 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 net.java.btrace;
26+
27+
import java.util.concurrent.TimeUnit;
28+
import org.openjdk.jmh.annotations.Benchmark;
29+
import org.openjdk.jmh.annotations.BenchmarkMode;
30+
import org.openjdk.jmh.annotations.Fork;
31+
import org.openjdk.jmh.annotations.Level;
32+
import org.openjdk.jmh.annotations.Measurement;
33+
import org.openjdk.jmh.annotations.Mode;
34+
import org.openjdk.jmh.annotations.OutputTimeUnit;
35+
import org.openjdk.jmh.annotations.Scope;
36+
import org.openjdk.jmh.annotations.Setup;
37+
import org.openjdk.jmh.annotations.State;
38+
import org.openjdk.jmh.annotations.Warmup;
39+
import org.openjdk.jmh.profile.ProfilerFactory;
40+
import org.openjdk.jmh.runner.Runner;
41+
import org.openjdk.jmh.runner.options.Options;
42+
import org.openjdk.jmh.runner.options.OptionsBuilder;
43+
44+
@State(Scope.Thread)
45+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
46+
@Fork(1)
47+
@BenchmarkMode(Mode.AverageTime)
48+
public class StringOpBenchmarks {
49+
private static final String STRING_PART = "h";
50+
51+
StringBuilder sb;
52+
String st;
53+
String res;
54+
55+
@Setup
56+
public void setup() {
57+
st = "";
58+
}
59+
60+
@Setup(Level.Invocation)
61+
public void setupEach() {
62+
sb = new StringBuilder();
63+
}
64+
65+
@Warmup(iterations = 5, time = 200, timeUnit = TimeUnit.MILLISECONDS)
66+
@Measurement(iterations = 5, time = 1200, timeUnit = TimeUnit.MILLISECONDS)
67+
@Benchmark
68+
public void testStringBuilder() {
69+
sb.append(STRING_PART).append(STRING_PART);
70+
}
71+
72+
@Warmup(iterations = 5, time = 200, timeUnit = TimeUnit.MILLISECONDS)
73+
@Measurement(iterations = 5, time = 1200, timeUnit = TimeUnit.MILLISECONDS)
74+
@Benchmark
75+
public void testStringPlus() {
76+
res = st + STRING_PART + STRING_PART;
77+
}
78+
79+
@Warmup(iterations = 5, time = 200, timeUnit = TimeUnit.MILLISECONDS)
80+
@Measurement(iterations = 5, time = 1200, timeUnit = TimeUnit.MILLISECONDS)
81+
@Benchmark
82+
public void testStrCat() {
83+
res = st.concat(STRING_PART).concat(STRING_PART);
84+
}
85+
86+
public static void main(String[] args) throws Exception {
87+
Options opt = new OptionsBuilder()
88+
.addProfiler(ProfilerFactory.getProfilerByName("gc"))
89+
.include(".*" + StringOpBenchmarks.class.getSimpleName() + ".*test.*")
90+
.build();
91+
92+
new Runner(opt).run();
93+
}
94+
}

samples/AllCalls1.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@
3838
@OnMethod(clazz="javax.swing.JTextField", method="/.*/",
3939
location=@Location(value=Kind.CALL, clazz="/.*/", method="/.*/"))
4040
public static void m(@Self Object self, @TargetMethodOrField String method, @ProbeMethodName String probeMethod) { // all calls to the methods with signature "()"
41-
println(Strings.strcat(method, Strings.strcat(" in ", probeMethod)));
41+
println(method + " in " + probeMethod);
4242
}
4343
}

samples/AllCalls2.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@
3939
location=@Location(value=Kind.CALL, clazz="/.*/", method="/.*/"))
4040
public static void n(@Self Object self, @ProbeClassName String pcm, @ProbeMethodName String pmn,
4141
@TargetInstance Object instance, @TargetMethodOrField String method, String text) { // all calls to the methods with signature "(String)"
42-
println(Strings.strcat("Context: ", Strings.strcat(pcm, Strings.strcat("#", pmn))));
43-
print(method);
44-
print(" ");
45-
println(text);
42+
println("Context: " + pcm + "#" + pmn + method + " " + text);
4643
}
47-
}
44+
}

samples/AllLines.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ public class AllLines {
4242
location=@Location(value=Kind.LINE, line=-1)
4343
)
4444
public static void online(@ProbeClassName String pcn, @ProbeMethodName String pmn, int line) {
45-
print(Strings.strcat(pcn, "."));
46-
print(Strings.strcat(pmn, ":"));
47-
println(line);
48-
}
45+
print(pcn + "." + pmn + ":" + line);
46+
}
4947
}

samples/AllMethods.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,18 @@
2929
import static com.sun.btrace.BTraceUtils.*;
3030

3131
/**
32-
* This script traces method entry into every method of
33-
* every class in javax.swing package! Think before using
32+
* This script traces method entry into every method of
33+
* every class in javax.swing package! Think before using
3434
* this script -- this will slow down your app significantly!!
3535
*/
3636
@BTrace public class AllMethods {
3737
@OnMethod(
3838
clazz="/javax\\.swing\\..*/",
3939
method="/.*/"
4040
)
41-
public static void m(@ProbeClassName String probeClass, @ProbeMethodName String probeMethod) {
42-
print(Strings.strcat("entered ", probeClass));
43-
println(Strings.strcat(".", probeMethod));
41+
public static void m(@Self Object o, @ProbeClassName String probeClass, @ProbeMethodName String probeMethod) {
42+
println("this = " + o);
43+
print("entered " + probeClass);
44+
println("." + probeMethod);
4445
}
45-
}
46+
}

samples/AllSync.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
import static com.sun.btrace.BTraceUtils.*;
3030

3131
/**
32-
* This script traces method/block entry into every method of
33-
* every class in javax.swing package! Think before using
32+
* This script traces method/block entry into every method of
33+
* every class in javax.swing package! Think before using
3434
* this script -- this will slow down your app significantly!!
3535
* Note tha Where.BEFORE is default. For synchronized blocks, BEFORE
3636
* means before "monitorenter" bytecode. For synchronized methods, we
@@ -42,18 +42,18 @@
4242
@OnMethod(
4343
clazz="/javax\\.swing\\..*/",
4444
method="/.*/",
45-
location=@Location(value=Kind.SYNC_ENTRY, where=Where.AFTER)
45+
location=@Location(value=Kind.SYNC_ENTRY, where=Where.AFTER)
4646
)
4747
public static void onSyncEntry(Object obj) {
48-
println(Strings.strcat("after synchronized entry: ", identityStr(obj)));
48+
println("after synchronized entry: " + identityStr(obj));
4949
}
5050

5151
@OnMethod(
5252
clazz="/javax\\.swing\\..*/",
5353
method="/.*/",
54-
location=@Location(Kind.SYNC_EXIT)
54+
location=@Location(Kind.SYNC_EXIT)
5555
)
5656
public static void onSyncExit(Object obj) {
57-
println(Strings.strcat("before synchronized exit: ", identityStr(obj)));
57+
println("before synchronized exit: " + identityStr(obj));
5858
}
59-
}
59+
}

samples/Classload.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@
3737
*/
3838
@BTrace public class Classload {
3939
@OnMethod(
40-
clazz="+java.lang.ClassLoader",
40+
clazz="+java.lang.ClassLoader",
4141
method="defineClass",
4242
location=@Location(Kind.RETURN)
43-
)
43+
)
4444
public static void defineclass(@Return Class cl) {
45-
println(Strings.strcat("loaded ", Reflective.name(cl)));
45+
println("loaded " + Reflective.name(cl));
4646
Threads.jstack();
4747
println("==========================");
4848
}

samples/CommandArg.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
)
4242
public static void started() {
4343
if (Strings.strcmp(Threads.name(Threads.currentThread()), Sys.$(2)) == 0) {
44-
println(Strings.strcat("started ", Sys.$(2)));
44+
println("started " + Sys.$(2));
4545
}
4646
}
4747
}

samples/DTraceRefDemo.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ public static void defineClass() {
4646
}
4747

4848
@OnMethod(
49-
clazz="java.lang.ClassLoader",
49+
clazz="java.lang.ClassLoader",
5050
method="defineClass",
5151
location=@Location(Kind.RETURN)
52-
)
52+
)
5353
public static void defineclass(Class cl) {
54-
println(Strings.strcat("loaded ", Reflective.name(cl)));
54+
println("loaded " + Reflective.name(cl));
5555
Threads.jstack();
5656
println("==========================");
5757
}

samples/FileTracker.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
/**
3535
* This sample prints all files opened for read/write
3636
* by a Java process. Note that if you pass FileDescriptor
37-
* to File{Input/Output}Stream or File{Reader/Writer},
37+
* to File{Input/Output}Stream or File{Reader/Writer},
3838
* that is not tracked by this script.
3939
*/
4040
@BTrace public class FileTracker {
@@ -56,7 +56,7 @@ public static void onNewFileInputStream(@Self FileInputStream self, File f) {
5656
)
5757
public static void onNewFileInputStreamReturn() {
5858
if (name != null) {
59-
println(Strings.strcat("opened for read ", name));
59+
println("opened for read " + name);
6060
name = null;
6161
}
6262
}
@@ -77,7 +77,7 @@ public static void onNewFileOutputStream(@Self FileOutputStream self, File f, bo
7777
)
7878
public static void OnNewFileOutputStreamReturn() {
7979
if (name != null) {
80-
println(Strings.strcat("opened for write ", name));
80+
println("opened for write " + name);
8181
name = null;
8282
}
8383
}

0 commit comments

Comments
 (0)