Skip to content

Commit

Permalink
Switch to using jemalloc
Browse files Browse the repository at this point in the history
  • Loading branch information
omerfirmak committed Feb 15, 2024
1 parent c61707f commit ccc75ac
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ARG VM_DEBUG

# Install Alpine Dependencies
RUN apt-get update && \
apt-get install build-essential cargo git golang upx-ucl -y
apt-get install build-essential cargo git golang upx-ucl libjemalloc-dev libjemalloc2 -y

WORKDIR /app

Expand All @@ -21,7 +21,7 @@ RUN upx-ucl /app/build/juno
# Stage 2: Build Docker image
FROM ubuntu:23.10 AS runtime

RUN apt-get update && apt-get install -y ca-certificates curl gawk grep
RUN apt-get update && apt-get install -y ca-certificates curl gawk grep libjemalloc-dev libjemalloc2

COPY --from=build /app/build/juno /usr/local/bin/

Expand Down
1 change: 1 addition & 0 deletions cmd/juno/juno.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
28 changes: 28 additions & 0 deletions jemalloc/jemalloc.go
Original file line number Diff line number Diff line change
@@ -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 <jemalloc/jemalloc.h>
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));
}
unsigned long long _get_jemalloc_active() {
size_t stat, stat_size;
stat = 0;
stat_size = sizeof(stat);
mallctl("stats.active", &stat, &stat_size, NULL, 0);
return (unsigned long long)stat;
}
*/
import "C"

func GetActive() C.ulonglong {
C._refresh_jemalloc_stats()
return C._get_jemalloc_active()
}
11 changes: 11 additions & 0 deletions node/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -301,3 +302,13 @@ func makePebbleMetrics(nodeDB db.DB) {
})
prometheus.MustRegister(blockCacheSize, blockHitRate, tableCacheSize, tableHitRate)
}

func makeJeMallocMetrics() {
active := prometheus.NewGaugeFunc(prometheus.GaugeOpts{
Namespace: "jemalloc",
Name: "active",
}, func() float64 {
return float64(jemalloc.GetActive())
})
prometheus.MustRegister(active)
}
1 change: 1 addition & 0 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ func New(cfg *Config, version string) (*Node, error) { //nolint:gocyclo,funlen
}
var metricsService service.Service
if cfg.Metrics {
makeJeMallocMetrics()
makePebbleMetrics(database)
chain.WithListener(makeBlockchainMetrics())
makeJunoMetrics(version)
Expand Down

0 comments on commit ccc75ac

Please sign in to comment.