Skip to content

Commit d8f19e9

Browse files
author
wuxianrong
committed
add tpp99
1 parent abcbd9b commit d8f19e9

3 files changed

Lines changed: 39 additions & 31 deletions

File tree

include/pika_cmd_table_manager.h

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,23 @@ struct CommandStatistics {
2929
std::atomic<uint64_t> cmd_time_consuming = 0;
3030
};
3131

32+
struct HistogramData {
33+
std::shared_ptr<prometheus::Registry> registry;
34+
prometheus::Family<prometheus::Histogram>* family;
35+
std::unordered_map<std::string, prometheus::Histogram*> histograms;
36+
37+
HistogramData() {
38+
registry = std::make_shared<prometheus::Registry>();
39+
family = &prometheus::BuildHistogram()
40+
.Name("pika_command_duration_seconds")
41+
.Help("Execution time of Pika commands in seconds")
42+
.Register(*registry);
43+
}
44+
45+
HistogramData(const HistogramData&) = delete;
46+
HistogramData& operator=(const HistogramData&) = delete;
47+
};
48+
3249
class PikaCmdTableManager {
3350
friend AclSelector;
3451

@@ -53,7 +70,7 @@ class PikaCmdTableManager {
5370
void UpdateSlowCommandCount(const std::string& opt);
5471
void ResetCommandCount();
5572
prometheus::Histogram& GetHistogram(const std::string& opt);
56-
prometheus::Family<prometheus::Histogram>* GetHistograms();
73+
std::shared_ptr<HistogramData> GetHistogramsData();
5774

5875
private:
5976
std::shared_ptr<Cmd> NewCommand(const std::string& opt);
@@ -73,11 +90,8 @@ class PikaCmdTableManager {
7390
*/
7491
std::unordered_map<std::string, CommandStatistics> cmdstat_map_;
7592
std::unordered_map<std::string, CommandStatistics> slow_command_count_;
76-
std::mutex command_mutex_;
77-
std::shared_mutex histograms_mutex_;
7893
std::shared_mutex slow_command_mutex_;
79-
std::shared_ptr<prometheus::Registry> prometheus_registry_;
80-
prometheus::Family<prometheus::Histogram>* histogram_family_;
81-
std::unordered_map<std::string, prometheus::Histogram*> histograms_;
94+
std::mutex data_mutex_;
95+
std::shared_ptr<HistogramData> data_;
8296
};
8397
#endif

src/pika_admin.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,8 +1497,8 @@ void InfoCmd::InfoCommandP99(std::string& info) {
14971497
tmp_stream.precision(2);
14981498
tmp_stream.setf(std::ios::fixed);
14991499
tmp_stream << "# Commands P99" << "\r\n";
1500-
auto histogram_family = g_pika_cmd_table_manager->GetHistograms();
1501-
1500+
auto data = g_pika_cmd_table_manager->GetHistogramsData();
1501+
auto* histogram_family = data->family;
15021502
for (const auto& metric_family : histogram_family->Collect()) {
15031503
for (const auto& metric : metric_family.metric) {
15041504
std::string command_name;

src/pika_cmd_table_manager.cc

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,17 @@
1515
extern std::unique_ptr<PikaConf> g_pika_conf;
1616

1717
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>());
3023
}
3124

3225
PikaCmdTableManager::PikaCmdTableManager() {
3326
cmds_ = std::make_unique<CmdTable>();
3427
cmds_->reserve(300);
35-
InitHistograms();
28+
std::atomic_store(&data_, std::make_shared<HistogramData>());
3629
}
3730

3831
void PikaCmdTableManager::InitCmdTable(void) {
@@ -80,24 +73,25 @@ void PikaCmdTableManager::RenameCommand(const std::string before, const std::str
8073
}
8174

8275
prometheus::Histogram& PikaCmdTableManager::GetHistogram(const std::string& opt) {
76+
auto current_data = std::atomic_load(&data_);
8377
{
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()) {
8780
return *(it->second);
8881
}
8982
}
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}
9488
);
95-
histograms_[opt] = &new_histogram;
89+
current_data->histograms[opt] = &new_histogram;
9690
return new_histogram;
9791
}
9892

99-
prometheus::Family<prometheus::Histogram>* PikaCmdTableManager::GetHistograms() {
100-
return histogram_family_;
93+
std::shared_ptr<HistogramData> PikaCmdTableManager::GetHistogramsData() {
94+
return std::atomic_load(&data_);
10195
}
10296

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

0 commit comments

Comments
 (0)