From 0a38bd435d5e3ba27b9492200d5fc066d83e9490 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Jaramago=20Fern=C3=A1ndez?= Date: Wed, 10 Apr 2024 15:55:39 +0200 Subject: [PATCH] Print jemalloc default config on startup Since jemalloc config can be changed via 'MALLOC_CONF' env variable, printing on startup ensures that we can check in the error log if the default config has been overridden. --- src/Makefile | 9 ++++++++- src/main.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/src/Makefile b/src/Makefile index 0d2e577cf..40480140b 100644 --- a/src/Makefile +++ b/src/Makefile @@ -149,11 +149,18 @@ ifeq ($(TEST_WITHASAN),1) WASAN += -DTEST_WITHASAN endif +NOJEMALLOC := $(shell echo $(NOJEMALLOC)) +ifeq ($(NOJEMALLOC),1) +NOJEM=-DNOJEM +else +NOJEM= +endif + MYCXXFLAGS := $(STDCPP) ifeq ($(CXX),clang++) MYCXXFLAGS += -fuse-ld=lld endif -MYCXXFLAGS += $(IDIRS) $(OPTZ) $(DEBUG) $(PSQLCH) -DGITVERSION=\"$(GIT_VERSION)\" $(WGCOV) $(WASAN) +MYCXXFLAGS += $(IDIRS) $(OPTZ) $(DEBUG) $(PSQLCH) -DGITVERSION=\"$(GIT_VERSION)\" $(NOJEM) $(WGCOV) $(WASAN) STATICMYLIBS := -Wl,-Bstatic -lconfig -lproxysql -ldaemon -lconfig++ -lre2 -lpcrecpp -lpcre -lmariadbclient -lhttpserver -lmicrohttpd -linjection -lcurl -lssl -lcrypto -lev diff --git a/src/main.cpp b/src/main.cpp index 700272f29..34f6f12e7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1902,7 +1902,61 @@ void handleProcessRestart() { } while (pid > 0); } +#ifndef NOJEM +int print_jemalloc_conf() { + int rc = 0; + + bool xmalloc = 0; + bool prof_accum = 0; + bool prof_leak = 0; + + size_t lg_cache_max = 0; + size_t lg_prof_sample = 0; + size_t lg_prof_interval = 0; + + size_t bool_sz = sizeof(bool); + size_t size_sz = sizeof(size_t); + size_t ssize_sz = sizeof(ssize_t); + + rc = mallctl("config.xmalloc", &xmalloc, &bool_sz, NULL, 0); + if (rc) { proxy_error("Failed to fetch 'config.xmalloc' with error %d", rc); return rc; } + + rc = mallctl("opt.lg_tcache_max", &lg_cache_max, &size_sz, NULL, 0); + if (rc) { proxy_error("Failed to fetch 'opt.lg_tcache_max' with error %d", rc); return rc; } + + rc = mallctl("opt.prof_accum", &prof_accum, &bool_sz, NULL, 0); + if (rc) { proxy_error("Failed to fetch 'opt.prof_accum' with error %d", rc); return rc; } + + rc = mallctl("opt.prof_leak", &prof_leak, &bool_sz, NULL, 0); + if (rc) { proxy_error("Failed to fetch 'opt.prof_leak' with error %d", rc); return rc; } + + rc = mallctl("opt.lg_prof_sample", &lg_prof_sample, &size_sz, NULL, 0); + if (rc) { proxy_error("Failed to fetch 'opt.lg_prof_sample' with error %d", rc); return rc; } + + rc = mallctl("opt.lg_prof_interval", &lg_prof_interval, &ssize_sz, NULL, 0); + if (rc) { proxy_error("Failed to fetch 'opt.lg_prof_interval' with error %d", rc); return rc; } + + proxy_info( + "Using jemalloc with MALLOC_CONF:" + " config.xmalloc:%d, lg_tcache_max:%lu, opt.prof_accum:%d, opt.prof_leak:%d," + " opt.lg_prof_sample:%lu, opt.lg_prof_interval:%lu, rc:%d\n", + xmalloc, lg_cache_max, prof_accum, prof_leak, lg_prof_sample, lg_prof_interval, rc + ); + + return 0; +} +#else +int print_jemalloc_conf() { + return 0; +} +#endif + int main(int argc, const char * argv[]) { + // Output current jemalloc conf; no action taken when disabled + { + int rc = print_jemalloc_conf(); + if (rc) { exit(EXIT_FAILURE); } + } { MYSQL *my = mysql_init(NULL);