optimize_performance.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/bin/bash
  2. set -e
  3. # Function to log with timestamp
  4. log() {
  5. echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1"
  6. }
  7. # Function to safely set sysctl value
  8. safe_sysctl() {
  9. if sysctl -n "$1" >/dev/null 2>&1; then
  10. sudo sysctl -w "$1=$2" >/dev/null 2>&1 || log "Warning: Could not set $1"
  11. else
  12. log "Notice: $1 not available on this system"
  13. fi
  14. return 0 # Always return success
  15. }
  16. log "Applying comprehensive performance optimizations..."
  17. # System-wide power management
  18. log "Configuring power management..."
  19. sudo pmset -a lessbright 0
  20. sudo pmset -a disablesleep 1
  21. sudo pmset -a sleep 0
  22. sudo pmset -a hibernatemode 0
  23. sudo pmset -a autopoweroff 0
  24. sudo pmset -a standby 0
  25. sudo pmset -a powernap 0
  26. sudo pmset -a proximitywake 0
  27. sudo pmset -a tcpkeepalive 1
  28. sudo pmset -a powermode 1
  29. sudo pmset -a gpuswitch 2
  30. sudo pmset -a displaysleep 0
  31. sudo pmset -a disksleep 0
  32. # Memory and kernel optimizations
  33. log "Configuring memory and kernel settings..."
  34. safe_sysctl "kern.memorystatus_purge_on_warning" 0
  35. safe_sysctl "kern.memorystatus_purge_on_critical" 0
  36. safe_sysctl "kern.timer.coalescing_enabled" 0
  37. # Check for integrated GPU using system_profiler
  38. if system_profiler SPDisplaysDataType | grep -q "Chipset Model: Apple"; then
  39. log "Apple Silicon GPU detected, configuring GPU settings..."
  40. safe_sysctl "kern.iogpu.dynamic_memory_management" 0
  41. safe_sysctl "kern.iogpu.dynamic_memory_management_debug" 0
  42. else
  43. log "Notice: Apple Silicon GPU not detected, skipping GPU-specific optimizations"
  44. fi
  45. # Metal and GPU optimizations
  46. log "Configuring Metal and GPU settings..."
  47. defaults write com.apple.CoreML MPSEnableGPUValidation -bool false
  48. defaults write com.apple.CoreML MPSEnableMetalValidation -bool false
  49. defaults write com.apple.CoreML MPSEnableGPUDebug -bool false
  50. defaults write com.apple.Metal GPUDebug -bool false
  51. defaults write com.apple.Metal GPUValidation -bool false
  52. defaults write com.apple.Metal MetalValidation -bool false
  53. defaults write com.apple.Metal MetalCaptureEnabled -bool false
  54. defaults write com.apple.Metal MTLValidationBehavior -string "Disabled"
  55. defaults write com.apple.Metal EnableMTLDebugLayer -bool false
  56. defaults write com.apple.Metal MTLDebugLevel -int 0
  57. defaults write com.apple.Metal PreferIntegratedGPU -bool false
  58. defaults write com.apple.Metal ForceMaximumPerformance -bool true
  59. defaults write com.apple.Metal MTLPreferredDeviceGPUFrame -bool true
  60. # Create MPS cache directory with proper permissions
  61. sudo mkdir -p /tmp/mps_cache
  62. sudo chmod 777 /tmp/mps_cache
  63. # Process and resource limits
  64. log "Configuring process limits..."
  65. sudo launchctl limit maxfiles 524288 524288
  66. ulimit -n 524288 || log "Warning: Could not set file descriptor limit"
  67. ulimit -c 0
  68. ulimit -l unlimited || log "Warning: Could not set memory lock limit"
  69. # Export performance-related environment variables
  70. cat << 'EOF' > /tmp/performance_env.sh
  71. # Metal optimizations
  72. export MTL_DEBUG_LAYER=0
  73. export METAL_DEVICE_WRAPPER_TYPE=1
  74. export METAL_DEBUG_ERROR_MODE=0
  75. export METAL_FORCE_PERFORMANCE_MODE=1
  76. export METAL_DEVICE_PRIORITY=high
  77. export METAL_MAX_COMMAND_QUEUES=1024
  78. export METAL_LOAD_LIMIT=0
  79. export METAL_VALIDATION_ENABLED=0
  80. export METAL_ENABLE_VALIDATION_LAYER=0
  81. export OBJC_DEBUG_MISSING_POOLS=NO
  82. export MPS_CACHEDIR=/tmp/mps_cache
  83. # MLX optimizations
  84. export MLX_USE_GPU=1
  85. export MLX_METAL_COMPILE_ASYNC=1
  86. export MLX_METAL_PREALLOCATE=1
  87. export MLX_METAL_MEMORY_GUARD=0
  88. export MLX_METAL_CACHE_KERNELS=1
  89. export MLX_PLACEMENT_POLICY=metal
  90. export MLX_METAL_VALIDATION=0
  91. export MLX_METAL_DEBUG=0
  92. export MLX_FORCE_P_CORES=1
  93. export MLX_METAL_MEMORY_BUDGET=0
  94. export MLX_METAL_PREWARM=1
  95. # Python optimizations
  96. export PYTHONUNBUFFERED=1
  97. export PYTHONOPTIMIZE=2
  98. export PYTHONHASHSEED=0
  99. export PYTHONDONTWRITEBYTECODE=1
  100. EOF
  101. log "Performance optimizations completed. Environment variables written to /tmp/performance_env.sh"