diff options
author | Andrew Cady <d@cryptonomic.net> | 2021-11-07 19:17:28 -0500 |
---|---|---|
committer | Andrew Cady <d@cryptonomic.net> | 2021-11-07 19:17:28 -0500 |
commit | 7d83d858a73cd6530d09bddacaa7a1253d8ba807 (patch) | |
tree | efc89911fbe24c40e623904330ac8a5e31df270a | |
parent | 6aae90784e59d104098b9b52fc9c4785e0826f71 (diff) |
sliceweasel will use v2 cgroup within the user.slice/firefox cgroup
-rwxr-xr-x | ioslay-mgr.sh | 56 | ||||
-rwxr-xr-x | sliceweasel | 56 |
2 files changed, 84 insertions, 28 deletions
diff --git a/ioslay-mgr.sh b/ioslay-mgr.sh index d4f545c..d7ce1cb 100755 --- a/ioslay-mgr.sh +++ b/ioslay-mgr.sh | |||
@@ -1,16 +1,16 @@ | |||
1 | #!/bin/sh | 1 | #!/bin/bash |
2 | 2 | ||
3 | ioslay= | 3 | ioslay= |
4 | each_new_process() | 4 | each_new_process() |
5 | { | 5 | { |
6 | while read pid _ | 6 | while read pid _ |
7 | do | 7 | do |
8 | set -- "$@" $pid | 8 | read -N 10001000 procs < /sys/fs/cgroup/$group/cgroup.procs |
9 | set -- $procs | ||
9 | 10 | ||
10 | # filter non-existent processes | 11 | for pid in "$@" |
11 | for _ in "$@" | ||
12 | do | 12 | do |
13 | if [ -e /proc/"$1" ] | 13 | if read comm < /proc/$pid/comm && [ "$comm" = 'Web Content' ] |
14 | then | 14 | then |
15 | set -- "$@" "$1" | 15 | set -- "$@" "$1" |
16 | fi | 16 | fi |
@@ -19,7 +19,11 @@ each_new_process() | |||
19 | 19 | ||
20 | if [ "$ioslay" ] | 20 | if [ "$ioslay" ] |
21 | then | 21 | then |
22 | kill $(pgrep -P $ioslay) | 22 | children=$(pgrep -P $ioslay) |
23 | if [ "$children" ] | ||
24 | then | ||
25 | kill $children | ||
26 | fi | ||
23 | wait $ioslay | 27 | wait $ioslay |
24 | fi | 28 | fi |
25 | 29 | ||
@@ -45,7 +49,41 @@ get_uid() | |||
45 | ) | 49 | ) |
46 | } | 50 | } |
47 | 51 | ||
48 | # group=firefox~$(get_uid) | 52 | #cgroup-show-each-new-process "$group" | grep --line-buffered 'Web Content' | each_new_process |
49 | group=firefox~1000 | 53 | |
54 | # Really, could just check for any/first cgroup with processes and basename | ||
55 | # 'firefox'. Fork for each one. | ||
56 | |||
57 | uid=1000 | ||
58 | group=user.slice/user-$uid.slice/firefox | ||
59 | wantcomm='Web Content' | ||
60 | ioslay= | ||
61 | lastprocs= | ||
62 | while true | ||
63 | do | ||
64 | read -N 1000100 procs < /sys/fs/cgroup/$group/cgroup.procs | ||
65 | if [ "$procs" ] | ||
66 | then | ||
67 | set -- | ||
68 | for pid in $procs | ||
69 | do | ||
70 | read comm < /proc/$pid/comm | ||
71 | if [ "$comm" = "$wantcomm" ] | ||
72 | then | ||
73 | set -- "$@" "$pid" | ||
74 | fi | ||
75 | done | ||
76 | |||
77 | echo "pids: ($*|$(echo $procs))" >&2 | ||
78 | if [ "$lastargs" != "$*" ] | ||
79 | then | ||
80 | [ -z "$ioslay" ] || kill $ioslay $(pgrep -P $ioslay) | ||
81 | wait $ioslay | ||
82 | ioslay-firefox "$@" & | ||
83 | ioslay=$! | ||
84 | fi | ||
85 | lastargs=$* | ||
86 | fi | ||
87 | sleep 1 | ||
88 | done | ||
50 | 89 | ||
51 | cgroup-show-each-new-process "$group" | grep --line-buffered 'Web Content' | each_new_process | ||
diff --git a/sliceweasel b/sliceweasel index 2460d0f..1b34d67 100755 --- a/sliceweasel +++ b/sliceweasel | |||
@@ -65,30 +65,44 @@ get_total_io() | |||
65 | echo ${total%.*} | 65 | echo ${total%.*} |
66 | } | 66 | } |
67 | 67 | ||
68 | root_write() | ||
69 | { | ||
70 | $AS_ROOT sh -c 'cat > "$1"' sh "$1" | ||
71 | } | ||
72 | |||
68 | join_group() | 73 | join_group() |
69 | { | 74 | { |
70 | GROUP_DIR=/sys/fs/cgroup/firefox~$id | 75 | GROUP_DIR=/sys/fs/cgroup/"$1" |
71 | [ -d "$GROUP_DIR" ] || sudo mkdir "$GROUP_DIR" | 76 | [ -d "$GROUP_DIR" ] || $AS_ROOT mkdir "$GROUP_DIR" |
72 | echo $$ | sudo tee "$GROUP_DIR"/cgroup.procs | 77 | echo $$ | root_write "$GROUP_DIR"/cgroup.procs |
73 | } | 78 | } |
74 | 79 | ||
75 | add_subtree_controller() | 80 | add_subtree_controller() |
76 | { | 81 | { |
77 | search="\\b$1\\b" | 82 | local group="$1" controller="$2" control_file |
78 | grep -q "$search" /sys/fs/cgroup/cgroup.controllers || return | 83 | control_file=$(realpath -e "/sys/fs/cgroup/$group/cgroup.subtree_control") || return |
79 | grep "$search" /sys/fs/cgroup/cgroup.subtree_control || echo +"$1" | $AS_ROOT tee /sys/fs/cgroup/cgroup.subtree_control | 84 | case "$control_file" in |
85 | /sys/fs/cgroup/cgroup.subtree_control) ;; | ||
86 | *) add_subtree_controller "$group"/.. "$2" ;; | ||
87 | esac | ||
88 | |||
89 | [ "$1" ] && [ "$2" ] && [ -e "$control_file" ] || return | ||
90 | if ! grep -q "\\b${controller}\\b" "$control_file" | ||
91 | then | ||
92 | echo +"$controller" | root_write "$control_file" | ||
93 | fi | ||
80 | } | 94 | } |
81 | 95 | ||
82 | set_max() | 96 | set_max() |
83 | { | 97 | { |
84 | add_subtree_controller "$1" | 98 | add_subtree_controller "$1"/.. "$2" |
85 | [ -e "$GROUP_DIR"/"$1".max ] || return | 99 | [ -e "$GROUP_DIR"/"$2".max ] || return |
86 | printf '%s\n' "$2" | $AS_ROOT tee "$GROUP_DIR"/"$1".max | 100 | printf '%s\n' "$3" | root_write "$GROUP_DIR"/"$2".max |
87 | } | 101 | } |
88 | 102 | ||
89 | set_max_ratio() | 103 | set_max_ratio() |
90 | { | 104 | { |
91 | local controller="$1" ratio="$2" limit | 105 | local group="$1" controller="$2" ratio="$3" limit |
92 | total=$(get_total_$controller) | 106 | total=$(get_total_$controller) |
93 | n=${ratio%%/*} | 107 | n=${ratio%%/*} |
94 | d=${ratio#$n/} | 108 | d=${ratio#$n/} |
@@ -104,22 +118,26 @@ set_max_ratio() | |||
104 | *:*) ;; | 118 | *:*) ;; |
105 | *) return 1 ;; | 119 | *) return 1 ;; |
106 | esac | 120 | esac |
107 | set_max "$controller" "$majmin wbps=$limit rbps=$limit" | 121 | set_max "$group" "$controller" "$majmin wbps=$limit rbps=$limit" |
108 | ;; | 122 | ;; |
109 | *) set_max "$controller" "$limit" | 123 | *) set_max "$group" "$controller" "$limit" |
110 | esac | 124 | esac |
111 | } | 125 | } |
112 | 126 | ||
113 | set -ex | 127 | get_current_group() |
114 | 128 | { | |
115 | [ "$(id -un)" = 0 ] && AS_ROOT= || AS_ROOT='sudo --' | 129 | read g < /proc/$$/cgroup |
130 | echo ${g#0::/} | ||
131 | } | ||
116 | 132 | ||
117 | id=$(id -u) | 133 | set -e |
118 | 134 | ||
119 | join_group firefox~"$id" | 135 | [ "$(id -un)" = 0 ] && AS_ROOT= || AS_ROOT='sudo --' |
120 | set_max_ratio memory 1/2 | ||
121 | set_max_ratio io 9/10 | ||
122 | 136 | ||
137 | group=user.slice/user-$(id -u).slice/firefox | ||
138 | join_group "$group" | ||
139 | set_max_ratio "$group" memory 1/2 | ||
140 | set_max_ratio "$group" io 9/10 | ||
123 | exec firefox "$@" | 141 | exec firefox "$@" |
124 | 142 | ||
125 | 143 | ||