Enable oppportunistic fd counting fast path#234
Merged
eminence merged 2 commits intoeminence:masterfrom Jan 15, 2023
Merged
Conversation
Existing slow path takes ~52000us of CPU time on my laptop to count 100k open files. Compare this to just 13us for the baseline, when no extra files are open by a program: ``` fd count = 7, elapsed = 13 us fd count = 100007, elapsed = 51863 us ``` Adding fastpath from Linux v6.2 makes it fast: ``` fd count = 4, elapsed = 2 us fd count = 100004, elapsed = 8 us ``` This is before taking in account any lock contention effects in the kernel if you try to count files from multiple threads concurrently, which makes the slow path even slower, burning a lot more CPU in the process. See: * torvalds/linux@f1f1f2569901 I used the following code to benchmark: ```rust use procfs::process::Process; use std::{fs::File, time::Instant}; fn main() { let myself = Process::myself().unwrap(); let started = Instant::now(); let count = myself.fd_count().unwrap(); let elapsed_us = started.elapsed().as_micros(); eprintln!("fd count = {}, elapsed = {} us", count, elapsed_us); let files = (0..100_000) .map(|_| File::open("/etc/hosts").unwrap()) .collect::<Vec<_>>(); let started = Instant::now(); let count = myself.fd_count().unwrap(); let elapsed_us = started.elapsed().as_micros(); eprintln!("fd count = {}, elapsed = {} us", count, elapsed_us); drop(files); } ```
Owner
|
Looks nice, thanks. I just realized that we don't have any tests for |
Contributor
Author
|
Sure, I added a basic test to make sure that the count increments and decrements as expected. It needs to run single threaded to avoid picking up fds from other threads. Let me know if you had something else in mind. |
Owner
|
That's perfect, thank you! |
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.
Existing slow path takes ~52000us of CPU time on my laptop to count 100k open files. Compare this to just 13us for the baseline, when no extra files are open by a program:
Adding fastpath from Linux v6.2 makes it fast:
This is before taking in account any lock contention effects in the kernel if you try to count files from multiple threads concurrently, which makes the slow path even slower, burning a lot more CPU in the process. See:
I used the following code to benchmark:
See also: prometheus/procfs#486