|
| 1 | +# Running Statistics Accuracy Tests |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This document explains how to run the statistics accuracy tests that have been added to the buddy allocator benchmark suite. |
| 6 | + |
| 7 | +## Test Location |
| 8 | + |
| 9 | +Tests are located in: |
| 10 | +- **Code**: `kernel/src/allocator_benchmark.rs` - `stats_accuracy` module |
| 11 | +- **Documentation**: `allocator/doc/STATS_ACCURACY_TEST.md` |
| 12 | + |
| 13 | +## Building the Tests |
| 14 | + |
| 15 | +### Prerequisites |
| 16 | +Ensure your development environment is properly set up: |
| 17 | +```bash |
| 18 | +rustup default nightly-2025-05-20 |
| 19 | +``` |
| 20 | + |
| 21 | +### Build Command |
| 22 | +```bash |
| 23 | +cargo xtask build |
| 24 | +``` |
| 25 | + |
| 26 | +Or specify a specific board configuration: |
| 27 | +```bash |
| 28 | +cargo xtask build --config=qemu-aarch64 |
| 29 | +``` |
| 30 | + |
| 31 | +## Running the Tests |
| 32 | + |
| 33 | +The statistics accuracy tests run automatically as part of the comprehensive benchmark suite when Axvisor boots: |
| 34 | + |
| 35 | +```bash |
| 36 | +# Run in QEMU |
| 37 | +cargo xtask run |
| 38 | + |
| 39 | +# Or with specific configuration |
| 40 | +cargo xtask run --config=qemu-aarch64 |
| 41 | +``` |
| 42 | + |
| 43 | +## Test Output |
| 44 | + |
| 45 | +When Axvisor starts, you will see the following output sections: |
| 46 | + |
| 47 | +### 1. Initial Allocator State |
| 48 | +``` |
| 49 | +分配器初始状态: |
| 50 | + 已用页面: 0 |
| 51 | + 可用页面: 6144 |
| 52 | + 已用字节: 0 |
| 53 | + 可用字节: 25165824 |
| 54 | +``` |
| 55 | + |
| 56 | +### 2. Statistics Accuracy Test Section |
| 57 | +``` |
| 58 | +═══════════════════════════════════════════════════════════ |
| 59 | +统计准确性测试 (Statistics Accuracy Tests) |
| 60 | +═══════════════════════════════════════════════════════════ |
| 61 | +
|
| 62 | +测试 1: 单线程统计准确性 (Single-threaded Stats Accuracy) |
| 63 | + 初始状态: |
| 64 | + 总页面: 6144 |
| 65 | + 空闲页面: 6144 |
| 66 | + 已用页面: 0 |
| 67 | + 验证统计一致性: |
| 68 | + ✓ 初始状态 统计一致性通过 |
| 69 | + ... |
| 70 | +✓ 统计准确性测试全部通过 |
| 71 | +``` |
| 72 | + |
| 73 | +## Test Details |
| 74 | + |
| 75 | +### Test 1: Single-threaded Statistics Accuracy |
| 76 | +- Performs 100 sequential allocations (1-16 pages each) |
| 77 | +- Verifies statistics after each allocation |
| 78 | +- Deallocates all and verifies recovery |
| 79 | +- **Expected**: Pass with small variance due to fragmentation |
| 80 | + |
| 81 | +### Test 2: Multi-threaded Statistics Accuracy |
| 82 | +- Simulates 4 concurrent threads |
| 83 | +- Each thread performs 50 allocations |
| 84 | +- Total: 200 allocations with varying sizes (1-128 pages) |
| 85 | +- Random-order deallocation to test buddy merging |
| 86 | +- **Expected**: Pass with variance allowed for fragmentation |
| 87 | + |
| 88 | +### Test 3: Fragmentation Statistics Accuracy |
| 89 | +- Creates fragmentation pattern: [1, 2, 1, 4, 1, 2, 8, 1, 16, 1] pages |
| 90 | +- Verifies statistics in fragmented state |
| 91 | +- Tests buddy merging by deallocating all |
| 92 | +- **Expected**: Pass, with accurate merging reflected in stats |
| 93 | + |
| 94 | +### Test 4: Statistics Stress Test |
| 95 | +- Runs 30 cycles of 40 allocations each |
| 96 | +- Verifies statistics after every allocation phase |
| 97 | +- Verifies statistics after every deallocation phase |
| 98 | +- **Expected**: Pass, consistent statistics throughout |
| 99 | + |
| 100 | +## Expected Results |
| 101 | + |
| 102 | +### All Tests Pass |
| 103 | +``` |
| 104 | +✓ 统计准确性测试全部通过 |
| 105 | +``` |
| 106 | + |
| 107 | +This indicates: |
| 108 | +- Statistics tracking is accurate |
| 109 | +- Free/used page counts are consistent |
| 110 | +- Buddy merging is correctly reflected |
| 111 | +- No memory leaks |
| 112 | + |
| 113 | +### Test Failures |
| 114 | + |
| 115 | +#### Inconsistency Detected |
| 116 | +``` |
| 117 | +✗ XXX 统计不一致: |
| 118 | + 已用页面: XXX (预期: XXX) |
| 119 | + 总页面: XXX, 空闲页面: XXX |
| 120 | +``` |
| 121 | +**Cause**: Bug in `update_stats()` or statistics calculation |
| 122 | +**Action**: Review allocator's stats update logic |
| 123 | + |
| 124 | +#### Free Pages Not Recovered |
| 125 | +``` |
| 126 | +⚠ 空闲页面未完全恢复: XXX (初始: XXX) |
| 127 | +``` |
| 128 | +**Cause**: Memory leak or deallocation bug |
| 129 | +**Action**: Review deallocation logic |
| 130 | + |
| 131 | +## Performance Impact |
| 132 | + |
| 133 | +The tests add minimal overhead: |
| 134 | +- No additional allocations (use existing allocator) |
| 135 | +- Statistics already collected by allocator (via `tracking` feature) |
| 136 | +- Tests only read and verify existing statistics |
| 137 | +- Time complexity: O(n) where n is number of operations |
| 138 | + |
| 139 | +## Troubleshooting |
| 140 | + |
| 141 | +### Build Failures |
| 142 | +``` |
| 143 | +error[E0425]: cannot find value `metrics` in this scope |
| 144 | +``` |
| 145 | +**Solution**: Ensure all test functions receive `metrics: &AllocatorMetrics` parameter |
| 146 | + |
| 147 | +### Link Errors |
| 148 | +``` |
| 149 | +error: linking with `cc` failed |
| 150 | +``` |
| 151 | +**Solution**: Check toolchain and ensure target is installed: |
| 152 | +```bash |
| 153 | +rustup target add aarch64-unknown-none-softfloat |
| 154 | +``` |
| 155 | + |
| 156 | +### Runtime Issues |
| 157 | +If QEMU fails to start: |
| 158 | +1. Check QEMU installation: `qemu-system-aarch64 --version` |
| 159 | +2. Ensure memory is sufficient for tests |
| 160 | +3. Try with smaller memory regions in configuration |
| 161 | + |
| 162 | +## Continuous Integration |
| 163 | + |
| 164 | +To run tests automatically in CI: |
| 165 | + |
| 166 | +```yaml |
| 167 | +# .github/workflows/test.yml |
| 168 | +- name: Run Statistics Accuracy Tests |
| 169 | + run: | |
| 170 | + cargo xtask build |
| 171 | + cargo xtask run --config=qemu-aarch64 & |
| 172 | + # Wait for test completion |
| 173 | + timeout 60 bash -c 'until grep "ALL TESTS PASSED" serial.out; do sleep 1; done' |
| 174 | +``` |
| 175 | +
|
| 176 | +## Customization |
| 177 | +
|
| 178 | +### Changing Test Parameters |
| 179 | +
|
| 180 | +In `kernel/src/allocator_benchmark.rs`, modify these values: |
| 181 | + |
| 182 | +```rust |
| 183 | +// Test 1: Change number of allocations |
| 184 | +let num_allocs = 100; // Increase to 1000 for more thorough test |
| 185 | +
|
| 186 | +// Test 2: Change concurrency |
| 187 | +let num_threads = 4; // Increase to 8 for more stress |
| 188 | +let allocs_per_thread = 50; // Increase to 100 |
| 189 | +
|
| 190 | +// Test 3: Change fragmentation pattern |
| 191 | +let fragmentation_pattern = [1, 2, 1, 4, 1, 2, 8, 1, 16, 1]; |
| 192 | +
|
| 193 | +// Test 4: Change stress cycles |
| 194 | +let num_cycles = 30; // Increase to 100 for longer test |
| 195 | +let ops_per_cycle = 40; // Increase to 100 for more operations |
| 196 | +``` |
| 197 | + |
| 198 | +### Adding New Tests |
| 199 | + |
| 200 | +To add a new test case: |
| 201 | + |
| 202 | +```rust |
| 203 | +fn test_your_scenario(metrics: &AllocatorMetrics) -> bool { |
| 204 | + info!("测试 X: Your Test Name"); |
| 205 | +
|
| 206 | + // Your test logic |
| 207 | + // ... |
| 208 | + // Use validate_stats_consistency() for validation |
| 209 | + validate_stats_consistency(total, free, used, "your stage") |
| 210 | +} |
| 211 | +
|
| 212 | +// Add to run_all(): |
| 213 | +pub fn run_all(metrics: &AllocatorMetrics) -> bool { |
| 214 | + let mut all_passed = true; |
| 215 | + all_passed &= test_single_thread_stats(metrics); |
| 216 | + all_passed &= test_your_scenario(metrics); // Add here |
| 217 | + // ... |
| 218 | +} |
| 219 | +``` |
| 220 | + |
| 221 | +## References |
| 222 | + |
| 223 | +- [Allocator Design](allocator/doc/MULTI_ZONE_DESIGN.md) |
| 224 | +- [Allocator Implementation](allocator/doc/MULTI_ZONE_IMPLEMENTATION.md) |
| 225 | +- [Statistics Analysis](allocator/src/buddy/stats.rs) |
0 commit comments