Skip to content

Commit a02bb89

Browse files
authored
Merge pull request eminence#257 from afranchuk/split-crates
Implementation of a split crate scheme.
2 parents f0654ec + 367131e commit a02bb89

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+4305
-3320
lines changed

.github/workflows/rust.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ jobs:
2525
toolchain: ${{ matrix.toolchain }}
2626

2727
- name: Build
28-
run: cargo +${{ matrix.toolchain }} build --verbose
28+
run: cargo +${{ matrix.toolchain }} build --workspace --verbose
2929
- name: Run tests
30-
run: cargo +${{ matrix.toolchain }} test --all-features --verbose -- --skip _runsinglethread
30+
run: cargo +${{ matrix.toolchain }} test --workspace --all-features --verbose -- --skip _runsinglethread
3131
- name: Run tests (single-threaded tests)
32-
run: cargo +${{ matrix.toolchain }} test --all-features --verbose -- _runsinglethread --test-threads 1
32+
run: cargo +${{ matrix.toolchain }} test --workspace --all-features --verbose -- _runsinglethread --test-threads 1
3333

3434
- name: Build docs (all features)
35-
run: cargo +${{ matrix.toolchain }} doc --all-features
35+
run: cargo +${{ matrix.toolchain }} doc --workspace --all-features
3636

3737
- name: Build docs
38-
run: cargo +${{ matrix.toolchain }} doc
38+
run: cargo +${{ matrix.toolchain }} doc --workspace
3939

4040

4141
check:
@@ -59,7 +59,7 @@ jobs:
5959
target: i686-unknown-linux-gnu
6060

6161
- name: cargo check (aarch64)
62-
run: cargo +${{ matrix.toolchain }} check --target aarch64-linux-android --all-features
62+
run: cargo +${{ matrix.toolchain }} check --workspace --target aarch64-linux-android --all-features
6363
- name: cargo check (i686)
64-
run: cargo +${{ matrix.toolchain }} check --target i686-unknown-linux-gnu --all-features
64+
run: cargo +${{ matrix.toolchain }} check --workspace --target i686-unknown-linux-gnu --all-features
6565

Cargo.toml

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,15 @@
1-
[package]
2-
name = "procfs"
1+
[workspace]
2+
members = [
3+
"procfs",
4+
"procfs-core",
5+
]
6+
7+
[workspace.package]
38
version = "0.15.1"
49
authors = ["Andrew Chin <achin@eminence32.net>"]
510
repository = "https://github.com/eminence/procfs"
6-
documentation = "https://docs.rs/procfs/"
7-
description = "Interface to the linux procfs pseudo-filesystem"
8-
readme = "README.md"
911
keywords = ["procfs", "proc", "linux", "process"]
1012
categories = ["os::unix-apis", "filesystem"]
1113
license = "MIT OR Apache-2.0"
1214
edition = "2018"
1315
rust-version = "1.48"
14-
15-
[features]
16-
default = ["chrono", "flate2"]
17-
serde1 = ["serde"]
18-
19-
[dependencies]
20-
rustix = { version = "0.37.0", features = ["fs", "process", "param", "thread"] }
21-
bitflags = "1.2"
22-
lazy_static = "1.0.2"
23-
chrono = {version = "0.4.20", optional = true, features = ["clock"], default-features = false }
24-
byteorder = {version="1.2.3", features=["i128"]}
25-
hex = "0.4"
26-
flate2 = { version = "1.0.3", optional = true }
27-
backtrace = { version = "0.3", optional = true }
28-
serde = { version = "1.0", features = ["derive"], optional = true }
29-
30-
[dev-dependencies]
31-
criterion = "0.4"
32-
procinfo = "0.4.2"
33-
failure = "0.1"
34-
libc = "0.2.139"
35-
36-
[package.metadata.docs.rs]
37-
all-features = true
38-
39-
[[bench]]
40-
name = "cpuinfo"
41-
harness = false

examples/pressure.rs

Lines changed: 0 additions & 7 deletions
This file was deleted.

procfs-core/Cargo.toml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[package]
2+
name = "procfs-core"
3+
documentation = "https://docs.rs/procfs-core/"
4+
description = "Data structures and parsing for the linux procfs pseudo-filesystem"
5+
readme = "../README.md"
6+
version.workspace = true
7+
authors.workspace = true
8+
repository.workspace = true
9+
keywords.workspace = true
10+
categories.workspace = true
11+
license.workspace = true
12+
edition.workspace = true
13+
rust-version.workspace = true
14+
15+
[features]
16+
default = ["chrono"]
17+
serde1 = ["serde"]
18+
19+
[dependencies]
20+
backtrace = { version = "0.3", optional = true }
21+
bitflags = "1.2"
22+
chrono = { version = "0.4.20", optional = true, features = ["clock"], default-features = false }
23+
hex = "0.4"
24+
serde = { version = "1.0", features = ["derive"], optional = true }
25+
26+
[package.metadata.docs.rs]
27+
all-features = true
Lines changed: 37 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
use crate::ProcResult;
2-
3-
use super::process::Process;
4-
52
#[cfg(feature = "serde1")]
63
use serde::{Deserialize, Serialize};
4+
use std::io::BufRead;
75

86
#[derive(Debug, Clone)]
97
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
108
/// Container group controller information.
11-
///
12-
/// See also the [cgroups()] method.
139
pub struct CGroupController {
1410
/// The name of the controller.
1511
pub name: String,
@@ -28,49 +24,45 @@ pub struct CGroupController {
2824
pub enabled: bool,
2925
}
3026

31-
/// Information about the cgroup controllers that are compiled into the kernel
32-
///
33-
/// (since Linux 2.6.24)
34-
// This is returning a vector, but if each subsystem name is unique, maybe this can be a hashmap
35-
// instead
36-
pub fn cgroups() -> ProcResult<Vec<CGroupController>> {
37-
use std::fs::File;
38-
use std::io::{BufRead, BufReader};
27+
#[derive(Debug, Clone)]
28+
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
29+
/// Container group controller information.
30+
// This contains a vector, but if each subsystem name is unique, maybe this can be a
31+
// hashmap instead
32+
pub struct CGroupControllers(pub Vec<CGroupController>);
3933

40-
let file = File::open("/proc/cgroups")?;
41-
let reader = BufReader::new(file);
34+
impl crate::FromBufRead for CGroupControllers {
35+
fn from_buf_read<R: BufRead>(reader: R) -> ProcResult<Self> {
36+
let mut vec = Vec::new();
4237

43-
let mut vec = Vec::new();
38+
for line in reader.lines() {
39+
let line = line?;
40+
if line.starts_with('#') {
41+
continue;
42+
}
4443

45-
for line in reader.lines() {
46-
let line = line?;
47-
if line.starts_with('#') {
48-
continue;
49-
}
44+
let mut s = line.split_whitespace();
45+
let name = expect!(s.next(), "name").to_owned();
46+
let hierarchy = from_str!(u32, expect!(s.next(), "hierarchy"));
47+
let num_cgroups = from_str!(u32, expect!(s.next(), "num_cgroups"));
48+
let enabled = expect!(s.next(), "enabled") == "1";
5049

51-
let mut s = line.split_whitespace();
52-
let name = expect!(s.next(), "name").to_owned();
53-
let hierarchy = from_str!(u32, expect!(s.next(), "hierarchy"));
54-
let num_cgroups = from_str!(u32, expect!(s.next(), "num_cgroups"));
55-
let enabled = expect!(s.next(), "enabled") == "1";
50+
vec.push(CGroupController {
51+
name,
52+
hierarchy,
53+
num_cgroups,
54+
enabled,
55+
});
56+
}
5657

57-
vec.push(CGroupController {
58-
name,
59-
hierarchy,
60-
num_cgroups,
61-
enabled,
62-
});
58+
Ok(CGroupControllers(vec))
6359
}
64-
65-
Ok(vec)
6660
}
6761

6862
/// Information about a process cgroup
69-
///
70-
/// See also the [Process::cgroups()] method.
7163
#[derive(Debug, Clone)]
7264
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
73-
pub struct ProcessCgroup {
65+
pub struct ProcessCGroup {
7466
/// For cgroups version 1 hierarchies, this field contains a unique hierarchy ID number
7567
/// that can be matched to a hierarchy ID in /proc/cgroups. For the cgroups version 2
7668
/// hierarchy, this field contains the value 0.
@@ -88,16 +80,13 @@ pub struct ProcessCgroup {
8880
pub pathname: String,
8981
}
9082

91-
impl Process {
92-
/// Describes control groups to which the process with the corresponding PID belongs.
93-
///
94-
/// The displayed information differs for cgroupsversion 1 and version 2 hierarchies.
95-
pub fn cgroups(&self) -> ProcResult<Vec<ProcessCgroup>> {
96-
use std::io::{BufRead, BufReader};
97-
98-
let file = self.open_relative("cgroup")?;
99-
let reader = BufReader::new(file);
83+
/// Information about process cgroups.
84+
#[derive(Debug, Clone)]
85+
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
86+
pub struct ProcessCGroups(pub Vec<ProcessCGroup>);
10087

88+
impl crate::FromBufRead for ProcessCGroups {
89+
fn from_buf_read<R: BufRead>(reader: R) -> ProcResult<Self> {
10190
let mut vec = Vec::new();
10291

10392
for line in reader.lines() {
@@ -114,31 +103,13 @@ impl Process {
114103
.collect();
115104
let pathname = expect!(s.next(), "path").to_owned();
116105

117-
vec.push(ProcessCgroup {
106+
vec.push(ProcessCGroup {
118107
hierarchy,
119108
controllers,
120109
pathname,
121110
});
122111
}
123112

124-
Ok(vec)
125-
}
126-
}
127-
128-
#[cfg(test)]
129-
mod tests {
130-
use super::*;
131-
132-
#[test]
133-
fn test_cgroups() {
134-
let groups = cgroups().unwrap();
135-
println!("{:?}", groups);
136-
}
137-
138-
#[test]
139-
fn test_process_cgroups() {
140-
let myself = Process::myself().unwrap();
141-
let groups = myself.cgroups();
142-
println!("{:?}", groups);
113+
Ok(ProcessCGroups(vec))
143114
}
144115
}

src/cpuinfo.rs renamed to procfs-core/src/cpuinfo.rs

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use crate::{FileWrapper, ProcResult};
1+
use crate::{expect, ProcResult};
22

33
#[cfg(feature = "serde1")]
44
use serde::{Deserialize, Serialize};
5-
use std::{collections::HashMap, io::Read};
5+
use std::{collections::HashMap, io::BufRead};
66

77
/// Represents the data from `/proc/cpuinfo`.
88
///
@@ -20,20 +20,15 @@ pub struct CpuInfo {
2020
pub cpus: Vec<HashMap<String, String>>,
2121
}
2222

23-
impl CpuInfo {
24-
/// Get CpuInfo from a custom Read instead of the default `/proc/cpuinfo`.
25-
pub fn from_reader<R: Read>(r: R) -> ProcResult<CpuInfo> {
26-
use std::io::{BufRead, BufReader};
27-
28-
let reader = BufReader::new(r);
29-
23+
impl crate::FromBufRead for CpuInfo {
24+
fn from_buf_read<R: BufRead>(r: R) -> ProcResult<Self> {
3025
let mut list = Vec::new();
3126
let mut map = Some(HashMap::new());
3227

3328
// the first line of a cpu block must start with "processor"
3429
let mut found_first = false;
3530

36-
for line in reader.lines().flatten() {
31+
for line in r.lines().flatten() {
3732
if !line.is_empty() {
3833
let mut s = line.split(':');
3934
let key = expect!(s.next());
@@ -88,12 +83,9 @@ impl CpuInfo {
8883
cpus: list,
8984
})
9085
}
91-
pub fn new() -> ProcResult<CpuInfo> {
92-
let file = FileWrapper::open("/proc/cpuinfo")?;
93-
94-
CpuInfo::from_reader(file)
95-
}
86+
}
9687

88+
impl CpuInfo {
9789
/// Get the total number of cpu cores.
9890
///
9991
/// This is the number of entries in the `/proc/cpuinfo` file.
@@ -115,6 +107,7 @@ impl CpuInfo {
115107
.collect()
116108
})
117109
}
110+
118111
/// Get the content of a specific field associated to a CPU
119112
///
120113
/// Returns None if the requested cpu index is not found.
@@ -130,13 +123,16 @@ impl CpuInfo {
130123
pub fn model_name(&self, cpu_num: usize) -> Option<&str> {
131124
self.get_field(cpu_num, "model name")
132125
}
126+
133127
pub fn vendor_id(&self, cpu_num: usize) -> Option<&str> {
134128
self.get_field(cpu_num, "vendor_id")
135129
}
130+
136131
/// May not be available on some older 2.6 kernels
137132
pub fn physical_id(&self, cpu_num: usize) -> Option<u32> {
138133
self.get_field(cpu_num, "physical id").and_then(|s| s.parse().ok())
139134
}
135+
140136
pub fn flags(&self, cpu_num: usize) -> Option<Vec<&str>> {
141137
self.get_field(cpu_num, "flags")
142138
.map(|flags| flags.split_whitespace().collect())
@@ -147,20 +143,6 @@ impl CpuInfo {
147143
mod tests {
148144
use super::*;
149145

150-
#[test]
151-
fn test_cpuinfo() {
152-
let info = CpuInfo::new().unwrap();
153-
println!("{:#?}", info.flags(0));
154-
for num in 0..info.num_cores() {
155-
info.model_name(num).unwrap();
156-
info.vendor_id(num).unwrap();
157-
// May not be available on some old kernels:
158-
info.physical_id(num);
159-
}
160-
161-
//assert_eq!(info.num_cores(), 8);
162-
}
163-
164146
#[test]
165147
fn test_cpuinfo_rpi() {
166148
// My rpi system includes some stuff at the end of /proc/cpuinfo that we shouldn't parse
@@ -212,7 +194,9 @@ Model : Raspberry Pi 3 Model B Plus Rev 1.3
212194

213195
let r = std::io::Cursor::new(data.as_bytes());
214196

215-
let info = CpuInfo::from_reader(r).unwrap();
197+
use crate::FromRead;
198+
199+
let info = CpuInfo::from_read(r).unwrap();
216200
assert_eq!(info.num_cores(), 4);
217201
let info = info.get_info(0).unwrap();
218202
assert!(info.get("model name").is_some());

0 commit comments

Comments
 (0)