Skip to content

Commit e768252

Browse files
committed
Use in-memory buffer and set debug to true
1 parent 974de8d commit e768252

File tree

5 files changed

+51
-42
lines changed

5 files changed

+51
-42
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ path = "tests/e2e/mod.rs"
4545
# This results in roughly a 5% reduction in performance when compiling locally vs when
4646
# compiled via the CI pipeline.
4747
[profile.release]
48-
debug = false # Do not include debug symbols in the executable.
48+
debug = true # Do not include debug symbols in the executable.
4949

5050
[profile.bench]
5151
debug = true
@@ -419,6 +419,7 @@ windows-service = "0.8.0"
419419
nix = { version = "0.26.2", default-features = false, features = ["socket", "signal"] }
420420

421421
[target.'cfg(target_os = "linux")'.dependencies]
422+
libc = "0.2.174"
422423
netlink-packet-utils = "0.5.2"
423424
netlink-packet-sock-diag = "0.4.2"
424425
netlink-packet-core = "0.7.0"

src/internal_telemetry/allocations/allocator/token.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ use tracing::Span;
99
use super::stack::GroupStack;
1010
use super::tracing::WithAllocationGroup;
1111

12+
#[cfg(all(target_os = "linux", feature = "component-probes"))]
13+
fn thread_id() -> usize {
14+
thread_local! { static TID: usize = unsafe { libc::gettid() } as usize; }
15+
TID.with(|t| *t)
16+
}
17+
1218
thread_local! {
1319
/// The currently executing allocation token.
1420
///
@@ -81,17 +87,18 @@ pub struct AllocationGroupToken {
8187
impl AllocationGroupToken {
8288
pub fn enter(&self) {
8389
_ = LOCAL_ALLOCATION_GROUP_STACK.try_with(|stack| stack.borrow_mut().push(self.id));
84-
#[cfg(feature = "component-probes")]
85-
crate::internal_telemetry::component_probes::vector_component_enter(self.id.as_raw());
90+
#[cfg(all(target_os = "linux", feature = "component-probes"))]
91+
crate::internal_telemetry::component_probes::VECTOR_COMPONENT_LABELS
92+
[thread_id() % crate::internal_telemetry::component_probes::LABELS_LEN]
93+
.store(self.id.as_raw(), std::sync::atomic::Ordering::Relaxed);
8694
}
8795

8896
pub fn exit(&self) {
8997
_ = LOCAL_ALLOCATION_GROUP_STACK.try_with(|stack| stack.borrow_mut().pop());
90-
}
91-
92-
pub fn close(&self) {
93-
#[cfg(feature = "component-probes")]
94-
crate::internal_telemetry::component_probes::vector_component_exit();
98+
#[cfg(all(target_os = "linux", feature = "component-probes"))]
99+
crate::internal_telemetry::component_probes::VECTOR_COMPONENT_LABELS
100+
[thread_id() % crate::internal_telemetry::component_probes::LABELS_LEN]
101+
.store(0, std::sync::atomic::Ordering::Relaxed);
95102
}
96103
}
97104

src/internal_telemetry/allocations/allocator/tracing.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,6 @@ where
6969
}
7070
}
7171

72-
fn on_close(&self, id: Id, ctx: Context<'_, S>) {
73-
#[cfg(feature = "component-probes")]
74-
if let Some(span_ref) = ctx.span(&id) {
75-
if let Some(token) = span_ref.extensions().get::<AllocationGroupToken>() {
76-
token.close();
77-
}
78-
}
79-
}
80-
8172
unsafe fn downcast_raw(&self, id: TypeId) -> Option<*const ()> {
8273
match id {
8374
id if id == TypeId::of::<Self>() => Some(addr_of!(self).cast::<()>()),

src/internal_telemetry/allocations/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ pub fn acquire_allocation_group_id(
206206
group_id.as_raw(),
207207
id_bytes.as_ptr(),
208208
id_bytes.len(),
209+
crate::internal_telemetry::component_probes::VECTOR_COMPONENT_LABELS.as_ptr()
210+
as *const u8,
211+
crate::internal_telemetry::component_probes::LABELS_LEN,
209212
);
210213
}
211214

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,45 @@
1+
use std::sync::atomic::AtomicU8;
2+
3+
pub const LABELS_LEN: usize = 4096;
4+
5+
/// Shared-memory array indexed by `tid % LABELS_LEN`.
6+
///
7+
/// Rust writes the component's group ID on enter and 0 on exit (~1ns, no kernel
8+
/// involvement). bpftrace reads this array on a fixed-rate profile timer
9+
/// (`profile:hz:997`) to attribute stack samples to components.
10+
///
11+
/// 4096 entries = 4KB. TID collision requires two Vector threads whose TIDs
12+
/// differ by exactly 4096 — effectively impossible for a process with ~8-32
13+
/// threads.
14+
#[no_mangle]
15+
pub static VECTOR_COMPONENT_LABELS: [AtomicU8; LABELS_LEN] = {
16+
const ZERO: AtomicU8 = AtomicU8::new(0);
17+
[ZERO; LABELS_LEN]
18+
};
19+
120
/// Uprobe attachment point: called once per component at startup to register
221
/// the mapping from allocation group ID to component name.
322
///
423
/// bpftrace attaches `uprobe:BINARY:vector_register_component` here at probe
5-
/// startup to build a `group_id → component_id` lookup table. At sample time,
6-
/// bpftrace reads the thread-local allocation group stack to get the current
7-
/// group ID and looks it up in that table.
24+
/// startup to build a `group_id -> component_id` lookup table. It also
25+
/// captures `labels_ptr` / `labels_len` on the first call so the profile
26+
/// handler can read the shared-memory array at runtime (ASLR makes the
27+
/// compile-time address unusable).
828
///
929
/// Arguments follow the C ABI so bpftrace can read them reliably:
1030
/// arg0 = group_id (u8)
1131
/// arg1/arg2 = component_id (ptr, len)
32+
/// arg3/arg4 = labels array (ptr, len)
1233
///
1334
/// `black_box` prevents LTO from eliding the call site.
1435
#[no_mangle]
1536
#[inline(never)]
16-
pub extern "C" fn vector_register_component(id: u8, name_ptr: *const u8, name_len: usize) {
17-
std::hint::black_box((id, name_ptr, name_len));
18-
}
19-
20-
/// Uprobe attachment point: called on every allocation group enter (i.e. every Tokio task poll
21-
/// that enters a component's group). bpftrace attaches here to record the active component ID
22-
/// on the current thread without needing span machinery.
23-
///
24-
/// Arguments follow the C ABI so bpftrace can read them reliably:
25-
/// arg0 = group_id (u8)
26-
#[no_mangle]
27-
#[inline(never)]
28-
pub extern "C" fn vector_component_enter(id: u8) {
29-
std::hint::black_box(id);
30-
}
31-
32-
/// Uprobe attachment point: called on every allocation group exit (i.e. every Tokio task poll
33-
/// that exits a component's group). bpftrace attaches here to clear the active component ID.
34-
#[no_mangle]
35-
#[inline(never)]
36-
pub extern "C" fn vector_component_exit() {
37-
std::hint::black_box(0u8);
37+
pub extern "C" fn vector_register_component(
38+
id: u8,
39+
name_ptr: *const u8,
40+
name_len: usize,
41+
labels_ptr: *const u8,
42+
labels_len: usize,
43+
) {
44+
std::hint::black_box((id, name_ptr, name_len, labels_ptr, labels_len));
3845
}

0 commit comments

Comments
 (0)