summaryrefslogtreecommitdiff
path: root/sliceweasel
blob: 1b34d67c5ec12ca30c06f6e4f64d5d1465a4bdc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/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%.*}
}

root_write()
{
    $AS_ROOT sh -c 'cat > "$1"' sh "$1"
}

join_group()
{
    GROUP_DIR=/sys/fs/cgroup/"$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=$(realpath -e "/sys/fs/cgroup/$group/cgroup.subtree_control") || return
    case "$control_file" in
        /sys/fs/cgroup/cgroup.subtree_control) ;;
        *) add_subtree_controller "$group"/.. "$2" ;;
    esac

    [ "$1" ] && [ "$2" ] && [ -e "$control_file" ] || return
    if ! grep -q "\\b${controller}\\b" "$control_file"
    then
        echo +"$controller" | root_write "$control_file"
    fi
}

set_max()
{
    add_subtree_controller "$1"/.. "$2"
    [ -e "$GROUP_DIR"/"$2".max ] || return
    printf '%s\n' "$3" | root_write "$GROUP_DIR"/"$2".max
}

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
            majmin=$(lsblk -o 'MOUNTPOINT,MAJ:MIN' | sed -ne "s?^$fsroot  *??p") || return
            case "$majmin" in
                *:*) ;;
                *) return 1 ;;
            esac
            set_max "$group" "$controller" "$majmin wbps=$limit rbps=$limit"
        ;;
        *) set_max "$group" "$controller" "$limit"
    esac
}

get_current_group()
{
    read g < /proc/$$/cgroup
    echo ${g#0::/}
}

set -e

[ "$(id -un)" = 0 ] && AS_ROOT= || AS_ROOT='sudo --'

group=user.slice/user-$(id -u).slice/firefox
join_group "$group"
set_max_ratio "$group" memory 1/2
set_max_ratio "$group" io 9/10
exec firefox "$@"