1 /* Copyright (c) 2010, 2021, Oracle and/or its affiliates.
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 Street, Fifth Floor, Boston, MA 02110-1301, USA */
22 
23 #ifndef PFS_ACCOUNT_H
24 #define PFS_ACCOUNT_H
25 
26 /**
27   @file storage/perfschema/pfs_account.h
28   Performance schema account (declarations).
29 */
30 
31 #include "pfs_lock.h"
32 #include "lf.h"
33 #include "pfs_con_slice.h"
34 #include "mysql_com.h" /* USERNAME_LENGTH */
35 
36 struct PFS_global_param;
37 struct PFS_user;
38 struct PFS_host;
39 struct PFS_thread;
40 struct PFS_memory_stat_delta;
41 
42 /**
43   @addtogroup Performance_schema_buffers
44   @{
45 */
46 
47 /** Hash key for an account. */
48 struct PFS_account_key
49 {
50   /**
51     Hash search key.
52     This has to be a string for LF_HASH,
53     the format is "<username><0x00><hostname><0x00>"
54   */
55   char m_hash_key[USERNAME_LENGTH + 1 + HOSTNAME_LENGTH + 1];
56   uint m_key_length;
57 };
58 
59 /** Per account statistics. */
60 struct PFS_ALIGNED PFS_account : PFS_connection_slice
61 {
62 public:
init_refcountPFS_account63   inline void init_refcount(void)
64   {
65     PFS_atomic::store_32(& m_refcount, 1);
66   }
67 
get_refcountPFS_account68   inline int get_refcount(void)
69   {
70     return PFS_atomic::load_32(& m_refcount);
71   }
72 
inc_refcountPFS_account73   inline void inc_refcount(void)
74   {
75     PFS_atomic::add_32(& m_refcount, 1);
76   }
77 
dec_refcountPFS_account78   inline void dec_refcount(void)
79   {
80     PFS_atomic::add_32(& m_refcount, -1);
81   }
82 
83   void aggregate(bool alive, PFS_user *safe_user, PFS_host *safe_host);
84   void aggregate_waits(PFS_user *safe_user, PFS_host *safe_host);
85   void aggregate_stages(PFS_user *safe_user, PFS_host *safe_host);
86   void aggregate_statements(PFS_user *safe_user, PFS_host *safe_host);
87   void aggregate_transactions(PFS_user *safe_user, PFS_host *safe_host);
88   void aggregate_memory(bool alive, PFS_user *safe_user, PFS_host *safe_host);
89   void aggregate_status(PFS_user *safe_user, PFS_host *safe_host);
90   void aggregate_stats(PFS_user *safe_user, PFS_host *safe_host);
91   void release(void);
92 
93   void carry_memory_stat_delta(PFS_memory_stat_delta *delta, uint index);
94 
95   /** Internal lock. */
96   pfs_lock m_lock;
97   PFS_account_key m_key;
98   /** True if this account is enabled, per rules in table SETUP_ACTORS. */
99   bool m_enabled;
100   /** True if this account has history enabled, per rules in table SETUP_ACTORS. */
101   bool m_history;
102   const char *m_username;
103   uint m_username_length;
104   const char *m_hostname;
105   uint m_hostname_length;
106   PFS_user *m_user;
107   PFS_host *m_host;
108 
109   ulonglong m_disconnected_count;
110 
111 private:
112   int m_refcount;
113 };
114 
115 int init_account(const PFS_global_param *param);
116 void cleanup_account(void);
117 int init_account_hash(const PFS_global_param *param);
118 void cleanup_account_hash(void);
119 
120 PFS_account *
121 find_or_create_account(PFS_thread *thread,
122                          const char *username, uint username_length,
123                          const char *hostname, uint hostname_length);
124 
125 PFS_account *sanitize_account(PFS_account *unsafe);
126 void purge_all_account(void);
127 
128 void update_accounts_derived_flags(PFS_thread *thread);
129 
130 /* For show status. */
131 
132 extern LF_HASH account_hash;
133 
134 /** @} */
135 #endif
136 
137