@@ -790,6 +790,10 @@ The documentation is based on the source files such as:
790790#include " sql/dd/upgrade_57/upgrade.h" // dd::upgrade_57::in_progress
791791#include " sql/srv_session.h"
792792
793+ #ifdef HAVE_JEMALLOC
794+ #include < jemalloc/jemalloc.h>
795+ #endif
796+
793797using std::max;
794798using std::min;
795799using std::vector;
@@ -4040,6 +4044,10 @@ SHOW_VAR com_status_vars[] = {
40404044 (char *)offsetof (System_status_var,
40414045 com_stat[(uint)SQLCOM_SHOW_MASTER_STAT]),
40424046 SHOW_LONG_STATUS, SHOW_SCOPE_ALL},
4047+ {" show_memory_status" ,
4048+ (char *)offsetof (System_status_var,
4049+ com_stat[(uint)SQLCOM_SHOW_MEMORY_STATUS]),
4050+ SHOW_LONG_STATUS, SHOW_SCOPE_ALL},
40434051 {" show_open_tables" ,
40444052 (char *)offsetof (System_status_var,
40454053 com_stat[(uint)SQLCOM_SHOW_OPEN_TABLES]),
@@ -7985,6 +7993,107 @@ struct my_option my_long_options[] = {
79857993
79867994 {0 , 0 , 0 , 0 , 0 , 0 , GET_NO_ARG, NO_ARG, 0 , 0 , 0 , 0 , 0 , 0 }};
79877995
7996+ #ifdef HAVE_JEMALLOC
7997+ #ifndef EMBEDDED_LIBRARY
7998+ std::atomic_bool need_update_malloc_status;
7999+
8000+ static void update_malloc_status () {
8001+ if (!need_update_malloc_status) return ;
8002+
8003+ uint64_t val = 1 ;
8004+ size_t len = sizeof (val);
8005+
8006+ mallctl (" epoch" , &val, &len, &val, sizeof (uint64_t ));
8007+ need_update_malloc_status = false ;
8008+ }
8009+
8010+ static int show_jemalloc_sizet (THD *thd MY_ATTRIBUTE ((unused)), SHOW_VAR *var,
8011+ char *buff, const char *stat_name) {
8012+ size_t len, val;
8013+ len = sizeof (val);
8014+
8015+ var->type = SHOW_LONGLONG;
8016+ var->value = buff;
8017+
8018+ if (!mallctl (stat_name, &val, &len, NULL , 0 )) {
8019+ *((ulonglong *)buff) = (ulonglong)val;
8020+ } else {
8021+ *((ulonglong *)buff) = 0 ;
8022+ }
8023+
8024+ /* Always return 0 to avoid worrying about error handling */
8025+ return 0 ;
8026+ }
8027+
8028+ static int show_jemalloc_unsigned (THD *thd MY_ATTRIBUTE ((unused)),
8029+ SHOW_VAR *var, char *buff,
8030+ const char *stat_name) {
8031+ unsigned val;
8032+ size_t len = sizeof (val);
8033+
8034+ var->type = SHOW_LONG;
8035+ var->value = buff;
8036+
8037+ if (!mallctl (stat_name, &val, &len, NULL , 0 )) {
8038+ *((ulong *)buff) = (ulong)val;
8039+ } else {
8040+ *((ulong *)buff) = 0 ;
8041+ }
8042+
8043+ /* Always return 0 to avoid worrying about error handling */
8044+ return 0 ;
8045+ }
8046+
8047+ static int show_jemalloc_bool (THD *thd MY_ATTRIBUTE ((unused)), SHOW_VAR *var,
8048+ char *buff, const char *stat_name) {
8049+ bool val;
8050+ size_t len = sizeof (val);
8051+
8052+ var->type = SHOW_LONG;
8053+ var->value = buff;
8054+
8055+ if (!mallctl (stat_name, &val, &len, NULL , 0 )) {
8056+ *((ulong *)buff) = (ulong)val;
8057+ } else {
8058+ *((ulong *)buff) = 0 ;
8059+ }
8060+
8061+ /* Always return 0 to avoid worrying about error handling */
8062+ return 0 ;
8063+ }
8064+
8065+ static int show_jemalloc_arenas_narenas (THD *thd, SHOW_VAR *var, char *buff) {
8066+ update_malloc_status ();
8067+ return show_jemalloc_unsigned (thd, var, buff, " arenas.narenas" );
8068+ }
8069+
8070+ static int show_jemalloc_opt_narenas (THD *thd, SHOW_VAR *var, char *buff) {
8071+ update_malloc_status ();
8072+ return show_jemalloc_sizet (thd, var, buff, " opt.narenas" );
8073+ }
8074+
8075+ static int show_jemalloc_opt_tcache (THD *thd, SHOW_VAR *var, char *buff) {
8076+ update_malloc_status ();
8077+ return show_jemalloc_bool (thd, var, buff, " opt.tcache" );
8078+ }
8079+
8080+ static int show_jemalloc_active (THD *thd, SHOW_VAR *var, char *buff) {
8081+ update_malloc_status ();
8082+ return show_jemalloc_sizet (thd, var, buff, " stats.active" );
8083+ }
8084+
8085+ static int show_jemalloc_allocated (THD *thd, SHOW_VAR *var, char *buff) {
8086+ update_malloc_status ();
8087+ return show_jemalloc_sizet (thd, var, buff, " stats.allocated" );
8088+ }
8089+
8090+ static int show_jemalloc_mapped (THD *thd, SHOW_VAR *var, char *buff) {
8091+ update_malloc_status ();
8092+ return show_jemalloc_sizet (thd, var, buff, " stats.mapped" );
8093+ }
8094+ #endif /* EMBEDDED_LIBRARY */
8095+ #endif /* HAVE_JEMALLOC */
8096+
79888097static int show_queries (THD *thd, SHOW_VAR *var, char *) {
79898098 var->type = SHOW_LONGLONG;
79908099 var->value = (char *)&thd->query_id ;
@@ -8507,6 +8616,20 @@ SHOW_VAR status_vars[] = {
85078616 SHOW_LONGLONG_STATUS, SHOW_SCOPE_ALL},
85088617 {" Handler_write" , (char *)offsetof (System_status_var, ha_write_count),
85098618 SHOW_LONGLONG_STATUS, SHOW_SCOPE_ALL},
8619+ #ifdef HAVE_JEMALLOC
8620+ {" Jemalloc_arenas_narenas" , (char *)&show_jemalloc_arenas_narenas,
8621+ SHOW_FUNC, SHOW_SCOPE_ALL},
8622+ {" Jemalloc_opt_narenas" , (char *)&show_jemalloc_opt_narenas, SHOW_FUNC,
8623+ SHOW_SCOPE_ALL},
8624+ {" Jemalloc_opt_tcache" , (char *)&show_jemalloc_opt_tcache, SHOW_FUNC,
8625+ SHOW_SCOPE_ALL},
8626+ {" Jemalloc_stats_active" , (char *)&show_jemalloc_active, SHOW_FUNC,
8627+ SHOW_SCOPE_ALL},
8628+ {" Jemalloc_stats_allocated" , (char *)&show_jemalloc_allocated, SHOW_FUNC,
8629+ SHOW_SCOPE_ALL},
8630+ {" Jemalloc_stats_mapped" , (char *)&show_jemalloc_mapped, SHOW_FUNC,
8631+ SHOW_SCOPE_ALL},
8632+ #endif
85108633 {" Key_blocks_not_flushed" ,
85118634 (char *)offsetof (KEY_CACHE, global_blocks_changed), SHOW_KEY_CACHE_LONG,
85128635 SHOW_SCOPE_GLOBAL},
0 commit comments