optimize_performance.sh 3.1 KB

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