Skip to content

Commit

Permalink
resource_mgmt: add option to filter out results collected before a gi…
Browse files Browse the repository at this point in the history
…ven timepoint
  • Loading branch information
ballard26 committed Nov 7, 2023
1 parent 8e377a9 commit e83cae6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
19 changes: 14 additions & 5 deletions src/v/resource_mgmt/cpu_profiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ ss::future<> cpu_profiler::stop() {
ss::engine().set_cpu_profiler_enabled(false);
}

ss::future<std::vector<cpu_profiler::shard_samples>>
cpu_profiler::results(std::optional<ss::shard_id> shard_id) {
ss::future<std::vector<cpu_profiler::shard_samples>> cpu_profiler::results(
std::optional<ss::shard_id> shard_id,
std::optional<ss::lowres_clock::time_point> filter_before) {
if (_gate.is_closed()) {
co_return std::vector<shard_samples>{};
}
Expand Down Expand Up @@ -84,10 +85,15 @@ cpu_profiler::results(std::optional<ss::shard_id> shard_id) {
co_return results;
}

cpu_profiler::shard_samples cpu_profiler::shard_results() const {
cpu_profiler::shard_samples cpu_profiler::shard_results(
std::optional<ss::lowres_clock::time_point> filter_before) const {
size_t dropped_samples = 0;
absl::node_hash_map<ss::simple_backtrace, size_t> backtraces;
for (auto& results_buffer : _results_buffers) {
if (filter_before && results_buffer.polled_time < *filter_before) {
continue;
}

dropped_samples += results_buffer.dropped_samples;
for (auto& result : results_buffer.samples) {
backtraces[result.user_backtrace]++;
Expand Down Expand Up @@ -120,7 +126,8 @@ void cpu_profiler::poll_samples() {
resourceslog.trace(
"Polled {} samples from the CPU profiler", results_buffer.size());

_results_buffers.emplace_front(dropped_samples, std::move(results_buffer));
_results_buffers.emplace_front(
dropped_samples, std::move(results_buffer), ss::lowres_clock::now());
}

bool cpu_profiler::is_enabled() const {
Expand Down Expand Up @@ -178,6 +185,8 @@ cpu_profiler::override_and_get_results(
// enable the profiler if disabled pre-override.
on_enabled_change();

auto polling_start_time = ss::lowres_clock::now();

try {
co_await ss::sleep_abortable(timeout, _as);
} catch (const ss::sleep_aborted&) {
Expand All @@ -188,7 +197,7 @@ cpu_profiler::override_and_get_results(
// check if profiler should be disabled post-override.
on_enabled_change();

co_return co_await results(shard_id);
co_return co_await results(shard_id, polling_start_time);
}

} // namespace resources
10 changes: 7 additions & 3 deletions src/v/resource_mgmt/cpu_profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,15 @@ class cpu_profiler : public ss::peering_sharded_service<cpu_profiler> {

// Collects `shard_results()` for each shard in a node and returns
// them as a vector.
ss::future<std::vector<shard_samples>>
results(std::optional<ss::shard_id> shard_id);
ss::future<std::vector<shard_samples>> results(
std::optional<ss::shard_id> shard_id,
std::optional<ss::lowres_clock::time_point> filter_before = std::nullopt);

// Returns the samples and dropped samples from the shard this function
// is called on.
shard_samples shard_results() const;
shard_samples shard_results(
std::optional<ss::lowres_clock::time_point> filter_before
= std::nullopt) const;

// Enables the profiler for `timeout` milliseconds, then returns samples
// collected during that time period.
Expand All @@ -99,6 +102,7 @@ class cpu_profiler : public ss::peering_sharded_service<cpu_profiler> {
struct profiler_result {
size_t dropped_samples;
std::vector<seastar::cpu_profiler_trace> samples;
ss::lowres_clock::time_point polled_time;
};

// Buffers to copy sampled traces from seastar into so
Expand Down

0 comments on commit e83cae6

Please sign in to comment.