From 4b252abc5c85239374c6e1d2e8164a345815e944 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Faruk=20IRMAK?= Date: Fri, 9 Feb 2024 15:23:13 +0300 Subject: [PATCH] Jemalloc --- cmd/juno/juno.go | 1 + jemalloc/jemalloc.go | 28 ++++++++++++++++++++++++++++ node/metrics.go | 11 +++++++++++ node/node.go | 1 + 4 files changed, 41 insertions(+) create mode 100644 jemalloc/jemalloc.go diff --git a/cmd/juno/juno.go b/cmd/juno/juno.go index 23db82c286..8d6377d8d9 100644 --- a/cmd/juno/juno.go +++ b/cmd/juno/juno.go @@ -13,6 +13,7 @@ import ( "syscall" "time" + _ "github.com/NethermindEth/juno/jemalloc" "github.com/NethermindEth/juno/node" "github.com/NethermindEth/juno/utils" "github.com/ethereum/go-ethereum/common" diff --git a/jemalloc/jemalloc.go b/jemalloc/jemalloc.go new file mode 100644 index 0000000000..bd67e2a2a4 --- /dev/null +++ b/jemalloc/jemalloc.go @@ -0,0 +1,28 @@ +package jemalloc + +/* +// This cgo directive is what actually causes jemalloc to be linked in to the +// final Go executable +#cgo pkg-config: jemalloc + +#include + +void _refresh_jemalloc_stats() { + // You just need to pass something not-null into the "epoch" mallctl. + size_t random_something = 1; + mallctl("epoch", NULL, NULL, &random_something, sizeof(random_something)); +} +int _get_jemalloc_active() { + size_t stat, stat_size; + stat = 0; + stat_size = sizeof(stat); + mallctl("stats.active", &stat, &stat_size, NULL, 0); + return (int)stat; +} +*/ +import "C" + +func GetActive() C.int { + C._refresh_jemalloc_stats() + return C._get_jemalloc_active() +} diff --git a/node/metrics.go b/node/metrics.go index c2ad598971..7ca81d231a 100644 --- a/node/metrics.go +++ b/node/metrics.go @@ -10,6 +10,7 @@ import ( "github.com/NethermindEth/juno/clients/gateway" "github.com/NethermindEth/juno/core" "github.com/NethermindEth/juno/db" + "github.com/NethermindEth/juno/jemalloc" "github.com/NethermindEth/juno/jsonrpc" "github.com/NethermindEth/juno/l1" "github.com/NethermindEth/juno/sync" @@ -261,3 +262,13 @@ func makeGatewayMetrics() gateway.EventListener { }, } } + +func makeJeMallocMetrics() { + active := prometheus.NewGaugeFunc(prometheus.GaugeOpts{ + Namespace: "jemalloc", + Name: "active", + }, func() float64 { + return float64(jemalloc.GetActive()) + }) + prometheus.MustRegister(active) +} diff --git a/node/node.go b/node/node.go index 035c8f2279..99d51c64ca 100644 --- a/node/node.go +++ b/node/node.go @@ -187,6 +187,7 @@ func New(cfg *Config, version string) (*Node, error) { //nolint:gocyclo,funlen client.WithListener(makeFeederMetrics()) gatewayClient.WithListener(makeGatewayMetrics()) metricsService = makeMetrics(cfg.MetricsHost, cfg.MetricsPort) + makeJeMallocMetrics() } if cfg.GRPC { services = append(services, makeGRPC(cfg.GRPCHost, cfg.GRPCPort, database, version))