You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

590 lines
16 KiB

11 years ago
6 years ago
6 years ago
10 years ago
13 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
  1. /* Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License, version 2.0,
  4. as published by the Free Software Foundation.
  5. This program is also distributed with certain software (including
  6. but not limited to OpenSSL) that is licensed under separate terms,
  7. as designated in a particular file or component or in included license
  8. documentation. The authors of MySQL hereby grant you an additional
  9. permission to link the program and your derivative works with the
  10. separately licensed software that they have included with MySQL.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License, version 2.0, for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */
  18. /**
  19. @file storage/perfschema/pfs_account.cc
  20. Performance schema user@host (implementation).
  21. */
  22. #include "my_global.h"
  23. #include "my_sys.h"
  24. #include "pfs.h"
  25. #include "pfs_stat.h"
  26. #include "pfs_instr.h"
  27. #include "pfs_setup_actor.h"
  28. #include "pfs_host.h"
  29. #include "pfs_host.h"
  30. #include "pfs_user.h"
  31. #include "pfs_account.h"
  32. #include "pfs_global.h"
  33. #include "pfs_instr_class.h"
  34. /**
  35. @addtogroup Performance_schema_buffers
  36. @{
  37. */
  38. ulong account_max;
  39. ulong account_lost;
  40. PFS_account *account_array= NULL;
  41. static PFS_single_stat *account_instr_class_waits_array= NULL;
  42. static PFS_stage_stat *account_instr_class_stages_array= NULL;
  43. static PFS_statement_stat *account_instr_class_statements_array= NULL;
  44. LF_HASH account_hash;
  45. static bool account_hash_inited= false;
  46. /**
  47. Initialize the user buffers.
  48. @param param sizing parameters
  49. @return 0 on success
  50. */
  51. int init_account(const PFS_global_param *param)
  52. {
  53. uint index;
  54. account_max= param->m_account_sizing;
  55. account_array= NULL;
  56. account_instr_class_waits_array= NULL;
  57. account_instr_class_stages_array= NULL;
  58. account_instr_class_statements_array= NULL;
  59. uint waits_sizing= account_max * wait_class_max;
  60. uint stages_sizing= account_max * stage_class_max;
  61. uint statements_sizing= account_max * statement_class_max;
  62. if (account_max > 0)
  63. {
  64. account_array= PFS_MALLOC_ARRAY(account_max, sizeof(PFS_account), PFS_account,
  65. MYF(MY_ZEROFILL));
  66. if (unlikely(account_array == NULL))
  67. return 1;
  68. }
  69. if (waits_sizing > 0)
  70. {
  71. account_instr_class_waits_array=
  72. PFS_connection_slice::alloc_waits_slice(waits_sizing);
  73. if (unlikely(account_instr_class_waits_array == NULL))
  74. return 1;
  75. }
  76. if (stages_sizing > 0)
  77. {
  78. account_instr_class_stages_array=
  79. PFS_connection_slice::alloc_stages_slice(stages_sizing);
  80. if (unlikely(account_instr_class_stages_array == NULL))
  81. return 1;
  82. }
  83. if (statements_sizing > 0)
  84. {
  85. account_instr_class_statements_array=
  86. PFS_connection_slice::alloc_statements_slice(statements_sizing);
  87. if (unlikely(account_instr_class_statements_array == NULL))
  88. return 1;
  89. }
  90. for (index= 0; index < account_max; index++)
  91. {
  92. account_array[index].m_instr_class_waits_stats=
  93. &account_instr_class_waits_array[index * wait_class_max];
  94. account_array[index].m_instr_class_stages_stats=
  95. &account_instr_class_stages_array[index * stage_class_max];
  96. account_array[index].m_instr_class_statements_stats=
  97. &account_instr_class_statements_array[index * statement_class_max];
  98. }
  99. return 0;
  100. }
  101. /** Cleanup all the user buffers. */
  102. void cleanup_account(void)
  103. {
  104. pfs_free(account_array);
  105. account_array= NULL;
  106. pfs_free(account_instr_class_waits_array);
  107. account_instr_class_waits_array= NULL;
  108. pfs_free(account_instr_class_stages_array);
  109. account_instr_class_stages_array= 0;
  110. pfs_free(account_instr_class_statements_array);
  111. account_instr_class_statements_array=0;
  112. account_max= 0;
  113. }
  114. C_MODE_START
  115. static uchar *account_hash_get_key(const uchar *entry, size_t *length,
  116. my_bool)
  117. {
  118. const PFS_account * const *typed_entry;
  119. const PFS_account *account;
  120. const void *result;
  121. typed_entry= reinterpret_cast<const PFS_account* const *> (entry);
  122. DBUG_ASSERT(typed_entry != NULL);
  123. account= *typed_entry;
  124. DBUG_ASSERT(account != NULL);
  125. *length= account->m_key.m_key_length;
  126. result= account->m_key.m_hash_key;
  127. return const_cast<uchar*> (reinterpret_cast<const uchar*> (result));
  128. }
  129. C_MODE_END
  130. /**
  131. Initialize the user hash.
  132. @return 0 on success
  133. */
  134. int init_account_hash(void)
  135. {
  136. if ((! account_hash_inited) && (account_max > 0))
  137. {
  138. lf_hash_init(&account_hash, sizeof(PFS_account*), LF_HASH_UNIQUE,
  139. 0, 0, account_hash_get_key, &my_charset_bin);
  140. /* account_hash.size= account_max; */
  141. account_hash_inited= true;
  142. }
  143. return 0;
  144. }
  145. /** Cleanup the user hash. */
  146. void cleanup_account_hash(void)
  147. {
  148. if (account_hash_inited)
  149. {
  150. lf_hash_destroy(&account_hash);
  151. account_hash_inited= false;
  152. }
  153. }
  154. static LF_PINS* get_account_hash_pins(PFS_thread *thread)
  155. {
  156. if (unlikely(thread->m_account_hash_pins == NULL))
  157. {
  158. if (! account_hash_inited)
  159. return NULL;
  160. thread->m_account_hash_pins= lf_hash_get_pins(&account_hash);
  161. }
  162. return thread->m_account_hash_pins;
  163. }
  164. static void set_account_key(PFS_account_key *key,
  165. const char *user, uint user_length,
  166. const char *host, uint host_length)
  167. {
  168. DBUG_ASSERT(user_length <= USERNAME_LENGTH);
  169. DBUG_ASSERT(host_length <= HOSTNAME_LENGTH);
  170. char *ptr= &key->m_hash_key[0];
  171. if (user_length > 0)
  172. {
  173. memcpy(ptr, user, user_length);
  174. ptr+= user_length;
  175. }
  176. ptr[0]= 0;
  177. ptr++;
  178. if (host_length > 0)
  179. {
  180. memcpy(ptr, host, host_length);
  181. ptr+= host_length;
  182. }
  183. ptr[0]= 0;
  184. ptr++;
  185. key->m_key_length= (uint)(ptr - &key->m_hash_key[0]);
  186. }
  187. PFS_account *
  188. find_or_create_account(PFS_thread *thread,
  189. const char *username, uint username_length,
  190. const char *hostname, uint hostname_length)
  191. {
  192. if (account_max == 0)
  193. {
  194. account_lost++;
  195. return NULL;
  196. }
  197. LF_PINS *pins= get_account_hash_pins(thread);
  198. if (unlikely(pins == NULL))
  199. {
  200. account_lost++;
  201. return NULL;
  202. }
  203. PFS_account_key key;
  204. set_account_key(&key, username, username_length,
  205. hostname, hostname_length);
  206. PFS_account **entry;
  207. uint retry_count= 0;
  208. const uint retry_max= 3;
  209. search:
  210. entry= reinterpret_cast<PFS_account**>
  211. (lf_hash_search(&account_hash, pins,
  212. key.m_hash_key, key.m_key_length));
  213. if (entry && (entry != MY_ERRPTR))
  214. {
  215. PFS_account *pfs;
  216. pfs= *entry;
  217. pfs->inc_refcount();
  218. lf_hash_search_unpin(pins);
  219. return pfs;
  220. }
  221. lf_hash_search_unpin(pins);
  222. PFS_scan scan;
  223. uint random= randomized_index(username, account_max);
  224. for (scan.init(random, account_max);
  225. scan.has_pass();
  226. scan.next_pass())
  227. {
  228. PFS_account *pfs= account_array + scan.first();
  229. PFS_account *pfs_last= account_array + scan.last();
  230. for ( ; pfs < pfs_last; pfs++)
  231. {
  232. if (pfs->m_lock.is_free())
  233. {
  234. if (pfs->m_lock.free_to_dirty())
  235. {
  236. pfs->m_key= key;
  237. if (username_length > 0)
  238. pfs->m_username= &pfs->m_key.m_hash_key[0];
  239. else
  240. pfs->m_username= NULL;
  241. pfs->m_username_length= username_length;
  242. if (hostname_length > 0)
  243. pfs->m_hostname= &pfs->m_key.m_hash_key[username_length + 1];
  244. else
  245. pfs->m_hostname= NULL;
  246. pfs->m_hostname_length= hostname_length;
  247. pfs->m_user= find_or_create_user(thread, username, username_length);
  248. pfs->m_host= find_or_create_host(thread, hostname, hostname_length);
  249. pfs->init_refcount();
  250. pfs->reset_stats();
  251. pfs->m_disconnected_count= 0;
  252. int res;
  253. res= lf_hash_insert(&account_hash, pins, &pfs);
  254. if (likely(res == 0))
  255. {
  256. pfs->m_lock.dirty_to_allocated();
  257. return pfs;
  258. }
  259. if (pfs->m_user)
  260. {
  261. pfs->m_user->release();
  262. pfs->m_user= NULL;
  263. }
  264. if (pfs->m_host)
  265. {
  266. pfs->m_host->release();
  267. pfs->m_host= NULL;
  268. }
  269. pfs->m_lock.dirty_to_free();
  270. if (res > 0)
  271. {
  272. if (++retry_count > retry_max)
  273. {
  274. account_lost++;
  275. return NULL;
  276. }
  277. goto search;
  278. }
  279. account_lost++;
  280. return NULL;
  281. }
  282. }
  283. }
  284. }
  285. account_lost++;
  286. return NULL;
  287. }
  288. void PFS_account::aggregate(PFS_user *safe_user, PFS_host *safe_host)
  289. {
  290. aggregate_waits(safe_user, safe_host);
  291. aggregate_stages(safe_user, safe_host);
  292. aggregate_statements(safe_user, safe_host);
  293. aggregate_stats(safe_user, safe_host);
  294. }
  295. void PFS_account::aggregate_waits(PFS_user *safe_user, PFS_host *safe_host)
  296. {
  297. if (likely(safe_user != NULL && safe_host != NULL))
  298. {
  299. /*
  300. Aggregate EVENTS_WAITS_SUMMARY_BY_ACCOUNT_BY_EVENT_NAME to:
  301. - EVENTS_WAITS_SUMMARY_BY_USER_BY_EVENT_NAME
  302. - EVENTS_WAITS_SUMMARY_BY_HOST_BY_EVENT_NAME
  303. in parallel.
  304. */
  305. aggregate_all_event_names(m_instr_class_waits_stats,
  306. safe_user->m_instr_class_waits_stats,
  307. safe_host->m_instr_class_waits_stats);
  308. return;
  309. }
  310. if (safe_user != NULL)
  311. {
  312. /*
  313. Aggregate EVENTS_WAITS_SUMMARY_BY_ACCOUNT_BY_EVENT_NAME to:
  314. - EVENTS_WAITS_SUMMARY_BY_USER_BY_EVENT_NAME
  315. */
  316. aggregate_all_event_names(m_instr_class_waits_stats,
  317. safe_user->m_instr_class_waits_stats);
  318. return;
  319. }
  320. if (safe_host != NULL)
  321. {
  322. /*
  323. Aggregate EVENTS_WAITS_SUMMARY_BY_ACCOUNT_BY_EVENT_NAME to:
  324. - EVENTS_WAITS_SUMMARY_BY_HOST_BY_EVENT_NAME
  325. */
  326. aggregate_all_event_names(m_instr_class_waits_stats,
  327. safe_host->m_instr_class_waits_stats);
  328. return;
  329. }
  330. /* Orphan account, no parent to aggregate to. */
  331. reset_waits_stats();
  332. return;
  333. }
  334. void PFS_account::aggregate_stages(PFS_user *safe_user, PFS_host *safe_host)
  335. {
  336. if (likely(safe_user != NULL && safe_host != NULL))
  337. {
  338. /*
  339. Aggregate EVENTS_STAGES_SUMMARY_BY_ACCOUNT_BY_EVENT_NAME to:
  340. - EVENTS_STAGES_SUMMARY_BY_USER_BY_EVENT_NAME
  341. - EVENTS_STAGES_SUMMARY_BY_HOST_BY_EVENT_NAME
  342. in parallel.
  343. */
  344. aggregate_all_stages(m_instr_class_stages_stats,
  345. safe_user->m_instr_class_stages_stats,
  346. safe_host->m_instr_class_stages_stats);
  347. return;
  348. }
  349. if (safe_user != NULL)
  350. {
  351. /*
  352. Aggregate EVENTS_STAGES_SUMMARY_BY_ACCOUNT_BY_EVENT_NAME to:
  353. - EVENTS_STAGES_SUMMARY_BY_USER_BY_EVENT_NAME
  354. - EVENTS_STAGES_SUMMARY_GLOBAL_BY_EVENT_NAME
  355. in parallel.
  356. */
  357. aggregate_all_stages(m_instr_class_stages_stats,
  358. safe_user->m_instr_class_stages_stats,
  359. global_instr_class_stages_array);
  360. return;
  361. }
  362. if (safe_host != NULL)
  363. {
  364. /*
  365. Aggregate EVENTS_STAGES_SUMMARY_BY_ACCOUNT_BY_EVENT_NAME to:
  366. - EVENTS_STAGES_SUMMARY_BY_HOST_BY_EVENT_NAME
  367. */
  368. aggregate_all_stages(m_instr_class_stages_stats,
  369. safe_host->m_instr_class_stages_stats);
  370. return;
  371. }
  372. /*
  373. Aggregate EVENTS_STAGES_SUMMARY_BY_ACCOUNT_BY_EVENT_NAME to:
  374. - EVENTS_STAGES_SUMMARY_GLOBAL_BY_EVENT_NAME
  375. */
  376. aggregate_all_stages(m_instr_class_stages_stats,
  377. global_instr_class_stages_array);
  378. return;
  379. }
  380. void PFS_account::aggregate_statements(PFS_user *safe_user, PFS_host *safe_host)
  381. {
  382. if (likely(safe_user != NULL && safe_host != NULL))
  383. {
  384. /*
  385. Aggregate EVENTS_STATEMENTS_SUMMARY_BY_ACCOUNT_BY_EVENT_NAME to:
  386. - EVENTS_STATEMENTS_SUMMARY_BY_USER_BY_EVENT_NAME
  387. - EVENTS_STATEMENTS_SUMMARY_BY_HOST_BY_EVENT_NAME
  388. in parallel.
  389. */
  390. aggregate_all_statements(m_instr_class_statements_stats,
  391. safe_user->m_instr_class_statements_stats,
  392. safe_host->m_instr_class_statements_stats);
  393. return;
  394. }
  395. if (safe_user != NULL)
  396. {
  397. /*
  398. Aggregate EVENTS_STATEMENTS_SUMMARY_BY_ACCOUNT_BY_EVENT_NAME to:
  399. - EVENTS_STATEMENTS_SUMMARY_BY_USER_BY_EVENT_NAME
  400. - EVENTS_STATEMENTS_SUMMARY_GLOBAL_BY_EVENT_NAME
  401. in parallel.
  402. */
  403. aggregate_all_statements(m_instr_class_statements_stats,
  404. safe_user->m_instr_class_statements_stats,
  405. global_instr_class_statements_array);
  406. return;
  407. }
  408. if (safe_host != NULL)
  409. {
  410. /*
  411. Aggregate EVENTS_STATEMENTS_SUMMARY_BY_ACCOUNT_BY_EVENT_NAME to:
  412. - EVENTS_STATEMENTS_SUMMARY_BY_HOST_BY_EVENT_NAME
  413. */
  414. aggregate_all_statements(m_instr_class_statements_stats,
  415. safe_host->m_instr_class_statements_stats);
  416. return;
  417. }
  418. /*
  419. Aggregate EVENTS_STATEMENTS_SUMMARY_BY_ACCOUNT_BY_EVENT_NAME to:
  420. - EVENTS_STATEMENTS_SUMMARY_GLOBAL_BY_EVENT_NAME
  421. */
  422. aggregate_all_statements(m_instr_class_statements_stats,
  423. global_instr_class_statements_array);
  424. return;
  425. }
  426. void PFS_account::aggregate_stats(PFS_user *safe_user, PFS_host *safe_host)
  427. {
  428. if (likely(safe_user != NULL && safe_host != NULL))
  429. {
  430. safe_user->m_disconnected_count+= m_disconnected_count;
  431. safe_host->m_disconnected_count+= m_disconnected_count;
  432. m_disconnected_count= 0;
  433. return;
  434. }
  435. if (safe_user != NULL)
  436. {
  437. safe_user->m_disconnected_count+= m_disconnected_count;
  438. m_disconnected_count= 0;
  439. return;
  440. }
  441. if (safe_host != NULL)
  442. {
  443. safe_host->m_disconnected_count+= m_disconnected_count;
  444. m_disconnected_count= 0;
  445. return;
  446. }
  447. m_disconnected_count= 0;
  448. return;
  449. }
  450. void PFS_account::release()
  451. {
  452. dec_refcount();
  453. }
  454. PFS_account *sanitize_account(PFS_account *unsafe)
  455. {
  456. if ((&account_array[0] <= unsafe) &&
  457. (unsafe < &account_array[account_max]))
  458. return unsafe;
  459. return NULL;
  460. }
  461. void purge_account(PFS_thread *thread, PFS_account *account,
  462. PFS_user *safe_user, PFS_host *safe_host)
  463. {
  464. account->aggregate(safe_user, safe_host);
  465. LF_PINS *pins= get_account_hash_pins(thread);
  466. if (unlikely(pins == NULL))
  467. return;
  468. PFS_account **entry;
  469. entry= reinterpret_cast<PFS_account**>
  470. (lf_hash_search(&account_hash, pins,
  471. account->m_key.m_hash_key,
  472. account->m_key.m_key_length));
  473. if (entry && (entry != MY_ERRPTR))
  474. {
  475. DBUG_ASSERT(*entry == account);
  476. if (account->get_refcount() == 0)
  477. {
  478. lf_hash_delete(&account_hash, pins,
  479. account->m_key.m_hash_key,
  480. account->m_key.m_key_length);
  481. if (account->m_user != NULL)
  482. {
  483. account->m_user->release();
  484. account->m_user= NULL;
  485. }
  486. if (account->m_host != NULL)
  487. {
  488. account->m_host->release();
  489. account->m_host= NULL;
  490. }
  491. account->m_lock.allocated_to_free();
  492. }
  493. }
  494. lf_hash_search_unpin(pins);
  495. }
  496. /** Purge non connected user@host, reset stats of connected user@host. */
  497. void purge_all_account(void)
  498. {
  499. PFS_thread *thread= PFS_thread::get_current_thread();
  500. if (unlikely(thread == NULL))
  501. return;
  502. PFS_account *pfs= account_array;
  503. PFS_account *pfs_last= account_array + account_max;
  504. PFS_user *user;
  505. PFS_host *host;
  506. for ( ; pfs < pfs_last; pfs++)
  507. {
  508. if (pfs->m_lock.is_populated())
  509. {
  510. user= sanitize_user(pfs->m_user);
  511. host= sanitize_host(pfs->m_host);
  512. pfs->aggregate_stats(user, host);
  513. if (pfs->get_refcount() == 0)
  514. purge_account(thread, pfs, user, host);
  515. }
  516. }
  517. }
  518. /** @} */