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 PFS_EVENTS_WAITS_H
24 #define PFS_EVENTS_WAITS_H
25 
26 /**
27   @file storage/perfschema/pfs_events_waits.h
28   Events waits data structures (declarations).
29 */
30 
31 #include <sys/types.h>
32 #include <atomic>
33 
34 #include "my_inttypes.h"
35 #include "storage/perfschema/pfs_column_types.h"
36 #include "storage/perfschema/pfs_events.h"
37 #include "storage/perfschema/pfs_global.h"
38 #include "storage/perfschema/pfs_lock.h"
39 
40 struct PFS_mutex;
41 struct PFS_rwlock;
42 struct PFS_cond;
43 struct PFS_table;
44 struct PFS_file;
45 struct PFS_thread;
46 struct PFS_socket;
47 struct PFS_instr_class;
48 struct PFS_table_share;
49 struct PFS_account;
50 struct PFS_user;
51 struct PFS_host;
52 struct PFS_metadata_lock;
53 
54 /** Class of a wait event. */
55 enum events_waits_class {
56   NO_WAIT_CLASS = 0,
57   WAIT_CLASS_MUTEX,
58   WAIT_CLASS_RWLOCK,
59   WAIT_CLASS_COND,
60   WAIT_CLASS_TABLE,
61   WAIT_CLASS_FILE,
62   WAIT_CLASS_SOCKET,
63   WAIT_CLASS_IDLE,
64   WAIT_CLASS_METADATA
65 };
66 
67 /** A wait event record. */
68 struct PFS_events_waits : public PFS_events {
69   /**
70     The type of wait.
71     Readers:
72     - the consumer threads.
73     Writers:
74     - the producer threads, in the instrumentation.
75     Out of bound Writers:
76     - TRUNCATE EVENTS_WAITS_CURRENT
77     - TRUNCATE EVENTS_WAITS_HISTORY
78     - TRUNCATE EVENTS_WAITS_HISTORY_LONG
79   */
80   events_waits_class m_wait_class;
81   /** Object type */
82   enum_object_type m_object_type;
83   /** Table share, for table operations only. */
84   PFS_table_share *m_weak_table_share;
85   /** File, for file operations only. */
86   PFS_file *m_weak_file;
87   /** Socket, for socket operations only. */
88   PFS_socket *m_weak_socket;
89   /** Metadata lock, for MDL operations only. */
90   PFS_metadata_lock *m_weak_metadata_lock;
91   /** For weak pointers, target object version. */
92   uint32 m_weak_version;
93   /** Address in memory of the object instance waited on. */
94   const void *m_object_instance_addr;
95   /** Operation performed. */
96   enum_operation_type m_operation;
97   /**
98     Number of bytes/rows read/written.
99     This member is populated for FILE READ/WRITE operations, with a number of
100     bytes.
101     This member is populated for TABLE I/O operations, with a number of rows.
102   */
103   size_t m_number_of_bytes;
104   /**
105     Index used.
106     This member is populated for TABLE I/O operations only.
107   */
108   uint m_index;
109   /** Flags */
110   ulong m_flags;
111 };
112 
113 /** TIMED bit in the state flags bitfield. */
114 #define STATE_FLAG_TIMED (1 << 0)
115 /** THREAD bit in the state flags bitfield. */
116 #define STATE_FLAG_THREAD (1 << 1)
117 /** EVENT bit in the state flags bitfield. */
118 #define STATE_FLAG_EVENT (1 << 2)
119 /** DIGEST bit in the state flags bitfield. */
120 #define STATE_FLAG_DIGEST (1 << 3)
121 
122 void insert_events_waits_history(PFS_thread *thread, PFS_events_waits *wait);
123 
124 void insert_events_waits_history_long(PFS_events_waits *wait);
125 
126 extern bool flag_events_waits_current;
127 extern bool flag_events_waits_history;
128 extern bool flag_events_waits_history_long;
129 extern bool flag_global_instrumentation;
130 extern bool flag_thread_instrumentation;
131 
132 extern bool events_waits_history_long_full;
133 extern PFS_cacheline_atomic_uint32 events_waits_history_long_index;
134 extern PFS_events_waits *events_waits_history_long_array;
135 extern ulong events_waits_history_long_size;
136 
137 int init_events_waits_history_long(uint events_waits_history_long_sizing);
138 void cleanup_events_waits_history_long();
139 
140 void reset_events_waits_current();
141 void reset_events_waits_history();
142 void reset_events_waits_history_long();
143 void reset_events_waits_by_thread();
144 void reset_events_waits_by_account();
145 void reset_events_waits_by_user();
146 void reset_events_waits_by_host();
147 void reset_events_waits_global();
148 void aggregate_account_waits(PFS_account *account);
149 void aggregate_user_waits(PFS_user *user);
150 void aggregate_host_waits(PFS_host *host);
151 
152 void reset_table_waits_by_table();
153 void reset_table_io_waits_by_table();
154 void reset_table_lock_waits_by_table();
155 void reset_table_waits_by_table_handle();
156 void reset_table_io_waits_by_table_handle();
157 void reset_table_lock_waits_by_table_handle();
158 
159 #endif
160