]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/commitdiff
unbound: Automatically scale configuration to system
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 8 Sep 2016 18:46:43 +0000 (19:46 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 8 Sep 2016 18:46:43 +0000 (19:46 +0100)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
config/unbound/unbound.conf
src/initscripts/init.d/unbound

index a736e19550962f214c059839ccd00d23c1f214d7..6d8a7f29c30e402d35ac3c17626f7abf02c9db2e 100644 (file)
@@ -10,7 +10,6 @@ server:
        chroot: ""
        directory: "/etc/unbound"
        username: "nobody"
-       num-threads: 2
        port: 53
        do-ip4: yes
        do-ip6: no
@@ -19,6 +18,9 @@ server:
        so-reuseport: yes
        do-not-query-localhost: yes
 
+       # System Tuning
+       include: "/etc/unbound/tuning.conf"
+
        # Logging Options
        verbosity: 1
        use-syslog: yes
@@ -30,10 +32,7 @@ server:
        statistics-cumulative: yes
        extended-statistics: yes
 
-       # Cache Sizes
-       msg-cache-size: 8m
-       rrset-cache-size: 8m
-       key-cache-size: 4m
+       # Prefetching
        prefetch: yes
        prefetch-key: yes
 
index 54e40834bb5b6527766f129feec06e790aa76935..f3d35cf4888775b9a3accf014a5b7603f513087f 100644 (file)
@@ -102,6 +102,69 @@ write_forward_conf() {
        ) > /etc/unbound/forward.conf
 }
 
+write_tuning_conf() {
+       # https://www.unbound.net/documentation/howto_optimise.html
+
+       # Determine number of online processors
+       local processors=$(getconf _NPROCESSORS_ONLN)
+
+       # Determine number of slabs
+       local slabs=1
+       while [ ${slabs} -lt ${processors} ]; do
+               slabs=$(( ${slabs} * 2 ))
+       done
+
+       # Determine amount of system memory
+       local mem=$(get_memory_amount)
+
+       # In the worst case scenario, unbound can use double the
+       # amount of memory allocated to a cache due to malloc overhead
+
+       # Large systems with more than 2GB of RAM
+       if [ ${mem} -ge 2048 ]; then
+               mem=128
+
+       # Small systems with less than 256MB of RAM
+       elif [ ${mem} -le 256 ]; then
+               mem=8
+
+       # Everything else
+       else
+               mem=32
+       fi
+
+       (
+               config_header
+
+               # We run one thread per processor
+               echo "num-threads: ${processors}"
+
+               # Adjust number of slabs
+               echo "infra-cache-slabs: ${slabs}"
+               echo "key-cache-slabs: ${slabs}"
+               echo "msg-cache-slabs: ${slabs}"
+               echo "rrset-cache-slabs: ${slabs}"
+
+               # Slice up the cache
+               echo "rrset-cache-size: $(( ${mem} / 2 ))m"
+               echo "msg-cache-size: $(( ${mem} / 4 ))m"
+               echo "key-cache-size: $(( ${mem} / 4 ))m"
+       ) > /etc/unbound/tuning.conf
+}
+
+get_memory_amount() {
+       local key val unit
+
+       while read -r key val unit; do
+               case "${key}" in
+                       MemTotal:*)
+                               # Convert to MB
+                               echo "$(( ${val} / 1024 ))"
+                               break
+                               ;;
+               esac
+       done < /proc/meminfo
+}
 
 case "$1" in
        start)
@@ -114,6 +177,7 @@ case "$1" in
                fi
 
                # Update configuration files
+               write_tuning_conf
                write_interfaces_conf
                write_forward_conf