README.md 7.46 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# InfiniCore Memory Management Test Suite

This test suite provides comprehensive testing for the InfiniCore memory management system, focusing on the critical issues identified in the memory management architecture analysis.

## Overview

The test suite includes six main test categories:

1. **Basic Memory Tests** - Basic allocation, deallocation, and memory operations
2. **Concurrency Tests** - Thread safety and concurrent access testing
3. **Exception Safety Tests** - Exception handling and safety testing
4. **Memory Leak Tests** - Memory leak detection and prevention
5. **Performance Tests** - Performance benchmarks and optimization validation
6. **Stress Tests** - High-load stress testing and edge cases

## Building

### Using XMake (if integrated with main build)
```bash
# From InfiniCore root directory
xmake build infinicore-test
```

## Running Tests

### Run All Tests
```bash
./infinicore-test
```

### Run Specific Test Categories
```bash
# Basic memory tests
./infinicore-test --test basic

# Concurrency tests
./infinicore-test --test concurrency

# Exception safety tests
./infinicore-test --test exception

# Memory leak tests
./infinicore-test --test leak

# Performance tests
./infinicore-test --test performance

# Stress tests
./infinicore-test --test stress
```

### Run with Specific Device
```bash
# Run on CPU
./infinicore-test --cpu

# Run on NVIDIA GPU
./infinicore-test --nvidia

# Run on other devices
./infinicore-test --cambricon
./infinicore-test --ascend
./infinicore-test --metax
./infinicore-test --moore
./infinicore-test --iluvatar
66
./infinicore-test --qy
67
68
./infinicore-test --kunlun
./infinicore-test --hygon
wooway777's avatar
wooway777 committed
69
./infinicore-test --ali
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
```

### Customize Test Parameters
```bash
# Run with custom thread count
./infinicore-test --threads 8

# Run with custom iteration count
./infinicore-test --iterations 5000

# Combine options
./infinicore-test --nvidia --test concurrency --threads 16 --iterations 2000
```

## Test Categories

### 1. Basic Memory Tests
Tests fundamental memory operations:
- Memory allocation and deallocation
- Memory size and device properties
- Memory read/write operations
- Pinned memory allocation
- Memory data integrity

### 2. Concurrency Tests
Tests thread safety and concurrent access:
- **Concurrent Allocations**: Multiple threads allocating memory simultaneously
- **Concurrent Device Switching**: Multiple threads switching device contexts
- **Memory Allocation Race**: Race condition testing for memory operations

### 3. Exception Safety Tests
Tests exception handling and safety:
- **Allocation Failure**: Tests behavior when allocation fails
- **Deallocation Exception**: Tests exception safety during deallocation
- **Context Switch Exception**: Tests exception handling during device switching

### 4. Memory Leak Tests
Tests memory leak detection and prevention:
- **Basic Leak Detection**: Basic memory leak detection
- **Cross-Device Leak Detection**: Memory leaks in cross-device scenarios
- **Exception Leak Detection**: Memory leaks during exception handling

### 5. Performance Tests
Tests performance and benchmarks:
- **Allocation Performance**: Memory allocation speed benchmarks
- **Concurrent Performance**: Performance under concurrent load
- **Memory Copy Performance**: Memory copy bandwidth tests

### 6. Stress Tests
Tests high-load scenarios and edge cases:
- **High Frequency Allocations**: Rapid allocation/deallocation cycles
- **Large Memory Allocations**: Large memory block allocation
- **Cross-Device Stress**: Stress testing across multiple devices

## Expected Results

### Critical Issues to Watch For

The tests are designed to detect the critical issues identified in the memory management analysis:

1. **Thread Safety Violations**
   - Race conditions in concurrent allocations
   - Inconsistent device context switching
   - Global state corruption

2. **Memory Leaks**
   - Unfreed memory after deallocation
   - Cross-device memory not properly cleaned up
   - Exception-related memory leaks

3. **Exception Safety Issues**
   - Exceptions during allocation causing resource leaks
   - Exceptions in destructors causing `std::terminate`
   - Incomplete cleanup on exceptions

4. **Performance Issues**
   - Slow allocation/deallocation performance
   - Poor concurrent performance
   - Inefficient memory copy operations

### Performance Thresholds

The tests include performance thresholds:

- **Allocation Performance**: < 100μs per allocation
- **Concurrent Performance**: < 200μs per allocation under load
- **Memory Bandwidth**: > 100 MB/s for memory copies

## Test Output

### Successful Test Run
```
==============================================
InfiniCore Memory Management Test Suite
==============================================
Device: 0
Threads: 4
Iterations: 1000
==============================================

[SUITE] Running: BasicMemoryTest
[TEST] Starting: BasicMemoryTest
[TEST] PASSED: BasicMemoryTest (Duration: 1234μs)

[SUITE] Running: ConcurrencyTest
[TEST] Starting: ConcurrencyTest
[TEST] PASSED: ConcurrencyTest (Duration: 5678μs)

...

==============================================
Test Summary
==============================================
Total Tests: 6
Passed: 6
Failed: 0
Total Time: 12345μs
==============================================

✅ All tests passed!
```

### Failed Test Run
```
[TEST] FAILED: ConcurrencyTest - Concurrent allocation test failed: expected 8000 successes, got 7995 successes and 5 failures

==============================================
Final Results
==============================================
Total Tests: 6
Passed: 5
Failed: 1
==============================================

❌ Some tests failed. Please review the output above.
```

## Debugging Failed Tests

### Common Issues and Solutions

1. **Thread Safety Failures**
   - Check for race conditions in global state access
   - Verify proper synchronization in allocators
   - Review device context switching logic

2. **Memory Leak Failures**
   - Check deallocation logic in allocators
   - Verify cross-device cleanup mechanisms
   - Review exception safety in destructors

3. **Performance Failures**
   - Profile allocation/deallocation paths
   - Check for unnecessary context switching
   - Review memory copy implementations

4. **Exception Safety Failures**
   - Verify no-throw guarantees in destructors
   - Check exception handling in allocation paths
   - Review resource cleanup on exceptions

## Integration with CI/CD

### GitHub Actions Example
```yaml
- name: Run Memory Tests
  run: |
    cd src/infinicore-test
    mkdir build && cd build
    cmake ..
    make
    ./infinicore-test --test all
```

### Custom Test Targets
```bash
# Run specific test categories
make test-memory-basic
make test-memory-concurrency
make test-memory-exception
make test-memory-leak
make test-memory-performance
make test-memory-stress
make test-memory-all
```

## Contributing

When adding new tests:

1. Follow the existing test framework pattern
2. Add appropriate error messages and logging
3. Include performance thresholds where applicable
4. Test both success and failure scenarios
5. Update this README with new test descriptions

## Dependencies

- InfiniCore library (infinicore, infiniop, infinirt, infiniccl)
- C++17 compatible compiler
- Threading library (pthread on Linux)
- CMake 3.16+ (for CMake build)

## Notes

- Tests are designed to be deterministic where possible
- Some tests may have timing dependencies
- Performance tests may vary based on system load
- Memory leak detection is basic and may not catch all leaks
- Tests assume proper InfiniCore initialization