~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/logging_stats/logging_stats.cc

Merge Joe, plus I updated the tests.

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
91
91
#include "logging_stats.h"
92
92
#include "status_tool.h"
93
93
#include "stats_schema.h"
94
 
#include <boost/program_options.hpp>
95
 
#include <drizzled/module/option_map.h>
 
94
 
96
95
#include <drizzled/session.h>
97
96
 
98
 
namespace po= boost::program_options;
99
97
using namespace drizzled;
100
98
using namespace plugin;
101
99
using namespace std;
102
100
 
103
101
static bool sysvar_logging_stats_enabled= true;
104
102
 
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;
 
103
static uint32_t sysvar_logging_stats_scoreboard_size= 2000;
 
104
 
 
105
static uint32_t sysvar_logging_stats_max_user_count= 500;
 
106
 
 
107
static uint32_t sysvar_logging_stats_bucket_count= 10;
113
108
 
114
109
LoggingStats::LoggingStats(string name_arg) : Logging(name_arg)
115
110
{
140
135
  scoreboard_slot->getStatusVars()->logStatusVar(session);
141
136
}
142
137
 
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
138
bool LoggingStats::post(Session *session)
173
139
{
174
140
  if (! isEnabled() || (session->getSessionId() == 0))
210
176
       the scoreboard would be filled up quickly with invalid users. 
211
177
    */
212
178
    scoreboard_slot= new ScoreboardSlot();
213
 
    scoreboard_slot->setUser(session->user()->username());
214
 
    scoreboard_slot->setIp(session->user()->address());
 
179
    scoreboard_slot->setUser(session->getSecurityContext().getUser());
 
180
    scoreboard_slot->setIp(session->getSecurityContext().getIp());
215
181
  }
216
182
 
217
183
  scoreboard_slot->getStatusVars()->logStatusVar(session);
218
 
  scoreboard_slot->getStatusVars()->getStatusVarCounters()->connection_time= session->getConnectSeconds(); 
 
184
  scoreboard_slot->getStatusVars()->getStatusVarCounters()->connection_time= time(NULL) - session->start_time; 
219
185
 
220
186
  cumulative_stats->logUserStats(scoreboard_slot, isInScoreboard);
221
187
  cumulative_stats->logGlobalStats(scoreboard_slot);
251
217
 
252
218
static CumulativeUserStatsTool *cumulative_user_stats_tool= NULL;
253
219
 
254
 
static ScoreboardStatsTool *scoreboard_stats_tool= NULL;
255
 
 
256
 
static void enable(Session *, sql_var_t)
 
220
static void enable(Session *,
 
221
                   drizzle_sys_var *,
 
222
                   void *var_ptr,
 
223
                   const void *save)
257
224
{
258
225
  if (logging_stats)
259
226
  {
260
 
    if (sysvar_logging_stats_enabled)
 
227
    if (*(bool *)save != false)
261
228
    {
262
229
      logging_stats->enable();
 
230
      *(bool *) var_ptr= (bool) true;
263
231
    }
264
232
    else
265
233
    {
266
234
      logging_stats->disable();
 
235
      *(bool *) var_ptr= (bool) false;
267
236
    }
268
237
  }
269
238
}
319
288
    return true;
320
289
  }
321
290
 
322
 
  scoreboard_stats_tool= new(nothrow)ScoreboardStatsTool(logging_stats);
323
 
  
324
 
  if (! scoreboard_stats_tool)
325
 
  {
326
 
    return true;
327
 
  }
328
 
 
329
291
  return false;
330
292
}
331
293
 
332
 
static int init(drizzled::module::Context &context)
 
294
static int init(module::Context &context)
333
295
{
334
 
  const module::option_map &vm= context.getOptions();
335
 
 
336
 
  sysvar_logging_stats_enabled= (vm.count("disable")) ? false : true;
337
 
 
338
296
  logging_stats= new LoggingStats("logging_stats");
339
297
 
340
298
  if (initTable())
350
308
  context.add(session_status_tool);
351
309
  context.add(global_status_tool);
352
310
  context.add(cumulative_user_stats_tool);
353
 
  context.add(scoreboard_stats_tool);
354
311
 
355
312
  if (sysvar_logging_stats_enabled)
356
313
  {
357
314
    logging_stats->enable();
358
315
  }
359
316
 
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
317
  return 0;
366
318
}
367
319
 
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
 
}
 
320
static DRIZZLE_SYSVAR_UINT(max_user_count,
 
321
                           sysvar_logging_stats_max_user_count,
 
322
                           PLUGIN_VAR_RQCMDARG,
 
323
                           N_("Max number of users that will be logged"),
 
324
                           NULL, /* check func */
 
325
                           NULL, /* update func */
 
326
                           500, /* default */
 
327
                           100, /* minimum */
 
328
                           50000,
 
329
                           0);
 
330
 
 
331
static DRIZZLE_SYSVAR_UINT(bucket_count,
 
332
                           sysvar_logging_stats_bucket_count,
 
333
                           PLUGIN_VAR_RQCMDARG,
 
334
                           N_("Max number of vector buckets to construct for logging"),
 
335
                           NULL, /* check func */
 
336
                           NULL, /* update func */
 
337
                           10, /* default */
 
338
                           5, /* minimum */
 
339
                           500,
 
340
                           0);
 
341
 
 
342
static DRIZZLE_SYSVAR_UINT(scoreboard_size,
 
343
                           sysvar_logging_stats_scoreboard_size,
 
344
                           PLUGIN_VAR_RQCMDARG,
 
345
                           N_("Max number of concurrent sessions that will be logged"),
 
346
                           NULL, /* check func */
 
347
                           NULL, /* update func */
 
348
                           2000, /* default */
 
349
                           10, /* minimum */
 
350
                           50000, 
 
351
                           0);
 
352
 
 
353
static DRIZZLE_SYSVAR_BOOL(enable,
 
354
                           sysvar_logging_stats_enabled,
 
355
                           PLUGIN_VAR_NOCMDARG,
 
356
                           N_("Enable Logging Statistics Collection"),
 
357
                           NULL, /* check func */
 
358
                           enable, /* update func */
 
359
                           true /* default */);
 
360
 
 
361
static drizzle_sys_var* system_var[]= {
 
362
  DRIZZLE_SYSVAR(max_user_count),
 
363
  DRIZZLE_SYSVAR(bucket_count),
 
364
  DRIZZLE_SYSVAR(scoreboard_size),
 
365
  DRIZZLE_SYSVAR(enable),
 
366
  NULL
 
367
};
382
368
 
383
369
DRIZZLE_DECLARE_PLUGIN
384
370
{
389
375
  N_("User Statistics as DATA_DICTIONARY tables"),
390
376
  PLUGIN_LICENSE_BSD,
391
377
  init,   /* Plugin Init      */
392
 
  NULL, /* depends */
393
 
  init_options    /* config options   */
 
378
  system_var, /* system variables */
 
379
  NULL    /* config options   */
394
380
}
395
381
DRIZZLE_DECLARE_PLUGIN_END;