Skip to content

Commit

Permalink
feat: help configure proxy memory allocation
Browse files Browse the repository at this point in the history
PR comment fixes

Refs: XRDDEV-55
  • Loading branch information
ovidijusnortal committed Feb 11, 2025
1 parent 8697ee9 commit f8a4dc8
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,30 @@ die () {
exit 1
}

function to_megabytes() {
if [[ $1 =~ ^([0-9]+)m$ ]]; then
echo "${BASH_REMATCH[1]}"
elif [[ $1 =~ ^([0-9]+)g$ ]]; then
echo "${BASH_REMATCH[1]} * 1024" | bc
size_rx="([0-9]+)([kKmMgG]?)"
initial_heap_size_rx="(-Xms|-XX:InitialHeapSize=)($size_rx)"
max_heap_size_rx="(-Xmx|-XX:MaxHeapSize=)($size_rx)"

function to_bytes() {
local -r regex="^${size_rx}$"
if [[ $1 =~ $regex ]]; then
local -r value=${BASH_REMATCH[1]}
local -r unit=${BASH_REMATCH[2]}

case $unit in
k|K)
echo "$value * 1024" | bc
;;
m|M)
echo "$value * 1024 * 1024" | bc
;;
g|G)
echo "$value * 1024 * 1024 * 1024" | bc
;;
*)
echo $value
;;
esac
else
echo ""
fi
Expand All @@ -29,19 +48,15 @@ apply_memory_config(){
if [ -z "$params" ]; then
echo "$1=$xms $xmx" >> "$params_file"
else
local pattern="-Xms[0-9]+[mg]"
if [[ "$params" =~ $pattern ]]; then
sed -i -E "/^$1=/s/$pattern/$xms/" "$params_file"
else
sed -i "s/^$1=.*/\0 $xms/" "$params_file"
if [[ "$params" =~ $initial_heap_size_rx ]]; then
sed -i -E "/^$1=/s/$initial_heap_size_rx ?//g" "$params_file"
fi
sed -i "s/^$1=.*/\0 $xms/" "$params_file"

local pattern="-Xmx[0-9]+[mg]"
if [[ "$params" =~ $pattern ]]; then
sed -i -E "/^$1=/s/$pattern/$xmx/" "$params_file"
else
sed -i "s/^$1=.*/\0 $xmx/" "$params_file"
if [[ "$params" =~ $max_heap_size_rx ]]; then
sed -i -E "/^$1=/s/$max_heap_size_rx ?//g" "$params_file"
fi
sed -i "s/^$1=.*/\0 $xmx/" "$params_file"
fi

local -r updated_params="$(get_params_line "$1")"
Expand All @@ -54,36 +69,55 @@ apply_memory_config(){

}

function to_gigabytes_str() {
local mb=$1
local gb=""
if [[ $1 =~ ^([0-9]+)g$ ]]; then
gb="${BASH_REMATCH[1]}"
elif [[ $1 =~ ^([0-9]+)m$ ]]; then
mb="${BASH_REMATCH[1]}"
fi
function to_unit_str() {
local -r as_bytes="$(to_bytes $1)"

if [[ -z "$gb" && "$mb" -lt "1024" ]]; then
echo "${mb}m"
elif [[ -z "$gb" && "$mb" -ge "1024" ]]; then
gb=$(($mb / 1024))
echo "${gb}g"
if [ -z "$as_bytes" ]; then
echo ""
else
echo "${gb}g"
local value="$as_bytes"
local unit=""

local bytes_in_k="1024"
local bytes_in_k_half="512"
local bytes_in_m=$(echo "$bytes_in_k * 1024" | bc)
local bytes_in_m_half=$(echo "$bytes_in_k_half * 1024" | bc)
local bytes_in_g=$(echo "$bytes_in_m * 1024" | bc)
local bytes_in_g_half=$(echo "$bytes_in_m_half * 1024" | bc)

if [ "$value" -gt "$bytes_in_g" ]; then
#value=$(($value / $bytes_in_g))
value=$(echo "scale=0; (($value+$bytes_in_g_half)/$bytes_in_g)" | bc)
unit="g"
elif [ "$value" -gt "$bytes_in_m" ]; then
#value=$(($value / $bytes_in_m))
value=$(echo "scale=0; (($value+$bytes_in_m_half)/$bytes_in_m)" | bc)
unit="m"
elif [ "$value" -gt "$bytes_in_k" ]; then
#value=$(($value / $bytes_in_k))
value=$(echo "scale=0; (($value+$bytes_in_k_half)/$bytes_in_k)" | bc)
unit="k"
fi

echo "${value}${unit}"
fi
}

function find_xms() {
if [[ "$1" =~ -Xms([0-9]+[mg]) ]]; then
echo "${BASH_REMATCH[1]}"
local -r last_match=$(echo "$1" | grep -o -E "$initial_heap_size_rx" | tail -n 1)

if [[ "$last_match" =~ $initial_heap_size_rx ]]; then
echo "${BASH_REMATCH[2]}"
else
echo ""
fi
}

function find_xmx() {
if [[ "$1" =~ -Xmx([0-9]+[mg]) ]]; then
echo "${BASH_REMATCH[1]}"
local -r last_match=$(echo "$1" | grep -o -E "$max_heap_size_rx" | tail -n 1)

if [[ "$last_match" =~ $max_heap_size_rx ]]; then
echo "${BASH_REMATCH[2]}"
else
echo ""
fi
Expand All @@ -109,17 +143,15 @@ function get_total_memory() {
fi

if [[ -z "$memory_limit" || "$memory_limit" == "max" ]]; then
total_memory=$(free --mega | awk '/Mem:/ {print $2}')
else
total_memory=$(($memory_limit / 1024 / 1024))
total_memory="$(free -b | awk '/Mem:/ {print $2}')"
fi

echo "$total_memory"
}

#Returns used memory in megabytes
function get_used_memory() {
echo "$(free --mega | awk '/Mem:/ {print $3}')"
echo "$(free -b | awk '/Mem:/ {print $3}')"
}

#Calculates memory based on total memory and given percentiles
Expand All @@ -133,34 +165,26 @@ function get_used_memory() {
memory_config=()
calculate_recommended_memory() {
local -r total_mem="$1"
local -r min_val=$(to_megabytes "$2")
local -r max_val=$(to_megabytes "$3")
local -r min_val=$(to_bytes "$2")
local -r max_val=$(to_bytes "$3")

local result="$max_val"

for pair in "${memory_config[@]}"; do

IFS=':' read -ra config <<< "$pair"
local limit=$(to_megabytes "${config[0]}")
local limit=$(to_bytes "${config[0]}")
if [[ "$total_mem" -lt "$limit" ]]; then
result=$(apply_percentile "${config[1]}" "$total_mem")
break
fi
done

if [[ "$result" -gt "$max_val" ]]; then
result="$max_val"
fi

if [[ "$result" -lt "$min_val" ]]; then
result="$min_val"
fi

if [[ "$result" -ge "2048" ]]; then
#round it up to nearest gb
result=$(echo "scale=0; (($result+512)/1024)" | bc)
echo "${result}g"
echo "$max_val"
elif [[ "$result" -lt "$min_val" ]]; then
echo "$min_val"
else
echo "${result}m"
echo "$(to_unit_str $result)"
fi
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ default_xms="100m"
default_xmx="512m"

verify_config(){
local -r xms=$(to_megabytes "$1")
local -r xmx=$(to_megabytes "$2")
local -r xms=$(to_bytes "$1")
local -r xmx=$(to_bytes "$2")

if [ -z "$xms" ]; then
die "Invalid first argument. Must be in <number><m|g> format, for example 128m"
die "Invalid first argument. Must be in <number>[k|m|g] format, for example 128m"
fi

if [ -z "$xmx" ]; then
die "Invalid second argument. Must be in <number><m|g> format, for example 3g"
die "Invalid second argument. Must be in <number>[k|m|g] format, for example 3g"
fi

if [ "$xms" -ge "$xmx" ]; then
Expand Down Expand Up @@ -94,7 +94,7 @@ display_status(){
local -r recommended_xms=$(get_recommended_xms)
local -r recommended_xmx=$(get_recommended_xmx)

local -r total_memory_str=$(to_gigabytes_str "$total_memory")
local -r total_memory_str=$(to_unit_str "$total_memory")
local -r used_memory_str=$(($used_memory * 100 / total_memory))

cat << EOF
Expand Down

0 comments on commit f8a4dc8

Please sign in to comment.