1 /* Copyright (c) 2015, 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 Foundation,
21   51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
22 
23 /**
24   @file storage/perfschema/pfs_status.cc
25   Status variables statistics (implementation).
26 */
27 
28 #include "my_global.h"
29 #include "my_sys.h"
30 #include "pfs_global.h"
31 #include "pfs_instr_class.h"
32 #include "pfs_instr.h"
33 #include "pfs_account.h"
34 #include "pfs_host.h"
35 #include "pfs_user.h"
36 #include "pfs_status.h"
37 #include "pfs_atomic.h"
38 #include "pfs_buffer_container.h"
39 
40 #include "sql_show.h" /* reset_status_vars */
41 
PFS_status_stats()42 PFS_status_stats::PFS_status_stats()
43 {
44   reset();
45 }
46 
reset()47 void PFS_status_stats::reset()
48 {
49   m_has_stats= false;
50   memset(&m_stats, 0, sizeof(m_stats));
51 }
52 
aggregate(const PFS_status_stats * from)53 void PFS_status_stats::aggregate(const PFS_status_stats *from)
54 {
55   if (from->m_has_stats)
56   {
57     m_has_stats= true;
58     for (int i= 0; i < COUNT_GLOBAL_STATUS_VARS; i++)
59     {
60       m_stats[i] += from->m_stats[i];
61     }
62   }
63 }
64 
aggregate_from(const STATUS_VAR * from)65 void PFS_status_stats::aggregate_from(const STATUS_VAR *from)
66 {
67   ulonglong *from_var= (ulonglong*) from;
68 
69   m_has_stats= true;
70   for (int i= 0;
71        i < COUNT_GLOBAL_STATUS_VARS;
72        i++, from_var++)
73   {
74     m_stats[i] += *from_var;
75   }
76 }
77 
aggregate_to(STATUS_VAR * to)78 void PFS_status_stats::aggregate_to(STATUS_VAR *to)
79 {
80   if (m_has_stats)
81   {
82     ulonglong *to_var= (ulonglong*) to;
83 
84     for (int i= 0;
85          i < COUNT_GLOBAL_STATUS_VARS;
86          i++, to_var++)
87     {
88       *to_var += m_stats[i];
89     }
90   }
91 }
92 
fct_reset_status_by_thread(PFS_thread * thread)93 static void fct_reset_status_by_thread(PFS_thread *thread)
94 {
95   PFS_account *account;
96   PFS_user *user;
97   PFS_host *host;
98 
99   if (thread->m_lock.is_populated())
100   {
101     account= sanitize_account(thread->m_account);
102     user= sanitize_user(thread->m_user);
103     host= sanitize_host(thread->m_host);
104     aggregate_thread_status(thread, account, user, host);
105   }
106 }
107 
108 /** Reset table STATUS_BY_THREAD data. */
reset_status_by_thread()109 void reset_status_by_thread()
110 {
111   global_thread_container.apply_all(fct_reset_status_by_thread);
112 }
113 
fct_reset_status_by_account(PFS_account * account)114 static void fct_reset_status_by_account(PFS_account *account)
115 {
116   PFS_user *user;
117   PFS_host *host;
118 
119   if (account->m_lock.is_populated())
120   {
121     user= sanitize_user(account->m_user);
122     host= sanitize_host(account->m_host);
123     account->aggregate_status(user, host);
124   }
125 }
126 
127 /** Reset table STATUS_BY_ACCOUNT data. */
reset_status_by_account()128 void reset_status_by_account()
129 {
130   global_account_container.apply_all(fct_reset_status_by_account);
131 }
132 
fct_reset_status_by_user(PFS_user * user)133 static void fct_reset_status_by_user(PFS_user *user)
134 {
135   if (user->m_lock.is_populated())
136     user->aggregate_status();
137 }
138 
139 /** Reset table STATUS_BY_USER data. */
reset_status_by_user()140 void reset_status_by_user()
141 {
142   global_user_container.apply_all(fct_reset_status_by_user);
143 }
144 
fct_reset_status_by_host(PFS_host * host)145 static void fct_reset_status_by_host(PFS_host *host)
146 {
147   if (host->m_lock.is_populated())
148     host->aggregate_status();
149 }
150 
151 /** Reset table STATUS_BY_HOST data. */
reset_status_by_host()152 void reset_status_by_host()
153 {
154   global_host_container.apply_all(fct_reset_status_by_host);
155 }
156 
157 /** Reset table GLOBAL_STATUS data. */
reset_global_status()158 void reset_global_status()
159 {
160   /*
161     Do not memset global_status_var,
162     NO_FLUSH counters need to be preserved
163   */
164   reset_status_vars();
165 }
166 
167