Skip to content

Instantly share code, notes, and snippets.

@kyze8439690
Forked from jhansche/watch-fds.sh
Created April 16, 2021 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kyze8439690/09f66f1856c2ce9e07a4ea9615ba1baf to your computer and use it in GitHub Desktop.
Save kyze8439690/09f66f1856c2ce9e07a4ea9615ba1baf to your computer and use it in GitHub Desktop.
Simple script to monitor the number open file descriptors for an Android application.
#!/bin/sh
# Usage: ./watch-fds.sh <application_id> [delay_secs = 5]
APP_ID=${1:?missing application id}
DELAY=$(( ${2:-5} ))
DEVICE_LIMIT=$(( $(adb shell ulimit -n) ))
WARN_THRESHOLD=$(( ${DEVICE_LIMIT} / 3 ))
echo "Will warn at ${WARN_THRESHOLD}"
APP_PID=$(( $(adb shell ps | grep -m1 "${APP_ID}" | awk '{print $2}') ))
echo "Watching application ${APP_ID} <${APP_PID}>. Press CTRL+C to stop."
fds=""
high_mark=0
while [ true ]; do
app_proc=$(( $(adb shell ps ${APP_PID} | grep -c ${APP_ID}) ))
if [ ${app_proc} -ne 1 ]; then
echo "Process ${APP_ID} <${APP_PID}> has died."
echo "Last list of File Descriptors:"
echo "${fds}"
exit
fi
fds="$(adb shell run-as ${APP_ID} ls -l /proc/${APP_PID}/fd 2>/dev/null)"
fd_count=$(( $(wc -l <<< "${fds}") ))
echo ${fd_count}
if [ ${fd_count} -ge ${WARN_THRESHOLD} -a ${fd_count} -gt ${high_mark} ]; then
echo " *** *** *** "
echo "Warning: FD count > warning threshold: ${fd_count}"
# echo "Current File Descriptors:"
# echo "${fds}"
echo " *** *** *** "
echo ""
# then ratchet the highwater mark
high_mark=$(( ${fd_count} ))
fi
sleep ${DELAY}
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment