Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update to limit the dirty bytes in user's system. #117

Merged
merged 4 commits into from
May 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions src/mountscript.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ AZNFS_CHECK_AZURE_NCONNECT="${AZNFS_CHECK_AZURE_NCONNECT:-1}"
# Default to fixing mount options passed in to help the user.
AZNFS_FIX_MOUNT_OPTIONS="${AZNFS_FIX_MOUNT_OPTIONS:-1}"

# Default to fixing dirty bytes config to help the user.
AZNFS_FIX_DIRTY_BYTES_CONFIG="${AZNFS_FIX_DIRTY_BYTES_CONFIG:-1}"

#
# Use noresvport mount option to allow using non-reserve ports by client.
# This allows much higher number of local ports to be used by NFS client and
Expand Down Expand Up @@ -96,6 +99,63 @@ check_nconnect()
fi
}

#
# Help fix/limit the dirty bytes config of user's machine.
# This is needed on machines with huge amount of RAM which causes the default
# dirty settings to accumulate lot of dirty pages. When lot of dirty pages are
# then flushed to the NFS server, it may cause slowness due to some writes
# timing out. To avoid this we set the dirty config to optimal values.
#
fix_dirty_bytes_config()
{
# Constants for desired settings.
local desired_dirty_bytes=$((8 * 1024 * 1024 * 1024)) # 8 GB in bytes
local desired_dirty_background_bytes=$((4 * 1024 * 1024 * 1024)) # 4 GB in bytes

# Get current settings.
local current_dirty_bytes=$(cat /proc/sys/vm/dirty_bytes 2>/dev/null)
local current_dirty_background_bytes=$(cat /proc/sys/vm/dirty_background_bytes 2>/dev/null)

# Should not happen but added for robustness.
if [ -z "$current_dirty_bytes" -o -z "$current_dirty_background_bytes" ]; then
wecho "current_dirty_bytes=$current_dirty_bytes"
wecho "current_dirty_background_bytes=$current_dirty_background_bytes"
return
fi

# If current dirty bytes are 0, calculate them from the ratio configs.
if [ $current_dirty_background_bytes -eq 0 ]; then
# Get total memory in bytes.
local total_mem_KB=$(grep MemTotal /proc/meminfo | awk '{print $2}')
local total_mem_bytes=$((total_mem_KB * 1024))

local current_dirty_ratio=$(cat /proc/sys/vm/dirty_ratio 2>/dev/null)
local current_dirty_background_ratio=$(cat /proc/sys/vm/dirty_background_ratio 2>/dev/null)

# Should not happen but added for robustness.
if [ -z "$current_dirty_ratio" -o \
-z "$current_dirty_background_ratio" -o \
"$current_dirty_ratio" == "0" -o \
"$current_dirty_background_ratio" == "0" ]; then
wecho "current_dirty_ratio=$current_dirty_ratio"
wecho "current_dirty_background_ratio=$current_dirty_background_ratio"
return
fi

current_dirty_bytes=$((total_mem_bytes * current_dirty_ratio / 100))
current_dirty_background_bytes=$((total_mem_bytes * current_dirty_background_ratio / 100))
fi

# If current dirty byte settings are higher than desired, set to desired.
if [ $desired_dirty_background_bytes -lt $current_dirty_background_bytes ]; then
pecho "Setting /proc/sys/vm/dirty_bytes to $desired_dirty_bytes bytes"
echo $desired_dirty_bytes > /proc/sys/vm/dirty_bytes

pecho "Setting /proc/sys/vm/dirty_background_bytes to $desired_dirty_background_bytes bytes"
echo $desired_dirty_background_bytes > /proc/sys/vm/dirty_background_bytes
fi
}

#
# Help fix the mount options passed in by user.
#
Expand Down Expand Up @@ -160,6 +220,18 @@ fix_mount_options()
fi
fi

matchstr="\<retrans\>=([0-9]+)"
if ! [[ "$MOUNT_OPTIONS" =~ $matchstr ]]; then
pecho "Adding retrans=6 mount option!"
MOUNT_OPTIONS="$MOUNT_OPTIONS,retrans=6"
else
value="${BASH_REMATCH[1]}"
if [ $value -lt 6 ]; then
pecho "Suboptimal retrans=$value mount option, setting retrans=6!"
MOUNT_OPTIONS=$(echo "$MOUNT_OPTIONS" | sed "s/\<retrans\>=$value/retrans=6/g")
fi
fi

if [ "$AZNFS_USE_NORESVPORT" == "1" ]; then
matchstr="\<resvport\>|\<noresvport\>"
if ! [[ "$MOUNT_OPTIONS" =~ $matchstr ]]; then
Expand Down Expand Up @@ -720,6 +792,13 @@ if [ "$AZNFS_FIX_MOUNT_OPTIONS" == "1" ]; then
fix_mount_options
fi

#
# Fix dirty bytes config if needed.
#
if [ "$AZNFS_FIX_DIRTY_BYTES_CONFIG" == "1" ]; then
fix_dirty_bytes_config
fi

#
# Get proxy IP to use for this nfs_ip.
# It'll ensure an appropriate entry is added to MOUNTMAP if not already added,
Expand Down