new(cmake,userspace): expose jemalloc stats in stats writer and prometheus metircs.

Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
This commit is contained in:
Federico Di Pierro 2024-11-20 12:10:47 +01:00 committed by poiana
parent d007418fd3
commit 1c71777dbd
7 changed files with 94 additions and 2 deletions

View File

@ -15,9 +15,10 @@
option(USE_BUNDLED_JEMALLOC "Use bundled jemalloc allocator" ${USE_BUNDLED_DEPS}) option(USE_BUNDLED_JEMALLOC "Use bundled jemalloc allocator" ${USE_BUNDLED_DEPS})
if(JEMALLOC_LIB) if(JEMALLOC_INCLUDE)
# we already have JEMALLOC # we already have JEMALLOC
elseif(NOT USE_BUNDLED_JEMALLOC) elseif(NOT USE_BUNDLED_JEMALLOC)
find_path(JEMALLOC_INCLUDE jemalloc/jemalloc.h)
if(BUILD_SHARED_LIBS) if(BUILD_SHARED_LIBS)
set(JEMALLOC_LIB_SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX}) set(JEMALLOC_LIB_SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX})
else() else()
@ -37,6 +38,7 @@ else()
endif() endif()
set(JEMALLOC_SRC "${PROJECT_BINARY_DIR}/jemalloc-prefix/src") set(JEMALLOC_SRC "${PROJECT_BINARY_DIR}/jemalloc-prefix/src")
set(JEMALLOC_LIB "${JEMALLOC_SRC}/jemalloc/lib/libjemalloc${JEMALLOC_LIB_SUFFIX}") set(JEMALLOC_LIB "${JEMALLOC_SRC}/jemalloc/lib/libjemalloc${JEMALLOC_LIB_SUFFIX}")
set(JEMALLOC_INCLUDE "${JEMALLOC_SRC}/jemalloc/include/jemalloc")
ExternalProject_Add( ExternalProject_Add(
jemalloc jemalloc
PREFIX "${PROJECT_BINARY_DIR}/jemalloc-prefix" PREFIX "${PROJECT_BINARY_DIR}/jemalloc-prefix"
@ -49,7 +51,7 @@ else()
UPDATE_COMMAND "" UPDATE_COMMAND ""
BUILD_BYPRODUCTS ${JEMALLOC_LIB} BUILD_BYPRODUCTS ${JEMALLOC_LIB}
) )
message(STATUS "Using bundled jemalloc: lib: ${JEMALLOC_LIB}") message(STATUS "Using bundled jemalloc: include: ${JEMALLOC_INCLUDE}, lib: ${JEMALLOC_LIB}")
install( install(
FILES "${JEMALLOC_LIB}" FILES "${JEMALLOC_LIB}"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/${LIBS_PACKAGE_NAME}" DESTINATION "${CMAKE_INSTALL_LIBDIR}/${LIBS_PACKAGE_NAME}"
@ -62,3 +64,6 @@ endif()
if(NOT TARGET jemalloc) if(NOT TARGET jemalloc)
add_custom_target(jemalloc) add_custom_target(jemalloc)
endif() endif()
include_directories(${JEMALLOC_INCLUDE})
add_compile_definitions(HAS_JEMALLOC)

View File

@ -1136,6 +1136,7 @@ metrics:
kernel_event_counters_per_cpu_enabled: false kernel_event_counters_per_cpu_enabled: false
libbpf_stats_enabled: true libbpf_stats_enabled: true
plugins_metrics_enabled: true plugins_metrics_enabled: true
jemalloc_stats_enabled: false
convert_memory_to_mb: true convert_memory_to_mb: true
include_empty_values: false include_empty_values: false

View File

@ -575,6 +575,9 @@ const char config_schema_string[] = LONG_STRING_CONST(
}, },
"include_empty_values": { "include_empty_values": {
"type": "boolean" "type": "boolean"
},
"jemalloc_stats_enabled": {
"type": "boolean"
} }
}, },
"minProperties": 1, "minProperties": 1,

View File

@ -611,6 +611,9 @@ void falco_configuration::load_yaml(const std::string &config_name) {
if(m_config.get_scalar<bool>("metrics.plugins_metrics_enabled", true)) { if(m_config.get_scalar<bool>("metrics.plugins_metrics_enabled", true)) {
m_metrics_flags |= METRICS_V2_PLUGINS; m_metrics_flags |= METRICS_V2_PLUGINS;
} }
if(m_config.get_scalar<bool>("metrics.jemalloc_stats_enabled", true)) {
m_metrics_flags |= METRICS_V2_JEMALLOC_STATS;
}
m_metrics_convert_memory_to_mb = m_metrics_convert_memory_to_mb =
m_config.get_scalar<bool>("metrics.convert_memory_to_mb", true); m_config.get_scalar<bool>("metrics.convert_memory_to_mb", true);

View File

@ -37,6 +37,9 @@ limitations under the License.
#include "event_drops.h" #include "event_drops.h"
#include "falco_outputs.h" #include "falco_outputs.h"
// Falco only metric
#define METRICS_V2_JEMALLOC_STATS 1 << 31
enum class engine_kind_t : uint8_t { KMOD, EBPF, MODERN_EBPF, REPLAY, GVISOR, NODRIVER }; enum class engine_kind_t : uint8_t { KMOD, EBPF, MODERN_EBPF, REPLAY, GVISOR, NODRIVER };
// Map that holds { config filename | validation status } for each loaded config file. // Map that holds { config filename | validation status } for each loaded config file.

View File

@ -23,6 +23,10 @@ limitations under the License.
#include <libsinsp/sinsp.h> #include <libsinsp/sinsp.h>
#ifdef HAS_JEMALLOC
#include <jemalloc.h>
#endif
namespace fs = std::filesystem; namespace fs = std::filesystem;
/*! /*!
@ -249,6 +253,38 @@ std::string falco_metrics::to_text(const falco::app::state& state) {
} }
} }
} }
#ifdef HAS_JEMALLOC
if(state.config->m_metrics_flags & METRICS_V2_JEMALLOC_STATS) {
nlohmann::json j;
malloc_stats_print(
[](void* to, const char* from) {
nlohmann::json* j = (nlohmann::json*)to;
*j = nlohmann::json::parse(from);
},
&j,
"Jmdablxeg");
const auto& j_stats = j["jemalloc"]["stats"];
for(auto it = j_stats.begin(); it != j_stats.end(); ++it) {
if(it.value().is_number_unsigned()) {
std::uint64_t val = it.value().template get<std::uint64_t>();
std::string key = "jemalloc." + it.key();
auto metric = libs::metrics::libsinsp_metrics::new_metric(
key.c_str(),
METRICS_V2_JEMALLOC_STATS,
METRIC_VALUE_TYPE_U64,
METRIC_VALUE_UNIT_MEMORY_BYTES,
METRIC_VALUE_METRIC_TYPE_MONOTONIC,
val);
prometheus_metrics_converter.convert_metric_to_unit_convention(metric);
prometheus_text +=
prometheus_metrics_converter.convert_metric_to_text_prometheus(
metric,
"falcosecurity",
"falco");
}
}
}
#endif
} }
// Libs metrics categories // Libs metrics categories

View File

@ -32,6 +32,10 @@ limitations under the License.
#include <libscap/strl.h> #include <libscap/strl.h>
#include <libscap/scap_vtable.h> #include <libscap/scap_vtable.h>
#ifdef HAS_JEMALLOC
#include <jemalloc.h>
#endif
namespace fs = std::filesystem; namespace fs = std::filesystem;
// note: ticker_t is an uint16_t, which is enough because we don't care about // 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(
} }
} }
#ifdef HAS_JEMALLOC
if(m_writer->m_config->m_metrics_flags & METRICS_V2_JEMALLOC_STATS) {
nlohmann::json j;
malloc_stats_print(
[](void* to, const char* from) {
nlohmann::json* j = (nlohmann::json*)to;
*j = nlohmann::json::parse(from);
},
&j,
"Jmdablxeg");
const auto& j_stats = j["jemalloc"]["stats"];
for(auto it = j_stats.begin(); it != j_stats.end(); ++it) {
if(it.value().is_number_unsigned()) {
std::uint64_t val = it.value().template get<std::uint64_t>();
if(m_writer->m_config->m_metrics_include_empty_values || val != 0) {
std::string key = "falco.jemalloc." + it.key() + "_bytes";
auto metric = libs::metrics::libsinsp_metrics::new_metric(
key.c_str(),
METRICS_V2_JEMALLOC_STATS,
METRIC_VALUE_TYPE_U64,
METRIC_VALUE_UNIT_MEMORY_BYTES,
METRIC_VALUE_METRIC_TYPE_MONOTONIC,
val);
if(m_writer->m_config->m_metrics_convert_memory_to_mb &&
m_writer->m_output_rule_metrics_converter) {
m_writer->m_output_rule_metrics_converter
->convert_metric_to_unit_convention(metric);
output_fields[metric.name] = metric.value.d;
} else {
output_fields[metric.name] = metric.value.u64;
}
}
}
}
}
#endif
#if defined(__linux__) and !defined(MINIMAL_BUILD) and !defined(__EMSCRIPTEN__) #if defined(__linux__) and !defined(MINIMAL_BUILD) and !defined(__EMSCRIPTEN__)
if(m_writer->m_libs_metrics_collector && m_writer->m_output_rule_metrics_converter) { if(m_writer->m_libs_metrics_collector && m_writer->m_output_rule_metrics_converter) {
// Libs metrics categories // Libs metrics categories