From 7dd265c42322b94b74942e38a0a294bab577ddb7 Mon Sep 17 00:00:00 2001 From: zhangyifan27 Date: Wed, 20 Jan 2021 17:48:54 +0800 Subject: [PATCH 1/4] fix(perf_counter): remove static map from counter_info --- src/runtime/profiler.cpp | 4 +--- src/runtime/profiler_command.cpp | 35 ++++++++++++++++---------------- src/runtime/profiler_header.h | 32 +++++++++++++---------------- 3 files changed, 33 insertions(+), 38 deletions(-) diff --git a/src/runtime/profiler.cpp b/src/runtime/profiler.cpp index 21cdd4f97e..0be2da3bc3 100644 --- a/src/runtime/profiler.cpp +++ b/src/runtime/profiler.cpp @@ -65,8 +65,6 @@ std::unique_ptr s_spec_profilers; int s_task_code_max = 0; -std::map counter_info::pointer_type; - counter_info *counter_info_ptr[] = { new counter_info({"queue.time", "qt"}, TASK_QUEUEING_TIME_NS, @@ -443,7 +441,7 @@ void profiler::install(service_spec &) "collect_call_count", collect_call_count, "whether to collect how many time this kind of tasks invoke each of other kinds tasks"); - s_spec_profilers[i].call_counts = new std::atomic[ s_task_code_max + 1 ]; + s_spec_profilers[i].call_counts = new std::atomic[s_task_code_max + 1]; std::fill(s_spec_profilers[i].call_counts, s_spec_profilers[i].call_counts + s_task_code_max + 1, 0); diff --git a/src/runtime/profiler_command.cpp b/src/runtime/profiler_command.cpp index dafbc692fe..e5a565a27f 100644 --- a/src/runtime/profiler_command.cpp +++ b/src/runtime/profiler_command.cpp @@ -77,11 +77,14 @@ static dsn_perf_counter_percentile_type_t find_percentail_type(const std::string static perf_counter_ptr_type find_counter_type(const std::string &name) { - auto it = counter_info::pointer_type.find(std::string(name)); - if (it == counter_info::pointer_type.end()) { - return PERF_COUNTER_INVALID; + for (int k = 0; k < PERF_COUNTER_COUNT; k++) { + perf_counter_ptr_type counter_type = static_cast(k); + const auto &keys = counter_info_ptr[counter_type]->keys; + if (std::find(keys.begin(), keys.end(), name) != keys.end()) { + return counter_info_ptr[counter_type]->counter_ptr_type; + } } - return it->second; + return PERF_COUNTER_INVALID; } std::string profiler_output_handler(const std::vector &args) @@ -377,8 +380,7 @@ std::string query_data_handler(const std::vector &args) if (s_spec_profilers[task_id].ptr[counter_type].get() == NULL) continue; - char name[20] = {0}; - strcpy(name, counter_info_ptr[counter_type]->title); + std::string name = counter_info_ptr[counter_type]->title; char name_suffix[10] = {0}; switch (task_spec::get(task_id)->type) { @@ -393,7 +395,7 @@ std::string query_data_handler(const std::vector &args) break; } - resp.name = std::string(name) + std::string(name_suffix); + resp.name = name + std::string(name_suffix); // get samples perf_counter::samples_t samples; @@ -491,25 +493,25 @@ std::string query_data_handler(const std::vector &args) timeGet = ((timeGet < 0) ? 0 : timeGet); - if (strcmp(counter_info_ptr[counter_type]->title, "RPC.SERVER(ns)") == 0 && + if (counter_info_ptr[counter_type]->title == "RPC.SERVER(ns)" && task_spec::get(task_id)->type == TASK_TYPE_RPC_REQUEST) timeList[0] = timeGet; - else if (strcmp(counter_info_ptr[counter_type]->title, "QUEUE(ns)") == 0 && + else if (counter_info_ptr[counter_type]->title == "QUEUE(ns)" && task_spec::get(task_id)->type == TASK_TYPE_RPC_REQUEST) timeList[1] = timeGet; - else if (strcmp(counter_info_ptr[counter_type]->title, "EXEC(ns)") == 0 && + else if (counter_info_ptr[counter_type]->title == "EXEC(ns)" && task_spec::get(task_id)->type == TASK_TYPE_RPC_REQUEST) timeList[2] = timeGet; - else if (strcmp(counter_info_ptr[counter_type]->title, "RPC.CLIENT(ns)") == 0 && + else if (counter_info_ptr[counter_type]->title == "RPC.CLIENT(ns)" && task_spec::get(task_id)->type == TASK_TYPE_RPC_RESPONSE) timeList[3] = timeGet; - else if (strcmp(counter_info_ptr[counter_type]->title, "QUEUE(ns)") == 0 && + else if (counter_info_ptr[counter_type]->title == "QUEUE(ns)" && task_spec::get(task_id)->type == TASK_TYPE_RPC_RESPONSE) timeList[4] = timeGet; - else if (strcmp(counter_info_ptr[counter_type]->title, "EXEC(ns)") == 0 && + else if (counter_info_ptr[counter_type]->title == "EXEC(ns)" && task_spec::get(task_id)->type == TASK_TYPE_RPC_RESPONSE) timeList[5] = timeGet; - else if (strcmp(counter_info_ptr[counter_type]->title, "AIO.LATENCY(ns)") == 0) + else if (counter_info_ptr[counter_type]->title == "AIO.LATENCY(ns)") timeList[6] = timeGet; } } @@ -554,8 +556,7 @@ std::string query_data_handler(const std::vector &args) if (s_spec_profilers[task_id].ptr[counter_type].get() == NULL) continue; - char name[20] = {0}; - strcpy(name, counter_info_ptr[counter_type]->title); + std::string name = counter_info_ptr[counter_type]->title; char name_suffix[10] = {0}; switch (task_spec::get(task_id)->type) { @@ -573,7 +574,7 @@ std::string query_data_handler(const std::vector &args) uint64_t sample = s_spec_profilers[task_id].ptr[counter_type]->get_latest_sample(); - data.push_back(nv_pair{std::string(name) + std::string(name_suffix), sample}); + data.push_back(nv_pair{name + std::string(name_suffix), sample}); } } if (task_spec::get(task_id)->type == TASK_TYPE_RPC_RESPONSE || diff --git a/src/runtime/profiler_header.h b/src/runtime/profiler_header.h index 827675e9de..4fbc30bc1a 100644 --- a/src/runtime/profiler_header.h +++ b/src/runtime/profiler_header.h @@ -59,28 +59,24 @@ enum perf_counter_ptr_type class counter_info { public: - counter_info(const std::vector &command_keys, - perf_counter_ptr_type _index, - dsn_perf_counter_type_t _type, - const char *_title, - const char *_unit) - : type(_type), title(_title), unit_name(_unit) + counter_info(const std::vector &command_keys, + perf_counter_ptr_type ptr_type, + dsn_perf_counter_type_t counter_type, + const std::string &title, + const std::string &unit) + : keys(command_keys), + counter_ptr_type(ptr_type), + type(counter_type), + title(title), + unit_name(unit) { - for (auto key : command_keys) { - if (key != nullptr) { - keys.push_back(key); - auto it = pointer_type.find(std::string(key)); - dassert(it == pointer_type.end(), "command '%s' already regisered", key); - pointer_type[std::string(key)] = _index; - } - } } - static std::map pointer_type; - std::vector keys; + std::vector keys; + perf_counter_ptr_type counter_ptr_type; dsn_perf_counter_type_t type; - const char *title; - const char *unit_name; + std::string title; + std::string unit_name; }; class profiler_output_data_type From 693b1581a245ab001dd3ef5099656f149794da10 Mon Sep 17 00:00:00 2001 From: zhangyifan27 Date: Thu, 21 Jan 2021 15:38:11 +0800 Subject: [PATCH 2/4] fix --- src/runtime/profiler_command.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/runtime/profiler_command.cpp b/src/runtime/profiler_command.cpp index e5a565a27f..7c0c2193de 100644 --- a/src/runtime/profiler_command.cpp +++ b/src/runtime/profiler_command.cpp @@ -77,11 +77,10 @@ static dsn_perf_counter_percentile_type_t find_percentail_type(const std::string static perf_counter_ptr_type find_counter_type(const std::string &name) { - for (int k = 0; k < PERF_COUNTER_COUNT; k++) { - perf_counter_ptr_type counter_type = static_cast(k); - const auto &keys = counter_info_ptr[counter_type]->keys; + for (int i = 0; i < PERF_COUNTER_COUNT; i++) { + const auto &keys = counter_info_ptr[i]->keys; if (std::find(keys.begin(), keys.end(), name) != keys.end()) { - return counter_info_ptr[counter_type]->counter_ptr_type; + return counter_info_ptr[i]->counter_ptr_type; } } return PERF_COUNTER_INVALID; From bae43f8938a17d6d5119dc91bc4b429b30aaaba9 Mon Sep 17 00:00:00 2001 From: zhangyifan27 Date: Thu, 21 Jan 2021 15:40:48 +0800 Subject: [PATCH 3/4] fix --- src/runtime/profiler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/profiler.cpp b/src/runtime/profiler.cpp index 0be2da3bc3..b7f2e303c5 100644 --- a/src/runtime/profiler.cpp +++ b/src/runtime/profiler.cpp @@ -441,7 +441,7 @@ void profiler::install(service_spec &) "collect_call_count", collect_call_count, "whether to collect how many time this kind of tasks invoke each of other kinds tasks"); - s_spec_profilers[i].call_counts = new std::atomic[s_task_code_max + 1]; + s_spec_profilers[i].call_counts = new std::atomic[ s_task_code_max + 1 ]; std::fill(s_spec_profilers[i].call_counts, s_spec_profilers[i].call_counts + s_task_code_max + 1, 0); From 93ea9ae46bea5235ea9a688b03747bf684e3a538 Mon Sep 17 00:00:00 2001 From: zhangyifan27 Date: Thu, 21 Jan 2021 17:23:17 +0800 Subject: [PATCH 4/4] fix --- src/runtime/profiler_command.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/runtime/profiler_command.cpp b/src/runtime/profiler_command.cpp index 7c0c2193de..ef92254a7c 100644 --- a/src/runtime/profiler_command.cpp +++ b/src/runtime/profiler_command.cpp @@ -379,7 +379,7 @@ std::string query_data_handler(const std::vector &args) if (s_spec_profilers[task_id].ptr[counter_type].get() == NULL) continue; - std::string name = counter_info_ptr[counter_type]->title; + const std::string &name = counter_info_ptr[counter_type]->title; char name_suffix[10] = {0}; switch (task_spec::get(task_id)->type) { @@ -555,7 +555,7 @@ std::string query_data_handler(const std::vector &args) if (s_spec_profilers[task_id].ptr[counter_type].get() == NULL) continue; - std::string name = counter_info_ptr[counter_type]->title; + const std::string &name = counter_info_ptr[counter_type]->title; char name_suffix[10] = {0}; switch (task_spec::get(task_id)->type) {