optimize_performance.sh 3.7 KB

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