From 07d4f9c7df8d226ce77b9cf8e81432530f8a1cb4 Mon Sep 17 00:00:00 2001 From: Andrew Cady Date: Tue, 29 Nov 2022 12:31:17 -0500 Subject: rename ioslay-mgr.sh; test write ops --- Makefile | 2 +- README.txt | 2 +- firestart | 31 ++++++++++++--- src/firefox-io-slayer | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/ioslay-mgr.sh | 105 -------------------------------------------------- 5 files changed, 133 insertions(+), 112 deletions(-) create mode 100755 src/firefox-io-slayer delete mode 100755 src/ioslay-mgr.sh diff --git a/Makefile b/Makefile index d259fd1..bcc1005 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ BINDIR = /usr/local/bin -BINARIES = firestart firestartx $(addprefix src/,sliceweasel.lib.sh ioslay-mgr.sh ioslay-firefox) +BINARIES = firestart firestartx $(addprefix src/,sliceweasel.lib.sh firefox-io-slayer ioslay-firefox) SUDO != [ "$$(id -u)" = 0 ] || echo sudo .PHONY: install install-bin diff --git a/README.txt b/README.txt index 492e278..c217f7a 100644 --- a/README.txt +++ b/README.txt @@ -14,7 +14,7 @@ Then you can use the two commands: firestartx - start Firefox in a CGroup and X server sandbox (recommended) Within the `firefox` systemd service, a background service called `ioslay` will -be started by `ioslay-mgr.sh`. +be started by `firefox-io-slayer`. The launching of Firefox through either of these commands launches a watchdog process that kills Firefox processes when they are spinning the disk. diff --git a/firestart b/firestart index 5f388b5..f9f3bf0 100755 --- a/firestart +++ b/firestart @@ -1,20 +1,41 @@ -#!/bin/sh +#!/bin/bash die() { printf "%s: Error: %s\n" "$0" "$*" >&2; exit 1; } +# Test DEVICE WRITE OPS BY WRITEOPS +test_write_ops() +{ + if ! [ -e "$1" ] + then + mkdir -p "$(dirname "$1")" + sync + begin=$(date +%s.%N) + dd if=/dev/urandom of="$DIR"/urandom.out bs=1M count=300 + sync + end=$(date +%s.%N) + rm "$DIR"/urandom.out + bc -q > "$1" <<< "scale=5; seconds=$end - $begin; 300*1000*1000/seconds" + fi + cat "$1" +} + CMD=firefox UNIT=$CMD DIR=~/.mozilla MEMORY_MAX=50% READ_OPS=1G WRITE_OPS=$READ_OPS -CONFDIR=$HOME/.config/firestart/conf +CONFDIR=$HOME/.config/firestart -if [ -e "$CONFDIR" ] +if [ -e "$CONFDIR"/conf ] then - . "$CONFDIR" + . "$CONFDIR"/conf fi +WRITE_OPS=$(test_write_ops "$CONFDIR"/diskspeed.dat) +WRITE_OPS=${WRITE_OPS%.*} +READ_OPS=$WRITE_OPS + if systemctl --user is-active "$UNIT" >/dev/null then exec "$CMD" "$@" # Asks existing firefox to open a new window. @@ -27,7 +48,7 @@ fi DEV=$(echo $(findmnt --target "$DIR" -o MAJ:MIN -n)) [ "$DEV" ] || die "could not determine backing device for $DIR" -ioslay-mgr.sh launch-unit +firefox-io-slayer launch-unit systemctl --user reset-failed "$UNIT" 2>/dev/null systemd-run --user \ diff --git a/src/firefox-io-slayer b/src/firefox-io-slayer new file mode 100755 index 0000000..56ab849 --- /dev/null +++ b/src/firefox-io-slayer @@ -0,0 +1,105 @@ +#!/bin/bash + +UNIT_NAME=ioslay +if [ "$1" = launch-unit ] +then + if systemctl --user is-active "$UNIT_NAME" + then systemctl --user restart "$UNIT_NAME" + else systemd-run --user -u "$UNIT_NAME" "$0" + fi + exit +fi + +if [ -e /usr/lib/bash/sleep ] +then + enable -f /usr/lib/bash/sleep sleep +fi + +export NOTICE=y +noticeLOG() { [ "$NOTICE" ] || return; echo "Notice: $*" >&2; } +debugLOG() { [ "$DBG" ] || return; echo "Debug: $*" >&2; } + +. sliceweasel.lib.sh + +vkill() +{ + if [ $# = 0 ] + then + return + fi + ( + if [ "$(id -u)" = 0 ] + then sudo= + else sudo=sudo + fi + set -x + ps u "$@" + $sudo kill "$@" + ) +} + +slay_slayer() +{ + if [ "$ioslay" ] + then + children=$(for pid in $ioslay; do pgrep -P $ioslay; done) + grandchildren=$(for pid in $children; do pgrep -P $pid; done) + vkill $ioslay $children $grandchildren + fi +} + +GROUP_BASENAME=firefox + +group_procs=$(get_firefox_cgroup_procs) + +ioslay= +lastprocs= +SIGNALLED= +trap 'SIGNALLED=y' SIGINT SIGTERM SIGHUP +while [ ! "$SIGNALLED" ] +do + if ! [ -e "$group_procs" ] + then + [ "$warned" ] || echo "Warning: firefox not running or cgroup not found" >&2 + warned=y + else + if [ "$warned" ] + then + echo "Found firefox cgroup: $group_procs" >&2 + warned= + fi + read -N 1000100 procs < "$group_procs" + if [ "$procs" ] + then + set -- + for pid in $procs + do + read comm < /proc/$pid/comm + case "$comm" in + 'Isolated Web Content' | 'Web Content' | 'Isolated Web Co') + set -- "$@" "$pid" + debugLOG "accept /proc/$pid/comm $comm" + ;; + *) + debugLOG "reject /proc/$pid/comm $comm" + esac + done + + # echo "pids: ($*|$(echo $procs))" >&2 + if [ "$lastargs" != "$*" ] + then + slay_slayer + wait $ioslay + if [ $# -gt 0 ] + then + ioslay-firefox "$@" & + ioslay=$! + noticeLOG "Launched ioslay-firefox[$ioslay] $*" + fi + fi + lastargs=$* + fi + fi + sleep 1 +done +slay_slayer diff --git a/src/ioslay-mgr.sh b/src/ioslay-mgr.sh deleted file mode 100755 index 56ab849..0000000 --- a/src/ioslay-mgr.sh +++ /dev/null @@ -1,105 +0,0 @@ -#!/bin/bash - -UNIT_NAME=ioslay -if [ "$1" = launch-unit ] -then - if systemctl --user is-active "$UNIT_NAME" - then systemctl --user restart "$UNIT_NAME" - else systemd-run --user -u "$UNIT_NAME" "$0" - fi - exit -fi - -if [ -e /usr/lib/bash/sleep ] -then - enable -f /usr/lib/bash/sleep sleep -fi - -export NOTICE=y -noticeLOG() { [ "$NOTICE" ] || return; echo "Notice: $*" >&2; } -debugLOG() { [ "$DBG" ] || return; echo "Debug: $*" >&2; } - -. sliceweasel.lib.sh - -vkill() -{ - if [ $# = 0 ] - then - return - fi - ( - if [ "$(id -u)" = 0 ] - then sudo= - else sudo=sudo - fi - set -x - ps u "$@" - $sudo kill "$@" - ) -} - -slay_slayer() -{ - if [ "$ioslay" ] - then - children=$(for pid in $ioslay; do pgrep -P $ioslay; done) - grandchildren=$(for pid in $children; do pgrep -P $pid; done) - vkill $ioslay $children $grandchildren - fi -} - -GROUP_BASENAME=firefox - -group_procs=$(get_firefox_cgroup_procs) - -ioslay= -lastprocs= -SIGNALLED= -trap 'SIGNALLED=y' SIGINT SIGTERM SIGHUP -while [ ! "$SIGNALLED" ] -do - if ! [ -e "$group_procs" ] - then - [ "$warned" ] || echo "Warning: firefox not running or cgroup not found" >&2 - warned=y - else - if [ "$warned" ] - then - echo "Found firefox cgroup: $group_procs" >&2 - warned= - fi - read -N 1000100 procs < "$group_procs" - if [ "$procs" ] - then - set -- - for pid in $procs - do - read comm < /proc/$pid/comm - case "$comm" in - 'Isolated Web Content' | 'Web Content' | 'Isolated Web Co') - set -- "$@" "$pid" - debugLOG "accept /proc/$pid/comm $comm" - ;; - *) - debugLOG "reject /proc/$pid/comm $comm" - esac - done - - # echo "pids: ($*|$(echo $procs))" >&2 - if [ "$lastargs" != "$*" ] - then - slay_slayer - wait $ioslay - if [ $# -gt 0 ] - then - ioslay-firefox "$@" & - ioslay=$! - noticeLOG "Launched ioslay-firefox[$ioslay] $*" - fi - fi - lastargs=$* - fi - fi - sleep 1 -done -slay_slayer -- cgit v1.2.3