From d71b5171eb7f66d4a71d7b889661a80958c9829c Mon Sep 17 00:00:00 2001 From: Zhang Yifan Date: Mon, 25 Jan 2021 11:28:13 +0800 Subject: [PATCH] fix(perf_counter): remove static map from counter_info (#735) --- src/runtime/profiler.cpp | 2 -- src/runtime/profiler_command.cpp | 34 ++++++++++++++++---------------- src/runtime/profiler_header.h | 32 +++++++++++++----------------- 3 files changed, 31 insertions(+), 37 deletions(-) diff --git a/src/runtime/profiler.cpp b/src/runtime/profiler.cpp index 21cdd4f97e..b7f2e303c5 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, diff --git a/src/runtime/profiler_command.cpp b/src/runtime/profiler_command.cpp index dafbc692fe..ef92254a7c 100644 --- a/src/runtime/profiler_command.cpp +++ b/src/runtime/profiler_command.cpp @@ -77,11 +77,13 @@ 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 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[i]->counter_ptr_type; + } } - return it->second; + return PERF_COUNTER_INVALID; } std::string profiler_output_handler(const std::vector &args) @@ -377,8 +379,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); + const std::string &name = counter_info_ptr[counter_type]->title; char name_suffix[10] = {0}; switch (task_spec::get(task_id)->type) { @@ -393,7 +394,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 +492,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 +555,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); + const std::string &name = counter_info_ptr[counter_type]->title; char name_suffix[10] = {0}; switch (task_spec::get(task_id)->type) { @@ -573,7 +573,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