Skip to content

Commit fcf1e83

Browse files
Merge pull request #2206 from madeline-underwood/java_flame
Java flame_JA to sign off
2 parents 7ce9aba + 2ce6f68 commit fcf1e83

File tree

4 files changed

+161
-93
lines changed

4 files changed

+161
-93
lines changed

content/learning-paths/servers-and-cloud-computing/java-perf-flamegraph/1_setup.md

Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Setup Tomcat Benchmark Environment
2+
title: Set up Tomcat benchmark environment
33
weight: 2
44

55
### FIXED, DO NOT MODIFY
@@ -8,43 +8,57 @@ layout: learningpathall
88

99

1010
## Overview
11-
There are numerous performance analysis methods and tools for Java applications, among which the call stack flame graph method is regarded as a conventional entry-level approach. Therefore, generating flame graphs is considered a basic operation.
12-
Various methods and tools are available for generating Java flame graphs, including `async-profiler`, `Java Agent`, `jstack`, `JFR` (Java Flight Recorder), etc.
13-
This Learning Path focuses on introducing two simple and easy-to-use methods: `async-profiler` and `Java Agent`.
1411

12+
Flame graphs are a widely used entry point for analyzing Java application performance. Tools for generating flame graphs include `async-profiler`, Java agents, `jstack`, and Java Flight Recorder (JFR). This Learning Path focuses on two practical approaches: using `async-profiler` and a Java agent.
1513

16-
## Setup Benchmark Server - Tomcat
17-
- [Apache Tomcat](https://tomcat.apache.org/) is an open-source Java Servlet container that enables running Java web applications, handling HTTP requests and serving dynamic content.
18-
- As a core component in Java web development, Apache Tomcat supports Servlet, JSP, and WebSocket technologies, providing a lightweight runtime environment for web apps.
14+
In this section, you'll set up a benchmark environment using Apache Tomcat and `wrk2` to simulate HTTP load and evaluate performance on an Arm-based server.
15+
16+
## Set up the Tomcat benchmark server
17+
[Apache Tomcat](https://tomcat.apache.org/) is an open-source Java Servlet container that runs Java web applications, handles HTTP requests, and serves dynamic content. It supports technologies such as Servlet, JSP, and WebSocket.
18+
19+
## Install the Java Development Kit (JDK)
20+
21+
Install OpenJDK 21 on your Arm-based Ubuntu server:
1922

20-
1. Start by installing Java Development Kit (JDK) on your Arm-based server running Ubuntu:
2123
```bash
2224
sudo apt update
2325
sudo apt install -y openjdk-21-jdk
2426
```
2527

26-
2. Next, you can install Tomcat by either [building it from source](https://github.com/apache/tomcat) or downloading the pre-built package simply from [the official website](https://tomcat.apache.org/whichversion.html)
28+
## Install Tomcat
29+
30+
Download and extract Tomcat:
31+
2732
```bash
2833
wget -c https://dlcdn.apache.org/tomcat/tomcat-11/v11.0.9/bin/apache-tomcat-11.0.9.tar.gz
2934
tar xzf apache-tomcat-11.0.9.tar.gz
3035
```
36+
Alternatively, you can build Tomcat [from source](https://github.com/apache/tomcat).
37+
38+
## Enable access to Tomcat examples
39+
40+
To access the built-in examples from your local network or external IP, modify the `context.xml` file:
3141

32-
3. If you intend to access the built-in examples of Tomcat via an intranet IP or even an external IP, you need to modify a configuration file as shown:
3342
```bash
3443
vi apache-tomcat-11.0.9/webapps/examples/META-INF/context.xml
3544
```
36-
Then change the allow value as shown and save the changes:
37-
```output
38-
# change <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
39-
# to
45+
Update the `RemoteAddrValve` configuration to allow all IPs:
46+
47+
<!-- Before -->
48+
<Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
49+
50+
<!-- After -->
4051
<Valve className="org.apache.catalina.valves.RemoteAddrValve" allow=".*" />
41-
```
42-
Now you can start Tomcat Server:
52+
53+
## Start the Tomcat server
54+
55+
Start the server:
56+
4357
```bash
4458
./apache-tomcat-11.0.9/bin/startup.sh
4559
```
4660

47-
The output from starting the server should look like:
61+
You should see output like:
4862

4963
```output
5064
Using CATALINA_BASE: /home/ubuntu/apache-tomcat-11.0.9
@@ -56,42 +70,58 @@ Using CATALINA_OPTS:
5670
Tomcat started.
5771
```
5872

59-
4. If you can access the page at "http://${tomcat_ip}:8080/examples" via a browser, you can proceed to the next benchmarking step.
73+
## Confirm server access
74+
75+
In your browser, open: `http://${tomcat_ip}:8080/examples`.
76+
77+
You should see the Tomcat welcome page and examples, as shown below:
78+
79+
![Screenshot of the Tomcat homepage showing version and welcome panel alt-text#center](./_images/lp-tomcat-homepage.png "Apache Tomcat homepage")
6080

61-
![example image alt-text#center](./_images/lp-tomcat-homepage.png "Tomcat-HomePage")
81+
![Screenshot of the Tomcat examples page showing servlet and JSP demo links alt-text#center](./_images/lp-tomcat-examples.png "Apache Tomcat examples")
6282

63-
![example image alt-text#center](./_images/lp-tomcat-examples.png "Tomcat-Examples")
83+
{{% notice Note %}}Make sure port 8080 is open in the security group of the IP address for your Arm-based Linux machine.{{% /notice%}}
6484

65-
Make sure port 8080 is open in the security group of the IP address for your Arm-based Linux machine.
85+
## Set up the benchmarking client using wrk2
86+
[Wrk2](https://github.com/giltene/wrk2) is a high-performance HTTP benchmarking tool specialized in generating constant throughput loads and measuring latency percentiles for web services. `wrk2` is an enhanced version of `wrk` that provides accurate latency statistics under controlled request rates, ideal for performance testing of HTTP servers.
6687

67-
## Setup Benchmark Client - [wrk2](https://github.com/giltene/wrk2)
68-
`wrk2` is a high-performance HTTP benchmarking tool specialized in generating constant throughput loads and measuring latency percentiles for web services. `wrk2` is an enhanced version of `wrk` that provides accurate latency statistics under controlled request rates, ideal for performance testing of HTTP servers.
88+
{{% notice Note %}}
89+
Currently `wrk2` is only supported on x86 machines. Run the benchmark client steps below on an `x86_64` server running Ubuntu.
90+
{{%/notice%}}
6991

70-
Currently `wrk2` is only supported on x86 machines. You will run the Benchmark Client steps shown below on an x86_64 server running Ubuntu.
92+
## Install dependencies
7193

94+
Install the required packages:
7295

73-
1. To use `wrk2`, you will need to install some essential tools before you can build it:
7496
```bash
7597
sudo apt-get update
7698
sudo apt-get install -y build-essential libssl-dev git zlib1g-dev
7799
```
78100

79-
2. Now you can clone and build it from source:
101+
## Clone and build wrk2
102+
103+
Clone the repository and compile the tool:
104+
80105
```bash
81106
sudo git clone https://github.com/giltene/wrk2.git
82107
cd wrk2
83108
sudo make
84109
```
85-
Move the executable to somewhere in your PATH:
110+
111+
Move the binary to a directory in your system’s PATH:
112+
86113
```bash
87114
sudo cp wrk /usr/local/bin
88115
```
89116

90-
3. Finally, you can run the benchmark of Tomcat through wrk2.
117+
## Run the benchmark
118+
119+
Use the following command to benchmark the HelloWorld servlet running on Tomcat:
120+
91121
```bash
92122
wrk -c32 -t16 -R50000 -d60 http://${tomcat_ip}:8080/examples/servlets/servlet/HelloWorldExample
93123
```
94-
Shown below is the output of wrk2:
124+
You should see output similar to:
95125

96126
```console
97127
Running 1m test @ http://172.26.203.139:8080/examples/servlets/servlet/HelloWorldExample
Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,54 @@
11
---
2-
title: Java FlameGraph - Async-profiler
2+
title: Generate Java flame graphs using async-profiler
33
weight: 3
44

55
### FIXED, DO NOT MODIFY
66
layout: learningpathall
77
---
88

9-
## Java Flame Graph Generation using [async-profiler](https://github.com/async-profiler/async-profiler)
10-
`async-profiler` is a low-overhead sampling profiler for JVM applications, capable of capturing CPU, allocation, and lock events to generate actionable performance insights.
11-
A lightweight tool for Java performance analysis, `async-profiler` produces flame graphs and detailed stack traces with minimal runtime impact, suitable for production environments. In this section, you will learn how to install and use it to profile your Tomcat instance being benchmarked.
9+
## Overview
10+
11+
[Async-profiler](https://github.com/async-profiler/async-profiler) is a low-overhead sampling profiler for JVM applications. It can capture CPU usage, memory allocations, and lock events to generate flame graphs and detailed stack traces.
12+
13+
14+
This tool is well-suited for production environments due to its minimal runtime impact. In this section, you'll install and run `async-profiler` to analyze performance on your Tomcat instance under benchmark load.
15+
16+
{{%notice Note%}}
17+
Install and run `async-profiler` on the same Arm-based Linux machine where Tomcat is running to ensure accurate profiling.
18+
{{%/notice%}}
19+
20+
## Install async-profiler
21+
22+
Download and extract the latest release:
1223

13-
You should deploy `async-profiler` on the same Arm Linux machine where Tomcat is running to ensure accurate performance profiling.
14-
1. Download async-profiler-4.0 and uncompress
1524
```bash
1625
wget -c https://github.com/async-profiler/async-profiler/releases/download/v4.0/async-profiler-4.0-linux-arm64.tar.gz
1726
tar xzf async-profiler-4.0-linux-arm64.tar.gz
1827
```
1928

20-
2. Run async-profiler to profile the Tomcat instance under benchmarking
29+
## Run the profiler
30+
31+
Navigate to the profiler binary directory:
32+
2133
```bash
2234
cd async-profiler-4.0-linux-arm64/bin
23-
./asprof -d 10 -f profile.html $(jps | awk /Bootstrap/'{print $1}')
2435
```
25-
You can also run:
36+
Run async-profiler against the Tomcat process:
37+
38+
```bash
39+
./asprof -d 10 -f profile.html $(jps | awk /Bootstrap/'{print $1}')
2640
```
41+
Alternatively, if you already know the process ID (PID):
42+
43+
```bash
2744
./asprof -d 10 -f profile.html ${tomcat_process_id}
2845
```
46+
* `-d 10` sets the profiling duration to 10 seconds
47+
48+
* `-f profile.html` specifies the output file
49+
50+
## View the flame graph
2951

30-
3. Now launch `profile.html` in a browser to analyse your profiling result
52+
Open the generated `profile.html` file in a browser to view your Java flame graph:
3153

32-
![example image alt-text#center](_images/lp-flamegraph-async.png "Java Flame Graph via async-profiler")
54+
![Flame graph visualization showing Java method stack traces captured using async-profile alt-text#center](_images/lp-flamegraph-async.png "Java flame graph built using async-profiler")
Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,72 @@
11
---
2-
title: Java FlameGraph - Java Agent
2+
title: Generate Java flame graphs using a Java agent
33
weight: 4
44

55

66
### FIXED, DO NOT MODIFY
77
layout: learningpathall
88
---
99

10-
## Java Flame Graph Generation using Java agent and perf
11-
To profile a Java application with perf and ensure proper symbol resolution, you must include `libperf-jvmti.so` when launching the Java application.
12-
- `libperf-jvmti.so` is a JVM TI agent library enabling perf to resolve Java symbols, facilitating accurate profiling of Java applications.
13-
- A specialized shared library, `libperf-jvmti.so` bridges perf and the JVM, enabling proper translation of memory addresses to Java method names during profiling.
10+
## Overview
11+
12+
You can profile a Java application using `perf` by including a Java agent that enables symbol resolution. This allows `perf` to capture meaningful method names instead of raw memory addresses.
13+
14+
The required library is `libperf-jvmti.so`, a JVM Tool Interface (JVMTI) agent that bridges `perf` and the JVM. It ensures that stack traces collected during profiling can be accurately resolved to Java methods.
15+
16+
In this section, you'll configure Tomcat to use this Java agent and generate a flame graph using the FlameGraph toolkit.
17+
18+
## Locate the Java agent
19+
20+
Locate the `libperf-jvmti.so` library:
1421

15-
1. Find where `libperf-jvmti.so` is installed on your Arm-based Linux server:
1622
```bash
1723
pushd /usr/lib
1824
find . -name libperf-jvmti.so`
1925
```
20-
The output will show the path of the library that you will then include in your Tomcat setup file:
26+
The output will show the path to the shared object file:
27+
28+
## Modify Tomcat configuration
29+
30+
Open the Tomcat launch script:
31+
2132
```bash
2233
vi apache-tomcat-11.0.9/bin/catalina.sh
2334
```
24-
Add JAVA_OPTS="$JAVA_OPTS -agentpath:/usr/lib/linux-tools-6.8.0-63/libperf-jvmti.so -XX:+PreserveFramePointer" to `catalina.sh`. Make sure the path matches the location on your machine from the previous step.
35+
Add the following line (replace the path if different on your system):
2536

37+
```bash
38+
JAVA_OPTS="$JAVA_OPTS -agentpath:/usr/lib/linux-tools-6.8.0-63/libperf-jvmti.so -XX:+PreserveFramePointer"
39+
```
2640
Now shutdown and restart Tomcat:
41+
2742
```bash
2843
cd apache-tomcat-11.0.9/bin
2944
./shutdown.sh
3045
./startup.sh
3146
```
3247

33-
2. Use perf to profile Tomcat, and restart wrk that running on your x86 instance if necessary:
48+
## Run perf to record profiling data
49+
50+
Run the following command to record a 10-second profile of the Tomcat process:
51+
3452
```bash
3553
sudo perf record -g -k1 -p $(jps | awk /Bootstrap/'{print $1}') -- sleep 10
3654
```
37-
This command will record the collected data in a file named `perf.data`
55+
This generates a file named `perf.data`.
56+
57+
If needed, restart `wrk` on your x86 client to generate load during profiling.
58+
59+
## Generate a flame graph
60+
61+
Clone the FlameGraph repository and add it to your PATH:
3862

39-
3. Convert the collected `perf.data` into a Java flame graph using FlameGraph
4063
```bash
4164
git clone https://github.com/brendangregg/FlameGraph.git
4265
export PATH=$PATH:`pwd`/FlameGraph
4366
sudo perf inject -j -i perf.data | perf script | stackcollapse-perf.pl | flamegraph.pl &> profile.svg
4467
```
68+
## View the result
4569

46-
4. You can now successfully launch `profile.svg` in a browser to analyse the profiling result
70+
You can now launch `profile.svg` in a browser to analyse the profiling result:
4771

48-
![example image alt-text#center](_images/lp-flamegraph-agent.png "Java Flame Graph via Java agent and perf")
72+
![Flame graph visualization of Java method calls collected using perf and a Java agent on a Tomcat server alt-text#center](_images/lp-flamegraph-agent.png "Java flame graph built through Java agent and perf")
Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,49 @@
11
---
2-
title: Analyze Java Performance on Arm servers using FlameGraphs
3-
4-
draft: true
5-
cascade:
6-
draft: true
7-
2+
title: Analyze Java performance on Arm servers using flame graphs
83
minutes_to_complete: 30
94

10-
who_is_this_for: This is an introductory topic for software developers looking to analyze the performance of their Java applications on the Arm Neoverse based servers using flame graphs.
5+
who_is_this_for: This is an introductory topic for developers who want to analyze the performance of Java applications on Arm Neoverse-based servers using flame graphs.
116

127
learning_objectives:
13-
- How to set up tomcat benchmark environment
14-
- How to generate flame graphs for Java applications using async-profiler
15-
- How to generate flame graphs for Java applications using Java agent
8+
- Set up a benchmarking environment using Tomcat and wrk2
9+
- Generate flame graphs using async-profiler
10+
- Generate flame graphs using a Java agent
1611

1712
prerequisites:
18-
- An Arm-based and x86 computer running Ubuntu. You can use a server instance from a cloud service provider of your choice.
19-
- Basic familiarity with Java applications and flame graphs
13+
- Access to both Arm-based and x86-based computers running Ubuntu (you can use cloud-based server instances)
14+
- Basic familiarity with Java applications and performance profiling using flame graphs
2015

21-
author: Ying Yu, Martin Ma
16+
author:
17+
- Ying Yu
18+
- Martin Ma
2219

23-
### Tags
20+
# Tags
2421
skilllevels: Introductory
2522
subjects: Performance and Architecture
2623
armips:
27-
- Neoverse
28-
24+
- Neoverse
25+
2926
tools_software_languages:
30-
- OpenJDK-21
31-
- Tomcat
32-
- Async-profiler
33-
- FlameGraph
34-
- wrk2
35-
operatingsystems:
36-
- Linux
27+
- OpenJDK-21
28+
- Tomcat
29+
- async-profiler
30+
- FlameGraph
31+
- wrk2
3732

33+
operatingsystems:
34+
- Linux
3835

3936
further_reading:
40-
- resource:
41-
title: OpenJDK Wiki
42-
link: https://wiki.openjdk.org/
43-
type: documentation
44-
- resource:
45-
title: Java FlameGraphs
46-
link: https://www.brendangregg.com/flamegraphs.html
47-
type: website
48-
49-
50-
51-
52-
### FIXED, DO NOT MODIFY
53-
# ================================================================================
54-
weight: 1 # _index.md always has weight of 1 to order correctly
55-
layout: "learningpathall" # All files under learning paths have this same wrapper
56-
learning_path_main_page: "yes" # This should be surfaced when looking for related content. Only set for _index.md of learning path content.
37+
- resource:
38+
title: OpenJDK Wiki
39+
link: https://wiki.openjdk.org/
40+
type: documentation
41+
- resource:
42+
title: Java FlameGraphs
43+
link: https://www.brendangregg.com/flamegraphs.html
44+
type: website
45+
46+
weight: 1
47+
layout: "learningpathall"
48+
learning_path_main_page: "yes"
5749
---

0 commit comments

Comments
 (0)