1 /* Copyright (c) 2008, 2020, Oracle and/or its affiliates. All rights reserved.
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_EVENTS_WAITS_H
24 #define TABLE_EVENTS_WAITS_H
25 
26 /**
27   @file storage/perfschema/table_events_waits.h
28   Table EVENTS_WAITS_xxx (declarations).
29 */
30 
31 #include <sys/types.h>
32 
33 #include "my_base.h"
34 #include "my_inttypes.h"
35 #include "storage/perfschema/pfs_column_types.h"
36 #include "storage/perfschema/pfs_engine_table.h"
37 #include "storage/perfschema/table_helper.h"
38 
39 class Field;
40 class Plugin_table;
41 struct PFS_events_waits;
42 struct PFS_thread;
43 struct TABLE;
44 struct THR_LOCK;
45 
46 /**
47   @addtogroup performance_schema_tables
48   @{
49 */
50 
51 /** A row of table_events_waits_common. */
52 struct row_events_waits {
53   /** Column THREAD_ID. */
54   ulonglong m_thread_internal_id;
55   /** Column EVENT_ID. */
56   ulonglong m_event_id;
57   /** Column END_EVENT_ID. */
58   ulonglong m_end_event_id;
59   /** Column NESTING_EVENT_ID. */
60   ulonglong m_nesting_event_id;
61   /** Column NESTING_EVENT_TYPE. */
62   enum_event_type m_nesting_event_type;
63   /** Column EVENT_NAME. */
64   const char *m_name;
65   /** Length in bytes of @c m_name. */
66   uint m_name_length;
67   /** Column TIMER_START. */
68   ulonglong m_timer_start;
69   /** Column TIMER_END. */
70   ulonglong m_timer_end;
71   /** Column TIMER_WAIT. */
72   ulonglong m_timer_wait;
73   /** Column OBJECT_TYPE. */
74   const char *m_object_type;
75   /** Length in bytes of @c m_object_type. */
76   uint m_object_type_length;
77   /** Column OBJECT_SCHEMA. */
78   char m_object_schema[COL_OBJECT_SCHEMA_SIZE];
79   /** Length in bytes of @c m_object_schema. */
80   uint m_object_schema_length;
81   /** Column OBJECT_NAME. */
82   char m_object_name[COL_OBJECT_NAME_EXTENDED_SIZE];
83   /** Length in bytes of @c m_object_name. */
84   uint m_object_name_length;
85   /** Column INDEX_NAME. */
86   char m_index_name[COL_INDEX_NAME_SIZE];
87   /** Length in bytes of @c m_index_name. */
88   uint m_index_name_length;
89   /** Column OBJECT_INSTANCE_BEGIN. */
90   intptr m_object_instance_addr;
91   /** Column SOURCE. */
92   char m_source[COL_SOURCE_SIZE];
93   /** Length in bytes of @c m_source. */
94   uint m_source_length;
95   /** Column OPERATION. */
96   enum_operation_type m_operation;
97   /** Column NUMBER_OF_BYTES. */
98   ulonglong m_number_of_bytes;
99   /** Column FLAGS. */
100   uint m_flags;
101 };
102 
103 /** Position of a cursor on PERFORMANCE_SCHEMA.EVENTS_WAITS_CURRENT. */
104 struct pos_events_waits_current : public PFS_double_index {
pos_events_waits_currentpos_events_waits_current105   pos_events_waits_current() : PFS_double_index(0, 0) {}
106 
resetpos_events_waits_current107   inline void reset(void) {
108     m_index_1 = 0;
109     m_index_2 = 0;
110   }
111 
next_threadpos_events_waits_current112   inline void next_thread(void) {
113     m_index_1++;
114     m_index_2 = 0;
115   }
116 };
117 
118 /** Position of a cursor on PERFORMANCE_SCHEMA.EVENTS_WAITS_HISTORY. */
119 struct pos_events_waits_history : public PFS_double_index {
pos_events_waits_historypos_events_waits_history120   pos_events_waits_history() : PFS_double_index(0, 0) {}
121 
resetpos_events_waits_history122   inline void reset(void) {
123     m_index_1 = 0;
124     m_index_2 = 0;
125   }
126 
next_threadpos_events_waits_history127   inline void next_thread(void) {
128     m_index_1++;
129     m_index_2 = 0;
130   }
131 };
132 
133 class PFS_index_events_waits : public PFS_engine_index {
134  public:
PFS_index_events_waits()135   PFS_index_events_waits()
136       : PFS_engine_index(&m_key_1, &m_key_2),
137         m_key_1("THREAD_ID"),
138         m_key_2("EVENT_ID") {}
139 
~PFS_index_events_waits()140   ~PFS_index_events_waits() {}
141 
142   bool match(PFS_thread *pfs);
143   bool match(PFS_events_waits *pfs);
144 
145  private:
146   PFS_key_thread_id m_key_1;
147   PFS_key_event_id m_key_2;
148 };
149 
150 /**
151   Adapter, for table sharing the structure of
152   PERFORMANCE_SCHEMA.EVENTS_WAITS_CURRENT.
153 */
154 class table_events_waits_common : public PFS_engine_table {
155  protected:
156   virtual int read_row_values(TABLE *table, unsigned char *buf, Field **fields,
157                               bool read_all);
158 
159   table_events_waits_common(const PFS_engine_table_share *share, void *pos);
160 
~table_events_waits_common()161   ~table_events_waits_common() {}
162 
163   void clear_object_columns();
164   int make_table_object_columns(PFS_events_waits *wait);
165   int make_file_object_columns(PFS_events_waits *wait);
166   int make_socket_object_columns(PFS_events_waits *wait);
167   int make_metadata_lock_object_columns(PFS_events_waits *wait);
168 
169   int make_row(PFS_events_waits *wait);
170 
171   /** Current row. */
172   row_events_waits m_row;
173 };
174 
175 /** Table PERFORMANCE_SCHEMA.EVENTS_WAITS_CURRENT. */
176 class table_events_waits_current : public table_events_waits_common {
177  public:
178   /** Table share */
179   static PFS_engine_table_share m_share;
180   static PFS_engine_table *create(PFS_engine_table_share *);
181   static int delete_all_rows();
182   static ha_rows get_row_count();
183 
184   virtual int index_init(uint idx, bool sorted);
185 
186   virtual int rnd_next();
187   virtual int rnd_pos(const void *pos);
188   virtual int index_next();
189   virtual void reset_position(void);
190 
191  protected:
192   table_events_waits_current();
193 
194  public:
~table_events_waits_current()195   ~table_events_waits_current() {}
196 
197  private:
198   friend class table_events_waits_history;
199   friend class table_events_waits_history_long;
200 
201   /** Table share lock. */
202   static THR_LOCK m_table_lock;
203   /** Table definition. */
204   static Plugin_table m_table_def;
205 
206   PFS_events_waits *get_wait(PFS_thread *pfs_thread, uint index_2);
207   int make_row(PFS_thread *thread, PFS_events_waits *wait);
208 
209   /** Current position. */
210   pos_events_waits_current m_pos;
211   /** Next position. */
212   pos_events_waits_current m_next_pos;
213 
214   PFS_index_events_waits *m_opened_index;
215 };
216 
217 /** Table PERFORMANCE_SCHEMA.EVENTS_WAITS_HISTORY. */
218 class table_events_waits_history : public table_events_waits_common {
219  public:
220   /** Table share */
221   static PFS_engine_table_share m_share;
222   static PFS_engine_table *create(PFS_engine_table_share *);
223   static int delete_all_rows();
224   static ha_rows get_row_count();
225 
226   virtual void reset_position(void);
227 
228   virtual int index_init(uint idx, bool sorted);
229   virtual int index_next();
230 
231   virtual int rnd_next();
232   virtual int rnd_pos(const void *pos);
233 
234  protected:
235   table_events_waits_history();
236 
237  public:
~table_events_waits_history()238   ~table_events_waits_history() {}
239 
240  private:
241   /** Table share lock. */
242   static THR_LOCK m_table_lock;
243   /** Table definition. */
244   static Plugin_table m_table_def;
245 
246   PFS_events_waits *get_wait(PFS_thread *pfs_thread, uint index_2);
247   int make_row(PFS_thread *thread, PFS_events_waits *wait);
248 
249   /** Current position. */
250   pos_events_waits_history m_pos;
251   /** Next position. */
252   pos_events_waits_history m_next_pos;
253 
254   PFS_index_events_waits *m_opened_index;
255 };
256 
257 /** Table PERFORMANCE_SCHEMA.EVENTS_WAITS_HISTORY_LONG. */
258 class table_events_waits_history_long : public table_events_waits_common {
259  public:
260   /** Table share */
261   static PFS_engine_table_share m_share;
262   static PFS_engine_table *create(PFS_engine_table_share *);
263   static int delete_all_rows();
264   static ha_rows get_row_count();
265 
266   virtual int rnd_next();
267   virtual int rnd_pos(const void *pos);
268   virtual void reset_position(void);
269 
270  protected:
271   table_events_waits_history_long();
272 
273  public:
~table_events_waits_history_long()274   ~table_events_waits_history_long() {}
275 
276  private:
277   /** Table share lock. */
278   static THR_LOCK m_table_lock;
279   /** Table definition. */
280   static Plugin_table m_table_def;
281 
282   /** Current position. */
283   PFS_simple_index m_pos;
284   /** Next position. */
285   PFS_simple_index m_next_pos;
286 };
287 
288 /** @} */
289 #endif
290