1
/* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software Foundation,
14
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
17
@file storage/perfschema/pfs_events_waits.cc
18
Events waits data structures (implementation).
21
#include "my_global.h"
23
#include "pfs_global.h"
24
#include "pfs_instr_class.h"
25
#include "pfs_instr.h"
28
#include "pfs_account.h"
29
#include "pfs_events_waits.h"
30
#include "pfs_atomic.h"
33
ulong events_waits_history_long_size= 0;
34
/** Consumer flag for table EVENTS_WAITS_CURRENT. */
35
bool flag_events_waits_current= false;
36
/** Consumer flag for table EVENTS_WAITS_HISTORY. */
37
bool flag_events_waits_history= false;
38
/** Consumer flag for table EVENTS_WAITS_HISTORY_LONG. */
39
bool flag_events_waits_history_long= false;
40
/** Consumer flag for the global instrumentation. */
41
bool flag_global_instrumentation= false;
42
/** Consumer flag for the per thread instrumentation. */
43
bool flag_thread_instrumentation= false;
45
/** True if EVENTS_WAITS_HISTORY_LONG circular buffer is full. */
46
bool events_waits_history_long_full= false;
47
/** Index in EVENTS_WAITS_HISTORY_LONG circular buffer. */
48
volatile uint32 events_waits_history_long_index= 0;
49
/** EVENTS_WAITS_HISTORY_LONG circular buffer. */
50
PFS_events_waits *events_waits_history_long_array= NULL;
53
Initialize table EVENTS_WAITS_HISTORY_LONG.
54
@param events_waits_history_long_sizing table sizing
56
int init_events_waits_history_long(uint events_waits_history_long_sizing)
58
events_waits_history_long_size= events_waits_history_long_sizing;
59
events_waits_history_long_full= false;
60
PFS_atomic::store_u32(&events_waits_history_long_index, 0);
62
if (events_waits_history_long_size == 0)
65
events_waits_history_long_array=
66
PFS_MALLOC_ARRAY(events_waits_history_long_size, PFS_events_waits,
69
return (events_waits_history_long_array ? 0 : 1);
72
/** Cleanup table EVENTS_WAITS_HISTORY_LONG. */
73
void cleanup_events_waits_history_long(void)
75
pfs_free(events_waits_history_long_array);
76
events_waits_history_long_array= NULL;
79
static inline void copy_events_waits(PFS_events_waits *dest,
80
const PFS_events_waits *source)
82
memcpy(dest, source, sizeof(PFS_events_waits));
86
Insert a wait record in table EVENTS_WAITS_HISTORY.
87
@param thread thread that executed the wait
88
@param wait record to insert
90
void insert_events_waits_history(PFS_thread *thread, PFS_events_waits *wait)
92
if (unlikely(events_waits_history_per_thread == 0))
95
uint index= thread->m_waits_history_index;
98
A concurrent thread executing TRUNCATE TABLE EVENTS_WAITS_CURRENT
99
could alter the data that this thread is inserting,
100
causing a potential race condition.
101
We are not testing for this and insert a possibly empty record,
102
to make this thread (the writer) faster.
103
This is ok, the readers of m_waits_history will filter this out.
105
copy_events_waits(&thread->m_waits_history[index], wait);
108
if (index >= events_waits_history_per_thread)
111
thread->m_waits_history_full= true;
113
thread->m_waits_history_index= index;
117
Insert a wait record in table EVENTS_WAITS_HISTORY_LONG.
118
@param wait record to insert
120
void insert_events_waits_history_long(PFS_events_waits *wait)
122
if (unlikely(events_waits_history_long_size == 0))
125
uint index= PFS_atomic::add_u32(&events_waits_history_long_index, 1);
127
index= index % events_waits_history_long_size;
129
events_waits_history_long_full= true;
131
/* See related comment in insert_events_waits_history. */
132
copy_events_waits(&events_waits_history_long_array[index], wait);
135
/** Reset table EVENTS_WAITS_CURRENT data. */
136
void reset_events_waits_current(void)
138
PFS_thread *pfs_thread= thread_array;
139
PFS_thread *pfs_thread_last= thread_array + thread_max;
141
for ( ; pfs_thread < pfs_thread_last; pfs_thread++)
143
PFS_events_waits *pfs_wait= pfs_thread->m_events_waits_stack;
144
PFS_events_waits *pfs_wait_last= pfs_wait + WAIT_STACK_SIZE;
146
for ( ; pfs_wait < pfs_wait_last; pfs_wait++)
147
pfs_wait->m_wait_class= NO_WAIT_CLASS;
151
/** Reset table EVENTS_WAITS_HISTORY data. */
152
void reset_events_waits_history(void)
154
PFS_thread *pfs_thread= thread_array;
155
PFS_thread *pfs_thread_last= thread_array + thread_max;
157
for ( ; pfs_thread < pfs_thread_last; pfs_thread++)
159
PFS_events_waits *wait= pfs_thread->m_waits_history;
160
PFS_events_waits *wait_last= wait + events_waits_history_per_thread;
162
pfs_thread->m_waits_history_index= 0;
163
pfs_thread->m_waits_history_full= false;
164
for ( ; wait < wait_last; wait++)
165
wait->m_wait_class= NO_WAIT_CLASS;
169
/** Reset table EVENTS_WAITS_HISTORY_LONG data. */
170
void reset_events_waits_history_long(void)
172
PFS_atomic::store_u32(&events_waits_history_long_index, 0);
173
events_waits_history_long_full= false;
175
PFS_events_waits *wait= events_waits_history_long_array;
176
PFS_events_waits *wait_last= wait + events_waits_history_long_size;
177
for ( ; wait < wait_last; wait++)
178
wait->m_wait_class= NO_WAIT_CLASS;
181
/** Reset table EVENTS_WAITS_SUMMARY_BY_THREAD_BY_EVENT_NAME data. */
182
void reset_events_waits_by_thread()
184
PFS_thread *thread= thread_array;
185
PFS_thread *thread_last= thread_array + thread_max;
186
PFS_account *account;
190
for ( ; thread < thread_last; thread++)
192
if (thread->m_lock.is_populated())
194
account= sanitize_account(thread->m_account);
195
user= sanitize_user(thread->m_user);
196
host= sanitize_host(thread->m_host);
197
aggregate_thread_waits(thread, account, user, host);
202
/** Reset table EVENTS_WAITS_SUMMARY_BY_ACCOUNT_BY_EVENT_NAME data. */
203
void reset_events_waits_by_account()
205
PFS_account *pfs= account_array;
206
PFS_account *pfs_last= account_array + account_max;
210
for ( ; pfs < pfs_last; pfs++)
212
if (pfs->m_lock.is_populated())
214
user= sanitize_user(pfs->m_user);
215
host= sanitize_host(pfs->m_host);
216
pfs->aggregate_waits(user, host);
221
/** Reset table EVENTS_WAITS_SUMMARY_BY_USER_BY_EVENT_NAME data. */
222
void reset_events_waits_by_user()
224
PFS_user *pfs= user_array;
225
PFS_user *pfs_last= user_array + user_max;
227
for ( ; pfs < pfs_last; pfs++)
229
if (pfs->m_lock.is_populated())
230
pfs->aggregate_waits();
234
/** Reset table EVENTS_WAITS_SUMMARY_BY_HOST_BY_EVENT_NAME data. */
235
void reset_events_waits_by_host()
237
PFS_host *pfs= host_array;
238
PFS_host *pfs_last= host_array + host_max;
240
for ( ; pfs < pfs_last; pfs++)
242
if (pfs->m_lock.is_populated())
243
pfs->aggregate_waits();
247
void reset_table_waits_by_table()
249
PFS_table_share *pfs= table_share_array;
250
PFS_table_share *pfs_last= pfs + table_share_max;
252
for ( ; pfs < pfs_last; pfs++)
254
if (pfs->m_lock.is_populated())
259
void reset_table_io_waits_by_table()
261
PFS_table_share *pfs= table_share_array;
262
PFS_table_share *pfs_last= pfs + table_share_max;
264
for ( ; pfs < pfs_last; pfs++)
266
if (pfs->m_lock.is_populated())
271
void reset_table_lock_waits_by_table()
273
PFS_table_share *pfs= table_share_array;
274
PFS_table_share *pfs_last= pfs + table_share_max;
276
for ( ; pfs < pfs_last; pfs++)
278
if (pfs->m_lock.is_populated())
279
pfs->aggregate_lock();
283
void reset_table_waits_by_table_handle()
285
PFS_table *pfs= table_array;
286
PFS_table *pfs_last= pfs + table_max;
288
for ( ; pfs < pfs_last; pfs++)
290
if (pfs->m_lock.is_populated())
291
pfs->sanitized_aggregate();
295
void reset_table_io_waits_by_table_handle()
297
PFS_table *pfs= table_array;
298
PFS_table *pfs_last= pfs + table_max;
300
for ( ; pfs < pfs_last; pfs++)
302
if (pfs->m_lock.is_populated())
303
pfs->sanitized_aggregate_io();
307
void reset_table_lock_waits_by_table_handle()
309
PFS_table *pfs= table_array;
310
PFS_table *pfs_last= pfs + table_max;
312
for ( ; pfs < pfs_last; pfs++)
314
if (pfs->m_lock.is_populated())
315
pfs->sanitized_aggregate_lock();