Commit b4ff6456 authored by Monty's avatar Monty

Fixed wrong counting of global Memory_used

parent 7c6cb41b
...@@ -194,8 +194,10 @@ struct st_mysql_show_var { ...@@ -194,8 +194,10 @@ struct st_mysql_show_var {
enum enum_mysql_show_type type; enum enum_mysql_show_type type;
}; };
struct system_status_var;
#define SHOW_VAR_FUNC_BUFF_SIZE (256 * sizeof(void*)) #define SHOW_VAR_FUNC_BUFF_SIZE (256 * sizeof(void*))
typedef int (*mysql_show_var_func)(MYSQL_THD, struct st_mysql_show_var*, void *, enum enum_var_type); typedef int (*mysql_show_var_func)(MYSQL_THD, struct st_mysql_show_var*, void *, struct system_status_var *status_var, enum enum_var_type);
/* /*
......
...@@ -281,7 +281,8 @@ struct st_mysql_show_var { ...@@ -281,7 +281,8 @@ struct st_mysql_show_var {
void *value; void *value;
enum enum_mysql_show_type type; enum enum_mysql_show_type type;
}; };
typedef int (*mysql_show_var_func)(void*, struct st_mysql_show_var*, void *, enum enum_var_type); struct system_status_var;
typedef int (*mysql_show_var_func)(void*, struct st_mysql_show_var*, void *, struct system_status_var *status_var, enum enum_var_type);
struct st_mysql_sys_var; struct st_mysql_sys_var;
struct st_mysql_value; struct st_mysql_value;
typedef int (*mysql_var_check_func)(void* thd, typedef int (*mysql_var_check_func)(void* thd,
......
...@@ -281,7 +281,8 @@ struct st_mysql_show_var { ...@@ -281,7 +281,8 @@ struct st_mysql_show_var {
void *value; void *value;
enum enum_mysql_show_type type; enum enum_mysql_show_type type;
}; };
typedef int (*mysql_show_var_func)(void*, struct st_mysql_show_var*, void *, enum enum_var_type); struct system_status_var;
typedef int (*mysql_show_var_func)(void*, struct st_mysql_show_var*, void *, struct system_status_var *status_var, enum enum_var_type);
struct st_mysql_sys_var; struct st_mysql_sys_var;
struct st_mysql_value; struct st_mysql_value;
typedef int (*mysql_var_check_func)(void* thd, typedef int (*mysql_var_check_func)(void* thd,
......
...@@ -281,7 +281,8 @@ struct st_mysql_show_var { ...@@ -281,7 +281,8 @@ struct st_mysql_show_var {
void *value; void *value;
enum enum_mysql_show_type type; enum enum_mysql_show_type type;
}; };
typedef int (*mysql_show_var_func)(void*, struct st_mysql_show_var*, void *, enum enum_var_type); struct system_status_var;
typedef int (*mysql_show_var_func)(void*, struct st_mysql_show_var*, void *, struct system_status_var *status_var, enum enum_var_type);
struct st_mysql_sys_var; struct st_mysql_sys_var;
struct st_mysql_value; struct st_mysql_value;
typedef int (*mysql_var_check_func)(void* thd, typedef int (*mysql_var_check_func)(void* thd,
......
...@@ -281,7 +281,8 @@ struct st_mysql_show_var { ...@@ -281,7 +281,8 @@ struct st_mysql_show_var {
void *value; void *value;
enum enum_mysql_show_type type; enum enum_mysql_show_type type;
}; };
typedef int (*mysql_show_var_func)(void*, struct st_mysql_show_var*, void *, enum enum_var_type); struct system_status_var;
typedef int (*mysql_show_var_func)(void*, struct st_mysql_show_var*, void *, struct system_status_var *status_var, enum enum_var_type);
struct st_mysql_sys_var; struct st_mysql_sys_var;
struct st_mysql_value; struct st_mysql_value;
typedef int (*mysql_var_check_func)(void* thd, typedef int (*mysql_var_check_func)(void* thd,
......
...@@ -281,7 +281,8 @@ struct st_mysql_show_var { ...@@ -281,7 +281,8 @@ struct st_mysql_show_var {
void *value; void *value;
enum enum_mysql_show_type type; enum enum_mysql_show_type type;
}; };
typedef int (*mysql_show_var_func)(void*, struct st_mysql_show_var*, void *, enum enum_var_type); struct system_status_var;
typedef int (*mysql_show_var_func)(void*, struct st_mysql_show_var*, void *, struct system_status_var *status_var, enum enum_var_type);
struct st_mysql_sys_var; struct st_mysql_sys_var;
struct st_mysql_value; struct st_mysql_value;
typedef int (*mysql_var_check_func)(void* thd, typedef int (*mysql_var_check_func)(void* thd,
......
...@@ -516,6 +516,9 @@ int init_embedded_server(int argc, char **argv, char **groups) ...@@ -516,6 +516,9 @@ int init_embedded_server(int argc, char **argv, char **groups)
if (my_thread_init()) if (my_thread_init())
return 1; return 1;
if (init_early_variables())
return 1;
if (argc) if (argc)
{ {
argcp= &argc; argcp= &argc;
......
...@@ -3997,35 +3997,31 @@ extern "C" { ...@@ -3997,35 +3997,31 @@ extern "C" {
static void my_malloc_size_cb_func(long long size, my_bool is_thread_specific) static void my_malloc_size_cb_func(long long size, my_bool is_thread_specific)
{ {
THD *thd= current_thd; THD *thd= current_thd;
/* If thread specific memory */
if (likely(is_thread_specific)) if (likely(is_thread_specific)) /* If thread specific memory */
{
if (mysqld_server_initialized || thd)
{ {
/* /*
THD may not be set if we are called from my_net_init() before THD When thread specfic is set, both mysqld_server_initialized and thd
thread has started. must be set
However, this should never happen, so better to assert and
fix this.
*/ */
DBUG_ASSERT(thd); DBUG_ASSERT(mysqld_server_initialized && thd);
if (thd)
{ DBUG_PRINT("info", ("thd memory_used: %lld size: %lld",
DBUG_PRINT("info", ("memory_used: %lld size: %lld",
(longlong) thd->status_var.local_memory_used, (longlong) thd->status_var.local_memory_used,
size)); size));
thd->status_var.local_memory_used+= size; thd->status_var.local_memory_used+= size;
DBUG_ASSERT((longlong) thd->status_var.local_memory_used >= 0); DBUG_ASSERT((longlong) thd->status_var.local_memory_used >= 0);
} }
}
}
else if (likely(thd)) else if (likely(thd))
{
DBUG_PRINT("info", ("global thd memory_used: %lld size: %lld",
(longlong) thd->status_var.global_memory_used,
size));
thd->status_var.global_memory_used+= size; thd->status_var.global_memory_used+= size;
}
else else
{ {
// workaround for gcc 4.2.4-1ubuntu4 -fPIE (from DEB_BUILD_HARDENING=1) update_global_memory_status(size);
int64 volatile * volatile ptr=&global_status_var.global_memory_used;
my_atomic_add64_explicit(ptr, size, MY_MEMORY_ORDER_RELAXED);
} }
} }
} }
...@@ -4066,6 +4062,22 @@ rpl_make_log_name(const char *opt, ...@@ -4066,6 +4062,22 @@ rpl_make_log_name(const char *opt,
DBUG_RETURN(NULL); DBUG_RETURN(NULL);
} }
/* We have to setup my_malloc_size_cb_func early to catch all mallocs */
static int init_early_variables()
{
if (pthread_key_create(&THR_THD, NULL))
{
fprintf(stderr, "Fatal error: Can't create thread-keys\n");
return 1;
}
set_current_thd(0);
set_malloc_size_cb(my_malloc_size_cb_func);
global_status_var.global_memory_used= 0;
return 0;
}
static int init_common_variables() static int init_common_variables()
{ {
umask(((~my_umask) & 0666)); umask(((~my_umask) & 0666));
...@@ -4077,16 +4089,12 @@ static int init_common_variables() ...@@ -4077,16 +4089,12 @@ static int init_common_variables()
connection_errors_peer_addr= 0; connection_errors_peer_addr= 0;
my_decimal_set_zero(&decimal_zero); // set decimal_zero constant; my_decimal_set_zero(&decimal_zero); // set decimal_zero constant;
if (pthread_key_create(&THR_THD,NULL) || if (pthread_key_create(&THR_MALLOC,NULL))
pthread_key_create(&THR_MALLOC,NULL))
{ {
sql_print_error("Can't create thread-keys"); sql_print_error("Can't create thread-keys");
return 1; return 1;
} }
set_current_thd(0);
set_malloc_size_cb(my_malloc_size_cb_func);
init_libstrings(); init_libstrings();
tzset(); // Set tzname tzset(); // Set tzname
...@@ -5184,6 +5192,9 @@ static int init_server_components() ...@@ -5184,6 +5192,9 @@ static int init_server_components()
init_global_table_stats(); init_global_table_stats();
init_global_index_stats(); init_global_index_stats();
/* It's now safe to use thread specific memory */
mysqld_server_initialized= 1;
/* Allow storage engine to give real error messages */ /* Allow storage engine to give real error messages */
if (ha_init_errors()) if (ha_init_errors())
DBUG_RETURN(1); DBUG_RETURN(1);
...@@ -5544,6 +5555,9 @@ int mysqld_main(int argc, char **argv) ...@@ -5544,6 +5555,9 @@ int mysqld_main(int argc, char **argv)
sf_leaking_memory= 1; // no safemalloc memory leak reports if we exit early sf_leaking_memory= 1; // no safemalloc memory leak reports if we exit early
mysqld_server_started= mysqld_server_initialized= 0; mysqld_server_started= mysqld_server_initialized= 0;
if (init_early_variables())
exit(1);
#ifdef HAVE_NPTL #ifdef HAVE_NPTL
ld_assume_kernel_is_set= (getenv("LD_ASSUME_KERNEL") != 0); ld_assume_kernel_is_set= (getenv("LD_ASSUME_KERNEL") != 0);
#endif #endif
...@@ -5846,9 +5860,6 @@ int mysqld_main(int argc, char **argv) ...@@ -5846,9 +5860,6 @@ int mysqld_main(int argc, char **argv)
if (Events::init((THD*) 0, opt_noacl || opt_bootstrap)) if (Events::init((THD*) 0, opt_noacl || opt_bootstrap))
unireg_abort(1); unireg_abort(1);
/* It's now safe to use thread specific memory */
mysqld_server_initialized= 1;
if (WSREP_ON) if (WSREP_ON)
{ {
if (opt_bootstrap) if (opt_bootstrap)
...@@ -8260,15 +8271,16 @@ static int show_default_keycache(THD *thd, SHOW_VAR *var, char *buff, ...@@ -8260,15 +8271,16 @@ static int show_default_keycache(THD *thd, SHOW_VAR *var, char *buff,
static int show_memory_used(THD *thd, SHOW_VAR *var, char *buff, static int show_memory_used(THD *thd, SHOW_VAR *var, char *buff,
struct system_status_var *status_var,
enum enum_var_type scope) enum enum_var_type scope)
{ {
var->type= SHOW_LONGLONG; var->type= SHOW_LONGLONG;
var->value= buff; var->value= buff;
if (scope == OPT_GLOBAL) if (scope == OPT_GLOBAL)
*(longlong*) buff= (global_status_var.local_memory_used + *(longlong*) buff= (status_var->global_memory_used +
global_status_var.global_memory_used); status_var->local_memory_used);
else else
*(longlong*) buff= thd->status_var.local_memory_used; *(longlong*) buff= status_var->local_memory_used;
return 0; return 0;
} }
...@@ -8697,7 +8709,9 @@ static int mysql_init_variables(void) ...@@ -8697,7 +8709,9 @@ static int mysql_init_variables(void)
prepared_stmt_count= 0; prepared_stmt_count= 0;
mysqld_unix_port= opt_mysql_tmpdir= my_bind_addr_str= NullS; mysqld_unix_port= opt_mysql_tmpdir= my_bind_addr_str= NullS;
bzero((uchar*) &mysql_tmpdir_list, sizeof(mysql_tmpdir_list)); bzero((uchar*) &mysql_tmpdir_list, sizeof(mysql_tmpdir_list));
bzero((char *) &global_status_var, sizeof(global_status_var)); /* Clear all except global_memory_used */
bzero((char*) &global_status_var, offsetof(STATUS_VAR,
last_cleared_system_status_var));
opt_large_pages= 0; opt_large_pages= 0;
opt_super_large_pages= 0; opt_super_large_pages= 0;
#if defined(ENABLED_DEBUG_SYNC) #if defined(ENABLED_DEBUG_SYNC)
...@@ -9931,6 +9945,7 @@ void refresh_status(THD *thd) ...@@ -9931,6 +9945,7 @@ void refresh_status(THD *thd)
/* Reset thread's status variables */ /* Reset thread's status variables */
thd->set_status_var_init(); thd->set_status_var_init();
thd->status_var.global_memory_used= 0;
bzero((uchar*) &thd->org_status_var, sizeof(thd->org_status_var)); bzero((uchar*) &thd->org_status_var, sizeof(thd->org_status_var));
thd->start_bytes_received= 0; thd->start_bytes_received= 0;
......
...@@ -914,7 +914,8 @@ THD::THD(bool is_wsrep_applier) ...@@ -914,7 +914,8 @@ THD::THD(bool is_wsrep_applier)
*/ */
THD *old_THR_THD= current_thd; THD *old_THR_THD= current_thd;
set_current_thd(this); set_current_thd(this);
status_var.local_memory_used= status_var.global_memory_used= 0; status_var.local_memory_used= sizeof(THD);
status_var.global_memory_used= 0;
main_da.init(); main_da.init();
/* /*
...@@ -1636,6 +1637,8 @@ THD::~THD() ...@@ -1636,6 +1637,8 @@ THD::~THD()
that memory allocation counting is done correctly that memory allocation counting is done correctly
*/ */
set_current_thd(this); set_current_thd(this);
if (!status_in_global)
add_status_to_global();
/* Ensure that no one is using THD */ /* Ensure that no one is using THD */
mysql_mutex_lock(&LOCK_thd_data); mysql_mutex_lock(&LOCK_thd_data);
...@@ -1698,13 +1701,14 @@ THD::~THD() ...@@ -1698,13 +1701,14 @@ THD::~THD()
if (xid_hash_pins) if (xid_hash_pins)
lf_hash_put_pins(xid_hash_pins); lf_hash_put_pins(xid_hash_pins);
/* Ensure everything is freed */ /* Ensure everything is freed */
status_var.local_memory_used-= sizeof(THD);
if (status_var.local_memory_used != 0) if (status_var.local_memory_used != 0)
{ {
DBUG_PRINT("error", ("memory_used: %lld", status_var.local_memory_used)); DBUG_PRINT("error", ("memory_used: %lld", status_var.local_memory_used));
SAFEMALLOC_REPORT_MEMORY(my_thread_dbug_id()); SAFEMALLOC_REPORT_MEMORY(my_thread_dbug_id());
DBUG_ASSERT(status_var.local_memory_used == 0); DBUG_ASSERT(status_var.local_memory_used == 0);
} }
update_global_memory_status(status_var.global_memory_used);
set_current_thd(orig_thd == this ? 0 : orig_thd); set_current_thd(orig_thd == this ? 0 : orig_thd);
DBUG_VOID_RETURN; DBUG_VOID_RETURN;
} }
...@@ -1723,6 +1727,7 @@ THD::~THD() ...@@ -1723,6 +1727,7 @@ THD::~THD()
other types are handled explicitely other types are handled explicitely
*/ */
void add_to_status(STATUS_VAR *to_var, STATUS_VAR *from_var) void add_to_status(STATUS_VAR *to_var, STATUS_VAR *from_var)
{ {
ulong *end= (ulong*) ((uchar*) to_var + ulong *end= (ulong*) ((uchar*) to_var +
...@@ -1742,12 +1747,17 @@ void add_to_status(STATUS_VAR *to_var, STATUS_VAR *from_var) ...@@ -1742,12 +1747,17 @@ void add_to_status(STATUS_VAR *to_var, STATUS_VAR *from_var)
to_var->binlog_bytes_written+= from_var->binlog_bytes_written; to_var->binlog_bytes_written+= from_var->binlog_bytes_written;
to_var->cpu_time+= from_var->cpu_time; to_var->cpu_time+= from_var->cpu_time;
to_var->busy_time+= from_var->busy_time; to_var->busy_time+= from_var->busy_time;
to_var->local_memory_used+= from_var->local_memory_used;
/* /*
Update global_memory_used. We have to do this with atomic_add as the Update global_memory_used. We have to do this with atomic_add as the
global value can change outside of LOCK_status. global value can change outside of LOCK_status.
*/ */
if (to_var == &global_status_var)
{
DBUG_PRINT("info", ("global memory_used: %lld size: %lld",
(longlong) global_status_var.global_memory_used,
(longlong) from_var->global_memory_used));
}
// workaround for gcc 4.2.4-1ubuntu4 -fPIE (from DEB_BUILD_HARDENING=1) // workaround for gcc 4.2.4-1ubuntu4 -fPIE (from DEB_BUILD_HARDENING=1)
int64 volatile * volatile ptr= &to_var->global_memory_used; int64 volatile * volatile ptr= &to_var->global_memory_used;
my_atomic_add64_explicit(ptr, from_var->global_memory_used, my_atomic_add64_explicit(ptr, from_var->global_memory_used,
......
...@@ -822,6 +822,20 @@ void add_to_status(STATUS_VAR *to_var, STATUS_VAR *from_var); ...@@ -822,6 +822,20 @@ void add_to_status(STATUS_VAR *to_var, STATUS_VAR *from_var);
void add_diff_to_status(STATUS_VAR *to_var, STATUS_VAR *from_var, void add_diff_to_status(STATUS_VAR *to_var, STATUS_VAR *from_var,
STATUS_VAR *dec_var); STATUS_VAR *dec_var);
/*
Update global_memory_used. We have to do this with atomic_add as the
global value can change outside of LOCK_status.
*/
inline void update_global_memory_status(int64 size)
{
DBUG_PRINT("info", ("global memory_used: %lld size: %lld",
(longlong) global_status_var.global_memory_used,
size));
// workaround for gcc 4.2.4-1ubuntu4 -fPIE (from DEB_BUILD_HARDENING=1)
int64 volatile * volatile ptr= &global_status_var.global_memory_used;
my_atomic_add64_explicit(ptr, size, MY_MEMORY_ORDER_RELAXED);
}
/** /**
Get collation by name, send error to client on failure. Get collation by name, send error to client on failure.
@param name Collation name @param name Collation name
...@@ -3836,9 +3850,11 @@ class THD :public Statement, ...@@ -3836,9 +3850,11 @@ class THD :public Statement,
void add_status_to_global() void add_status_to_global()
{ {
DBUG_ASSERT(status_in_global == 0);
mysql_mutex_lock(&LOCK_status); mysql_mutex_lock(&LOCK_status);
add_to_status(&global_status_var, &status_var); add_to_status(&global_status_var, &status_var);
/* Mark that this THD status has already been added in global status */ /* Mark that this THD status has already been added in global status */
status_var.global_memory_used= 0;
status_in_global= 1; status_in_global= 1;
mysql_mutex_unlock(&LOCK_status); mysql_mutex_unlock(&LOCK_status);
} }
......
...@@ -2961,8 +2961,7 @@ int fill_schema_processlist(THD* thd, TABLE_LIST* tables, COND* cond) ...@@ -2961,8 +2961,7 @@ int fill_schema_processlist(THD* thd, TABLE_LIST* tables, COND* cond)
thread in this thread. However it's better that we notice it eventually thread in this thread. However it's better that we notice it eventually
than hide it. than hide it.
*/ */
table->field[12]->store((longlong) (tmp->status_var.local_memory_used + table->field[12]->store((longlong) tmp->status_var.local_memory_used,
sizeof(THD)),
FALSE); FALSE);
table->field[12]->set_notnull(); table->field[12]->set_notnull();
table->field[13]->store((longlong) tmp->get_examined_row_count(), TRUE); table->field[13]->store((longlong) tmp->get_examined_row_count(), TRUE);
...@@ -3258,7 +3257,8 @@ static bool show_status_array(THD *thd, const char *wild, ...@@ -3258,7 +3257,8 @@ static bool show_status_array(THD *thd, const char *wild,
*/ */
for (var=variables; var->type == SHOW_FUNC || for (var=variables; var->type == SHOW_FUNC ||
var->type == SHOW_SIMPLE_FUNC; var= &tmp) var->type == SHOW_SIMPLE_FUNC; var= &tmp)
((mysql_show_var_func)(var->value))(thd, &tmp, buff, scope); ((mysql_show_var_func)(var->value))(thd, &tmp, buff,
status_var, scope);
SHOW_TYPE show_type=var->type; SHOW_TYPE show_type=var->type;
if (show_type == SHOW_ARRAY) if (show_type == SHOW_ARRAY)
...@@ -3390,10 +3390,14 @@ static bool show_status_array(THD *thd, const char *wild, ...@@ -3390,10 +3390,14 @@ static bool show_status_array(THD *thd, const char *wild,
DBUG_RETURN(res); DBUG_RETURN(res);
} }
/* collect status for all running threads */ /*
collect status for all running threads
Return number of threads used
*/
void calc_sum_of_all_status(STATUS_VAR *to) uint calc_sum_of_all_status(STATUS_VAR *to)
{ {
uint count= 0;
DBUG_ENTER("calc_sum_of_all_status"); DBUG_ENTER("calc_sum_of_all_status");
/* Ensure that thread id not killed during loop */ /* Ensure that thread id not killed during loop */
...@@ -3404,16 +3408,21 @@ void calc_sum_of_all_status(STATUS_VAR *to) ...@@ -3404,16 +3408,21 @@ void calc_sum_of_all_status(STATUS_VAR *to)
/* Get global values as base */ /* Get global values as base */
*to= global_status_var; *to= global_status_var;
to->local_memory_used= 0;
/* Add to this status from existing threads */ /* Add to this status from existing threads */
while ((tmp= it++)) while ((tmp= it++))
{ {
count++;
if (!tmp->status_in_global) if (!tmp->status_in_global)
{
add_to_status(to, &tmp->status_var); add_to_status(to, &tmp->status_var);
to->local_memory_used+= tmp->status_var.local_memory_used;
}
} }
mysql_mutex_unlock(&LOCK_thread_count); mysql_mutex_unlock(&LOCK_thread_count);
DBUG_VOID_RETURN; DBUG_RETURN(count);
} }
......
...@@ -102,7 +102,7 @@ bool mysqld_show_authors(THD *thd); ...@@ -102,7 +102,7 @@ bool mysqld_show_authors(THD *thd);
bool mysqld_show_contributors(THD *thd); bool mysqld_show_contributors(THD *thd);
bool mysqld_show_privileges(THD *thd); bool mysqld_show_privileges(THD *thd);
char *make_backup_log_name(char *buff, const char *name, const char* log_ext); char *make_backup_log_name(char *buff, const char *name, const char* log_ext);
void calc_sum_of_all_status(STATUS_VAR *to); uint calc_sum_of_all_status(STATUS_VAR *to);
void append_definer(THD *thd, String *buffer, const LEX_STRING *definer_user, void append_definer(THD *thd, String *buffer, const LEX_STRING *definer_user,
const LEX_STRING *definer_host); const LEX_STRING *definer_host);
int add_status_vars(SHOW_VAR *list); int add_status_vars(SHOW_VAR *list);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment