~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/logging_stats/logging_stats.cc

  • Committer: Brian Aker
  • Date: 2010-03-31 19:14:14 UTC
  • Revision ID: brian@gaz-20100331191414-9yv44mmpvf0tb7l1
Updated to use show schemas specific table.

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
/**
31
31
 * @details
32
32
 *
33
 
 * This plugin tracks session and global statistics, as well as user statistics. 
34
 
 * The commands are logged using the post() and postEnd() logging APIs. 
35
 
 * The statistics are stored in a Scoreboard where each active session owns a 
36
 
 * ScoreboardSlot during the sessions active lifetime. 
37
 
 * 
38
 
 * Scoreboard
39
 
 *
40
 
 * The scoreboard is a pre-allocated vector of vectors of ScoreboardSlots. It 
41
 
 * can be thought of as a vector of buckets where each bucket contains 
42
 
 * pre-allocated ScoreboardSlots. To determine which bucket gets used for 
43
 
 * recording statistics the modulus operator is used on the session_id. This 
44
 
 * will result in a bucket to search for a unused ScoreboardSlot. Once a 
45
 
 * ScoreboardSlot is found the index of the slot is stored in the Session 
46
 
 * for later use. 
 
33
 * This tracks current user commands. The commands are logged using
 
34
 * the post() and postEnd() logging APIs. It uses a scoreboard
 
35
 * approach that initializes the scoreboard size to the value set
 
36
 * by logging_stats_scoreboard_size. Each ScoreBoardSlot wraps 
 
37
 * a UserCommand object containing the statistics for a particular
 
38
 * session. As other statistics are added they can then be added
 
39
 * to the ScoreBoardSlot object. 
47
40
 *
48
41
 * Locking  
49
 
 * 
50
 
 * Each vector in the Scoreboard has its own lock. This allows session 2 
51
 
 * to not have to wait for session 1 to locate a slot to use, as they
52
 
 * will be in different buckets.  A lock is taken to locate a open slot
53
 
 * in the scoreboard. Subsequent queries by the session will not take
54
 
 * a lock.  
55
 
 *
56
 
 * A read lock is taken on the scoreboard vector when the table is queried 
57
 
 * in the data_dictionary. The "show status" and "show global status" do
58
 
 * not take a read lock when the data_dictionary table is queried, the 
59
 
 * user is not displayed in these results so it is not necessary. 
60
 
 *
61
 
 * Atomics
62
 
 *
63
 
 * The cumulative statistics use atomics, for the index into the vector
64
 
 * marking the last index that is used by a user. New users will increment
65
 
 * the atomic and claim the slot for use.  
66
 
 * 
67
 
 * System Variables
68
 
 * 
69
 
 * logging_stats_scoreboard_size - the size of the scoreboard this corresponds
70
 
 *   to the maximum number of concurrent connections that can be tracked
71
 
 *
72
 
 * logging_stats_max_user_count - this is used for cumulative statistics it 
73
 
 *   represents the maximum users that can be tracked 
74
 
 * 
75
 
 * logging_stats_bucket_count - the number of buckets to have in the scoreboard
76
 
 *   this splits up locking across several buckets so the entire scoreboard is 
77
 
 *   not locked at a single point in time.
78
 
 * 
79
 
 * logging_stats_enabled - enable/disable plugin 
 
42
 *
 
43
 * A RW lock is taken to locate a open slot for a session or to locate the
 
44
 * slot that the current session has claimed. 
 
45
 * 
 
46
 * A read lock is taken when the table is queried in the data_dictionary.
80
47
 * 
81
48
 * TODO 
82
49
 *
83
 
 * Allow expansion of Scoreboard and cumulative vector 
 
50
 * To improve as more statistics are added, a pointer to the scoreboard
 
51
 * slot should be added to the Session object. This will avoid the session
 
52
 * having to do multiple lookups in the scoreboard.  
 
53
 *  
 
54
 * Save the statistics off into a vector so you can query by user/ip and get
 
55
 * commands run based on those keys over time. 
84
56
 * 
85
57
 */
86
58
 
87
59
#include "config.h"
88
 
#include "user_commands.h"
89
 
#include "status_vars.h"
90
 
#include "global_stats.h"
91
60
#include "logging_stats.h"
92
 
#include "status_tool.h"
93
61
#include "stats_schema.h"
94
 
#include <boost/program_options.hpp>
95
 
#include <drizzled/module/option_map.h>
96
62
#include <drizzled/session.h>
97
63
 
98
 
namespace po= boost::program_options;
99
64
using namespace drizzled;
100
65
using namespace plugin;
101
66
using namespace std;
102
67
 
103
 
static bool sysvar_logging_stats_enabled= true;
104
 
 
105
 
typedef constrained_check<uint32_t, 50000, 10> scoreboard_size_constraint;
106
 
static scoreboard_size_constraint sysvar_logging_stats_scoreboard_size;
107
 
 
108
 
typedef constrained_check<uint32_t, 50000, 100> max_user_count_constraint;
109
 
static max_user_count_constraint sysvar_logging_stats_max_user_count;
110
 
 
111
 
typedef constrained_check<uint32_t, 500, 5> bucket_count_constraint;
112
 
static bucket_count_constraint sysvar_logging_stats_bucket_count;
 
68
static bool sysvar_logging_stats_enabled= false;
 
69
 
 
70
static uint32_t sysvar_logging_stats_scoreboard_size= 2000;
 
71
 
 
72
pthread_rwlock_t LOCK_scoreboard;
113
73
 
114
74
LoggingStats::LoggingStats(string name_arg) : Logging(name_arg)
115
75
{
116
 
  current_scoreboard= new Scoreboard(sysvar_logging_stats_scoreboard_size, 
117
 
                                     sysvar_logging_stats_bucket_count);
118
 
 
119
 
  cumulative_stats= new CumulativeStats(sysvar_logging_stats_max_user_count); 
 
76
  scoreboard_size= sysvar_logging_stats_scoreboard_size;
 
77
  score_board_slots= new ScoreBoardSlot[scoreboard_size];
 
78
  for (uint32_t j=0; j < scoreboard_size; j++)
 
79
  { 
 
80
    UserCommands *user_commands= new UserCommands();
 
81
    ScoreBoardSlot *score_board_slot= &score_board_slots[j];
 
82
    score_board_slot->setUserCommands(user_commands); 
 
83
  } 
120
84
}
121
85
 
122
86
LoggingStats::~LoggingStats()
123
87
{
124
 
  delete current_scoreboard;
125
 
  delete cumulative_stats;
126
 
}
127
 
 
128
 
void LoggingStats::updateCurrentScoreboard(ScoreboardSlot *scoreboard_slot,
129
 
                                           Session *session)
130
 
{
131
 
  enum_sql_command sql_command= session->lex->sql_command;
132
 
 
133
 
  scoreboard_slot->getUserCommands()->logCommand(sql_command);
134
 
 
135
 
  /* If a flush occurred copy over values before setting new values */
136
 
  if (scoreboard_slot->getStatusVars()->hasBeenFlushed(session))
137
 
  {
138
 
    cumulative_stats->logGlobalStatusVars(scoreboard_slot);
139
 
  }
140
 
  scoreboard_slot->getStatusVars()->logStatusVar(session);
141
 
}
142
 
 
143
 
bool LoggingStats::resetGlobalScoreboard()
144
 
{
145
 
  cumulative_stats->getGlobalStatusVars()->reset();
146
 
  cumulative_stats->getGlobalStats()->getUserCommands()->reset();
147
 
 
148
 
  ScoreBoardVectors *vector_of_scoreboard_vectors=
149
 
    current_scoreboard->getVectorOfScoreboardVectors();
150
 
 
151
 
  ScoreBoardVectors::iterator v_of_scoreboard_v_begin_it= vector_of_scoreboard_vectors->begin();
152
 
 
153
 
  ScoreBoardVectors::iterator v_of_scoreboard_v_end_it= vector_of_scoreboard_vectors->end();
154
 
 
155
 
  for (; v_of_scoreboard_v_begin_it != v_of_scoreboard_v_end_it; ++v_of_scoreboard_v_begin_it)
156
 
  {
157
 
    std::vector<ScoreboardSlot* > *scoreboard_vector= *v_of_scoreboard_v_begin_it;
158
 
 
159
 
    std::vector<ScoreboardSlot* >::iterator scoreboard_vector_it= scoreboard_vector->begin();
160
 
    std::vector<ScoreboardSlot* >::iterator scoreboard_vector_end= scoreboard_vector->end();
161
 
    for (; scoreboard_vector_it != scoreboard_vector_end; ++scoreboard_vector_it)
162
 
    {
163
 
      ScoreboardSlot *scoreboard_slot= *scoreboard_vector_it;
164
 
      scoreboard_slot->getStatusVars()->reset();
165
 
      scoreboard_slot->getUserCommands()->reset();
166
 
    }
167
 
  }
168
 
 
169
 
  return false;
 
88
  delete[] score_board_slots;
 
89
}
 
90
 
 
91
bool LoggingStats::isBeingLogged(Session *session)
 
92
{
 
93
  enum_sql_command sql_command= session->lex->sql_command;
 
94
 
 
95
  switch(sql_command)
 
96
  {
 
97
    case SQLCOM_UPDATE:
 
98
    case SQLCOM_DELETE:
 
99
    case SQLCOM_INSERT:
 
100
    case SQLCOM_ROLLBACK:
 
101
    case SQLCOM_COMMIT:
 
102
    case SQLCOM_CREATE_TABLE:
 
103
    case SQLCOM_ALTER_TABLE:
 
104
    case SQLCOM_DROP_TABLE:
 
105
    case SQLCOM_SELECT:
 
106
      return true;
 
107
    default:
 
108
      return false;
 
109
  }
 
110
 
111
 
 
112
void LoggingStats::updateScoreBoard(ScoreBoardSlot *score_board_slot,
 
113
                                    Session *session)
 
114
{
 
115
  enum_sql_command sql_command= session->lex->sql_command;
 
116
 
 
117
  UserCommands *user_commands= score_board_slot->getUserCommands();
 
118
 
 
119
  switch(sql_command)
 
120
  {
 
121
    case SQLCOM_UPDATE:
 
122
      user_commands->incrementUpdateCount();
 
123
      break;
 
124
    case SQLCOM_DELETE:
 
125
      user_commands->incrementDeleteCount();
 
126
      break;
 
127
    case SQLCOM_INSERT:
 
128
      user_commands->incrementInsertCount();
 
129
      break;
 
130
    case SQLCOM_ROLLBACK:
 
131
      user_commands->incrementRollbackCount();
 
132
      break;
 
133
    case SQLCOM_COMMIT:
 
134
      user_commands->incrementCommitCount();
 
135
      break;
 
136
    case SQLCOM_CREATE_TABLE:
 
137
      user_commands->incrementCreateCount();
 
138
      break;
 
139
    case SQLCOM_ALTER_TABLE:
 
140
      user_commands->incrementAlterCount();
 
141
      break;
 
142
    case SQLCOM_DROP_TABLE:
 
143
      user_commands->incrementDropCount();
 
144
      break;
 
145
    case SQLCOM_SELECT:
 
146
      user_commands->incrementSelectCount();
 
147
      break;
 
148
    default:
 
149
      return;
 
150
  }
170
151
}
171
152
 
172
153
bool LoggingStats::post(Session *session)
173
154
{
174
 
  if (! isEnabled() || (session->getSessionId() == 0))
175
 
  {
176
 
    return false;
177
 
  }
178
 
 
179
 
  ScoreboardSlot *scoreboard_slot= current_scoreboard->findScoreboardSlotToLog(session);
180
 
 
181
 
  /* Its possible that the scoreboard is full with active sessions in which case 
182
 
     this could be null */
183
 
  if (scoreboard_slot)
184
 
  {
185
 
    updateCurrentScoreboard(scoreboard_slot, session);
186
 
  }
 
155
  if (! isEnabled())
 
156
  {
 
157
    return false;
 
158
  }
 
159
 
 
160
  /* exit early if we are not logging this type of command */
 
161
  if (isBeingLogged(session) == false)
 
162
  {
 
163
    return false;
 
164
  }
 
165
 
 
166
  /* Find a slot that is unused */
 
167
 
 
168
  pthread_rwlock_wrlock(&LOCK_scoreboard);
 
169
  ScoreBoardSlot *score_board_slot;
 
170
  int our_slot= UNINITIALIZED; 
 
171
  int open_slot= UNINITIALIZED;
 
172
 
 
173
  for (uint32_t j=0; j < scoreboard_size; j++)
 
174
  {
 
175
    score_board_slot= &score_board_slots[j];
 
176
 
 
177
    if (score_board_slot->isInUse() == true)
 
178
    {
 
179
      /* Check if this session is the one using this slot */
 
180
      if (score_board_slot->getSessionId() == session->getSessionId())
 
181
      {
 
182
        our_slot= j;
 
183
        break; 
 
184
      } 
 
185
      else 
 
186
      {
 
187
        continue; 
 
188
      }
 
189
    }
 
190
    else 
 
191
    {
 
192
      /* save off the open slot */ 
 
193
      if (open_slot == -1)
 
194
      {
 
195
        open_slot= j;
 
196
      } 
 
197
      continue;
 
198
    }
 
199
  }
 
200
 
 
201
  if (our_slot != UNINITIALIZED)
 
202
  {
 
203
    pthread_rwlock_unlock(&LOCK_scoreboard); 
 
204
  }
 
205
  else if (open_slot != UNINITIALIZED)
 
206
  {
 
207
    score_board_slot= &score_board_slots[open_slot];
 
208
    score_board_slot->setInUse(true);
 
209
    score_board_slot->setSessionId(session->getSessionId());
 
210
    score_board_slot->setUser(session->getSecurityContext().getUser());
 
211
    score_board_slot->setIp(session->getSecurityContext().getIp());
 
212
    pthread_rwlock_unlock(&LOCK_scoreboard);
 
213
  }
 
214
  else 
 
215
  {
 
216
    pthread_rwlock_unlock(&LOCK_scoreboard);
 
217
    /* there was no available slot for this session */
 
218
    return false;
 
219
  }
 
220
 
 
221
  updateScoreBoard(score_board_slot, session);
 
222
 
187
223
  return false;
188
224
}
189
225
 
190
226
bool LoggingStats::postEnd(Session *session)
191
227
{
192
 
  if (! isEnabled() || (session->getSessionId() == 0))
 
228
  if (! isEnabled())
193
229
  {
194
230
    return false;
195
231
  }
196
232
 
197
 
  bool isInScoreboard= false;
198
 
  ScoreboardSlot *scoreboard_slot= current_scoreboard->findOurScoreboardSlot(session);
199
 
 
200
 
  if (scoreboard_slot)
 
233
  ScoreBoardSlot *score_board_slot;
 
234
 
 
235
  pthread_rwlock_wrlock(&LOCK_scoreboard);
 
236
 
 
237
  for (uint32_t j=0; j < scoreboard_size; j++)
201
238
  {
202
 
    isInScoreboard= true;
203
 
  } 
204
 
  else 
205
 
  { 
206
 
    /* the session did not have a slot reserved, that could be because the scoreboard was
207
 
       full, but most likely its a failed authentication so post() is never called where
208
 
       the slot is assigned. Log the global status values below, and if the user has a slot
209
 
       log to it, but do not reserve a new slot for a user. If it was a failed authentication
210
 
       the scoreboard would be filled up quickly with invalid users. 
211
 
    */
212
 
    scoreboard_slot= new ScoreboardSlot();
213
 
    scoreboard_slot->setUser(session->getSecurityContext().getUser());
214
 
    scoreboard_slot->setIp(session->getSecurityContext().getIp());
 
239
    score_board_slot= &score_board_slots[j];
 
240
 
 
241
    if (score_board_slot->getSessionId() == session->getSessionId())
 
242
    {
 
243
      score_board_slot->reset();
 
244
      break;
 
245
    }
215
246
  }
216
247
 
217
 
  scoreboard_slot->getStatusVars()->logStatusVar(session);
218
 
  scoreboard_slot->getStatusVars()->getStatusVarCounters()->connection_time= time(NULL) - session->start_time; 
219
 
 
220
 
  cumulative_stats->logUserStats(scoreboard_slot, isInScoreboard);
221
 
  cumulative_stats->logGlobalStats(scoreboard_slot);
222
 
  cumulative_stats->logGlobalStatusVars(scoreboard_slot);
223
 
 
224
 
  if (isInScoreboard)
225
 
  {
226
 
    scoreboard_slot->reset();
227
 
  } 
228
 
  else 
229
 
  {
230
 
    delete scoreboard_slot;
231
 
  } 
 
248
  pthread_rwlock_unlock(&LOCK_scoreboard);
232
249
 
233
250
  return false;
234
251
}
237
254
 
238
255
static LoggingStats *logging_stats= NULL;
239
256
 
240
 
static CurrentCommandsTool *current_commands_tool= NULL;
241
 
 
242
 
static CumulativeCommandsTool *cumulative_commands_tool= NULL;
243
 
 
244
 
static GlobalStatementsTool *global_statements_tool= NULL;
245
 
 
246
 
static SessionStatementsTool *session_statements_tool= NULL;
247
 
 
248
 
static StatusTool *global_status_tool= NULL;
249
 
 
250
 
static StatusTool *session_status_tool= NULL;
251
 
 
252
 
static CumulativeUserStatsTool *cumulative_user_stats_tool= NULL;
253
 
 
254
 
static ScoreboardStatsTool *scoreboard_stats_tool= NULL;
255
 
 
256
 
static void enable(Session *, sql_var_t)
 
257
static CommandsTool *commands_tool= NULL;
 
258
 
 
259
static void enable(Session *,
 
260
                   drizzle_sys_var *,
 
261
                   void *var_ptr,
 
262
                   const void *save)
257
263
{
258
264
  if (logging_stats)
259
265
  {
260
 
    if (sysvar_logging_stats_enabled)
 
266
    if (*(bool *)save != false)
261
267
    {
262
268
      logging_stats->enable();
 
269
      *(bool *) var_ptr= (bool) true;
263
270
    }
264
271
    else
265
272
    {
266
273
      logging_stats->disable();
 
274
      *(bool *) var_ptr= (bool) false;
267
275
    }
268
276
  }
269
277
}
270
278
 
271
279
static bool initTable()
272
280
{
273
 
  current_commands_tool= new(nothrow)CurrentCommandsTool(logging_stats);
274
 
 
275
 
  if (! current_commands_tool)
276
 
  {
277
 
    return true;
278
 
  }
279
 
 
280
 
  cumulative_commands_tool= new(nothrow)CumulativeCommandsTool(logging_stats);
281
 
 
282
 
  if (! cumulative_commands_tool)
283
 
  {
284
 
    return true;
285
 
  }
286
 
 
287
 
  global_statements_tool= new(nothrow)GlobalStatementsTool(logging_stats);
288
 
 
289
 
  if (! global_statements_tool)
290
 
  {
291
 
    return true;
292
 
  }
293
 
 
294
 
  session_statements_tool= new(nothrow)SessionStatementsTool(logging_stats);
295
 
 
296
 
  if (! session_statements_tool)
297
 
  {
298
 
    return true;
299
 
  }
300
 
 
301
 
  session_status_tool= new(nothrow)StatusTool(logging_stats, true);
302
 
 
303
 
  if (! session_status_tool)
304
 
  {
305
 
    return true;
306
 
  }
307
 
 
308
 
  global_status_tool= new(nothrow)StatusTool(logging_stats, false);
309
 
 
310
 
  if (! global_status_tool)
311
 
  {
312
 
    return true;
313
 
  }
314
 
 
315
 
  cumulative_user_stats_tool= new(nothrow)CumulativeUserStatsTool(logging_stats);
316
 
 
317
 
  if (! cumulative_user_stats_tool)
318
 
  {
319
 
    return true;
320
 
  }
321
 
 
322
 
  scoreboard_stats_tool= new(nothrow)ScoreboardStatsTool(logging_stats);
323
 
  
324
 
  if (! scoreboard_stats_tool)
 
281
  commands_tool= new(nothrow)CommandsTool(logging_stats);
 
282
 
 
283
  if (! commands_tool)
325
284
  {
326
285
    return true;
327
286
  }
329
288
  return false;
330
289
}
331
290
 
332
 
static int init(drizzled::module::Context &context)
 
291
static int init(drizzled::plugin::Context &context)
333
292
{
334
 
  const module::option_map &vm= context.getOptions();
335
 
 
336
 
  sysvar_logging_stats_enabled= (vm.count("disable")) ? false : true;
337
 
 
338
293
  logging_stats= new LoggingStats("logging_stats");
339
294
 
340
295
  if (initTable())
343
298
  }
344
299
 
345
300
  context.add(logging_stats);
346
 
  context.add(current_commands_tool);
347
 
  context.add(cumulative_commands_tool);
348
 
  context.add(global_statements_tool);
349
 
  context.add(session_statements_tool);
350
 
  context.add(session_status_tool);
351
 
  context.add(global_status_tool);
352
 
  context.add(cumulative_user_stats_tool);
353
 
  context.add(scoreboard_stats_tool);
 
301
  context.add(commands_tool);
354
302
 
355
303
  if (sysvar_logging_stats_enabled)
356
304
  {
357
305
    logging_stats->enable();
358
306
  }
359
307
 
360
 
  context.registerVariable(new sys_var_constrained_value_readonly<uint32_t>("max_user_count", sysvar_logging_stats_max_user_count));
361
 
  context.registerVariable(new sys_var_constrained_value_readonly<uint32_t>("bucket_count", sysvar_logging_stats_bucket_count));
362
 
  context.registerVariable(new sys_var_constrained_value_readonly<uint32_t>("scoreboard_size", sysvar_logging_stats_scoreboard_size));
363
 
  context.registerVariable(new sys_var_bool_ptr("enable", &sysvar_logging_stats_enabled, enable));
364
 
 
365
308
  return 0;
366
309
}
367
310
 
368
 
 
369
 
static void init_options(drizzled::module::option_context &context)
370
 
{
371
 
  context("max-user-count",
372
 
          po::value<max_user_count_constraint>(&sysvar_logging_stats_max_user_count)->default_value(500),
373
 
          N_("Max number of users that will be logged"));
374
 
  context("bucket-count",
375
 
          po::value<bucket_count_constraint>(&sysvar_logging_stats_bucket_count)->default_value(10),
376
 
          N_("Max number of range locks to use for Scoreboard"));
377
 
  context("scoreboard-size",
378
 
          po::value<scoreboard_size_constraint>(&sysvar_logging_stats_scoreboard_size)->default_value(2000),
379
 
          N_("Max number of concurrent sessions that will be logged"));
380
 
  context("disable", N_("Enable Logging Statistics Collection"));
381
 
}
 
311
static DRIZZLE_SYSVAR_UINT(scoreboard_size,
 
312
                           sysvar_logging_stats_scoreboard_size,
 
313
                           PLUGIN_VAR_RQCMDARG,
 
314
                           N_("Max number of concurrent sessions that will be logged"),
 
315
                           NULL, /* check func */
 
316
                           NULL, /* update func */
 
317
                           2000, /* default */
 
318
                           1000, /* minimum */
 
319
                           50000, 
 
320
                           0);
 
321
 
 
322
static DRIZZLE_SYSVAR_BOOL(enable,
 
323
                           sysvar_logging_stats_enabled,
 
324
                           PLUGIN_VAR_NOCMDARG,
 
325
                           N_("Enable Logging Statistics Collection"),
 
326
                           NULL, /* check func */
 
327
                           enable, /* update func */
 
328
                           false /* default */);
 
329
 
 
330
static drizzle_sys_var* system_var[]= {
 
331
  DRIZZLE_SYSVAR(scoreboard_size),
 
332
  DRIZZLE_SYSVAR(enable),
 
333
  NULL
 
334
};
382
335
 
383
336
DRIZZLE_DECLARE_PLUGIN
384
337
{
389
342
  N_("User Statistics as DATA_DICTIONARY tables"),
390
343
  PLUGIN_LICENSE_BSD,
391
344
  init,   /* Plugin Init      */
392
 
  NULL, /* system variables */
393
 
  init_options    /* config options   */
 
345
  system_var, /* system variables */
 
346
  NULL    /* config options   */
394
347
}
395
348
DRIZZLE_DECLARE_PLUGIN_END;