diff options
author | Andrew Cady <d@cryptonomic.net> | 2022-11-29 22:31:18 -0500 |
---|---|---|
committer | Andrew Cady <d@cryptonomic.net> | 2022-11-29 22:31:18 -0500 |
commit | 20ee182befabc679b12d26e7ee64f52f58332802 (patch) | |
tree | 3a97114c84cb76dd081ff83d6c7efc6ed9ab228c | |
parent | 48fb88314fc8dcbf9483d212b2c32ff9d021a744 (diff) |
remove redundant
-rwxr-xr-x | dot/local/bin/sliceweasel | 119 |
1 files changed, 0 insertions, 119 deletions
diff --git a/dot/local/bin/sliceweasel b/dot/local/bin/sliceweasel deleted file mode 100755 index b9dc389..0000000 --- a/dot/local/bin/sliceweasel +++ /dev/null | |||
@@ -1,119 +0,0 @@ | |||
1 | #!/bin/bash | ||
2 | VERBOSE=y | ||
3 | |||
4 | command=firefox | ||
5 | memory_ratio=0.5 | ||
6 | control_group=$command | ||
7 | |||
8 | as_root=sudo | ||
9 | |||
10 | total_memory() { free -b | (read line; read Mem total _; echo $total); } | ||
11 | out() { $as_root tee "$@" >/dev/null; } | ||
12 | |||
13 | die() { printf 'Error: %s\n' "$*" >&2; exit 1; } | ||
14 | verbose() { [ ! "$VERBOSE" ] || printf '%s\n' "$*" >&2; } | ||
15 | |||
16 | total_memory=$(total_memory) || die "could not ascertain total system memory; probable bug" | ||
17 | memory_limit_in_bytes=$(bc -q <<< "$total_memory * $memory_ratio / 1" ) || die "process 'bc' failed. Is it installed?" | ||
18 | |||
19 | init_control_group() | ||
20 | { | ||
21 | control_group_dir=/sys/fs/cgroup/"$1"/"$control_group" | ||
22 | [ -d "$control_group_dir" ] && return | ||
23 | $as_root mkdir "$control_group_dir" || die "mkdir failed" | ||
24 | set_memory_limit $memory_limit_in_bytes || die "set_memory_limit failed" | ||
25 | } | ||
26 | |||
27 | get_cpu_limit() | ||
28 | { | ||
29 | false | ||
30 | } | ||
31 | |||
32 | get_memory_limit() | ||
33 | { | ||
34 | cat "$control_group_dir"/memory.limit_in_bytes | ||
35 | } | ||
36 | |||
37 | set_memory_limit() | ||
38 | { | ||
39 | prev=$(get_memory_limit) | ||
40 | if [ "$1" -ne "$prev" ]; then | ||
41 | out "$control_group_dir"/memory.limit_in_bytes <<< $1 | ||
42 | verbose "changed memory limit ($prev -> $1)" | ||
43 | else | ||
44 | verbose "memory limit unchanged ($prev)" | ||
45 | fi | ||
46 | } | ||
47 | |||
48 | join_control_group() | ||
49 | { | ||
50 | out "$control_group_dir"/cgroup.procs <<< $$ | ||
51 | } | ||
52 | |||
53 | get_cgroup() | ||
54 | { | ||
55 | local key="$1" pid="${2:-$$}" line val | ||
56 | while read line; do | ||
57 | line=${line#*:} | ||
58 | val=${line#$key:} | ||
59 | [ "$val" != "$line" ] || continue | ||
60 | echo "$val" | ||
61 | return | ||
62 | done < /proc/"$pid"/cgroup | ||
63 | false | ||
64 | } | ||
65 | |||
66 | launch_command() | ||
67 | { | ||
68 | init_control_group memory || die "init_control_group failed" | ||
69 | cg=$(get_cgroup memory) || die "failed to ascertain current control group" | ||
70 | # [ "$cg" = / ] || die "current control group should be '/', but is instead '$cg'" | ||
71 | join_control_group || die "join_control_group failed" | ||
72 | exec $command "$@" | ||
73 | } | ||
74 | |||
75 | pretty_show_memory_limit() | ||
76 | { | ||
77 | local memory_limit="$(get_memory_limit)" | ||
78 | printf "current memory limit: %d bytes (%d%%)\n" \ | ||
79 | "$memory_limit" \ | ||
80 | "$((memory_limit * 100 / total_memory))" | ||
81 | } | ||
82 | |||
83 | recpu_control_group() | ||
84 | { | ||
85 | init_control_group cpu | ||
86 | case "$1" in | ||
87 | [0-9]*%) set_cpu_limit $((total_cpu * "${1%\%}" / 100)) ;; | ||
88 | [0-9]*) set_cpu_limit "$1" ;; | ||
89 | [-+*/][0-9]*) set_cpu_limit $(( $(get_cpu_limit) "$1" )) ;; | ||
90 | esac | ||
91 | } | ||
92 | |||
93 | resize_control_group() | ||
94 | { | ||
95 | init_control_group memory | ||
96 | case "$1" in | ||
97 | '') pretty_show_memory_limit >&2 ;; | ||
98 | [0-9]*%) set_memory_limit $((total_memory * "${1%\%}" / 100)) ;; | ||
99 | [0-9]*) set_memory_limit "$1" ;; | ||
100 | [-+*/][0-9]*) set_memory_limit $(( $(get_memory_limit) "$1" )) ;; | ||
101 | esac | ||
102 | } | ||
103 | |||
104 | usage() | ||
105 | { | ||
106 | cat >&2 <<EOF | ||
107 | usage: ${0##*/} [--resize|--resize <bytes>|--resize=<bytes>] [--] [pass-through-options] [...] | ||
108 | ${0##*/} --help | ||
109 | EOF | ||
110 | } | ||
111 | |||
112 | case "$1" in | ||
113 | --) shift; launch_command "$@" ;; | ||
114 | --resize) resize_control_group "$2" ;; | ||
115 | --resize=*) resize_control_group "${1#--resize=}" ;; | ||
116 | --cpu=*) recpu_control_group "${1#--cpu=}" ;; | ||
117 | --*) usage; [ "$1" = '--help' -o "$1" = '--usage' ] ;; | ||
118 | *) launch_command "$@" ;; | ||
119 | esac | ||