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 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/// * `symbol` (`GET`) - reports that symbolization is available
42/// * `symbol` (`POST`) - maps the `+`-separated hex addresses of the request
43/// body to function names, one `0x<address> <name>` line per resolved address
44///
45/// ## Usage of the `pprof` protocol
46/// @code
47/// jeprof --raw http://localhost:1188/service/jemalloc/pprof/heap > out.raw
48/// jeprof --text out.raw
49/// @endcode
50class Jemalloc final : public HttpHandlerBase {
51public:
52 enum class Command {
53 kStat,
54 kEnable,
55 kDisable,
56 kDump,
57 kBgThreadsSetMax,
58 kBgThreadsEnable,
59 kBgThreadsDisable,
60 kHeap,
61 kSymbol,
62 };
63 static std::optional<Command> GetCommandFromString(std::string_view str);
64 static std::string ListCommands();
65
66 Jemalloc(const components::ComponentConfig&, const components::ComponentContext&);
67
68 /// @ingroup userver_component_names
69 /// @brief The default name of server::handlers::Jemalloc
70 static constexpr std::string_view kName = "handler-jemalloc";
71
72 std::string HandleRequestThrow(const http::HttpRequest&, request::RequestContext&) const override;
73
74 static yaml_config::Schema GetStaticConfigSchema();
75
76private:
77 engine::TaskProcessor& fs_task_processor_;
78};
79
80} // namespace server::handlers
81
82template <>
83inline constexpr bool components::kHasValidate<server::handlers::Jemalloc> = true;
84
85USERVER_NAMESPACE_END