Skip to content

Commit 68eab7c

Browse files
committed
fix high cpu usage
1 parent 5754d83 commit 68eab7c

File tree

11 files changed

+431
-348
lines changed

11 files changed

+431
-348
lines changed

oryx-tui/src/app.rs

Lines changed: 319 additions & 231 deletions
Large diffs are not rendered by default.

oryx-tui/src/ebpf.rs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,15 @@ impl Source for RingBuffer<'_> {
5959
}
6060

6161
impl Ebpf {
62-
pub fn load_ingress(iface: String, sender: kanal::Sender<Event>, terminate: Arc<AtomicBool>) {
62+
pub fn load_ingress(
63+
iface: String,
64+
notification_sender: kanal::Sender<Event>,
65+
data_sender: kanal::Sender<[u8; RawPacket::LEN]>,
66+
terminate: Arc<AtomicBool>,
67+
) {
6368
thread::spawn({
6469
let iface = iface.to_owned();
65-
let sender = sender.clone();
70+
let notification_sender = notification_sender.clone();
6671

6772
move || {
6873
let rlim = libc::rlimit {
@@ -81,7 +86,7 @@ impl Ebpf {
8186
Notification::send(
8287
format!("Failed to load the ingress eBPF bytecode\n {}", e),
8388
NotificationLevel::Error,
84-
sender,
89+
notification_sender,
8590
)
8691
.unwrap();
8792
return;
@@ -97,7 +102,7 @@ impl Ebpf {
97102
Notification::send(
98103
format!("Failed to load the ingress eBPF bytecode\n {}", e),
99104
NotificationLevel::Error,
100-
sender,
105+
notification_sender,
101106
)
102107
.unwrap();
103108
return;
@@ -116,7 +121,7 @@ impl Ebpf {
116121
e
117122
),
118123
NotificationLevel::Error,
119-
sender,
124+
notification_sender,
120125
)
121126
.unwrap();
122127
return;
@@ -129,7 +134,7 @@ impl Ebpf {
129134
e
130135
),
131136
NotificationLevel::Error,
132-
sender,
137+
notification_sender,
133138
)
134139
.unwrap();
135140
return;
@@ -168,7 +173,7 @@ impl Ebpf {
168173
}
169174
let packet: [u8; RawPacket::LEN] =
170175
item.to_owned().try_into().unwrap();
171-
sender.send(Event::Packet(packet)).ok();
176+
data_sender.send(packet).ok();
172177
}
173178
}
174179
}
@@ -181,10 +186,15 @@ impl Ebpf {
181186
});
182187
}
183188

184-
pub fn load_egress(iface: String, sender: kanal::Sender<Event>, terminate: Arc<AtomicBool>) {
189+
pub fn load_egress(
190+
iface: String,
191+
notification_sender: kanal::Sender<Event>,
192+
data_sender: kanal::Sender<[u8; RawPacket::LEN]>,
193+
terminate: Arc<AtomicBool>,
194+
) {
185195
thread::spawn({
186196
let iface = iface.to_owned();
187-
let sender = sender.clone();
197+
let notification_sender = notification_sender.clone();
188198

189199
move || {
190200
let rlim = libc::rlimit {
@@ -203,7 +213,7 @@ impl Ebpf {
203213
Notification::send(
204214
format!("Fail to load the egress eBPF bytecode\n {}", e),
205215
NotificationLevel::Error,
206-
sender,
216+
notification_sender,
207217
)
208218
.unwrap();
209219
return;
@@ -219,7 +229,7 @@ impl Ebpf {
219229
Notification::send(
220230
format!("Failed to load the egress eBPF bytecode\n {}", e),
221231
NotificationLevel::Error,
222-
sender,
232+
notification_sender,
223233
)
224234
.unwrap();
225235
return;
@@ -234,7 +244,7 @@ impl Ebpf {
234244
Notification::send(
235245
format!("Fail to load the egress eBPF program to the kernel\n{}", e),
236246
NotificationLevel::Error,
237-
sender,
247+
notification_sender,
238248
)
239249
.unwrap();
240250
return;
@@ -247,7 +257,7 @@ impl Ebpf {
247257
e
248258
),
249259
NotificationLevel::Error,
250-
sender,
260+
notification_sender,
251261
)
252262
.unwrap();
253263
return;
@@ -286,7 +296,7 @@ impl Ebpf {
286296
}
287297
let packet: [u8; RawPacket::LEN] =
288298
item.to_owned().try_into().unwrap();
289-
sender.send(Event::Packet(packet)).ok();
299+
data_sender.send(packet).ok();
290300
}
291301
}
292302
}

oryx-tui/src/event.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ pub enum Event {
1212
Key(KeyEvent),
1313
Mouse(MouseEvent),
1414
Resize(u16, u16),
15-
Packet([u8; 72]),
1615
Notification(Notification),
1716
Reset,
1817
}

oryx-tui/src/filters/fuzzy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use tui_input::Input;
77

88
use oryx_common::AppPacket;
99

10-
#[derive(Debug, Default)]
10+
#[derive(Debug, Clone, Default)]
1111
pub struct Fuzzy {
1212
enabled: bool,
1313
paused: bool,

oryx-tui/src/filters/link.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use std::fmt::Display;
1+
use std::{
2+
fmt::Display,
3+
sync::{Arc, Mutex},
4+
};
25

36
use ratatui::{
47
layout::{Alignment, Constraint, Direction, Flex, Layout, Rect},
@@ -15,7 +18,7 @@ pub const NB_LINK_PROTOCOL: u16 = 1;
1518
pub struct LinkFilter {
1619
pub state: TableState,
1720
pub selected_protocols: Vec<LinkProtocol>,
18-
pub applied_protocols: Vec<LinkProtocol>,
21+
pub applied_protocols: Arc<Mutex<Vec<LinkProtocol>>>,
1922
}
2023

2124
#[derive(Debug, Copy, Clone, PartialEq)]
@@ -34,14 +37,15 @@ impl Default for LinkFilter {
3437
Self {
3538
state: TableState::default(),
3639
selected_protocols: vec![LinkProtocol::Arp],
37-
applied_protocols: Vec::new(),
40+
applied_protocols: Arc::new(Mutex::new(Vec::new())),
3841
}
3942
}
4043
}
4144

4245
impl LinkFilter {
4346
pub fn apply(&mut self) {
44-
self.applied_protocols = self.selected_protocols.clone();
47+
let mut applied_protocols = self.applied_protocols.lock().unwrap();
48+
*applied_protocols = self.selected_protocols.clone();
4549
self.selected_protocols.clear();
4650
}
4751

oryx-tui/src/filters/network.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use std::fmt::Display;
1+
use std::{
2+
fmt::Display,
3+
sync::{Arc, Mutex},
4+
};
25

36
use ratatui::{
47
layout::{Alignment, Constraint, Direction, Flex, Layout, Rect},
@@ -15,7 +18,7 @@ pub const NB_NETWORK_PROTOCOL: u16 = 3;
1518
pub struct NetworkFilter {
1619
pub state: TableState,
1720
pub selected_protocols: Vec<NetworkProtocol>,
18-
pub applied_protocols: Vec<NetworkProtocol>,
21+
pub applied_protocols: Arc<Mutex<Vec<NetworkProtocol>>>,
1922
}
2023

2124
impl Default for NetworkFilter {
@@ -27,7 +30,7 @@ impl Default for NetworkFilter {
2730
NetworkProtocol::Ipv6,
2831
NetworkProtocol::Icmp,
2932
],
30-
applied_protocols: Vec::new(),
33+
applied_protocols: Arc::new(Mutex::new(Vec::new())),
3134
}
3235
}
3336
}
@@ -51,7 +54,8 @@ impl Display for NetworkProtocol {
5154

5255
impl NetworkFilter {
5356
pub fn apply(&mut self) {
54-
self.applied_protocols = self.selected_protocols.clone();
57+
let mut applied_protocols = self.applied_protocols.lock().unwrap();
58+
*applied_protocols = self.selected_protocols.clone();
5559
self.selected_protocols.clear();
5660
}
5761
pub fn render(&mut self, frame: &mut Frame, block: Rect, focused_block: &FocusedBlock) {

oryx-tui/src/filters/transport.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use std::fmt::Display;
1+
use std::{
2+
fmt::Display,
3+
sync::{Arc, Mutex},
4+
};
25

36
use ratatui::{
47
layout::{Alignment, Constraint, Direction, Flex, Layout, Rect},
@@ -15,7 +18,7 @@ pub const NB_TRANSPORT_PROTOCOL: u16 = 2;
1518
pub struct TransportFilter {
1619
pub state: TableState,
1720
pub selected_protocols: Vec<TransportProtocol>,
18-
pub applied_protocols: Vec<TransportProtocol>,
21+
pub applied_protocols: Arc<Mutex<Vec<TransportProtocol>>>,
1922
}
2023

2124
#[derive(Debug, Copy, Clone, PartialEq)]
@@ -38,14 +41,15 @@ impl Default for TransportFilter {
3841
Self {
3942
state: TableState::default(),
4043
selected_protocols: vec![TransportProtocol::TCP, TransportProtocol::UDP],
41-
applied_protocols: Vec::new(),
44+
applied_protocols: Arc::new(Mutex::new(Vec::new())),
4245
}
4346
}
4447
}
4548

4649
impl TransportFilter {
4750
pub fn apply(&mut self) {
48-
self.applied_protocols = self.selected_protocols.clone();
51+
let mut applied_protocols = self.applied_protocols.lock().unwrap();
52+
*applied_protocols = self.selected_protocols.clone();
4953
self.selected_protocols.clear();
5054
}
5155

0 commit comments

Comments
 (0)