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_USER_H
24 #define PFS_USER_H
25 
26 /**
27   @file storage/perfschema/pfs_user.h
28   Performance schema user (declarations).
29 */
30 
31 #include "pfs_lock.h"
32 #include "lf.h"
33 #include "pfs_con_slice.h"
34 
35 struct PFS_global_param;
36 struct PFS_thread;
37 
38 /**
39   @addtogroup Performance_schema_buffers
40   @{
41 */
42 
43 /** Hash key for a user. */
44 struct PFS_user_key
45 {
46   /**
47     Hash search key.
48     This has to be a string for LF_HASH,
49     the format is "<username><0x00>"
50   */
51   char m_hash_key[USERNAME_LENGTH + 1];
52   uint m_key_length;
53 };
54 
55 /** Per user statistics. */
56 struct PFS_ALIGNED PFS_user : public PFS_connection_slice
57 {
58 public:
init_refcountPFS_user59   inline void init_refcount(void)
60   {
61     PFS_atomic::store_32(& m_refcount, 1);
62   }
63 
get_refcountPFS_user64   inline int get_refcount(void)
65   {
66     return PFS_atomic::load_32(& m_refcount);
67   }
68 
inc_refcountPFS_user69   inline void inc_refcount(void)
70   {
71     PFS_atomic::add_32(& m_refcount, 1);
72   }
73 
dec_refcountPFS_user74   inline void dec_refcount(void)
75   {
76     PFS_atomic::add_32(& m_refcount, -1);
77   }
78 
79   void aggregate(bool alive);
80   void aggregate_waits(void);
81   void aggregate_stages(void);
82   void aggregate_statements(void);
83   void aggregate_transactions(void);
84   void aggregate_memory(bool alive);
85   void aggregate_status(void);
86   void aggregate_stats(void);
87   void release(void);
88 
89   void carry_memory_stat_delta(PFS_memory_stat_delta *delta, uint index);
90 
91   /** Internal lock. */
92   pfs_lock m_lock;
93   PFS_user_key m_key;
94   const char *m_username;
95   uint m_username_length;
96 
97   ulonglong m_disconnected_count;
98 
99 private:
100   int m_refcount;
101 };
102 
103 int init_user(const PFS_global_param *param);
104 void cleanup_user(void);
105 int init_user_hash(const PFS_global_param *param);
106 void cleanup_user_hash(void);
107 
108 PFS_user *
109 find_or_create_user(PFS_thread *thread,
110                     const char *username, uint username_length);
111 
112 PFS_user *sanitize_user(PFS_user *unsafe);
113 void purge_all_user(void);
114 
115 
116 /* For show status. */
117 
118 extern LF_HASH user_hash;
119 
120 /** @} */
121 #endif
122 
123