From 61564e4d18058031d96e207862f72f0628934ea3 Mon Sep 17 00:00:00 2001 From: Andrew Cady Date: Tue, 29 Nov 2022 12:00:08 -0500 Subject: move files to src; add README.txt --- sliceweasel.lib.sh | 154 ----------------------------------------------------- 1 file changed, 154 deletions(-) delete mode 100755 sliceweasel.lib.sh (limited to 'sliceweasel.lib.sh') diff --git a/sliceweasel.lib.sh b/sliceweasel.lib.sh deleted file mode 100755 index 549e1e8..0000000 --- a/sliceweasel.lib.sh +++ /dev/null @@ -1,154 +0,0 @@ -#!/bin/sh -die() -{ - printf '%s: Error: %s\n' "$0" "$*" >&2 - exit 1 -} - -get_age() -{ - local mtime now - mtime=$(stat --format=%Y "$1") || return - now=$(date +%s) || return - echo $(( now - mtime )) -} - -get_filesystem() -{ - echo / -} - -get_total_memory() -{ - free -b | { - read _ - read _ total _ - echo $total - } -} - -math() -{ - printf '%s\n' "$*" | bc -lq -} - -get_total_io() -{ - results=$IO_ROOT_DIR/io.test.result - zeroes=$IO_ROOT_DIR/io.test - if [ -e "$results" ] - then - age=$(get_age "$results") || return - if [ "$age" -gt $((60 * 60 * 24 * 7)) ] - then - rm "$results" - fi - fi - - if ! [ -e "$results" ] - then - MEGS=128 - then=$(date +%s.%N) || return - dd if=/dev/zero of="$zeroes" bs=${MEGS}M count=1 || return - now=$(date +%s.%N) || return - rm "$zeroes" - speed=$(math $now - $then / $((MEGS * 1024 * 1024))) || return - echo $speed > "$results" - fi - read total < "$results" || return - echo ${total%.*} -} - -root_write() -{ - $AS_ROOT sh -c 'cat > "$1"' sh "$1" || true -} - -join_group() -{ - GROUP_DIR="$1" - [ -d "$GROUP_DIR" ] || $AS_ROOT mkdir "$GROUP_DIR" - echo $$ | root_write "$GROUP_DIR"/cgroup.procs -} - -add_subtree_controller() -{ - local group="$1" controller="$2" control_file - control_file="$group/cgroup.subtree_control" - - [ "$group" ] && [ "$controller" ] && [ -d "$group" ] && [ -e "$control_file" ] || return - - case "$group" in - /sys/fs/cgroup) ;; - *) add_subtree_controller "$(realpath "$group/..")" "$2" ;; - esac - - if ! grep -qe "\\b${controller}\\b" "$control_file" - then - echo +"$controller" | root_write "$control_file" - fi -} - -set_max() -{ - max_file=$group/$2.max - if [ ! -e "$max_file" ] - then - add_subtree_controller "$1"/.. "$2" - [ -e "$max_file" ] || return - fi - printf '%s\n' "$3" | root_write "$max_file" -} - -set_max_ratio() -{ - local group="$1" controller="$2" ratio="$3" limit - total=$(get_total_$controller) - n=${ratio%%/*} - d=${ratio#$n/} - [ "$d" = "${d%/*}" ] || return - limit=$(( total * n / d )) - [ "$limit" ] || return - [ "$limit" -gt 0 ] || return - case "$controller" in - io) - fsroot=$(get_filesystem "$IO_ROOT_DIR") || return - # TODO: Do not use lsblk, because it fails to report the correct - # MOUNTPOINT when a bind mount is present. - majmin=$(lsblk -o 'MOUNTPOINT,MAJ:MIN' | sed -ne "s?^$fsroot *??p") || return - case "$majmin" in - *:*) ;; - *) echo "Error: majmin=$majmin" >&2; return 1 ;; - esac - set_max "$group" "$controller" "$majmin wbps=$limit rbps=$limit" - ;; - *) set_max "$group" "$controller" "$limit" - esac -} - -get_uid() -{ - ( - if [ "$SUDO_USER" ] - then - IFS=: - set -- $(getent passwd "${SUDO_USER}") || return - echo $3 - else - id -u - fi - ) -} - -get_firefox_cgroup_procs() -{ - uid=$(get_uid) - [ "$uid" ] || return - echo /sys/fs/cgroup/user.slice/user-$uid.slice/user@$uid.service/app.slice/firefox.service/cgroup.procs -} - -get_current_group() -{ - read g < /proc/$$/cgroup - echo /sys/fs/cgroup/${g#0::/} -} -- cgit v1.2.3