Skip to content

๐Ÿ“Š Performance Tracking (08_02_04)

Real-time performance monitoring and validation for audio processing.

๐Ÿ“ฆ Components

1. CPUMonitor

Tracks CPU usage in real-time audio thread.

CPUMonitor monitor;

void processBlock(AudioBuffer& buffer) {
    CPUScope scope(monitor, buffer.getNumSamples(), sampleRate);

    // ... audio processing ...

}  // CPU usage calculated automatically

float usage = monitor.getCurrentCPU();  // Current block %
float average = monitor.getAverageCPU();  // Moving average
float peak = monitor.getPeakCPU();  // Peak usage

if (monitor.isOverloaded()) {
    // CPU > 90% - take action
}

2. RTSafetyValidator

Detects real-time violations (allocations, locks, etc.)

void processBlock(AudioBuffer& buffer) {
    RTSafetyValidator validator;
    RTSafetyScope scope(validator);

    // Processing code - violations detected

    if (validator.hasViolations()) {
        auto violations = validator.getViolations();
        // Log or report violations
    }
}

3. PerformanceBudget

Tracks CPU budget per processing stage.

PerformanceBudget budget(0.8f);  // 80% CPU budget

budget.beginStage("routing");
// ... routing code ...
budget.endStage();

budget.beginStage("modulation");
// ... modulation code ...
budget.endStage();

// Check budget
if (budget.isOverBudget()) {
    auto report = budget.getReport();
    // Find bottleneck: report.stages[...]
}

4. ProfileScope

RAII profiling scope.

void myFunction() {
    ProfileScope profile("myFunction");

    // ... code to profile ...

}  // Time automatically recorded

ProfileScope::printStats();  // Print all profiled functions

๐Ÿš€ Features

  • Zero-overhead in release: All tracking compiled out
  • Real-time safe: No allocations in monitoring code
  • Thread-local: Per-thread tracking
  • Minimal impact: < 0.1% CPU overhead

๐Ÿ“Š Example Output

=== CPU Monitor ===
Current: 45.2%
Average: 42.8%
Peak: 67.3%
Status: OK

=== Performance Budget ===
Total CPU: 68.5% / 80.0% budget
Stages:
  routing:     12.3% (18% of budget)
  modulation:  15.7% (23% of budget)
  dsp:         40.5% (59% of budget) โš ๏ธ HEAVY

=== RT Safety ===
Violations: 0
Status: SAFE โœ“

๐Ÿงช Integration Test

#include "CPUMonitor.hpp"
#include "RTSafetyValidator.hpp"

void testAudioProcessing() {
    CPUMonitor cpuMonitor;
    RTSafetyValidator rtValidator;

    constexpr int numBlocks = 1000;
    constexpr int blockSize = 512;
    constexpr double sampleRate = 44100.0;

    for (int i = 0; i < numBlocks; ++i) {
        CPUScope cpuScope(cpuMonitor, blockSize, sampleRate);
        RTSafetyScope rtScope(rtValidator);

        // Process audio
        processBlock(buffer);

        // Check for issues
        if (cpuMonitor.getCurrentCPU() > 90.0f) {
            std::cerr << "CPU overload detected!" << std::endl;
        }

        if (rtValidator.hasViolations()) {
            std::cerr << "RT violation detected!" << std::endl;
        }
    }

    // Final stats
    std::cout << "Average CPU: " << cpuMonitor.getAverageCPU() << "%" << std::endl;
    std::cout << "Peak CPU: " << cpuMonitor.getPeakCPU() << "%" << std::endl;
    std::cout << "RT Violations: " << rtValidator.getViolationCount() << std::endl;
}

Status: โœ… Complete Version: 1.0.0 Last Updated: 2025-10-09