summaryrefslogtreecommitdiff
path: root/dynamicarcZFS.sh
blob: 536aa7bd406d32f9d76f8a67c23d9883680e0898 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/bin/bash

# Config
MAX_ARC=$((32 * 1024 * 1024 * 1024))  # 32 GB
MIN_ARC=$((16 * 1024 * 1024 * 1024))  # 16 GB
UPPER_FREE=$((20 * 1024 * 1024))      # 20 GB in KB
LOWER_FREE=$((10 * 1024 * 1024))      # 10 GB in KB
ARC_SYS="/sys/module/zfs/parameters/zfs_arc_max"

# Function to get free memory in KB (Linux)
get_free_mem() {
  awk '/MemAvailable/ {print $2}' /proc/meminfo
}

# Function to get current arc max
get_arc_max() {
  cat $ARC_SYS
}

# Function to set arc max
set_arc_max() {
  local val=$1
  echo $val | sudo tee $ARC_SYS > /dev/null
}

while true; do
  free_mem_kb=$(get_free_mem)
  current_arc=$(get_arc_max)

  if [ "$free_mem_kb" -lt "$LOWER_FREE" ] && [ "$current_arc" -gt "$MIN_ARC" ]; then
    echo "Low memory ($free_mem_kb KB). Lowering ARC max to $MIN_ARC bytes."
    set_arc_max $MIN_ARC
  elif [ "$free_mem_kb" -gt "$UPPER_FREE" ] && [ "$current_arc" -lt "$MAX_ARC" ]; then
    echo "High memory ($free_mem_kb KB). Raising ARC max to $MAX_ARC bytes."
    set_arc_max $MAX_ARC
  fi

  sleep 60  # Check every 60 seconds
done