1 /* Copyright (c) 2015, 2021, Oracle and/or its affiliates.
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, version 2.0,
5   as published by the Free Software Foundation.
6 
7   This program is also distributed with certain software (including
8   but not limited to OpenSSL) that is licensed under separate terms,
9   as designated in a particular file or component or in included license
10   documentation.  The authors of MySQL hereby grant you an additional
11   permission to link the program and your derivative works with the
12   separately licensed software that they have included with MySQL.
13 
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License, version 2.0, for more details.
18 
19   You should have received a copy of the GNU General Public License
20   along with this program; if not, write to the Free Software
21   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 /**
24   @file storage/perfschema/table_session_status.cc
25   Table SESSION_STATUS (implementation).
26 */
27 
28 #include "my_global.h"
29 #include "my_thread.h"
30 #include "table_session_status.h"
31 #include "pfs_instr_class.h"
32 #include "pfs_column_types.h"
33 #include "pfs_column_values.h"
34 #include "pfs_global.h"
35 
36 THR_LOCK table_session_status::m_table_lock;
37 
38 PFS_engine_table_share
39 table_session_status::m_share=
40 {
41   { C_STRING_WITH_LEN("session_status") },
42   &pfs_readonly_world_acl,
43   table_session_status::create,
44   NULL, /* write_row */
45   NULL, /* delete_all_rows */
46   table_session_status::get_row_count,
47   sizeof(pos_t),
48   &m_table_lock,
49   { C_STRING_WITH_LEN("CREATE TABLE session_status("
50   "VARIABLE_NAME VARCHAR(64) not null comment 'The session status variable name.',"
51   "VARIABLE_VALUE VARCHAR(1024) comment 'The session status variable value.')") },
52   true   /* perpetual */
53 };
54 
55 PFS_engine_table*
create(void)56 table_session_status::create(void)
57 {
58   return new table_session_status();
59 }
60 
get_row_count(void)61 ha_rows table_session_status::get_row_count(void)
62 {
63   mysql_mutex_lock(&LOCK_status);
64   ha_rows status_var_count= all_status_vars.elements;
65   mysql_mutex_unlock(&LOCK_status);
66   return status_var_count;
67 }
68 
table_session_status()69 table_session_status::table_session_status()
70   : PFS_engine_table(&m_share, &m_pos),
71     m_status_cache(false), m_row_exists(false), m_pos(0), m_next_pos(0)
72 {}
73 
reset_position(void)74 void table_session_status::reset_position(void)
75 {
76   m_pos.m_index = 0;
77   m_next_pos.m_index = 0;
78 }
79 
rnd_init(bool scan)80 int table_session_status::rnd_init(bool scan)
81 {
82  /* Build a cache of all status variables for this thread. */
83   m_status_cache.materialize_all(current_thd);
84 
85   /* Record the current number of status variables to detect subsequent changes. */
86   ulonglong status_version= m_status_cache.get_status_array_version();
87 
88   /*
89     The table context holds the current version of the global status array.
90     If scan == true, then allocate a new context from mem_root and store in TLS.
91     If scan == false, then restore from TLS.
92   */
93   m_context= (table_session_status_context *)current_thd->alloc(sizeof(table_session_status_context));
94   new(m_context) table_session_status_context(status_version, !scan);
95   return 0;
96 }
97 
rnd_next(void)98 int table_session_status::rnd_next(void)
99 {
100   for (m_pos.set_at(&m_next_pos);
101        m_pos.m_index < m_status_cache.size();
102        m_pos.next())
103   {
104     if (m_status_cache.is_materialized())
105     {
106       const Status_variable *stat_var= m_status_cache.get(m_pos.m_index);
107       if (stat_var != NULL)
108       {
109         make_row(stat_var);
110         m_next_pos.set_after(&m_pos);
111         return 0;
112       }
113     }
114   }
115   return HA_ERR_END_OF_FILE;
116 }
117 
118 int
rnd_pos(const void * pos)119 table_session_status::rnd_pos(const void *pos)
120 {
121   /* If global status array has changed, do nothing. */ // TODO: warning
122   if (!m_context->versions_match())
123     return HA_ERR_RECORD_DELETED;
124 
125   set_position(pos);
126   assert(m_pos.m_index < m_status_cache.size());
127 
128   if (m_status_cache.is_materialized())
129   {
130     const Status_variable *stat_var= m_status_cache.get(m_pos.m_index);
131     if (stat_var != NULL)
132     {
133       make_row(stat_var);
134       return 0;
135     }
136   }
137 
138   return HA_ERR_RECORD_DELETED;
139 }
140 
141 void table_session_status
make_row(const Status_variable * status_var)142 ::make_row(const Status_variable *status_var)
143 {
144   m_row_exists= false;
145   m_row.m_variable_name.make_row(status_var->m_name, status_var->m_name_length);
146   m_row.m_variable_value.make_row(status_var);
147   m_row_exists= true;
148 }
149 
150 int table_session_status
read_row_values(TABLE * table,unsigned char * buf,Field ** fields,bool read_all)151 ::read_row_values(TABLE *table,
152                   unsigned char *buf,
153                   Field **fields,
154                   bool read_all)
155 {
156   Field *f;
157 
158   if (unlikely(!m_row_exists))
159     return HA_ERR_RECORD_DELETED;
160 
161   /* Set the null bits */
162   assert(table->s->null_bytes == 1);
163   buf[0]= 0;
164 
165   for (; (f= *fields) ; fields++)
166   {
167     if (read_all || bitmap_is_set(table->read_set, f->field_index))
168     {
169       switch(f->field_index)
170       {
171       case 0: /* VARIABLE_NAME */
172         set_field_varchar_utf8(f, m_row.m_variable_name.m_str, m_row.m_variable_name.m_length);
173         break;
174       case 1: /* VARIABLE_VALUE */
175         m_row.m_variable_value.set_field(f);
176         break;
177       default:
178         assert(false);
179       }
180     }
181   }
182 
183   return 0;
184 }
185 
186