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

Commit

Permalink
perf-counter: add remote command to get perf-counters by substr/prefi…
Browse files Browse the repository at this point in the history
…x/postfix (#261)
  • Loading branch information
qinzuoyan authored and neverchanje committed Jun 20, 2019
1 parent fcac203 commit bf6c29a
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 3 deletions.
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",
"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"},
"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";

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;
info.encode_json_state(ss);
return ss.str();
}

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

0 comments on commit bf6c29a

Please sign in to comment.