21 #include "my_global.h"
49 static bool account_hash_inited=
false;
63 account_instr_class_waits_array= NULL;
64 account_instr_class_stages_array= NULL;
65 account_instr_class_statements_array= NULL;
66 uint waits_sizing= account_max * wait_class_max;
74 if (unlikely(account_array == NULL))
80 account_instr_class_waits_array=
82 if (unlikely(account_instr_class_waits_array == NULL))
86 if (stages_sizing > 0)
88 account_instr_class_stages_array=
90 if (unlikely(account_instr_class_stages_array == NULL))
94 if (statements_sizing > 0)
96 account_instr_class_statements_array=
98 if (unlikely(account_instr_class_statements_array == NULL))
102 for (index= 0; index < account_max; index++)
105 &account_instr_class_waits_array[index * wait_class_max];
120 pfs_free(account_instr_class_waits_array);
121 account_instr_class_waits_array= NULL;
126 static uchar *account_hash_get_key(
const uchar *
entry,
size_t *length,
133 DBUG_ASSERT(typed_entry != NULL);
134 account= *typed_entry;
135 DBUG_ASSERT(account != NULL);
136 *length= account->m_key.m_key_length;
138 return const_cast<uchar*
> (
reinterpret_cast<const uchar*
> (result));
148 if ((! account_hash_inited) && (account_max > 0))
150 lf_hash_init(&account_hash,
sizeof(
PFS_account*), LF_HASH_UNIQUE,
151 0, 0, account_hash_get_key, &my_charset_bin);
152 account_hash.size= account_max;
153 account_hash_inited=
true;
161 if (account_hash_inited)
163 lf_hash_destroy(&account_hash);
164 account_hash_inited=
false;
172 if (! account_hash_inited)
180 const char *user, uint user_length,
181 const char *host, uint host_length)
183 DBUG_ASSERT(user_length <= USERNAME_LENGTH);
184 DBUG_ASSERT(host_length <= HOSTNAME_LENGTH);
189 memcpy(ptr, user, user_length);
196 memcpy(ptr, host, host_length);
206 const char *username, uint username_length,
207 const char *hostname, uint hostname_length)
209 if (account_max == 0)
215 LF_PINS *pins= get_account_hash_pins(thread);
216 if (unlikely(pins == NULL))
223 set_account_key(&key, username, username_length,
224 hostname, hostname_length);
228 const uint retry_max= 3;
232 (lf_hash_search(&account_hash, pins,
234 if (entry && (entry != MY_ERRPTR))
239 lf_hash_search_unpin(pins);
243 lf_hash_search_unpin(pins);
248 for (scan.
init(random, account_max);
254 for ( ; pfs < pfs_last; pfs++)
261 if (username_length > 0)
264 pfs->m_username= NULL;
265 pfs->m_username_length= username_length;
267 if (hostname_length > 0)
268 pfs->m_hostname= &pfs->m_key.
m_hash_key[username_length + 1];
270 pfs->m_hostname= NULL;
271 pfs->m_hostname_length= hostname_length;
273 pfs->m_user= find_or_create_user(thread, username, username_length);
274 pfs->m_host= find_or_create_host(thread, hostname, hostname_length);
276 pfs->init_refcount();
278 pfs->m_disconnected_count= 0;
281 res= lf_hash_insert(&account_hash, pins, &pfs);
282 if (likely(res == 0))
290 pfs->m_user->release();
295 pfs->m_host->release();
303 if (++retry_count > retry_max)
324 aggregate_waits(safe_user, safe_host);
325 aggregate_stages(safe_user, safe_host);
326 aggregate_statements(safe_user, safe_host);
327 aggregate_stats(safe_user, safe_host);
332 if (likely(safe_user != NULL && safe_host != NULL))
346 if (safe_user != NULL)
357 if (safe_host != NULL)
373 void PFS_account::aggregate_stages(
PFS_user *safe_user,
PFS_host *safe_host)
375 if (likely(safe_user != NULL && safe_host != NULL))
389 if (safe_user != NULL)
399 global_instr_class_stages_array);
403 if (safe_host != NULL)
419 global_instr_class_stages_array);
423 void PFS_account::aggregate_statements(
PFS_user *safe_user,
PFS_host *safe_host)
425 if (likely(safe_user != NULL && safe_host != NULL))
439 if (safe_user != NULL)
449 global_instr_class_statements_array);
453 if (safe_host != NULL)
469 global_instr_class_statements_array);
475 if (likely(safe_user != NULL && safe_host != NULL))
477 safe_user->m_disconnected_count+= m_disconnected_count;
478 safe_host->m_disconnected_count+= m_disconnected_count;
479 m_disconnected_count= 0;
483 if (safe_user != NULL)
485 safe_user->m_disconnected_count+= m_disconnected_count;
486 m_disconnected_count= 0;
490 if (safe_host != NULL)
492 safe_host->m_disconnected_count+= m_disconnected_count;
493 m_disconnected_count= 0;
497 m_disconnected_count= 0;
501 void PFS_account::release()
508 if ((&account_array[0] <= unsafe) &&
509 (unsafe < &account_array[account_max]))
517 account->aggregate(safe_user, safe_host);
519 LF_PINS *pins= get_account_hash_pins(thread);
520 if (unlikely(pins == NULL))
525 (lf_hash_search(&account_hash, pins,
527 account->m_key.m_key_length));
528 if (entry && (entry != MY_ERRPTR))
532 DBUG_ASSERT(pfs == account);
533 if (account->get_refcount() == 0)
535 lf_hash_delete(&account_hash, pins,
537 account->m_key.m_key_length);
538 if (account->m_user != NULL)
540 account->m_user->release();
541 account->m_user= NULL;
543 if (account->m_host != NULL)
545 account->m_host->release();
546 account->m_host= NULL;
552 lf_hash_search_unpin(pins);
558 PFS_thread *thread= PFS_thread::get_current_thread();
559 if (unlikely(thread == NULL))
563 PFS_account *pfs_last= account_array + account_max;
567 for ( ; pfs < pfs_last; pfs++)
571 user= sanitize_user(pfs->m_user);
572 host= sanitize_host(pfs->m_host);
573 pfs->aggregate_stats(user, host);
575 if (pfs->get_refcount() == 0)
576 purge_account(thread, pfs, user, host);