Repository: tikv/tikv (language: rust)

Issue:
Incorrect mapped allocation per thread metric
## Bug Report

![Image](https://github.com/user-attachments/assets/0d5c1be1-96ca-401a-a4a3-5d291cfdba28)

From allocation bytes rate, we can see memory is allocated on `sst-importer` and `apply` threads. While mapped allocation per thread metric shows high memory usage in `sched-worker-high` and `unified-read-pool` which isn't used at that time


Discussion / hints:

Fix patch:
diff --git a/components/batch-system/src/batch.rs b/components/batch-system/src/batch.rs
index 8cc873bd4b6..0b7f9f45489 100644
--- a/components/batch-system/src/batch.rs
+++ b/components/batch-system/src/batch.rs
@@ -583,7 +583,6 @@ where
         let t = thread::Builder::new()
             .name(name)
             .spawn_wrapper(move || {
-                tikv_alloc::thread_allocate_exclusive_arena().unwrap();
                 tikv_util::thread_group::set_properties(props);
                 set_io_type(IoType::ForegroundWrite);
                 poller.poll();
diff --git a/components/server/src/server.rs b/components/server/src/server.rs
index b8ba53641ab..5cdc21609a4 100644
--- a/components/server/src/server.rs
+++ b/components/server/src/server.rs
@@ -122,7 +122,9 @@ use tikv::{
         Engine, Storage,
     },
 };
-use tikv_alloc::{add_thread_memory_accessor, remove_thread_memory_accessor};
+use tikv_alloc::{
+    add_thread_memory_accessor, remove_thread_memory_accessor, thread_allocate_exclusive_arena,
+};
 use tikv_util::{
     check_environment_variables,
     config::VersionTrack,
@@ -330,7 +332,7 @@ where
 
                     // SAFETY: we will call `remove_thread_memory_accessor` at before_stop.
                     unsafe { add_thread_memory_accessor() };
-                    tikv_alloc::thread_allocate_exclusive_arena().unwrap();
+                    thread_allocate_exclusive_arena().unwrap();
                 })
                 .before_stop(|| {
                     remove_thread_memory_accessor();
diff --git a/components/server/src/server2.rs b/components/server/src/server2.rs
index 5381a365de8..5b85a1c6842 100644
--- a/components/server/src/server2.rs
+++ b/components/server/src/server2.rs
@@ -109,7 +109,9 @@ use tikv::{
         Engine, Storage,
     },
 };
-use tikv_alloc::{add_thread_memory_accessor, remove_thread_memory_accessor};
+use tikv_alloc::{
+    add_thread_memory_accessor, remove_thread_memory_accessor, thread_allocate_exclusive_arena,
+};
 use tikv_util::{
     check_environment_variables,
     config::VersionTrack,
@@ -301,6 +303,7 @@ where
 
                     // SAFETY: we will call `remove_thread_memory_accessor` at before_stop.
                     unsafe { add_thread_memory_accessor() };
+                    thread_allocate_exclusive_arena().unwrap();
                 })
                 .before_stop(|| {
                     remove_thread_memory_accessor();
diff --git a/components/tikv_util/src/sys/thread.rs b/components/tikv_util/src/sys/thread.rs
index 818d8795b31..9d119cc09c8 100644
--- a/components/tikv_util/src/sys/thread.rs
+++ b/components/tikv_util/src/sys/thread.rs
@@ -7,7 +7,9 @@
 use std::{io, io::Result, sync::Mutex, thread};
 
 use collections::HashMap;
-use tikv_alloc::{add_thread_memory_accessor, remove_thread_memory_accessor};
+use tikv_alloc::{
+    add_thread_memory_accessor, remove_thread_memory_accessor, thread_allocate_exclusive_arena,
+};
 
 /// A cross-platform CPU statistics data structure.
 #[derive(Debug, Copy, Clone, Default, PartialEq)]
@@ -430,6 +432,7 @@ impl StdThreadBuildWrapper for std::thread::Builder {
             call_thread_start_hooks();
             // SAFETY: we will call `remove_thread_memory_accessor` at defer.
             unsafe { add_thread_memory_accessor() };
+            thread_allocate_exclusive_arena().unwrap();
             add_thread_name_to_map();
             defer! {{
                 remove_thread_name_from_map();
@@ -452,9 +455,8 @@ impl ThreadBuildWrapper for tokio::runtime::Builder {
             // SAFETY: we will call `remove_thread_memory_accessor` at
             // `before-stop_wrapper`.
             // FIXME: What if the user only calls `after_start_wrapper`?
-            unsafe {
-                add_thread_memory_accessor();
-            }
+            unsafe { add_thread_memory_accessor() };
+            thread_allocate_exclusive_arena().unwrap();
             add_thread_name_to_map();
             start();
         })
@@ -478,9 +480,8 @@ impl ThreadBuildWrapper for futures::executor::ThreadPoolBuilder {
             // SAFETY: we will call `remove_thread_memory_accessor` at
             // `before-stop_wrapper`.
             // FIXME: What if the user only calls `after_start_wrapper`?
-            unsafe {
-                add_thread_memory_accessor();
-            }
+            unsafe { add_thread_memory_accessor() };
+            thread_allocate_exclusive_arena().unwrap();
             add_thread_name_to_map();
             start();
         })


Test patch: