-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmm_limit
executable file
·83 lines (62 loc) · 1.45 KB
/
mm_limit
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
#!/bin/bash
if mount | grep -qw cgroup2; then
ver=2
elif mount | grep -qw cgroup; then
ver=1
else
echo "No cgroup detected."
exit 1
fi
cg_name="mm_limit_cgroup"
if [[ $ver == 1 ]]; then
cg_root="/sys/fs/cgroup/memory"
else
cg_root="/sys/fs/cgroup"
fi
cg_path="$cg_root/$cg_name"
if [[ $ver == 1 ]]; then
mem_high_path="$cg_path/memory.limit_in_bytes"
else
mem_high_path="$cg_path/memory.high"
fi
proc_path="$cg_path/cgroup.procs"
# By default, allow 4MB mem usage
mem_high_default=4M
pid="$1"
mem_high="$2"
err()
{
echo "$@"
exit 1
}
usage()
{
cat <<EOF
usage: $0 <pid> [mem_high]
Create a shared cgroup "$cg_name" and put process into the group. When
mem_high is specified, limit memory usage of the group to 'mem_high'. By
default, mem_high=$mem_high_default.
The user must be in the sudoers list.
EOF
exit 1
}
if [[ -z "$mem_high" ]]; then
mem_high=$mem_high_default
fi
if [[ -z "$pid" ]]; then
usage
fi
if [[ ! -d "$cg_path" ]]; then
echo "Creating directory $cg_path"
sudo mkdir $cg_path || err "Failed to create $cg_path"
fi
if ! [[ -f "$mem_high_path" ]]; then
err "Didn't find path $mem_high_path"
fi
echo "Setting $mem_high_path to $mem_high..."
echo $mem_high | sudo tee $mem_high_path &> /dev/null || \
err "Failed to setup memory.high"
echo "Moving PID $pid into cgroup..."
echo $pid | sudo tee $proc_path &> /dev/null ||
err "Failed to add PID $pid to cgroup"
echo "All done"