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
27THR_LOCK table_performance_timers::m_table_lock;
28
29PFS_engine_table_share
30table_performance_timers::m_share=
31{
32 { C_STRING_WITH_LEN("performance_timers") },
33 &pfs_readonly_acl,
34 &table_performance_timers::create,
35 NULL, /* write_row */
36 NULL, /* delete_all_rows */
37 NULL, /* get_row_count */
38 COUNT_TIMER_NAME, /* records */
39 sizeof(PFS_simple_index), /* ref length */
40 &m_table_lock,
41 { C_STRING_WITH_LEN("CREATE TABLE performance_timers("
42 "TIMER_NAME ENUM ('CYCLE', 'NANOSECOND', 'MICROSECOND', 'MILLISECOND', 'TICK') not null,"
43 "TIMER_FREQUENCY BIGINT,"
44 "TIMER_RESOLUTION BIGINT,"
45 "TIMER_OVERHEAD BIGINT)") }
46};
47
48PFS_engine_table* table_performance_timers::create(void)
49{
50 return new table_performance_timers();
51}
52
53table_performance_timers::table_performance_timers()
54 : PFS_engine_table(&m_share, &m_pos),
55 m_row(NULL), m_pos(0), m_next_pos(0)
56{
57 int index;
58
59 index= (int)TIMER_NAME_CYCLE - FIRST_TIMER_NAME;
60 m_data[index].m_timer_name= TIMER_NAME_CYCLE;
61 m_data[index].m_info= pfs_timer_info.cycles;
62
63 index= (int)TIMER_NAME_NANOSEC - FIRST_TIMER_NAME;
64 m_data[index].m_timer_name= TIMER_NAME_NANOSEC;
65 m_data[index].m_info= pfs_timer_info.nanoseconds;
66
67 index= (int)TIMER_NAME_MICROSEC - FIRST_TIMER_NAME;
68 m_data[index].m_timer_name= TIMER_NAME_MICROSEC;
69 m_data[index].m_info= pfs_timer_info.microseconds;
70
71 index= (int)TIMER_NAME_MILLISEC - FIRST_TIMER_NAME;
72 m_data[index].m_timer_name= TIMER_NAME_MILLISEC;
73 m_data[index].m_info= pfs_timer_info.milliseconds;
74
75 index= (int)TIMER_NAME_TICK - FIRST_TIMER_NAME;
76 m_data[index].m_timer_name= TIMER_NAME_TICK;
77 m_data[index].m_info= pfs_timer_info.ticks;
78}
79
80void table_performance_timers::reset_position(void)
81{
82 m_pos.m_index= 0;
83 m_next_pos.m_index= 0;
84}
85
86int table_performance_timers::rnd_next(void)
87{
88 int result;
89
90 m_pos.set_at(&m_next_pos);
91
92 if (m_pos.m_index < COUNT_TIMER_NAME)
93 {
94 m_row= &m_data[m_pos.m_index];
95 m_next_pos.set_after(&m_pos);
96 result= 0;
97 }
98 else
99 {
100 m_row= NULL;
101 result= HA_ERR_END_OF_FILE;
102 }
103
104 return result;
105}
106
107int table_performance_timers::rnd_pos(const void *pos)
108{
109 set_position(pos);
110 DBUG_ASSERT(m_pos.m_index < COUNT_TIMER_NAME);
111 m_row= &m_data[m_pos.m_index];
112 return 0;
113}
114
115int table_performance_timers::read_row_values(TABLE *table,
116 unsigned char *buf,
117 Field **fields,
118 bool read_all)
119{
120 Field *f;
121
122 DBUG_ASSERT(m_row);
123
124 /* Set the null bits */
125 DBUG_ASSERT(table->s->null_bytes == 1);
126 buf[0]= 0;
127
128 for (; (f= *fields) ; fields++)
129 {
130 if (read_all || bitmap_is_set(table->read_set, f->field_index))
131 {
132 switch(f->field_index)
133 {
134 case 0: /* TIMER_NAME */
135 set_field_enum(f, m_row->m_timer_name);
136 break;
137 case 1: /* TIMER_FREQUENCY */
138 if (m_row->m_info.routine != 0)
139 set_field_ulonglong(f, m_row->m_info.frequency);
140 else
141 f->set_null();
142 break;
143 case 2: /* TIMER_RESOLUTION */
144 if (m_row->m_info.routine != 0)
145 set_field_ulonglong(f, m_row->m_info.resolution);
146 else
147 f->set_null();
148 break;
149 case 3: /* TIMER_OVERHEAD */
150 if (m_row->m_info.routine != 0)
151 set_field_ulonglong(f, m_row->m_info.overhead);
152 else
153 f->set_null();
154 break;
155 default:
156 DBUG_ASSERT(false);
157 }
158 }
159 }
160
161 return 0;
162}
163
164