Skip to content

Commit

Permalink
Merge pull request #234 from bobrik/ivan/fastpath
Browse files Browse the repository at this point in the history
Enable oppportunistic fd counting fast path
  • Loading branch information
eminence authored Jan 15, 2023
2 parents a3fb995 + 621886c commit e441e71
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/process/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,15 @@ impl Process {
///
/// Calling this function is more efficient than calling `fd().unwrap().count()`
pub fn fd_count(&self) -> ProcResult<usize> {
// Use fast path if available (Linux v6.2): /~https://github.com/torvalds/linux/commit/f1f1f2569901
let stat = wrap_io_error!(
self.root.join("fd"),
rustix::fs::statat(&self.fd, "fd", AtFlags::SYMLINK_NOFOLLOW)
)?;
if stat.st_size > 0 {
return Ok(stat.st_size as usize);
}

let fds = wrap_io_error!(
self.root.join("fd"),
rustix::fs::openat(
Expand Down
27 changes: 27 additions & 0 deletions src/process/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,33 @@ fn test_proc_fds() {
}
}

#[test]
fn test_proc_fd_count_runsinglethread() {
let myself = Process::myself().unwrap();

let before = myself.fd_count().unwrap();

let one = File::open("/proc/self").unwrap();
let two = File::open("/proc/self/status").unwrap();

let after = myself.fd_count().unwrap();

assert_eq!(
before + 2,
after,
"opened two files and expected {} open fds, saw {}",
before + 2,
after
);

drop(one);
drop(two);

let after_closing = myself.fd_count().unwrap();

assert_eq!(before, after_closing);
}

#[test]
fn test_proc_fd() {
let myself = Process::myself().unwrap();
Expand Down

0 comments on commit e441e71

Please sign in to comment.