Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

perf-counter: add remote command to get perf-counters by substr/prefi… #261

Merged
merged 2 commits into from
Jun 17, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion include/dsn/perf_counter/perf_counters.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,18 @@ class perf_counters : public utils::singleton<perf_counters>
const snapshot_iterator &v,
std::vector<bool> *found) const;

// this function collects all counters to perf_counter_info which matches
// this function collects all counters to perf_counter_info which match
// any of the regular expressions in args and returns the json representation
// of perf_counter_info
std::string list_snapshot_by_regexp(const std::vector<std::string> &args) const;

// this function collects all counters to perf_counter_info which satisfy
// any of the filters generated by args and returns the json representation
// of perf_counter_info
std::string list_snapshot_by_literal(
const std::vector<std::string> &args,
std::function<bool(const std::string &arg, const counter_snapshot &cs)> filter) const;

private:
// full_name = perf_counter::build_full_name(...);
perf_counter *new_counter(const char *app,
Expand Down
74 changes: 72 additions & 2 deletions src/core/perf_counter/perf_counters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,45 @@ perf_counters::perf_counters()
{
command_manager::instance().register_command(
{"perf-counters"},
"perf-counters - query perf counters, supporting filter by POSIX basic regular expressions",
"perf-counters [name-filter]...",
"perf-counters - query perf counters, filtered by OR of POSIX basic regular expressions",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filtered by 就对了,不需要 OR of,下面同理,加 OR of 原因是什么

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

用户指定多个的话,是或关系,不是与关系

"perf-counters [regexp]...",
[](const std::vector<std::string> &args) {
return perf_counters::instance().list_snapshot_by_regexp(args);
});
command_manager::instance().register_command(
{"perf-counters-by-substr"},
"perf-counters-by-substr - query perf counters, filtered by OR of substrs",
"perf-counters-by-substr [substr]...",
[](const std::vector<std::string> &args) {
return perf_counters::instance().list_snapshot_by_literal(
args, [](const std::string &arg, const counter_snapshot &cs) {
return cs.name.find(arg) != std::string::npos;
});
});
command_manager::instance().register_command(
{"perf-counters-by-prefix"},
"perf-counters-by-prefix - query perf counters, filtered by OR of prefix strings",
"perf-counters-by-prefix [prefix]...",
[](const std::vector<std::string> &args) {
return perf_counters::instance().list_snapshot_by_literal(
args, [](const std::string &arg, const counter_snapshot &cs) {
return cs.name.size() >= arg.size() &&
::memcmp(cs.name.c_str(), arg.c_str(), arg.size()) == 0;
});
});
command_manager::instance().register_command(
{"perf-counters-by-postfix"},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suffix,不是 postfix

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

postfix也是后缀的意思。而且我们之前的各种用法都是postfix,那就都一致吧

"perf-counters-by-postfix - query perf counters, filtered by OR of postfix strings",
"perf-counters-by-postfix [postfix]...",
[](const std::vector<std::string> &args) {
return perf_counters::instance().list_snapshot_by_literal(
args, [](const std::string &arg, const counter_snapshot &cs) {
return cs.name.size() >= arg.size() &&
::memcmp(cs.name.c_str() + cs.name.size() - arg.size(),
arg.c_str(),
arg.size()) == 0;
});
});
}

perf_counters::~perf_counters() = default;
Expand Down Expand Up @@ -201,6 +235,42 @@ std::string perf_counters::list_snapshot_by_regexp(const std::vector<std::string
return ss.str();
}

// the filter should return true if the counter satisfies condition.
std::string perf_counters::list_snapshot_by_literal(
const std::vector<std::string> &args,
std::function<bool(const std::string &arg, const counter_snapshot &cs)> filter) const
{
perf_counter_info info;

snapshot_iterator visitor = [&args, &info, &filter](const counter_snapshot &cs) {
bool matched = false;
if (args.empty()) {
matched = true;
} else {
for (auto &arg : args) {
if (filter(arg, cs)) {
matched = true;
break;
}
}
}

if (matched) {
info.counters.emplace_back(cs.name.c_str(), cs.type, cs.value);
}
};
iterate_snapshot(visitor);
info.result = "OK";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

移到info变量的定义后面一行吧

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为了和 by_regexp() 函数里面看起来比较一致


std::stringstream ss;
info.timestamp = _timestamp;
char buf[20];
utils::time_ms_to_date_time(info.timestamp * 1000, buf, sizeof(buf));
info.timestamp_str = buf;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个结构里面有两个timestamp? 能干掉一个吗

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个当时就是为了便于阅读,所以特意放了个timestamp_str,都保留问题不大

info.encode_json_state(ss);
return ss.str();
}

void perf_counters::take_snapshot()
{
builtin_counters::instance().update_counters();
Expand Down