@@ -19,13 +19,16 @@ void PikaCmdTableManager::ResetCommandCount() {
1919 std::unique_lock<std::shared_mutex> write_lock (slow_command_mutex_);
2020 slow_command_count_.clear ();
2121 }
22- std::atomic_store (&data_, std::make_shared<HistogramData>());
22+ {
23+ std::lock_guard<std::mutex> lock (data_mutex_);
24+ data_ = std::make_shared<HistogramData>();
25+ }
2326}
2427
2528PikaCmdTableManager::PikaCmdTableManager () {
2629 cmds_ = std::make_unique<CmdTable>();
2730 cmds_->reserve (300 );
28- std::atomic_store (& data_, std::make_shared<HistogramData>());
31+ data_ = std::make_shared<HistogramData>();
2932}
3033
3134void PikaCmdTableManager::InitCmdTable (void ) {
@@ -73,25 +76,36 @@ void PikaCmdTableManager::RenameCommand(const std::string before, const std::str
7376}
7477
7578prometheus::Histogram& PikaCmdTableManager::GetHistogram (const std::string& opt) {
76- auto current_data = std::atomic_load (&data_);
79+ std::shared_ptr<HistogramData> data_copy;
80+ {
81+ std::lock_guard<std::mutex> lock (data_mutex_);
82+ data_copy = data_;
83+ }
84+
7785 {
78- auto it = current_data->histograms .find (opt);
79- if (it != current_data->histograms .end ()) {
86+ std::shared_lock<std::shared_mutex> read_lock (histograms_mutex_);
87+ auto it = data_copy->histograms .find (opt);
88+ if (it != data_copy->histograms .end ()) {
8089 return *(it->second );
8190 }
8291 }
8392
84- std::lock_guard <std::mutex> lock (data_mutex_ );
85- auto & new_histogram = current_data ->family ->Add (
93+ std::unique_lock <std::shared_mutex> write_lock (histograms_mutex_ );
94+ auto & new_histogram = data_copy ->family ->Add (
8695 {{" command" , opt}},
8796 prometheus::Histogram::BucketBoundaries{0.5 , 1 , 2 , 3 , 5 , 7 , 10 , 15 , 20 , 30 , 40 , 50 , 65 , 75 , 85 , 100 , 125 , 140 , 150 , 160 , 175 , 185 , 200 , 300 , 400 , 500 , 750 , 1000 , 2000 , 5000 , 10000 }
8897 );
89- current_data ->histograms [opt] = &new_histogram;
98+ data_copy ->histograms [opt] = &new_histogram;
9099 return new_histogram;
91100}
92101
93102std::shared_ptr<HistogramData> PikaCmdTableManager::GetHistogramsData () {
94- return std::atomic_load (&data_);
103+ std::shared_ptr<HistogramData> data_copy;
104+ {
105+ std::lock_guard<std::mutex> lock (data_mutex_);
106+ data_copy = data_;
107+ }
108+ return data_copy;
95109}
96110
97111void PikaCmdTableManager::UpdateSlowCommandCount (const std::string& opt) {
0 commit comments