userver: userver/server/handlers/jemalloc.hpp Source File
Loading...
Searching...
No Matches
jemalloc.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/server/handlers/jemalloc.hpp
4/// @brief @copybrief server::handlers::Jemalloc
5
6#include <userver/engine/task/task_processor_fwd.hpp>
7#include <userver/server/handlers/http_handler_base.hpp>
8
9USERVER_NAMESPACE_BEGIN
10
11namespace server::handlers {
12
13/// @ingroup userver_components userver_http_handlers
14///
15/// @brief Handler that controls the jemalloc allocator.
16///
17/// The component also implements the `pprof` remote profiling protocol, so that
18/// `jeprof` can fetch and symbolize a heap profile without a copy of the
19/// service binary. The service symbolizes its own addresses, which removes both
20/// problems of the binary-based flow: hauling a multi-gigabyte binary off the
21/// build cache, and getting silently wrong symbols when that binary does not
22/// match the deployed one.
23///
24/// The component has no service configuration except the
25/// @ref userver_http_handlers "common handler options".
26///
27/// ## Static configuration example:
28///
29/// @snippet core/src/components/common_server_component_list_test.cpp Sample handler jemalloc component config
30///
31/// ## Schema
32/// Set an URL path argument `command` to one of the following values:
33/// * `stat` - to get jemalloc stats
34/// * `enable` - to start memory profiling
35/// * `disable` - to stop memory profiling
36/// * `dump` - to get jemalloc profiling dump
37/// * `bg_threads_set_max` - to set maximum number of background threads
38/// * `bg_threads_enable` - to start background threads
39/// * `bg_threads_disable` - to *synchronously* stop background threads
40/// * `heap` (`GET`) - the jemalloc heap profile dump, in the `pprof` format
41/// * `cmdline` (`GET`, Linux) - the process arguments separated by null bytes
42/// * `symbol` (`GET`) - reports that symbolization is available
43/// * `symbol` (`POST`) - maps the `+`-separated hex addresses of the request
44/// body to function names, one `0x<address> <name>` line per resolved address
45///
46/// ## Usage of the `pprof` protocol
47/// @code
48/// jeprof --raw http://localhost:1188/service/jemalloc/pprof/heap > out.raw
49/// jeprof --text out.raw
50/// @endcode
51class Jemalloc final : public HttpHandlerBase {
52public:
53 enum class Command {
54 kStat,
55 kEnable,
56 kDisable,
57 kDump,
58 kBgThreadsSetMax,
59 kBgThreadsEnable,
60 kBgThreadsDisable,
61 kHeap,
62 kCmdline,
63 kSymbol,
64 };
65 static std::optional<Command> GetCommandFromString(std::string_view str);
66 static std::string ListCommands();
67
68 Jemalloc(const components::ComponentConfig&, const components::ComponentContext&);
69
70 /// @ingroup userver_component_names
71 /// @brief The default name of server::handlers::Jemalloc
72 static constexpr std::string_view kName = "handler-jemalloc";
73
74 std::string HandleRequestThrow(const http::HttpRequest&, request::RequestContext&) const override;
75
76 static yaml_config::Schema GetStaticConfigSchema();
77
78private:
79 engine::TaskProcessor& fs_task_processor_;
80};
81
82} // namespace server::handlers
83
84template <>
85inline constexpr bool components::kHasValidate<server::handlers::Jemalloc> = true;
86
87USERVER_NAMESPACE_END