Skip to content

Commit 869f4c4

Browse files
author
Charlotte Brandhorst-Satzkorn
committed
src,unix: expose cpu total_time
This change exposes cpu total_time for the manual cpu method. This change is required to expose cpu usage seconds in metrics. Updates edera-dev/protect#1107 Signed-off-by: Charlotte Brandhorst-Satzkorn <charlotte@edera.dev>
1 parent 57876c0 commit 869f4c4

File tree

6 files changed

+46
-1
lines changed

6 files changed

+46
-1
lines changed

src/common/system.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2334,7 +2334,7 @@ pub enum ProcessesToUpdate<'a> {
23342334
/// information from `/proc/<pid>/` as well as all the information from `/proc/<pid>/task/<tid>/`
23352335
/// folders. This makes the refresh mechanism a lot slower depending on the number of tasks
23362336
/// each process has.
2337-
///
2337+
///
23382338
/// If you don't care about tasks information, use `ProcessRefreshKind::everything().without_tasks()`
23392339
/// as much as possible.
23402340
///
@@ -2777,6 +2777,22 @@ impl Cpu {
27772777
self.inner.cpu_usage()
27782778
}
27792779

2780+
/// Returns this CPU's total_time.
2781+
///
2782+
/// ```no_run
2783+
/// use sysinfo::{System, RefreshKind, CpuRefreshKind};
2784+
///
2785+
/// let s = System::new_with_specifics(
2786+
/// RefreshKind::nothing().with_cpu(CpuRefreshKind::everything()),
2787+
/// );
2788+
/// for cpu in s.cpus() {
2789+
/// println!("{}", cpu.cpu_total_time());
2790+
/// }
2791+
/// ```
2792+
pub fn cpu_total_time(&self) -> u64 {
2793+
self.inner.cpu_total_time()
2794+
}
2795+
27802796
/// Returns this CPU's name.
27812797
///
27822798
/// ```no_run

src/unix/apple/cpu.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ pub(crate) struct CpuUsage {
115115
data: Arc<CpuData>,
116116
// Cannot be frequency for each CPU apparently so we store it in the CPU usage...
117117
frequency: u64,
118+
total_time: u64,
118119
}
119120

120121
impl CpuUsage {
@@ -123,13 +124,18 @@ impl CpuUsage {
123124
percent: 0.,
124125
data: Arc::new(CpuData::new(std::ptr::null_mut(), 0)),
125126
frequency: 0,
127+
total_time: 0,
126128
}
127129
}
128130

129131
pub(crate) fn percent(&self) -> f32 {
130132
self.percent
131133
}
132134

135+
pub(crate) fn total_time(&self) -> u64 {
136+
self.total_time
137+
}
138+
133139
pub(crate) fn set_cpu_usage(&mut self, value: f32) {
134140
self.percent = value;
135141
}
@@ -149,13 +155,15 @@ impl CpuInner {
149155
frequency: u64,
150156
vendor_id: String,
151157
brand: String,
158+
total_time: u64,
152159
) -> Self {
153160
Self {
154161
name,
155162
usage: CpuUsage {
156163
percent: 0.,
157164
data: cpu_data,
158165
frequency,
166+
total_time,
159167
},
160168
vendor_id,
161169
brand,
@@ -183,6 +191,10 @@ impl CpuInner {
183191
self.usage.percent()
184192
}
185193

194+
pub(crate) fn cpu_total_time(&self) -> u64 {
195+
self.usage.total_time()
196+
}
197+
186198
pub(crate) fn name(&self) -> &str {
187199
&self.name
188200
}

src/unix/freebsd/cpu.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ impl CpuInner {
135135
self.cpu_usage
136136
}
137137

138+
pub(crate) fn cpu_total_time(&self) -> u64 {
139+
self.usage.total_time()
140+
}
141+
138142
pub(crate) fn name(&self) -> &str {
139143
&self.name
140144
}

src/unix/linux/cpu.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,10 @@ impl CpuInner {
395395
self.usage.percent
396396
}
397397

398+
pub(crate) fn cpu_total_time(&self) -> u64 {
399+
self.usage.total_time
400+
}
401+
398402
pub(crate) fn name(&self) -> &str {
399403
&self.name
400404
}

src/unknown/cpu.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ impl CpuInner {
77
0.0
88
}
99

10+
pub(crate) fn cpu_total_time(&self) -> u64 {
11+
0
12+
}
13+
1014
pub(crate) fn name(&self) -> &str {
1115
""
1216
}

src/windows/cpu.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ impl CpusWrapper {
308308

309309
pub(crate) struct CpuUsage {
310310
percent: f32,
311+
total_time: u64,
311312
pub(crate) key_used: Option<KeyHandler>,
312313
}
313314

@@ -330,6 +331,10 @@ impl CpuInner {
330331
self.usage.percent
331332
}
332333

334+
pub(crate) fn cpu_total_time(&self) -> u64 {
335+
self.usage.total_time()
336+
}
337+
333338
pub(crate) fn name(&self) -> &str {
334339
&self.name
335340
}

0 commit comments

Comments
 (0)