Skip to content

Commit c0d8e91

Browse files
jbachorikzhengyu123
authored andcommitted
Add the micro-benchmark for thread filtering (#237)
* Add the micro-benchmark for thread filtering * Do not test for obviously invalid thread id * Relax threadfilter mem order
1 parent edcb336 commit c0d8e91

File tree

14 files changed

+150
-24
lines changed

14 files changed

+150
-24
lines changed

ddprof-lib/src/main/cpp/threadFilter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ void ThreadFilter::add(int thread_id) {
135135
thread_id = mapThreadId(thread_id);
136136
assert(b == bitmap(thread_id));
137137
u64 bit = 1ULL << (thread_id & 0x3f);
138-
if (!(__sync_fetch_and_or(&word(b, thread_id), bit) & bit)) {
138+
if (!(__atomic_fetch_or(&word(b, thread_id), bit, __ATOMIC_RELAXED) & bit)) {
139139
atomicInc(_size);
140140
}
141141
}
@@ -148,7 +148,7 @@ void ThreadFilter::remove(int thread_id) {
148148
}
149149

150150
u64 bit = 1ULL << (thread_id & 0x3f);
151-
if (__sync_fetch_and_and(&word(b, thread_id), ~bit) & bit) {
151+
if (__atomic_fetch_and(&word(b, thread_id), ~bit, __ATOMIC_RELAXED) & bit) {
152152
atomicInc(_size, -1);
153153
}
154154
}

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,15 @@ 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+
} catch (Exception ignore) {
47+
}
48+
}
4649
UNSAFE = unsafe;
4750
}
4851

ddprof-lib/src/test/cpp/ddprof_ut.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@
129129
// increase step gradually to create different bit densities
130130
int step = 1;
131131
int size = 0;
132-
for (int tid = 0; tid < maxTid - step - 1; tid += step, size++) {
132+
for (int tid = 1; tid < maxTid - step - 1; tid += step, size++) {
133133
EXPECT_FALSE(filter.accept(tid));
134134
filter.add(tid);
135135
EXPECT_TRUE(filter.accept(tid));

ddprof-stresstest/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ task runStressTests(type: Exec) {
3939
}
4040
group = 'Execution'
4141
description = 'Run JMH stresstests'
42-
commandLine "${javaHome}/bin/java", '-jar', 'build/libs/stresstests.jar'
42+
commandLine "${javaHome}/bin/java", '-jar', 'build/libs/stresstests.jar', 'counters.*'
4343
}
4444

4545
tasks.withType(JavaCompile).configureEach {

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();

ddprof-stresstest/src/jmh/java/com/datadoghq/profiler/stresstest/scenarios/CapturingLambdas.java renamed to ddprof-stresstest/src/jmh/java/com/datadoghq/profiler/stresstest/scenarios/counters/CapturingLambdas.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.datadoghq.profiler.stresstest.scenarios;
1+
package com.datadoghq.profiler.stresstest.scenarios.counters;
22

33
import com.datadoghq.profiler.stresstest.Configuration;
44
import org.openjdk.jmh.annotations.Benchmark;

ddprof-stresstest/src/jmh/java/com/datadoghq/profiler/stresstest/scenarios/DumpRecording.java renamed to ddprof-stresstest/src/jmh/java/com/datadoghq/profiler/stresstest/scenarios/counters/DumpRecording.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.datadoghq.profiler.stresstest.scenarios;
1+
package com.datadoghq.profiler.stresstest.scenarios.counters;
22

33
import com.datadoghq.profiler.JavaProfiler;
44
import com.datadoghq.profiler.stresstest.Configuration;

ddprof-stresstest/src/jmh/java/com/datadoghq/profiler/stresstest/scenarios/GraphMutation.java renamed to ddprof-stresstest/src/jmh/java/com/datadoghq/profiler/stresstest/scenarios/counters/GraphMutation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.datadoghq.profiler.stresstest.scenarios;
1+
package com.datadoghq.profiler.stresstest.scenarios.counters;
22

33
import org.openjdk.jmh.annotations.Benchmark;
44
import org.openjdk.jmh.annotations.Threads;

ddprof-stresstest/src/jmh/java/com/datadoghq/profiler/stresstest/scenarios/GraphState.java renamed to ddprof-stresstest/src/jmh/java/com/datadoghq/profiler/stresstest/scenarios/counters/GraphState.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.datadoghq.profiler.stresstest.scenarios;
1+
package com.datadoghq.profiler.stresstest.scenarios.counters;
22

33
import com.datadoghq.profiler.stresstest.Configuration;
44
import org.openjdk.jmh.annotations.*;

0 commit comments

Comments
 (0)