1 /* Copyright (c) 2011, 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/table_mems_global_by_event_name.cc
25   Table MEMORY_SUMMARY_GLOBAL_BY_EVENT_NAME (implementation).
26 */
27 
28 #include "my_global.h"
29 #include "my_thread.h"
30 #include "pfs_instr_class.h"
31 #include "pfs_column_types.h"
32 #include "pfs_column_values.h"
33 #include "table_mems_global_by_event_name.h"
34 #include "pfs_global.h"
35 #include "pfs_visitor.h"
36 #include "pfs_builtin_memory.h"
37 #include "pfs_memory.h"
38 #include "field.h"
39 
40 THR_LOCK table_mems_global_by_event_name::m_table_lock;
41 
42 static const TABLE_FIELD_TYPE field_types[]=
43 {
44   {
45     { C_STRING_WITH_LEN("EVENT_NAME") },
46     { C_STRING_WITH_LEN("varchar(128)") },
47     { NULL, 0}
48   },
49   {
50     { C_STRING_WITH_LEN("COUNT_ALLOC") },
51     { C_STRING_WITH_LEN("bigint(20)") },
52     { NULL, 0}
53   },
54   {
55     { C_STRING_WITH_LEN("COUNT_FREE") },
56     { C_STRING_WITH_LEN("bigint(20)") },
57     { NULL, 0}
58   },
59   {
60     { C_STRING_WITH_LEN("SUM_NUMBER_OF_BYTES_ALLOC") },
61     { C_STRING_WITH_LEN("bigint(20)") },
62     { NULL, 0}
63   },
64   {
65     { C_STRING_WITH_LEN("SUM_NUMBER_OF_BYTES_FREE") },
66     { C_STRING_WITH_LEN("bigint(20)") },
67     { NULL, 0}
68   },
69   {
70     { C_STRING_WITH_LEN("LOW_COUNT_USED") },
71     { C_STRING_WITH_LEN("bigint(20)") },
72     { NULL, 0}
73   },
74   {
75     { C_STRING_WITH_LEN("CURRENT_COUNT_USED") },
76     { C_STRING_WITH_LEN("bigint(20)") },
77     { NULL, 0}
78   },
79   {
80     { C_STRING_WITH_LEN("HIGH_COUNT_USED") },
81     { C_STRING_WITH_LEN("bigint(20)") },
82     { NULL, 0}
83   },
84   {
85     { C_STRING_WITH_LEN("LOW_NUMBER_OF_BYTES_USED") },
86     { C_STRING_WITH_LEN("bigint(20)") },
87     { NULL, 0}
88   },
89   {
90     { C_STRING_WITH_LEN("CURRENT_NUMBER_OF_BYTES_USED") },
91     { C_STRING_WITH_LEN("bigint(20)") },
92     { NULL, 0}
93   },
94   {
95     { C_STRING_WITH_LEN("HIGH_NUMBER_OF_BYTES_USED") },
96     { C_STRING_WITH_LEN("bigint(20)") },
97     { NULL, 0}
98   }
99 };
100 
101 TABLE_FIELD_DEF
102 table_mems_global_by_event_name::m_field_def=
103 { 11, field_types };
104 
105 PFS_engine_table_share
106 table_mems_global_by_event_name::m_share=
107 {
108   { C_STRING_WITH_LEN("memory_summary_global_by_event_name") },
109   &pfs_readonly_acl,
110   table_mems_global_by_event_name::create,
111   NULL, /* write_row */
112   table_mems_global_by_event_name::delete_all_rows,
113   table_mems_global_by_event_name::get_row_count,
114   sizeof(pos_t),
115   &m_table_lock,
116   &m_field_def,
117   false, /* checked */
118   false  /* perpetual */
119 };
120 
create(void)121 PFS_engine_table* table_mems_global_by_event_name::create(void)
122 {
123   return new table_mems_global_by_event_name();
124 }
125 
126 int
delete_all_rows(void)127 table_mems_global_by_event_name::delete_all_rows(void)
128 {
129   reset_memory_by_thread();
130   reset_memory_by_account();
131   reset_memory_by_user();
132   reset_memory_by_host();
133   reset_memory_global();
134   return 0;
135 }
136 
137 ha_rows
get_row_count(void)138 table_mems_global_by_event_name::get_row_count(void)
139 {
140   return memory_class_max;
141 }
142 
table_mems_global_by_event_name()143 table_mems_global_by_event_name::table_mems_global_by_event_name()
144   : PFS_engine_table(&m_share, &m_pos),
145   m_row_exists(false), m_pos(), m_next_pos()
146 {}
147 
reset_position(void)148 void table_mems_global_by_event_name::reset_position(void)
149 {
150   m_pos.reset();
151   m_next_pos.reset();
152 }
153 
rnd_next(void)154 int table_mems_global_by_event_name::rnd_next(void)
155 {
156   PFS_memory_class *pfs;
157   PFS_builtin_memory_class *pfs_builtin;
158 
159   /* Do not advertise hard coded instruments when disabled. */
160   if (! pfs_initialized)
161     return HA_ERR_END_OF_FILE;
162 
163   for (m_pos.set_at(&m_next_pos);
164        m_pos.has_more_view();
165        m_pos.next_view())
166   {
167     switch (m_pos.m_index_1)
168     {
169     case pos_mems_global_by_event_name::VIEW_BUILTIN_MEMORY:
170       pfs_builtin= find_builtin_memory_class(m_pos.m_index_2);
171       if (pfs_builtin != NULL)
172       {
173         make_row(pfs_builtin);
174         m_next_pos.set_after(&m_pos);
175         return 0;
176       }
177       break;
178     case pos_mems_global_by_event_name::VIEW_MEMORY:
179       pfs= find_memory_class(m_pos.m_index_2);
180       if (pfs != NULL)
181       {
182         make_row(pfs);
183         m_next_pos.set_after(&m_pos);
184         return 0;
185       }
186       break;
187     }
188   }
189 
190   return HA_ERR_END_OF_FILE;
191 }
192 
rnd_pos(const void * pos)193 int table_mems_global_by_event_name::rnd_pos(const void *pos)
194 {
195   PFS_builtin_memory_class *pfs_builtin;
196   PFS_memory_class *pfs;
197 
198   /* Do not advertise hard coded instruments when disabled. */
199   if (! pfs_initialized)
200     return HA_ERR_END_OF_FILE;
201 
202   set_position(pos);
203 
204   switch(m_pos.m_index_1)
205   {
206   case pos_mems_global_by_event_name::VIEW_BUILTIN_MEMORY:
207     pfs_builtin= find_builtin_memory_class(m_pos.m_index_2);
208     if (pfs_builtin != NULL)
209     {
210       make_row(pfs_builtin);
211       return 0;
212     }
213     break;
214   case pos_mems_global_by_event_name::VIEW_MEMORY:
215     pfs= find_memory_class(m_pos.m_index_2);
216     if (pfs != NULL)
217     {
218       make_row(pfs);
219       return 0;
220     }
221     break;
222   }
223 
224   return HA_ERR_RECORD_DELETED;
225 }
226 
make_row(PFS_memory_class * klass)227 void table_mems_global_by_event_name::make_row(PFS_memory_class *klass)
228 {
229   m_row.m_event_name.make_row(klass);
230 
231   PFS_connection_memory_visitor visitor(klass);
232 
233   if (klass->is_global())
234   {
235     PFS_connection_iterator::visit_global(false, /* hosts */
236                                           false, /* users */
237                                           false, /* accounts */
238                                           false, /* threads */
239                                           false, /* THDs */
240                                           &visitor);
241   }
242   else
243   {
244     PFS_connection_iterator::visit_global(true,  /* hosts */
245                                           false, /* users */
246                                           true,  /* accounts */
247                                           true,  /* threads */
248                                           false, /* THDs */
249                                           &visitor);
250   }
251 
252   m_row.m_stat.set(& visitor.m_stat);
253   m_row_exists= true;
254 }
255 
make_row(PFS_builtin_memory_class * klass)256 void table_mems_global_by_event_name::make_row(PFS_builtin_memory_class *klass)
257 {
258   m_row.m_event_name.make_row(& klass->m_class);
259   m_row.m_stat.set(& klass->m_stat);
260   m_row_exists= true;
261 }
262 
read_row_values(TABLE * table,unsigned char *,Field ** fields,bool read_all)263 int table_mems_global_by_event_name::read_row_values(TABLE *table,
264                                                     unsigned char *,
265                                                     Field **fields,
266                                                     bool read_all)
267 {
268   Field *f;
269 
270   if (unlikely(! m_row_exists))
271     return HA_ERR_RECORD_DELETED;
272 
273   /* Set the null bits */
274   assert(table->s->null_bytes == 0);
275 
276   for (; (f= *fields) ; fields++)
277   {
278     if (read_all || bitmap_is_set(table->read_set, f->field_index))
279     {
280       switch(f->field_index)
281       {
282       case 0: /* EVENT_NAME */
283         m_row.m_event_name.set_field(f);
284         break;
285       default: /* 1, ... HIGH_NUMBER_OF_BYTES_USED */
286         m_row.m_stat.set_field(f->field_index - 1, f);
287         break;
288       }
289     }
290   }
291 
292   return 0;
293 }
294 
295