Skip to content

Commit

Permalink
make BPF non-optional on Linux (#336)
Browse files Browse the repository at this point in the history
Changes BPF to be non-optional on Linux. Drops fall-back samplers
so that metric provenance is clear and so sampler overhead is
consistent.
  • Loading branch information
brayniac authored Sep 13, 2024
1 parent c71a90f commit 72f79d9
Show file tree
Hide file tree
Showing 30 changed files with 31 additions and 496 deletions.
10 changes: 0 additions & 10 deletions .github/workflows/cargo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ jobs:
matrix:
os: [ ubuntu-latest, ubuntu-20.04, macos-latest ]
profile: [ release, debug ]
features: [ default, no-default ]
steps:
- uses: actions/checkout@v3
- uses: ./.github/actions/setup-rust
Expand All @@ -118,15 +117,6 @@ jobs:
run: echo 'FLAGS=' >> $GITHUB_ENV
shell: bash

- name: Update feature flags
if: ${{ matrix.features == 'default' }}
run: echo 'FEATURE_FLAGS=--features default' >> $GITHUB_ENV
shell: bash
- name: Update feature flags
if: ${{ matrix.features == 'no-default' }}
run: echo 'FEATURE_FLAGS=--no-default-features' >> $GITHUB_ENV
shell: bash

- name: Install dependencies
if: ${{ matrix.os != 'macos-latest' }}
shell: bash
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## [Unreleased]

### Changed

- BPF is now non-optional on Linux. (#336)

## [3.18.1] - 2024-08-09

### Changed
Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 4 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
description = "High resolution systems performance telemetry agent"

[workspace.package]
version = "3.18.2-alpha.11"
version = "4.0.0-alpha.1"
license = "MIT OR Apache-2.0"

[dependencies]
Expand Down Expand Up @@ -41,18 +41,13 @@ async-trait = "0.1.81"
parking_lot = "0.12.3"

[target.'cfg(target_os = "linux")'.dependencies]
libbpf-rs = { version = "0.24.2", optional = true }
libbpf-sys = { version = "1.4.3", optional = true }
libbpf-rs = { version = "0.24.2" }
libbpf-sys = { version = "1.4.3" }
perf-event2 = "0.7.2"
nvml-wrapper = "0.9.0"

[target.'cfg(target_os = "linux")'.build-dependencies]
libbpf-cargo = { version = "0.24.2", optional = true }

[features]
all = ["bpf"]
default = ["bpf"]
bpf = ["libbpf-cargo","libbpf-rs","libbpf-sys"]
libbpf-cargo = { version = "0.24.2" }

[profile.bench]
debug = true
Expand Down
4 changes: 2 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
fn main() {
#[cfg(all(feature = "bpf", target_os = "linux"))]
#[cfg(target_os = "linux")]
bpf::generate();
}

#[cfg(all(feature = "bpf", target_os = "linux"))]
#[cfg(target_os = "linux")]
mod bpf {
use libbpf_cargo::SkeletonBuilder;

Expand Down
16 changes: 4 additions & 12 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ histogram_grouping_power = 4
# collection for that sampler. Setting the default to false requires that
# individual sampler configs are used to opt-in to collection.
enabled = true
# Controls whether BPF sampler will be used. When a metric can be collected
# without BPF, that sampler will be used instead. Otherwise, the sampler will
# effectively be disabled.
bpf = true
# The collection interval for the sampler. Shorter intervals result in less
# error when calculating rates and allow for more accurately capturing bursts in
# the related percentile metrics.
Expand All @@ -83,17 +79,14 @@ enabled = true
[samplers.block_io_requests]
enabled = true

# Instruments CPU frequency, instructions, and cycles using BPF perf events or
# frequency only via /proc/cpuinfo
# Instruments CPU frequency, instructions, and cycles using perf events
[samplers.cpu_perf]
enabled = true
bpf = true

# Instruments CPU usage by state with BPF or by reading /proc/stat on linux
# Instruments CPU usage by state with BPF
# On macos host_processor_info() is used
[samplers.cpu_usage]
enabled = true
bpf = true

# Instruments the number of currently open file descriptors using
# /proc/sys/fs/file-nr
Expand Down Expand Up @@ -161,8 +154,7 @@ enabled = true
[samplers.tcp_retransmit]
enabled = true

# Samples TCP traffic using either a BPF sampler or /proc/net/snmp to provide
# metrics for TX/RX bytes and packets
# Samples TCP traffic using a BPF sampler to get metrics for TX/RX bytes and
# packets
[samplers.tcp_traffic]
enabled = true
bpf = true
2 changes: 1 addition & 1 deletion src/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[cfg(all(feature = "bpf", target_os = "linux"))]
#[cfg(target_os = "linux")]
pub mod bpf;

pub mod classic;
Expand Down
9 changes: 0 additions & 9 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,6 @@ impl Config {
.unwrap_or(self.defaults.enabled.unwrap_or(enabled()))
}

pub fn bpf(&self, name: &str) -> bool {
self.samplers
.get(name)
.and_then(|v| v.bpf)
.unwrap_or(self.defaults.bpf.unwrap_or(enabled()))
}

pub fn interval(&self, name: &str) -> Duration {
let interval = self
.samplers
Expand Down Expand Up @@ -261,8 +254,6 @@ pub struct SamplerConfig {
#[serde(default)]
enabled: Option<bool>,
#[serde(default)]
bpf: Option<bool>,
#[serde(default)]
interval: Option<String>,
}

Expand Down
2 changes: 1 addition & 1 deletion src/samplers/block_io/linux/latency/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::*;
#[distributed_slice(ASYNC_SAMPLERS)]
fn spawn(config: Arc<Config>, runtime: &Runtime) {
// check if sampler should be enabled
if !(config.enabled(NAME) && config.bpf(NAME)) {
if !config.enabled(NAME) {
return;
}

Expand Down
3 changes: 0 additions & 3 deletions src/samplers/block_io/linux/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
#[cfg(feature = "bpf")]
mod latency;

#[cfg(feature = "bpf")]
mod requests;
2 changes: 1 addition & 1 deletion src/samplers/block_io/linux/requests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::*;
#[distributed_slice(ASYNC_SAMPLERS)]
fn spawn(config: Arc<Config>, runtime: &Runtime) {
// check if sampler should be enabled
if !(config.enabled(NAME) && config.bpf(NAME)) {
if !config.enabled(NAME) {
return;
}

Expand Down
10 changes: 1 addition & 9 deletions src/samplers/cpu/linux/perf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,13 @@ use perf_event::{Builder, ReadFormat};
use samplers::hwinfo::hardware_info;

mod perf_group;
mod proc_cpuinfo;

use perf_group::*;
use proc_cpuinfo::*;

#[distributed_slice(SAMPLERS)]
fn init(config: Arc<Config>) -> Box<dyn Sampler> {
// try to initialize the perf counter based sampler that provides more info
// with lower overhead
if let Ok(perf) = Perf::new(config.clone()) {
Box::new(perf)
// try to fallback to the /proc/cpuinfo based sampler if perf events are not
// supported
} else if let Ok(cpuinfo) = ProcCpuinfo::new(config) {
Box::new(cpuinfo)
} else {
Box::new(Nop {})
}
Expand All @@ -41,7 +33,7 @@ pub struct Perf {
impl Perf {
pub fn new(config: Arc<Config>) -> Result<Self, ()> {
// check if sampler should be enabled
if !(config.enabled(NAME) && config.bpf(NAME)) {
if !config.enabled(NAME) {
return Err(());
}

Expand Down
79 changes: 0 additions & 79 deletions src/samplers/cpu/linux/perf/proc_cpuinfo.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/samplers/cpu/linux/usage/bpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl OnlineCores {
impl CpuUsage {
pub fn new(config: Arc<Config>) -> Result<Self, ()> {
// check if sampler should be enabled
if !(config.enabled(NAME) && config.bpf(NAME)) {
if !config.enabled(NAME) {
return Err(());
}

Expand Down
23 changes: 0 additions & 23 deletions src/samplers/cpu/linux/usage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,14 @@ use crate::*;

const NAME: &str = "cpu_usage";

#[cfg(feature = "bpf")]
mod bpf;

mod proc_stat;

#[cfg(feature = "bpf")]
use bpf::*;

use proc_stat::*;

#[cfg(feature = "bpf")]
#[distributed_slice(SAMPLERS)]
fn init(config: Arc<Config>) -> Box<dyn Sampler> {
// try to initialize the bpf based sampler
if let Ok(s) = CpuUsage::new(config.clone()) {
Box::new(s)
// try to fallback to the /proc/stat based sampler if there was an error
} else if let Ok(s) = ProcStat::new(config) {
Box::new(s)
} else {
Box::new(Nop {})
}
}

#[cfg(not(feature = "bpf"))]
#[distributed_slice(SAMPLERS)]
fn init(config: Arc<Config>) -> Box<dyn Sampler> {
// try to use the /proc/stat based sampler since BPF was not enabled for
// this build
if let Ok(s) = ProcStat::new(config) {
Box::new(s)
} else {
Box::new(Nop {})
}
Expand Down
Loading

0 comments on commit 72f79d9

Please sign in to comment.