2
* Copyright (c) 2010, Joseph Daly <skinny.moey@gmail.com>
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
8
* * Redistributions of source code must retain the above copyright notice,
9
* this list of conditions and the following disclaimer.
10
* * Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
13
* * Neither the name of Joseph Daly nor the names of its contributors
14
* may be used to endorse or promote products derived from this software
15
* without specific prior written permission.
17
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27
* THE POSSIBILITY OF SUCH DAMAGE.
39
#include "logging_stats.h"
40
#include "stats_schema.h"
41
#include <drizzled/session.h>
43
using namespace drizzled;
44
using namespace plugin;
47
static bool sysvar_logging_stats_enabled= false;
49
static uint32_t sysvar_logging_stats_scoreboard_size= 1500;
51
pthread_rwlock_t LOCK_scoreboard;
53
LoggingStats::LoggingStats(std::string name_arg) : drizzled::plugin::Logging(name_arg)
55
scoreboard_size= sysvar_logging_stats_scoreboard_size;
56
score_board_slots= new ScoreBoardSlot[scoreboard_size];
57
for (uint32_t j=0; j < scoreboard_size; j++)
59
UserCommands *user_commands= new UserCommands();
60
ScoreBoardSlot *score_board_slot= &score_board_slots[j];
61
score_board_slot->setUserCommands(user_commands);
65
LoggingStats::~LoggingStats()
67
delete[] score_board_slots;
70
bool LoggingStats::isBeingLogged(Session *session)
72
enum_sql_command sql_command= session->lex->sql_command;
81
case SQLCOM_CREATE_TABLE:
82
case SQLCOM_ALTER_TABLE:
83
case SQLCOM_DROP_TABLE:
91
void LoggingStats::updateScoreBoard(ScoreBoardSlot *score_board_slot,
94
enum_sql_command sql_command= session->lex->sql_command;
96
UserCommands *user_commands= score_board_slot->getUserCommands();
101
user_commands->incrementUpdateCount();
104
user_commands->incrementDeleteCount();
107
user_commands->incrementInsertCount();
109
case SQLCOM_ROLLBACK:
110
user_commands->incrementRollbackCount();
113
user_commands->incrementCommitCount();
115
case SQLCOM_CREATE_TABLE:
116
user_commands->incrementCreateCount();
118
case SQLCOM_ALTER_TABLE:
119
user_commands->incrementAlterCount();
121
case SQLCOM_DROP_TABLE:
122
user_commands->incrementDropCount();
125
user_commands->incrementSelectCount();
132
bool LoggingStats::post(Session *session)
139
/* exit early if we are not logging this type of command */
140
if (isBeingLogged(session) == false)
145
/* Find a slot that is unused */
147
pthread_rwlock_wrlock(&LOCK_scoreboard);
148
ScoreBoardSlot *score_board_slot;
151
for (uint32_t j=0; j < scoreboard_size; j++)
153
score_board_slot= &score_board_slots[j];
155
if (score_board_slot->isInUse() == true)
157
/* Check if this session is the one using this slot */
158
if (score_board_slot->getSessionId() == session->getSessionId())
170
/* save off the open slot */
178
score_board_slot->setInUse(true);
179
score_board_slot->setSessionId(session->getSessionId());
180
score_board_slot->setUser(session->getSecurityContext().getUser());
181
score_board_slot->setIp(session->getSecurityContext().getIp());
182
pthread_rwlock_unlock(&LOCK_scoreboard);
186
pthread_rwlock_unlock(&LOCK_scoreboard);
187
/* there was no available slot for this session */
191
updateScoreBoard(score_board_slot, session);
196
bool LoggingStats::postEnd(Session *session)
203
/* do not pull a lock when we write this is the only thread that
204
can write to a particular sessions slot. */
206
ScoreBoardSlot *score_board_slot;
208
for (uint32_t j=0; j < scoreboard_size; j++)
210
score_board_slot= &score_board_slots[j];
212
if (score_board_slot->getSessionId() == session->getSessionId())
214
score_board_slot->reset();
221
/* Plugin initialization and system variables */
223
static LoggingStats *logging_stats= NULL;
225
static CommandsTool *commands_tool= NULL;
227
static void enable(Session *,
234
if (*(bool *)save != false)
236
logging_stats->enable();
237
*(bool *) var_ptr= (bool) true;
241
logging_stats->disable();
242
*(bool *) var_ptr= (bool) false;
247
static bool initTable()
249
commands_tool= new(std::nothrow)CommandsTool(logging_stats);
259
static int init(drizzled::plugin::Registry ®istry)
261
logging_stats= new LoggingStats("logging_stats");
268
registry.add(logging_stats);
269
registry.add(commands_tool);
271
if (sysvar_logging_stats_enabled)
273
logging_stats->enable();
279
static int deinit(drizzled::plugin::Registry ®istry)
283
registry.remove(commands_tool);
284
registry.remove(logging_stats);
286
delete commands_tool;
287
delete logging_stats;
292
static DRIZZLE_SYSVAR_BOOL(enable,
293
sysvar_logging_stats_enabled,
295
N_("Enable Logging Statistics Collection"),
296
NULL, /* check func */
297
enable, /* update func */
298
false /* default */);
300
static drizzle_sys_var* system_var[]= {
301
DRIZZLE_SYSVAR(enable),
305
DRIZZLE_DECLARE_PLUGIN
311
N_("User Statistics as DATA_DICTIONARY tables"),
313
init, /* Plugin Init */
314
deinit, /* Plugin Deinit */
315
system_var, /* system variables */
316
NULL /* config options */
318
DRIZZLE_DECLARE_PLUGIN_END;