From 21e50133cbcd1303e3c3eeeed1055406ec2f7a0e Mon Sep 17 00:00:00 2001 From: wangchenguang Date: Wed, 26 Aug 2026 10:46:08 +0800 Subject: [PATCH] refactor(bthread): make TaskControl metric ownership explicit 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 | 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 5a6bfe831c..6da8a4ab8d 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("bthread_worker_count", tag_str)); - _tagged_cumulated_worker_time.push_back(new bvar::PassiveStatus( - get_cumulated_worker_time_from_this_with_tag, new CumulatedWithTagArgs{this, i})); - _tagged_worker_usage_second.push_back(new bvar::PerSecond>( - "bthread_worker_usage", tag_str, _tagged_cumulated_worker_time[i], 1)); - _tagged_nbthreads.push_back(new bvar::Adder("bthread_count", tag_str)); + _tagged_nworkers.emplace_back( + std::make_unique>("bthread_worker_count", tag_str)); + _tagged_cumulated_worker_time_args.emplace_back( + std::make_unique(this, i)); + _tagged_cumulated_worker_time.emplace_back( + std::make_unique>( + get_cumulated_worker_time_from_this_with_tag, + _tagged_cumulated_worker_time_args.back().get())); + _tagged_worker_usage_second.emplace_back( + std::make_unique>>( + "bthread_worker_usage", tag_str, + _tagged_cumulated_worker_time.back().get(), 1)); + _tagged_nbthreads.emplace_back( + std::make_unique>("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 8cc8c4ba4e..fed5bcf3b6 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 @@ friend bthread_t init_for_pthread_stack_trace(); bvar::PassiveStatus _status; bvar::Adder _nbthreads; - std::vector*> _tagged_nworkers; - std::vector*> _tagged_cumulated_worker_time; - std::vector>*> _tagged_worker_usage_second; - std::vector*> _tagged_nbthreads; + std::vector>> _tagged_nworkers; + std::vector> + _tagged_cumulated_worker_time_args; + std::vector>> + _tagged_cumulated_worker_time; + std::vector>>> + _tagged_worker_usage_second; + std::vector>> _tagged_nbthreads; bool _enable_priority_queue; int _ed_priority_queue_num_of_each_tag;