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 #ifndef TABLE_VARIABLES_BY_THREAD_H
24 #define TABLE_VARIABLES_BY_THREAD_H
25 
26 /**
27   @file storage/perfschema/table_variables_by_thread.h
28   Table VARIABLES_BY_THREAD (declarations).
29 */
30 
31 #include "pfs_column_types.h"
32 #include "pfs_engine_table.h"
33 #include "pfs_instr_class.h"
34 #include "pfs_instr.h"
35 #include "table_helper.h"
36 #include "pfs_variable.h"
37 #include "pfs_buffer_container.h"
38 
39 /**
40   @addtogroup Performance_schema_tables
41   @{
42 */
43 
44 /**
45   A row of table
46   PERFORMANCE_SCHEMA.VARIABLES_BY_THREAD.
47 */
48 struct row_variables_by_thread
49 {
50   /** Column THREAD_ID. */
51   ulonglong m_thread_internal_id;
52   /** Column VARIABLE_NAME. */
53   PFS_variable_name_row m_variable_name;
54   /** Column VARIABLE_VALUE. */
55   PFS_variable_value_row m_variable_value;
56 };
57 
58 /**
59   Position of a cursor on
60   PERFORMANCE_SCHEMA.VARIABLES_BY_THREAD.
61   Index 1 on thread (0 based)
62   Index 2 on system variable (0 based)
63 */
64 struct pos_variables_by_thread
65 : public PFS_double_index
66 {
pos_variables_by_threadpos_variables_by_thread67   pos_variables_by_thread()
68     : PFS_double_index(0, 0)
69   {}
70 
resetpos_variables_by_thread71   inline void reset(void)
72   {
73     m_index_1= 0;
74     m_index_2= 0;
75   }
76 
has_more_threadpos_variables_by_thread77   inline bool has_more_thread(void)
78   { return (m_index_1 < global_thread_container.get_row_count()); }
79 
next_threadpos_variables_by_thread80   inline void next_thread(void)
81   {
82     m_index_1++;
83     m_index_2= 0;
84   }
85 };
86 
87 /**
88   Store and retrieve table state information during queries that reinstantiate
89   the table object.
90 */
91 class table_variables_by_thread_context : public PFS_table_context
92 {
93 public:
table_variables_by_thread_context(ulonglong hash_version,bool restore)94   table_variables_by_thread_context(ulonglong hash_version, bool restore) :
95     PFS_table_context(hash_version, global_thread_container.get_row_count(), restore, THR_PFS_VBT) { }
96 };
97 
98 /** Table PERFORMANCE_SCHEMA.VARIABLES_BY_THREAD. */
99 class table_variables_by_thread : public PFS_engine_table
100 {
101   typedef pos_variables_by_thread pos_t;
102 
103 public:
104   /** Table share */
105   static PFS_engine_table_share m_share;
106   static PFS_engine_table* create();
107   static ha_rows get_row_count();
108 
109   virtual int rnd_init(bool scan);
110   virtual int rnd_next();
111   virtual int rnd_pos(const void *pos);
112   virtual void reset_position(void);
113 
114 protected:
115   virtual int read_row_values(TABLE *table,
116                               unsigned char *buf,
117                               Field **fields,
118                               bool read_all);
119   table_variables_by_thread();
120 
121 public:
~table_variables_by_thread()122   ~table_variables_by_thread()
123   {}
124 
125 protected:
126   int materialize(PFS_thread *thread);
127   void make_row(PFS_thread *thread, const System_variable *system_var);
128 
129 private:
130   /** Table share lock. */
131   static THR_LOCK m_table_lock;
132   /** Fields definition. */
133   static TABLE_FIELD_DEF m_field_def;
134 
135   /** Current THD variables. */
136   PFS_system_variable_cache m_sysvar_cache;
137   /** Current row. */
138   row_variables_by_thread m_row;
139   /** True if the current row exists. */
140   bool m_row_exists;
141   /** Current position. */
142   pos_t m_pos;
143   /** Next position. */
144   pos_t m_next_pos;
145 
146   /** Table context with system variable hash version and map of materialized threads. */
147   table_variables_by_thread_context *m_context;
148 };
149 
150 /** @} */
151 #endif
152