|
15 | 15 | extern std::unique_ptr<PikaConf> g_pika_conf; |
16 | 16 |
|
17 | 17 | void PikaCmdTableManager::ResetCommandCount() { |
18 | | - std::lock_guard<std::mutex> lock(command_mutex_); |
19 | | - slow_command_count_.clear(); |
20 | | - InitHistograms(); |
21 | | -} |
22 | | - |
23 | | -void PikaCmdTableManager::InitHistograms() { |
24 | | - histograms_.clear(); |
25 | | - prometheus_registry_ = std::make_shared<prometheus::Registry>(); |
26 | | - histogram_family_ = &prometheus::BuildHistogram() |
27 | | - .Name("pika_command_duration_seconds") |
28 | | - .Help("Execution time of Pika commands in seconds") |
29 | | - .Register(*prometheus_registry_); |
| 18 | + { |
| 19 | + std::unique_lock<std::shared_mutex> write_lock(slow_command_mutex_); |
| 20 | + slow_command_count_.clear(); |
| 21 | + } |
| 22 | + std::atomic_store(&data_, std::make_shared<HistogramData>()); |
30 | 23 | } |
31 | 24 |
|
32 | 25 | PikaCmdTableManager::PikaCmdTableManager() { |
33 | 26 | cmds_ = std::make_unique<CmdTable>(); |
34 | 27 | cmds_->reserve(300); |
35 | | - InitHistograms(); |
| 28 | + std::atomic_store(&data_, std::make_shared<HistogramData>()); |
36 | 29 | } |
37 | 30 |
|
38 | 31 | void PikaCmdTableManager::InitCmdTable(void) { |
@@ -80,24 +73,25 @@ void PikaCmdTableManager::RenameCommand(const std::string before, const std::str |
80 | 73 | } |
81 | 74 |
|
82 | 75 | prometheus::Histogram& PikaCmdTableManager::GetHistogram(const std::string& opt) { |
| 76 | + auto current_data = std::atomic_load(&data_); |
83 | 77 | { |
84 | | - std::shared_lock<std::shared_mutex> read_lock(histograms_mutex_); |
85 | | - auto it = histograms_.find(opt); |
86 | | - if (it != histograms_.end()) { |
| 78 | + auto it = current_data->histograms.find(opt); |
| 79 | + if (it != current_data->histograms.end()) { |
87 | 80 | return *(it->second); |
88 | 81 | } |
89 | 82 | } |
90 | | - std::unique_lock<std::shared_mutex> write_lock(histograms_mutex_); |
91 | | - auto& new_histogram = histogram_family_->Add( |
92 | | - {{"command", opt}}, |
93 | | - 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} |
| 83 | + |
| 84 | + std::lock_guard<std::mutex> lock(data_mutex_); |
| 85 | + auto& new_histogram = current_data->family->Add( |
| 86 | + {{"command", opt}}, |
| 87 | + 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} |
94 | 88 | ); |
95 | | - histograms_[opt] = &new_histogram; |
| 89 | + current_data->histograms[opt] = &new_histogram; |
96 | 90 | return new_histogram; |
97 | 91 | } |
98 | 92 |
|
99 | | -prometheus::Family<prometheus::Histogram>* PikaCmdTableManager::GetHistograms() { |
100 | | - return histogram_family_; |
| 93 | +std::shared_ptr<HistogramData> PikaCmdTableManager::GetHistogramsData() { |
| 94 | + return std::atomic_load(&data_); |
101 | 95 | } |
102 | 96 |
|
103 | 97 | void PikaCmdTableManager::UpdateSlowCommandCount(const std::string& opt) { |
|
0 commit comments