-
Notifications
You must be signed in to change notification settings - Fork 59
perf-counter: add remote command to get perf-counters by substr/prefi… #261
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suffix,不是 postfix There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
@@ -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"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 移到 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个结构里面有两个timestamp? 能干掉一个吗 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
There was a problem hiding this comment.
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 原因是什么
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
用户指定多个的话,是或关系,不是与关系