sysinfo/unix/apple/
process.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::fmt;
4
5pub(crate) use crate::sys::inner::process::*;
6use crate::ProcessStatus;
7
8#[doc(hidden)]
9impl From<u32> for ProcessStatus {
10    fn from(status: u32) -> ProcessStatus {
11        match status {
12            libc::SIDL => ProcessStatus::Idle,
13            libc::SRUN => ProcessStatus::Run,
14            libc::SSLEEP => ProcessStatus::Sleep,
15            libc::SSTOP => ProcessStatus::Stop,
16            libc::SZOMB => ProcessStatus::Zombie,
17            x => ProcessStatus::Unknown(x),
18        }
19    }
20}
21
22#[doc(hidden)]
23impl From<ThreadStatus> for ProcessStatus {
24    fn from(status: ThreadStatus) -> ProcessStatus {
25        match status {
26            ThreadStatus::Running => ProcessStatus::Run,
27            ThreadStatus::Stopped => ProcessStatus::Stop,
28            ThreadStatus::Waiting => ProcessStatus::Sleep,
29            ThreadStatus::Uninterruptible => ProcessStatus::Dead,
30            ThreadStatus::Halted => ProcessStatus::Parked,
31            ThreadStatus::Unknown(x) => ProcessStatus::Unknown(x as _),
32        }
33    }
34}
35
36impl fmt::Display for ProcessStatus {
37    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
38        f.write_str(match *self {
39            ProcessStatus::Idle => "Idle",
40            ProcessStatus::Run => "Runnable",
41            ProcessStatus::Sleep => "Sleeping",
42            ProcessStatus::Stop => "Stopped",
43            ProcessStatus::Zombie => "Zombie",
44            _ => "Unknown",
45        })
46    }
47}
48
49/// Enum describing the different status of a thread.
50#[derive(Clone, Copy, Debug, PartialEq, Eq)]
51pub(crate) enum ThreadStatus {
52    /// Thread is running normally.
53    Running,
54    /// Thread is stopped.
55    Stopped,
56    /// Thread is waiting normally.
57    Waiting,
58    /// Thread is in an uninterruptible wait
59    Uninterruptible,
60    /// Thread is halted at a clean point.
61    Halted,
62    /// Unknown.
63    Unknown(i32),
64}
65
66impl From<i32> for ThreadStatus {
67    fn from(status: i32) -> ThreadStatus {
68        match status {
69            libc::TH_STATE_RUNNING => ThreadStatus::Running,
70            libc::TH_STATE_STOPPED => ThreadStatus::Stopped,
71            libc::TH_STATE_WAITING => ThreadStatus::Waiting,
72            libc::TH_STATE_UNINTERRUPTIBLE => ThreadStatus::Uninterruptible,
73            libc::TH_STATE_HALTED => ThreadStatus::Halted,
74            x => ThreadStatus::Unknown(x),
75        }
76    }
77}
78
79impl ProcessInner {
80    pub(crate) fn open_files(&self) -> Option<usize> {
81        let buffer_size_bytes = unsafe {
82            libc::proc_pidinfo(
83                self.pid().0,
84                libc::PROC_PIDLISTFDS,
85                0,
86                std::ptr::null_mut(),
87                0,
88            )
89        };
90
91        if buffer_size_bytes < 0 {
92            sysinfo_debug!("proc_pidinfo failed");
93            None
94        } else {
95            Some(buffer_size_bytes as usize / std::mem::size_of::<libc::proc_fdinfo>())
96        }
97    }
98
99    // FIXME: Should query the value, because it can be changed with setrlimit and other means.
100    // For now, cannot find where to get this information sadly...
101    pub(crate) fn open_files_limit(&self) -> Option<usize> {
102        crate::System::open_files_limit()
103    }
104}