How to view slab/cache → slabtop
How to View Slab Cache with slabtop: A Complete System Administrator's Guide
Table of Contents
1. [Introduction](#introduction)
2. [Prerequisites](#prerequisites)
3. [Understanding Slab Cache](#understanding-slab-cache)
4. [Installing slabtop](#installing-slabtop)
5. [Basic slabtop Usage](#basic-slabtop-usage)
6. [Command Options and Parameters](#command-options-and-parameters)
7. [Interpreting slabtop Output](#interpreting-slabtop-output)
8. [Practical Examples and Use Cases](#practical-examples-and-use-cases)
9. [Advanced Monitoring Techniques](#advanced-monitoring-techniques)
10. [Troubleshooting Common Issues](#troubleshooting-common-issues)
11. [Best Practices](#best-practices)
12. [Alternative Tools and Methods](#alternative-tools-and-methods)
13. [Conclusion](#conclusion)
Introduction
The Linux kernel's slab cache is a crucial memory management mechanism that optimizes the allocation and deallocation of frequently used kernel objects. Understanding and monitoring slab cache usage is essential for system administrators, developers, and performance analysts who need to diagnose memory-related issues, optimize system performance, and troubleshoot kernel memory leaks.
The `slabtop` command provides a real-time, top-like interface for viewing kernel slab cache information, making it an invaluable tool for system monitoring and debugging. This comprehensive guide will walk you through everything you need to know about using `slabtop` effectively, from basic usage to advanced troubleshooting techniques.
By the end of this article, you'll understand how to install, configure, and use `slabtop` to monitor your system's slab cache, interpret the output data, and identify potential memory management issues in your Linux environment.
Prerequisites
Before diving into `slabtop`, ensure you have the following:
System Requirements
- Linux operating system (kernel 2.6 or later)
- Root or sudo privileges for installation and certain monitoring tasks
- Basic understanding of Linux command line interface
- Familiarity with system administration concepts
Knowledge Prerequisites
- Understanding of Linux memory management concepts
- Basic knowledge of kernel operations
- Familiarity with process monitoring tools like `top` and `htop`
Hardware Requirements
- Sufficient terminal access (local or SSH)
- No specific hardware requirements beyond a functioning Linux system
Understanding Slab Cache
What is Slab Cache?
The slab cache is a memory management mechanism in the Linux kernel that maintains pools of pre-allocated memory objects. Instead of repeatedly allocating and deallocating memory for commonly used kernel data structures, the slab allocator maintains caches of these objects, significantly improving performance and reducing memory fragmentation.
How Slab Cache Works
The slab allocator organizes memory into three main components:
1. Caches: Collections of one or more slabs containing objects of the same type
2. Slabs: Contiguous memory areas containing pre-allocated objects
3. Objects: Individual memory chunks of specific sizes for kernel data structures
Common Slab Cache Objects
Typical objects stored in slab caches include:
- File system inodes
- Directory entries (dentries)
- Network socket structures
- Process descriptors
- Buffer heads
- Memory mapping structures
Installing slabtop
Ubuntu/Debian Systems
On Ubuntu and Debian-based distributions, `slabtop` is part of the `procps` package:
```bash
Update package lists
sudo apt update
Install procps package (if not already installed)
sudo apt install procps
Verify installation
which slabtop
```
Red Hat/CentOS/Fedora Systems
For Red Hat-based distributions:
```bash
For RHEL/CentOS 7 and earlier
sudo yum install procps-ng
For RHEL/CentOS 8+ and Fedora
sudo dnf install procps-ng
Verify installation
slabtop --version
```
SUSE/openSUSE Systems
```bash
Install procps package
sudo zypper install procps
Verify installation
slabtop --help
```
Arch Linux
```bash
Install procps-ng package
sudo pacman -S procps-ng
Verify installation
slabtop --version
```
Building from Source
If you need to compile from source:
```bash
Download procps source
wget https://sourceforge.net/projects/procps-ng/files/Production/procps-ng-3.3.17.tar.xz
Extract and compile
tar -xf procps-ng-3.3.17.tar.xz
cd procps-ng-3.3.17
./configure
make
sudo make install
```
Basic slabtop Usage
Running slabtop
The simplest way to run `slabtop` is without any arguments:
```bash
slabtop
```
This command displays a real-time view of slab cache statistics, similar to the `top` command for processes.
Basic Navigation
Once `slabtop` is running, you can use these keyboard shortcuts:
- q: Quit the program
- Space: Refresh the display immediately
- s: Change the refresh interval
- c: Sort by cache size
- l: Sort by number of slabs
- o: Sort by number of objects
- u: Sort by cache utilization
Understanding the Default Display
The default `slabtop` output shows:
- Cache name
- Number of objects
- Object size
- Cache size
- Slab count
- Objects per slab
Command Options and Parameters
Common Command Line Options
Display Options
```bash
Show help information
slabtop --help
Display version information
slabtop --version
Run once and exit (no continuous refresh)
slabtop --once
Set delay between updates (in seconds)
slabtop --delay=2
Sort by specific column
slabtop --sort=c # Sort by cache size
slabtop --sort=l # Sort by number of slabs
slabtop --sort=o # Sort by number of objects
```
Output Formatting
```bash
Display only active caches
slabtop --active
Show specific number of lines
slabtop --lines=20
Use alternative /proc/slabinfo path
slabtop --slabinfo=/custom/path/slabinfo
```
Advanced Usage Examples
Continuous Monitoring with Custom Interval
```bash
Update every 5 seconds
slabtop --delay=5
Monitor once per minute
slabtop --delay=60
```
Sorting and Filtering
```bash
Sort by cache utilization and run once
slabtop --sort=u --once
Show only first 10 entries
slabtop --sort=c --once | head -10
```
Interpreting slabtop Output
Column Meanings
Understanding each column in the `slabtop` output is crucial for effective monitoring:
OBJS (Objects)
- Active: Number of objects currently in use
- Total: Total number of objects allocated in the cache
- Ratio: Active/Total ratio indicating cache efficiency
SIZE (Object Size)
- Size of individual objects in bytes
- Helps identify memory usage patterns
SLABS
- Number of slabs allocated for this cache
- Each slab contains multiple objects
SOBJ (Objects per Slab)
- Average number of objects that fit in each slab
- Indicates packing efficiency
CACHE SIZE
- Total memory consumed by the cache
- Calculated as: (number of slabs) × (slab size)
NAME
- Kernel cache identifier
- Describes the type of objects stored
Sample Output Analysis
```
Active / Total Objects (% used) : 1547325 / 1684742 (91.8%)
Active / Total Slabs (% used) : 75234 / 75234 (100.0%)
Active / Total Caches (% used) : 96 / 147 (65.3%)
Active / Total Size (% used) : 284.44M / 301.23M (94.4%)
Minimum / Average / Maximum Object : 0.01K / 0.19K / 8.00K
OBJS ACTIVE USE OBJ SIZE SLABS OBJ/SLAB CACHE SIZE NAME
524288 524288 100% 0.06K 8192 64 32768K anon_vma_chain
245760 245760 100% 0.19K 5856 42 46848K dentry
89600 89600 100% 0.55K 3200 28 49280K inode_cache
```
Key Metrics to Monitor
Memory Utilization
- High utilization (>90%) may indicate memory pressure
- Low utilization (<50%) might suggest over-allocation
Object Efficiency
- Active/Total object ratio shows cache effectiveness
- Consistently low ratios may indicate tuning opportunities
Cache Growth Patterns
- Rapidly growing caches may indicate memory leaks
- Stable growth patterns are typically normal
Practical Examples and Use Cases
Example 1: Identifying Memory-Heavy Caches
To find caches consuming the most memory:
```bash
Sort by cache size and show top 10
slabtop --sort=c --once | head -20
```
This helps identify which kernel subsystems are using the most memory.
Example 2: Monitoring Inode Cache Usage
File system performance often relates to inode cache efficiency:
```bash
Monitor inode-related caches
slabtop --once | grep -i inode
```
Look for:
- `inode_cache`: File system inodes
- `proc_inode_cache`: /proc filesystem inodes
- `sock_inode_cache`: Socket inodes
Example 3: Detecting Potential Memory Leaks
Monitor cache growth over time:
```bash
Create a monitoring script
#!/bin/bash
while true; do
echo "=== $(date) ==="
slabtop --sort=c --once | head -10
sleep 300 # Check every 5 minutes
done
```
Example 4: Network-Related Cache Monitoring
For network-intensive systems:
```bash
Monitor network-related caches
slabtop --once | grep -E "(tcp|udp|sock|skbuff)"
```
Common network caches include:
- `tcp_bind_bucket`
- `UDP-Lite`
- `sock_inode_cache`
- `skbuff_head_cache`
Example 5: File System Cache Analysis
Monitor file system caches:
```bash
Monitor file system caches
slabtop --once | grep -E "(dentry|buffer|ext4)"
```
Key file system caches:
- `dentry`: Directory entries
- `buffer_head`: File system buffers
- `ext4_inode_cache`: ext4 filesystem inodes
Advanced Monitoring Techniques
Combining slabtop with Other Tools
Using with vmstat
```bash
Monitor overall memory statistics alongside slab info
vmstat 5 & slabtop --delay=5
```
Integration with sar
```bash
Collect slab info with system activity reporter
sar -r 5 10 & slabtop --delay=5 --once
```
Automated Monitoring Scripts
Basic Monitoring Script
```bash
#!/bin/bash
slab_monitor.sh - Basic slab cache monitoring
LOG_FILE="/var/log/slab_monitor.log"
THRESHOLD_MB=100
while true; do
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
# Get top 5 caches by size
TOP_CACHES=$(slabtop --sort=c --once | head -10 | tail -5)
echo "[$TIMESTAMP] Top slab caches:" >> $LOG_FILE
echo "$TOP_CACHES" >> $LOG_FILE
echo "" >> $LOG_FILE
# Check for large caches
slabtop --sort=c --once | awk -v threshold=$THRESHOLD_MB '
NR > 7 {
if ($7 ~ /K$/) {
size = substr($7, 1, length($7)-1) / 1024
} else if ($7 ~ /M$/) {
size = substr($7, 1, length($7)-1)
}
if (size > threshold) {
print "WARNING: Large cache detected - " $8 " (" $7 ")"
}
}' >> $LOG_FILE
sleep 600 # Check every 10 minutes
done
```
Advanced Analysis Script
```bash
#!/bin/bash
slab_analyzer.sh - Advanced slab cache analysis
REPORT_FILE="/tmp/slab_analysis_$(date +%Y%m%d_%H%M%S).txt"
echo "Slab Cache Analysis Report" > $REPORT_FILE
echo "Generated: $(date)" >> $REPORT_FILE
echo "=================================" >> $REPORT_FILE
Overall statistics
echo "" >> $REPORT_FILE
echo "OVERALL STATISTICS:" >> $REPORT_FILE
slabtop --once | head -6 >> $REPORT_FILE
Top 10 caches by size
echo "" >> $REPORT_FILE
echo "TOP 10 CACHES BY SIZE:" >> $REPORT_FILE
slabtop --sort=c --once | head -17 | tail -10 >> $REPORT_FILE
Efficiency analysis
echo "" >> $REPORT_FILE
echo "CACHE EFFICIENCY ANALYSIS:" >> $REPORT_FILE
slabtop --once | awk '
NR > 7 {
if (NF >= 3 && $3 ~ /%/) {
usage = substr($3, 1, length($3)-1)
if (usage < 50) {
print "Low efficiency: " $8 " (" $3 " utilization)"
}
}
}' >> $REPORT_FILE
echo "Report saved to: $REPORT_FILE"
```
Performance Tuning Based on slabtop Data
Identifying Tuning Opportunities
1. Low Cache Utilization: Caches with consistently low usage might benefit from size reduction
2. High Fragmentation: Many small slabs might indicate fragmentation issues
3. Rapid Growth: Quickly growing caches might need size limits or investigation
Kernel Parameter Tuning
Based on slabtop observations, you might adjust:
```bash
Adjust slab cache behavior
echo 1 > /proc/sys/vm/slab_reclaim_ratio
Modify memory reclaim behavior
echo 60 > /proc/sys/vm/swappiness
```
Troubleshooting Common Issues
Issue 1: slabtop Command Not Found
Symptoms:
- `bash: slabtop: command not found`
- Command execution fails
Solutions:
```bash
Check if procps is installed
dpkg -l | grep procps # Debian/Ubuntu
rpm -qa | grep procps # Red Hat/CentOS
Install missing package
sudo apt install procps # Debian/Ubuntu
sudo yum install procps-ng # Red Hat/CentOS 7
sudo dnf install procps-ng # Red Hat/CentOS 8+/Fedora
```
Issue 2: Permission Denied Errors
Symptoms:
- `slabtop: cannot open /proc/slabinfo`
- Access denied messages
Solutions:
```bash
Check file permissions
ls -la /proc/slabinfo
Run with appropriate privileges
sudo slabtop
Check if /proc is mounted
mount | grep proc
```
Issue 3: Incomplete or Garbled Output
Symptoms:
- Missing columns
- Truncated display
- Formatting issues
Solutions:
```bash
Increase terminal width
export COLUMNS=120
slabtop
Use alternative formatting
slabtop --once | less
Check terminal capabilities
echo $TERM
```
Issue 4: High Memory Usage Without Clear Cause
Symptoms:
- Unexpectedly high slab cache usage
- System performance degradation
- Memory pressure warnings
Investigation Steps:
```bash
Identify largest caches
slabtop --sort=c --once | head -20
Check for memory leaks
echo 2 > /proc/sys/vm/drop_caches
sleep 5
slabtop --sort=c --once | head -10
Monitor growth over time
watch -n 30 'slabtop --sort=c --once | head -10'
```
Issue 5: Kernel Version Compatibility
Symptoms:
- Unexpected output format
- Missing information
- Feature unavailability
Solutions:
```bash
Check kernel version
uname -r
Verify /proc/slabinfo format
cat /proc/slabinfo | head -5
Use kernel-appropriate options
Older kernels may have different column layouts
```
Best Practices
Monitoring Best Practices
1. Regular Monitoring Schedule
```bash
Set up cron job for regular monitoring
Add to crontab: 0 /6 /usr/local/bin/slab_monitor.sh
```
2. Baseline Establishment
Create baseline measurements during normal operation:
```bash
Capture baseline during low activity
slabtop --once > /tmp/slab_baseline.txt
Compare current state to baseline
diff /tmp/slab_baseline.txt <(slabtop --once)
```
3. Alert Thresholds
Establish meaningful alert thresholds:
- Memory Usage: Alert when total slab usage exceeds 80% of available memory
- Cache Growth: Alert on >50% cache size increase within an hour
- Utilization: Alert on consistently low utilization (<30%) for large caches
Performance Optimization
1. Cache-Specific Tuning
```bash
For web servers, monitor dentry cache
watch -n 10 'slabtop --once | grep dentry'
For database servers, focus on buffer caches
slabtop --once | grep -E "(buffer|page)"
```
2. Memory Pressure Management
```bash
Monitor memory pressure indicators
cat /proc/meminfo | grep -E "(Slab|SReclaimable|SUnreclaim)"
Force slab cache cleanup if needed (use carefully)
echo 2 > /proc/sys/vm/drop_caches
```
Security Considerations
1. Access Control
- Limit `slabtop` access to authorized personnel
- Use sudo rules for controlled access:
```bash
/etc/sudoers.d/slabtop
%monitoring ALL=(root) NOPASSWD: /usr/bin/slabtop
```
2. Log Management
```bash
Secure log files
chmod 640 /var/log/slab_monitor.log
chown root:adm /var/log/slab_monitor.log
Implement log rotation
/etc/logrotate.d/slab_monitor
/var/log/slab_monitor.log {
daily
missingok
rotate 30
compress
create 640 root adm
}
```
Alternative Tools and Methods
Direct /proc/slabinfo Access
```bash
View raw slab information
cat /proc/slabinfo
Monitor specific cache
watch -n 5 'grep dentry /proc/slabinfo'
Parse with awk for custom analysis
awk '/^[^#]/ {print $1, $3, $4}' /proc/slabinfo
```
Using /sys/kernel/slab
```bash
Explore individual cache directories
ls /sys/kernel/slab/
Get detailed cache information
cat /sys/kernel/slab/dentry/aliases
cat /sys/kernel/slab/dentry/cache_dma
cat /sys/kernel/slab/dentry/cpu_slabs
```
Integration with Monitoring Systems
Nagios Plugin Example
```bash
#!/bin/bash
check_slab.sh - Nagios plugin for slab monitoring
WARNING_THRESHOLD=80
CRITICAL_THRESHOLD=90
TOTAL_SLAB=$(awk '/^Active.*Size/ {gsub(/[^0-9.]/, "", $6); print $6}' <(slabtop --once))
TOTAL_MEM=$(awk '/MemTotal/ {print $2/1024}' /proc/meminfo)
USAGE_PERCENT=$(echo "scale=2; $TOTAL_SLAB / $TOTAL_MEM * 100" | bc)
if (( $(echo "$USAGE_PERCENT > $CRITICAL_THRESHOLD" | bc -l) )); then
echo "CRITICAL: Slab usage ${USAGE_PERCENT}% exceeds ${CRITICAL_THRESHOLD}%"
exit 2
elif (( $(echo "$USAGE_PERCENT > $WARNING_THRESHOLD" | bc -l) )); then
echo "WARNING: Slab usage ${USAGE_PERCENT}% exceeds ${WARNING_THRESHOLD}%"
exit 1
else
echo "OK: Slab usage ${USAGE_PERCENT}% is normal"
exit 0
fi
```
Prometheus Metrics Collection
```bash
#!/bin/bash
slab_metrics.sh - Collect slab metrics for Prometheus
METRICS_FILE="/var/lib/node_exporter/textfile_collector/slab_metrics.prom"
{
echo "# HELP slab_cache_size_bytes Size of slab cache in bytes"
echo "# TYPE slab_cache_size_bytes gauge"
slabtop --once | awk '
NR > 7 && NF >= 8 {
cache_name = $8
size_str = $7
# Convert size to bytes
if (size_str ~ /K$/) {
size_bytes = substr(size_str, 1, length(size_str)-1) * 1024
} else if (size_str ~ /M$/) {
size_bytes = substr(size_str, 1, length(size_str)-1) 1024 1024
} else {
size_bytes = size_str
}
print "slab_cache_size_bytes{cache=\"" cache_name "\"} " size_bytes
}'
} > $METRICS_FILE
```
Conclusion
The `slabtop` command is an essential tool for Linux system administrators and performance analysts who need to understand and monitor kernel memory usage. Through this comprehensive guide, we've explored everything from basic installation and usage to advanced monitoring techniques and troubleshooting strategies.
Key Takeaways
1. Installation: `slabtop` is part of the procps package and is available on all major Linux distributions
2. Basic Usage: The command provides a real-time, top-like interface for viewing slab cache statistics
3. Interpretation: Understanding column meanings and metrics is crucial for effective monitoring
4. Practical Applications: Regular monitoring helps identify memory leaks, optimize performance, and troubleshoot issues
5. Advanced Techniques: Automation scripts and integration with monitoring systems enhance operational capabilities
Next Steps
To further enhance your system monitoring capabilities:
1. Implement Regular Monitoring: Set up automated monitoring scripts and establish baseline metrics
2. Integrate with Existing Tools: Combine `slabtop` with your current monitoring infrastructure
3. Develop Custom Solutions: Create tailored analysis scripts for your specific environment
4. Stay Updated: Keep your procps package updated to access the latest features and improvements
Best Practices Summary
- Monitor slab cache usage regularly as part of your system maintenance routine
- Establish baselines and alert thresholds appropriate for your environment
- Combine `slabtop` with other memory monitoring tools for comprehensive analysis
- Document your findings and create runbooks for common issues
- Train your team on proper interpretation and response procedures
By mastering `slabtop` and implementing the techniques described in this guide, you'll be well-equipped to maintain optimal system performance and quickly identify memory-related issues in your Linux environment. Remember that effective system monitoring is an ongoing process that requires consistent attention and continuous improvement of your monitoring strategies.
The knowledge gained from this guide provides a solid foundation for advanced Linux system administration and performance optimization. Continue to practice these techniques and adapt them to your specific use cases for maximum effectiveness.