1 /* Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
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 as published by
5 the Free Software Foundation; version 2 of the License.
6
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software Foundation,
14 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
15
16 /**
17 @file storage/perfschema/table_performance_timers.cc
18 Table PERFORMANCE_TIMERS (implementation).
19 */
20
21 #include "my_global.h"
22 #include "my_pthread.h"
23 #include "table_performance_timers.h"
24 #include "pfs_timer.h"
25 #include "pfs_global.h"
26
27 THR_LOCK table_performance_timers::m_table_lock;
28
29 static const TABLE_FIELD_TYPE field_types[]=
30 {
31 {
32 { C_STRING_WITH_LEN("TIMER_NAME") },
33 { C_STRING_WITH_LEN("enum(\'CYCLE\',\'NANOSECOND\',\'MICROSECOND\',"
34 "\'MILLISECOND\',\'TICK\')") },
35 { NULL, 0}
36 },
37 {
38 { C_STRING_WITH_LEN("TIMER_FREQUENCY") },
39 { C_STRING_WITH_LEN("bigint(20)") },
40 { NULL, 0}
41 },
42 {
43 { C_STRING_WITH_LEN("TIMER_RESOLUTION") },
44 { C_STRING_WITH_LEN("bigint(20)") },
45 { NULL, 0}
46 },
47 {
48 { C_STRING_WITH_LEN("TIMER_OVERHEAD") },
49 { C_STRING_WITH_LEN("bigint(20)") },
50 { NULL, 0}
51 }
52 };
53
54 TABLE_FIELD_DEF
55 table_performance_timers::m_field_def=
56 { 4, field_types };
57
58 PFS_engine_table_share
59 table_performance_timers::m_share=
60 {
61 { C_STRING_WITH_LEN("performance_timers") },
62 &pfs_readonly_acl,
63 &table_performance_timers::create,
64 NULL, /* write_row */
65 NULL, /* delete_all_rows */
66 COUNT_TIMER_NAME, /* records */
67 sizeof(PFS_simple_index), /* ref length */
68 &m_table_lock,
69 &m_field_def,
70 false /* checked */
71 };
72
create(void)73 PFS_engine_table* table_performance_timers::create(void)
74 {
75 return new table_performance_timers();
76 }
77
table_performance_timers()78 table_performance_timers::table_performance_timers()
79 : PFS_engine_table(&m_share, &m_pos),
80 m_row(NULL), m_pos(0), m_next_pos(0)
81 {
82 int index;
83
84 index= (int)TIMER_NAME_CYCLE - FIRST_TIMER_NAME;
85 m_data[index].m_timer_name= TIMER_NAME_CYCLE;
86 m_data[index].m_info= pfs_timer_info.cycles;
87
88 index= (int)TIMER_NAME_NANOSEC - FIRST_TIMER_NAME;
89 m_data[index].m_timer_name= TIMER_NAME_NANOSEC;
90 m_data[index].m_info= pfs_timer_info.nanoseconds;
91
92 index= (int)TIMER_NAME_MICROSEC - FIRST_TIMER_NAME;
93 m_data[index].m_timer_name= TIMER_NAME_MICROSEC;
94 m_data[index].m_info= pfs_timer_info.microseconds;
95
96 index= (int)TIMER_NAME_MILLISEC - FIRST_TIMER_NAME;
97 m_data[index].m_timer_name= TIMER_NAME_MILLISEC;
98 m_data[index].m_info= pfs_timer_info.milliseconds;
99
100 index= (int)TIMER_NAME_TICK - FIRST_TIMER_NAME;
101 m_data[index].m_timer_name= TIMER_NAME_TICK;
102 m_data[index].m_info= pfs_timer_info.ticks;
103 }
104
reset_position(void)105 void table_performance_timers::reset_position(void)
106 {
107 m_pos.m_index= 0;
108 m_next_pos.m_index= 0;
109 }
110
rnd_next(void)111 int table_performance_timers::rnd_next(void)
112 {
113 int result;
114
115 m_pos.set_at(&m_next_pos);
116
117 if (m_pos.m_index < COUNT_TIMER_NAME)
118 {
119 m_row= &m_data[m_pos.m_index];
120 m_next_pos.set_after(&m_pos);
121 result= 0;
122 }
123 else
124 {
125 m_row= NULL;
126 result= HA_ERR_END_OF_FILE;
127 }
128
129 return result;
130 }
131
rnd_pos(const void * pos)132 int table_performance_timers::rnd_pos(const void *pos)
133 {
134 set_position(pos);
135 DBUG_ASSERT(m_pos.m_index < COUNT_TIMER_NAME);
136 m_row= &m_data[m_pos.m_index];
137 return 0;
138 }
139
read_row_values(TABLE * table,unsigned char * buf,Field ** fields,bool read_all)140 int table_performance_timers::read_row_values(TABLE *table,
141 unsigned char *buf,
142 Field **fields,
143 bool read_all)
144 {
145 Field *f;
146
147 DBUG_ASSERT(m_row);
148
149 /* Set the null bits */
150 DBUG_ASSERT(table->s->null_bytes == 1);
151 buf[0]= 0;
152
153 for (; (f= *fields) ; fields++)
154 {
155 if (read_all || bitmap_is_set(table->read_set, f->field_index))
156 {
157 switch(f->field_index)
158 {
159 case 0: /* TIMER_NAME */
160 set_field_enum(f, m_row->m_timer_name);
161 break;
162 case 1: /* TIMER_FREQUENCY */
163 if (m_row->m_info.routine != 0)
164 set_field_ulonglong(f, m_row->m_info.frequency);
165 else
166 f->set_null();
167 break;
168 case 2: /* TIMER_RESOLUTION */
169 if (m_row->m_info.routine != 0)
170 set_field_ulonglong(f, m_row->m_info.resolution);
171 else
172 f->set_null();
173 break;
174 case 3: /* TIMER_OVERHEAD */
175 if (m_row->m_info.routine != 0)
176 set_field_ulonglong(f, m_row->m_info.overhead);
177 else
178 f->set_null();
179 break;
180 default:
181 DBUG_ASSERT(false);
182 }
183 }
184 }
185
186 return 0;
187 }
188
189