Skip to content

Commit

Permalink
Merge pull request #650 from aya-rs/test-cleanup
Browse files Browse the repository at this point in the history
Remove async feature; misc test cleanup
  • Loading branch information
tamird committed Jul 14, 2023
2 parents 0766e70 + 8e9712a commit 61608e6
Show file tree
Hide file tree
Showing 19 changed files with 136 additions and 112 deletions.
2 changes: 0 additions & 2 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
[alias]
xtask = "run --package xtask --"
build-bpfel = "build -Zbuild-std=core --target=bpfel-unknown-none"
build-bpfeb = "build -Zbuild-std=core --target=bpfeb-unknown-none"

[target.armv7-unknown-linux-gnueabi]
linker = "arm-linux-gnueabi-gcc"
Expand Down
13 changes: 9 additions & 4 deletions .github/workflows/build-aya-bpf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ env:
jobs:
build:
strategy:
fail-fast: false
matrix:
arch:
- x86_64
- aarch64
- arm
- riscv64
target:
- bpfel-unknown-none
- bpfeb-unknown-none
runs-on: ubuntu-20.04

steps:
Expand All @@ -37,11 +41,12 @@ jobs:
- name: Prereqs
run: cargo install bpf-linker

- uses: taiki-e/install-action@cargo-hack
- name: Build
env:
CARGO_CFG_BPF_TARGET_ARCH: ${{ matrix.arch }}
run: |
cargo build-bpfel -p aya-bpf --verbose
cargo build-bpfeb -p aya-bpf --verbose
cargo build-bpfel -p aya-log-ebpf --verbose
cargo build-bpfeb -p aya-log-ebpf --verbose
cargo hack build --package aya-bpf --package aya-log-ebpf \
--feature-powerset \
--target ${{ matrix.target }} \
-Z build-std=core
32 changes: 23 additions & 9 deletions .github/workflows/build-aya.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ env:
jobs:
build-test:
strategy:
fail-fast: false
matrix:
arch:
- x86_64-unknown-linux-gnu
Expand All @@ -29,20 +30,33 @@ jobs:
- uses: dtolnay/rust-toolchain@master
with:
toolchain: stable
targets: ${{ matrix.arch }}

- uses: Swatinem/rust-cache@v2

- uses: taiki-e/install-action@cargo-hack
- name: Check
run: cargo hack check --all-targets --feature-powerset --ignore-private

- uses: Swatinem/rust-cache@v2
- name: Prereqs
run: cargo install cross --git https://github.com/cross-rs/cross
- uses: taiki-e/setup-cross-toolchain-action@v1
with:
target: ${{ matrix.arch }}

- name: Build
run: cross build --verbose --target ${{matrix.arch}}

- name: Run test
run: |
cargo hack build --all-targets --feature-powerset \
--exclude aya-bpf \
--exclude aya-bpf-bindings \
--exclude aya-log-ebpf \
--exclude integration-ebpf \
--workspace
- name: Test
env:
RUST_BACKTRACE: full
run: |
cross test --verbose --target ${{matrix.arch}}
cargo hack test --all-targets --feature-powerset \
--exclude aya-bpf \
--exclude aya-bpf-bindings \
--exclude aya-log-ebpf \
--exclude integration-ebpf \
--exclude integration-test \
--workspace
5 changes: 4 additions & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ jobs:
toolchain: nightly
components: rustfmt, clippy, miri, rust-src

- uses: Swatinem/rust-cache@v2

- name: Check formatting
run: cargo fmt --all -- --check

- uses: taiki-e/install-action@cargo-hack
- name: Run clippy
run: cargo clippy --all-targets --workspace -- --deny warnings
run: cargo hack clippy --all-targets --feature-powerset --workspace -- --deny warnings

- name: Run miri
run: cargo miri test --all-targets
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ jobs:
with:
tag_name: ${{ github.ref }}
release_name: ${{ github.ref }}
body: ${{steps.github_release.outputs.changelog}}
body: ${{ steps.github_release.outputs.changelog }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
17 changes: 14 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[workspace]
members = [
"aya",
"aya-obj",
"aya-tool",
"aya-log",
"aya-log-common",
"aya-log-parser",
"aya-obj",
"aya-tool",
"test/integration-test",
"xtask",

Expand All @@ -19,15 +19,26 @@ members = [
"bpf/aya-log-ebpf",
"test/integration-ebpf",
]

resolver = "2"

default-members = [
"aya",
"aya-bpf-macros",
"aya-log",
"aya-log-common",
"aya-log-parser",
"aya-obj",
"aya-tool",
"aya-log",
# test/integration-test is omitted; it must be built with xtask.
"xtask",

"aya-bpf-macros",
"aya-log-ebpf-macros",

# ebpf crates are omitted; they must be built with:
# --target bpfe{b,l}-unknown-none
# CARGO_CFG_BPF_TARGET_ARCH={x86_64,aarch64,arm,riscv64}
]

[profile.dev]
Expand Down
4 changes: 2 additions & 2 deletions aya-log-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,8 @@ mod test {
#[test]
fn log_value_length_sufficient() {
assert!(
LOG_BUF_CAPACITY >= LogValueLength::MAX.into(),
"{} < {}",
LOG_BUF_CAPACITY <= LogValueLength::MAX.into(),
"{} > {}",
LOG_BUF_CAPACITY,
LogValueLength::MAX
);
Expand Down
3 changes: 1 addition & 2 deletions aya-log-parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ mod test {
Fragment::Parameter(Parameter {
hint: DisplayHint::Ip
}),
Fragment::Literal(" lmao ".into()),
Fragment::Literal(" {{}} {{something}}".into()),
Fragment::Literal(" lmao {} {something}".into()),
])
);
assert!(parse("foo {:}").is_err());
Expand Down
10 changes: 4 additions & 6 deletions aya-log/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ use thiserror::Error;

use aya::{
maps::{
perf::{AsyncPerfEventArray, PerfBufferError},
perf::{AsyncPerfEventArray, Events, PerfBufferError},
MapError,
},
util::online_cpus,
Expand Down Expand Up @@ -121,12 +121,10 @@ impl BpfLogger {
let mut buffers = vec![BytesMut::with_capacity(LOG_BUF_CAPACITY); 10];

loop {
let events = buf.read_events(&mut buffers).await.unwrap();
let Events { read, lost: _ } = buf.read_events(&mut buffers).await.unwrap();

#[allow(clippy::needless_range_loop)]
for i in 0..events.read {
let buf = &mut buffers[i];
log_buf(buf, &*log).unwrap();
for buf in buffers.iter().take(read) {
log_buf(buf.as_ref(), &*log).unwrap();
}
}
});
Expand Down
3 changes: 3 additions & 0 deletions aya-obj/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@
//! let bytes = std::fs::read("program.o").unwrap();
//! let mut object = Object::parse(&bytes).unwrap();
//! // Relocate the programs
//! #[cfg(feature = "std")]
//! let text_sections = std::collections::HashSet::new();
//! #[cfg(not(feature = "std"))]
//! let text_sections = hashbrown::HashSet::new();
//! object.relocate_calls(&text_sections).unwrap();
//! object.relocate_maps(std::iter::empty(), &text_sections).unwrap();
//!
Expand Down
21 changes: 8 additions & 13 deletions aya/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "aya"
version = "0.11.0"
description = "An eBPF library with a focus on developer experience and operability."
keywords = ["ebpf", "bpf", "linux", "kernel"]
keywords = ["bpf", "ebpf", "kernel", "linux"]
license = "MIT OR Apache-2.0"
authors = ["The Aya Contributors"]
repository = "https://github.com/aya-rs/aya"
Expand All @@ -19,30 +19,25 @@ lazy_static = "1"
libc = { version = "0.2.105" }
log = "0.4"
object = { version = "0.31", default-features = false, features = [
"std",
"read_core",
"elf",
"read_core",
"std",
] }
parking_lot = { version = "0.12.0", features = ["send_guard"] }
text_io = "0.1.12"
thiserror = "1"
tokio = { version = "1.24.0", features = [
"macros",
"rt",
"rt-multi-thread",
"net",
], optional = true }
tokio = { version = "1.24.0", features = ["rt"], optional = true }

[dev-dependencies]
futures = { version = "0.3.12", default-features = false, features = ["std"] }
matches = "0.1.8"
tempfile = "3"

[features]
default = []
async = []
async_tokio = ["tokio", "async"]
async_std = ["async-io", "async"]
async_tokio = ["tokio/net"]
async_std = ["async-io"]

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs", "-D", "warnings"]
rustdoc-args = ["--cfg", "-D", "docsrs", "warnings"]
8 changes: 4 additions & 4 deletions aya/src/maps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ pub use array::{Array, PerCpuArray, ProgramArray};
pub use bloom_filter::BloomFilter;
pub use hash_map::{HashMap, PerCpuHashMap};
pub use lpm_trie::LpmTrie;
#[cfg(feature = "async")]
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
#[cfg(any(feature = "async_tokio", feature = "async_std"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "async_tokio", feature = "async_std"))))]
pub use perf::AsyncPerfEventArray;
pub use perf::PerfEventArray;
pub use queue::Queue;
Expand Down Expand Up @@ -349,8 +349,8 @@ impl_try_from_map!(
StackTraceMap from Map::StackTraceMap,
);

#[cfg(feature = "async")]
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
#[cfg(any(feature = "async_tokio", feature = "async_std"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "async_tokio", feature = "async_std"))))]
impl_try_from_map!(
AsyncPerfEventArray from Map::PerfEventArray,
);
Expand Down
68 changes: 28 additions & 40 deletions aya/src/maps/perf/async_perf_event_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ use std::{
os::fd::{AsRawFd, RawFd},
};

// See https://doc.rust-lang.org/cargo/reference/features.html#mutually-exclusive-features.
//
// We should eventually split async functionality out into separate crates "aya-async-tokio" and
// "async-async-std". Presently we arbitrarily choose tokio over async-std when both are requested.
#[cfg(all(not(feature = "async_tokio"), feature = "async_std"))]
use async_io::Async;

Expand Down Expand Up @@ -98,16 +102,17 @@ impl<T: BorrowMut<MapData> + Borrow<MapData>> AsyncPerfEventArray<T> {
index: u32,
page_count: Option<usize>,
) -> Result<AsyncPerfEventArrayBuffer<T>, PerfBufferError> {
let buf = self.perf_map.open(index, page_count)?;
let Self { perf_map } = self;
let buf = perf_map.open(index, page_count)?;
let fd = buf.as_raw_fd();
Ok(AsyncPerfEventArrayBuffer {
buf,

#[cfg(feature = "async_tokio")]
async_fd: AsyncFd::new(fd)?,
async_tokio_fd: AsyncFd::new(fd)?,

#[cfg(all(not(feature = "async_tokio"), feature = "async_std"))]
async_fd: Async::new(fd)?,
async_std_fd: Async::new(fd)?,
})
}
}
Expand All @@ -131,13 +136,12 @@ pub struct AsyncPerfEventArrayBuffer<T> {
buf: PerfEventArrayBuffer<T>,

#[cfg(feature = "async_tokio")]
async_fd: AsyncFd<RawFd>,
async_tokio_fd: AsyncFd<RawFd>,

#[cfg(all(not(feature = "async_tokio"), feature = "async_std"))]
async_fd: Async<RawFd>,
async_std_fd: Async<RawFd>,
}

#[cfg(feature = "async_tokio")]
impl<T: BorrowMut<MapData> + Borrow<MapData>> AsyncPerfEventArrayBuffer<T> {
/// Reads events from the buffer.
///
Expand All @@ -152,46 +156,30 @@ impl<T: BorrowMut<MapData> + Borrow<MapData>> AsyncPerfEventArrayBuffer<T> {
&mut self,
buffers: &mut [BytesMut],
) -> Result<Events, PerfBufferError> {
let Self {
buf,
#[cfg(feature = "async_tokio")]
async_tokio_fd,
#[cfg(all(not(feature = "async_tokio"), feature = "async_std"))]
async_std_fd,
} = self;
loop {
let mut guard = self.async_fd.readable_mut().await?;
#[cfg(feature = "async_tokio")]
let mut guard = async_tokio_fd.readable_mut().await?;

match self.buf.read_events(buffers) {
Ok(events) if events.read > 0 || events.lost > 0 => return Ok(events),
Ok(_) => {
guard.clear_ready();
continue;
}
Err(e) => return Err(e),
#[cfg(all(not(feature = "async_tokio"), feature = "async_std"))]
if !buf.readable() {
async_std_fd.readable().await?;
}
}
}
}

#[cfg(all(not(feature = "async_tokio"), feature = "async_std"))]
impl<T: BorrowMut<MapData> + Borrow<MapData>> AsyncPerfEventArrayBuffer<T> {
/// Reads events from the buffer.
///
/// This method reads events into the provided slice of buffers, filling
/// each buffer in order stopping when there are no more events to read or
/// all the buffers have been filled.
///
/// Returns the number of events read and the number of events lost. Events
/// are lost when user space doesn't read events fast enough and the ring
/// buffer fills up.
pub async fn read_events(
&mut self,
buffers: &mut [BytesMut],
) -> Result<Events, PerfBufferError> {
loop {
if !self.buf.readable() {
let _ = self.async_fd.readable().await?;
let events = buf.read_events(buffers)?;
const EMPTY: Events = Events { read: 0, lost: 0 };
if events != EMPTY {
break Ok(events);
}

match self.buf.read_events(buffers) {
Ok(events) if events.read > 0 || events.lost > 0 => return Ok(events),
Ok(_) => continue,
Err(e) => return Err(e),
}
#[cfg(feature = "async_tokio")]
guard.clear_ready();
}
}
}
Loading

0 comments on commit 61608e6

Please sign in to comment.