Skip to content

Commit f93e696

Browse files
committed
new(cmake,userspace): expose jemalloc stats in stats writer and prometheus metircs.
Signed-off-by: Federico Di Pierro <[email protected]>
1 parent 6bc348b commit f93e696

7 files changed

+94
-2
lines changed

cmake/modules/jemalloc.cmake

+7-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515

1616
option(USE_BUNDLED_JEMALLOC "Use bundled jemalloc allocator" ${USE_BUNDLED_DEPS})
1717

18-
if(JEMALLOC_LIB)
18+
if(JEMALLOC_INCLUDE)
1919
# we already have JEMALLOC
2020
elseif(NOT USE_BUNDLED_JEMALLOC)
21+
find_path(JEMALLOC_INCLUDE jemalloc/jemalloc.h)
2122
if(BUILD_SHARED_LIBS)
2223
set(JEMALLOC_LIB_SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX})
2324
else()
@@ -37,6 +38,7 @@ else()
3738
endif()
3839
set(JEMALLOC_SRC "${PROJECT_BINARY_DIR}/jemalloc-prefix/src")
3940
set(JEMALLOC_LIB "${JEMALLOC_SRC}/jemalloc/lib/libjemalloc${JEMALLOC_LIB_SUFFIX}")
41+
set(JEMALLOC_INCLUDE "${JEMALLOC_SRC}/jemalloc/include/jemalloc")
4042
ExternalProject_Add(
4143
jemalloc
4244
PREFIX "${PROJECT_BINARY_DIR}/jemalloc-prefix"
@@ -49,7 +51,7 @@ else()
4951
UPDATE_COMMAND ""
5052
BUILD_BYPRODUCTS ${JEMALLOC_LIB}
5153
)
52-
message(STATUS "Using bundled jemalloc: lib: ${JEMALLOC_LIB}")
54+
message(STATUS "Using bundled jemalloc: include: ${JEMALLOC_INCLUDE}, lib: ${JEMALLOC_LIB}")
5355
install(
5456
FILES "${JEMALLOC_LIB}"
5557
DESTINATION "${CMAKE_INSTALL_LIBDIR}/${LIBS_PACKAGE_NAME}"
@@ -62,3 +64,6 @@ endif()
6264
if(NOT TARGET jemalloc)
6365
add_custom_target(jemalloc)
6466
endif()
67+
68+
include_directories(${JEMALLOC_INCLUDE})
69+
add_compile_definitions(HAS_JEMALLOC)

falco.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,7 @@ metrics:
11241124
kernel_event_counters_per_cpu_enabled: false
11251125
libbpf_stats_enabled: true
11261126
plugins_metrics_enabled: true
1127+
jemalloc_stats_enabled: false
11271128
convert_memory_to_mb: true
11281129
include_empty_values: false
11291130

userspace/falco/config_json_schema.h

+3
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,9 @@ const char config_schema_string[] = LONG_STRING_CONST(
569569
},
570570
"include_empty_values": {
571571
"type": "boolean"
572+
},
573+
"jemalloc_stats_enabled": {
574+
"type": "boolean"
572575
}
573576
},
574577
"minProperties": 1,

userspace/falco/configuration.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,9 @@ void falco_configuration::load_yaml(const std::string &config_name) {
610610
if(m_config.get_scalar<bool>("metrics.plugins_metrics_enabled", true)) {
611611
m_metrics_flags |= METRICS_V2_PLUGINS;
612612
}
613+
if(m_config.get_scalar<bool>("metrics.jemalloc_stats_enabled", true)) {
614+
m_metrics_flags |= METRICS_V2_JEMALLOC_STATS;
615+
}
613616

614617
m_metrics_convert_memory_to_mb =
615618
m_config.get_scalar<bool>("metrics.convert_memory_to_mb", true);

userspace/falco/configuration.h

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ limitations under the License.
3737
#include "event_drops.h"
3838
#include "falco_outputs.h"
3939

40+
// Falco only metric
41+
#define METRICS_V2_JEMALLOC_STATS 1 << 31
42+
4043
enum class engine_kind_t : uint8_t { KMOD, EBPF, MODERN_EBPF, REPLAY, GVISOR, NODRIVER };
4144

4245
// Map that holds { config filename | validation status } for each loaded config file.

userspace/falco/falco_metrics.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ limitations under the License.
2323

2424
#include <libsinsp/sinsp.h>
2525

26+
#ifdef HAS_JEMALLOC
27+
#include <jemalloc.h>
28+
#endif
29+
2630
namespace fs = std::filesystem;
2731

2832
/*!
@@ -249,6 +253,38 @@ std::string falco_metrics::to_text(const falco::app::state& state) {
249253
}
250254
}
251255
}
256+
#ifdef HAS_JEMALLOC
257+
if(state.config->m_metrics_flags & METRICS_V2_JEMALLOC_STATS) {
258+
nlohmann::json j;
259+
malloc_stats_print(
260+
[](void* to, const char* from) {
261+
nlohmann::json* j = (nlohmann::json*)to;
262+
*j = nlohmann::json::parse(from);
263+
},
264+
&j,
265+
"Jmdablxeg");
266+
const auto& j_stats = j["jemalloc"]["stats"];
267+
for(auto it = j_stats.begin(); it != j_stats.end(); ++it) {
268+
if(it.value().is_number_unsigned()) {
269+
std::uint64_t val = it.value().template get<std::uint64_t>();
270+
std::string key = "jemalloc." + it.key();
271+
auto metric = libs::metrics::libsinsp_metrics::new_metric(
272+
key.c_str(),
273+
METRICS_V2_JEMALLOC_STATS,
274+
METRIC_VALUE_TYPE_U64,
275+
METRIC_VALUE_UNIT_MEMORY_BYTES,
276+
METRIC_VALUE_METRIC_TYPE_MONOTONIC,
277+
val);
278+
prometheus_metrics_converter.convert_metric_to_unit_convention(metric);
279+
prometheus_text +=
280+
prometheus_metrics_converter.convert_metric_to_text_prometheus(
281+
metric,
282+
"falcosecurity",
283+
"falco");
284+
}
285+
}
286+
}
287+
#endif
252288
}
253289

254290
// Libs metrics categories

userspace/falco/stats_writer.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ limitations under the License.
3232
#include <libscap/strl.h>
3333
#include <libscap/scap_vtable.h>
3434

35+
#ifdef HAS_JEMALLOC
36+
#include <jemalloc.h>
37+
#endif
38+
3539
namespace fs = std::filesystem;
3640

3741
// note: ticker_t is an uint16_t, which is enough because we don't care about
@@ -434,6 +438,43 @@ void stats_writer::collector::get_metrics_output_fields_additional(
434438
}
435439
}
436440

441+
#ifdef HAS_JEMALLOC
442+
if(m_writer->m_config->m_metrics_flags & METRICS_V2_JEMALLOC_STATS) {
443+
nlohmann::json j;
444+
malloc_stats_print(
445+
[](void* to, const char* from) {
446+
nlohmann::json* j = (nlohmann::json*)to;
447+
*j = nlohmann::json::parse(from);
448+
},
449+
&j,
450+
"Jmdablxeg");
451+
const auto& j_stats = j["jemalloc"]["stats"];
452+
for(auto it = j_stats.begin(); it != j_stats.end(); ++it) {
453+
if(it.value().is_number_unsigned()) {
454+
std::uint64_t val = it.value().template get<std::uint64_t>();
455+
if(m_writer->m_config->m_metrics_include_empty_values || val != 0) {
456+
std::string key = "falco.jemalloc." + it.key() + "_bytes";
457+
auto metric = libs::metrics::libsinsp_metrics::new_metric(
458+
key.c_str(),
459+
METRICS_V2_JEMALLOC_STATS,
460+
METRIC_VALUE_TYPE_U64,
461+
METRIC_VALUE_UNIT_MEMORY_BYTES,
462+
METRIC_VALUE_METRIC_TYPE_MONOTONIC,
463+
val);
464+
if(m_writer->m_config->m_metrics_convert_memory_to_mb &&
465+
m_writer->m_output_rule_metrics_converter) {
466+
m_writer->m_output_rule_metrics_converter
467+
->convert_metric_to_unit_convention(metric);
468+
output_fields[metric.name] = metric.value.d;
469+
} else {
470+
output_fields[metric.name] = metric.value.u64;
471+
}
472+
}
473+
}
474+
}
475+
}
476+
#endif
477+
437478
#if defined(__linux__) and !defined(MINIMAL_BUILD) and !defined(__EMSCRIPTEN__)
438479
if(m_writer->m_libs_metrics_collector && m_writer->m_output_rule_metrics_converter) {
439480
// Libs metrics categories

0 commit comments

Comments
 (0)