1 /* Copyright (c) 2008, 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_file_summary_by_instance.cc
25   Table FILE_SUMMARY_BY_INSTANCE (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_file_summary_by_instance.h"
34 #include "pfs_global.h"
35 #include "pfs_buffer_container.h"
36 #include "field.h"
37 
38 THR_LOCK table_file_summary_by_instance::m_table_lock;
39 
40 static const TABLE_FIELD_TYPE field_types[]=
41 {
42   {
43     { C_STRING_WITH_LEN("FILE_NAME") },
44     { C_STRING_WITH_LEN("varchar(512)") },
45     { NULL, 0}
46   },
47   {
48     { C_STRING_WITH_LEN("EVENT_NAME") },
49     { C_STRING_WITH_LEN("varchar(128)") },
50     { NULL, 0}
51   },
52   {
53     { C_STRING_WITH_LEN("OBJECT_INSTANCE_BEGIN") },
54     { C_STRING_WITH_LEN("bigint(20)") },
55     { NULL, 0}
56   },
57   {
58     { C_STRING_WITH_LEN("COUNT_STAR") },
59     { C_STRING_WITH_LEN("bigint(20)") },
60     { NULL, 0}
61   },
62   {
63     { C_STRING_WITH_LEN("SUM_TIMER_WAIT") },
64     { C_STRING_WITH_LEN("bigint(20)") },
65     { NULL, 0}
66   },
67   {
68     { C_STRING_WITH_LEN("MIN_TIMER_WAIT") },
69     { C_STRING_WITH_LEN("bigint(20)") },
70     { NULL, 0}
71   },
72   {
73     { C_STRING_WITH_LEN("AVG_TIMER_WAIT") },
74     { C_STRING_WITH_LEN("bigint(20)") },
75     { NULL, 0}
76   },
77   {
78     { C_STRING_WITH_LEN("MAX_TIMER_WAIT") },
79     { C_STRING_WITH_LEN("bigint(20)") },
80     { NULL, 0}
81   },
82 
83   /** Read */
84   {
85     { C_STRING_WITH_LEN("COUNT_READ") },
86     { C_STRING_WITH_LEN("bigint(20)") },
87     { NULL, 0}
88   },
89   {
90     { C_STRING_WITH_LEN("SUM_TIMER_READ") },
91     { C_STRING_WITH_LEN("bigint(20)") },
92     { NULL, 0}
93   },
94   {
95     { C_STRING_WITH_LEN("MIN_TIMER_READ") },
96     { C_STRING_WITH_LEN("bigint(20)") },
97     { NULL, 0}
98   },
99   {
100     { C_STRING_WITH_LEN("AVG_TIMER_READ") },
101     { C_STRING_WITH_LEN("bigint(20)") },
102     { NULL, 0}
103   },
104   {
105     { C_STRING_WITH_LEN("MAX_TIMER_READ") },
106     { C_STRING_WITH_LEN("bigint(20)") },
107     { NULL, 0}
108   },
109   {
110     { C_STRING_WITH_LEN("SUM_NUMBER_OF_BYTES_READ") },
111     { C_STRING_WITH_LEN("bigint(20)") },
112     { NULL, 0}
113   },
114 
115   /** Write */
116   {
117     { C_STRING_WITH_LEN("COUNT_WRITE") },
118     { C_STRING_WITH_LEN("bigint(20)") },
119     { NULL, 0}
120   },
121   {
122     { C_STRING_WITH_LEN("SUM_TIMER_WRITE") },
123     { C_STRING_WITH_LEN("bigint(20)") },
124     { NULL, 0}
125   },
126   {
127     { C_STRING_WITH_LEN("MIN_TIMER_WRITE") },
128     { C_STRING_WITH_LEN("bigint(20)") },
129     { NULL, 0}
130   },
131   {
132     { C_STRING_WITH_LEN("AVG_TIMER_WRITE") },
133     { C_STRING_WITH_LEN("bigint(20)") },
134     { NULL, 0}
135   },
136   {
137     { C_STRING_WITH_LEN("MAX_TIMER_WRITE") },
138     { C_STRING_WITH_LEN("bigint(20)") },
139     { NULL, 0}
140   },
141   {
142     { C_STRING_WITH_LEN("SUM_NUMBER_OF_BYTES_WRITE") },
143     { C_STRING_WITH_LEN("bigint(20)") },
144     { NULL, 0}
145   },
146 
147   /** Misc */
148   {
149     { C_STRING_WITH_LEN("COUNT_MISC") },
150     { C_STRING_WITH_LEN("bigint(20)") },
151     { NULL, 0}
152   },
153   {
154     { C_STRING_WITH_LEN("SUM_TIMER_MISC") },
155     { C_STRING_WITH_LEN("bigint(20)") },
156     { NULL, 0}
157   },
158   {
159     { C_STRING_WITH_LEN("MIN_TIMER_MISC") },
160     { C_STRING_WITH_LEN("bigint(20)") },
161     { NULL, 0}
162   },
163   {
164     { C_STRING_WITH_LEN("AVG_TIMER_MISC") },
165     { C_STRING_WITH_LEN("bigint(20)") },
166     { NULL, 0}
167   },
168   {
169     { C_STRING_WITH_LEN("MAX_TIMER_MISC") },
170     { C_STRING_WITH_LEN("bigint(20)") },
171     { NULL, 0}
172   }
173 };
174 
175 TABLE_FIELD_DEF
176 table_file_summary_by_instance::m_field_def=
177 { 25, field_types };
178 
179 PFS_engine_table_share
180 table_file_summary_by_instance::m_share=
181 {
182   { C_STRING_WITH_LEN("file_summary_by_instance") },
183   &pfs_truncatable_acl,
184   table_file_summary_by_instance::create,
185   NULL, /* write_row */
186   table_file_summary_by_instance::delete_all_rows,
187   table_file_summary_by_instance::get_row_count,
188   sizeof(PFS_simple_index),
189   &m_table_lock,
190   &m_field_def,
191   false, /* checked */
192   false  /* perpetual */
193 };
194 
create(void)195 PFS_engine_table* table_file_summary_by_instance::create(void)
196 {
197   return new table_file_summary_by_instance();
198 }
199 
delete_all_rows(void)200 int table_file_summary_by_instance::delete_all_rows(void)
201 {
202   reset_file_instance_io();
203   return 0;
204 }
205 
206 ha_rows
get_row_count(void)207 table_file_summary_by_instance::get_row_count(void)
208 {
209   return global_file_container.get_row_count();
210 }
211 
table_file_summary_by_instance()212 table_file_summary_by_instance::table_file_summary_by_instance()
213   : PFS_engine_table(&m_share, &m_pos),
214   m_row_exists(false), m_pos(0), m_next_pos(0)
215 {}
216 
reset_position(void)217 void table_file_summary_by_instance::reset_position(void)
218 {
219   m_pos.m_index= 0;
220   m_next_pos.m_index= 0;
221 }
222 
rnd_next(void)223 int table_file_summary_by_instance::rnd_next(void)
224 {
225   PFS_file *pfs;
226 
227   m_pos.set_at(&m_next_pos);
228   PFS_file_iterator it= global_file_container.iterate(m_pos.m_index);
229   pfs= it.scan_next(& m_pos.m_index);
230   if (pfs != NULL)
231   {
232     make_row(pfs);
233     m_next_pos.set_after(&m_pos);
234     return 0;
235   }
236 
237   return HA_ERR_END_OF_FILE;
238 }
239 
rnd_pos(const void * pos)240 int table_file_summary_by_instance::rnd_pos(const void *pos)
241 {
242   PFS_file *pfs;
243 
244   set_position(pos);
245 
246   pfs= global_file_container.get(m_pos.m_index);
247   if (pfs != NULL)
248   {
249     make_row(pfs);
250     return 0;
251   }
252 
253   return HA_ERR_RECORD_DELETED;
254 }
255 
256 /**
257   Build a row.
258   @param pfs              the file the cursor is reading
259 */
make_row(PFS_file * pfs)260 void table_file_summary_by_instance::make_row(PFS_file *pfs)
261 {
262   pfs_optimistic_state lock;
263   PFS_file_class *safe_class;
264 
265   m_row_exists= false;
266 
267   /* Protect this reader against a file delete */
268   pfs->m_lock.begin_optimistic_lock(&lock);
269 
270   safe_class= sanitize_file_class(pfs->m_class);
271   if (unlikely(safe_class == NULL))
272     return;
273 
274   m_row.m_filename= pfs->m_filename;
275   m_row.m_filename_length= pfs->m_filename_length;
276   m_row.m_event_name.make_row(safe_class);
277   m_row.m_identity= pfs->m_identity;
278 
279   time_normalizer *normalizer= time_normalizer::get(wait_timer);
280 
281   /* Collect timer and byte count stats */
282   m_row.m_io_stat.set(normalizer, &pfs->m_file_stat.m_io_stat);
283 
284   if (pfs->m_lock.end_optimistic_lock(&lock))
285     m_row_exists= true;
286 }
287 
read_row_values(TABLE * table,unsigned char *,Field ** fields,bool read_all)288 int table_file_summary_by_instance::read_row_values(TABLE *table,
289                                                           unsigned char *,
290                                                           Field **fields,
291                                                           bool read_all)
292 {
293   Field *f;
294 
295   if (unlikely(! m_row_exists))
296     return HA_ERR_RECORD_DELETED;
297 
298   /* Set the null bits */
299   assert(table->s->null_bytes == 0);
300 
301   for (; (f= *fields) ; fields++)
302   {
303     if (read_all || bitmap_is_set(table->read_set, f->field_index))
304     {
305       switch(f->field_index)
306       {
307       case  0: /* FILE_NAME */
308         set_field_varchar_utf8(f, m_row.m_filename, m_row.m_filename_length);
309         break;
310       case  1: /* EVENT_NAME */
311         m_row.m_event_name.set_field(f);
312         break;
313       case  2: /* OBJECT_INSTANCE */
314         set_field_ulonglong(f, (ulonglong)m_row.m_identity);
315         break;
316 
317       case  3:/* COUNT_STAR */
318         set_field_ulonglong(f, m_row.m_io_stat.m_all.m_waits.m_count);
319         break;
320       case  4:/* SUM_TIMER_WAIT */
321         set_field_ulonglong(f, m_row.m_io_stat.m_all.m_waits.m_sum);
322         break;
323       case  5: /* MIN_TIMER_WAIT */
324         set_field_ulonglong(f, m_row.m_io_stat.m_all.m_waits.m_min);
325         break;
326       case  6: /* AVG_TIMER_WAIT */
327         set_field_ulonglong(f, m_row.m_io_stat.m_all.m_waits.m_avg);
328         break;
329       case  7: /* MAX_TIMER_WAIT */
330         set_field_ulonglong(f, m_row.m_io_stat.m_all.m_waits.m_max);
331         break;
332 
333       case  8: /* COUNT_READ */
334         set_field_ulonglong(f, m_row.m_io_stat.m_read.m_waits.m_count);
335         break;
336       case  9: /* SUM_TIMER_READ */
337         set_field_ulonglong(f, m_row.m_io_stat.m_read.m_waits.m_sum);
338         break;
339       case 10: /* MIN_TIMER_READ */
340         set_field_ulonglong(f, m_row.m_io_stat.m_read.m_waits.m_min);
341         break;
342       case 11: /* AVG_TIMER_READ */
343         set_field_ulonglong(f, m_row.m_io_stat.m_read.m_waits.m_avg);
344         break;
345       case 12: /* MAX_TIMER_READ */
346         set_field_ulonglong(f, m_row.m_io_stat.m_read.m_waits.m_max);
347         break;
348       case 13: /* SUM_NUMBER_OF_BYTES_READ */
349         set_field_ulonglong(f, m_row.m_io_stat.m_read.m_bytes);
350         break;
351 
352       case 14: /* COUNT_WRITE */
353         set_field_ulonglong(f, m_row.m_io_stat.m_write.m_waits.m_count);
354         break;
355       case 15: /* SUM_TIMER_WRITE */
356         set_field_ulonglong(f, m_row.m_io_stat.m_write.m_waits.m_sum);
357         break;
358       case 16: /* MIN_TIMER_WRITE */
359         set_field_ulonglong(f, m_row.m_io_stat.m_write.m_waits.m_min);
360         break;
361       case 17: /* AVG_TIMER_WRITE */
362         set_field_ulonglong(f, m_row.m_io_stat.m_write.m_waits.m_avg);
363         break;
364       case 18: /* MAX_TIMER_WRITE */
365         set_field_ulonglong(f, m_row.m_io_stat.m_write.m_waits.m_max);
366         break;
367       case 19: /* SUM_NUMBER_OF_BYTES_WRITE */
368         set_field_ulonglong(f, m_row.m_io_stat.m_write.m_bytes);
369         break;
370 
371       case 20: /* COUNT_MISC */
372         set_field_ulonglong(f, m_row.m_io_stat.m_misc.m_waits.m_count);
373         break;
374       case 21: /* SUM_TIMER_MISC */
375         set_field_ulonglong(f, m_row.m_io_stat.m_misc.m_waits.m_sum);
376         break;
377       case 22: /* MIN_TIMER_MISC */
378         set_field_ulonglong(f, m_row.m_io_stat.m_misc.m_waits.m_min);
379         break;
380       case 23: /* AVG_TIMER_MISC */
381         set_field_ulonglong(f, m_row.m_io_stat.m_misc.m_waits.m_avg);
382         break;
383       case 24: /* MAX_TIMER_MISC */
384         set_field_ulonglong(f, m_row.m_io_stat.m_misc.m_waits.m_max);
385         break;
386       default:
387         assert(false);
388       }
389     }
390   }
391 
392   return 0;
393 }
394 
395