MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
table_socket_instances.cc
Go to the documentation of this file.
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 
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 static const TABLE_FIELD_TYPE field_types[]=
32 {
33  {
34  { C_STRING_WITH_LEN("EVENT_NAME") },
35  { C_STRING_WITH_LEN("varchar(128)") },
36  { NULL, 0}
37  },
38  {
39  { C_STRING_WITH_LEN("OBJECT_INSTANCE_BEGIN") },
40  { C_STRING_WITH_LEN("bigint(20)") },
41  { NULL, 0}
42  },
43  {
44  { C_STRING_WITH_LEN("THREAD_ID") },
45  { C_STRING_WITH_LEN("bigint(20)") },
46  { NULL, 0}
47  },
48  {
49  { C_STRING_WITH_LEN("SOCKET_ID") },
50  { C_STRING_WITH_LEN("int(11)") },
51  { NULL, 0}
52  },
53  {
54  { C_STRING_WITH_LEN("IP") },
55  { C_STRING_WITH_LEN("varchar(64)") },
56  { NULL, 0}
57  },
58  {
59  { C_STRING_WITH_LEN("PORT") },
60  { C_STRING_WITH_LEN("int(11)") },
61  { NULL, 0}
62  },
63  {
64  { C_STRING_WITH_LEN("STATE") },
65  { C_STRING_WITH_LEN("enum('IDLE','ACTIVE')") },
66  { NULL, 0}
67  }
68 };
69 
71 table_socket_instances::m_field_def=
72 { 7, field_types };
73 
76 {
77  { C_STRING_WITH_LEN("socket_instances") },
79  &table_socket_instances::create,
80  NULL, /* write_row */
81  NULL, /* delete_all_rows */
82  NULL, /* get_row_count */
83  1000, /* records */
84  sizeof(PFS_simple_index),
85  &m_table_lock,
86  &m_field_def,
87  false /* checked */
88 };
89 
90 PFS_engine_table* table_socket_instances::create(void)
91 {
92  return new table_socket_instances();
93 }
94 
95 table_socket_instances::table_socket_instances()
96  : PFS_engine_table(&m_share, &m_pos),
97  m_row_exists(false), m_pos(0), m_next_pos(0)
98 {}
99 
101 {
102  m_pos.m_index= 0;
103  m_next_pos.m_index= 0;
104 }
105 
107 {
108  PFS_socket *pfs;
109 
110  for (m_pos.set_at(&m_next_pos);
111  m_pos.m_index < socket_max;
112  m_pos.next())
113  {
114  pfs= &socket_array[m_pos.m_index];
115  if (pfs->m_lock.is_populated())
116  {
117  make_row(pfs);
118  m_next_pos.set_after(&m_pos);
119  return 0;
120  }
121  }
122 
123  return HA_ERR_END_OF_FILE;
124 }
125 
127 {
128  PFS_socket *pfs;
129 
130  set_position(pos);
131  DBUG_ASSERT(m_pos.m_index < socket_max);
132  pfs= &socket_array[m_pos.m_index];
133 
134  if (! pfs->m_lock.is_populated())
135  return HA_ERR_RECORD_DELETED;
136 
137  make_row(pfs);
138  return 0;
139 }
140 
141 void table_socket_instances::make_row(PFS_socket *pfs)
142 {
143  pfs_lock lock;
144  PFS_socket_class *safe_class;
145 
146  m_row_exists= false;
147 
148  /* Protect this reader against a socket delete */
149  pfs->m_lock.begin_optimistic_lock(&lock);
150 
151  safe_class= sanitize_socket_class(pfs->m_class);
152  if (unlikely(safe_class == NULL))
153  return;
154 
156  m_row.m_ip_length= pfs_get_socket_address(m_row.m_ip, sizeof(m_row.m_ip),
157  &m_row.m_port,
158  &pfs->m_sock_addr, pfs->m_addr_len);
159  m_row.m_event_name= safe_class->m_name;
160  m_row.m_event_name_length= safe_class->m_name_length;
161  m_row.m_identity= pfs->m_identity;
162  m_row.m_fd= pfs->m_fd;
163  m_row.m_state= (pfs->m_idle ? PSI_SOCKET_STATE_IDLE
164  : PSI_SOCKET_STATE_ACTIVE);
165  PFS_thread *safe_thread= sanitize_thread(pfs->m_thread_owner);
166 
167  if (safe_thread != NULL)
168  {
169  m_row.m_thread_id= safe_thread->m_thread_internal_id;
170  m_row.m_thread_id_set= true;
171  }
172  else
173  m_row.m_thread_id_set= false;
174 
175 
176  if (pfs->m_lock.end_optimistic_lock(&lock))
177  m_row_exists= true;
178 }
179 
180 int table_socket_instances::read_row_values(TABLE *table,
181  unsigned char *buf,
182  Field **fields,
183  bool read_all)
184 {
185  Field *f;
186 
187  if (unlikely(!m_row_exists))
188  return HA_ERR_RECORD_DELETED;
189 
190  /* Set the null bits */
191  DBUG_ASSERT(table->s->null_bytes == 1);
192  buf[0]= 0;
193 
194  for (; (f= *fields) ; fields++)
195  {
196  if (read_all || bitmap_is_set(table->read_set, f->field_index))
197  {
198  switch(f->field_index)
199  {
200  case 0: /* EVENT_NAME */
202  break;
203  case 1: /* OBJECT_INSTANCE_BEGIN */
204  set_field_ulonglong(f, (intptr)m_row.m_identity);
205  break;
206  case 2: /* THREAD_ID */
207  if (m_row.m_thread_id_set)
209  else
210  f->set_null();
211  break;
212  case 3: /* SOCKET_ID */
213  set_field_ulong(f, m_row.m_fd);
214  break;
215  case 4: /* IP */
216  set_field_varchar_utf8(f, m_row.m_ip, m_row.m_ip_length);
217  break;
218  case 5: /* PORT */
219  set_field_ulong(f, m_row.m_port);
220  break;
221  case 6: /* STATE */
222  set_field_enum(f, m_row.m_state);
223  break;
224  default:
225  DBUG_ASSERT(false);
226  }
227  }
228  }
229 
230  return 0;
231 }
232