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_STATUS_BY_HOST_H
24 #define TABLE_STATUS_BY_HOST_H
25 
26 /**
27   @file storage/perfschema/table_status_by_host.h
28   Table STATUS_BY_HOST (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 "pfs_host.h"
36 #include "table_helper.h"
37 #include "pfs_variable.h"
38 #include "pfs_buffer_container.h"
39 
40 /**
41   @addtogroup Performance_schema_tables
42   @{
43 */
44 
45 /**
46   A row of table
47   PERFORMANCE_SCHEMA.STATUS_BY_HOST.
48 */
49 struct row_status_by_host
50 {
51   /** Column HOST */
52   PFS_host_row m_host;
53   /** Column VARIABLE_NAME. */
54   PFS_variable_name_row m_variable_name;
55   /** Column VARIABLE_VALUE. */
56   PFS_variable_value_row m_variable_value;
57 };
58 
59 /**
60   Position of a cursor on
61   PERFORMANCE_SCHEMA.STATUS_BY_HOST.
62   Index 1 on host (0 based)
63   Index 2 on status variable (0 based)
64 */
65 struct pos_status_by_host
66 : public PFS_double_index
67 {
pos_status_by_hostpos_status_by_host68   pos_status_by_host()
69     : PFS_double_index(0, 0)
70   {}
71 
resetpos_status_by_host72   inline void reset(void)
73   {
74     m_index_1= 0;
75     m_index_2= 0;
76   }
77 
has_more_hostpos_status_by_host78   inline bool has_more_host(void)
79   { return (m_index_1 < global_host_container.get_row_count()); }
80 
next_hostpos_status_by_host81   inline void next_host(void)
82   {
83     m_index_1++;
84     m_index_2= 0;
85   }
86 };
87 
88 /**
89   Store and retrieve table state information for queries that reinstantiate
90   the table object.
91 */
92 class table_status_by_host_context : public PFS_table_context
93 {
94 public:
table_status_by_host_context(ulonglong current_version,bool restore)95   table_status_by_host_context(ulonglong current_version, bool restore) :
96     PFS_table_context(current_version, global_host_container.get_row_count(), restore, THR_PFS_SBH) { }
97 };
98 
99 /** Table PERFORMANCE_SCHEMA.STATUS_BY_HOST. */
100 class table_status_by_host : public PFS_engine_table
101 {
102   typedef pos_status_by_host pos_t;
103 
104 public:
105   /** Table share */
106   static PFS_engine_table_share m_share;
107   static PFS_engine_table* create();
108   static int delete_all_rows();
109   static ha_rows get_row_count();
110 
111   virtual int rnd_init(bool scan);
112   virtual int rnd_next();
113   virtual int rnd_pos(const void *pos);
114   virtual void reset_position(void);
115 
116 protected:
117   virtual int read_row_values(TABLE *table,
118                               unsigned char *buf,
119                               Field **fields,
120                               bool read_all);
121   table_status_by_host();
122 
123 public:
~table_status_by_host()124   ~table_status_by_host()
125   {}
126 
127 protected:
128   int materialize(PFS_thread *thread);
129   void make_row(PFS_host *pfs_host, const Status_variable *status_var);
130 
131 private:
132   /** Table share lock. */
133   static THR_LOCK m_table_lock;
134   /** Fields definition. */
135   static TABLE_FIELD_DEF m_field_def;
136 
137   /** Status variable cache for one host. */
138   PFS_status_variable_cache m_status_cache;
139 
140   /** Current row. */
141   row_status_by_host m_row;
142   /** True if the current row exists. */
143   bool m_row_exists;
144   /** Current position. */
145   pos_t m_pos;
146   /** Next position. */
147   pos_t m_next_pos;
148 
149   /** Table context with global status array version and map of materialized threads. */
150   table_status_by_host_context *m_context;
151 };
152 
153 /** @} */
154 #endif
155