workflow_perf_monitor/fd/
mod.rs

1//! Get file descriptor(say handle for windows) numbers for current process.
2//!
3//! ```
4//! use perf_monitor::fd::fd_count_cur;
5//!
6//! let count = fd_count_cur().unwrap();
7//! ```
8//!
9//! ## Bottom Layer Interface
10//!
11//! - Windows: [GetProcessHandleCount](https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getprocesshandlecount)
12//! - Linux & android: [/proc/{pid}/fd](https://man7.org/linux/man-pages/man5/proc.5.html)
13//! - MacOS: [/dev/fd](https://www.informit.com/articles/article.aspx?p=99706&seqNum=15)
14//! - iOS Unfortunately there is no api to retrieve the fd count of the process for iOS.
15//! Following links contains a available method, but it's complicated and
16//! inefficient. <https://stackoverflow.com/questions/4083608/on-ios-iphone-too-many-open-files-need-to-list-open-files-like-lsof>
17//!
18//! ## Other Process
19//!
20//! For windows, linux and android(maybe), it is possible to get fd number of other process.
21//! However we didn't re-export these function because macos and ios is not supported.
22//!
23
24#[cfg(target_os = "windows")]
25mod windows;
26#[cfg(target_os = "windows")]
27use windows as platform;
28
29#[cfg(any(target_os = "linux", target_os = "android"))]
30mod android_linux;
31#[cfg(any(target_os = "linux", target_os = "android"))]
32use android_linux as platform;
33
34#[cfg(all(target_os = "macos", not(feature = "darwin_private")))]
35mod macos;
36#[cfg(all(target_os = "macos", not(feature = "darwin_private")))]
37use macos as platform;
38
39#[cfg(all(target_os = "ios", not(feature = "darwin_private")))]
40mod ios;
41#[cfg(all(target_os = "ios", not(feature = "darwin_private")))]
42use ios as platform;
43
44#[cfg(all(
45    any(target_os = "macos", target_os = "ios"),
46    feature = "darwin_private"
47))]
48mod darwin_private;
49#[cfg(all(
50    any(target_os = "macos", target_os = "ios"),
51    feature = "darwin_private"
52))]
53use darwin_private as platform;
54
55/// return the fd count of current process
56#[inline]
57pub fn fd_count_cur() -> std::io::Result<usize> {
58    platform::fd_count_cur().map(|count| count as usize)
59}