From 8edef482a790cf684602e52800361567cb3d65a1 Mon Sep 17 00:00:00 2001 From: Monty Date: Sat, 9 Sep 2023 17:13:41 +0300 Subject: [PATCH] Changed some malloc() calls to my_malloc() - hostnames in hostname_cache added - Some Galera (WSREP) allocations - Table caches --- sql/hostname.cc | 5 +++-- sql/mysqld.cc | 3 ++- sql/mysqld.h | 1 + sql/table_cache.cc | 22 ++++++++++++++++++++-- sql/wsrep_binlog.cc | 12 ++++++------ sql/wsrep_mysqld.cc | 1 + sql/wsrep_sst.cc | 8 ++++---- sql/wsrep_utils.cc | 17 ++++++++++------- sql/wsrep_var.cc | 15 +++++++++++++-- sql/wsrep_var.h | 1 + 10 files changed, 61 insertions(+), 24 deletions(-) diff --git a/sql/hostname.cc b/sql/hostname.cc index e23560c7b8c..138254594c8 100644 --- a/sql/hostname.cc +++ b/sql/hostname.cc @@ -151,7 +151,7 @@ bool hostname_cache_init() if (!(hostname_cache= new Hash_filo(key_memory_host_cache_hostname, host_cache_size, key_offset, HOST_ENTRY_KEY_SIZE, - NULL, (my_hash_free_key) free, &my_charset_bin))) + NULL, (my_hash_free_key) my_free, &my_charset_bin))) return 1; hostname_cache->clear(); @@ -204,7 +204,8 @@ static void add_hostname_impl(const char *ip_key, const char *hostname, if (likely(entry == NULL)) { - entry= (Host_entry *) malloc(sizeof (Host_entry)); + entry= (Host_entry *) my_malloc(key_memory_host_cache_hostname, + sizeof (Host_entry), 0); if (entry == NULL) return; diff --git a/sql/mysqld.cc b/sql/mysqld.cc index c3c0183d983..c0dd56ab3d5 100644 --- a/sql/mysqld.cc +++ b/sql/mysqld.cc @@ -9304,8 +9304,8 @@ PSI_memory_key key_memory_thd_transactions; PSI_memory_key key_memory_user_conn; PSI_memory_key key_memory_user_var_entry; PSI_memory_key key_memory_user_var_entry_value; - PSI_memory_key key_memory_String_value; +PSI_memory_key key_memory_WSREP; #ifdef HAVE_PSI_INTERFACE @@ -9593,6 +9593,7 @@ static PSI_memory_info all_server_memory[]= // { &key_memory_get_all_tables, "get_all_tables", 0}, // { &key_memory_fill_schema_schemata, "fill_schema_schemata", 0}, { &key_memory_native_functions, "native_functions", PSI_FLAG_GLOBAL}, + { &key_memory_WSREP, "wsrep", 0 } }; /** diff --git a/sql/mysqld.h b/sql/mysqld.h index cab7dafdc19..fc8afa06638 100644 --- a/sql/mysqld.h +++ b/sql/mysqld.h @@ -537,6 +537,7 @@ extern PSI_memory_key key_memory_get_all_tables; extern PSI_memory_key key_memory_fill_schema_schemata; extern PSI_memory_key key_memory_native_functions; extern PSI_memory_key key_memory_JSON; +extern PSI_memory_key key_memory_WSREP; /* MAINTAINER: Please keep this list in order, to limit merge collisions. diff --git a/sql/table_cache.cc b/sql/table_cache.cc index 789d0c87e02..e454444032e 100644 --- a/sql/table_cache.cc +++ b/sql/table_cache.cc @@ -57,6 +57,7 @@ ulong tdc_size; /**< Table definition cache threshold for LRU eviction. */ ulong tc_size; /**< Table cache threshold for LRU eviction. */ uint32 tc_instances; +static size_t tc_allocated_size; static std::atomic tc_active_instances(1); static std::atomic tc_contention_warning_reported; @@ -148,8 +149,20 @@ struct Table_cache_instance } static void *operator new[](size_t size) - { return aligned_malloc(size, CPU_LEVEL1_DCACHE_LINESIZE); } + { + void *res= aligned_malloc(size, CPU_LEVEL1_DCACHE_LINESIZE); + if (res) + { + tc_allocated_size= size; + update_malloc_size(size, 0); + } + return res; + } static void operator delete[](void *ptr) { aligned_free(ptr); } + static void mark_memory_freed() + { + update_malloc_size(-(longlong) tc_allocated_size, 0); + } /** Lock table cache mutex and check contention. @@ -654,7 +667,12 @@ void tdc_deinit(void) tdc_inited= false; lf_hash_destroy(&tdc_hash); mysql_mutex_destroy(&LOCK_unused_shares); - delete [] tc; + if (tc) + { + tc->mark_memory_freed(); + delete [] tc; + tc= 0; + } } DBUG_VOID_RETURN; } diff --git a/sql/wsrep_binlog.cc b/sql/wsrep_binlog.cc index 5e1fa137fed..84392facba3 100644 --- a/sql/wsrep_binlog.cc +++ b/sql/wsrep_binlog.cc @@ -198,7 +198,7 @@ void wsrep_dump_rbr_buf(THD *thd, const void* rbr_buf, size_t buf_len) to alloc and pass as an argument to snprintf. */ - char *filename= (char *)malloc(len+1); + char *filename= (char *) my_malloc(key_memory_WSREP, len+1, 0); int len1= snprintf(filename, len+1, "%s/GRA_%lld_%lld.log", wsrep_data_home_dir, (longlong) thd->thread_id, (long long)wsrep_thd_trx_seqno(thd)); @@ -206,7 +206,7 @@ void wsrep_dump_rbr_buf(THD *thd, const void* rbr_buf, size_t buf_len) if (len > len1) { WSREP_ERROR("RBR dump path truncated: %d, skipping dump.", len); - free(filename); + my_free(filename); return; } @@ -225,7 +225,7 @@ void wsrep_dump_rbr_buf(THD *thd, const void* rbr_buf, size_t buf_len) WSREP_ERROR("Failed to open file '%s': %d (%s)", filename, errno, strerror(errno)); } - free(filename); + my_free(filename); } /* Dump replication buffer along with header to a file. */ @@ -248,7 +248,7 @@ void wsrep_dump_rbr_buf_with_header(THD *thd, const void *rbr_buf, to alloc and pass as an argument to snprintf. */ char *filename; - if (len < 0 || !(filename= (char*)malloc(len+1))) + if (len < 0 || !(filename= (char*) my_malloc(key_memory_WSREP, len+1, 0))) { WSREP_ERROR("snprintf error: %d, skipping dump.", len); DBUG_VOID_RETURN; @@ -261,7 +261,7 @@ void wsrep_dump_rbr_buf_with_header(THD *thd, const void *rbr_buf, if (len > len1) { WSREP_ERROR("RBR dump path truncated: %d, skipping dump.", len); - free(filename); + my_free(filename); DBUG_VOID_RETURN; } @@ -301,7 +301,7 @@ cleanup2: end_io_cache(&cache); cleanup1: - free(filename); + my_free(filename); mysql_file_close(file, MYF(MY_WME)); if (!thd->wsrep_applier) delete ev; diff --git a/sql/wsrep_mysqld.cc b/sql/wsrep_mysqld.cc index ae539668827..ae48a887a74 100644 --- a/sql/wsrep_mysqld.cc +++ b/sql/wsrep_mysqld.cc @@ -847,6 +847,7 @@ void wsrep_deinit_server() { wsrep_deinit_schema(); Wsrep_server_state::destroy(); + wsrep_free_status_vars(); } int wsrep_init() diff --git a/sql/wsrep_sst.cc b/sql/wsrep_sst.cc index 36629195d43..b56c5fc9de9 100644 --- a/sql/wsrep_sst.cc +++ b/sql/wsrep_sst.cc @@ -1656,9 +1656,9 @@ static int sst_flush_tables(THD* thd) const char base_name[]= "tables_flushed"; ssize_t const full_len= strlen(mysql_real_data_home) + strlen(base_name)+2; - char *real_name= (char*) malloc(full_len); + char *real_name= (char*) my_malloc(key_memory_WSREP, full_len, 0); sprintf(real_name, "%s/%s", mysql_real_data_home, base_name); - char *tmp_name= (char*) malloc(full_len + 4); + char *tmp_name= (char*) my_malloc(key_memory_WSREP, full_len + 4, 0); sprintf(tmp_name, "%s.tmp", real_name); FILE* file= fopen(tmp_name, "w+"); @@ -1686,8 +1686,8 @@ static int sst_flush_tables(THD* thd) tmp_name, real_name, err,strerror(err)); } } - free(real_name); - free(tmp_name); + my_free(real_name); + my_free(tmp_name); if (err) ha_disable_internal_writes(false); } diff --git a/sql/wsrep_utils.cc b/sql/wsrep_utils.cc index a679304c40a..ecb88eae597 100644 --- a/sql/wsrep_utils.cc +++ b/sql/wsrep_utils.cc @@ -96,14 +96,16 @@ namespace wsp bool env::ctor_common(char** e) { - env_= static_cast(malloc((len_ + 1) * sizeof(char*))); + env_= static_cast(my_malloc(key_memory_WSREP, + (len_ + 1) * sizeof(char*), + 0)); if (env_) { for (size_t i(0); i < len_; ++i) { assert(e[i]); // caller should make sure about len_ - env_[i]= strdup(e[i]); + env_[i]= my_strdup(key_memory_WSREP, e[i], MYF(0)); if (!env_[i]) { errno_= errno; @@ -129,8 +131,8 @@ env::dtor() if (env_) { /* don't need to go beyond the first NULL */ - for (size_t i(0); env_[i] != NULL; ++i) { free(env_[i]); } - free(env_); + for (size_t i(0); env_[i] != NULL; ++i) { my_free(env_[i]); } + my_free(env_); env_= NULL; } len_= 0; @@ -157,12 +159,13 @@ env::~env() { dtor(); } int env::append(const char* val) { - char** tmp= static_cast(realloc(env_, (len_ + 2)*sizeof(char*))); - + char** tmp= static_cast(my_realloc(key_memory_WSREP, + env_, (len_ + 2)*sizeof(char*), + 0)); if (tmp) { env_= tmp; - env_[len_]= strdup(val); + env_[len_]= my_strdup(key_memory_WSREP, val, 0); if (env_[len_]) { diff --git a/sql/wsrep_var.cc b/sql/wsrep_var.cc index 5ec32d63626..47af783b06d 100644 --- a/sql/wsrep_var.cc +++ b/sql/wsrep_var.cc @@ -1057,8 +1057,10 @@ static void export_wsrep_status_to_mysql(THD* thd) #if DYNAMIC if (wsrep_status_len != mysql_status_len) { - void* tmp= realloc (mysql_status_vars, - (wsrep_status_len + 1) * sizeof(SHOW_VAR)); + void* tmp= my_realloc(key_memory_WSREP, + mysql_status_vars, + (wsrep_status_len + 1) * sizeof(SHOW_VAR), + MYF(MY_ALLOW_ZERO_PTR)); if (!tmp) { sql_print_error ("Out of memory for wsrep status variables." @@ -1110,6 +1112,15 @@ void wsrep_free_status (THD* thd) thd->wsrep_status_vars.clear(); } +void wsrep_free_status_vars() +{ +#if DYNAMIC + my_free(mysql_status_vars); + mysql_status_vars= NULL; + mysql_status_len= 0; +#endif +} + bool wsrep_gtid_domain_id_update(sys_var* self, THD *thd, enum_var_type) { WSREP_DEBUG("wsrep_gtid_domain_id_update: %llu", diff --git a/sql/wsrep_var.h b/sql/wsrep_var.h index 7908e873795..f877c810621 100644 --- a/sql/wsrep_var.h +++ b/sql/wsrep_var.h @@ -37,6 +37,7 @@ class THD; int wsrep_init_vars(); void wsrep_set_wsrep_on(THD *thd); +void wsrep_free_status_vars(); #define CHECK_ARGS (sys_var *self, THD* thd, set_var *var) #define UPDATE_ARGS (sys_var *self, THD* thd, enum_var_type type)