This is an automated email from the ASF dual-hosted git repository.

chenBright pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brpc.git


The following commit(s) were added to refs/heads/master by this push:
     new a4896820 refactor(bthread): make TaskControl metric ownership explicit 
(#3490)
a4896820 is described below

commit a48968201189e54845abfcdebe8b624c9d4e5b0b
Author: darion-yaphet <[email protected]>
AuthorDate: Thu Aug 27 17:33:33 2026 +0800

    refactor(bthread): make TaskControl metric ownership explicit (#3490)
    
    Per-tag bvar metrics and cumulative-time callback arguments were created 
with raw pointers, leaving destruction responsibilities unclear. Store them 
with unique_ptr while preserving metric names, access patterns, and scheduler 
behavior.
    
    Constraint: PassiveStatus does not own its callback argument
    
    Rejected: vector<CumulatedWithTagArgs> | reallocation can invalidate 
callback addresses
    
    Confidence: high
    
    Scope-risk: narrow
    
    Reversibility: clean
    
    Directive: Keep callback arguments alive until after their PassiveStatus is 
destroyed
    
    Tested: Rebuilt task_control.cpp CMake object target; git diff --check
    
    Not-tested: Full bthread tests are blocked by the existing EPROGREADTIMEOUT 
build error
---
 src/bthread/task_control.cpp | 20 ++++++++++++++------
 src/bthread/task_control.h   | 13 +++++++++----
 2 files changed, 23 insertions(+), 10 deletions(-)

diff --git a/src/bthread/task_control.cpp b/src/bthread/task_control.cpp
index 5a6bfe83..6da8a4ab 100644
--- a/src/bthread/task_control.cpp
+++ b/src/bthread/task_control.cpp
@@ -258,12 +258,20 @@ int TaskControl::init(int concurrency) {
     for (int i = 0; i < FLAGS_task_group_ntags; ++i) {
         _tagged_ngroup[i].store(0, std::memory_order_relaxed);
         auto tag_str = std::to_string(i);
-        _tagged_nworkers.push_back(new 
bvar::Adder<int64_t>("bthread_worker_count", tag_str));
-        _tagged_cumulated_worker_time.push_back(new 
bvar::PassiveStatus<double>(
-            get_cumulated_worker_time_from_this_with_tag, new 
CumulatedWithTagArgs{this, i}));
-        _tagged_worker_usage_second.push_back(new 
bvar::PerSecond<bvar::PassiveStatus<double>>(
-            "bthread_worker_usage", tag_str, _tagged_cumulated_worker_time[i], 
1));
-        _tagged_nbthreads.push_back(new bvar::Adder<int64_t>("bthread_count", 
tag_str));
+        _tagged_nworkers.emplace_back(
+            std::make_unique<bvar::Adder<int64_t>>("bthread_worker_count", 
tag_str));
+        _tagged_cumulated_worker_time_args.emplace_back(
+            std::make_unique<CumulatedWithTagArgs>(this, i));
+        _tagged_cumulated_worker_time.emplace_back(
+            std::make_unique<bvar::PassiveStatus<double>>(
+                get_cumulated_worker_time_from_this_with_tag,
+                _tagged_cumulated_worker_time_args.back().get()));
+        _tagged_worker_usage_second.emplace_back(
+            std::make_unique<bvar::PerSecond<bvar::PassiveStatus<double>>>(
+                "bthread_worker_usage", tag_str,
+                _tagged_cumulated_worker_time.back().get(), 1));
+        _tagged_nbthreads.emplace_back(
+            std::make_unique<bvar::Adder<int64_t>>("bthread_count", tag_str));
     }
 
     if (init_ed_priority_queues() != 0) {
diff --git a/src/bthread/task_control.h b/src/bthread/task_control.h
index 8cc8c4ba..fed5bcf3 100644
--- a/src/bthread/task_control.h
+++ b/src/bthread/task_control.h
@@ -41,6 +41,7 @@ DECLARE_int32(task_group_ntags);
 namespace bthread {
 
 class TaskGroup;
+struct CumulatedWithTagArgs;
 
 // Control all task groups
 class TaskControl {
@@ -171,10 +172,14 @@ private:
     bvar::PassiveStatus<std::string> _status;
     bvar::Adder<int64_t> _nbthreads;
 
-    std::vector<bvar::Adder<int64_t>*> _tagged_nworkers;
-    std::vector<bvar::PassiveStatus<double>*> _tagged_cumulated_worker_time;
-    std::vector<bvar::PerSecond<bvar::PassiveStatus<double>>*> 
_tagged_worker_usage_second;
-    std::vector<bvar::Adder<int64_t>*> _tagged_nbthreads;
+    std::vector<std::unique_ptr<bvar::Adder<int64_t>>> _tagged_nworkers;
+    std::vector<std::unique_ptr<CumulatedWithTagArgs>>
+        _tagged_cumulated_worker_time_args;
+    std::vector<std::unique_ptr<bvar::PassiveStatus<double>>>
+        _tagged_cumulated_worker_time;
+    std::vector<std::unique_ptr<bvar::PerSecond<bvar::PassiveStatus<double>>>>
+        _tagged_worker_usage_second;
+    std::vector<std::unique_ptr<bvar::Adder<int64_t>>> _tagged_nbthreads;
 
     bool _enable_priority_queue;
     int _ed_priority_queue_num_of_each_tag;


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to