From 77635116c62c79c680138155d2f2b9f08f8dbce0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D0=BE=D0=BD=D0=B8=D0=B4=20=D0=AE=D1=80=D1=8C?= =?UTF-8?q?=D0=B5=D0=B2=20=28Leonid=20Yuriev=29?= Date: Thu, 4 Aug 2022 14:28:35 +0300 Subject: [PATCH] mdbx: enable solib profiling with `-pg` and `gprof` with GLIBC >= 2.37. However such profiling requires https://sourceware.org/bugzilla/show_bug.cgi?id=29438 to be fixed. --- src/core.c | 9 +++++++-- src/internals.h | 10 ++++++++++ src/lck-posix.c | 20 ++++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/core.c b/src/core.c index ddb9de38..816dd55f 100644 --- a/src/core.c +++ b/src/core.c @@ -1459,7 +1459,8 @@ __cold void mdbx_rthc_global_init(void) { } bootid = mdbx_osal_bootid(); -#if 0 /* debug */ + +#if 0 /* debug */ for (unsigned i = 0; i < 65536; ++i) { size_t pages = pv2pages(i); unsigned x = pages2pv(pages); @@ -1469,7 +1470,7 @@ __cold void mdbx_rthc_global_init(void) { assert(pages == xp); } fflush(stdout); -#endif +#endif /* #if 0 */ } /* dtor called for thread, i.e. for all mdbx's environment objects */ @@ -1547,6 +1548,7 @@ __cold void mdbx_rthc_thread_dtor(void *rthc) { #endif } +MDBX_EXCLUDE_FOR_GPROF __cold void mdbx_rthc_global_dtor(void) { mdbx_trace(">> pid %d", mdbx_getpid()); @@ -23877,6 +23879,9 @@ __dll_export #endif /* MDBX_BUILD_TYPE */ , "MDBX_DEBUG=" MDBX_STRINGIFY(MDBX_DEBUG) +#ifdef ENABLE_GPROF + " ENABLE_GPROF" +#endif /* ENABLE_GPROF */ " MDBX_WORDBITS=" MDBX_STRINGIFY(MDBX_WORDBITS) " BYTE_ORDER=" #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ diff --git a/src/internals.h b/src/internals.h index cc2a578a..4d830abf 100644 --- a/src/internals.h +++ b/src/internals.h @@ -195,6 +195,16 @@ #endif #endif /* -Walignment-reduction-ignored */ +#ifndef MDBX_EXCLUDE_FOR_GPROF +#ifdef ENABLE_GPROF +#define MDBX_EXCLUDE_FOR_GPROF \ + __attribute__((__no_instrument_function__, \ + __no_profile_instrument_function__)) +#else +#define MDBX_EXCLUDE_FOR_GPROF +#endif /* ENABLE_GPROF */ +#endif /* MDBX_EXCLUDE_FOR_GPROF */ + #ifdef __cplusplus extern "C" { #endif diff --git a/src/lck-posix.c b/src/lck-posix.c index a1a465f8..d4c26e4c 100644 --- a/src/lck-posix.c +++ b/src/lck-posix.c @@ -32,6 +32,7 @@ uint32_t mdbx_linux_kernel_version; bool mdbx_RunningOnWSL1; #endif /* xMDBX_ALLOY */ +MDBX_EXCLUDE_FOR_GPROF __cold static uint8_t probe_for_WSL(const char *tag) { const char *const WSL = strstr(tag, "WSL"); if (WSL && WSL[3] >= '2' && WSL[3] <= '9') @@ -48,8 +49,22 @@ __cold static uint8_t probe_for_WSL(const char *tag) { #endif /* Linux */ +#ifdef ENABLE_GPROF +extern void _mcleanup(void); +extern void monstartup(unsigned long, unsigned long); +extern void _init(void); +extern void _fini(void); +extern void __gmon_start__(void) __attribute__((__weak__)); +#endif /* ENABLE_GPROF */ + +MDBX_EXCLUDE_FOR_GPROF __cold static __attribute__((__constructor__)) void mdbx_global_constructor(void) { +#ifdef ENABLE_GPROF + if (!&__gmon_start__) + monstartup((uintptr_t)&_init, (uintptr_t)&_fini); +#endif /* ENABLE_GPROF */ + #if defined(__linux__) || defined(__gnu_linux__) struct utsname buffer; if (uname(&buffer) == 0) { @@ -84,9 +99,14 @@ mdbx_global_constructor(void) { mdbx_rthc_global_init(); } +MDBX_EXCLUDE_FOR_GPROF __cold static __attribute__((__destructor__)) void mdbx_global_destructor(void) { mdbx_rthc_global_dtor(); +#ifdef ENABLE_GPROF + if (!&__gmon_start__) + _mcleanup(); +#endif /* ENABLE_GPROF */ } /*----------------------------------------------------------------------------*/