| 1 | /* Copyright (C) 2012-2017 Kentoku Shiba |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License as published by |
| 5 | the Free Software Foundation; version 2 of the License. |
| 6 | |
| 7 | This program is distributed in the hope that it will be useful, |
| 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | GNU General Public License for more details. |
| 11 | |
| 12 | You should have received a copy of the GNU General Public License |
| 13 | along with this program; if not, write to the Free Software |
| 14 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ |
| 15 | |
| 16 | #define MYSQL_SERVER 1 |
| 17 | #include <my_global.h> |
| 18 | #include "mysql_version.h" |
| 19 | #include "spd_environ.h" |
| 20 | #if MYSQL_VERSION_ID < 50500 |
| 21 | #include "mysql_priv.h" |
| 22 | #include <mysql/plugin.h> |
| 23 | #else |
| 24 | #include "sql_priv.h" |
| 25 | #include "probes_mysql.h" |
| 26 | #include "sql_class.h" |
| 27 | #include "sql_partition.h" |
| 28 | #include "sql_show.h" |
| 29 | #endif |
| 30 | #include "spd_db_include.h" |
| 31 | #include "spd_include.h" |
| 32 | #include "spd_table.h" |
| 33 | |
| 34 | extern pthread_mutex_t spider_mem_calc_mutex; |
| 35 | |
| 36 | extern const char *spider_alloc_func_name[SPIDER_MEM_CALC_LIST_NUM]; |
| 37 | extern const char *spider_alloc_file_name[SPIDER_MEM_CALC_LIST_NUM]; |
| 38 | extern ulong spider_alloc_line_no[SPIDER_MEM_CALC_LIST_NUM]; |
| 39 | extern ulonglong spider_total_alloc_mem[SPIDER_MEM_CALC_LIST_NUM]; |
| 40 | extern longlong spider_current_alloc_mem[SPIDER_MEM_CALC_LIST_NUM]; |
| 41 | extern ulonglong spider_alloc_mem_count[SPIDER_MEM_CALC_LIST_NUM]; |
| 42 | extern ulonglong spider_free_mem_count[SPIDER_MEM_CALC_LIST_NUM]; |
| 43 | |
| 44 | static struct st_mysql_storage_engine spider_i_s_info = |
| 45 | { MYSQL_INFORMATION_SCHEMA_INTERFACE_VERSION }; |
| 46 | |
| 47 | static ST_FIELD_INFO spider_i_s_alloc_mem_fields_info[] = |
| 48 | { |
| 49 | {"ID" , 10, MYSQL_TYPE_LONG, 0, MY_I_S_UNSIGNED, "id" , SKIP_OPEN_TABLE}, |
| 50 | {"FUNC_NAME" , 64, MYSQL_TYPE_STRING, 0, |
| 51 | MY_I_S_MAYBE_NULL, "func_name" , SKIP_OPEN_TABLE}, |
| 52 | {"FILE_NAME" , 64, MYSQL_TYPE_STRING, 0, |
| 53 | MY_I_S_MAYBE_NULL, "file_name" , SKIP_OPEN_TABLE}, |
| 54 | {"LINE_NO" , 10, MYSQL_TYPE_LONG, 0, |
| 55 | MY_I_S_UNSIGNED | MY_I_S_MAYBE_NULL, "line_no" , SKIP_OPEN_TABLE}, |
| 56 | {"TOTAL_ALLOC_MEM" , 20, MYSQL_TYPE_LONGLONG, 0, |
| 57 | MY_I_S_UNSIGNED | MY_I_S_MAYBE_NULL, "total_alloc_mem" , SKIP_OPEN_TABLE}, |
| 58 | {"CURRENT_ALLOC_MEM" , 20, MYSQL_TYPE_LONGLONG, 0, |
| 59 | MY_I_S_MAYBE_NULL, "current_alloc_mem" , SKIP_OPEN_TABLE}, |
| 60 | {"ALLOC_MEM_COUNT" , 20, MYSQL_TYPE_LONGLONG, 0, |
| 61 | MY_I_S_UNSIGNED | MY_I_S_MAYBE_NULL, "alloc_mem_count" , SKIP_OPEN_TABLE}, |
| 62 | {"FREE_MEM_COUNT" , 20, MYSQL_TYPE_LONGLONG, 0, |
| 63 | MY_I_S_UNSIGNED | MY_I_S_MAYBE_NULL, "free_mem_count" , SKIP_OPEN_TABLE}, |
| 64 | {NULL, 0, MYSQL_TYPE_STRING, 0, 0, NULL, 0} |
| 65 | }; |
| 66 | |
| 67 | static int spider_i_s_alloc_mem_fill_table( |
| 68 | THD *thd, |
| 69 | TABLE_LIST *tables, |
| 70 | COND *cond |
| 71 | ) { |
| 72 | uint roop_count; |
| 73 | TABLE *table = tables->table; |
| 74 | DBUG_ENTER("spider_i_s_alloc_mem_fill_table" ); |
| 75 | for (roop_count = 0; roop_count < SPIDER_MEM_CALC_LIST_NUM; roop_count++) |
| 76 | { |
| 77 | table->field[0]->store(roop_count, TRUE); |
| 78 | if (spider_alloc_func_name[roop_count]) |
| 79 | { |
| 80 | table->field[1]->set_notnull(); |
| 81 | table->field[2]->set_notnull(); |
| 82 | table->field[3]->set_notnull(); |
| 83 | table->field[4]->set_notnull(); |
| 84 | table->field[5]->set_notnull(); |
| 85 | table->field[6]->set_notnull(); |
| 86 | table->field[7]->set_notnull(); |
| 87 | table->field[1]->store(spider_alloc_func_name[roop_count], |
| 88 | strlen(spider_alloc_func_name[roop_count]), system_charset_info); |
| 89 | table->field[2]->store(spider_alloc_file_name[roop_count], |
| 90 | strlen(spider_alloc_file_name[roop_count]), system_charset_info); |
| 91 | table->field[3]->store(spider_alloc_line_no[roop_count], TRUE); |
| 92 | pthread_mutex_lock(&spider_mem_calc_mutex); |
| 93 | table->field[4]->store(spider_total_alloc_mem[roop_count], TRUE); |
| 94 | table->field[5]->store(spider_current_alloc_mem[roop_count], FALSE); |
| 95 | table->field[6]->store(spider_alloc_mem_count[roop_count], TRUE); |
| 96 | table->field[7]->store(spider_free_mem_count[roop_count], TRUE); |
| 97 | pthread_mutex_unlock(&spider_mem_calc_mutex); |
| 98 | } else { |
| 99 | table->field[1]->set_null(); |
| 100 | table->field[2]->set_null(); |
| 101 | table->field[3]->set_null(); |
| 102 | table->field[4]->set_null(); |
| 103 | table->field[5]->set_null(); |
| 104 | table->field[6]->set_null(); |
| 105 | table->field[7]->set_null(); |
| 106 | } |
| 107 | if (schema_table_store_record(thd, table)) |
| 108 | { |
| 109 | DBUG_RETURN(1); |
| 110 | } |
| 111 | } |
| 112 | DBUG_RETURN(0); |
| 113 | } |
| 114 | |
| 115 | static int spider_i_s_alloc_mem_init( |
| 116 | void *p |
| 117 | ) { |
| 118 | ST_SCHEMA_TABLE *schema = (ST_SCHEMA_TABLE *) p; |
| 119 | DBUG_ENTER("spider_i_s_alloc_mem_init" ); |
| 120 | schema->fields_info = spider_i_s_alloc_mem_fields_info; |
| 121 | schema->fill_table = spider_i_s_alloc_mem_fill_table; |
| 122 | schema->idx_field1 = 0; |
| 123 | DBUG_RETURN(0); |
| 124 | } |
| 125 | |
| 126 | static int spider_i_s_alloc_mem_deinit( |
| 127 | void *p |
| 128 | ) { |
| 129 | DBUG_ENTER("spider_i_s_alloc_mem_deinit" ); |
| 130 | DBUG_RETURN(0); |
| 131 | } |
| 132 | |
| 133 | struct st_mysql_plugin spider_i_s_alloc_mem = |
| 134 | { |
| 135 | MYSQL_INFORMATION_SCHEMA_PLUGIN, |
| 136 | &spider_i_s_info, |
| 137 | "SPIDER_ALLOC_MEM" , |
| 138 | "Kentoku Shiba" , |
| 139 | "Spider memory allocating viewer" , |
| 140 | PLUGIN_LICENSE_GPL, |
| 141 | spider_i_s_alloc_mem_init, |
| 142 | spider_i_s_alloc_mem_deinit, |
| 143 | 0x0001, |
| 144 | NULL, |
| 145 | NULL, |
| 146 | NULL, |
| 147 | #if MYSQL_VERSION_ID >= 50600 |
| 148 | 0, |
| 149 | #endif |
| 150 | }; |
| 151 | |
| 152 | #ifdef MARIADB_BASE_VERSION |
| 153 | struct st_maria_plugin spider_i_s_alloc_mem_maria = |
| 154 | { |
| 155 | MYSQL_INFORMATION_SCHEMA_PLUGIN, |
| 156 | &spider_i_s_info, |
| 157 | "SPIDER_ALLOC_MEM" , |
| 158 | "Kentoku Shiba" , |
| 159 | "Spider memory allocating viewer" , |
| 160 | PLUGIN_LICENSE_GPL, |
| 161 | spider_i_s_alloc_mem_init, |
| 162 | spider_i_s_alloc_mem_deinit, |
| 163 | 0x0100, |
| 164 | NULL, |
| 165 | NULL, |
| 166 | "1.0" , |
| 167 | MariaDB_PLUGIN_MATURITY_GAMMA, |
| 168 | }; |
| 169 | #endif |
| 170 | |