forked from apache/incubator-kie-kogito-images
/
container-limits
76 lines (65 loc) · 1.92 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
70
71
72
73
74
75
76
#!/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.
if [ "${SCRIPT_DEBUG}" = "true" ] ; then
set -x
fi
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_unbounded() {
cat /proc/meminfo | grep 'MemTotal:' | awk '{print $2*1024}'
}
container_memory() {
# High number which is the max limit unit which memory is supposed to be unbounded.
local mem_file="/sys/fs/cgroup/memory/memory.limit_in_bytes"
local max_mem_unbounded="$(max_unbounded)"
if [ -r "${mem_file}" ]; then
local max_mem="$(cat ${mem_file})"
if [ ${max_mem} -lt ${max_mem_unbounded} ]; then
echo "${max_mem}"
fi
fi
}
min() {
printf "%s\n" "$@" | sort -g | head -n1
}
local limit="$(core_limit)"
if [ x$limit != x ]; then
export CONTAINER_CORE_LIMIT="$limit"
fi
local env_core_limit="$(min $CONTAINER_CORE_LIMIT $JAVA_CORE_LIMIT)"
if [ -n "$env_core_limit" ]; then
export CORE_LIMIT="$env_core_limit"
fi
local max_mem="$(container_memory)"
if [ x$max_mem != x ]; then
export CONTAINER_MAX_MEMORY="$max_mem"
fi