Skip to content

Commit

Permalink
fix high cpu usage
Browse files Browse the repository at this point in the history
  • Loading branch information
pythops committed Sep 14, 2024
1 parent 5754d83 commit 68eab7c
Show file tree
Hide file tree
Showing 11 changed files with 431 additions and 348 deletions.
550 changes: 319 additions & 231 deletions oryx-tui/src/app.rs

Large diffs are not rendered by default.

38 changes: 24 additions & 14 deletions oryx-tui/src/ebpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,15 @@ impl Source for RingBuffer<'_> {
}

impl Ebpf {
pub fn load_ingress(iface: String, sender: kanal::Sender<Event>, terminate: Arc<AtomicBool>) {
pub fn load_ingress(
iface: String,
notification_sender: kanal::Sender<Event>,
data_sender: kanal::Sender<[u8; RawPacket::LEN]>,
terminate: Arc<AtomicBool>,
) {
thread::spawn({
let iface = iface.to_owned();
let sender = sender.clone();
let notification_sender = notification_sender.clone();

move || {
let rlim = libc::rlimit {
Expand All @@ -81,7 +86,7 @@ impl Ebpf {
Notification::send(
format!("Failed to load the ingress eBPF bytecode\n {}", e),
NotificationLevel::Error,
sender,
notification_sender,
)
.unwrap();
return;
Expand All @@ -97,7 +102,7 @@ impl Ebpf {
Notification::send(
format!("Failed to load the ingress eBPF bytecode\n {}", e),
NotificationLevel::Error,
sender,
notification_sender,
)
.unwrap();
return;
Expand All @@ -116,7 +121,7 @@ impl Ebpf {
e
),
NotificationLevel::Error,
sender,
notification_sender,
)
.unwrap();
return;
Expand All @@ -129,7 +134,7 @@ impl Ebpf {
e
),
NotificationLevel::Error,
sender,
notification_sender,
)
.unwrap();
return;
Expand Down Expand Up @@ -168,7 +173,7 @@ impl Ebpf {
}
let packet: [u8; RawPacket::LEN] =
item.to_owned().try_into().unwrap();
sender.send(Event::Packet(packet)).ok();
data_sender.send(packet).ok();
}
}
}
Expand All @@ -181,10 +186,15 @@ impl Ebpf {
});
}

pub fn load_egress(iface: String, sender: kanal::Sender<Event>, terminate: Arc<AtomicBool>) {
pub fn load_egress(
iface: String,
notification_sender: kanal::Sender<Event>,
data_sender: kanal::Sender<[u8; RawPacket::LEN]>,
terminate: Arc<AtomicBool>,
) {
thread::spawn({
let iface = iface.to_owned();
let sender = sender.clone();
let notification_sender = notification_sender.clone();

move || {
let rlim = libc::rlimit {
Expand All @@ -203,7 +213,7 @@ impl Ebpf {
Notification::send(
format!("Fail to load the egress eBPF bytecode\n {}", e),
NotificationLevel::Error,
sender,
notification_sender,
)
.unwrap();
return;
Expand All @@ -219,7 +229,7 @@ impl Ebpf {
Notification::send(
format!("Failed to load the egress eBPF bytecode\n {}", e),
NotificationLevel::Error,
sender,
notification_sender,
)
.unwrap();
return;
Expand All @@ -234,7 +244,7 @@ impl Ebpf {
Notification::send(
format!("Fail to load the egress eBPF program to the kernel\n{}", e),
NotificationLevel::Error,
sender,
notification_sender,
)
.unwrap();
return;
Expand All @@ -247,7 +257,7 @@ impl Ebpf {
e
),
NotificationLevel::Error,
sender,
notification_sender,
)
.unwrap();
return;
Expand Down Expand Up @@ -286,7 +296,7 @@ impl Ebpf {
}
let packet: [u8; RawPacket::LEN] =
item.to_owned().try_into().unwrap();
sender.send(Event::Packet(packet)).ok();
data_sender.send(packet).ok();
}
}
}
Expand Down
1 change: 0 additions & 1 deletion oryx-tui/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ pub enum Event {
Key(KeyEvent),
Mouse(MouseEvent),
Resize(u16, u16),
Packet([u8; 72]),
Notification(Notification),
Reset,
}
Expand Down
2 changes: 1 addition & 1 deletion oryx-tui/src/filters/fuzzy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use tui_input::Input;

use oryx_common::AppPacket;

#[derive(Debug, Default)]
#[derive(Debug, Clone, Default)]
pub struct Fuzzy {
enabled: bool,
paused: bool,
Expand Down
12 changes: 8 additions & 4 deletions oryx-tui/src/filters/link.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::fmt::Display;
use std::{
fmt::Display,
sync::{Arc, Mutex},
};

use ratatui::{
layout::{Alignment, Constraint, Direction, Flex, Layout, Rect},
Expand All @@ -15,7 +18,7 @@ pub const NB_LINK_PROTOCOL: u16 = 1;
pub struct LinkFilter {
pub state: TableState,
pub selected_protocols: Vec<LinkProtocol>,
pub applied_protocols: Vec<LinkProtocol>,
pub applied_protocols: Arc<Mutex<Vec<LinkProtocol>>>,
}

#[derive(Debug, Copy, Clone, PartialEq)]
Expand All @@ -34,14 +37,15 @@ impl Default for LinkFilter {
Self {
state: TableState::default(),
selected_protocols: vec![LinkProtocol::Arp],
applied_protocols: Vec::new(),
applied_protocols: Arc::new(Mutex::new(Vec::new())),
}
}
}

impl LinkFilter {
pub fn apply(&mut self) {
self.applied_protocols = self.selected_protocols.clone();
let mut applied_protocols = self.applied_protocols.lock().unwrap();
*applied_protocols = self.selected_protocols.clone();
self.selected_protocols.clear();
}

Expand Down
12 changes: 8 additions & 4 deletions oryx-tui/src/filters/network.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::fmt::Display;
use std::{
fmt::Display,
sync::{Arc, Mutex},
};

use ratatui::{
layout::{Alignment, Constraint, Direction, Flex, Layout, Rect},
Expand All @@ -15,7 +18,7 @@ pub const NB_NETWORK_PROTOCOL: u16 = 3;
pub struct NetworkFilter {
pub state: TableState,
pub selected_protocols: Vec<NetworkProtocol>,
pub applied_protocols: Vec<NetworkProtocol>,
pub applied_protocols: Arc<Mutex<Vec<NetworkProtocol>>>,
}

impl Default for NetworkFilter {
Expand All @@ -27,7 +30,7 @@ impl Default for NetworkFilter {
NetworkProtocol::Ipv6,
NetworkProtocol::Icmp,
],
applied_protocols: Vec::new(),
applied_protocols: Arc::new(Mutex::new(Vec::new())),
}
}
}
Expand All @@ -51,7 +54,8 @@ impl Display for NetworkProtocol {

impl NetworkFilter {
pub fn apply(&mut self) {
self.applied_protocols = self.selected_protocols.clone();
let mut applied_protocols = self.applied_protocols.lock().unwrap();
*applied_protocols = self.selected_protocols.clone();
self.selected_protocols.clear();
}
pub fn render(&mut self, frame: &mut Frame, block: Rect, focused_block: &FocusedBlock) {
Expand Down
12 changes: 8 additions & 4 deletions oryx-tui/src/filters/transport.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::fmt::Display;
use std::{
fmt::Display,
sync::{Arc, Mutex},
};

use ratatui::{
layout::{Alignment, Constraint, Direction, Flex, Layout, Rect},
Expand All @@ -15,7 +18,7 @@ pub const NB_TRANSPORT_PROTOCOL: u16 = 2;
pub struct TransportFilter {
pub state: TableState,
pub selected_protocols: Vec<TransportProtocol>,
pub applied_protocols: Vec<TransportProtocol>,
pub applied_protocols: Arc<Mutex<Vec<TransportProtocol>>>,
}

#[derive(Debug, Copy, Clone, PartialEq)]
Expand All @@ -38,14 +41,15 @@ impl Default for TransportFilter {
Self {
state: TableState::default(),
selected_protocols: vec![TransportProtocol::TCP, TransportProtocol::UDP],
applied_protocols: Vec::new(),
applied_protocols: Arc::new(Mutex::new(Vec::new())),
}
}
}

impl TransportFilter {
pub fn apply(&mut self) {
self.applied_protocols = self.selected_protocols.clone();
let mut applied_protocols = self.applied_protocols.lock().unwrap();
*applied_protocols = self.selected_protocols.clone();
self.selected_protocols.clear();
}

Expand Down
Loading

0 comments on commit 68eab7c

Please sign in to comment.