Performance Optimization Tips

Overview

This document introduces system performance optimization techniques for the Quectel Pi H1 single board computer, covering kernel configuration, system tuning, application optimization, and other aspects. Through reasonable optimization strategies, system response speed, throughput, and energy efficiency can be significantly improved.

Environment Preparation

Before using performance analysis tools, you need to configure the system environment first.

Terminal Environment Configuration

TERM Environment Variable

Some tools (such as perf report, perf top) require the TERM environment variable:

# Temporary setting (valid for current session)
export TERM=xterm-256color

# Permanent setting (add to .bashrc)
echo 'export TERM=xterm-256color' >> ~/.bashrc
source ~/.bashrc

# Verify setting
echo $TERM

perf Tool Permission Configuration

To obtain more detailed kernel performance data, you need to adjust perf-related permissions:

# View current permission settings
cat /proc/sys/kernel/kptr_restrict
cat /proc/sys/kernel/perf_event_paranoid

# Temporary configuration (invalid after reboot)
echo 0 > /proc/sys/kernel/kptr_restrict
echo -1 > /proc/sys/kernel/perf_event_paranoid

# Permanent configuration
cat >> /etc/sysctl.conf << 'EOF'

# perf performance analysis tool configuration
kernel.kptr_restrict = 0
kernel.perf_event_paranoid = -1
EOF

# Apply configuration
sysctl -p

Parameter Description:

  • kptr_restrict = 0: Allow reading kernel symbol addresses (for symbol resolution)
  • perf_event_paranoid = -1: Allow all users to use all perf features

Software Package Installation

Install commonly used performance analysis tools:

# Update package index
apt update

# Install basic performance tools
apt install -y htop sysstat iperf3

# Verify installation
htop --version
iostat -V
iperf3 --version

Performance Analysis Tools

Before optimization, you need to use tools to analyze system bottlenecks and find the root cause of performance issues.

System Performance Monitoring

top/htop - Process Monitoring

Real-time monitoring of system resource usage:

# Install htop (more powerful)
apt install htop

Key Metrics:

  • CPU usage (user, system, iowait)
  • Memory usage (used, buffers, cache)
  • Process priority and status

vmstat - Virtual Memory Statistics

Monitor overall system performance:

# Output once per second, output 10 times total
vmstat 1 10

Key Metrics:

  • si/so: swap in/out (should be close to 0)
  • bi/bo: block device I/O (block in/out)
  • wa: I/O wait time percentage

iostat - I/O Statistics

Analyze disk I/O performance:

# Install sysstat
apt install sysstat

# Output disk statistics once per second
iostat -x 1

Key Metrics:

  • %util: Device utilization
  • await: Average I/O wait time
  • svctm: Average service time

perf - Performance Analysis Tool

Powerful performance analysis tool provided by Linux kernel:

# Record system performance data (10 seconds)
perf record -a -g sleep 10

# View report (requires TERM environment variable)
perf report

# View CPU hotspots (real-time display)
perf top

# Or use non-interactive mode
perf top -n

Prerequisites:

  • Need to set TERM environment variable (see "Environment Preparation" section)
  • It is recommended to configure perf permissions to obtain complete kernel symbol information
  • If permission warnings are encountered, they can be ignored or configured according to the "Environment Preparation" section

Common Warning:

WARNING: Kernel address maps are restricted

This is a normal security restriction and can be resolved by configuring kernel.kptr_restrict.

Network Performance Testing

iperf3 - Network Bandwidth Test

Test network throughput:

# Install iperf3 (if not installed)
apt install iperf3

# View version
iperf3 --version

# Server mode (listen)
iperf3 -s

# Client mode (test for 10 seconds)
iperf3 -c <server_IP> -t 10

# Bidirectional test
iperf3 -c <server_IP> -t 10 --bidir

# Use multiple parallel streams
iperf3 -c <server_IP> -t 10 -P 4

Test Example:

# On server side (192.168.1.100)
iperf3 -s

# On client side
iperf3 -c 192.168.1.100 -t 10

ping - Network Latency Test

Test network latency and packet loss rate:

ping -c 100 <target_IP>

Kernel Optimization

Kernel Configuration Optimization

# Enter kernel source directory and edit configuration
cd sources/quectel-src/kernel/qcm6490-idp/kernel-source
make menuconfig

Key Configurations:

  • Scheduler: Server (high throughput) / Desktop (balanced) / Low-Latency (real-time)
  • Memory: Enable transparent hugepages (Transparent Hugepage)
  • Debug: Disable all debug options in production environment

Boot Optimization

# Disable unnecessary services
systemctl disable <service_name>

# Disable unused peripherals in device tree
&usb3 {
    status = "disabled";
};

System-Level Optimization

CPU Optimization

# CPU frequency scaling policy: performance / powersave / ondemand (recommended)
echo ondemand > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor

# CPU affinity (bind process to specific cores)
taskset -cp 0-3 <PID>

Memory Optimization

Edit /etc/sysctl.conf:

vm.swappiness = 10              # Reduce swap usage
vm.dirty_ratio = 10             # Dirty page cache ratio
vm.dirty_background_ratio = 5
vm.min_free_kbytes = 65536      # Minimum free memory

Enable zram compressed memory:

modprobe zram
echo 536870912 > /sys/block/zram0/disksize
mkswap /dev/zram0
swapon /dev/zram0 -p 10

Storage I/O Optimization

# Edit /etc/fstab to add noatime mount option
/dev/mmcblk0p8  /  ext4  defaults,noatime,nodiratime  0  1

# I/O scheduler (deadline suitable for eMMC, bfq general purpose)
echo deadline > /sys/block/mmcblk0/queue/scheduler

# Increase read-ahead cache
echo 2048 > /sys/block/mmcblk0/queue/read_ahead_kb

Network Optimization

Edit /etc/sysctl.conf:

net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.core.netdev_max_backlog = 5000
net.ipv4.tcp_fastopen = 3
net.ipv4.tcp_tw_reuse = 1

Apply configuration: sysctl -p

Application Optimization

Compiler Optimization

# Use -O3 optimization during compilation
gcc -O3 -flto program.c -o program

# Multi-threaded parallelization
gcc -fopenmp -O3 program.c -o program

Library Optimization

# Use high-performance math library
apt install libopenblas-dev

# Prelink to reduce startup time
apt install prelink
prelink -amR

Benchmarking

# CPU test
apt install sysbench
sysbench cpu --threads=4 --time=60 run

# Storage test
dd if=/dev/zero of=/tmp/testfile bs=1M count=1024 conv=fdatasync
dd if=/tmp/testfile of=/dev/null bs=1M

# I/O test
apt install fio
fio --name=test --rw=randwrite --bs=4k --size=1G --runtime=60

Optimization Checklist

  • Environment preparation: DNS, TERM, perf permissions
  • Performance analysis: htop, iostat, perf, iperf3
  • CPU: Frequency scaling policy (ondemand)
  • Memory: swappiness, zram
  • Storage: noatime, I/O scheduler
  • Network: TCP/IP parameters
  • Compilation: Use -O3 optimization
  • Disable unnecessary services

Common Issues and Solutions

Software Package Installation Issues

DNS Resolution Failure

Symptoms:

Error: Failed to fetch https://mirrors.xxx.xxx/...
Temporary failure resolving 'mirrors.xxx.xxx'

Solution:

# 1. Check network connection
ping -c 3 8.8.8.8

# 2. Configure DNS
cat > /etc/resolv.conf << 'EOF'
nameserver 8.8.8.8
nameserver 114.114.114.114
EOF

# 3. Test DNS
ping -c 3 www.baidu.com

# 4. Update software sources and retry
apt update
apt install <package_name>

Package Manager Error

Issue: Using opkg command reports command not found

Cause: Quectel Pi H1 uses Linux system, should use apt instead of opkg

Solution:

# Wrong usage
opkg install htop

# Correct usage
apt install htop

Performance Analysis Tool Issues

perf Error "TERM needs set"

Symptoms:

# perf report
TERM environment variable needs set.

# perf top  
TERM environment variable needs set.

Solution:

# Temporary setting
export TERM=xterm-256color

# Permanent setting
echo 'export TERM=xterm-256color' >> ~/.bashrc
source ~/.bashrc

# Verify
echo $TERM

perf Kernel Symbol Warning

Symptoms:

WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted

Impact: Cannot obtain detailed kernel symbol information, performance analysis is not accurate enough

Solution:

# Check current settings
cat /proc/sys/kernel/kptr_restrict
cat /proc/sys/kernel/perf_event_paranoid

# Temporary configuration
echo 0 > /proc/sys/kernel/kptr_restrict
echo -1 > /proc/sys/kernel/perf_event_paranoid

# Permanent configuration
cat >> /etc/sysctl.conf << 'EOF'
kernel.kptr_restrict = 0
kernel.perf_event_paranoid = -1
EOF
sysctl -p

htop/top Chinese Garbled Text

Symptoms: Terminal displays garbled text when showing Chinese

Cause: Terminal encoding setting issue

Solution:

# Set locale
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8

# Or use English interface to avoid garbled text
export LANG=C

# Permanent setting
echo 'export LANG=en_US.UTF-8' >> ~/.bashrc

Network Testing Issues

iperf3 Command Not Found

Symptoms:

bash: iperf3: command not found

Solution:

# Install iperf3
apt update
apt install iperf3

# Verify installation
iperf3 --version

iperf3 Connection Failed

Symptoms: Client cannot connect to server

Troubleshooting Steps:

# 1. Check if server is started
# On server side
iperf3 -s

# 2. Check network connectivity
ping <server_IP>

# 3. Check firewall
# View firewall status
iptables -L -n

# Temporarily open iperf3 port (5201)
iptables -A INPUT -p tcp --dport 5201 -j ACCEPT

# 4. Specify listen address
iperf3 -s -B 0.0.0.0

System Configuration Issues

sysctl Configuration Not Effective

Symptoms: Configuration not effective after modifying /etc/sysctl.conf

Solution:

# 1. Apply configuration
sysctl -p

# 2. Verify configuration
sysctl -a | grep <parameter_name>

# 3. If still not effective, check configuration file syntax
cat /etc/sysctl.conf

# 4. Manually set for testing
echo <value> > /proc/sys/<path>

Permission Denied

Symptoms:

Permission denied

Solution:

# 1. Confirm running as root user
whoami

# 2. Switch to root
sudo su -
# or
su -

# 3. Or use sudo
sudo <command>

Performance Issue Troubleshooting

Issue Diagnostic Tool Solution
High CPU htop, perf top Find high CPU processes, optimize algorithms
Insufficient Memory free, vmstat Enable zram, check for memory leaks
Slow I/O iostat Optimize mount options, change scheduler
Network Latency ping, iperf3 Adjust TCP/IP parameters, check connections

Optimization Principles

Measure first, optimize, then verify - Don't optimize prematurely!

  1. Use tools to find bottlenecks (htop, iostat, perf)
  2. Optimize for bottlenecks
  3. Re-measure to verify effectiveness
  4. Balance performance, power consumption, and complexity

Performance and Power Consumption Balance:

  • Performance priority: echo performance > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
  • Balanced mode: echo ondemand > ... (recommended)
  • Power saving priority: echo powersave > ...

Summary

Optimization Key Points:

  1. Environment Preparation: DNS, TERM, perf permissions
  2. Performance Analysis: Use tools to locate bottlenecks
  3. System Tuning: CPU, memory, I/O, network
  4. Continuous Monitoring: Prevent performance degradation