# Performance Tuning Guide

**Last Updated:** 2025-01-20  
**Document Version:** 1.0  
**Status:** Active Documentation

---

## Overview

This guide provides performance tuning recommendations for Proxmox infrastructure, including host optimization, VM/container optimization, storage optimization, and network optimization.

---

## Host Performance Tuning

### CPU Optimization

**Settings:**

1. **CPU Governor:**
   ```bash
   # Set performance governor
   cpupower frequency-set -g performance
   
   # Make permanent
   echo 'GRUB_CMDLINE_LINUX_DEFAULT="quiet intel_pstate=disable"' >> /etc/default/grub
   update-grub
   ```

2. **CPU Affinity:**
   - Pin critical VMs to specific CPU cores
   - Isolate CPU cores for host operations
   - Use CPU sets for resource allocation

### Memory Optimization

**Settings:**

1. **Transparent Huge Pages:**
   ```bash
   # Check current setting
   cat /sys/kernel/mm/transparent_hugepage/enabled
   
   # Set to always (for performance)
   echo always > /sys/kernel/mm/transparent_hugepage/enabled
   ```

2. **Swappiness:**
   ```bash
   # Reduce swappiness (default is 60)
   echo 10 > /proc/sys/vm/swappiness
   
   # Make permanent
   echo 'vm.swappiness=10' >> /etc/sysctl.conf
   ```

3. **Memory Overcommit:**
   ```bash
   # Allow memory overcommit (for Proxmox)
   echo 1 > /proc/sys/vm/overcommit_memory
   ```

### Storage Optimization

**ZFS Tuning:**

1. **ARC Size:**
   ```bash
   # Set ARC max size (adjust based on RAM)
   echo 'options zfs zfs_arc_max=4294967296' >> /etc/modprobe.d/zfs.conf
   ```

2. **ZFS Recordsize:**
   ```bash
   # Set recordsize for database workloads
   zfs set recordsize=16k <pool>/<dataset>
   ```

3. **ZFS Compression:**
   ```bash
   # Enable compression (lz4 recommended)
   zfs set compression=lz4 <pool>/<dataset>
   ```

**LVM Tuning:**

1. **I/O Scheduler:**
   ```bash
   # Set to deadline or noop for SSDs
   echo deadline > /sys/block/sda/queue/scheduler
   ```

---

## VM/Container Performance

### VM Optimization

**CPU Settings:**

1. **CPU Type:**
   - Use `host` CPU type for best performance
   - Or use specific CPU model matching host

2. **CPU Cores:**
   - Allocate appropriate number of cores
   - Avoid over-allocation
   - Consider CPU pinning for critical VMs

**Memory Settings:**

1. **Memory Allocation:**
   - Allocate sufficient memory
   - Use ballooning for dynamic allocation
   - Monitor memory usage

2. **Memory Ballooning:**
   ```bash
   # Enable ballooning in VM config
   balloon: 1024
   ```

**Storage Settings:**

1. **Disk Cache:**
   - Use `writeback` cache for better performance
   - Use `none` for critical data integrity

2. **Disk I/O:**
   - Set appropriate I/O limits
   - Use SSD storage for high I/O workloads
   - Consider separate storage pools

### Container Optimization

**Resource Limits:**

1. **CPU Limits:**
   ```bash
   # Set CPU limit in container config
   lxc.cgroup.cpuset.cpus = "0-3"
   ```

2. **Memory Limits:**
   ```bash
   # Set memory limit
   lxc.cgroup.memory.limit_in_bytes = 2147483648
   ```

3. **I/O Limits:**
   ```bash
   # Set I/O limits
   lxc.cgroup.blkio.weight = 500
   ```

---

## Network Performance

### Network Optimization

**Settings:**

1. **Network Interface:**
   - Use virtio network drivers
   - Enable SR-IOV if available
   - Tune network buffer sizes

2. **Bridge Optimization:**
   ```bash
   # Increase bridge forward delay
   echo 0 > /sys/class/net/vmbr0/bridge/forward_delay
   ```

3. **TCP Tuning:**
   ```bash
   # Increase TCP buffer sizes
   echo 'net.core.rmem_max = 16777216' >> /etc/sysctl.conf
   echo 'net.core.wmem_max = 16777216' >> /etc/sysctl.conf
   sysctl -p
   ```

---

## Monitoring Performance

### Key Metrics

1. **CPU:**
   - CPU utilization
   - CPU wait time
   - CPU steal time

2. **Memory:**
   - Memory usage
   - Swap usage
   - Memory pressure

3. **Storage:**
   - I/O wait
   - Disk utilization
   - I/O throughput

4. **Network:**
   - Network throughput
   - Packet loss
   - Latency

### Monitoring Tools

1. **Proxmox Built-in:**
   - Resource usage graphs
   - Performance metrics
   - Historical data

2. **External Tools:**
   - Prometheus + Grafana
   - Zabbix
   - Custom monitoring scripts

---

## Performance Benchmarks

### Baseline Measurements

**Before Optimization:**
- Document current performance
- Identify bottlenecks
- Set performance targets

**After Optimization:**
- Measure improvements
- Document results
- Adjust as needed

### Benchmark Tools

1. **CPU:**
   ```bash
   # CPU benchmark
   sysbench cpu --cpu-max-prime=20000 run
   ```

2. **Memory:**
   ```bash
   # Memory benchmark
   sysbench memory --memory-total-size=10G run
   ```

3. **Disk:**
   ```bash
   # Disk I/O benchmark
   fio --name=test --ioengine=libaio --iodepth=16 --rw=read --bs=4k --size=1G
   ```

---

## Troubleshooting Performance Issues

### Common Issues

1. **High CPU Usage:**
   - Check for runaway processes
   - Review CPU allocation
   - Consider CPU pinning

2. **High Memory Usage:**
   - Check for memory leaks
   - Review memory allocation
   - Enable ballooning

3. **High I/O Wait:**
   - Check disk I/O
   - Review storage configuration
   - Consider SSD storage

4. **Network Latency:**
   - Check network configuration
   - Review network drivers
   - Tune network settings

---

## Best Practices

1. **Regular Monitoring:**
   - Monitor performance metrics
   - Identify trends
   - Proactive optimization

2. **Baseline Measurements:**
   - Document baseline performance
   - Track changes over time
   - Set performance targets

3. **Gradual Optimization:**
   - Make one change at a time
   - Measure impact
   - Document results

4. **Resource Planning:**
   - Plan for growth
   - Monitor resource usage
   - Scale proactively

---

## Related Documentation

- **[MONITORING_SUMMARY.md](../08-monitoring/MONITORING_SUMMARY.md)** - Monitoring setup
- **[TROUBLESHOOTING_FAQ.md](/docs/09-troubleshooting/TROUBLESHOOTING_FAQ.md)** - Troubleshooting
- **[RECOMMENDATIONS_AND_SUGGESTIONS.md](RECOMMENDATIONS_AND_SUGGESTIONS.md)** - Best practices

---

**Last Updated:** 2025-01-20  
**Review Cycle:** Quarterly
