Skip to content

Commit 398bff0

Browse files
[#175]: added repr(C,packed) for monitoring structures. Fixed imports. Added error handling in event listener
1 parent a32698b commit 398bff0

File tree

5 files changed

+38
-19
lines changed

5 files changed

+38
-19
lines changed

core/common/src/buffer_type.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ unsafe impl aya::Pod for TcpPacketRegistry {}
8686
#[cfg(feature = "monitoring-structs")]
8787
pub const TASK_COMM_LEN: usize = 16; // linux/sched.h
8888
#[cfg(feature = "monitoring-structs")]
89-
#[repr(C)]
89+
#[repr(C, packed)]
9090
#[derive(Clone, Copy, Zeroable)]
9191
pub struct NetworkMetrics {
9292
pub tgid: u32,
@@ -104,7 +104,7 @@ pub struct NetworkMetrics {
104104
unsafe impl aya::Pod for NetworkMetrics {}
105105

106106
#[cfg(feature = "monitoring-structs")]
107-
#[repr(C)]
107+
#[repr(C, packed)]
108108
#[derive(Clone, Copy, Zeroable)]
109109
pub struct TimeStampMetrics {
110110
pub delta_us: u64,

core/src/components/identity/src/main.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,21 @@ use aya::{
2121
#[cfg(feature = "experimental")]
2222
use crate::helpers::scan_cgroup_cronjob;
2323

24-
use cortexbrain_common::buffer_type::read_perf_buffer;
25-
use cortexbrain_common::map_handlers::{
26-
init_bpf_maps, map_manager, map_pinner, populate_blocklist,
24+
use cortexbrain_common::{
25+
buffer_type::{BufferSize, BufferType, read_perf_buffer},
26+
constants, logger,
27+
map_handlers::BpfMapsData,
28+
map_handlers::{init_bpf_maps, map_manager, map_pinner, populate_blocklist},
29+
program_handlers::load_program,
2730
};
28-
use cortexbrain_common::program_handlers::load_program;
29-
use cortexbrain_common::{buffer_type::BufferType, map_handlers::BpfMapsData};
3031
use std::{
3132
convert::TryInto,
3233
path::Path,
3334
sync::{Arc, Mutex},
3435
};
3536

36-
use anyhow::{Context, Ok};
37-
use cortexbrain_common::buffer_type::BufferSize;
38-
use cortexbrain_common::{constants, logger};
37+
use anyhow::{Context, Ok, anyhow};
38+
3939
use std::collections::HashMap;
4040
use tokio::{fs, signal};
4141
use tracing::{error, info};
@@ -206,7 +206,12 @@ async fn event_listener(bpf_maps: BpfMapsData) -> Result<(), anyhow::Error> {
206206
// fill the input buffers with data from the PerfEventArrays
207207
for cpu_id in online_cpus().map_err(|e| anyhow::anyhow!("Error {:?}", e))? {
208208
for (name, (perf_evt_array, perf_evt_array_buffer)) in maps.iter_mut() {
209-
let buf = perf_evt_array.open(cpu_id, None)?;
209+
let buf = perf_evt_array.open(cpu_id, None).map_err(|e| {
210+
anyhow!(
211+
"Cannot create perf_event_array buffer from perf_event_array. Reason: {}",
212+
e
213+
)
214+
})?;
210215
info!(
211216
"Buffer created for map {:?} on cpu_id {:?}. Buffer size: {}",
212217
name,

core/src/components/metrics/src/helpers.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use anyhow::anyhow;
12
use aya::util::online_cpus;
23
use cortexbrain_common::map_handlers::map_manager;
34
use cortexbrain_common::{
@@ -16,7 +17,18 @@ pub async fn event_listener(bpf_maps: BpfMapsData) -> Result<(), anyhow::Error>
1617

1718
for cpu_id in cpu_count {
1819
for (name, (perf_event_array, perf_event_buffer)) in maps.iter_mut() {
19-
let buf = perf_event_array.open(cpu_id, None)?;
20+
let buf = perf_event_array.open(cpu_id, None).map_err(|e| {
21+
anyhow!(
22+
"Cannot create perf_event_array buffer from perf_event_array. Reason: {}",
23+
e
24+
)
25+
})?;
26+
info!(
27+
"Buffer created for map {:?} on cpu_id {:?}. Buffer size: {}",
28+
name,
29+
cpu_id,
30+
std::mem::size_of_val(&buf)
31+
);
2032
perf_event_buffer.push(buf);
2133
}
2234
}

core/src/components/metrics/src/main.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use anyhow::{Context, Ok};
22
use aya::Ebpf;
3-
use cortexbrain_common::constants;
43
use std::{
54
env, fs,
65
path::Path,
@@ -11,9 +10,12 @@ use tracing::{error, info};
1110
mod helpers;
1211
use crate::helpers::event_listener;
1312

14-
use cortexbrain_common::logger::otlp_logger_init;
15-
use cortexbrain_common::map_handlers::{init_bpf_maps, map_pinner};
16-
use cortexbrain_common::program_handlers::load_program;
13+
use cortexbrain_common::{
14+
constants,
15+
logger::otlp_logger_init,
16+
map_handlers::{init_bpf_maps, map_pinner},
17+
program_handlers::load_program,
18+
};
1719

1820
#[tokio::main]
1921
async fn main() -> Result<(), anyhow::Error> {

core/src/components/metrics_tracer/src/data_structures.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use aya_ebpf::{macros::map, maps::{LruPerCpuHashMap, HashMap, PerfEventArray}};
22

33
pub const TASK_COMM_LEN: usize = 16;
44

5-
5+
#[repr(C,packed)]
66
pub struct NetworkMetrics {
77
pub tgid: u32,
88
pub comm: [u8; TASK_COMM_LEN],
@@ -16,7 +16,7 @@ pub struct NetworkMetrics {
1616
pub sk_drops: i32, // Offset 136
1717
}
1818

19-
#[repr(C)]
19+
#[repr(C,packed)]
2020
#[derive(Copy, Clone)]
2121
pub struct TimeStampStartInfo {
2222
pub comm: [u8; TASK_COMM_LEN],
@@ -25,7 +25,7 @@ pub struct TimeStampStartInfo {
2525
}
2626

2727
// Event we send to userspace when latency is computed
28-
#[repr(C)]
28+
#[repr(C,packed)]
2929
#[derive(Copy, Clone)]
3030
pub struct TimeStampEvent {
3131
pub delta_us: u64,

0 commit comments

Comments
 (0)