-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcontainer-limits
executable file
·69 lines (61 loc) · 1.73 KB
/
container-limits
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
#!/bin/sh
# Detected container limits
# If found these are exposed as the following environment variables:
#
# - CONTAINER_MAX_MEMORY
# - CONTAINER_CORE_LIMIT
#
# This script is meant to be sourced.
ceiling() {
awk -vnumber="$1" -vdiv="$2" '
function ceiling(x){
return x%1 ? int(x)+1 : x
}
BEGIN{
print ceiling(number/div)
}
'
}
# Based on the cgroup limits, figure out the max number of core we should utilize
core_limit() {
local cpu_period_file="/sys/fs/cgroup/cpu/cpu.cfs_period_us"
local cpu_quota_file="/sys/fs/cgroup/cpu/cpu.cfs_quota_us"
if [ -r "${cpu_period_file}" ]; then
local cpu_period="$(cat ${cpu_period_file})"
if [ -r "${cpu_quota_file}" ]; then
local cpu_quota="$(cat ${cpu_quota_file})"
# cfs_quota_us == -1 --> no restrictions
if [ "x$cpu_quota" != "x-1" ]; then
ceiling "$cpu_quota" "$cpu_period"
fi
fi
fi
}
max_memory() {
# High number which is the max limit unti which memory is supposed to be
# unbounded.
local max_mem_unbounded_file="/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes"
local mem_file="/sys/fs/cgroup/memory/memory.limit_in_bytes"
if [ -r "${mem_file}" ] && [ -r "${max_mem_unbounded_file}" ]; then
local max_mem="$(cat ${mem_file})"
local max_mem_unbounded="$(cat ${max_mem_unbounded_file})"
if [ ${max_mem} -lt ${max_mem_unbounded} ]; then
echo "${max_mem}"
else
echo "${max_mem_unbounded}"
fi
else [ -r "${mem_file}" ]
local max_mem="$(cat ${mem_file})"
echo "${max_mem}"
fi
}
limit="$(core_limit)"
if [ x$limit != x ]; then
export CONTAINER_CORE_LIMIT="${limit}"
fi
unset limit
limit="$(max_memory)"
if [ x$limit != x ]; then
export CONTAINER_MAX_MEMORY="$limit"
fi
unset limit