chore(host_scanner): improve cleanup algorithm in scans#1226
Open
Molter73 wants to merge 1 commit into
Open
Conversation
In order to remove items from the inode maps for files that may have been deleted and may have missed events, or paths that are no longer monitored due to a configuration change, we used to: * Iterate over the userspace map. * Check if the path matches the configured globs. * Check if the path still exists in the filesystem (via statx). * If either of the previous checks failed, we remove the inode from the maps After this cleanup is done, we proceed to glob the configured paths and check all of them on disk, adding any missing inodes and updating any changed paths. The most common situation for these scans is that most inodes should hit (unless the configuration is changed or massive changes to the fs were missed), which means we are essentially doing two statx calls per element in the inode (once during the cleanup iteration and another during the glob expansion). This change does a few things: * Add a marker to each tracked inode (a counter that increments every scan iteration). * Do the glob expansion first, marking each found inode with the current scan number. * Once we are done exploring the glob expansion, iterate over the map and remove any entries that have an out of date scan marker (i.e: they were not found as part of the glob expansion). This saves us some `statx` calls during scans, making them faster. Of note, this change works because scanning pauses processing of events until the scan is done, otherwise it could be subject to race conditions.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## mauro/fix/miscellaneous-host-scanner-improvements #1226 +/- ##
=====================================================================================
- Coverage 32.94% 32.80% -0.15%
=====================================================================================
Files 21 21
Lines 2996 3009 +13
Branches 2996 3009 +13
=====================================================================================
Hits 987 987
- Misses 2006 2019 +13
Partials 3 3 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
In order to remove items from the inode maps for files that may have been deleted and may have missed events, or paths that are no longer monitored due to a configuration change, we used to:
After this cleanup is done, we proceed to glob the configured paths and check all of them on disk, adding any missing inodes and updating any changed paths. The most common situation for these scans is that most inodes should hit (unless the configuration is changed or massive changes to the fs were missed), which means we are essentially doing two statx calls per element in the inode (once during the cleanup iteration and another during the glob expansion).
This change does a few things:
This saves us some
statxcalls during scans, making them faster.Of note, this change works because scanning pauses processing of events until the scan is done, otherwise it could be subject to race conditions.
This is a small follow-up to #1221.
Checklist
Automated testing
If any of these don't apply, please comment below.
Testing Performed
Reduced number of syscalls
Running fact with the following command:
FACT_LOGLEVEL=info RUST_BACKTRACE=1 cargo srun --bin fact --all-features -- -p '/etc/**/*:/etc/' --inodes-max=2097152 --expose-metrics --scan-interval 30, then usingperf stat -e 'syscalls:sys_enter_statx,syscalls:sys_enter_bpf' -p "$(pgrep fact)" -- sleep 30capturing a single scan. No events generated during the run, no modifications to the /etc directory, 25966 inodes tracked for all the scans.Before changes:
After changes:
Average scan time
Average scan time before changes: 114.19ms
Average scan time after changes: 79.35ms