#!/bin/bash # analyze_freelist_sites.sh - Automated freelist site analysis set -e echo "========================================" echo "Atomic Freelist Site Analysis" echo "========================================" echo "" # Color codes RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color cd "$(dirname "$0")/.." echo "=== OVERALL STATISTICS ===" TOTAL=$(grep -rn "meta->freelist" core/ --include="*.c" --include="*.h" 2>/dev/null | wc -l) echo -e "${GREEN}Total freelist access sites: ${TOTAL}${NC}" READS=$(grep -rn "meta->freelist" core/ --include="*.c" --include="*.h" 2>/dev/null | grep -v "=" | wc -l) echo " Read operations (checks/loads): ${READS}" WRITES=$(grep -rn "meta->freelist.*=" core/ --include="*.c" --include="*.h" 2>/dev/null | grep -v "==" | grep -v "!=" | wc -l) echo " Write operations (assignments): ${WRITES}" echo "" echo "=== PHASE 1: CRITICAL HOT PATHS ===" echo "" echo -e "${YELLOW}File 1: core/tiny_superslab_alloc.inc.h${NC}" SITES=$(grep -n "meta->freelist" core/tiny_superslab_alloc.inc.h 2>/dev/null | wc -l) echo " Total sites: ${SITES}" grep -n "meta->freelist" core/tiny_superslab_alloc.inc.h 2>/dev/null | head -10 echo "" echo -e "${YELLOW}File 2: core/hakmem_tiny_refill_p0.inc.h${NC}" SITES=$(grep -n "meta->freelist" core/hakmem_tiny_refill_p0.inc.h 2>/dev/null | wc -l) echo " Total sites: ${SITES}" grep -n "meta->freelist" core/hakmem_tiny_refill_p0.inc.h 2>/dev/null | head -10 echo "" echo -e "${YELLOW}File 3: core/box/carve_push_box.c${NC}" SITES=$(grep -n "meta->freelist" core/box/carve_push_box.c 2>/dev/null | wc -l) echo " Total sites: ${SITES}" grep -n "meta->freelist" core/box/carve_push_box.c 2>/dev/null | head -10 echo "" echo -e "${YELLOW}File 4: core/hakmem_tiny_tls_ops.h${NC}" SITES=$(grep -n "meta->freelist" core/hakmem_tiny_tls_ops.h 2>/dev/null | wc -l) echo " Total sites: ${SITES}" grep -n "meta->freelist" core/hakmem_tiny_tls_ops.h 2>/dev/null | head -10 echo "" echo "=== PHASE 2: IMPORTANT PATHS ===" echo "" echo -e "${YELLOW}File 5: core/tiny_refill_opt.h${NC}" SITES=$(grep -n "meta->freelist" core/tiny_refill_opt.h 2>/dev/null | wc -l) echo " Total sites: ${SITES}" echo -e "${YELLOW}File 6: core/tiny_free_magazine.inc.h${NC}" SITES=$(grep -n "meta->freelist" core/tiny_free_magazine.inc.h 2>/dev/null | wc -l) echo " Total sites: ${SITES}" echo -e "${YELLOW}File 7: core/refill/ss_refill_fc.h${NC}" SITES=$(grep -n "meta->freelist" core/refill/ss_refill_fc.h 2>/dev/null | wc -l) echo " Total sites: ${SITES}" echo -e "${YELLOW}File 8: core/slab_handle.h${NC}" SITES=$(grep -n "meta->freelist" core/slab_handle.h 2>/dev/null | wc -l) echo " Total sites: ${SITES}" echo "" echo "=== PHASE 3: DEBUG/STATS (SKIP CONVERSION) ===" echo "" echo -e "${YELLOW}File 9: core/box/ss_stats_box.c${NC}" SITES=$(grep -n "meta->freelist" core/box/ss_stats_box.c 2>/dev/null | wc -l) echo " Total sites: ${SITES} (debug only)" echo -e "${YELLOW}File 10: core/tiny_debug.h${NC}" SITES=$(grep -n "meta->freelist" core/tiny_debug.h 2>/dev/null | wc -l) echo " Total sites: ${SITES} (debug only)" echo "" echo "=== OPERATION BREAKDOWN ===" echo "" POP_PATTERN=$(grep -B1 -A1 "meta->freelist.*tiny_next_read\|tiny_next_read.*meta->freelist" core/ -r --include="*.c" --include="*.h" 2>/dev/null | grep -c "meta->freelist" || true) echo " POP operations (load + next): ${POP_PATTERN}" PUSH_PATTERN=$(grep -B1 "meta->freelist = " core/ -r --include="*.c" --include="*.h" 2>/dev/null | grep -c "tiny_next_write" || true) echo " PUSH operations (write + assign): ${PUSH_PATTERN}" NULL_CHECKS=$(grep -rn "meta->freelist" core/ --include="*.c" --include="*.h" 2>/dev/null | grep -E "if.*freelist|while.*freelist" | wc -l) echo " NULL checks (if/while conditions): ${NULL_CHECKS}" DIRECT_ASSIGN=$(grep -rn "meta->freelist.*=" core/ --include="*.c" --include="*.h" 2>/dev/null | grep -v "==" | grep -v "!=" | wc -l) echo " Direct assignments (store): ${DIRECT_ASSIGN}" echo "" echo "=== FILES WITH FREELIST USAGE ===" echo "" grep -rl "meta->freelist" core/ --include="*.c" --include="*.h" 2>/dev/null | sort | nl echo "" echo "=== CONVERSION ESTIMATES ===" echo "" PHASE1_FILES=5 PHASE1_SITES=25 PHASE2_FILES=10 PHASE2_SITES=40 PHASE3_FILES=5 PHASE3_SITES=25 echo "Phase 1 (Critical Hot Paths):" echo " Files: ${PHASE1_FILES}" echo " Sites: ${PHASE1_SITES}" echo " Time: 2-3 hours" echo "" echo "Phase 2 (Important Paths):" echo " Files: ${PHASE2_FILES}" echo " Sites: ${PHASE2_SITES}" echo " Time: 2-3 hours" echo "" echo "Phase 3 (Cleanup):" echo " Files: ${PHASE3_FILES}" echo " Sites: ${PHASE3_SITES}" echo " Time: 1-2 hours" echo "" TOTAL_EFFORT="5-8 hours" echo -e "${GREEN}Total Estimated Effort: ${TOTAL_EFFORT}${NC}" echo "" echo "=== LOCK-PROTECTED SITES CHECK ===" echo "" LOCK_PROTECTED=$(grep -B10 "meta->freelist" core/ -r --include="*.c" --include="*.h" 2>/dev/null | grep -c "pthread_mutex\|mutex_lock" || true) echo " Sites potentially protected by locks: ${LOCK_PROTECTED}" if [ ${LOCK_PROTECTED} -gt 0 ]; then echo -e " ${YELLOW}Review these sites - may not need atomic conversion${NC}" fi echo "" echo "=== EXISTING ATOMIC PATTERNS (for reference) ===" echo "" EXISTING_ATOMIC=$(grep -rn "atomic_load\|atomic_store\|atomic_compare_exchange" core/ --include="*.c" --include="*.h" 2>/dev/null | wc -l) echo " Existing atomic operations in codebase: ${EXISTING_ATOMIC}" echo " (can use as reference for memory ordering)" echo "" echo "=== NEXT STEPS ===" echo "" echo "1. Review ATOMIC_FREELIST_IMPLEMENTATION_STRATEGY.md" echo "2. Review ATOMIC_FREELIST_SITE_BY_SITE_GUIDE.md" echo "3. Create core/box/slab_freelist_atomic.h (30 min)" echo "4. Start Phase 1 conversion (5 files, 2-3 hours)" echo "5. Test with: ./out/release/larson_hakmem 8 100000 256" echo "" echo "========================================" echo "Analysis Complete" echo "========================================"