0% found this document useful (0 votes)
15 views100 pages

Java Performance with Async Profiler

The document contains code snippets and discussions around various Java and JVM profiling and performance monitoring techniques. These include using tools like perf to profile CPU usage and identify hotspots, using perf-map-agent to symbolicate perf profiles, generating flame graphs from perf data, setting clocksources, using JVMTI to sample object allocations, and setting heap sampling intervals.

Uploaded by

trialistmail
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views100 pages

Java Performance with Async Profiler

The document contains code snippets and discussions around various Java and JVM profiling and performance monitoring techniques. These include using tools like perf to profile CPU usage and identify hotspots, using perf-map-agent to symbolicate perf profiles, generating flame graphs from perf data, setting clocksources, using JVMTI to sample object allocations, and setting heap sampling intervals.

Uploaded by

trialistmail
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd





• •





Socket s = new Socket(host, port);

InputStream in = [Link]();
while ([Link](buf) >= 0) {
// keep reading
}
Socket s = new Socket(host, port);

InputStream in = [Link]();
while ([Link](buf) >= 0) {
// keep reading
}



AsyncGetCallTrace(ASGCT_CallTrace *trace,
jint depth,
void* ucontext)









double avg(Number... numbers) {
double sum = 0;
for (Number n : numbers) {
sum += [Link]();
}
return sum / [Link];
}

x = avg(123, 45.67, 890L, 33.3f, 999, 787878L);


enum {
ticks_no_Java_frame = 0,
ticks_no_class_load = -1,
ticks_GC_active = -2,
ticks_unknown_not_Java = -3,
ticks_not_walkable_not_Java = -4,
ticks_unknown_Java = -5,
ticks_not_walkable_Java = -6,
ticks_unknown_state = -7,
ticks_thread_exit = -8,
ticks_deopt = -9,
ticks_safepoint = -10
}; src/share/vm/prims/[Link]





• S

S





$ perf record –F 1009 java ...
$ perf report
$ perf record –F 1009 java ...
$ perf report

4.70% java [[Link]] [k] clear_page_c


2.10% java [Link] [.] pthread_cond_wait
1.97% java [Link] [.] Unsafe_Park
1.40% java [Link] [.] Parker::park
1.31% java [[Link]] [k] try_to_wake_up
1.31% java [Link] [.] 0x00007f8510e9e757
1.21% java [Link] [.] 0x00007f8510e9e89e
1.17% java [Link] [.] 0x00007f8510e9cc17
[Link]/jvm-profiling-tools/perf-map-agent

$ java -agentpath:/usr/lib/[Link] ...

7fe0e91175e0 140 [Link]::hashCode


7fe0e9117900 20 [Link]::min
7fe0e9117ae0 60 [Link]::length
7fe0e9117d20 180 [Link]::indexOf
SP

prev FP FP

prev FP

0
SP

prev FP FP

• prev FP

0
1. perf record

2. perf script

3. FlameGraph/[Link]

4. FlameGraph/[Link]

[Link]/brendangregg/FlameGraph










xfs_file_aio_read S
sys_read S
perf
system_call_fastpath
readBytes

[Link]::readBytes
[Link]::read
JavaApp::main
!
byte[] buf = new byte[bufSize];

try (FileInputStream in = new FileInputStream(fileName)) {


int bytesRead;
while ((bytesRead = [Link](buf)) > 0) {
...
} ❑
} ❑


51

# cat /sys/devices/system/clocksource/*/available_clocksource
tsc hpet acpi_pm
# echo tsc > /sys/devices/system/clocksource/*/current_clocksource
$ cat /sys/devices/system/clocksource/clocksource0/current_clocksource
xen














synchronized (obj) {
[Link]()
LocalDateTime deadline =
[Link]().plusSeconds(10);

while ([Link]().isBefore(deadline)) {
iterations++;
}



→ [Link]([Link], 24);
List<String> list = new ArrayList<>();



$ stap -e '
probe hotspot.object_alloc { log(probestr) }
'


top
Object in Eden

top
Object in Eden

top




SampledObjectAlloc(jvmtiEnv *jvmti_env,
JNIEnv* jni_env,
jthread thread,
jobject object,
jclass object_klass,
jlong size)

SetHeapSamplingInterval(jint sampling_interval)








































Common questions

Powered by AI

CPU profiling becomes ineffective in scenarios dominated by I/O operations, network latency, or thread contention where the CPU is not the primary bottleneck. In such cases, it is more beneficial to employ profiling techniques that focus on identifying wait times, lock contention, and I/O wait, such as using lock profiling, network tracing, or I/O profiling to gain insights into non-CPU related performance issues .

Enabling the `-XX:+PreserveFramePointer` option incurs a slight performance overhead, less than 5%, but provides a significant benefit by allowing accurate stack tracing during profiling. This is particularly important for tools relying on frame pointers to create comprehensive call graphs, which enhances the quality and depth of performance insights that can be derived from profiling data .

FlameGraph plays a crucial role in visualizing profiling data by providing an interactive representation of stack traces collected during profiling sessions. It converts raw sampling data into a hierarchical view, displaying function calls and their frequency of execution. This visualization tool helps developers quickly identify hot spots and performance bottlenecks within their Java applications .

Java perf support enhances profiling accuracy by providing functionality to record, script, and analyze execution flows using tools like FlameGraph. It supports symbols via perf-map-agent and allows for precise tracking of frame pointers with the `-XX:+PreserveFramePointer` option, providing cleaner and more informative stack traces. This approach is valuable for identifying performance issues within Java applications .

Using hardware counters in CPU profiling provides low-overhead measurement of critical metrics such as cycles, instructions, cache misses, and branch misses. This allows for precise profiling of performance bottlenecks with minimal impact on application performance. These counters are triggered by events such as overflow, and can capture detailed call chains involving both user and kernel space, making them suitable for comprehensive performance analysis .

The `-F` parameter in perf sets the frequency of sampling, which allows for customizing the granularity of profiling data collection. High frequency can lead to more precise data but at the cost of increased overhead, whereas lower frequency might reduce overhead but at the risk of missing short-lived events. Thus, the challenge is to strike a balance that maximizes data accuracy while minimizing profiling-induced performance degradation .

While async-profiler is designed to work across all Java platforms, one notable challenge in integrating it with Windows systems is the lack of native or JVM code profiling support initially part of async-profiler's design. This constraint makes adapting the profiler to Windows environments potentially complex, requiring additional customization or alternative solutions to handle system-specific differences .

Thread state analysis in async-profiler helps performance tuning by identifying threads in different states such as sleeping, waiting, or actively consuming CPU resources. By examining these states, developers can pinpoint bottlenecks like lock contention or excessive waiting on I/O, leading to targeted optimizations that improve application responsiveness and throughput .

Async-profiler distinguishes itself from traditional sampling Java profilers by avoiding safepoint bias and profiling only active threads, including native code, without requiring additional JVM options. It uses AsyncGetCallTrace to gather call stacks directly from signal handlers, leading to more precise profiling with less interference .

Avoiding safepoint bias is essential because it can skew profiling results by only sampling threads that reach a safepoint, potentially overlooking others in critical execution paths. Async-profiler addresses this by sampling threads without safepoint interruptions, thus ensuring a more representative profile of the application's runtime behavior, particularly under heavy loads where thread diversity is vital .

You might also like