drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
stats_schema.cc
1/*
2 * Copyright (C) 2010 Joseph Daly <[email protected]>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
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.
16 *
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.
28 */
29
105
106#include <config.h>
107#include <drizzled/statistics_variables.h>
108#include "stats_schema.h"
109#include <sstream>
110
111using namespace drizzled;
112using namespace plugin;
113using namespace std;
114
115SessionStatementsTool::SessionStatementsTool(LoggingStats *in_logging_stats) :
116 plugin::TableFunction("DATA_DICTIONARY", "SESSION_STATEMENTS")
117{
118 logging_stats= in_logging_stats;
119 add_field("VARIABLE_NAME");
120 add_field("VARIABLE_VALUE", 1024);
121}
122
123SessionStatementsTool::Generator::Generator(Field **arg, LoggingStats *in_logging_stats) :
124 plugin::TableFunction::Generator(arg)
125{
126 count= 0;
127
128 /* Set user_commands */
129 Scoreboard *current_scoreboard= in_logging_stats->getCurrentScoreboard();
130
131 uint32_t bucket_number= current_scoreboard->getBucketNumber(&getSession());
132
133 std::vector<ScoreboardSlot* > *scoreboard_vector=
134 current_scoreboard->getVectorOfScoreboardVectors()->at(bucket_number);
135
136 ScoreboardSlot *scoreboard_slot= NULL;
137 for (std::vector<ScoreboardSlot *>::iterator it= scoreboard_vector->begin();
138 it != scoreboard_vector->end(); ++it)
139 {
140 scoreboard_slot= *it;
141 if (scoreboard_slot->getSessionId() == getSession().getSessionId())
142 {
143 break;
144 }
145 }
146
147 user_commands= NULL;
148
149 if (scoreboard_slot != NULL)
150 {
151 user_commands= scoreboard_slot->getUserCommands();
152 }
153}
154
155bool SessionStatementsTool::Generator::populate()
156{
157 if (user_commands == NULL)
158 {
159 return false;
160 }
161
162 uint32_t number_identifiers= UserCommands::getStatusVarsCount();
163
164 if (count == number_identifiers)
165 {
166 return false;
167 }
168
169 push(UserCommands::COM_STATUS_VARS[count]);
170 ostringstream oss;
171 oss << user_commands->getCount(count);
172 push(oss.str());
173
174 ++count;
175 return true;
176}
177
178GlobalStatementsTool::GlobalStatementsTool(LoggingStats *in_logging_stats) :
179 plugin::TableFunction("DATA_DICTIONARY", "GLOBAL_STATEMENTS")
180{
181 logging_stats= in_logging_stats;
182 add_field("VARIABLE_NAME");
183 add_field("VARIABLE_VALUE", 1024);
184}
185
186GlobalStatementsTool::Generator::Generator(Field **arg, LoggingStats *in_logging_stats) :
187 plugin::TableFunction::Generator(arg)
188{
189 count= 0;
190 /* add the current scoreboard and the saved global statements */
191 global_stats_to_display= new GlobalStats();
192 CumulativeStats *cumulativeStats= in_logging_stats->getCumulativeStats();
193 cumulativeStats->sumCurrentScoreboard(in_logging_stats->getCurrentScoreboard(),
194 NULL, global_stats_to_display->getUserCommands());
195 global_stats_to_display->merge(in_logging_stats->getCumulativeStats()->getGlobalStats());
196}
197
198GlobalStatementsTool::Generator::~Generator()
199{
200 delete global_stats_to_display;
201}
202
203bool GlobalStatementsTool::Generator::populate()
204{
205 uint32_t number_identifiers= UserCommands::getStatusVarsCount();
206 if (count == number_identifiers)
207 {
208 return false;
209 }
210
211 push(UserCommands::COM_STATUS_VARS[count]);
212 ostringstream oss;
213 oss << global_stats_to_display->getUserCommands()->getCount(count);
214 push(oss.str());
215
216 ++count;
217 return true;
218}
219
220CurrentCommandsTool::CurrentCommandsTool(LoggingStats *logging_stats) :
221 plugin::TableFunction("DATA_DICTIONARY", "CURRENT_SQL_COMMANDS")
222{
223 outer_logging_stats= logging_stats;
224
225 add_field("USERNAME");
226 add_field("IP");
227
228 uint32_t number_commands= UserCommands::getUserCounts();
229
230 for (uint32_t j= 0; j < number_commands; ++j)
231 {
232 add_field(UserCommands::USER_COUNTS[j], TableFunction::NUMBER);
233 }
234}
235
236CurrentCommandsTool::Generator::Generator(Field **arg, LoggingStats *logging_stats) :
237 plugin::TableFunction::Generator(arg)
238{
239 inner_logging_stats= logging_stats;
240
241 isEnabled= inner_logging_stats->isEnabled();
242
243 if (isEnabled == false)
244 {
245 return;
246 }
247
248 current_scoreboard= logging_stats->getCurrentScoreboard();
249 current_bucket= 0;
250
251 vector_of_scoreboard_vectors_it= current_scoreboard->getVectorOfScoreboardVectors()->begin();
252 vector_of_scoreboard_vectors_end= current_scoreboard->getVectorOfScoreboardVectors()->end();
253
254 setVectorIteratorsAndLock(current_bucket);
255}
256
257void CurrentCommandsTool::Generator::setVectorIteratorsAndLock(uint32_t bucket_number)
258{
259 std::vector<ScoreboardSlot* > *scoreboard_vector=
260 current_scoreboard->getVectorOfScoreboardVectors()->at(bucket_number);
261
262 current_lock= current_scoreboard->getVectorOfScoreboardLocks()->at(bucket_number);
263
264 scoreboard_vector_it= scoreboard_vector->begin();
265 scoreboard_vector_end= scoreboard_vector->end();
266 current_lock->lock_shared();
267}
268
269bool CurrentCommandsTool::Generator::populate()
270{
271 if (isEnabled == false)
272 {
273 return false;
274 }
275
276 while (vector_of_scoreboard_vectors_it != vector_of_scoreboard_vectors_end)
277 {
278 while (scoreboard_vector_it != scoreboard_vector_end)
279 {
280 ScoreboardSlot *scoreboard_slot= *scoreboard_vector_it;
281 if (scoreboard_slot->isInUse())
282 {
283 UserCommands *user_commands= scoreboard_slot->getUserCommands();
284 push(scoreboard_slot->getUser());
285 push(scoreboard_slot->getIp());
286
287 uint32_t number_commands= UserCommands::getUserCounts();
288
289 for (uint32_t j= 0; j < number_commands; ++j)
290 {
291 push(user_commands->getUserCount(j));
292 }
293
294 ++scoreboard_vector_it;
295 return true;
296 }
297 ++scoreboard_vector_it;
298 }
299
300 ++vector_of_scoreboard_vectors_it;
301 current_lock->unlock_shared();
302 ++current_bucket;
303 if (vector_of_scoreboard_vectors_it != vector_of_scoreboard_vectors_end)
304 {
305 setVectorIteratorsAndLock(current_bucket);
306 }
307 }
308
309 return false;
310}
311
312CumulativeCommandsTool::CumulativeCommandsTool(LoggingStats *logging_stats) :
313 plugin::TableFunction("DATA_DICTIONARY", "CUMULATIVE_SQL_COMMANDS")
314{
315 outer_logging_stats= logging_stats;
316
317 add_field("USERNAME");
318
319 uint32_t number_commands= UserCommands::getUserCounts();
320
321 for (uint32_t j= 0; j < number_commands; ++j)
322 {
323 add_field(UserCommands::USER_COUNTS[j], TableFunction::NUMBER);
324 }
325}
326
327CumulativeCommandsTool::Generator::Generator(Field **arg, LoggingStats *logging_stats) :
328 plugin::TableFunction::Generator(arg)
329{
330 inner_logging_stats= logging_stats;
331 record_number= 0;
332
333 if (inner_logging_stats->isEnabled())
334 {
335 last_valid_index= inner_logging_stats->getCumulativeStats()->getCumulativeStatsLastValidIndex();
336 }
337 else
338 {
339 last_valid_index= INVALID_INDEX;
340 }
341}
342
343bool CumulativeCommandsTool::Generator::populate()
344{
345 if ((record_number > last_valid_index) || (last_valid_index == INVALID_INDEX))
346 {
347 return false;
348 }
349
350 while (record_number <= last_valid_index)
351 {
352 ScoreboardSlot *cumulative_scoreboard_slot=
353 inner_logging_stats->getCumulativeStats()->getCumulativeStatsByUserVector()->at(record_number);
354
355 if (cumulative_scoreboard_slot->isInUse())
356 {
357 UserCommands *user_commands= cumulative_scoreboard_slot->getUserCommands();
358 push(cumulative_scoreboard_slot->getUser());
359
360 uint32_t number_commands= UserCommands::getUserCounts();
361
362 for (uint32_t j= 0; j < number_commands; ++j)
363 {
364 push(user_commands->getUserCount(j));
365 }
366 ++record_number;
367 return true;
368 }
369 else
370 {
371 ++record_number;
372 }
373 }
374
375 return false;
376}
377
378CumulativeUserStatsTool::CumulativeUserStatsTool(LoggingStats *logging_stats) :
379 plugin::TableFunction("DATA_DICTIONARY", "CUMULATIVE_USER_STATS")
380{
381 outer_logging_stats= logging_stats;
382
383 add_field("USERNAME");
384 add_field("BYTES_RECEIVED");
385 add_field("BYTES_SENT");
386 add_field("DENIED_CONNECTIONS");
387 add_field("LOST_CONNECTIONS");
388 add_field("ACCESS_DENIED");
389 add_field("CONNECTED_TIME_SEC");
390 add_field("EXECUTION_TIME_NSEC");
391 add_field("ROWS_FETCHED");
392 add_field("ROWS_UPDATED");
393 add_field("ROWS_DELETED");
394 add_field("ROWS_INSERTED");
395}
396
397CumulativeUserStatsTool::Generator::Generator(Field **arg, LoggingStats *logging_stats) :
398 plugin::TableFunction::Generator(arg)
399{
400 inner_logging_stats= logging_stats;
401 record_number= 0;
402
403 if (inner_logging_stats->isEnabled())
404 {
405 last_valid_index= inner_logging_stats->getCumulativeStats()->getCumulativeStatsLastValidIndex();
406 }
407 else
408 {
409 last_valid_index= INVALID_INDEX;
410 }
411}
412
413bool CumulativeUserStatsTool::Generator::populate()
414{
415 if ((record_number > last_valid_index) || (last_valid_index == INVALID_INDEX))
416 {
417 return false;
418 }
419
420 while (record_number <= last_valid_index)
421 {
422 ScoreboardSlot *cumulative_scoreboard_slot=
423 inner_logging_stats->getCumulativeStats()->getCumulativeStatsByUserVector()->at(record_number);
424
425 if (cumulative_scoreboard_slot->isInUse())
426 {
427 StatusVars *status_vars= cumulative_scoreboard_slot->getStatusVars();
428 push(cumulative_scoreboard_slot->getUser());
429
430 push(status_vars->getStatusVarCounters()->bytes_received);
431 push(status_vars->getStatusVarCounters()->bytes_sent);
432 push(status_vars->getStatusVarCounters()->aborted_connects);
433 push(status_vars->getStatusVarCounters()->aborted_threads);
434 push(status_vars->getStatusVarCounters()->access_denied);
435 push(status_vars->getStatusVarCounters()->connection_time);
436 push(status_vars->getStatusVarCounters()->execution_time_nsec);
437 push(status_vars->sent_row_count);
438 push(status_vars->getStatusVarCounters()->updated_row_count);
439 push(status_vars->getStatusVarCounters()->deleted_row_count);
440 push(status_vars->getStatusVarCounters()->inserted_row_count);
441
442 ++record_number;
443 return true;
444 }
445 else
446 {
447 ++record_number;
448 }
449 }
450
451 return false;
452}
453
454ScoreboardStatsTool::ScoreboardStatsTool(LoggingStats *logging_stats) :
455 plugin::TableFunction("DATA_DICTIONARY", "SCOREBOARD_STATISTICS")
456{
457 outer_logging_stats= logging_stats;
458
459 add_field("SCOREBOARD_SIZE", TableFunction::NUMBER);
460 add_field("NUMBER_OF_RANGE_LOCKS", TableFunction::NUMBER);
461 add_field("MAX_USERS_LOGGED", TableFunction::NUMBER);
462 add_field("MEMORY_USAGE_BYTES", TableFunction::NUMBER);
463}
464
465ScoreboardStatsTool::Generator::Generator(Field **arg, LoggingStats *logging_stats) :
466 plugin::TableFunction::Generator(arg)
467{
468 inner_logging_stats= logging_stats;
469 is_last_record= false;
470}
471
472bool ScoreboardStatsTool::Generator::populate()
473{
474 if (is_last_record)
475 {
476 return false;
477 }
478
479 Scoreboard *scoreboard= inner_logging_stats->getCurrentScoreboard();
480 CumulativeStats *cumulativeStats= inner_logging_stats->getCumulativeStats();
481
482 push(static_cast<uint64_t>(scoreboard->getNumberPerBucket() * scoreboard->getNumberBuckets()));
483 push(static_cast<uint64_t>(scoreboard->getNumberBuckets()));
484 push(static_cast<uint64_t>(cumulativeStats->getCumulativeStatsByUserMax()));
485 push(cumulativeStats->getCumulativeSizeBytes() + scoreboard->getScoreboardSizeBytes());
486
487 is_last_record= true;
488
489 return true;
490}
TODO: Rename this file - func.h is stupid.
STL namespace.