~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-08-12 17:19:46 UTC
  • mfrom: (1701.1.1 turn-off-csv)
  • Revision ID: brian@tangent.org-20100812171946-n44naaqhg27gehlh
MErge Monty, remove CSV from auto-build

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
84
84
 * 
85
85
 */
86
86
 
87
 
#include <config.h>
 
87
#include "config.h"
88
88
#include "user_commands.h"
89
89
#include "status_vars.h"
90
90
#include "global_stats.h"
102
102
 
103
103
static bool sysvar_logging_stats_enabled= true;
104
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;
 
105
static uint32_t sysvar_logging_stats_scoreboard_size= 2000;
 
106
 
 
107
static uint32_t sysvar_logging_stats_max_user_count= 500;
 
108
 
 
109
static uint32_t sysvar_logging_stats_bucket_count= 10;
113
110
 
114
111
LoggingStats::LoggingStats(string name_arg) : Logging(name_arg)
115
112
{
128
125
void LoggingStats::updateCurrentScoreboard(ScoreboardSlot *scoreboard_slot,
129
126
                                           Session *session)
130
127
{
131
 
  enum_sql_command sql_command= session->getLex()->sql_command;
 
128
  enum_sql_command sql_command= session->lex->sql_command;
132
129
 
133
130
  scoreboard_slot->getUserCommands()->logCommand(sql_command);
134
131
 
140
137
  scoreboard_slot->getStatusVars()->logStatusVar(session);
141
138
}
142
139
 
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;
170
 
}
171
 
 
172
140
bool LoggingStats::post(Session *session)
173
141
{
174
142
  if (! isEnabled() || (session->getSessionId() == 0))
210
178
       the scoreboard would be filled up quickly with invalid users. 
211
179
    */
212
180
    scoreboard_slot= new ScoreboardSlot();
213
 
    scoreboard_slot->setUser(session->user()->username());
214
 
    scoreboard_slot->setIp(session->user()->address());
 
181
    scoreboard_slot->setUser(session->getSecurityContext().getUser());
 
182
    scoreboard_slot->setIp(session->getSecurityContext().getIp());
215
183
  }
216
184
 
217
185
  scoreboard_slot->getStatusVars()->logStatusVar(session);
218
 
  scoreboard_slot->getStatusVars()->getStatusVarCounters()->connection_time= session->getConnectSeconds(); 
 
186
  scoreboard_slot->getStatusVars()->getStatusVarCounters()->connection_time= time(NULL) - session->start_time; 
219
187
 
220
188
  cumulative_stats->logUserStats(scoreboard_slot, isInScoreboard);
221
189
  cumulative_stats->logGlobalStats(scoreboard_slot);
251
219
 
252
220
static CumulativeUserStatsTool *cumulative_user_stats_tool= NULL;
253
221
 
254
 
static ScoreboardStatsTool *scoreboard_stats_tool= NULL;
255
 
 
256
 
static void enable(Session *, sql_var_t)
 
222
static void enable(Session *,
 
223
                   drizzle_sys_var *,
 
224
                   void *var_ptr,
 
225
                   const void *save)
257
226
{
258
227
  if (logging_stats)
259
228
  {
260
 
    if (sysvar_logging_stats_enabled)
 
229
    if (*(bool *)save != false)
261
230
    {
262
231
      logging_stats->enable();
 
232
      *(bool *) var_ptr= (bool) true;
263
233
    }
264
234
    else
265
235
    {
266
236
      logging_stats->disable();
 
237
      *(bool *) var_ptr= (bool) false;
267
238
    }
268
239
  }
269
240
}
319
290
    return true;
320
291
  }
321
292
 
322
 
  scoreboard_stats_tool= new(nothrow)ScoreboardStatsTool(logging_stats);
323
 
  
324
 
  if (! scoreboard_stats_tool)
325
 
  {
326
 
    return true;
327
 
  }
328
 
 
329
293
  return false;
330
294
}
331
295
 
332
296
static int init(drizzled::module::Context &context)
333
297
{
334
298
  const module::option_map &vm= context.getOptions();
 
299
  if (vm.count("max-user-count"))
 
300
  {
 
301
    if (sysvar_logging_stats_max_user_count < 100 || sysvar_logging_stats_max_user_count > 50000)
 
302
    {
 
303
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for max-user-count\n"));
 
304
      exit(-1);
 
305
    }
 
306
  }
 
307
  if (vm.count("bucket-count"))
 
308
  {
 
309
    if (sysvar_logging_stats_bucket_count < 5 || sysvar_logging_stats_bucket_count > 500)
 
310
    {
 
311
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for bucket-count\n"));
 
312
      exit(-1);
 
313
    }
 
314
  }
335
315
 
336
 
  sysvar_logging_stats_enabled= (vm.count("disable")) ? false : true;
 
316
  if (vm.count("scoreboard-size"))
 
317
  {
 
318
    if (sysvar_logging_stats_scoreboard_size < 10 || sysvar_logging_stats_scoreboard_size > 50000)
 
319
    {
 
320
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for scoreboard-size\n"));
 
321
      exit(-1);
 
322
    }
 
323
    else
 
324
      sysvar_logging_stats_scoreboard_size= vm["scoreboard-size"].as<uint32_t>(); 
 
325
  }
337
326
 
338
327
  logging_stats= new LoggingStats("logging_stats");
339
328
 
350
339
  context.add(session_status_tool);
351
340
  context.add(global_status_tool);
352
341
  context.add(cumulative_user_stats_tool);
353
 
  context.add(scoreboard_stats_tool);
354
342
 
355
343
  if (sysvar_logging_stats_enabled)
356
344
  {
357
345
    logging_stats->enable();
358
346
  }
359
347
 
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
348
  return 0;
366
349
}
367
350
 
 
351
static DRIZZLE_SYSVAR_UINT(max_user_count,
 
352
                           sysvar_logging_stats_max_user_count,
 
353
                           PLUGIN_VAR_RQCMDARG,
 
354
                           N_("Max number of users that will be logged"),
 
355
                           NULL, /* check func */
 
356
                           NULL, /* update func */
 
357
                           500, /* default */
 
358
                           100, /* minimum */
 
359
                           50000,
 
360
                           0);
 
361
 
 
362
static DRIZZLE_SYSVAR_UINT(bucket_count,
 
363
                           sysvar_logging_stats_bucket_count,
 
364
                           PLUGIN_VAR_RQCMDARG,
 
365
                           N_("Max number of vector buckets to construct for logging"),
 
366
                           NULL, /* check func */
 
367
                           NULL, /* update func */
 
368
                           10, /* default */
 
369
                           5, /* minimum */
 
370
                           500,
 
371
                           0);
 
372
 
 
373
static DRIZZLE_SYSVAR_UINT(scoreboard_size,
 
374
                           sysvar_logging_stats_scoreboard_size,
 
375
                           PLUGIN_VAR_RQCMDARG,
 
376
                           N_("Max number of concurrent sessions that will be logged"),
 
377
                           NULL, /* check func */
 
378
                           NULL, /* update func */
 
379
                           2000, /* default */
 
380
                           10, /* minimum */
 
381
                           50000, 
 
382
                           0);
 
383
 
 
384
static DRIZZLE_SYSVAR_BOOL(enable,
 
385
                           sysvar_logging_stats_enabled,
 
386
                           PLUGIN_VAR_NOCMDARG,
 
387
                           N_("Enable Logging Statistics Collection"),
 
388
                           NULL, /* check func */
 
389
                           enable, /* update func */
 
390
                           true /* default */);
368
391
 
369
392
static void init_options(drizzled::module::option_context &context)
370
393
{
371
394
  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"));
 
395
          po::value<uint32_t>(&sysvar_logging_stats_max_user_count)->default_value(500),
 
396
          N_("Max number of users that will be logged"));
374
397
  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"));
 
398
          po::value<uint32_t>(&sysvar_logging_stats_bucket_count)->default_value(10),
 
399
          N_("Max number of vector buckets to construct for logging"));
377
400
  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"));
 
401
          po::value<uint32_t>(&sysvar_logging_stats_scoreboard_size)->default_value(2000),
 
402
          N_("Max number of concurrent sessions that will be logged"));
 
403
  context("enable",
 
404
          po::value<bool>(&sysvar_logging_stats_enabled)->default_value(true)->zero_tokens(),
 
405
          N_("Enable Logging Statistics Collection"));
381
406
}
382
407
 
 
408
static drizzle_sys_var* system_var[]= {
 
409
  DRIZZLE_SYSVAR(max_user_count),
 
410
  DRIZZLE_SYSVAR(bucket_count),
 
411
  DRIZZLE_SYSVAR(scoreboard_size),
 
412
  DRIZZLE_SYSVAR(enable),
 
413
  NULL
 
414
};
 
415
 
383
416
DRIZZLE_DECLARE_PLUGIN
384
417
{
385
418
  DRIZZLE_VERSION_ID,
389
422
  N_("User Statistics as DATA_DICTIONARY tables"),
390
423
  PLUGIN_LICENSE_BSD,
391
424
  init,   /* Plugin Init      */
392
 
  NULL, /* depends */
 
425
  system_var, /* system variables */
393
426
  init_options    /* config options   */
394
427
}
395
428
DRIZZLE_DECLARE_PLUGIN_END;