#!/bin/bash # audit_atomics.sh - Comprehensive atomic operation audit # Purpose: Find and classify all atomic operations in hot/warm/cold paths # Output: JSON-formatted audit report for Phase 26+ planning set -euo pipefail CORE_DIR="/mnt/workdisk/public_share/hakmem/core" OUTPUT_FILE="/mnt/workdisk/public_share/hakmem/docs/analysis/ATOMIC_AUDIT_FULL.txt" echo "=== HAKMEM Atomic Operations Audit ===" > "$OUTPUT_FILE" echo "Date: $(date)" >> "$OUTPUT_FILE" echo "Purpose: Identify telemetry-only atomics for compile-out (Phase 26+)" >> "$OUTPUT_FILE" echo "" >> "$OUTPUT_FILE" # Find all atomic_fetch_add/sub operations echo "## Part 1: atomic_fetch_add/sub operations" >> "$OUTPUT_FILE" echo "" >> "$OUTPUT_FILE" rg -n "atomic_fetch_(add|sub)_explicit\(" "$CORE_DIR/" --no-heading | \ while IFS=: read -r file line code; do echo "FILE: $file" >> "$OUTPUT_FILE" echo "LINE: $line" >> "$OUTPUT_FILE" echo "CODE: $code" >> "$OUTPUT_FILE" # Extract variable name var=$(echo "$code" | grep -oP '&\K[a-zA-Z_][a-zA-Z0-9_]*(?=\s*,)' || echo "UNKNOWN") echo "VAR: $var" >> "$OUTPUT_FILE" # Classify based on variable naming patterns if echo "$var" | grep -qE '(stats|count|trace|debug|diag|log|metric|observe|enter|exit|hit|miss|attempt|success)'; then echo "CLASS: TELEMETRY (candidate for compile-out)" >> "$OUTPUT_FILE" elif echo "$var" | grep -qE '(remote|refcount|owner|lock|head|tail|used|active|in_use)'; then echo "CLASS: CORRECTNESS (do not touch)" >> "$OUTPUT_FILE" else echo "CLASS: UNKNOWN (manual review needed)" >> "$OUTPUT_FILE" fi # Determine path type based on file if echo "$file" | grep -qE '(alloc_fast|free_fast|malloc_tiny_fast)'; then echo "PATH: HOT (highest priority)" >> "$OUTPUT_FILE" elif echo "$file" | grep -qE '(superslab_free|hakmem_tiny_free|tiny_alloc)'; then echo "PATH: HOT (high priority)" >> "$OUTPUT_FILE" elif echo "$file" | grep -qE '(refill|spill|magazine)'; then echo "PATH: WARM (medium priority)" >> "$OUTPUT_FILE" else echo "PATH: COLD (low priority)" >> "$OUTPUT_FILE" fi echo "---" >> "$OUTPUT_FILE" done echo "" >> "$OUTPUT_FILE" echo "## Part 2: Summary by Classification" >> "$OUTPUT_FILE" echo "" >> "$OUTPUT_FILE" # Count telemetry atomics TELEMETRY_COUNT=$(grep -c "CLASS: TELEMETRY" "$OUTPUT_FILE" || true) CORRECTNESS_COUNT=$(grep -c "CLASS: CORRECTNESS" "$OUTPUT_FILE" || true) UNKNOWN_COUNT=$(grep -c "CLASS: UNKNOWN" "$OUTPUT_FILE" || true) echo "Total TELEMETRY atomics: $TELEMETRY_COUNT" >> "$OUTPUT_FILE" echo "Total CORRECTNESS atomics: $CORRECTNESS_COUNT" >> "$OUTPUT_FILE" echo "Total UNKNOWN atomics: $UNKNOWN_COUNT" >> "$OUTPUT_FILE" echo "" >> "$OUTPUT_FILE" # Count by path HOT_COUNT=$(grep -c "PATH: HOT" "$OUTPUT_FILE" || true) WARM_COUNT=$(grep -c "PATH: WARM" "$OUTPUT_FILE" || true) COLD_COUNT=$(grep -c "PATH: COLD" "$OUTPUT_FILE" || true) echo "Hot path atomics: $HOT_COUNT" >> "$OUTPUT_FILE" echo "Warm path atomics: $WARM_COUNT" >> "$OUTPUT_FILE" echo "Cold path atomics: $COLD_COUNT" >> "$OUTPUT_FILE" echo "" >> "$OUTPUT_FILE" echo "Audit complete. Review $OUTPUT_FILE for details." >> "$OUTPUT_FILE" cat "$OUTPUT_FILE"