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_socket_instances.cc |
18 | Table SOCKET_INSTANCES (implementation). |
19 | */ |
20 | |
21 | #include "my_global.h" |
22 | #include "my_pthread.h" |
23 | #include "pfs_instr.h" |
24 | #include "pfs_column_types.h" |
25 | #include "pfs_column_values.h" |
26 | #include "table_socket_instances.h" |
27 | #include "pfs_global.h" |
28 | |
29 | THR_LOCK table_socket_instances::m_table_lock; |
30 | |
31 | PFS_engine_table_share |
32 | table_socket_instances::m_share= |
33 | { |
34 | { C_STRING_WITH_LEN("socket_instances" ) }, |
35 | &pfs_readonly_acl, |
36 | &table_socket_instances::create, |
37 | NULL, /* write_row */ |
38 | NULL, /* delete_all_rows */ |
39 | NULL, /* get_row_count */ |
40 | 1000, /* records */ |
41 | sizeof(PFS_simple_index), |
42 | &m_table_lock, |
43 | { C_STRING_WITH_LEN("CREATE TABLE socket_instances(" |
44 | "EVENT_NAME VARCHAR(128) not null," |
45 | "OBJECT_INSTANCE_BEGIN BIGINT unsigned not null," |
46 | "THREAD_ID BIGINT unsigned," |
47 | "SOCKET_ID INTEGER not null," |
48 | "IP VARCHAR(64) not null," |
49 | "PORT INTEGER not null," |
50 | "STATE ENUM('IDLE','ACTIVE') not null)" ) } |
51 | }; |
52 | |
53 | PFS_engine_table* table_socket_instances::create(void) |
54 | { |
55 | return new table_socket_instances(); |
56 | } |
57 | |
58 | table_socket_instances::table_socket_instances() |
59 | : PFS_engine_table(&m_share, &m_pos), |
60 | m_row_exists(false), m_pos(0), m_next_pos(0) |
61 | {} |
62 | |
63 | void table_socket_instances::reset_position(void) |
64 | { |
65 | m_pos.m_index= 0; |
66 | m_next_pos.m_index= 0; |
67 | } |
68 | |
69 | int table_socket_instances::rnd_next(void) |
70 | { |
71 | PFS_socket *pfs; |
72 | |
73 | for (m_pos.set_at(&m_next_pos); |
74 | m_pos.m_index < socket_max; |
75 | m_pos.next()) |
76 | { |
77 | pfs= &socket_array[m_pos.m_index]; |
78 | if (pfs->m_lock.is_populated()) |
79 | { |
80 | make_row(pfs); |
81 | m_next_pos.set_after(&m_pos); |
82 | return 0; |
83 | } |
84 | } |
85 | |
86 | return HA_ERR_END_OF_FILE; |
87 | } |
88 | |
89 | int table_socket_instances::rnd_pos(const void *pos) |
90 | { |
91 | PFS_socket *pfs; |
92 | |
93 | set_position(pos); |
94 | DBUG_ASSERT(m_pos.m_index < socket_max); |
95 | pfs= &socket_array[m_pos.m_index]; |
96 | |
97 | if (! pfs->m_lock.is_populated()) |
98 | return HA_ERR_RECORD_DELETED; |
99 | |
100 | make_row(pfs); |
101 | return 0; |
102 | } |
103 | |
104 | void table_socket_instances::make_row(PFS_socket *pfs) |
105 | { |
106 | pfs_lock lock; |
107 | PFS_socket_class *safe_class; |
108 | |
109 | m_row_exists= false; |
110 | |
111 | /* Protect this reader against a socket delete */ |
112 | pfs->m_lock.begin_optimistic_lock(&lock); |
113 | |
114 | safe_class= sanitize_socket_class(pfs->m_class); |
115 | if (unlikely(safe_class == NULL)) |
116 | return; |
117 | |
118 | /** Extract ip address and port from raw address */ |
119 | m_row.m_ip_length= pfs_get_socket_address(m_row.m_ip, sizeof(m_row.m_ip), |
120 | &m_row.m_port, |
121 | &pfs->m_sock_addr, pfs->m_addr_len); |
122 | m_row.m_event_name= safe_class->m_name; |
123 | m_row.m_event_name_length= safe_class->m_name_length; |
124 | m_row.m_identity= pfs->m_identity; |
125 | m_row.m_fd= pfs->m_fd; |
126 | m_row.m_state= (pfs->m_idle ? PSI_SOCKET_STATE_IDLE |
127 | : PSI_SOCKET_STATE_ACTIVE); |
128 | PFS_thread *safe_thread= sanitize_thread(pfs->m_thread_owner); |
129 | |
130 | if (safe_thread != NULL) |
131 | { |
132 | m_row.m_thread_id= safe_thread->m_thread_internal_id; |
133 | m_row.m_thread_id_set= true; |
134 | } |
135 | else |
136 | m_row.m_thread_id_set= false; |
137 | |
138 | |
139 | if (pfs->m_lock.end_optimistic_lock(&lock)) |
140 | m_row_exists= true; |
141 | } |
142 | |
143 | int table_socket_instances::read_row_values(TABLE *table, |
144 | unsigned char *buf, |
145 | Field **fields, |
146 | bool read_all) |
147 | { |
148 | Field *f; |
149 | |
150 | if (unlikely(!m_row_exists)) |
151 | return HA_ERR_RECORD_DELETED; |
152 | |
153 | /* Set the null bits */ |
154 | DBUG_ASSERT(table->s->null_bytes == 1); |
155 | buf[0]= 0; |
156 | |
157 | for (; (f= *fields) ; fields++) |
158 | { |
159 | if (read_all || bitmap_is_set(table->read_set, f->field_index)) |
160 | { |
161 | switch(f->field_index) |
162 | { |
163 | case 0: /* EVENT_NAME */ |
164 | set_field_varchar_utf8(f, m_row.m_event_name, m_row.m_event_name_length); |
165 | break; |
166 | case 1: /* OBJECT_INSTANCE_BEGIN */ |
167 | set_field_ulonglong(f, (intptr)m_row.m_identity); |
168 | break; |
169 | case 2: /* THREAD_ID */ |
170 | if (m_row.m_thread_id_set) |
171 | set_field_ulonglong(f, m_row.m_thread_id); |
172 | else |
173 | f->set_null(); |
174 | break; |
175 | case 3: /* SOCKET_ID */ |
176 | set_field_ulong(f, m_row.m_fd); |
177 | break; |
178 | case 4: /* IP */ |
179 | set_field_varchar_utf8(f, m_row.m_ip, m_row.m_ip_length); |
180 | break; |
181 | case 5: /* PORT */ |
182 | set_field_ulong(f, m_row.m_port); |
183 | break; |
184 | case 6: /* STATE */ |
185 | set_field_enum(f, m_row.m_state); |
186 | break; |
187 | default: |
188 | DBUG_ASSERT(false); |
189 | } |
190 | } |
191 | } |
192 | |
193 | return 0; |
194 | } |
195 | |
196 | |