This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
/
biolatpcts.rs
139 lines (118 loc) · 4.24 KB
/
biolatpcts.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
// This example is a remake of examples/tracing/biolatpcts.py of bcc using
// RedBPF. The purpose of this program is to show how to use kprobe and percpu
// array in userspace. This source code deals with userspace part only. You can
// find another half, a BPF program, at example-probes/biolatpcts/main.rs
use std::cmp;
use std::time::Duration;
use tokio::time::sleep;
use tracing::{error, Level};
use tracing_subscriber::FmtSubscriber;
use redbpf::load::Loader;
use redbpf::PerCpuArray;
fn find_pct(req: f32, total: u64, slots: &[u64], mut idx: usize, mut counted: u64) -> (usize, u64) {
while idx > 0 {
idx -= 1;
if slots[idx] > 0 {
counted += slots[idx];
if counted as f32 / total as f32 * 100.0 >= 100.0 - req {
break;
}
}
}
(idx, counted)
}
fn calc_lat_pct(
req_pcts: &[u64],
total: u64,
lat_100ms: &[u64],
lat_1ms: &[u64],
lat_10us: &[u64],
) -> Box<[u64]> {
let mut pcts = vec![0u64; req_pcts.len()].into_boxed_slice();
if total == 0 {
return pcts;
}
let data = [(100_000, lat_100ms), (1000, lat_1ms), (10, lat_10us)];
let mut data_sel = 0;
let mut idx = 100;
let mut counted = 0;
for pct_idx in (0..req_pcts.len()).rev() {
let req = req_pcts[pct_idx] as f32;
let mut gran;
loop {
let last_counted = counted;
gran = data[data_sel].0;
let slots = data[data_sel].1;
let pct = find_pct(req, total, &slots, idx, counted);
idx = pct.0;
counted = pct.1;
if idx > 0 || data_sel == data.len() - 1 {
break;
}
counted = last_counted;
data_sel += 1;
idx = 100;
}
pcts[pct_idx] = gran * idx as u64 + gran / 2;
}
return pcts;
}
#[tokio::main(flavor = "current_thread")]
async fn main() -> ! {
let subscriber = FmtSubscriber::builder()
.with_max_level(Level::TRACE)
.finish();
tracing::subscriber::set_global_default(subscriber).unwrap();
if unsafe { libc::geteuid() != 0 } {
error!("You must be root to use eBPF!");
std::process::exit(1);
}
let mut loaded = Loader::load(include_bytes!(concat!(
env!("OUT_DIR"),
"/target/bpf/programs/biolatpcts/biolatpcts.elf"
)))
.expect("error loading BPF program");
for kp in loaded.kprobes_mut() {
kp.attach_kprobe(&kp.name(), 0).expect(&format!(
"error attaching kprobe BPF program to kernel function {}",
kp.name()
));
}
let cur_lat_100ms =
PerCpuArray::<u64>::new(loaded.map("lat_100ms").expect("array lat_100ms not found"))
.expect("error creating PerCpuArray in userspace");
let cur_lat_1ms =
PerCpuArray::<u64>::new(loaded.map("lat_1ms").expect("array lat_1ms not found"))
.expect("error creating PerCpuArray in userspace");
let cur_lat_10us =
PerCpuArray::<u64>::new(loaded.map("lat_10us").expect("array lat_10us not found"))
.expect("error creating PerCpuArray in userspace");
let mut last_lat_100ms = [0; 100];
let mut last_lat_1ms = [0; 100];
let mut last_lat_10us = [0; 100];
let mut lat_100ms = [0; 100];
let mut lat_1ms = [0; 100];
let mut lat_10us = [0; 100];
loop {
sleep(Duration::from_secs(3)).await;
let mut lat_total = 0;
for i in 0usize..100 {
let v: u64 = cur_lat_100ms.get(i as u32).unwrap().iter().sum();
lat_100ms[i] = cmp::max(v - last_lat_100ms[i], 0);
last_lat_100ms[i] = v;
let v: u64 = cur_lat_1ms.get(i as u32).unwrap().iter().sum();
lat_1ms[i] = cmp::max(v - last_lat_1ms[i], 0);
last_lat_1ms[i] = v;
let v: u64 = cur_lat_10us.get(i as u32).unwrap().iter().sum();
lat_10us[i] = cmp::max(v - last_lat_10us[i], 0);
last_lat_10us[i] = v;
lat_total += lat_100ms[i];
}
let target_pcts = [50, 75, 90, 99];
let pcts = calc_lat_pct(&target_pcts, lat_total, &lat_100ms, &lat_1ms, &lat_10us);
for i in 0..target_pcts.len() {
print!("p{}={}us ", target_pcts[i], pcts[i]);
}
println!();
}
}