Java Profiling
Overview
light-profiling-java-collect.sh
is a shell script used to perform profiling on Java applications. It uses JFR to collect application performance data and upload it to Lighthouse.
Prerequisites
- Install light-agent
- Log in to the Lighthouse platform, go to the "Data Collection" module to get light-agent installation instructions and steps.
Environment Variables
Here are some important environment variables, their default values, and purposes:
- LIGHT_WAY_URL:
- Default value:
http://localhost:8085
- Purpose: Specifies the machine IP where the Lighthouse platform is located, used for uploading generated JFR files.
- Default value:
- APP_ENV:
- Default value:
dev
- Purpose: Specifies the application environment, such as dev, staging, prod, etc.
- Default value:
- APP_VERSION:
- Default value:
1.0.0
- Purpose: Specifies the application version number.
- Default value:
- HOST_NAME:
- Default value: Current hostname
- Purpose: Specifies the hostname to identify the data source.
- SERVICE_NAME:
- Default value: Empty
- Purpose: Specifies the service name to identify the data source.
- PROFILING_DURATION:
- Default value:
60 seconds
- Purpose: Specifies the duration of profiling (in seconds).
- Default value:
- PROFILING_EVENT:
- Default value:
cpu,alloc,wall,lock
- Purpose: Specifies the types of profiling events, supported event types include cpu, alloc, wall, lock, live, etc.
- Default value:
- PROCESS_ID:
- Default value: Empty
- Purpose: Specifies the Java process ID(s) to collect from, multiple process IDs can be separated by commas.
- PROFILING_TAGS:
- Default value: Empty
- Purpose: Specifies additional tags for identifying and categorizing data.
How to Execute
-
Prepare Environment
Ensure the following tools and libraries are installed:
- bash
- curl
- gzip
- uuidgen
- jps (usually installed with JDK)
-
Set Executable Permission, ensure the script has executable permission.
cd /usr/local/light-agent/profiling/java
chmod +x light-profiling-java-collect.sh
- Set Environment Variables Set environment variables as needed. You can set them directly in the script or before running the script.
export LIGHT_WAY_URL=http://your-light-trans-url:8085
export APP_ENV=prod
export APP_VERSION=2.0.0
export HOST_NAME=your-hostname
export SERVICE_NAME=your-service-name
export PROFILING_DURATION=120
export PROFILING_EVENT=cpu,alloc,wall
export PROCESS_ID=1234,5678
export PROFILING_TAGS=tag1,tag2
- Run Script Run the script to start collecting profiling data.
cd /usr/local/light-agent/profiling/java
nohup ./light-profiling-java-collect.sh &
- View Logs The script will generate log files in the runtime directory, one log file per process ID. You can view logs using:
cd /usr/local/light-agent/profiling/java
cat logs/1234.log
- Stop Script
The script will run in an infinite loop until manually stopped. You can use
Ctrl+C
to terminate the script.
Notes
- Log Files: The script will append logs to corresponding files in the runtime directory. If you need to clear log files, you can manually delete or empty these files.
- Resource Consumption: Running the script for a long time may consume significant system resources. Please adjust
PROFILING_DURATION
andPROFILING_EVENT
according to actual needs. - Network Connection: Ensure the
LIGHT_WAY_URL
service address is reachable, otherwise data upload will fail. The IP is the machine where Lighthouse is installed, default port is 8085.
Common Issues
- Q: Why are no log files generated?
A: Ensure the runtime directory exists and has write permissions. If no log files are generated, it might be because the script couldn't find the corresponding Java process.
- Q: What if data upload fails?
A: Check if the
LIGHT_WAY_URL
service address is correct and the network connection is normal. Check the script's error output to locate the issue. - Q: How to adjust profiling event types?
A: Adjust profiling event types by setting the PROFILING_EVENT environment variable. For example:
export PROFILING_EVENT=cpu,alloc,wall,lock
.