#!/bin/sh # HAHAHAH Bro it finally works. It fucking works. It finally fucking works. # It just needs to monitor IO with iotop and start killing firefox processes # from the bottom (least memory usage) up. This allows any one tab access to the # full memory available to Firefox! And integrate it into the your-fired.sh # script. And it's truly fixed. And integrate the firefox updater. IO_ROOT_DIR=$HOME/.cache/mozilla/firefox/ [ -d "$IO_ROOT_DIR" ] || exit 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%.*} } join_group() { GROUP_DIR=/sys/fs/cgroup/firefox~$id [ -d "$GROUP_DIR" ] || sudo mkdir "$GROUP_DIR" echo $$ | sudo tee "$GROUP_DIR"/cgroup.procs } add_subtree_controller() { search="\\b$1\\b" grep -q "$search" /sys/fs/cgroup/cgroup.controllers || return grep "$search" /sys/fs/cgroup/cgroup.subtree_control || echo +"$1" | $AS_ROOT tee /sys/fs/cgroup/cgroup.subtree_control } set_max() { add_subtree_controller "$1" [ -e "$GROUP_DIR"/"$1".max ] || return printf '%s\n' "$2" | $AS_ROOT tee "$GROUP_DIR"/"$1".max } set_max_ratio() { local controller="$1" ratio="$2" 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 majmin=$(lsblk -o 'MOUNTPOINT,MAJ:MIN' | sed -ne "s?^$fsroot *??p") || return case "$majmin" in *:*) ;; *) return 1 ;; esac set_max "$controller" "$majmin wbps=$limit rbps=$limit" ;; *) set_max "$controller" "$limit" esac } set -ex [ "$(id -un)" = 0 ] && AS_ROOT= || AS_ROOT='sudo --' id=$(id -u) join_group firefox~"$id" set_max_ratio memory 1/2 set_max_ratio io 9/10 exec firefox "$@"