~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/logging_stats/logging_stats.cc

  • Committer: Joe Daly
  • Date: 2010-05-21 02:16:56 UTC
  • mto: This revision was merged to the branch mainline in revision 1555.
  • Revision ID: skinny.moey@gmail.com-20100521021656-bx6piitfh77jnl28
add statistics_variables.h

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (C) 2010 Joseph Daly <skinny.moey@gmail.com>
 
2
 * Copyright (c) 2010, Joseph Daly <skinny.moey@gmail.com>
3
3
 * All rights reserved.
4
4
 *
5
5
 * Redistribution and use in source and binary forms, with or without
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
 
 * 
 
33
 * This plugin tracks the current user commands for a session, and copies
 
34
 * them into a cumulative vector of all commands run by a user over time. 
 
35
 * The commands are logged using the post() and postEnd() logging APIs.
 
36
 * User commands are stored in a Scoreboard where each active session
 
37
 * owns a ScoreboardSlot.  
 
38
 *
38
39
 * Scoreboard
39
40
 *
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. 
 
41
 * The scoreboard is a pre-allocated vector of vectors of ScoreboardSlots. It
 
42
 * can be thought of as a vector of buckets where each bucket contains
 
43
 * pre-allocated ScoreboardSlots. To determine which bucket gets used for
 
44
 * recording statistics the modulus operator is used on the session_id. This
 
45
 * will result in a bucket to search for a unused ScoreboardSlot.
47
46
 *
48
47
 * Locking  
49
48
 * 
50
49
 * Each vector in the Scoreboard has its own lock. This allows session 2 
51
50
 * to not have to wait for session 1 to locate a slot to use, as they
52
51
 * 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.  
 
52
 * in the scoreboard.
55
53
 *
56
54
 * 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. 
 
55
 * in the data_dictionary.
60
56
 *
61
57
 * Atomics
62
58
 *
82
78
 *
83
79
 * Allow expansion of Scoreboard and cumulative vector 
84
80
 * 
 
81
 * Possibly add a scoreboard_slot_index variable onto the Session class
 
82
 * this would avoid having to relocate the Scoreboard slot for each Session
 
83
 * doing multiple statements. 
 
84
 * 
85
85
 */
86
86
 
87
 
#include <config.h>
88
 
#include "user_commands.h"
89
 
#include "status_vars.h"
90
 
#include "global_stats.h"
 
87
#include "config.h"
91
88
#include "logging_stats.h"
92
 
#include "status_tool.h"
93
89
#include "stats_schema.h"
94
 
#include <boost/program_options.hpp>
95
 
#include <drizzled/module/option_map.h>
96
90
#include <drizzled/session.h>
97
91
 
98
 
namespace po= boost::program_options;
99
92
using namespace drizzled;
100
93
using namespace plugin;
101
94
using namespace std;
102
95
 
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;
 
96
static bool sysvar_logging_stats_enabled= false;
 
97
 
 
98
static uint32_t sysvar_logging_stats_scoreboard_size= 2000;
 
99
 
 
100
static uint32_t sysvar_logging_stats_max_user_count= 1000;
 
101
 
 
102
static uint32_t sysvar_logging_stats_bucket_count= 10;
113
103
 
114
104
LoggingStats::LoggingStats(string name_arg) : Logging(name_arg)
115
105
{
128
118
void LoggingStats::updateCurrentScoreboard(ScoreboardSlot *scoreboard_slot,
129
119
                                           Session *session)
130
120
{
131
 
  enum_sql_command sql_command= session->getLex()->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;
 
121
  enum_sql_command sql_command= session->lex->sql_command;
 
122
 
 
123
  UserCommands *user_commands= scoreboard_slot->getUserCommands();
 
124
 
 
125
  user_commands->logCommand(sql_command);
170
126
}
171
127
 
172
128
bool LoggingStats::post(Session *session)
194
150
    return false;
195
151
  }
196
152
 
197
 
  bool isInScoreboard= false;
198
 
  ScoreboardSlot *scoreboard_slot= current_scoreboard->findOurScoreboardSlot(session);
 
153
  ScoreboardSlot *scoreboard_slot= current_scoreboard->findAndResetScoreboardSlot(session);
199
154
 
200
155
  if (scoreboard_slot)
201
156
  {
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->user()->username());
214
 
    scoreboard_slot->setIp(session->user()->address());
215
 
  }
216
 
 
217
 
  scoreboard_slot->getStatusVars()->logStatusVar(session);
218
 
  scoreboard_slot->getStatusVars()->getStatusVarCounters()->connection_time= session->getConnectSeconds(); 
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
 
  {
 
157
    cumulative_stats->logUserStats(scoreboard_slot);
 
158
    cumulative_stats->logGlobalStats(scoreboard_slot);
230
159
    delete scoreboard_slot;
231
 
  } 
 
160
  }
232
161
 
233
162
  return false;
234
163
}
245
174
 
246
175
static SessionStatementsTool *session_statements_tool= NULL;
247
176
 
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)
 
177
static void enable(Session *,
 
178
                   drizzle_sys_var *,
 
179
                   void *var_ptr,
 
180
                   const void *save)
257
181
{
258
182
  if (logging_stats)
259
183
  {
260
 
    if (sysvar_logging_stats_enabled)
 
184
    if (*(bool *)save != false)
261
185
    {
262
186
      logging_stats->enable();
 
187
      *(bool *) var_ptr= (bool) true;
263
188
    }
264
189
    else
265
190
    {
266
191
      logging_stats->disable();
 
192
      *(bool *) var_ptr= (bool) false;
267
193
    }
268
194
  }
269
195
}
298
224
    return true;
299
225
  }
300
226
 
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)
325
 
  {
326
 
    return true;
327
 
  }
328
 
 
329
227
  return false;
330
228
}
331
229
 
332
 
static int init(drizzled::module::Context &context)
 
230
static int init(Context &context)
333
231
{
334
 
  const module::option_map &vm= context.getOptions();
335
 
 
336
 
  sysvar_logging_stats_enabled= (vm.count("disable")) ? false : true;
337
 
 
338
232
  logging_stats= new LoggingStats("logging_stats");
339
233
 
340
234
  if (initTable())
347
241
  context.add(cumulative_commands_tool);
348
242
  context.add(global_statements_tool);
349
243
  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);
354
244
 
355
245
  if (sysvar_logging_stats_enabled)
356
246
  {
357
247
    logging_stats->enable();
358
248
  }
359
249
 
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
250
  return 0;
366
251
}
367
252
 
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
 
          _("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
 
          _("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
 
          _("Max number of concurrent sessions that will be logged"));
380
 
  context("disable", _("Enable Logging Statistics Collection"));
381
 
}
 
253
static DRIZZLE_SYSVAR_UINT(max_user_count,
 
254
                           sysvar_logging_stats_max_user_count,
 
255
                           PLUGIN_VAR_RQCMDARG,
 
256
                           N_("Max number of users that will be logged"),
 
257
                           NULL, /* check func */
 
258
                           NULL, /* update func */
 
259
                           1000, /* default */
 
260
                           500, /* minimum */
 
261
                           50000,
 
262
                           0);
 
263
 
 
264
static DRIZZLE_SYSVAR_UINT(bucket_count,
 
265
                           sysvar_logging_stats_bucket_count,
 
266
                           PLUGIN_VAR_RQCMDARG,
 
267
                           N_("Max number of vector buckets to construct for logging"),
 
268
                           NULL, /* check func */
 
269
                           NULL, /* update func */
 
270
                           10, /* default */
 
271
                           5, /* minimum */
 
272
                           500,
 
273
                           0);
 
274
 
 
275
static DRIZZLE_SYSVAR_UINT(scoreboard_size,
 
276
                           sysvar_logging_stats_scoreboard_size,
 
277
                           PLUGIN_VAR_RQCMDARG,
 
278
                           N_("Max number of concurrent sessions that will be logged"),
 
279
                           NULL, /* check func */
 
280
                           NULL, /* update func */
 
281
                           2000, /* default */
 
282
                           10, /* minimum */
 
283
                           50000, 
 
284
                           0);
 
285
 
 
286
static DRIZZLE_SYSVAR_BOOL(enable,
 
287
                           sysvar_logging_stats_enabled,
 
288
                           PLUGIN_VAR_NOCMDARG,
 
289
                           N_("Enable Logging Statistics Collection"),
 
290
                           NULL, /* check func */
 
291
                           enable, /* update func */
 
292
                           false /* default */);
 
293
 
 
294
static drizzle_sys_var* system_var[]= {
 
295
  DRIZZLE_SYSVAR(max_user_count),
 
296
  DRIZZLE_SYSVAR(bucket_count),
 
297
  DRIZZLE_SYSVAR(scoreboard_size),
 
298
  DRIZZLE_SYSVAR(enable),
 
299
  NULL
 
300
};
382
301
 
383
302
DRIZZLE_DECLARE_PLUGIN
384
303
{
389
308
  N_("User Statistics as DATA_DICTIONARY tables"),
390
309
  PLUGIN_LICENSE_BSD,
391
310
  init,   /* Plugin Init      */
392
 
  NULL, /* depends */
393
 
  init_options    /* config options   */
 
311
  system_var, /* system variables */
 
312
  NULL    /* config options   */
394
313
}
395
314
DRIZZLE_DECLARE_PLUGIN_END;