Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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