--- mysql-5.6.6.orig/include/my_sys.h 2012-04-09 20:05:16.000000000 +0800 +++ mysql-5.6.6.thread-mem-usage/include/my_sys.h 2012-04-28 11:32:37.000000000 +0800 @@ -150,6 +150,19 @@ #define GETDATE_GMT 8 #define GETDATE_FIXEDLENGTH 16 +/* Thread Memory Usage By P.Linux */ +#if defined(__APPLE__) +#define _msize(p) malloc_size(p) +#elif defined(__sun) +#define _msize(p) (*((size_t*)(p)-1)) +#elif !defined(_WIN32) +#define _msize(p) malloc_usable_size(p) +#endif + +typedef void (*MALLOC_SIZE_CB) (long long size); +extern void set_malloc_size_cb(MALLOC_SIZE_CB func); +/* End */ + /* defines when allocating data */ extern void *my_malloc(size_t Size,myf MyFlags); extern void *my_multi_malloc(myf MyFlags, ...); --- mysql-5.6.6.orig/mysys/my_malloc.c 2012-04-09 20:05:16.000000000 +0800 +++ mysql-5.6.6.thread-mem-usage/mysys/my_malloc.c 2012-04-28 16:46:58.000000000 +0800 @@ -17,6 +17,22 @@ #include "mysys_err.h" #include +/* Thread Memory Usage By P.Linux */ +MALLOC_SIZE_CB malloc_size_cb_func= NULL; + +void static_upd_malloc_size(long long size) +{ + if (malloc_size_cb_func) + malloc_size_cb_func(size); +} + +void set_malloc_size_cb(MALLOC_SIZE_CB func) +{ + malloc_size_cb_func= func; +} +/* End */ + + /** Allocate a sized block of memory. @@ -36,9 +52,15 @@ size=1; point= malloc(size); + /* Thread Memory Usage By P.Linux */ + static_upd_malloc_size(0 + _msize(point)); + /* End */ DBUG_EXECUTE_IF("simulate_out_of_memory", { free(point); + /* Thread Memory Usage By P.Linux */ + static_upd_malloc_size(0 - _msize(point)); + /* End */ point= NULL; }); @@ -86,8 +108,17 @@ DBUG_RETURN(my_malloc(size, my_flags)); #ifdef USE_HALLOC point= malloc(size); + /* Thread Memory Usage By P.Linux */ + static_upd_malloc_size(0 + _msize(point)); + /* End */ #else + /* Thread Memory Usage By P.Linux */ + size_t old_size= _msize(oldpoint); + /* End */ point= realloc(oldpoint, size); + /* Thread Memory Usage By P.Linux */ + static_upd_malloc_size(_msize(point) - old_size); + /* End */ #endif #ifndef DBUG_OFF end: @@ -108,6 +139,9 @@ else { memcpy(point,oldpoint,size); + /* Thread Memory Usage By P.Linux */ + static_upd_malloc_size(0 - _msize(oldpoint)); + /* End */ free(oldpoint); } #endif @@ -127,6 +161,9 @@ { DBUG_ENTER("my_free"); DBUG_PRINT("my",("ptr: %p", ptr)); + /* Thread Memory Usage By P.Linux */ + static_upd_malloc_size(0 - _msize(ptr)); + /* End */ free(ptr); DBUG_VOID_RETURN; } --- mysql-5.6.6.orig/sql/sql_class.h 2012-04-09 20:05:16.000000000 +0800 +++ mysql-5.6.6.thread-mem-usage/sql/sql_class.h 2012-04-28 09:54:20.000000000 +0800 @@ -3954,6 +3954,10 @@ */ LEX_STRING invoker_user; LEX_STRING invoker_host; +public: + /* Thread Mem Usage By P.Linux */ + size_t malloc_size; + /* End*/ }; --- mysql-5.6.6.orig/sql/sql_class.cc 2012-04-09 20:05:16.000000000 +0800 +++ mysql-5.6.6.thread-mem-usage/sql/sql_class.cc 2012-04-27 18:37:16.000000000 +0800 @@ -930,6 +930,9 @@ m_binlog_invoker= FALSE; memset(&invoker_user, 0, sizeof(invoker_user)); memset(&invoker_host, 0, sizeof(invoker_host)); + /* Thread Memory Usage By P.Linux */ + malloc_size= 0; + /* End */ binlog_next_event_pos.file_name= NULL; binlog_next_event_pos.pos= 0; --- mysql-5.6.6.orig/sql/sql_show.cc 2012-04-09 20:05:16.000000000 +0800 +++ mysql-5.6.6.thread-mem-usage/sql/sql_show.cc 2012-04-26 23:37:35.000000000 +0800 @@ -2243,6 +2243,13 @@ } mysql_mutex_unlock(&tmp->LOCK_thd_data); + /* Thread Memory Usage By P.Linux */ + if (tmp->malloc_size>= 0) + { + table->field[8]->store((longlong) (tmp->malloc_size + sizeof(THD)), TRUE); + table->field[8]->set_notnull(); + } + /* End */ if (schema_table_store_record(thd, table)) { mysql_mutex_unlock(&LOCK_thread_count); @@ -7766,6 +7773,7 @@ {"STATE", 64, MYSQL_TYPE_STRING, 0, 1, "State", SKIP_OPEN_TABLE}, {"INFO", PROCESS_LIST_INFO_WIDTH, MYSQL_TYPE_STRING, 0, 1, "Info", SKIP_OPEN_TABLE}, + {"MEM_USAGE", 7, MYSQL_TYPE_LONG, 0, 0, "Mem_Usage", SKIP_OPEN_TABLE}, {0, 0, MYSQL_TYPE_STRING, 0, 0, 0, SKIP_OPEN_TABLE} }; --- mysql-5.6.6.orig/sql/mysqld.cc 2012-04-09 20:05:16.000000000 +0800 +++ mysql-5.6.6.thread-mem-usage/sql/mysqld.cc 2012-04-28 11:30:27.000000000 +0800 @@ -4741,6 +4741,18 @@ } #endif//DBUG_OFF + +/* Thread Mem Usage By P.Linux */ +extern "C" +void my_malloc_size_cb_func(long long size) +{ + THD *thd= current_thd; + if (thd) + thd->malloc_size+= size; +} +/* End */ + + #ifdef __WIN__ int win_main(int argc, char **argv) #else @@ -4775,6 +4787,10 @@ remaining_argc= argc; remaining_argv= argv; + /* Thread Memory Usage By P.Linux */ + set_malloc_size_cb(my_malloc_size_cb_func); + /* End */ + /* Must be initialized early for comparison of options name */ system_charset_info= &my_charset_utf8_general_ci;