-
Notifications
You must be signed in to change notification settings - Fork 103
/
lib.rs
1337 lines (1175 loc) · 40.5 KB
/
lib.rs
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Don't throw clippy warnings for manual string stripping.
// The suggested fix with `strip_prefix` removes support for Rust 1.33 and 1.38
#![allow(clippy::unknown_clippy_lints)]
#![allow(clippy::manual_strip)]
// Don't throw rustc lint warnings for the deprecated name `intra_doc_link_resolution_failure`.
// The suggested rename to `broken_intra_doc_links` removes support for Rust 1.33 and 1.38.
#![allow(renamed_and_removed_lints)]
#![deny(intra_doc_link_resolution_failure)]
//! This crate provides to an interface into the linux `procfs` filesystem, usually mounted at
//! `/proc`.
//!
//! This is a pseudo-filesystem which is available on most every linux system and provides an
//! interface to kernel data structures.
//!
//!
//! # Kernel support
//!
//! Not all fields/data are available in each kernel. Some fields were added in specific kernel
//! releases, and other fields are only present in certain kernel configuration options are
//! enabled. These are represented as `Option` fields in this crate.
//!
//! This crate aims to support all 2.6 kernels (and newer). WSL2 is also supported.
//!
//! # Documentation
//!
//! In almost all cases, the documentation is taken from the
//! [`proc.5`](http://man7.org/linux/man-pages/man5/proc.5.html) manual page. This means that
//! sometimes the style of writing is not very "rusty", or may do things like reference related files
//! (instead of referencing related structs). Contributions to improve this are welcome.
//!
//! # Panicing
//!
//! While previous versions of the library could panic, this current version aims to be panic-free
//! in a many situations as possible. Whenever the procfs crate encounters a bug in its own
//! parsing code, it will return an [`InternalError`](enum.ProcError.html#variant.InternalError) error. This should be considered a
//! bug and should be [reported](https://github.com/eminence/procfs). If you encounter a panic,
//! please report that as well.
//!
//! # Cargo features
//!
//! The following cargo features are available:
//!
//! * `chrono` -- Default. Optional. This feature enables a few methods that return values as `DateTime` objects.
//! * `backtrace` -- Optional. This feature lets you get a stack trace whenever an `InternalError` is raised.
//!
//! # Examples
//!
//! Examples can be found in the various modules shown below, or in the
//! [examples](https://github.com/eminence/procfs/tree/master/examples) folder of the code repository.
//!
use bitflags::bitflags;
use lazy_static::lazy_static;
use libc::pid_t;
use libc::sysconf;
use libc::{_SC_CLK_TCK, _SC_PAGESIZE};
use std::fmt;
use std::fs::File;
use std::io::{self, BufRead, BufReader, Read, Write};
use std::mem;
use std::os::raw::c_char;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::{collections::HashMap, time::Duration};
use std::{ffi::CStr, fs::OpenOptions};
#[cfg(feature = "chrono")]
use chrono::{DateTime, Local};
const PROC_CONFIG_GZ: &str = "/proc/config.gz";
const BOOT_CONFIG: &str = "/boot/config";
trait IntoOption<T> {
fn into_option(t: Self) -> Option<T>;
}
impl<T> IntoOption<T> for Option<T> {
fn into_option(t: Option<T>) -> Option<T> {
t
}
}
impl<T, R> IntoOption<T> for Result<T, R> {
fn into_option(t: Result<T, R>) -> Option<T> {
t.ok()
}
}
pub(crate) trait IntoResult<T, E> {
fn into(t: Self) -> Result<T, E>;
}
macro_rules! build_internal_error {
($err: expr) => {
crate::ProcError::InternalError(crate::InternalError {
msg: format!("Internal Unwrap Error: {}", $err),
file: file!(),
line: line!(),
#[cfg(feature = "backtrace")]
backtrace: backtrace::Backtrace::new(),
})
};
($err: expr, $msg: expr) => {
crate::ProcError::InternalError(crate::InternalError {
msg: format!("Internal Unwrap Error: {}: {}", $msg, $err),
file: file!(),
line: line!(),
#[cfg(feature = "backtrace")]
backtrace: backtrace::Backtrace::new(),
})
};
}
// custom NoneError, since std::option::NoneError is nightly-only
// See https://github.com/rust-lang/rust/issues/42327
struct NoneError;
impl std::fmt::Display for NoneError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "NoneError")
}
}
impl<T> IntoResult<T, NoneError> for Option<T> {
fn into(t: Option<T>) -> Result<T, NoneError> {
t.ok_or(NoneError)
}
}
impl<T, E> IntoResult<T, E> for Result<T, E> {
fn into(t: Result<T, E>) -> Result<T, E> {
t
}
}
#[macro_use]
#[allow(unused_macros)]
macro_rules! proc_panic {
($e:expr) => {
crate::IntoOption::into_option($e).unwrap_or_else(|| {
panic!(
"Failed to unwrap {}. Please report this as a procfs bug.",
stringify!($e)
)
})
};
($e:expr, $msg:expr) => {
crate::IntoOption::into_option($e).unwrap_or_else(|| {
panic!(
"Failed to unwrap {} ({}). Please report this as a procfs bug.",
stringify!($e),
$msg
)
})
};
}
macro_rules! expect {
($e:expr) => {
match crate::IntoResult::into($e) {
Ok(v) => v,
Err(e) => return Err(build_internal_error!(e)),
}
};
($e:expr, $msg:expr) => {
match crate::IntoResult::into($e) {
Ok(v) => v,
Err(e) => return Err(build_internal_error!(e, $msg)),
}
};
}
#[macro_use]
macro_rules! from_str {
($t:tt, $e:expr) => {{
let e = $e;
expect!(
$t::from_str_radix(e, 10),
format!("Failed to parse {} ({:?}) as a {}", stringify!($e), e, stringify!($t),)
)
}};
($t:tt, $e:expr, $radix:expr) => {{
let e = $e;
expect!(
$t::from_str_radix(e, $radix),
format!("Failed to parse {} ({:?}) as a {}", stringify!($e), e, stringify!($t))
)
}};
($t:tt, $e:expr, $radix:expr, pid:$pid:expr) => {{
let e = $e;
expect!(
$t::from_str_radix(e, $radix),
format!(
"Failed to parse {} ({:?}) as a {} (pid {})",
stringify!($e),
e,
stringify!($t),
$pid
)
)
}};
}
macro_rules! wrap_io_error {
($path:expr, $expr:expr) => {
match $expr {
Ok(v) => Ok(v),
Err(e) => {
let kind = e.kind();
Err(::std::io::Error::new(
kind,
crate::IoErrorWrapper {
path: $path.to_owned(),
inner: e.into_inner(),
},
))
}
}
};
}
pub(crate) fn read_file<P: AsRef<Path>>(path: P) -> ProcResult<String> {
let mut f = FileWrapper::open(path)?;
let mut buf = String::new();
f.read_to_string(&mut buf)?;
Ok(buf)
}
pub(crate) fn write_file<P: AsRef<Path>, T: AsRef<[u8]>>(path: P, buf: T) -> ProcResult<()> {
let mut f = OpenOptions::new().read(false).write(true).open(path)?;
f.write_all(buf.as_ref())?;
Ok(())
}
pub(crate) fn read_value<P, T, E>(path: P) -> ProcResult<T>
where
P: AsRef<Path>,
T: FromStr<Err = E>,
ProcError: From<E>,
{
let val = read_file(path)?;
Ok(<T as FromStr>::from_str(val.trim())?)
//Ok(val.trim().parse()?)
}
pub(crate) fn write_value<P: AsRef<Path>, T: fmt::Display>(path: P, value: T) -> ProcResult<()> {
write_file(path, value.to_string().as_bytes())
}
pub(crate) fn from_iter<'a, I, U>(i: I) -> ProcResult<U>
where
I: IntoIterator<Item = &'a str>,
U: FromStr,
{
let mut iter = i.into_iter();
let val = expect!(iter.next());
match FromStr::from_str(val) {
Ok(u) => Ok(u),
Err(..) => Err(build_internal_error!("Failed to convert")),
}
}
pub mod process;
mod meminfo;
pub use crate::meminfo::*;
mod sysvipc_shm;
pub use crate::sysvipc_shm::*;
pub mod net;
mod cpuinfo;
pub use crate::cpuinfo::*;
mod cgroups;
pub use crate::cgroups::*;
pub mod sys;
pub use crate::sys::kernel::Version as KernelVersion;
mod pressure;
pub use crate::pressure::*;
mod diskstats;
pub use diskstats::*;
mod locks;
pub use locks::*;
pub mod keyring;
mod uptime;
pub use uptime::*;
lazy_static! {
/// The number of clock ticks per second.
///
/// This is calculated from `sysconf(_SC_CLK_TCK)`.
static ref TICKS_PER_SECOND: ProcResult<i64> = {
Ok(ticks_per_second()?)
};
/// The version of the currently running kernel.
///
/// This is a lazily constructed static. You can also get this information via
/// [KernelVersion::new()].
static ref KERNEL: ProcResult<KernelVersion> = {
KernelVersion::current()
};
/// Memory page size, in bytes.
///
/// This is calculated from `sysconf(_SC_PAGESIZE)`.
static ref PAGESIZE: ProcResult<i64> = {
Ok(page_size()?)
};
}
fn convert_to_kibibytes(num: u64, unit: &str) -> ProcResult<u64> {
match unit {
"B" => Ok(num),
"KiB" | "kiB" | "kB" | "KB" => Ok(num * 1024),
"MiB" | "miB" | "MB" | "mB" => Ok(num * 1024 * 1024),
"GiB" | "giB" | "GB" | "gB" => Ok(num * 1024 * 1024 * 1024),
unknown => Err(build_internal_error!(format!("Unknown unit type {}", unknown))),
}
}
trait FromStrRadix: Sized {
fn from_str_radix(t: &str, radix: u32) -> Result<Self, std::num::ParseIntError>;
}
impl FromStrRadix for u64 {
fn from_str_radix(s: &str, radix: u32) -> Result<u64, std::num::ParseIntError> {
u64::from_str_radix(s, radix)
}
}
impl FromStrRadix for i32 {
fn from_str_radix(s: &str, radix: u32) -> Result<i32, std::num::ParseIntError> {
i32::from_str_radix(s, radix)
}
}
fn split_into_num<T: FromStrRadix>(s: &str, sep: char, radix: u32) -> ProcResult<(T, T)> {
let mut s = s.split(sep);
let a = expect!(FromStrRadix::from_str_radix(expect!(s.next()), radix));
let b = expect!(FromStrRadix::from_str_radix(expect!(s.next()), radix));
Ok((a, b))
}
/// This is used to hold both an IO error as well as the path of the file that originated the error
#[derive(Debug)]
struct IoErrorWrapper {
path: PathBuf,
inner: Option<Box<dyn std::error::Error + Send + Sync>>,
}
impl std::error::Error for IoErrorWrapper {}
impl fmt::Display for IoErrorWrapper {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
if let Some(inner) = &self.inner {
write!(f, "IO Error({}): {}", self.path.display(), inner)
} else {
write!(f, "IO Error({})", self.path.display())
}
}
}
/// A wrapper around a `File` that remembers the name of the path
struct FileWrapper {
inner: File,
path: PathBuf,
}
impl FileWrapper {
fn open<P: AsRef<Path>>(path: P) -> Result<FileWrapper, io::Error> {
let p = path.as_ref();
match File::open(&p) {
Ok(f) => Ok(FileWrapper {
inner: f,
path: p.to_owned(),
}),
Err(e) => {
let kind = e.kind();
Err(io::Error::new(
kind,
IoErrorWrapper {
path: p.to_owned(),
inner: e.into_inner(),
},
))
}
}
}
}
impl Read for FileWrapper {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
wrap_io_error!(self.path, self.inner.read(buf))
}
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
wrap_io_error!(self.path, self.inner.read_to_end(buf))
}
fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
wrap_io_error!(self.path, self.inner.read_to_string(buf))
}
fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
wrap_io_error!(self.path, self.inner.read_exact(buf))
}
}
/// The main error type for the procfs crate.
///
/// For more info, see the [ProcError] type.
pub type ProcResult<T> = Result<T, ProcError>;
/// The various error conditions in the procfs crate.
///
/// Most of the variants have an `Option<PathBuf>` component. If the error root cause was related
/// to some operation on a file, the path of this file will be stored in this component.
#[derive(Debug)]
pub enum ProcError {
/// A standard permission denied error.
///
/// This will be a common error, since some files in the procfs filesystem are only readable by
/// the root user.
PermissionDenied(Option<PathBuf>),
/// This might mean that the process no longer exists, or that your kernel doesn't support the
/// feature you are trying to use.
NotFound(Option<PathBuf>),
/// This might mean that a procfs file has incomplete contents.
///
/// If you encounter this error, consider retrying the operation.
Incomplete(Option<PathBuf>),
/// Any other IO error (rare).
Io(std::io::Error, Option<PathBuf>),
/// Any other non-IO error (very rare).
Other(String),
/// This error indicates that some unexpected error occurred. This is a bug. The inner
/// [InternalError] struct will contain some more info.
///
/// If you ever encounter this error, consider it a bug in the procfs crate and please report
/// it on github.
InternalError(InternalError),
}
/// An internal error in the procfs crate
///
/// If you encounter this error, consider it a bug and please report it on
/// [github](https://github.com/eminence/procfs).
///
/// If you compile with the optional `backtrace` feature (disabled by default),
/// you can gain access to a stack trace of where the error happened.
pub struct InternalError {
pub msg: String,
pub file: &'static str,
pub line: u32,
#[cfg(feature = "backtrace")]
pub backtrace: backtrace::Backtrace,
}
impl std::fmt::Debug for InternalError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"bug at {}:{} (please report this procfs bug)\n{}",
self.file, self.line, self.msg
)
}
}
impl std::fmt::Display for InternalError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"bug at {}:{} (please report this procfs bug)\n{}",
self.file, self.line, self.msg
)
}
}
impl From<std::io::Error> for ProcError {
fn from(io: std::io::Error) -> Self {
use std::io::ErrorKind;
let kind = io.kind();
let path: Option<PathBuf> = io.get_ref().and_then(|inner| {
if let Some(ref inner) = inner.downcast_ref::<IoErrorWrapper>() {
Some(inner.path.clone())
} else {
None
}
});
match kind {
ErrorKind::PermissionDenied => ProcError::PermissionDenied(path),
ErrorKind::NotFound => ProcError::NotFound(path),
_other => ProcError::Io(io, path),
}
}
}
impl From<&'static str> for ProcError {
fn from(val: &'static str) -> Self {
ProcError::Other(val.to_owned())
}
}
impl From<std::num::ParseIntError> for ProcError {
fn from(val: std::num::ParseIntError) -> Self {
ProcError::Other(format!("ParseIntError: {}", val))
}
}
impl From<std::string::ParseError> for ProcError {
fn from(e: std::string::ParseError) -> Self {
match e {}
}
}
impl std::fmt::Display for ProcError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
match self {
// Variants with paths:
ProcError::PermissionDenied(Some(p)) => write!(f, "Permission Denied: {}", p.display()),
ProcError::NotFound(Some(p)) => write!(f, "File not found: {}", p.display()),
ProcError::Incomplete(Some(p)) => write!(f, "Data incomplete: {}", p.display()),
ProcError::Io(inner, Some(p)) => {
write!(f, "Unexpected IO error({}): {}", p.display(), inner)
}
// Variants without paths:
ProcError::PermissionDenied(None) => write!(f, "Permission Denied"),
ProcError::NotFound(None) => write!(f, "File not found"),
ProcError::Incomplete(None) => write!(f, "Data incomplete"),
ProcError::Io(inner, None) => write!(f, "Unexpected IO error: {}", inner),
ProcError::Other(s) => write!(f, "Unknown error {}", s),
ProcError::InternalError(e) => write!(f, "Internal error: {}", e),
}
}
}
impl std::error::Error for ProcError {}
/// Load average figures.
///
/// Load averages are calculated as the number of jobs in the run queue (state R) or waiting for
/// disk I/O (state D) averaged over 1, 5, and 15 minutes.
#[derive(Debug, Clone)]
pub struct LoadAverage {
/// The one-minute load average
pub one: f32,
/// The five-minute load average
pub five: f32,
/// The fifteen-minute load average
pub fifteen: f32,
/// The number of currently runnable kernel scheduling entities (processes, threads).
pub cur: u32,
/// The number of kernel scheduling entities that currently exist on the system.
pub max: u32,
/// The fifth field is the PID of the process that was most recently created on the system.
pub latest_pid: u32,
}
impl LoadAverage {
/// Reads load average info from `/proc/loadavg`
pub fn new() -> ProcResult<LoadAverage> {
let mut f = FileWrapper::open("/proc/loadavg")?;
let mut s = String::new();
f.read_to_string(&mut s)?;
let mut s = s.split_whitespace();
let one = expect!(f32::from_str(expect!(s.next())));
let five = expect!(f32::from_str(expect!(s.next())));
let fifteen = expect!(f32::from_str(expect!(s.next())));
let curmax = expect!(s.next());
let latest_pid = expect!(u32::from_str(expect!(s.next())));
let mut s = curmax.split('/');
let cur = expect!(u32::from_str(expect!(s.next())));
let max = expect!(u32::from_str(expect!(s.next())));
Ok(LoadAverage {
one,
five,
fifteen,
cur,
max,
latest_pid,
})
}
}
/// Return the number of ticks per second.
///
/// This isn't part of the proc file system, but it's a useful thing to have, since several fields
/// count in ticks. This is calculated from `sysconf(_SC_CLK_TCK)`.
pub fn ticks_per_second() -> std::io::Result<i64> {
if cfg!(unix) {
match unsafe { sysconf(_SC_CLK_TCK) } {
-1 => Err(std::io::Error::last_os_error()),
x => Ok(x.into()),
}
} else {
panic!("Not supported on non-unix platforms")
}
}
/// The boot time of the system, as a `DateTime` object.
///
/// This is calculated from `/proc/stat`.
///
/// This function requires the "chrono" features to be enabled (which it is by default).
#[cfg(feature = "chrono")]
pub fn boot_time() -> ProcResult<DateTime<Local>> {
use chrono::TimeZone;
let secs = boot_time_secs()?;
Ok(chrono::Local.timestamp(secs as i64, 0))
}
/// The boottime of the system, in seconds since the epoch
///
/// This is calculated from `/proc/stat`.
///
#[cfg_attr(
not(feature = "chrono"),
doc = "If you compile with the optional `chrono` feature, you can use the `boot_time()` method to get the boot time as a `DateTime` object."
)]
#[cfg_attr(
feature = "chrono",
doc = "See also [boot_time()] to get the boot time as a `DateTime`"
)]
pub fn boot_time_secs() -> ProcResult<u64> {
BOOT_TIME.with(|x| {
let mut btime = x.borrow_mut();
if let Some(btime) = *btime {
Ok(btime)
} else {
let stat = KernelStats::new()?;
*btime = Some(stat.btime);
Ok(stat.btime)
}
})
}
thread_local! {
static BOOT_TIME : std::cell::RefCell<Option<u64>> = std::cell::RefCell::new(None);
}
/// Memory page size, in bytes.
///
/// This is calculated from `sysconf(_SC_PAGESIZE)`.
pub fn page_size() -> std::io::Result<i64> {
if cfg!(unix) {
match unsafe { sysconf(_SC_PAGESIZE) } {
-1 => Err(std::io::Error::last_os_error()),
x => Ok(x.into()),
}
} else {
panic!("Not supported on non-unix platforms")
}
}
/// Possible values for a kernel config option
#[derive(Debug, Clone, PartialEq)]
pub enum ConfigSetting {
Yes,
Module,
Value(String),
}
/// Returns a configuration options used to build the currently running kernel
///
/// If CONFIG_KCONFIG_PROC is available, the config is read from `/proc/config.gz`.
/// Else look in `/boot/config-$(uname -r)` or `/boot/config` (in that order).
pub fn kernel_config() -> ProcResult<HashMap<String, ConfigSetting>> {
use flate2::read::GzDecoder;
let reader: Box<dyn BufRead> = if Path::new(PROC_CONFIG_GZ).exists() {
let file = FileWrapper::open(PROC_CONFIG_GZ)?;
let decoder = GzDecoder::new(file);
Box::new(BufReader::new(decoder))
} else {
let mut kernel: libc::utsname = unsafe { mem::zeroed() };
if unsafe { libc::uname(&mut kernel) != 0 } {
return Err(ProcError::Other("Failed to call uname()".to_string()));
}
let filename = format!(
"{}-{}",
BOOT_CONFIG,
unsafe { CStr::from_ptr(kernel.release.as_ptr() as *const c_char) }.to_string_lossy()
);
if Path::new(&filename).exists() {
let file = FileWrapper::open(filename)?;
Box::new(BufReader::new(file))
} else {
let file = FileWrapper::open(BOOT_CONFIG)?;
Box::new(BufReader::new(file))
}
};
let mut map = HashMap::new();
for line in reader.lines() {
let line = line?;
if line.starts_with('#') {
continue;
}
if line.contains('=') {
let mut s = line.splitn(2, '=');
let name = expect!(s.next()).to_owned();
let value = match expect!(s.next()) {
"y" => ConfigSetting::Yes,
"m" => ConfigSetting::Module,
s => ConfigSetting::Value(s.to_owned()),
};
map.insert(name, value);
}
}
Ok(map)
}
/// The amount of time, measured in ticks, the CPU has been in specific states
///
/// These fields are measured in ticks because the underlying data from the kernel is measured in ticks.
/// The number of ticks per second can be returned by [`ticks_per_second()`](crate::ticks_per_second)
/// and is generally 100 on most systems.
/// To convert this value to seconds, you can divide by the tps. There are also convenience methods
/// that you can use too.
#[derive(Debug, Clone)]
pub struct CpuTime {
/// Ticks spent in user mode
pub user: u64,
/// Ticks spent in user mode with low priority (nice)
pub nice: u64,
/// Ticks spent in system mode
pub system: u64,
/// Ticks spent in the idle state
pub idle: u64,
/// Ticks waiting for I/O to complete
///
/// This value is not reliable, for the following reasons:
///
/// 1. The CPU will not wait for I/O to complete; iowait is the time that a
/// task is waiting for I/O to complete. When a CPU goes into idle state
/// for outstanding task I/O, another task will be scheduled on this CPU.
///
/// 2. On a multi-core CPU, this task waiting for I/O to complete is not running
/// on any CPU, so the iowait for each CPU is difficult to calculate.
///
/// 3. The value in this field may *decrease* in certain conditions.
///
/// (Since Linux 2.5.41)
pub iowait: Option<u64>,
/// Ticks servicing interrupts
///
/// (Since Linux 2.6.0)
pub irq: Option<u64>,
/// Ticks servicing softirqs
///
/// (Since Linux 2.6.0)
pub softirq: Option<u64>,
/// Ticks of stolen time.
///
/// Stolen time is the time spent in other operating systems when running in
/// a virtualized environment.
///
/// (Since Linux 2.6.11)
pub steal: Option<u64>,
/// Ticks spent running a virtual CPU for guest operating systems under control
/// of the linux kernel
///
/// (Since Linux 2.6.24)
pub guest: Option<u64>,
/// Ticks spent running a niced guest
///
/// (Since Linux 2.6.33)
pub guest_nice: Option<u64>,
tps: u64,
}
impl CpuTime {
fn from_str(s: &str) -> ProcResult<CpuTime> {
let mut s = s.split_whitespace();
// Store this field in the struct so we don't have to attempt to unwrap ticks_per_second() when we convert
// from ticks into other time units
let tps = crate::ticks_per_second()? as u64;
s.next();
let user = from_str!(u64, expect!(s.next()));
let nice = from_str!(u64, expect!(s.next()));
let system = from_str!(u64, expect!(s.next()));
let idle = from_str!(u64, expect!(s.next()));
let iowait = s.next().map(|s| Ok(from_str!(u64, s))).transpose()?;
let irq = s.next().map(|s| Ok(from_str!(u64, s))).transpose()?;
let softirq = s.next().map(|s| Ok(from_str!(u64, s))).transpose()?;
let steal = s.next().map(|s| Ok(from_str!(u64, s))).transpose()?;
let guest = s.next().map(|s| Ok(from_str!(u64, s))).transpose()?;
let guest_nice = s.next().map(|s| Ok(from_str!(u64, s))).transpose()?;
Ok(CpuTime {
user,
nice,
system,
idle,
iowait,
irq,
softirq,
steal,
guest,
guest_nice,
tps,
})
}
/// Milliseconds spent in user mode
pub fn user_ms(&self) -> u64 {
let ms_per_tick = 1000 / self.tps;
self.user * ms_per_tick
}
/// Time spent in user mode
pub fn user_duration(&self) -> Duration {
Duration::from_millis(self.user_ms())
}
/// Milliseconds spent in user mode with low priority (nice)
pub fn nice_ms(&self) -> u64 {
let ms_per_tick = 1000 / self.tps;
self.nice * ms_per_tick
}
/// Time spent in user mode with low priority (nice)
pub fn nice_duration(&self) -> Duration {
Duration::from_millis(self.nice_ms())
}
/// Milliseconds spent in system mode
pub fn system_ms(&self) -> u64 {
let ms_per_tick = 1000 / self.tps;
self.system * ms_per_tick
}
/// Time spent in system mode
pub fn system_duration(&self) -> Duration {
Duration::from_millis(self.system_ms())
}
/// Milliseconds spent in the idle state
pub fn idle_ms(&self) -> u64 {
let ms_per_tick = 1000 / self.tps;
self.idle * ms_per_tick
}
/// Time spent in the idle state
pub fn idle_duration(&self) -> Duration {
Duration::from_millis(self.idle_ms())
}
/// Milliseconds spent waiting for I/O to complete
pub fn iowait_ms(&self) -> Option<u64> {
let ms_per_tick = 1000 / self.tps;
self.iowait.map(|io| io * ms_per_tick)
}
/// Time spent waiting for I/O to complete
pub fn iowait_duration(&self) -> Option<Duration> {
self.iowait_ms().map(|io| Duration::from_millis(io))
}
/// Milliseconds spent servicing interrupts
pub fn irq_ms(&self) -> Option<u64> {
let ms_per_tick = 1000 / self.tps;
self.irq.map(|ms| ms * ms_per_tick)
}
/// Time spent servicing interrupts
pub fn irq_duration(&self) -> Option<Duration> {
self.irq_ms().map(|ms| Duration::from_millis(ms))
}
/// Milliseconds spent servicing softirqs
pub fn softirq_ms(&self) -> Option<u64> {
let ms_per_tick = 1000 / self.tps;
self.softirq.map(|ms| ms * ms_per_tick)
}
/// Time spent servicing softirqs
pub fn softirq_duration(&self) -> Option<Duration> {
self.softirq_ms().map(|ms| Duration::from_millis(ms))
}
/// Milliseconds of stolen time
pub fn steal_ms(&self) -> Option<u64> {
let ms_per_tick = 1000 / self.tps;
self.steal.map(|ms| ms * ms_per_tick)
}
/// Amount of stolen time
pub fn steal_duration(&self) -> Option<Duration> {
self.steal_ms().map(|ms| Duration::from_millis(ms))
}
/// Milliseconds spent running a virtual CPU for guest operating systems under control of the linux kernel
pub fn guest_ms(&self) -> Option<u64> {
let ms_per_tick = 1000 / self.tps;
self.guest.map(|ms| ms * ms_per_tick)
}
/// Time spent running a virtual CPU for guest operating systems under control of the linux kernel
pub fn guest_duration(&self) -> Option<Duration> {
self.guest_ms().map(|ms| Duration::from_millis(ms))
}
/// Milliseconds spent running a niced guest
pub fn guest_nice_ms(&self) -> Option<u64> {
let ms_per_tick = 1000 / self.tps;
self.guest_nice.map(|ms| ms * ms_per_tick)
}
/// Time spent running a niced guest
pub fn guest_nice_duration(&self) -> Option<Duration> {
self.guest_nice_ms().map(|ms| Duration::from_millis(ms))
}
}
/// Kernel/system statistics, from `/proc/stat`
#[derive(Debug, Clone)]
pub struct KernelStats {
/// The amount of time the system spent in various states
pub total: CpuTime,
/// The amount of time that specific CPUs spent in various states
pub cpu_time: Vec<CpuTime>,
/// The number of context switches that the system underwent
pub ctxt: u64,
/// Boot time, in number of seconds since the Epoch
pub btime: u64,
/// Number of forks since boot
pub processes: u64,
/// Number of processes in runnable state
///
/// (Since Linux 2.5.45)
pub procs_running: Option<u32>,
/// Number of processes blocked waiting for I/O
///
/// (Since Linux 2.5.45)
pub procs_blocked: Option<u32>,
}
impl KernelStats {
pub fn new() -> ProcResult<KernelStats> {
KernelStats::from_reader(FileWrapper::open("/proc/stat")?)
}
/// Get KernelStatus from a custom Read instead of the default `/proc/stat`.
pub fn from_reader<R: io::Read>(r: R) -> ProcResult<KernelStats> {
let bufread = BufReader::new(r);
let lines = bufread.lines();
let mut total_cpu = None;
let mut cpus = Vec::new();
let mut ctxt = None;
let mut btime = None;
let mut processes = None;
let mut procs_running = None;
let mut procs_blocked = None;
for line in lines {
let line = line?;
if line.starts_with("cpu ") {
total_cpu = Some(CpuTime::from_str(&line)?);
} else if line.starts_with("cpu") {
cpus.push(CpuTime::from_str(&line)?);
} else if line.starts_with("ctxt ") {
ctxt = Some(from_str!(u64, &line[5..]));
} else if line.starts_with("btime ") {
btime = Some(from_str!(u64, &line[6..]));
} else if line.starts_with("processes ") {
processes = Some(from_str!(u64, &line[10..]));
} else if line.starts_with("procs_running ") {
procs_running = Some(from_str!(u32, &line[14..]));
} else if line.starts_with("procs_blocked ") {
procs_blocked = Some(from_str!(u32, &line[14..]));
}
}
Ok(KernelStats {
total: expect!(total_cpu),
cpu_time: cpus,
ctxt: expect!(ctxt),