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

Filesystem: get_pids() "safe": scan /proc only once #1978

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

lge
Copy link
Member

@lge lge commented Sep 19, 2024

Caution: advanced shell trickery.

With many processes, scanning /proc can take significant time. Use only one find /proc, feed /proc/**/maps to grep via fifo and xargs, and send matching symlinks to stdout directly.

On a "busy" system, this cuts the time for get_pids() almost in half.

Caution: advanced shell trickery.

With many processes, scanning /proc can take significant time.
Use only one find /proc, feed /proc/**/maps to grep via fifo and xargs,
and send matching symlinks to stdout directly.

On a "busy" system, this cuts the time for get_pids() almost in half.
D=$(mktemp -d "$HA_RSCTMP/Filesystem.get_pids.safe.XXXXXX")
mkfifo "$D/maps"
(
< "$D/maps" xargs -r grep -l " $dir/" | cut -d/ -f3 | uniq &
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

xargs on BSD doesnt have the -l parameter.

FreeBSD manpage: https://man.freebsd.org/cgi/man.cgi?query=xargs
Manpage with POSIX-supported parameters: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/xargs.html

# we can remove the tmpdir and fifo early
rm -rf "$D"
find /proc -path '/proc/[!0-9]*' -prune -o \
-type f -name maps -fprint /dev/fd/3 -o \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-fprint is GNU findutils only.
POSIX manpage: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/find.html

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-lname is not supported by POSIX, but available on *BSD as compatibility option, so no need to worry about that parameter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants