optimize_performance.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. # Only try to set iogpu settings if they exist
  37. if sysctl -n kern.iogpu >/dev/null 2>&1; then
  38. safe_sysctl "kern.iogpu.dynamic_memory_management" 0
  39. safe_sysctl "kern.iogpu.dynamic_memory_management_debug" 0
  40. fi
  41. # Metal and GPU optimizations
  42. log "Configuring Metal and GPU settings..."
  43. defaults write com.apple.CoreML MPSEnableGPUValidation -bool false
  44. defaults write com.apple.CoreML MPSEnableMetalValidation -bool false
  45. defaults write com.apple.CoreML MPSEnableGPUDebug -bool false
  46. defaults write com.apple.Metal GPUDebug -bool false
  47. defaults write com.apple.Metal GPUValidation -bool false
  48. defaults write com.apple.Metal MetalValidation -bool false
  49. defaults write com.apple.Metal MetalCaptureEnabled -bool false
  50. defaults write com.apple.Metal MTLValidationBehavior -string "Disabled"
  51. defaults write com.apple.Metal EnableMTLDebugLayer -bool false
  52. defaults write com.apple.Metal MTLDebugLevel -int 0
  53. defaults write com.apple.Metal PreferIntegratedGPU -bool false
  54. defaults write com.apple.Metal ForceMaximumPerformance -bool true
  55. defaults write com.apple.Metal MTLPreferredDeviceGPUFrame -bool true
  56. # Create MPS cache directory with proper permissions
  57. sudo mkdir -p /tmp/mps_cache
  58. sudo chmod 777 /tmp/mps_cache
  59. # Process and resource limits
  60. log "Configuring process limits..."
  61. sudo launchctl limit maxfiles 524288 524288
  62. ulimit -n 524288 || log "Warning: Could not set file descriptor limit"
  63. ulimit -c 0
  64. ulimit -l unlimited || log "Warning: Could not set memory lock limit"
  65. # Export performance-related environment variables
  66. cat << 'EOF' > /tmp/performance_env.sh
  67. # Metal optimizations
  68. export MTL_DEBUG_LAYER=0
  69. export METAL_DEVICE_WRAPPER_TYPE=1
  70. export METAL_DEBUG_ERROR_MODE=0
  71. export METAL_FORCE_PERFORMANCE_MODE=1
  72. export METAL_DEVICE_PRIORITY=high
  73. export METAL_MAX_COMMAND_QUEUES=1024
  74. export METAL_LOAD_LIMIT=0
  75. export METAL_VALIDATION_ENABLED=0
  76. export METAL_ENABLE_VALIDATION_LAYER=0
  77. export OBJC_DEBUG_MISSING_POOLS=NO
  78. export MPS_CACHEDIR=/tmp/mps_cache
  79. # MLX optimizations
  80. export MLX_USE_GPU=1
  81. export MLX_METAL_COMPILE_ASYNC=1
  82. export MLX_METAL_PREALLOCATE=1
  83. export MLX_METAL_MEMORY_GUARD=0
  84. export MLX_METAL_CACHE_KERNELS=1
  85. export MLX_PLACEMENT_POLICY=metal
  86. export MLX_METAL_VALIDATION=0
  87. export MLX_METAL_DEBUG=0
  88. export MLX_FORCE_P_CORES=1
  89. export MLX_METAL_MEMORY_BUDGET=0
  90. export MLX_METAL_PREWARM=1
  91. # Python optimizations
  92. export PYTHONUNBUFFERED=1
  93. export PYTHONOPTIMIZE=2
  94. export PYTHONHASHSEED=0
  95. export PYTHONDONTWRITEBYTECODE=1
  96. EOF
  97. log "Performance optimizations completed. Environment variables written to /tmp/performance_env.sh"