~jaypipes/drizzle/split-xa-resource-manager

« back to all changes in this revision

Viewing changes to plugin/info_schema/processlist.cc

  • Committer: Jay Pipes
  • Date: 2010-02-14 20:26:43 UTC
  • mfrom: (1273.1.27 staging)
  • Revision ID: jpipes@serialcoder-20100214202643-ahuqvc8rhn8u7y33
Merge trunk and resolve conflicts

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2009 Sun Microsystems
5
 
 *
6
 
 *  This program is free software; you can redistribute it and/or modify
7
 
 *  it under the terms of the GNU General Public License as published by
8
 
 *  the Free Software Foundation; either version 2 of the License, or
9
 
 *  (at your option) any later version.
10
 
 *
11
 
 *  This program is distributed in the hope that it will be useful,
12
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 *  GNU General Public License for more details.
15
 
 *
16
 
 *  You should have received a copy of the GNU General Public License
17
 
 *  along with this program; if not, write to the Free Software
18
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
 
 */
20
 
 
21
 
/**
22
 
 * @file 
23
 
 *   proceslist I_S table methods.
24
 
 */
25
 
 
26
 
#include "config.h"
27
 
#include "drizzled/session.h"
28
 
#include "drizzled/show.h"
29
 
#include "drizzled/plugin/client.h"
30
 
#include "drizzled/session_list.h"
31
 
#include "drizzled/pthread_globals.h"
32
 
#include "drizzled/internal/my_sys.h"
33
 
 
34
 
#include "helper_methods.h"
35
 
#include "processlist.h"
36
 
 
37
 
#include <vector>
38
 
 
39
 
#define LIST_PROCESS_HOST_LEN 64
40
 
 
41
 
using namespace drizzled;
42
 
using namespace std;
43
 
 
44
 
/*
45
 
 * Vectors of columns for the processlist I_S table.
46
 
 */
47
 
static vector<const plugin::ColumnInfo *> *columns= NULL;
48
 
 
49
 
/*
50
 
 * Methods for the processlist I_S table.
51
 
 */
52
 
static plugin::InfoSchemaMethods *methods= NULL;
53
 
 
54
 
/*
55
 
 * processlist I_S table.
56
 
 */
57
 
static plugin::InfoSchemaTable *pl_table= NULL;
58
 
 
59
 
/**
60
 
 * Populate the vectors of columns for the I_S table.
61
 
 *
62
 
 * @return a pointer to a std::vector of Columns.
63
 
 */
64
 
vector<const plugin::ColumnInfo *> *ProcessListIS::createColumns()
65
 
{
66
 
  if (columns == NULL)
67
 
  {
68
 
    columns= new vector<const plugin::ColumnInfo *>;
69
 
  }
70
 
  else
71
 
  {
72
 
    clearColumns(*columns);
73
 
  }
74
 
 
75
 
  /*
76
 
   * Create each column for the PROCESSLIST table.
77
 
   */
78
 
  columns->push_back(new plugin::ColumnInfo("ID", 
79
 
                                            4,
80
 
                                            DRIZZLE_TYPE_LONGLONG,
81
 
                                            0,
82
 
                                            0,
83
 
                                            "Id"));
84
 
 
85
 
  columns->push_back(new plugin::ColumnInfo("USER",
86
 
                                            16,
87
 
                                            DRIZZLE_TYPE_VARCHAR,
88
 
                                            0,
89
 
                                            0,
90
 
                                            "User"));
91
 
 
92
 
  columns->push_back(new plugin::ColumnInfo("HOST",
93
 
                                            LIST_PROCESS_HOST_LEN,
94
 
                                            DRIZZLE_TYPE_VARCHAR,
95
 
                                            0,
96
 
                                            0,
97
 
                                            "Host"));
98
 
 
99
 
  columns->push_back(new plugin::ColumnInfo("DB",
100
 
                                            NAME_CHAR_LEN,
101
 
                                            DRIZZLE_TYPE_VARCHAR,
102
 
                                            0,
103
 
                                            1,
104
 
                                            "Db"));
105
 
 
106
 
  columns->push_back(new plugin::ColumnInfo("COMMAND",
107
 
                                            16,
108
 
                                            DRIZZLE_TYPE_VARCHAR,
109
 
                                            0,
110
 
                                            0,
111
 
                                            "Command"));
112
 
 
113
 
  columns->push_back(new plugin::ColumnInfo("TIME",
114
 
                                            7,
115
 
                                            DRIZZLE_TYPE_LONGLONG,
116
 
                                            0,
117
 
                                            0,
118
 
                                            "Time"));
119
 
 
120
 
  columns->push_back(new plugin::ColumnInfo("STATE",
121
 
                                            64,
122
 
                                            DRIZZLE_TYPE_VARCHAR,
123
 
                                            0,
124
 
                                            1,
125
 
                                            "State"));
126
 
 
127
 
  columns->push_back(new plugin::ColumnInfo("INFO",
128
 
                                            16383,
129
 
                                            DRIZZLE_TYPE_VARCHAR,
130
 
                                            0,
131
 
                                            1,
132
 
                                            "Info"));
133
 
 
134
 
  return columns;
135
 
}
136
 
 
137
 
/**
138
 
 * Initialize the I_S table.
139
 
 *
140
 
 * @return a pointer to an I_S table
141
 
 */
142
 
plugin::InfoSchemaTable *ProcessListIS::getTable()
143
 
{
144
 
  columns= createColumns();
145
 
 
146
 
  if (methods == NULL)
147
 
  {
148
 
    methods= new ProcessListISMethods();
149
 
  }
150
 
 
151
 
  if (pl_table == NULL)
152
 
  {
153
 
    pl_table= new plugin::InfoSchemaTable("PROCESSLIST",
154
 
                                          *columns,
155
 
                                          -1, -1, false, false, 0,
156
 
                                          methods);
157
 
  }
158
 
 
159
 
  return pl_table;
160
 
}
161
 
 
162
 
/**
163
 
 * Delete memory allocated for the table, columns and methods.
164
 
 */
165
 
void ProcessListIS::cleanup()
166
 
{
167
 
  clearColumns(*columns);
168
 
  delete pl_table;
169
 
  delete methods;
170
 
  delete columns;
171
 
}
172
 
 
173
 
int ProcessListISMethods::fillTable(Session* session, 
174
 
                                    Table *table,
175
 
                                    plugin::InfoSchemaTable *schema_table)
176
 
{
177
 
  const CHARSET_INFO * const cs= system_charset_info;
178
 
  time_t now= time(NULL);
179
 
  size_t length;
180
 
 
181
 
  if (now == (time_t)-1)
182
 
    return 1;
183
 
 
184
 
  pthread_mutex_lock(&LOCK_thread_count);
185
 
 
186
 
  if (! session->killed)
187
 
  {
188
 
    Session* tmp;
189
 
 
190
 
    for (vector<Session*>::iterator it= getSessionList().begin(); it != getSessionList().end(); ++it)
191
 
    {
192
 
      tmp= *it;
193
 
      Security_context *tmp_sctx= &tmp->security_ctx;
194
 
      internal::st_my_thread_var *mysys_var;
195
 
      const char *val;
196
 
 
197
 
      if (! tmp->client->isConnected())
198
 
        continue;
199
 
 
200
 
      table->restoreRecordAsDefault();
201
 
      table->setWriteSet(0);
202
 
      table->setWriteSet(1);
203
 
      table->setWriteSet(2);
204
 
      table->setWriteSet(3);
205
 
      table->setWriteSet(4);
206
 
      table->setWriteSet(5);
207
 
      table->setWriteSet(6);
208
 
      table->setWriteSet(7);
209
 
      /* ID */
210
 
      table->field[0]->store((int64_t) tmp->thread_id, true);
211
 
      /* USER */
212
 
      val= tmp_sctx->user.c_str() ? tmp_sctx->user.c_str() : "unauthenticated user";
213
 
      table->field[1]->store(val, strlen(val), cs);
214
 
      /* HOST */
215
 
      table->field[2]->store(tmp_sctx->ip.c_str(), strlen(tmp_sctx->ip.c_str()), cs);
216
 
      /* DB */
217
 
      if (! tmp->db.empty())
218
 
      {
219
 
        table->field[3]->store(tmp->db.c_str(), tmp->db.length(), cs);
220
 
        table->field[3]->set_notnull();
221
 
      }
222
 
 
223
 
      if ((mysys_var= tmp->mysys_var))
224
 
        pthread_mutex_lock(&mysys_var->mutex);
225
 
      /* COMMAND */
226
 
      if ((val= (char *) (tmp->killed == Session::KILL_CONNECTION? "Killed" : 0)))
227
 
        table->field[4]->store(val, strlen(val), cs);
228
 
      else
229
 
        table->field[4]->store(command_name[tmp->command].str,
230
 
                               command_name[tmp->command].length, cs);
231
 
      /* DRIZZLE_TIME */
232
 
      table->field[5]->store((uint32_t)(tmp->start_time ?
233
 
                                      now - tmp->start_time : 0), true);
234
 
      /* STATE */
235
 
      val= (char*) (tmp->client->isWriting() ?
236
 
                    "Writing to net" :
237
 
                    tmp->client->isReading() ?
238
 
                    (tmp->command == COM_SLEEP ?
239
 
                     NULL : "Reading from net") :
240
 
                    tmp->get_proc_info() ? tmp->get_proc_info() :
241
 
                    tmp->mysys_var &&
242
 
                    tmp->mysys_var->current_cond ?
243
 
                    "Waiting on cond" : NULL);
244
 
      if (val)
245
 
      {
246
 
        table->field[6]->store(val, strlen(val), cs);
247
 
        table->field[6]->set_notnull();
248
 
      }
249
 
 
250
 
      if (mysys_var)
251
 
        pthread_mutex_unlock(&mysys_var->mutex);
252
 
 
253
 
      length= strlen(tmp->process_list_info);
254
 
 
255
 
      if (length)
256
 
      {
257
 
        table->field[7]->store(tmp->process_list_info, length, cs);
258
 
        table->field[7]->set_notnull();
259
 
      }
260
 
 
261
 
      schema_table->addRow(table->record[0], table->s->reclength);
262
 
    }
263
 
  }
264
 
 
265
 
  pthread_mutex_unlock(&LOCK_thread_count);
266
 
  return 0;
267
 
}
268