From 1b194789a60fb1873fff63fbf5f0c1afcd870c2d Mon Sep 17 00:00:00 2001 From: Xuanwo Date: Sat, 25 Jun 2022 00:07:23 +0800 Subject: [PATCH] feat: Fix and cover tests for target x86_64-unknown-linux-musl (#140) * Fix tests and examples under musl Signed-off-by: Xuanwo * Cover build and test under musl Signed-off-by: Xuanwo * Fix target not installed Signed-off-by: Xuanwo * Install musl-tools Signed-off-by: Xuanwo * Don't run test for aarch Signed-off-by: Xuanwo * Cancel previous jobs Signed-off-by: Xuanwo --- .github/workflows/rust.yml | 51 ++++++++++++++++++++++++++++++-------- examples/malloc_hook.rs | 4 +-- src/collector.rs | 7 +++++- src/profiler.rs | 11 +++++--- 4 files changed, 56 insertions(+), 17 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index b79e23fb..10a9a0eb 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -2,6 +2,10 @@ on: [push, pull_request] name: build +concurrency: + group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event_name }} + cancel-in-progress: true + jobs: lint: name: Format and Clippy @@ -43,16 +47,24 @@ jobs: matrix: os: [ubuntu-latest, macos-latest] toolchain: [stable, nightly, 1.56.1] - target: [x86_64-unknown-linux-gnu, aarch64-unknown-linux-gnu, x86_64-apple-darwin, aarch64-apple-darwin] + target: [ + x86_64-unknown-linux-gnu, + aarch64-unknown-linux-gnu, + x86_64-unknown-linux-musl, + x86_64-apple-darwin, + aarch64-apple-darwin, + ] exclude: - - os: ubuntu-latest - target: x86_64-apple-darwin - - os: ubuntu-latest - target: aarch64-apple-darwin - - os: macos-latest - target: x86_64-unknown-linux-gnu - - os: macos-latest - target: aarch64-unknown-linux-gnu + - os: ubuntu-latest + target: x86_64-apple-darwin + - os: ubuntu-latest + target: aarch64-apple-darwin + - os: macos-latest + target: x86_64-unknown-linux-gnu + - os: macos-latest + target: aarch64-unknown-linux-gnu + - os: macos-latest + target: x86_64-unknown-linux-musl runs-on: ${{ matrix.os }} steps: @@ -92,6 +104,18 @@ jobs: matrix: os: [ubuntu-latest, macos-latest] toolchain: [stable, nightly] + target: [ + x86_64-unknown-linux-gnu, + x86_64-unknown-linux-musl, + x86_64-apple-darwin, + ] + exclude: + - os: ubuntu-latest + target: x86_64-apple-darwin + - os: macos-latest + target: x86_64-unknown-linux-gnu + - os: macos-latest + target: x86_64-unknown-linux-musl runs-on: ${{ matrix.os }} steps: @@ -103,16 +127,21 @@ jobs: with: profile: minimal toolchain: ${{ matrix.toolchain }} + target: ${{ matrix.target }} override: true + - if: ${{ matrix.target == 'x86_64-unknown-linux-musl' }} + name: Install musl + run: sudo apt install musl-tools + - name: Run cargo test prost uses: actions-rs/cargo@v1.0.3 with: command: test - args: --features flamegraph,prost-codec + args: --features flamegraph,prost-codec --target ${{ matrix.target }} - name: Run cargo test protobuf uses: actions-rs/cargo@v1.0.3 with: command: test - args: --features flamegraph,protobuf-codec + args: --features flamegraph,protobuf-codec --target ${{ matrix.target }} diff --git a/examples/malloc_hook.rs b/examples/malloc_hook.rs index 2957c36d..fb19ad2b 100644 --- a/examples/malloc_hook.rs +++ b/examples/malloc_hook.rs @@ -5,13 +5,13 @@ extern crate libc; use pprof; use std::ffi::c_void; -#[cfg(not(target_os = "linux"))] +#[cfg(not(all(target_os = "linux", target_env = "gnu")))] #[allow(clippy::wrong_self_convention)] #[allow(non_upper_case_globals)] static mut __malloc_hook: Option *mut c_void> = None; extern "C" { - #[cfg(target_os = "linux")] + #[cfg(all(target_os = "linux", target_env = "gnu"))] static mut __malloc_hook: Option *mut c_void>; fn malloc(size: usize) -> *mut c_void; diff --git a/src/collector.rs b/src/collector.rs index 0e63b695..d53fef4a 100644 --- a/src/collector.rs +++ b/src/collector.rs @@ -369,8 +369,13 @@ mod malloc_free_test { use std::collections::BTreeMap; use std::ffi::c_void; + #[cfg(not(target_env = "gnu"))] + #[allow(clippy::wrong_self_convention)] + #[allow(non_upper_case_globals)] + static mut __malloc_hook: Option *mut c_void> = None; + extern "C" { - #[cfg(target_os = "linux")] + #[cfg(target_env = "gnu")] static mut __malloc_hook: Option *mut c_void>; fn malloc(size: usize) -> *mut c_void; diff --git a/src/profiler.rs b/src/profiler.rs index 25c7b123..4bc72ad2 100644 --- a/src/profiler.rs +++ b/src/profiler.rs @@ -183,12 +183,12 @@ fn write_thread_name_fallback(current_thread: libc::pthread_t, name: &mut [libc: } } -#[cfg(not(any(target_os = "linux", target_os = "macos")))] +#[cfg(not(all(any(target_os = "linux", target_os = "macos"), target_env = "gnu")))] fn write_thread_name(current_thread: libc::pthread_t, name: &mut [libc::c_char]) { write_thread_name_fallback(current_thread, name); } -#[cfg(any(target_os = "linux", target_os = "macos"))] +#[cfg(all(any(target_os = "linux", target_os = "macos"), target_env = "gnu"))] fn write_thread_name(current_thread: libc::pthread_t, name: &mut [libc::c_char]) { let name_ptr = name as *mut [libc::c_char] as *mut libc::c_char; let ret = unsafe { libc::pthread_getname_np(current_thread, name_ptr, MAX_THREAD_NAME) }; @@ -411,10 +411,15 @@ mod tests { use std::cell::RefCell; use std::ffi::c_void; - use std::ptr::null_mut; + #[cfg(not(target_env = "gnu"))] + #[allow(clippy::wrong_self_convention)] + #[allow(non_upper_case_globals)] + static mut __malloc_hook: Option *mut c_void> = None; + extern "C" { + #[cfg(target_env = "gnu")] static mut __malloc_hook: Option *mut c_void>; fn malloc(size: usize) -> *mut c_void;