File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -412,6 +412,7 @@ public:
412412```
413413
414414### get_stats
415+
415416根据指定label获取对应的单维度统计项bvar。
416417
417418get_stats除了支持(默认)std::list< std::string > 参数类型,也支持自定义参数类型,满足以下条件:
@@ -421,6 +422,12 @@ get_stats除了支持(默认)std::list<std::string>参数类型,也支持
421422
422423推荐使用不需要分配内存的容器(例如,std::array、absl::InlinedVector)和不需要拷贝字符串的数据结构(例如,const char* 、std::string_view、butil::StringPieces),可以提高性能。
423424
425+ bvar::MultiDimension的模板参数Shared,默认为false:
426+ 1 . 如果Shared等于false,get_stats返回模板参数T的指针,例如bvar::Adder<int >* 。
427+ 2 . 如果Shared等于true,get_stats返回模板参数T的shared_ptr,例如std::shared_ptr\< bvar::Adder<int >\> 。
428+
429+ ** 注意** :因为shared_ptr的开销,Shared等于true的性能会比Shared等于false的性能差一些。
430+
424431``` c++
425432#include < bvar/bvar.h>
426433#include < bvar/multi_dimension.h>
@@ -523,6 +530,20 @@ int request_count = get_request_count(request_label_list);
523530 * store(bvar)
524531 * return bvar
525532
533+ ### delete_stats
534+
535+ 根据指定label删除对应的单维度统计项bvar。
536+
537+ bvar::MultiDimension的模板参数Shared,默认为false:
538+ 1 . 如果Shared等于false,get_stats返回的是模板参数T的指针,delete_stats无法保证没有使用者,所以无法安全删除bvar。
539+ 2 . 如果Shared等于true,get_stats返回的是模板参数T的shared_ptr,delete_stats可以安全删除bvar。
540+
541+ ### clear_stats
542+
543+ 清理所有单维度统计项bvar。
544+
545+ 安全性同样受bvar::MultiDimension的模板参数Shared的影响,见delete_stats一节说明。
546+
526547** bvar的生命周期**
527548
528549label对应的单维度统计项bvar存储在多维度统计项(mbvar)中,当mbvar析构的时候会释放自身所有bvar,所以用户必须保证在mbvar的生命周期之内操作bvar,在mbvar生命周期外访问bvar的行为未定义,极有可能出core。
Original file line number Diff line number Diff line change @@ -205,12 +205,10 @@ friend class GlobalValue<self_type>;
205205 //
206206 // NOTE: Only available to non-atomic types.
207207 template <typename Op>
208- void merge_global (const Op &op, self_shared_type c = NULL ) {
209- if (NULL == c) {
210- c = combiner.lock ();
211- }
212- if (NULL != c) {
213- GlobalValue<self_type> g (this , c.get ());
208+ void merge_global (const Op &op, self_shared_type& c) {
209+ const self_shared_type& c_ref = NULL != c ? c : combiner.lock ();
210+ if (NULL != c_ref) {
211+ GlobalValue<self_type> g (this , c_ref.get ());
214212 element.merge_global (op, g);
215213 }
216214 }
Original file line number Diff line number Diff line change @@ -42,6 +42,7 @@ namespace bvar {
4242// `delete_stats' and `clear_stats' are not thread safe.
4343// If `Shared' is true, `get_stats` returns a shared_ptr,
4444// `delete_stats' and `clear_stats' are thread safe.
45+ // Note: The shared mode may be less performant than the non-shared mode.
4546template <typename T, typename KeyType = std::list<std::string>, bool Shared = false >
4647class MultiDimension : public MVariable <KeyType> {
4748 typedef std::shared_ptr<T> shared_value_type;
You can’t perform that action at this time.
0 commit comments