-
-
Save erayack/fb277a8e061e2bdf6a1ebc470f499b4c to your computer and use it in GitHub Desktop.
Script to set MLX memory limits
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -euo pipefail | |
| DEFAULT_WIRED_LIMIT_PERCENT=85 | |
| DEFAULT_WIRED_LWM_PERCENT=75 | |
| usage() { | |
| cat <<'EOF' | |
| Usage: set_wired_limits.sh [MAX_PERCENT] [LWM_PERCENT] [--apply] | |
| - MAX_PERCENT: upper bound percent (0-100), default 85 | |
| - LWM_PERCENT: lower bound percent (0-100), default 75 | |
| - --apply: actually write values via sysctl (otherwise dry-run) | |
| EOF | |
| } | |
| APPLY=0 | |
| ARGS=() | |
| for arg in "$@"; do | |
| case "$arg" in | |
| --apply) APPLY=1 ;; | |
| -h|--help) usage; exit 0 ;; | |
| *) ARGS+=("$arg") ;; | |
| esac | |
| done | |
| WIRED_LIMIT_PERCENT=${ARGS[0]:-$DEFAULT_WIRED_LIMIT_PERCENT} | |
| WIRED_LWM_PERCENT=${ARGS[1]:-$DEFAULT_WIRED_LWM_PERCENT} | |
| if ! [[ "$WIRED_LIMIT_PERCENT" =~ ^[0-9]+$ && "$WIRED_LWM_PERCENT" =~ ^[0-9]+$ ]]; then | |
| echo "Error: Percentages must be integers." >&2 | |
| exit 1 | |
| fi | |
| if (( WIRED_LIMIT_PERCENT < 0 || WIRED_LIMIT_PERCENT > 100 || WIRED_LWM_PERCENT < 0 || WIRED_LWM_PERCENT > 100 )); then | |
| echo "Error: Percentages must be between 0 and 100." >&2 | |
| exit 1 | |
| fi | |
| TOTAL_MEM_MB=$(( $(sysctl -n hw.memsize) / 1024 / 1024 )) | |
| WIRED_LIMIT_MB=$(( TOTAL_MEM_MB * WIRED_LIMIT_PERCENT / 100 )) | |
| WIRED_LWM_MB=$(( TOTAL_MEM_MB * WIRED_LWM_PERCENT / 100 )) | |
| echo "Total memory: ${TOTAL_MEM_MB} MB" | |
| echo "Maximum limit: ${WIRED_LIMIT_MB} MB (${WIRED_LIMIT_PERCENT}%) -> sysctl key: iogpu.wired_limit_mb" | |
| echo "Lower bound: ${WIRED_LWM_MB} MB (${WIRED_LWM_PERCENT}%) -> sysctl key: iogpu.wired_lwm_mb" | |
| if (( APPLY )); then | |
| echo "Applying settings (sudo required)…" | |
| sudo sysctl -w iogpu.wired_limit_mb="$WIRED_LIMIT_MB" | |
| sudo sysctl -w iogpu.wired_lwm_mb="$WIRED_LWM_MB" | |
| echo "Done." | |
| else | |
| echo "Dry-run only. Re-run with --apply to write values." | |
| fi |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Adds
set -euo pipefailfor safer shell execution.Testing guidance:
./set_wired_limits.sh./set_wired_limits.sh 80 70./set_wired_limits.sh 80 70 --apply./set_wired_limits.sh 110should exit with validation error.