From 4e93a2d8b757b554acbe5c791d3effe0abd43716 Mon Sep 17 00:00:00 2001 From: Andrew Cady Date: Fri, 22 Oct 2021 09:38:29 -0400 Subject: automatic restarting of "iotop" to pick up new firefox pids --- fireslay | 89 ++++++++++++++++++++++++---------- ioslay-firefox | 147 +++++++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 191 insertions(+), 45 deletions(-) diff --git a/fireslay b/fireslay index 0b26c86..1926e19 100755 --- a/fireslay +++ b/fireslay @@ -1,10 +1,67 @@ #!/bin/sh -if [ "$1" = -q ] -then - exec 2>/dev/null - shift -fi +die() +{ + printf '%s: Error: %s\n' "$0" "$*" >&2 + exit 1 +} + +parseopts() +{ + while [ $# -gt 0 ] + do + case "$1" in + -q | --quiet ) + exec 2>/dev/null + ;; + -y | --kill-last-process ) + FIRESLAY_KILL_LAST_PROCESS=y + ;; + --) + shift + break + ;; + -* ) + die unknown option + ;; + *) + break + ;; + esac + shift + done + [ $# = 0 ] || die usage +} + +main() +{ + parseopts "$@" + + set -- $(web_content_pids) + + ( + case $# in + 0 ) echo "$0: Warning: Not slaying: no Web Content process found." >&2 + exit + ;; + 1 ) if ! [ "$FIRESLAY_KILL_LAST_PROCESS" ] + then + echo "$0: Warning: Not slaying: only one 'Web Content' process. Specify -y to kill it." >&2 + exit + fi + ;; + * ) shift + ;; + esac + set -x + ps $* >&2 + kill $* + ) + + r=$? + ps $* >&2 + exit $r +} web_content_pids() { @@ -18,25 +75,5 @@ web_content_pids() } } -set -- $(web_content_pids | tac) +main "$@" -case $# in - 0) - echo "$0: Warning: Not slaying: no Web Content process found." >&2 - exit - ;; - 1) - echo "$0: Warning: Not slaying: only one Web Content process found." >&2 - ps $* >&2 - exit - ;; -esac - -( - set -x - ps $1 >&2 - kill $1 -) -r=$? -ps $* >&2 -exit $r diff --git a/ioslay-firefox b/ioslay-firefox index 36f9346..dcae987 100755 --- a/ioslay-firefox +++ b/ioslay-firefox @@ -1,27 +1,68 @@ -#!/bin/sh +#!/bin/bash -if [ "$1" = -q ] +if [ "$UID" != 0 ] then - exec 2>/dev/null - shift + sudo -- "$0" "$@" + exit +fi + +while [ $# -gt 0 ] +do + case "$1" in + -q | --quiet ) + exec 2>/dev/null + shift + ;; + -a | --all ) + IOSLAY_KILL_ALL=y + shift + ;; + -- ) + shift + break + ;; + -* ) + exit 1 + ;; + * ) + break + ;; + esac +done + +if [ $# -gt 0 ] +then + echo "$0: Error: usage" >&2 + exit 1 fi web_content_pids() { - top -w512 -bn1 -o '%MEM' | sed -e '1,/^$/d' | { + top -w512 -b -o '%MEM' | { + unset PIDS while read line - do set -- $line - [ "${12} ${13}" = "Web Content" ] || continue - echo $1 + do + case "$line" in + top*) + if [ "${PIDS+is_set}" ] + then + echo $PIDS + fi + while [ "$line" ] + do read line + done + PIDS= + ;; + *) + set -- $line + [ "${12} ${13}" = "Web Content" ] || continue + PIDS="$PIDS $1" + ;; + esac done } } -set -- $(web_content_pids | tac) - -set -- $(printf "\055\055pid=%s " $*) - - sum() { local total=0 n @@ -44,14 +85,68 @@ keep() printf '%s\n' "$*" } -log= -pct_log= -sudo -- iotop -qq -b "$@" | +new_pids() +{ + # This is not correct because the correct code is not implemented yet. + # But this is OK for testing the other parts. Fixing this is only an + # optimization. + [ "$1" != "$2" ] +} + +unset IOTOP_PID +relaunch_iotop() +{ + if [ "$IOTOP_PID" ] + then + kill "$IOTOP_PID" + fi + + web_content_pids | ( + while read pids + do + set -- $pids + if [ $# = 0 ] + then + kill_iotop + elif new_pids "$pids" "$oldpids" + then + kill_iotop + IOTOP_PID=$(launch_iotop $pids) + fi + sleep 1 + oldpids=$pids + done + ) +} + +kill_iotop() +{ + if [ "$IOTOP_PID" ] + then + ( + set -x + kill "$IOTOP_PID" + ) + fi +} + +launch_iotop() +{ + set -- $(printf '%s\n' "$@" | tac | sed 's/^/--pid=/') + (set -x; iotop -qq -b "$@") | iotop_reader & + echo $! +} + +iotop_reader() +{ + log= + pct_log= while read line do case "$line" in "Total DISK"*) - [ "$(sum $pct_log)" -ge 85 ] && over=y || over= + pct=$(sum $pct_log) + [ "$pct" -ge 85 ] && over=y || over= pct_log= log=$(keep 9 $log) if [ "$over" ] @@ -60,12 +155,23 @@ sudo -- iotop -qq -b "$@" | over= if [ "$(sum $log)" -ge 3 ] then - fireslay + ( + set -x + fireslay -y + ) fi else log="$log 0" fi - ;; + case "$log" in + *1* ) + ( + set -x + : $log + ) + ;; + esac + ;; "Current DISK"*) continue ;; *) set -- $line @@ -74,3 +180,6 @@ sudo -- iotop -qq -b "$@" | ;; esac done +} + +relaunch_iotop -- cgit v1.2.3