-
-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathstate.rs
More file actions
152 lines (140 loc) · 4.4 KB
/
Copy pathstate.rs
File metadata and controls
152 lines (140 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use crate::process::ProcessInfo;
use crate::{column_default, Column};
use std::cmp;
use std::collections::HashMap;
pub struct State {
header: String,
unit: String,
fmt_contents: HashMap<i32, String>,
raw_contents: HashMap<i32, String>,
width: usize,
}
impl State {
pub fn new(header: Option<String>) -> Self {
let header = header.unwrap_or_else(|| String::from("State"));
let unit = String::new();
Self {
fmt_contents: HashMap::new(),
raw_contents: HashMap::new(),
width: 0,
header,
unit,
}
}
}
#[cfg(any(target_os = "linux", target_os = "android"))]
impl Column for State {
fn add(&mut self, proc: &ProcessInfo) {
let fmt_content = format!("{}", proc.curr_proc.stat().state);
let raw_content = fmt_content.clone();
self.fmt_contents.insert(proc.pid, fmt_content);
self.raw_contents.insert(proc.pid, raw_content);
}
column_default!(String, false);
}
#[cfg(target_os = "macos")]
impl Column for State {
fn add(&mut self, proc: &ProcessInfo) {
let mut state = 7;
for t in &proc.curr_threads {
let s = match t.pth_run_state {
1 => 1, // TH_STATE_RUNNING
2 => 5, // TH_STATE_STOPPED
3 => {
if t.pth_sleep_time > 20 {
4
} else {
3
}
} // TH_STATE_WAITING
4 => 2, // TH_STATE_UNINTERRUPTIBLE
5 => 6, // TH_STATE_HALTED
_ => 7,
};
state = cmp::min(s, state);
}
let state = match state {
0 => "",
1 => "R",
2 => "U",
3 => "S",
4 => "I",
5 => "T",
6 => "H",
_ => "?",
};
let fmt_content = state.to_string();
let raw_content = fmt_content.clone();
self.fmt_contents.insert(proc.pid, fmt_content);
self.raw_contents.insert(proc.pid, raw_content);
}
column_default!(String, false);
}
#[cfg(target_os = "freebsd")]
impl Column for State {
fn add(&mut self, proc: &ProcessInfo) {
let info = &proc.curr_proc.info;
let flag = info.flag;
let tdflags = info.tdflags;
let cr_flags = info.cr_flags;
let kiflag = info.kiflag;
let mut state = match info.stat {
libc::SSTOP => "T",
libc::SSLEEP => {
if (tdflags & libc::TDF_SINTR as i64) != 0 {
if info.slptime >= 20 {
"I"
} else {
"S"
}
} else {
"D"
}
}
libc::SRUN | libc::SIDL => "R",
libc::SWAIT => "W",
libc::SLOCK => "L",
libc::SZOMB => "Z",
_ => "?",
}
.to_string();
if (flag & libc::P_INMEM as i64) == 0 {
state.push_str("W");
}
if info.nice < libc::NZERO as i8 || info.pri.class == bsd_kvm_sys::PRI_REALTIME as u8 {
state.push_str("<");
}
if info.nice > libc::NZERO as i8 || info.pri.class == bsd_kvm_sys::PRI_IDLE as u8 {
state.push_str("N");
}
if (flag & libc::P_TRACED as i64) != 0 {
state.push_str("X");
}
if (flag & libc::P_WEXIT as i64) != 0 && info.stat != libc::SZOMB as std::os::raw::c_char {
state.push_str("E");
}
if (flag & libc::P_PPWAIT as i64) != 0 {
state.push_str("V");
}
if (flag & libc::P_SYSTEM as i64) != 0 || info.lock > 0 {
state.push_str("L");
}
if (cr_flags & libc::KI_CRF_CAPABILITY_MODE as u32) != 0 {
state.push_str("C");
}
if (kiflag & libc::KI_SLEADER as i64) != 0 {
state.push_str("s");
}
if (flag & libc::P_CONTROLT as i64) != 0 && info.pgid == info.tpgid {
state.push_str("+");
}
if (flag & libc::P_JAILED as i64) != 0 {
state.push_str("J");
}
let fmt_content = state;
let raw_content = fmt_content.clone();
self.fmt_contents.insert(proc.pid, fmt_content);
self.raw_contents.insert(proc.pid, raw_content);
}
column_default!(String, false);
}