From c2f467c17418c9279341f7a8d80dd407d5e53e92 Mon Sep 17 00:00:00 2001 From: Fredrik Fornwall Date: Thu, 26 Oct 2023 11:58:51 +0200 Subject: [PATCH 1/3] Name spawned threads Name spawned threads to make things more clear during debugging and profiling. --- android-activity/src/game_activity/mod.rs | 34 ++++++++++++-------- android-activity/src/native_activity/glue.rs | 34 ++++++++++++-------- 2 files changed, 42 insertions(+), 26 deletions(-) diff --git a/android-activity/src/game_activity/mod.rs b/android-activity/src/game_activity/mod.rs index f9904d1..3465449 100644 --- a/android-activity/src/game_activity/mod.rs +++ b/android-activity/src/game_activity/mod.rs @@ -925,21 +925,24 @@ pub unsafe extern "C" fn _rust_glue_entry(native_app: *mut ffi::android_app) { File::from_raw_fd(logpipe[0]) }; - thread::spawn(move || { - let tag = CStr::from_bytes_with_nul(b"RustStdoutStderr\0").unwrap(); - let mut reader = BufReader::new(file); - let mut buffer = String::new(); - loop { - buffer.clear(); - if let Ok(len) = reader.read_line(&mut buffer) { - if len == 0 { - break; - } else if let Ok(msg) = CString::new(buffer.clone()) { - android_log(Level::Info, tag, &msg); + thread::Builder::new() + .name("android-stdio".to_string()) + .spawn(move || { + let tag = CStr::from_bytes_with_nul(b"RustStdoutStderr\0").unwrap(); + let mut reader = BufReader::new(file); + let mut buffer = String::new(); + loop { + buffer.clear(); + if let Ok(len) = reader.read_line(&mut buffer) { + if len == 0 { + break; + } else if let Ok(msg) = CString::new(buffer.clone()) { + android_log(Level::Info, tag, &msg); + } } } - } - }); + }) + .unwrap(); let jvm = unsafe { let jvm = (*(*native_app).activity).vm; @@ -955,6 +958,11 @@ pub unsafe extern "C" fn _rust_glue_entry(native_app: *mut ffi::android_app) { }; unsafe { + // Name thread - this needs to happen here after attaching to a JVM thread, + // since that changes the thread name to something like "Thread-2". + let thread_name = CStr::from_bytes_with_nul(b"android-main\0").unwrap(); + libc::pthread_setname_np(libc::pthread_self(), thread_name.as_ptr()); + let app = AndroidApp::from_ptr(NonNull::new(native_app).unwrap(), jvm.clone()); // We want to specifically catch any panic from the application's android_main diff --git a/android-activity/src/native_activity/glue.rs b/android-activity/src/native_activity/glue.rs index 003eaa5..eaa2b17 100644 --- a/android-activity/src/native_activity/glue.rs +++ b/android-activity/src/native_activity/glue.rs @@ -845,21 +845,24 @@ extern "C" fn ANativeActivity_onCreate( File::from_raw_fd(logpipe[0]) }; - std::thread::spawn(move || { - let tag = CStr::from_bytes_with_nul(b"RustStdoutStderr\0").unwrap(); - let mut reader = BufReader::new(file); - let mut buffer = String::new(); - loop { - buffer.clear(); - if let Ok(len) = reader.read_line(&mut buffer) { - if len == 0 { - break; - } else if let Ok(msg) = CString::new(buffer.clone()) { - android_log(Level::Info, tag, &msg); + std::thread::Builder::new() + .name("android-stdio".to_string()) + .spawn(move || { + let tag = CStr::from_bytes_with_nul(b"RustStdoutStderr\0").unwrap(); + let mut reader = BufReader::new(file); + let mut buffer = String::new(); + loop { + buffer.clear(); + if let Ok(len) = reader.read_line(&mut buffer) { + if len == 0 { + break; + } else if let Ok(msg) = CString::new(buffer.clone()) { + android_log(Level::Info, tag, &msg); + } } } - } - }); + }) + .unwrap(); log::trace!( "Creating: {:p}, saved_state = {:p}, save_state_size = {}", @@ -899,6 +902,11 @@ extern "C" fn ANativeActivity_onCreate( rust_glue.notify_main_thread_running(); unsafe { + // Name thread - this needs to happen here after attaching to a JVM thread, + // since that changes the thread name to something like "Thread-2". + let thread_name = CStr::from_bytes_with_nul(b"android-main\0").unwrap(); + libc::pthread_setname_np(libc::pthread_self(), thread_name.as_ptr()); + // We want to specifically catch any panic from the application's android_main // so we can finish + destroy the Activity gracefully via the JVM catch_unwind(|| { From d9af67008a03118027c72a1285cfb362b6fe510d Mon Sep 17 00:00:00 2001 From: Fredrik Fornwall Date: Mon, 20 Nov 2023 12:48:37 +0100 Subject: [PATCH 2/3] Rename threads --- android-activity/src/game_activity/mod.rs | 4 ++-- android-activity/src/native_activity/glue.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/android-activity/src/game_activity/mod.rs b/android-activity/src/game_activity/mod.rs index 3465449..56df05b 100644 --- a/android-activity/src/game_activity/mod.rs +++ b/android-activity/src/game_activity/mod.rs @@ -926,7 +926,7 @@ pub unsafe extern "C" fn _rust_glue_entry(native_app: *mut ffi::android_app) { }; thread::Builder::new() - .name("android-stdio".to_string()) + .name("stdio-to-logcat".to_string()) .spawn(move || { let tag = CStr::from_bytes_with_nul(b"RustStdoutStderr\0").unwrap(); let mut reader = BufReader::new(file); @@ -960,7 +960,7 @@ pub unsafe extern "C" fn _rust_glue_entry(native_app: *mut ffi::android_app) { unsafe { // Name thread - this needs to happen here after attaching to a JVM thread, // since that changes the thread name to something like "Thread-2". - let thread_name = CStr::from_bytes_with_nul(b"android-main\0").unwrap(); + let thread_name = CStr::from_bytes_with_nul(b"android_main\0").unwrap(); libc::pthread_setname_np(libc::pthread_self(), thread_name.as_ptr()); let app = AndroidApp::from_ptr(NonNull::new(native_app).unwrap(), jvm.clone()); diff --git a/android-activity/src/native_activity/glue.rs b/android-activity/src/native_activity/glue.rs index eaa2b17..8680982 100644 --- a/android-activity/src/native_activity/glue.rs +++ b/android-activity/src/native_activity/glue.rs @@ -846,7 +846,7 @@ extern "C" fn ANativeActivity_onCreate( }; std::thread::Builder::new() - .name("android-stdio".to_string()) + .name("stdio-to-logcat".to_string()) .spawn(move || { let tag = CStr::from_bytes_with_nul(b"RustStdoutStderr\0").unwrap(); let mut reader = BufReader::new(file); @@ -904,7 +904,7 @@ extern "C" fn ANativeActivity_onCreate( unsafe { // Name thread - this needs to happen here after attaching to a JVM thread, // since that changes the thread name to something like "Thread-2". - let thread_name = CStr::from_bytes_with_nul(b"android-main\0").unwrap(); + let thread_name = CStr::from_bytes_with_nul(b"android_main\0").unwrap(); libc::pthread_setname_np(libc::pthread_self(), thread_name.as_ptr()); // We want to specifically catch any panic from the application's android_main From eeeb80209fb5daa2848b76c1fe1e1da08fe8f65e Mon Sep 17 00:00:00 2001 From: Fredrik Fornwall Date: Mon, 20 Nov 2023 15:36:04 +0100 Subject: [PATCH 3/3] Fix error after merge conflict --- android-activity/src/game_activity/mod.rs | 2 +- android-activity/src/native_activity/glue.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/android-activity/src/game_activity/mod.rs b/android-activity/src/game_activity/mod.rs index c4e5561..5fc0a99 100644 --- a/android-activity/src/game_activity/mod.rs +++ b/android-activity/src/game_activity/mod.rs @@ -927,7 +927,7 @@ pub unsafe extern "C" fn _rust_glue_entry(native_app: *mut ffi::android_app) { unsafe { // Name thread - this needs to happen here after attaching to a JVM thread, // since that changes the thread name to something like "Thread-2". - let thread_name = CStr::from_bytes_with_nul(b"android_main\0").unwrap(); + let thread_name = std::ffi::CStr::from_bytes_with_nul(b"android_main\0").unwrap(); libc::pthread_setname_np(libc::pthread_self(), thread_name.as_ptr()); let app = AndroidApp::from_ptr(NonNull::new(native_app).unwrap(), jvm.clone()); diff --git a/android-activity/src/native_activity/glue.rs b/android-activity/src/native_activity/glue.rs index 8fdb280..2838d6a 100644 --- a/android-activity/src/native_activity/glue.rs +++ b/android-activity/src/native_activity/glue.rs @@ -870,7 +870,7 @@ extern "C" fn ANativeActivity_onCreate( unsafe { // Name thread - this needs to happen here after attaching to a JVM thread, // since that changes the thread name to something like "Thread-2". - let thread_name = CStr::from_bytes_with_nul(b"android_main\0").unwrap(); + let thread_name = std::ffi::CStr::from_bytes_with_nul(b"android_main\0").unwrap(); libc::pthread_setname_np(libc::pthread_self(), thread_name.as_ptr()); // We want to specifically catch any panic from the application's android_main