Skip to content

Commit

Permalink
fix nts thread names
Browse files Browse the repository at this point in the history
  • Loading branch information
realFlowControl committed Nov 8, 2024
1 parent 2c8d507 commit 05d4949
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
43 changes: 23 additions & 20 deletions profiling/src/profiling/thread_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,25 +73,28 @@ pub fn join_timeout(handle: JoinHandle<()>, timeout: Duration, impact: &str) {
}

pub fn get_current_thread_name() -> String {
let mut name = [0u8; 32];

let result = unsafe {
libc::pthread_getname_np(
libc::pthread_self(),
name.as_mut_ptr() as *mut c_char,
name.len(),
)
};

let mut thread_name = SAPI.to_string();

if result == 0 {
// If successful, convert the result to a Rust String
let cstr = unsafe { std::ffi::CStr::from_ptr(name.as_ptr() as *const c_char) };
let str_slice: &str = cstr.to_str().unwrap();
if !str_slice.is_empty() {
thread_name.push_str(": ");
thread_name.push_str(str_slice);
#[cfg(php_zts)]
{
let mut name = [0u8; 32];

let result = unsafe {
libc::pthread_getname_np(
libc::pthread_self(),
name.as_mut_ptr() as *mut c_char,
name.len(),
)
};

if result == 0 {
// If successful, convert the result to a Rust String
let cstr = unsafe { std::ffi::CStr::from_ptr(name.as_ptr() as *const c_char) };
let str_slice: &str = cstr.to_str().unwrap();
if !str_slice.is_empty() {
thread_name.push_str(": ");
thread_name.push_str(str_slice);
}
}
}

Expand All @@ -110,7 +113,7 @@ mod tests {
libc::pthread_setname_np(
#[cfg(target_os = "linux")]
libc::pthread_self(),
b"\0".as_ptr() as *const c_char
b"\0".as_ptr() as *const c_char,
);
}
assert_eq!(get_current_thread_name(), "unknown");
Expand All @@ -119,7 +122,7 @@ mod tests {
libc::pthread_setname_np(
#[cfg(target_os = "linux")]
libc::pthread_self(),
b"foo\0".as_ptr() as *const c_char
b"foo\0".as_ptr() as *const c_char,
);
}
assert_eq!(get_current_thread_name(), "unknown: foo");
Expand All @@ -128,7 +131,7 @@ mod tests {
libc::pthread_setname_np(
#[cfg(target_os = "linux")]
libc::pthread_self(),
b"bar\0".as_ptr() as *const c_char
b"bar\0".as_ptr() as *const c_char,
);
}
assert_eq!(get_current_thread_name(), "unknown: bar");
Expand Down
4 changes: 3 additions & 1 deletion profiling/tests/correctness/exceptions.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
},
{
"key": "thread name",
"values_regex": "^cli.*$"
"values": [
"cli"
]
}
]
}
Expand Down

0 comments on commit 05d4949

Please sign in to comment.