Skip to content

Commit

Permalink
Changed some malloc() calls to my_malloc()
Browse files Browse the repository at this point in the history
- hostnames in hostname_cache added
- Some Galera (WSREP) allocations
- Table caches
  • Loading branch information
montywi committed Oct 3, 2023
1 parent c4a5bd1 commit 8edef48
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 24 deletions.
5 changes: 3 additions & 2 deletions sql/hostname.cc
Expand Up @@ -151,7 +151,7 @@ bool hostname_cache_init()

if (!(hostname_cache= new Hash_filo<Host_entry>(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();
Expand Down Expand Up @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion sql/mysqld.cc
Expand Up @@ -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

Expand Down Expand Up @@ -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 }
};

/**
Expand Down
1 change: 1 addition & 0 deletions sql/mysqld.h
Expand Up @@ -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.
Expand Down
22 changes: 20 additions & 2 deletions sql/table_cache.cc
Expand Up @@ -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<uint32_t> tc_active_instances(1);
static std::atomic<bool> tc_contention_warning_reported;

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand Down
12 changes: 6 additions & 6 deletions sql/wsrep_binlog.cc
Expand Up @@ -198,15 +198,15 @@ 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));

if (len > len1)
{
WSREP_ERROR("RBR dump path truncated: %d, skipping dump.", len);
free(filename);
my_free(filename);
return;
}

Expand All @@ -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. */
Expand All @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -301,7 +301,7 @@ void wsrep_dump_rbr_buf_with_header(THD *thd, const void *rbr_buf,
end_io_cache(&cache);

cleanup1:
free(filename);
my_free(filename);
mysql_file_close(file, MYF(MY_WME));

if (!thd->wsrep_applier) delete ev;
Expand Down
1 change: 1 addition & 0 deletions sql/wsrep_mysqld.cc
Expand Up @@ -847,6 +847,7 @@ void wsrep_deinit_server()
{
wsrep_deinit_schema();
Wsrep_server_state::destroy();
wsrep_free_status_vars();
}

int wsrep_init()
Expand Down
8 changes: 4 additions & 4 deletions sql/wsrep_sst.cc
Expand Up @@ -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+");
Expand Down Expand Up @@ -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);
}
Expand Down
17 changes: 10 additions & 7 deletions sql/wsrep_utils.cc
Expand Up @@ -96,14 +96,16 @@ namespace wsp
bool
env::ctor_common(char** e)
{
env_= static_cast<char**>(malloc((len_ + 1) * sizeof(char*)));
env_= static_cast<char**>(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;
Expand All @@ -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;
Expand All @@ -157,12 +159,13 @@ env::~env() { dtor(); }
int
env::append(const char* val)
{
char** tmp= static_cast<char**>(realloc(env_, (len_ + 2)*sizeof(char*)));

char** tmp= static_cast<char**>(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_])
{
Expand Down
15 changes: 13 additions & 2 deletions sql/wsrep_var.cc
Expand Up @@ -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."
Expand Down Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions sql/wsrep_var.h
Expand Up @@ -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)
Expand Down

0 comments on commit 8edef48

Please sign in to comment.