1 /* Copyright (c) 2008, 2011, 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, 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, Fifth Floor, Boston, MA 02110-1335 USA */
22 
23 /**
24   @file storage/perfschema/table_setup_timers.cc
25   Table SETUP_TIMERS (implementation).
26 */
27 
28 #include "my_global.h"
29 #include "my_pthread.h"
30 #include "table_setup_timers.h"
31 #include "pfs_column_values.h"
32 #include "pfs_timer.h"
33 
34 #define COUNT_SETUP_TIMERS 4
35 
36 static row_setup_timers all_setup_timers_data[COUNT_SETUP_TIMERS]=
37 {
38   {
39     { C_STRING_WITH_LEN("idle") },
40     &idle_timer
41   },
42   {
43     { C_STRING_WITH_LEN("wait") },
44     &wait_timer
45   },
46   {
47     { C_STRING_WITH_LEN("stage") },
48     &stage_timer
49   },
50   {
51     { C_STRING_WITH_LEN("statement") },
52     &statement_timer
53   }
54 };
55 
56 THR_LOCK table_setup_timers::m_table_lock;
57 
58 PFS_engine_table_share
59 table_setup_timers::m_share=
60 {
61   { C_STRING_WITH_LEN("setup_timers") },
62   &pfs_updatable_acl,
63   &table_setup_timers::create,
64   NULL, /* write_row */
65   NULL, /* delete_all_rows */
66   NULL, /* get_row_count */
67   COUNT_SETUP_TIMERS,
68   sizeof(PFS_simple_index),
69   &m_table_lock,
70   { C_STRING_WITH_LEN("CREATE TABLE setup_timers("
71                       "NAME VARCHAR(64) not null comment 'Type of instrument the timer is used for.',"
72                       "TIMER_NAME ENUM ('CYCLE', 'NANOSECOND', 'MICROSECOND', 'MILLISECOND', 'TICK') not null comment 'Timer applying to the instrument type. Can be modified.')") }
73 };
74 
create(void)75 PFS_engine_table* table_setup_timers::create(void)
76 {
77   return new table_setup_timers();
78 }
79 
table_setup_timers()80 table_setup_timers::table_setup_timers()
81   : PFS_engine_table(&m_share, &m_pos),
82     m_row(NULL), m_pos(0), m_next_pos(0)
83 {}
84 
reset_position(void)85 void table_setup_timers::reset_position(void)
86 {
87   m_pos.m_index= 0;
88   m_next_pos.m_index= 0;
89 }
90 
rnd_next(void)91 int table_setup_timers::rnd_next(void)
92 {
93   int result;
94 
95   m_pos.set_at(&m_next_pos);
96 
97   if (m_pos.m_index < COUNT_SETUP_TIMERS)
98   {
99     m_row= &all_setup_timers_data[m_pos.m_index];
100     m_next_pos.set_after(&m_pos);
101     result= 0;
102   }
103   else
104   {
105     m_row= NULL;
106     result= HA_ERR_END_OF_FILE;
107   }
108 
109   return result;
110 }
111 
rnd_pos(const void * pos)112 int table_setup_timers::rnd_pos(const void *pos)
113 {
114   set_position(pos);
115   DBUG_ASSERT(m_pos.m_index < COUNT_SETUP_TIMERS);
116   m_row= &all_setup_timers_data[m_pos.m_index];
117   return 0;
118 }
119 
read_row_values(TABLE * table,unsigned char *,Field ** fields,bool read_all)120 int table_setup_timers::read_row_values(TABLE *table,
121                                         unsigned char *,
122                                         Field **fields,
123                                         bool read_all)
124 {
125   Field *f;
126 
127   DBUG_ASSERT(m_row);
128 
129   /* Set the null bits */
130   DBUG_ASSERT(table->s->null_bytes == 0);
131 
132   for (; (f= *fields) ; fields++)
133   {
134     if (read_all || bitmap_is_set(table->read_set, f->field_index))
135     {
136       switch(f->field_index)
137       {
138       case 0: /* NAME */
139         set_field_varchar_utf8(f, m_row->m_name.str,(uint) m_row->m_name.length);
140         break;
141       case 1: /* TIMER_NAME */
142         set_field_enum(f, *(m_row->m_timer_name_ptr));
143         break;
144       default:
145         DBUG_ASSERT(false);
146       }
147     }
148   }
149 
150   return 0;
151 }
152 
update_row_values(TABLE * table,const unsigned char *,const unsigned char *,Field ** fields)153 int table_setup_timers::update_row_values(TABLE *table,
154                                           const unsigned char *,
155                                           const unsigned char *,
156                                           Field **fields)
157 {
158   Field *f;
159   longlong value;
160 
161   DBUG_ASSERT(m_row);
162 
163   for (; (f= *fields) ; fields++)
164   {
165     if (bitmap_is_set(table->write_set, f->field_index))
166     {
167       switch(f->field_index)
168       {
169       case 0: /* NAME */
170         return HA_ERR_WRONG_COMMAND;
171       case 1: /* TIMER_NAME */
172         value= get_field_enum(f);
173         if ((value >= FIRST_TIMER_NAME) && (value <= LAST_TIMER_NAME))
174           *(m_row->m_timer_name_ptr)= (enum_timer_name) value;
175         else
176           return HA_ERR_WRONG_COMMAND;
177         break;
178       default:
179         DBUG_ASSERT(false);
180       }
181     }
182   }
183 
184   return 0;
185 }
186 
187