Skip to content

Commit 629ecc0

Browse files
author
wuxianrong
committed
The Copy on Write mode is used
1 parent ab079be commit 629ecc0

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

include/pika_cmd_table_manager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ class PikaCmdTableManager {
5454
virtual ~PikaCmdTableManager() = default;
5555
void InitCmdTable(void);
5656
void RenameCommand(const std::string before, const std::string after);
57-
void InitHistograms();
5857
std::shared_ptr<Cmd> GetCmd(const std::string& opt);
5958
bool CmdExist(const std::string& cmd) const;
6059
CmdTable* GetCmdTable();
@@ -91,6 +90,7 @@ class PikaCmdTableManager {
9190
std::unordered_map<std::string, CommandStatistics> cmdstat_map_;
9291
std::unordered_map<std::string, CommandStatistics> slow_command_count_;
9392
std::shared_mutex slow_command_mutex_;
93+
std::shared_mutex histograms_mutex_;
9494
std::mutex data_mutex_;
9595
std::shared_ptr<HistogramData> data_;
9696
};

src/pika_cmd_table_manager.cc

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ 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

2528
PikaCmdTableManager::PikaCmdTableManager() {
@@ -73,25 +76,36 @@ void PikaCmdTableManager::RenameCommand(const std::string before, const std::str
7376
}
7477

7578
prometheus::Histogram& PikaCmdTableManager::GetHistogram(const std::string& opt) {
76-
auto current_data = std::atomic_load(&data_);
79+
std::shared_ptr<HistogramData> data_copy;
7780
{
78-
auto it = current_data->histograms.find(opt);
79-
if (it != current_data->histograms.end()) {
81+
std::lock_guard<std::mutex> lock(data_mutex_);
82+
data_copy = data_;
83+
}
84+
85+
{
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

93102
std::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

97111
void PikaCmdTableManager::UpdateSlowCommandCount(const std::string& opt) {

0 commit comments

Comments
 (0)