Skip to content
This repository was archived by the owner on Jan 9, 2026. It is now read-only.

Commit 8c645b1

Browse files
authored
Merge pull request #8 from JJ-YY-JJ-YY/dev_fj
feat(core): swagger support
2 parents ae9e6f0 + bde608c commit 8c645b1

File tree

30 files changed

+791
-214
lines changed

30 files changed

+791
-214
lines changed

Cargo.lock

Lines changed: 243 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ lazy_static = "1.4.0"
1515
nix = "0.24.1"
1616
log = "0.4"
1717
config = "0.13.1"
18-
strum = { version = "0.24", features = ["derive"] }
18+
strum = { version = "0.24", features = ["derive"] }
19+
utoipa = { version = "3", features = ["rocket_extras"] }

lib/src/cgroup/blkio_cg.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ limitations under the License.
1616
use crate::cgroup::utils;
1717
use crate::common;
1818
use crate::common::CGroupType;
19-
use crate::ffi::bpf::ffi::fs_data;
2019
use crate::ffi::{
21-
is_bpf_moudule_valid, wrapper_get_cgroup_fs_data, wrapper_get_cgroup_io_latpcts,
20+
is_bpf_moudule_valid, wrapper_get_cgroup_fs_data, wrapper_get_cgroup_io_latpcts, WrapperFSData,
2221
WrapperIoLatpcts, BPF_MODULE_CGROUP_FS, BPF_MODULE_CGROUP_IO,
2322
};
2423
use crate::psi::PressureStallInfo;
@@ -30,6 +29,7 @@ use std::collections::HashMap;
3029
use std::fmt::{self, Formatter};
3130
use std::fs;
3231
use std::path::{Path, PathBuf};
32+
use utoipa::ToSchema;
3333

3434
pub fn new_blkio_cgroup(
3535
mount_point: &str,
@@ -62,7 +62,7 @@ pub fn new_blkio_cgroup(
6262
}
6363
}
6464

65-
#[derive(Debug, Clone, Serialize, Deserialize)]
65+
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
6666
pub enum BlkIOCGroup {
6767
V1(BlkIOCGroupV1),
6868
V2(BlkIOCGroupV2),
@@ -98,7 +98,7 @@ impl BlkIOCGroup {
9898
}
9999
}
100100

101-
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
101+
#[derive(Debug, Clone, Serialize, Deserialize, Default, ToSchema)]
102102
pub struct BlkIOCGroupV2 {
103103
full_path: PathBuf,
104104
user_path: PathBuf,
@@ -107,13 +107,13 @@ pub struct BlkIOCGroupV2 {
107107
io_pressure: PressureStallInfo,
108108
io_latency: HashMap<String, u64>,
109109
io_weight: HashMap<String, u64>,
110-
pub(crate) bpf_fs_data: fs_data,
111-
pub(crate) old_bpf_fs_data: fs_data,
110+
pub(crate) bpf_fs_data: WrapperFSData,
111+
pub(crate) old_bpf_fs_data: WrapperFSData,
112112
bpf_io_latency: WrapperIoLatpcts,
113113
update_time: u64,
114114
}
115115

116-
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
116+
#[derive(Debug, Clone, Serialize, Deserialize, Default, ToSchema)]
117117
pub struct BlkIOStatV2 {
118118
rbytes: u64,
119119
wbytes: u64,
@@ -123,7 +123,7 @@ pub struct BlkIOStatV2 {
123123
dios: u64,
124124
}
125125

126-
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
126+
#[derive(Debug, Clone, Serialize, Deserialize, Default, ToSchema)]
127127
pub struct BlkIOMaxV2 {
128128
rbps: u64,
129129
wbps: u64,
@@ -358,16 +358,16 @@ impl fmt::Display for BlkOperationType {
358358
}
359359
}
360360

361-
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
361+
#[derive(Debug, Clone, Serialize, Deserialize, Default, ToSchema)]
362362
pub struct BlkIOCGroupV1 {
363363
full_path: PathBuf,
364364
user_path: PathBuf,
365365
pub(crate) iops_details: HashMap<String, HashMap<BlkOperationType, u64>>,
366366
pub(crate) bps_details: HashMap<String, HashMap<BlkOperationType, u64>>,
367367
pub(crate) iops_total: u64,
368368
pub(crate) bps_total: u64,
369-
pub(crate) bpf_fs_data: fs_data,
370-
pub(crate) old_bpf_fs_data: fs_data,
369+
pub(crate) bpf_fs_data: WrapperFSData,
370+
pub(crate) old_bpf_fs_data: WrapperFSData,
371371
update_time: u64,
372372
}
373373

lib/src/cgroup/cg.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use serde::{Deserialize, Serialize};
2626
use std::collections::HashMap;
2727
use std::fmt;
2828
use std::path::{Path, PathBuf};
29+
use utoipa::ToSchema;
2930

3031
const SUB_SYSTEM_MEM_STR: &str = "memory";
3132
const SUB_SYSTEM_CPU_SET_STR: &str = "cpuset";
@@ -34,7 +35,7 @@ const SUB_SYSTEM_BLK_IOSTR: &str = "blkio";
3435
const SUB_SYSTEM_NET_STR: &str = "net_cls";
3536
const SUB_SYSTEM_PERF_EVENT_STR: &str = "perf_event";
3637

37-
#[derive(Eq, PartialEq, Hash, Serialize, Deserialize, Clone, Debug)]
38+
#[derive(Eq, PartialEq, Hash, Serialize, Deserialize, Clone, Debug, ToSchema)]
3839
pub enum SubSystemType {
3940
Memory,
4041
Cpuset,
@@ -72,11 +73,15 @@ impl fmt::Display for SubSystemType {
7273

7374
pub type CGroupUserPath = PathBuf;
7475

75-
#[derive(Clone, Debug, Serialize, Deserialize)]
76+
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
7677
pub struct CGroup {
78+
/// cgroup fs mount point
7779
mount_point: PathBuf,
78-
user_path: CGroupUserPath,
79-
pub(crate) sub_system_groups: HashMap<SubSystemType, SubSystem>,
80+
/// user cgroup relative path
81+
user_path: PathBuf,
82+
// cgroup sub systems
83+
pub sub_system_groups: HashMap<SubSystemType, SubSystem>,
84+
// cgroup v1 or v2
8085
cgroup_type: CGroupType,
8186
}
8287

@@ -191,7 +196,7 @@ impl CGroup {
191196
}
192197
}
193198

194-
#[derive(Debug, Clone, Serialize, Deserialize)]
199+
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
195200
pub enum SubSystem {
196201
Memory(MemoryCGroup),
197202
CpuSet(CpuSetCGroup),

lib/src/cgroup/cpu_cg.rs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ use std::fs::File;
3232
use std::io::{BufRead, BufReader};
3333
use std::ops::Sub;
3434
use std::path::{Path, PathBuf};
35+
use utoipa::ToSchema;
3536

36-
#[derive(Debug, Clone, Serialize, Deserialize)]
37+
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
3738
pub enum CpuSetCGroup {
3839
V1(CpuSetCGroupV1),
3940
V2(CpuSetCGroupV2),
@@ -89,7 +90,7 @@ pub fn new_cpuset_cgroup(
8990
}
9091
}
9192

92-
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
93+
#[derive(Debug, Clone, Serialize, Deserialize, Default, ToSchema)]
9394
pub struct CpuSetCGroupV2 {
9495
full_path: PathBuf,
9596
pub(crate) mems: NodeVec,
@@ -154,7 +155,7 @@ impl CpuSetCGroupV2 {
154155
}
155156
}
156157

157-
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
158+
#[derive(Debug, Clone, Serialize, Deserialize, Default, ToSchema)]
158159
pub struct CpuSetCGroupV1 {
159160
full_path: PathBuf,
160161
pub(crate) mems: NodeVec,
@@ -222,7 +223,7 @@ impl CpuSetCGroupV1 {
222223
}
223224
}
224225

225-
#[derive(Clone, Debug, Copy, Eq, PartialEq, Deserialize, Serialize)]
226+
#[derive(Clone, Debug, Copy, Eq, PartialEq, Deserialize, Serialize, ToSchema)]
226227
pub struct CpuCGroupBasicInfo {
227228
cpu_usage: u64, // unit: ns
228229
cpu_user_time: u64, // unit: ns
@@ -266,7 +267,7 @@ impl Sub for CpuCGroupBasicInfo {
266267
}
267268
}
268269

269-
#[derive(Debug, Clone, Serialize, Deserialize)]
270+
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
270271
pub enum CpuCGroup {
271272
V1(CpuCGroupV1),
272273
V2(CpuCGroupV2),
@@ -321,7 +322,7 @@ pub fn new_cpu_cgroup(mount_point: &str, user_path: &Path, cgroup_type: CGroupTy
321322
}
322323
}
323324

324-
#[derive(Clone, Debug, Deserialize, Serialize, Default)]
325+
#[derive(Clone, Debug, Deserialize, Serialize, Default, ToSchema)]
325326
pub struct CpuCGroupV2 {
326327
full_path: PathBuf,
327328
user_path: PathBuf,
@@ -348,7 +349,7 @@ pub struct CpuCGroupV2 {
348349
imc_writes: u64,
349350
}
350351

351-
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
352+
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq, ToSchema)]
352353
pub struct CpuStatsV2 {
353354
usage_usec: u64,
354355
user_usec: u64,
@@ -559,7 +560,7 @@ impl CpuCGroupV2 {
559560
}
560561
}
561562

562-
#[derive(Clone, Debug, Deserialize, Serialize, Default)]
563+
#[derive(Clone, Debug, Deserialize, Serialize, Default, ToSchema)]
563564
pub struct CpuCGroupV1 {
564565
full_path: PathBuf,
565566
user_path: PathBuf,
@@ -578,6 +579,8 @@ pub struct CpuCGroupV1 {
578579
cpu_user_usage_ratio: f32,
579580
cpu_sys_usage_ratio: f32,
580581

582+
percpu_usage: Vec<u64>,
583+
581584
cpu_nr_throttled: Option<u64>,
582585
cpu_nr_periods: Option<u64>,
583586
cpu_throttled_time: Option<u64>,
@@ -718,6 +721,18 @@ impl CpuCGroupV1 {
718721
Ok(true)
719722
}
720723

724+
pub fn update_percpu_usage(&mut self) -> common::Result<bool> {
725+
let mut path = PathBuf::from(&self.full_path);
726+
path.push("cpuacct.usage_percpu");
727+
let contents = fs::read_to_string(&path)?;
728+
self.percpu_usage = contents
729+
.split_whitespace()
730+
.map(|x| x.parse::<u64>().unwrap())
731+
.collect();
732+
733+
Ok(true)
734+
}
735+
721736
pub fn update(&mut self) {
722737
if let Err(e) = self.update_period_us() {
723738
warn!(
@@ -754,6 +769,13 @@ impl CpuCGroupV1 {
754769
self.full_path.display()
755770
);
756771
}
772+
if let Err(e) = self.update_percpu_usage() {
773+
warn!(
774+
"[cpucg] update percpu usage error: {}, path= {}",
775+
e,
776+
self.full_path.display()
777+
);
778+
}
757779
self.update_time = get_secs_since_epoch()
758780
}
759781

lib/src/cgroup/memory_cg.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use std::fs;
2727
use std::fs::File;
2828
use std::io::{BufRead, BufReader};
2929
use std::path::{Path, PathBuf};
30+
use utoipa::ToSchema;
3031

3132
pub fn new_memory_cgroup(
3233
mount_point: &str,
@@ -52,9 +53,11 @@ pub fn new_memory_cgroup(
5253
}
5354
}
5455

55-
#[derive(Debug, Clone, Serialize, Deserialize)]
56+
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
5657
pub enum MemoryCGroup {
58+
/// cgroup v1 info from cgroupfs
5759
V1(MemoryCGroupV1),
60+
/// cgroup v1 info from cgroupfs
5861
V2(MemoryCGroupV2),
5962
}
6063

@@ -99,7 +102,7 @@ impl MemoryCGroup {
99102
}
100103
}
101104

102-
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
105+
#[derive(Debug, Clone, Serialize, Deserialize, Default, ToSchema)]
103106
pub struct MemoryCGroupV2 {
104107
full_path: PathBuf,
105108
user_path: PathBuf,
@@ -118,7 +121,7 @@ pub struct MemoryCGroupV2 {
118121
update_time: u64,
119122
}
120123

121-
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
124+
#[derive(Debug, Clone, Serialize, Deserialize, Default, ToSchema)]
122125
pub struct MemEventLocalV2 {
123126
low: u64,
124127
high: u64,
@@ -127,7 +130,7 @@ pub struct MemEventLocalV2 {
127130
oom_kill: u64,
128131
}
129132

130-
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
133+
#[derive(Debug, Clone, Serialize, Deserialize, Default, ToSchema)]
131134
pub struct MemNumaStatsV2 {
132135
anon: u64,
133136
file: u64,
@@ -149,7 +152,7 @@ pub struct MemNumaStatsV2 {
149152
workingset_nodereclaim: u64,
150153
}
151154

152-
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
155+
#[derive(Debug, Clone, Serialize, Deserialize, Default, ToSchema)]
153156
pub struct MemStatsV2 {
154157
anon: u64,
155158
file: u64,
@@ -368,8 +371,8 @@ impl MemoryCGroupV2 {
368371
}
369372
}
370373

371-
#[derive(Debug, Clone, Serialize, Deserialize)]
372-
struct MemoryCGroupNumaStat {
374+
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
375+
pub struct MemoryCGroupNumaStat {
373376
numa_name: String,
374377
total: Option<u64>,
375378
file: Option<u64>,
@@ -397,7 +400,7 @@ impl From<(&str, HashMap<&str, u64>)> for MemoryCGroupNumaStat {
397400
}
398401
}
399402

400-
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
403+
#[derive(Debug, Clone, Serialize, Deserialize, Default, ToSchema)]
401404
pub struct MemoryCGroupV1 {
402405
full_path: PathBuf,
403406
user_path: PathBuf,

lib/src/cgroup/net_cg.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,20 @@ limitations under the License.
1515

1616
use crate::common;
1717
use crate::common::CGroupType;
18-
use crate::ffi::bpf::ffi::net_data;
19-
use crate::ffi::{is_bpf_moudule_valid, wrapper_get_cgroup_net_data, BPF_MODULE_CGROUP_NET};
18+
use crate::ffi::{
19+
is_bpf_moudule_valid, wrapper_get_cgroup_net_data, WrapperNetData, BPF_MODULE_CGROUP_NET,
20+
};
2021
use log::{info, warn};
2122
use serde::{Deserialize, Serialize};
2223
use std::path::{Path, PathBuf};
24+
use utoipa::ToSchema;
2325

24-
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
26+
#[derive(Debug, Clone, Serialize, Deserialize, Default, ToSchema)]
2527
pub struct NetCGroup {
2628
full_path: PathBuf,
2729
user_path: PathBuf,
28-
pub(crate) bpf_net_data: net_data,
29-
pub(crate) old_bpf_net_data: net_data,
30+
pub(crate) bpf_net_data: WrapperNetData,
31+
pub(crate) old_bpf_net_data: WrapperNetData,
3032
update_time: u64,
3133
}
3234

lib/src/cgroup/perf_event_cg.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use log::{debug, info};
2121
use serde::{Deserialize, Serialize};
2222
use std::ffi::CStr;
2323
use std::path::{Path, PathBuf};
24+
use utoipa::ToSchema;
2425

2526
pub fn new_perf_event_cgroup(
2627
mount_point: &str,
@@ -43,7 +44,7 @@ pub fn new_perf_event_cgroup(
4344
}
4445
}
4546

46-
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
47+
#[derive(Debug, Clone, Serialize, Deserialize, Default, ToSchema)]
4748
pub struct PerfEventCGroup {
4849
user_path: PathBuf,
4950
full_path: PathBuf,
@@ -166,8 +167,6 @@ mod tests_perf_event_cg {
166167
user_path
167168
));
168169
let cpi: f64 = 1.0;
169-
let icache_miss: f64 = 2.0;
170-
let l2_cache_miss: f64 = 3.0;
171170
let l3_cache_miss: f64 = 4.0;
172171
let instructions: f64 = 5.0;
173172
let cycles: f64 = 6.0;

lib/src/common.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ use once_cell::sync::Lazy;
1818
use serde::{Deserialize, Serialize};
1919
use std::error;
2020
use std::fs::File;
21+
use utoipa::ToSchema;
2122

2223
pub static MOUNT_POINT: &str = "/sys/fs/cgroup";
2324
pub(crate) type Result<T> = std::result::Result<T, Box<dyn error::Error>>;
2425

25-
#[derive(Clone, Debug, Copy, Eq, Deserialize, Serialize, PartialEq, Hash)]
26+
#[derive(Clone, Debug, Copy, Eq, Deserialize, Serialize, PartialEq, Hash, ToSchema)]
2627
pub enum CGroupType {
2728
V1,
2829
V2,

0 commit comments

Comments
 (0)