-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathides-set-oom-adj
executable file
·152 lines (122 loc) · 3.66 KB
/
ides-set-oom-adj
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
144
145
146
147
148
149
150
151
152
#! /bin/sh
#
PROG="$(basename $0)"
## defaults
DEFAULT_MAX_UID=500
DEFAULT_OOM_ADJ=-17
## usage
usage () {
cat <<EOF
Usage: $PROG [options]
Set the '/proc/PID/oom_adj' value for all processes owned by system users.
A "system user" is any user whose UID is less than $DEFAULT_MAX_UID (you can
change this value with the '-m' option).
This is a crude hack to workaround the lack of cgroups support in GridEngine 6.2.
Options:
--max-uid, -m NUM
Only affect processes whose real user ID is less than NUM.
(Default: $DEFAULT_MAX_UID)
--oom-adj, -a NUM
Set the '/proc/PID/oom_adj' value to NUM (default: $DEFAULT_OOM_ADJ).
--no-act, -n
Print what would have been done, but do not actually make any change
to the system.
--help, -h Print this help text.
EOF
}
## helper functions
die () {
rc="$1"
shift
(echo -n "$PROG: ERROR: ";
if [ $# -gt 0 ]; then echo "$@"; else cat; fi) 1>&2
exit $rc
}
log () {
lvl="$1"
shift
if [ "${VERBOSE:-0}" -ge "$lvl" ]; then
echo "$@"
fi
}
warn () {
(echo -n "$PROG: WARNING: ";
if [ $# -gt 0 ]; then echo "$@"; else cat; fi) 1>&2
}
have_command () {
type "$1" >/dev/null 2>/dev/null
}
require_command () {
if ! have_command "$1"; then
die 1 "Could not find required command '$1' in system PATH. Aborting."
fi
}
is_absolute_path () {
expr match "$1" '/' >/dev/null 2>/dev/null
}
## parse command-line
short_opts='a:hm:nv'
long_opts='help,max-uid:,oom-adj:,no-act,dry-run,just-print,verbose'
if [ "x$(getopt -T)" != 'x--' ]; then
# GNU getopt
args=$(getopt --name "$PROG" --shell sh -l "$long_opts" -o "$short_opts" -- "$@")
if [ $? -ne 0 ]; then
die 1 "Type '$PROG --help' to get usage information."
fi
# use 'eval' to remove getopt quoting
eval set -- $args
else
# old-style getopt, use compatibility syntax
args=$(getopt "$short_opts" "$@")
if [ $? -ne 0 ]; then
die 1 "Type '$PROG --help' to get usage information."
fi
set -- $args
fi
while [ $# -gt 0 ]; do
case "$1" in
--oom-adj|-a) oom_adj="$2"; shift ;;
--max-uid|-m) max_uid="$2"; shift ;;
--no-act|--dry-run|--just-print|-n) maybe=echo ;;
--verbose|-v) verbose='-v'; VERBOSE=$(expr ${VERBOSE:-0} + 1) ;;
--help|-h) usage; exit 0 ;;
--) shift; break ;;
esac
shift
done
## main
require_command expr
require_command ps
if [ -z "$max_uid" ]; then
max_uid="$DEFAULT_MAX_UID"
fi
if [ -z "$oom_adj" ]; then
oom_adj="$DEFAULT_OOM_ADJ"
fi
# either print command (echo) or execute (eval) it
if [ -z "$maybe" ]; then
maybe=eval
fi
ps -e -o pid,uid,ruser,cmd,command -ww --no-headers | ( \
while read pid uid ruser cmd command; do
if expr match "$command" '^\[' >/dev/null; then
log 1 "Process $pid ('$cmd') is an in-kernel process; skipping it."
continue
fi
if [ "$uid" -lt "$max_uid" ]; then
current_oom_adj=$(cat "/proc/$pid/oom_adj")
if [ -z "$current_oom_adj" ]; then
# the process has finished and disappeared while we
# were running, so skip it
continue
fi
log 2 "Examining process $pid ('$cmd'), owned by user '$ruser' ..."
if [ "$current_oom_adj" -ne "$oom_adj" ]; then
log 1 "Process $pid's current 'oom_adj' value is $current_oom_adj, setting it to $oom_adj instead."
$maybe echo "$oom_adj" '>' "/proc/$pid/oom_adj"
else
log 2 "Process $pid's current 'oom_adj' value is already $oom_adj, nothing to do."
fi
fi
done
)