summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Cady <d@cryptonomic.net>2022-11-29 22:31:18 -0500
committerAndrew Cady <d@cryptonomic.net>2022-11-29 22:31:18 -0500
commit20ee182befabc679b12d26e7ee64f52f58332802 (patch)
tree3a97114c84cb76dd081ff83d6c7efc6ed9ab228c
parent48fb88314fc8dcbf9483d212b2c32ff9d021a744 (diff)
remove redundant
-rwxr-xr-xdot/local/bin/sliceweasel119
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
2VERBOSE=y
3
4command=firefox
5memory_ratio=0.5
6control_group=$command
7
8as_root=sudo
9
10total_memory() { free -b | (read line; read Mem total _; echo $total); }
11out() { $as_root tee "$@" >/dev/null; }
12
13die() { printf 'Error: %s\n' "$*" >&2; exit 1; }
14verbose() { [ ! "$VERBOSE" ] || printf '%s\n' "$*" >&2; }
15
16total_memory=$(total_memory) || die "could not ascertain total system memory; probable bug"
17memory_limit_in_bytes=$(bc -q <<< "$total_memory * $memory_ratio / 1" ) || die "process 'bc' failed. Is it installed?"
18
19init_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
27get_cpu_limit()
28{
29 false
30}
31
32get_memory_limit()
33{
34 cat "$control_group_dir"/memory.limit_in_bytes
35}
36
37set_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
48join_control_group()
49{
50 out "$control_group_dir"/cgroup.procs <<< $$
51}
52
53get_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
66launch_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
75pretty_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
83recpu_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
93resize_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
104usage()
105{
106 cat >&2 <<EOF
107usage: ${0##*/} [--resize|--resize <bytes>|--resize=<bytes>] [--] [pass-through-options] [...]
108 ${0##*/} --help
109EOF
110}
111
112case "$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 "$@" ;;
119esac