Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add arch/version to cpu profiler #24658

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
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
2 changes: 2 additions & 0 deletions src/v/redpanda/admin/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,10 @@ redpanda_cc_library(
"//src/v/strings:utf8",
"//src/v/transform",
"//src/v/transform:fwd",
"//src/v/utils:arch",
"//src/v/utils:functional",
"//src/v/utils:unresolved_address",
"//src/v/version",
"//src/v/wasm:api",
"@abseil-cpp//absl/container:flat_hash_map",
"@abseil-cpp//absl/container:flat_hash_set",
Expand Down
44 changes: 37 additions & 7 deletions src/v/redpanda/admin/api-doc/debug.json
Original file line number Diff line number Diff line change
Expand Up @@ -588,10 +588,7 @@
"produces": [
"application/json"
],
"type": "array",
"items": {
"type": "cpu_profile_shard_samples"
},
"type": "cpu_profile_result",
"parameters": [
{
"name": "shard",
Expand Down Expand Up @@ -641,9 +638,42 @@
}
],
"models": {
"cpu_profile_result": {
"id": "cpu_profile_result",
"description": "top level object for a cpu profile request",
"properties" : {
"schema": {
"description": "the schema version of the response",
"type": "int"
},
"arch": {
"description": "the CPU architecture the profile was taken on, one of [amd64, arm64]",
"type": "string"
},
"version": {
"description": "the version of Redpanda the profile was taken on",
"type": "string"
},
"wait_ms": {
"description": "the requested sample period, in milliseconds, if specified using wait_ms query parameter, or missing otherwise",
"type": "int"
},
"sample_period_ms": {
"description": "the configured sample period in milliseconds (each shard samples at this rate)",
"type": "int"
},
"profile": {
"description": "the profile samples, one object per shard",
"type": "array",
"items": {
"type": "cpu_profile_shard_samples"
}
}
}
},
"cpu_profile_shard_samples": {
"id": "cpu_profile_sample",
"description": "cpu profile sample",
"id": "cpu_profile_shard_samples",
"description": "cpu profile object for one shard",
"properties": {
"shard_id": {
"type": "long",
Expand All @@ -663,7 +693,7 @@
},
"cpu_profile_sample": {
"id": "cpu_profile_sample",
"description": "cpu profile sample",
"description": "an individual cpu profile sample with backtrace and count",
"properties": {
"user_backtrace": {
"type": "string",
Expand Down
96 changes: 61 additions & 35 deletions src/v/redpanda/admin/debug.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,23 @@
#include "cluster/cloud_storage_size_reducer.h"
#include "cluster/controller.h"
#include "cluster/controller_stm.h"
#include "cluster/members_manager.h"
#include "cluster/metadata_cache.h"
#include "cluster/partition_leaders_table.h"
#include "cluster/shard_table.h"
#include "cluster/topics_frontend.h"
#include "cluster/types.h"
#include "config/configuration.h"
#include "config/node_config.h"
#include "container/lw_shared_container.h"
#include "json/validator.h"
#include "model/fundamental.h"
#include "model/metadata.h"
#include "redpanda/admin/api-doc/debug.json.hh"
#include "redpanda/admin/server.h"
#include "redpanda/admin/util.h"
#include "resource_mgmt/cpu_profiler.h"
#include "serde/rw/rw.h"
#include "storage/kvstore.h"
#include "utils/arch.h"
#include "version/version.h"

#include <seastar/core/shard_id.hh>
#include <seastar/core/sstring.hh>
Expand Down Expand Up @@ -505,38 +504,47 @@ void check_shard_id(seastar::shard_id id) {

ss::future<ss::json::json_return_type>
admin_server::cpu_profile_handler(std::unique_ptr<ss::http::request> req) {
vlog(adminlog.info, "Request to sampled cpu profile");
using namespace ss::httpd::debug_json;

auto shard_param = req->get_query_param("shard");
auto wait_param = req->get_query_param("wait_ms");

vlog(
adminlog.info,
"Request to sample cpu profile, shard: {}, wait_ms: {}",
shard_param,
wait_param);

cpu_profile_result result;
// update this when you make an incompatible change to the result schema
result.schema = 2;
result.sample_period_ms = _cpu_profiler.local().sample_period() / 1ms;

std::optional<size_t> shard_id;
if (auto e = req->get_query_param("shard"); !e.empty()) {
if (!shard_param.empty()) {
try {
shard_id = boost::lexical_cast<size_t>(e);
shard_id = boost::lexical_cast<size_t>(shard_param);
} catch (const boost::bad_lexical_cast&) {
throw ss::httpd::bad_param_exception(
fmt::format("Invalid parameter 'shard_id' value {{{}}}", e));
throw ss::httpd::bad_param_exception(fmt::format(
"Invalid parameter 'shard_id' value {{{}}}", shard_param));
}
}

if (shard_id.has_value()) {
check_shard_id(*shard_id);
}

std::optional<std::chrono::milliseconds> wait_ms;
if (auto e = req->get_query_param("wait_ms"); !e.empty()) {
if (!wait_param.empty()) {
try {
wait_ms = std::chrono::milliseconds(
boost::lexical_cast<uint64_t>(e));
boost::lexical_cast<uint64_t>(wait_param));
} catch (const boost::bad_lexical_cast&) {
throw ss::httpd::bad_param_exception(
fmt::format("Invalid parameter 'wait_ms' value {{{}}}", e));
throw ss::httpd::bad_param_exception(fmt::format(
"Invalid parameter 'wait_ms' value {{{}}}", wait_param));
}
}

if (wait_ms.has_value()) {
if (*wait_ms < 1ms || *wait_ms > 15min) {
throw ss::httpd::bad_param_exception(
"wait_ms must be between 1ms and 15min");
}
result.wait_ms = *wait_ms / 1ms;
}

std::vector<resources::cpu_profiler::shard_samples> profiles;
Expand All @@ -547,23 +555,41 @@ admin_server::cpu_profile_handler(std::unique_ptr<ss::http::request> req) {
*wait_ms, shard_id);
}

co_return co_await ss::make_ready_future<ss::json::json_return_type>(
ss::json::stream_range_as_array(
lw_shared_container(std::move(profiles)),
[](const resources::cpu_profiler::shard_samples& profile) {
ss::httpd::debug_json::cpu_profile_shard_samples ret;
ret.shard_id = profile.shard;
ret.dropped_samples = profile.dropped_samples;

for (auto& sample : profile.samples) {
ss::httpd::debug_json::cpu_profile_sample s;
s.occurrences = sample.occurrences;
s.user_backtrace = sample.user_backtrace;

ret.samples.push(s);
}
return ret;
}));
result.arch = ss::sstring{util::cpu_arch::current().name};
// this version will help us identify the right symbols, it is like so:
// In released builds:
// v24.2.11 - 29b8a8e2329043d587e6de2cbf8e73cc32d9d69e
// Local bazel builds:
// 0.0.0-dev - 0000000000000000000000000000000000000000
// Local cmake builds with ENABLE_GIT_HASH=OFF and ENABLE_GIT_VERSION=OFF:
// no_version - 000-dev
result.version = ss::sstring{redpanda_version()};

auto& profile_vec = result.profile._elements;
profile_vec.reserve(profiles.size());

for (auto& shard_profile : profiles) {
ss::httpd::debug_json::cpu_profile_shard_samples shard_samples;
shard_samples.shard_id = shard_profile.shard;
shard_samples.dropped_samples = shard_profile.dropped_samples;

// build up the samples list
std::vector<cpu_profile_sample> samples;
samples.reserve(shard_profile.samples.size());
for (auto& sample : shard_profile.samples) {
ss::httpd::debug_json::cpu_profile_sample json_sample;
json_sample.occurrences = sample.occurrences;
json_sample.user_backtrace = sample.user_backtrace;
samples.emplace_back(std::move(json_sample));
}

shard_samples.samples._set = true;
shard_samples.samples._elements = std::move(samples);

result.profile.push(shard_samples);
}

co_return co_await ssx::now(stream_object(result));
}

ss::future<ss::json::json_return_type>
Expand Down
4 changes: 4 additions & 0 deletions src/v/resource_mgmt/cpu_profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ class cpu_profiler : public ss::peering_sharded_service<cpu_profiler> {
ss::future<std::vector<shard_samples>> collect_results_for_period(
std::chrono::milliseconds timeout, std::optional<ss::shard_id> shard_id);

// Return the configured sample period for the profiler. Note that this will
// return the configured value even if the profiler is currently disabled.
std::chrono::milliseconds sample_period() const { return _sample_period(); }

private:
// impl for the above
ss::future<>
Expand Down
8 changes: 8 additions & 0 deletions src/v/utils/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ load("//bazel:build.bzl", "redpanda_cc_library")

package(default_visibility = ["//visibility:public"])

redpanda_cc_library(
name = "arch",
hdrs = [
"arch.h",
],
include_prefix = "utils",
)

redpanda_cc_library(
name = "auto_fmt",
hdrs = [
Expand Down
45 changes: 45 additions & 0 deletions src/v/utils/arch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2024 Redpanda Data, Inc.
*
* Use of this software is governed by the Business Source License
* included in the file licenses/BSL.md
*
* As of the Change Date specified in that file, in accordance with
* the Business Source License, use of this software will be governed
* by the Apache License, Version 2.0
*/

#include <string_view>

namespace util {
class cpu_arch {
// consteval means our string_view will have static lifetime
explicit consteval cpu_arch(std::string_view name)
: name{name} {}

friend struct arch;

public:
std::string_view name;

static inline constexpr cpu_arch current();

bool operator<=>(const cpu_arch&) const = default;
};

struct arch {
static constexpr cpu_arch AMD64{"amd64"};
static constexpr cpu_arch ARM64{"arm64"};
};

inline constexpr cpu_arch cpu_arch::current() {
#if defined(__x86_64__)
return arch::AMD64;
#elif defined(__aarch64__)
return arch::ARM64
#else
#error unknown arch
#endif
}

} // namespace util
13 changes: 13 additions & 0 deletions src/v/utils/tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -534,3 +534,16 @@ redpanda_cc_gtest(
"@seastar",
],
)

redpanda_cc_gtest(
name = "arch_test",
timeout = "short",
srcs = [
"arch_test.cc",
],
deps = [
"//src/v/test_utils:gtest",
"//src/v/utils:arch",
"@googletest//:gtest",
],
)
24 changes: 24 additions & 0 deletions src/v/utils/tests/arch_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2024 Redpanda Data, Inc.
*
* Use of this software is governed by the Business Source License
* included in the file licenses/BSL.md
*
* As of the Change Date specified in that file, in accordance with
* the Business Source License, use of this software will be governed
* by the Apache License, Version 2.0
*/

#include "utils/arch.h"

#include <gtest/gtest.h>

using namespace util;

#ifdef __x86_64__
constexpr auto expected_arch = arch::AMD64;
#else
constexpr auto expected_arch = arch::ARM64;
#endif

GTEST_TEST(arch, equality) { EXPECT_EQ(cpu_arch::current(), expected_arch); }
Loading