#!/usr/bin/env sh set -eu OUTPUT_FILE="${1:-}" if [ -z "$OUTPUT_FILE" ]; then echo "用法: scripts/docker-limits.sh " >&2 exit 1 fi detect_cpus() { if command -v nproc >/dev/null 2>&1; then nproc return fi if command -v getconf >/dev/null 2>&1; then getconf _NPROCESSORS_ONLN return fi if command -v sysctl >/dev/null 2>&1; then sysctl -n hw.ncpu return fi echo 2 } detect_memory_mb() { if [ -r /proc/meminfo ]; then awk '/MemTotal/ { printf "%d\n", $2 / 1024 }' /proc/meminfo return fi if command -v sysctl >/dev/null 2>&1; then sysctl -n hw.memsize | awk '{ printf "%d\n", $1 / 1024 / 1024 }' return fi echo 4096 } cpu_value() { awk -v total="$1" -v share="$2" 'BEGIN { value = total * 0.80 * share / 100; if (value < 0.10) value = 0.10; printf "%.2f", value; }' } memory_value() { awk -v total="$1" -v share="$2" -v minimum="${3:-96}" 'BEGIN { value = int(total * 0.65 * share / 100); if (value < minimum) value = minimum; printf "%dm", value; }' } integer_at_least_one() { awk -v value="$1" 'BEGIN { n = int(value); if (n < 1) n = 1; printf "%d", n; }' } CPUS="$(detect_cpus)" MEMORY_MB="$(detect_memory_mb)" BUILD_CPUS="$(integer_at_least_one "$(awk -v total="$CPUS" 'BEGIN { print total * 0.80 }')")" MAVEN_HEAP_MB="$(awk -v total="$MEMORY_MB" 'BEGIN { value = int(total * 0.65 * 0.41 * 0.70); if (value < 768) value = 768; printf "%d", value; }')" NODE_OLD_SPACE_MB="$(awk -v total="$MEMORY_MB" 'BEGIN { value = int(total * 0.65 * 0.25); if (value < 1024) value = 1024; if (value > 3072) value = 3072; printf "%d", value; }')" mkdir -p "$(dirname "$OUTPUT_FILE")" cat > "$OUTPUT_FILE" <