Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Fix and cover tests for target x86_64-unknown-linux-musl #140

Merged
merged 6 commits into from Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 40 additions & 11 deletions .github/workflows/rust.yml
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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 }}
4 changes: 2 additions & 2 deletions examples/malloc_hook.rs
Expand Up @@ -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<extern "C" fn(size: usize) -> *mut c_void> = None;

extern "C" {
#[cfg(target_os = "linux")]
#[cfg(all(target_os = "linux", target_env = "gnu"))]
static mut __malloc_hook: Option<extern "C" fn(size: usize) -> *mut c_void>;

fn malloc(size: usize) -> *mut c_void;
Expand Down
7 changes: 6 additions & 1 deletion src/collector.rs
Expand Up @@ -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<extern "C" fn(size: usize) -> *mut c_void> = None;

extern "C" {
#[cfg(target_os = "linux")]
#[cfg(target_env = "gnu")]
static mut __malloc_hook: Option<extern "C" fn(size: usize) -> *mut c_void>;

fn malloc(size: usize) -> *mut c_void;
Expand Down
11 changes: 8 additions & 3 deletions src/profiler.rs
Expand Up @@ -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"))]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

musl added pthread_getname_np after 1.2.3, and it's also included in the rust libc 🤔

I don't know whether upgrading musl would be a heavy burden for users, but I recommanded to upgrade it.

Copy link
Member Author

@Xuanwo Xuanwo Jun 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part is a bit tricky. Even on musl 1.2.3, it still doesn't work correctly.

cargo build works, but cargo test will have the following error:

  = note: /usr/bin/ld: /home/xuanwo/Code/tikv/pprof-rs/target/x86_64-unknown-linux-musl/debug/deps/pprof-f3595f784aae57b5.1iodjb8b0079y0t7.rcgu.o: in function `pprof::profiler::write_thread_name':
          /home/xuanwo/Code/tikv/pprof-rs/src/profiler.rs:194: undefined reference to `pthread_getname_np'
          collect2: error: ld returned 1 exit status

  = help: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  = note: use the `-l` flag to specify native libraries to link
  = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)

Any ideas about this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@YangKeao Hi, can you take a look at this PR again? I'm willing to change the code if there are other ways to address this issue.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's pretty wired. I run nm libc.a |grep pthread|grep getname in the alpine, and got:

0000000000000000 T pthread_getname_np

But I came across the same issue you faced. I will approve and merge this PR to make it work under musl (and I'll investigate further why it cannot link with this symbol)

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) };
Expand Down Expand Up @@ -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<extern "C" fn(size: usize) -> *mut c_void> = None;

extern "C" {
#[cfg(target_env = "gnu")]
static mut __malloc_hook: Option<extern "C" fn(size: usize) -> *mut c_void>;

fn malloc(size: usize) -> *mut c_void;
Expand Down