update rules - use auto calc for buffers based on hardware and speed

This commit is contained in:
Frede H 2024-09-11 15:53:24 +02:00
parent 7310b48b1d
commit 5294f9cc53
No known key found for this signature in database
GPG key ID: 3629B5D280E47F0A
5 changed files with 72 additions and 77 deletions

View file

@ -1,18 +1,19 @@
pkgbase = udev-usb-sync pkgbase = udev-usb-sync
pkgdesc = Fine tune write cache and impose buffer limites when USB storage device is plugged pkgdesc = Fine tune write cache and impose buffer limites when USB storage device is plugged
pkgver = 0.8 pkgver = 0.9
pkgrel = 1 pkgrel = 1
url = https://codeberg.org/wonky/udev-usb-sync url = https://codeberg.org/wonky/udev-usb-sync
install = udev-usb-sync.install install = udev-usb-sync.install
arch = any arch = any
license = MIT license = MIT
depends = hdparm depends = hdparm
depends = bc
backup = etc/udev-usb-sync/udev-usb-sync.conf backup = etc/udev-usb-sync/udev-usb-sync.conf
source = 99-usb-sync.rules source = 99-usb-sync.rules
source = udev-usb-sync source = udev-usb-sync
source = udev-usb-sync.conf source = udev-usb-sync.conf
sha256sums = 0c945450f8b472995c27ac4d0b41f333a49a7c8a181827cd57bb80e71194a6f3 sha256sums = 14ff9fa9783f72cb321792ea8a44051d0eb0aaf244edec773d7fa16fdf8dc023
sha256sums = b514e3bf1ea55f5e1dc4f5af46da3b5f9f2409da1efe7b36a11647704faf0a8f sha256sums = bdc35135e6f8074890a911a501ca4fb3e6a7888963b51c3a1edb98c23bb62ec2
sha256sums = 7cf194b2e3767f8ce4dcbf98b665e46d67624034d5ec0f4cb89f359b677c3687 sha256sums = ec26baede73e94f9cfab77cd5aa6e0ffebcc413ff657a4e98eae6c9e2145655e
pkgname = udev-usb-sync pkgname = udev-usb-sync

View file

@ -31,16 +31,13 @@
# : @linux-aarhus (Manjaro Forum) # : @linux-aarhus (Manjaro Forum)
# inspired by : @kwg (EndeavourOS Forum) # inspired by : @kwg (EndeavourOS Forum)
# rule to disable write cache for usb storage KERNEL!="sd[a-z]", GOTO="usb_limit_write_cache_end"
# requires hdparm to be installed ENV{ID_USB_TYPE}!="disk", GOTO="usb_limit_write_cache_end"
ACTION=="add|change", KERNEL=="sd[a-z]", ENV{ID_USB_TYPE}=="disk", RUN+="/usr/bin/hdparm -W 0 /dev/%k" ACTION!="add|change", GOTO="usb_limit_write_cache_end"
# rules to impose buffer limits on USB devices ATTRS{bDeviceClass}!="09", ATTRS{speed}=="10000", RUN+="/usr/bin/udev-usb-sync %k %s{speed}", GOTO="usb_limit_write_cache_end"
# implemented using: /usr/bin/udev-usb-sync ATTRS{bDeviceClass}!="09", ATTRS{speed}=="5000", RUN+="/usr/bin/udev-usb-sync %k %s{speed}", GOTO="usb_limit_write_cache_end"
# see configuration: /etc/udev-usb-sync/udev-usb-sync.conf ATTRS{bDeviceClass}!="09", ATTRS{speed}=="480", RUN+="/usr/bin/udev-usb-sync %k %s{speed}", GOTO="usb_limit_write_cache_end"
# Works since Linux 6.2 ATTRS{bDeviceClass}!="09", ATTRS{speed}=="12", RUN+="/usr/bin/udev-usb-sync %k %s{speed}", GOTO="usb_limit_write_cache_end"
ACTION=="add|change", KERNEL=="sd[a-z]", ENV{ID_USB_TYPE}=="disk", RUN+="/usr/bin/udev-usb-sync %k"
ACTION=="add|change", KERNEL=="sd[a-z]", ENV{ID_USB_TYPE}=="disk", ATTRS{speed}=="12", RUN+="/usr/bin/udev-usb-sync %k 12" LABEL="usb_limit_write_cache_end"
ACTION=="add|change", KERNEL=="sd[a-z]", ENV{ID_USB_TYPE}=="disk", ATTRS{speed}=="480", RUN+="/usr/bin/udev-usb-sync %k 480"
ACTION=="add|change", KERNEL=="sd[a-z]", ENV{ID_USB_TYPE}=="disk", ATTRS{speed}=="5000", RUN+="/usr/bin/udev-usb-sync %k 5000"
ACTION=="add|change", KERNEL=="sd[a-z]", ENV{ID_USB_TYPE}=="disk", ATTRS{speed}=="10000", RUN+="/usr/bin/udev-usb-sync %k 10000"

View file

@ -1,18 +1,18 @@
# Maintainer: root.nix.dk # Maintainer: root.nix.dk
pkgname='udev-usb-sync' pkgname='udev-usb-sync'
pkgver=0.8 pkgver=0.9
pkgrel=2 pkgrel=1
pkgdesc='Fine tune write cache and impose buffer limites when USB storage device is plugged' pkgdesc='Fine tune write cache and impose buffer limites when USB storage device is plugged'
arch=('any') arch=('any')
url='https://codeberg.org/wonky/udev-usb-sync' url='https://codeberg.org/wonky/udev-usb-sync'
license=('MIT') license=('MIT')
depends=('hdparm') depends=('hdparm' 'bc')
backup=("etc/${pkgname}/${pkgname}.conf") backup=("etc/${pkgname}/${pkgname}.conf")
install="${pkgname}.install" install="${pkgname}.install"
source=('99-usb-sync.rules' 'udev-usb-sync' 'udev-usb-sync.conf') source=('99-usb-sync.rules' 'udev-usb-sync' 'udev-usb-sync.conf')
sha256sums=('0c945450f8b472995c27ac4d0b41f333a49a7c8a181827cd57bb80e71194a6f3' sha256sums=('14ff9fa9783f72cb321792ea8a44051d0eb0aaf244edec773d7fa16fdf8dc023'
'b514e3bf1ea55f5e1dc4f5af46da3b5f9f2409da1efe7b36a11647704faf0a8f' 'bdc35135e6f8074890a911a501ca4fb3e6a7888963b51c3a1edb98c23bb62ec2'
'7cf194b2e3767f8ce4dcbf98b665e46d67624034d5ec0f4cb89f359b677c3687') 'ec26baede73e94f9cfab77cd5aa6e0ffebcc413ff657a4e98eae6c9e2145655e')
package() { package() {
install -d -m755 "$pkgdir/etc/udev/rules.d" install -d -m755 "$pkgdir/etc/udev/rules.d"

View file

@ -18,7 +18,7 @@
# @linux-aarhus - root.nix.dk # @linux-aarhus - root.nix.dk
# #
# configuration : /etc/usb-dev-sync/usb-dev-sync.conf # configuration : /etc/usb-dev-sync/usb-dev-sync.conf
# triggered by : /usr/lib/udev/rules.d/99-usb-sync.rules # triggered by : /etc/udev/rules.d/99-usb-sync.rules
# #
# contributors: @megavolt (Manjaro Forum) # contributors: @megavolt (Manjaro Forum)
# : @linux-aarhus (Manjaro Forum) # : @linux-aarhus (Manjaro Forum)
@ -28,46 +28,51 @@
# $1: usb block device # $1: usb block device
# $2: usb bandwidth reported by device # $2: usb bandwidth reported by device
# #
# default values (override in configuration file)
#
# block device is passed in $1 set -euo pipefail
# speed is passed as $2
# defaults LANG=C
use_tweaks=1 LC_NUMERIC=C
use_bandwith=1
max_ratio=50
strict_limit=1
max_bytes=16777216
# speed defined values
max_bytes_12=10485
max_bytes_480=629145
max_bytes_5000=655369
max_bytes_10000=13107200
# source configuratoin to override default values AUTOCALC=${AUTOCALC:-1}
source /etc/udev-usb-sync/udev-usb-sync.conf CONFIG='/etc/udev-usb-sync/udev-usb-sync.conf'
if [[ "$use_tweaks" = 0 ]]; then
exit 0
fi
if [[ -z "$1" ]]; then [[ -f $CONFIG ]] && source $CONFIG
exit 1
fi
# if speed value is not present use default max_bytes BLOCKDEVICE="$1"
[[ -z $2 ]] && use_bandwith=0 SPEED="$2"
KERNEL_MAJOR_VERSION=$(uname -r | awk -F'.' '{print $1}')
# apply max_ratio # disable write cache for device if possible
echo "$max_ratio" > "/sys/block/$1/bdi/max_ratio" [[ -n $(which hdparm) ]] && $(which hdparm) -W 0 /dev/$BLOCKDEVICE
# apply strict limit
echo "$strict_limit" > "/sys/block/$1/bdi/strict_limit" if [[ $KERNEL_MAJOR_VERSION -le 5 ]]; then
# apply max_bytes depending on speed value # the following rules is introduced with kernel 2.6
if [[ use_bandwith == 0 ]]; then # https://docs.kernel.org/admin-guide/abi-testing.html#abi-sys-class-bdi-bdi-max-ratio
# apply default value
echo "$max_bytes" > "/sys/block/$1/bdi/max_bytes" # 1% of available RAM -> 8046522kB -> 80.465kB -> 80MB
else echo 1 > /sys/block/$BLOCKDEVICE/bdi/max_ratio
# apply bandwidth defined value elif [[ $KERNEL_MAJOR_VERSION -ge 6 ]]; then
echo "$max_bytes_$2" > "/sys/block/%1/bdi/max_bytes" # the following rules is introduced with kernel 6.1
# https://docs.kernel.org/admin-guide/abi-testing.html#abi-sys-class-bdi-bdi-strict-limit
# https://docs.kernel.org/admin-guide/abi-testing.html#abi-sys-class-bdi-bdi-max-bytes
# apply strict limit
echo 1 > /sys/block/$BLOCKDEVICE/bdi/strict_limit
if [[ ${AUTOCALC} == 0 ]]; then
# apply 16M as max_bytes
echo 16777216 > /sys/block/$BLOCKDEVICE/bdi/max_bytes
elif [[ ${AUTOCALC} == 1 ]]; then
BUFFER_TIME=${BUFFER_TIME:-"0.05"}
SAFETY_FACTOR=${SAFETY_FACTOR:-"1.3"}
BUFFER_SIZE=$(printf '%.0f' `echo "( ($SPEED / 8) * $BUFFER_TIME * $SAFETY_FACTOR) * 1024 * 1024" | bc`)
# for x in 12 480 5000 10000; do echo -n "$x -> " ;printf "%.0f\n" ` echo "(($x / 8) * 0.05 * 1.3) * 1024 * 1024" | bc`; done
# 62915
# 4089446
# 42593157
# 85196800
# apply calculated buffer size
echo "$BUFFER_SIZE" > /sys/block/$BLOCKDEVICE/bdi/max_bytes
fi
fi fi

View file

@ -24,8 +24,8 @@
# #
# @linux-aarhus - root.nix.dk # @linux-aarhus - root.nix.dk
# #
# configuration : /etc/usb-dev-sync/usb-dev-sync.conf # trigger script: /usr/bin/usb-dev-sync
# triggered by : /usr/lib/udev/rules.d/99-usb-sync.rules # triggered by : /etc/udev/rules.d/99-usb-sync.rules
# #
# contributors: @megavolt (Manjaro Forum) # contributors: @megavolt (Manjaro Forum)
# : @linux-aarhus (Manjaro Forum) # : @linux-aarhus (Manjaro Forum)
@ -33,21 +33,13 @@
# #
# The values used can be modified if you need to do so # The values used can be modified if you need to do so
# The commented default usually works well # The commented default usually works well
#use_tweaks=1 #
#use_bandwith=1 #
## Let it calculate based on given bandwidth
#AUTOCALC=1
#max_ratio=50 ## The time in seconds to hold data in RAM
# strict limit requires kernel >= 6.2 #BUFFER_TIME='0.05'
#strict_limit=1
# a sane default bandwidth value ## multiplicator for safety reasons.
#max_bytes=16777216 #SAFETY_FACTOR='1.3'
# bandwith based limitation requires >= 6.1
# values as reported by the device ATTRS{speed}
# These values seem to be good and stable.
# It buffers 0,01s of the current bandwidth.
#max_bytes_12=10485
#max_bytes_480=629145
#max_bytes_5000=655369
#max_bytes_10000=13107200