Skip to content

Commit 17fd4c8

Browse files
committed
WIP
1 parent 27e0693 commit 17fd4c8

File tree

4 files changed

+158
-12
lines changed

4 files changed

+158
-12
lines changed

ddprof-lib/src/main/java/com/datadoghq/profiler/JavaProfiler.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,16 @@ public final class JavaProfiler {
3737
static final Unsafe UNSAFE;
3838
static {
3939
Unsafe unsafe = null;
40-
String version = System.getProperty("java.version");
41-
try {
42-
Field f = Unsafe.class.getDeclaredField("theUnsafe");
43-
f.setAccessible(true);
44-
unsafe = (Unsafe) f.get(null);
45-
} catch (Exception ignore) { }
40+
// a safety and testing valve to disable unsafe access
41+
if (!Boolean.getBoolean("ddprof.disable_unsafe")) {
42+
try {
43+
Field f = Unsafe.class.getDeclaredField("theUnsafe");
44+
f.setAccessible(true);
45+
unsafe = (Unsafe) f.get(null);
46+
System.out.println("===> Using Unsafe for Java Profiler");
47+
} catch (Exception ignore) {
48+
}
49+
}
4650
UNSAFE = unsafe;
4751
}
4852

ddprof-stresstest/src/jmh/java/com/datadoghq/profiler/stresstest/Main.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,18 @@ public class Main {
1616
public static final String SCENARIOS_PACKAGE = "com.datadoghq.profiler.stresstest.scenarios.";
1717

1818
public static void main(String... args) throws Exception {
19+
String filter = "*";
20+
if (args.length == 1) {
21+
filter = args[0];
22+
} else if (args.length > 1) {
23+
System.err.println("Usage: java -jar ddprof-stresstest.jar [scenario filter]");
24+
System.exit(1);
25+
}
1926
CommandLineOptions commandLineOptions = new CommandLineOptions(args);
2027
Mode mode = Mode.AverageTime;
2128
Options options = new OptionsBuilder()
2229
.parent(new CommandLineOptions(args))
23-
.include(SCENARIOS_PACKAGE + "*")
30+
.include(SCENARIOS_PACKAGE + filter)
2431
.addProfiler(WhiteboxProfiler.class)
2532
.forks(commandLineOptions.getForkCount().orElse(1))
2633
.warmupIterations(commandLineOptions.getWarmupIterations().orElse(0))

ddprof-stresstest/src/jmh/java/com/datadoghq/profiler/stresstest/WhiteboxProfiler.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,16 @@ public Collection<? extends Result> afterIteration(BenchmarkParams benchmarkPara
4646
JavaProfiler.getInstance().stop();
4747
long fileSize = Files.size(jfr);
4848
Files.deleteIfExists(jfr);
49-
List<ScalarResult> results = new ArrayList<>();
50-
results.add(new ScalarResult("jfr_filesize_bytes", fileSize, "", AggregationPolicy.MAX));
51-
for (Map.Entry<String, Long> counter : JavaProfiler.getInstance().getDebugCounters().entrySet()) {
52-
results.add(new ScalarResult(counter.getKey(), counter.getValue(), "", AggregationPolicy.MAX));
49+
if (!Boolean.parseBoolean(benchmarkParams.getParam("skipResults"))) {
50+
List<ScalarResult> results = new ArrayList<>();
51+
results.add(new ScalarResult("jfr_filesize_bytes", fileSize, "", AggregationPolicy.MAX));
52+
for (Map.Entry<String, Long> counter : JavaProfiler.getInstance().getDebugCounters().entrySet()) {
53+
results.add(new ScalarResult(counter.getKey(), counter.getValue(), "", AggregationPolicy.MAX));
54+
}
55+
return results;
56+
} else {
57+
return Collections.emptyList();
5358
}
54-
return results;
5559
} catch (IOException e) {
5660
e.printStackTrace();
5761
return Collections.emptyList();
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package com.datadoghq.profiler.stresstest.scenarios;
2+
3+
import com.datadoghq.profiler.JavaProfiler;
4+
import com.datadoghq.profiler.stresstest.Configuration;
5+
import org.openjdk.jmh.annotations.Benchmark;
6+
import org.openjdk.jmh.annotations.BenchmarkMode;
7+
import org.openjdk.jmh.annotations.Fork;
8+
import org.openjdk.jmh.annotations.Level;
9+
import org.openjdk.jmh.annotations.Measurement;
10+
import org.openjdk.jmh.annotations.Mode;
11+
import org.openjdk.jmh.annotations.OutputTimeUnit;
12+
import org.openjdk.jmh.annotations.Param;
13+
import org.openjdk.jmh.annotations.Scope;
14+
import org.openjdk.jmh.annotations.Setup;
15+
import org.openjdk.jmh.annotations.State;
16+
import org.openjdk.jmh.annotations.TearDown;
17+
import org.openjdk.jmh.annotations.Threads;
18+
import org.openjdk.jmh.annotations.Warmup;
19+
import org.openjdk.jmh.infra.Blackhole;
20+
21+
import java.io.FileWriter;
22+
import java.io.IOException;
23+
import java.io.PrintWriter;
24+
import java.util.concurrent.CountDownLatch;
25+
import java.util.concurrent.ExecutorService;
26+
import java.util.concurrent.Executors;
27+
import java.util.concurrent.TimeUnit;
28+
import java.util.concurrent.atomic.AtomicBoolean;
29+
import java.util.concurrent.atomic.AtomicIntegerArray;
30+
import java.util.concurrent.atomic.AtomicLong;
31+
import java.util.stream.IntStream;
32+
33+
@State(Scope.Benchmark)
34+
public class ThreadFilter2Benchmark extends Configuration {
35+
private JavaProfiler profiler;
36+
37+
@Param(BASE_COMMAND + ",filter=1")
38+
public String command;
39+
40+
@Param("true")
41+
public String skipResults;
42+
43+
@Setup(Level.Trial)
44+
public void setup() throws IOException {
45+
profiler = JavaProfiler.getInstance();
46+
}
47+
48+
@Benchmark
49+
@BenchmarkMode(Mode.Throughput)
50+
@Fork(value = 3, warmups = 3)
51+
@Warmup(iterations = 5)
52+
@Measurement(iterations = 8)
53+
@Threads(1)
54+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
55+
public void threadFilterStress01() throws InterruptedException {
56+
profiler.addThread();
57+
// Simulate per-thread work
58+
Blackhole.consumeCPU(7);
59+
profiler.removeThread();
60+
}
61+
62+
@Benchmark
63+
@BenchmarkMode(Mode.Throughput)
64+
@Fork(value = 3, warmups = 3)
65+
@Warmup(iterations = 5)
66+
@Measurement(iterations = 8)
67+
@Threads(2)
68+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
69+
public void threadFilterStress02() throws InterruptedException {
70+
profiler.addThread();
71+
// Simulate per-thread work
72+
Blackhole.consumeCPU(7);
73+
profiler.removeThread();
74+
}
75+
76+
@Benchmark
77+
@BenchmarkMode(Mode.Throughput)
78+
@Fork(value = 3, warmups = 3)
79+
@Warmup(iterations = 5)
80+
@Measurement(iterations = 8)
81+
@Threads(4)
82+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
83+
public void threadFilterStress04() throws InterruptedException {
84+
profiler.addThread();
85+
// Simulate per-thread work
86+
Blackhole.consumeCPU(7);
87+
profiler.removeThread();
88+
}
89+
90+
@Benchmark
91+
@BenchmarkMode(Mode.Throughput)
92+
@Fork(value = 3, warmups = 3)
93+
@Warmup(iterations = 5)
94+
@Measurement(iterations = 8)
95+
@Threads(8)
96+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
97+
public void threadFilterStress08() throws InterruptedException {
98+
profiler.addThread();
99+
// Simulate per-thread work
100+
Blackhole.consumeCPU(7);
101+
profiler.removeThread();
102+
}
103+
104+
@Benchmark
105+
@BenchmarkMode(Mode.Throughput)
106+
@Fork(value = 3, warmups = 3)
107+
@Warmup(iterations = 5)
108+
@Measurement(iterations = 8)
109+
@Threads(16)
110+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
111+
public void threadFilterStress16() throws InterruptedException {
112+
profiler.addThread();
113+
// Simulate per-thread work
114+
Blackhole.consumeCPU(7);
115+
profiler.removeThread();
116+
}
117+
118+
@Benchmark
119+
@BenchmarkMode(Mode.Throughput)
120+
@Fork(value = 3, warmups = 3)
121+
@Warmup(iterations = 5)
122+
@Measurement(iterations = 8)
123+
@Threads(99)
124+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
125+
public void threadFilterStress99() throws InterruptedException {
126+
profiler.addThread();
127+
// Simulate per-thread work
128+
Blackhole.consumeCPU(7);
129+
profiler.removeThread();
130+
}
131+
}

0 commit comments

Comments
 (0)