Drizzled Public API Documentation

stats_schema.cc
00001 /*
00002  * Copyright (C) 2010 Joseph Daly <skinny.moey@gmail.com>
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *   * Redistributions of source code must retain the above copyright notice,
00009  *     this list of conditions and the following disclaimer.
00010  *   * Redistributions in binary form must reproduce the above copyright notice,
00011  *     this list of conditions and the following disclaimer in the documentation
00012  *     and/or other materials provided with the distribution.
00013  *   * Neither the name of Joseph Daly nor the names of its contributors
00014  *     may be used to endorse or promote products derived from this software
00015  *     without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
00027  * THE POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00106 #include <config.h>
00107 #include <drizzled/statistics_variables.h>
00108 #include "stats_schema.h"
00109 #include <sstream>
00110 
00111 using namespace drizzled;
00112 using namespace plugin;
00113 using namespace std;
00114 
00115 SessionStatementsTool::SessionStatementsTool(LoggingStats *in_logging_stats) :
00116   plugin::TableFunction("DATA_DICTIONARY", "SESSION_STATEMENTS")
00117 {
00118   logging_stats= in_logging_stats;
00119   add_field("VARIABLE_NAME");
00120   add_field("VARIABLE_VALUE", 1024);
00121 }
00122 
00123 SessionStatementsTool::Generator::Generator(Field **arg, LoggingStats *in_logging_stats) :
00124   plugin::TableFunction::Generator(arg)
00125 {
00126   count= 0;
00127 
00128   /* Set user_commands */
00129   Scoreboard *current_scoreboard= in_logging_stats->getCurrentScoreboard();
00130 
00131   uint32_t bucket_number= current_scoreboard->getBucketNumber(&getSession());
00132 
00133   std::vector<ScoreboardSlot* > *scoreboard_vector=
00134      current_scoreboard->getVectorOfScoreboardVectors()->at(bucket_number);
00135 
00136   ScoreboardSlot *scoreboard_slot= NULL;
00137   for (std::vector<ScoreboardSlot *>::iterator it= scoreboard_vector->begin();
00138        it != scoreboard_vector->end(); ++it)
00139   {
00140     scoreboard_slot= *it;
00141     if (scoreboard_slot->getSessionId() == getSession().getSessionId())
00142     {
00143       break;
00144     }
00145   }
00146 
00147   user_commands= NULL;
00148 
00149   if (scoreboard_slot != NULL)
00150   {
00151     user_commands= scoreboard_slot->getUserCommands();
00152   }
00153 }
00154 
00155 bool SessionStatementsTool::Generator::populate()
00156 {
00157   if (user_commands == NULL)
00158   {
00159     return false;
00160   } 
00161 
00162   uint32_t number_identifiers= UserCommands::getStatusVarsCount();
00163 
00164   if (count == number_identifiers)
00165   {
00166     return false;
00167   }
00168 
00169   push(UserCommands::COM_STATUS_VARS[count]);
00170   ostringstream oss;
00171   oss << user_commands->getCount(count);
00172   push(oss.str()); 
00173 
00174   ++count;
00175   return true;
00176 }
00177 
00178 GlobalStatementsTool::GlobalStatementsTool(LoggingStats *in_logging_stats) :
00179   plugin::TableFunction("DATA_DICTIONARY", "GLOBAL_STATEMENTS")
00180 {   
00181   logging_stats= in_logging_stats;
00182   add_field("VARIABLE_NAME");
00183   add_field("VARIABLE_VALUE", 1024);
00184 }
00185 
00186 GlobalStatementsTool::Generator::Generator(Field **arg, LoggingStats *in_logging_stats) :
00187   plugin::TableFunction::Generator(arg)
00188 {
00189   count= 0;
00190   /* add the current scoreboard and the saved global statements */
00191   global_stats_to_display= new GlobalStats();
00192   CumulativeStats *cumulativeStats= in_logging_stats->getCumulativeStats();
00193   cumulativeStats->sumCurrentScoreboard(in_logging_stats->getCurrentScoreboard(), 
00194                                         NULL, global_stats_to_display->getUserCommands());
00195   global_stats_to_display->merge(in_logging_stats->getCumulativeStats()->getGlobalStats()); 
00196 }
00197 
00198 GlobalStatementsTool::Generator::~Generator()
00199 {
00200   delete global_stats_to_display;
00201 }
00202 
00203 bool GlobalStatementsTool::Generator::populate()
00204 {
00205   uint32_t number_identifiers= UserCommands::getStatusVarsCount(); 
00206   if (count == number_identifiers)
00207   {
00208     return false;
00209   }
00210 
00211   push(UserCommands::COM_STATUS_VARS[count]);
00212   ostringstream oss;
00213   oss << global_stats_to_display->getUserCommands()->getCount(count);
00214   push(oss.str());
00215 
00216   ++count;
00217   return true;
00218 }
00219 
00220 CurrentCommandsTool::CurrentCommandsTool(LoggingStats *logging_stats) :
00221   plugin::TableFunction("DATA_DICTIONARY", "CURRENT_SQL_COMMANDS")
00222 {
00223   outer_logging_stats= logging_stats;
00224 
00225   add_field("USERNAME");
00226   add_field("IP");
00227 
00228   uint32_t number_commands= UserCommands::getUserCounts();
00229 
00230   for (uint32_t j= 0; j < number_commands; ++j)
00231   {
00232     add_field(UserCommands::USER_COUNTS[j], TableFunction::NUMBER);
00233   } 
00234 }
00235 
00236 CurrentCommandsTool::Generator::Generator(Field **arg, LoggingStats *logging_stats) :
00237   plugin::TableFunction::Generator(arg)
00238 {
00239   inner_logging_stats= logging_stats;
00240 
00241   isEnabled= inner_logging_stats->isEnabled();
00242 
00243   if (isEnabled == false)
00244   {
00245     return;
00246   }
00247 
00248   current_scoreboard= logging_stats->getCurrentScoreboard();
00249   current_bucket= 0;
00250 
00251   vector_of_scoreboard_vectors_it= current_scoreboard->getVectorOfScoreboardVectors()->begin();
00252   vector_of_scoreboard_vectors_end= current_scoreboard->getVectorOfScoreboardVectors()->end();
00253 
00254   setVectorIteratorsAndLock(current_bucket);
00255 }
00256 
00257 void CurrentCommandsTool::Generator::setVectorIteratorsAndLock(uint32_t bucket_number)
00258 {
00259   std::vector<ScoreboardSlot* > *scoreboard_vector= 
00260     current_scoreboard->getVectorOfScoreboardVectors()->at(bucket_number); 
00261 
00262   current_lock= current_scoreboard->getVectorOfScoreboardLocks()->at(bucket_number);
00263 
00264   scoreboard_vector_it= scoreboard_vector->begin();
00265   scoreboard_vector_end= scoreboard_vector->end();
00266   current_lock->lock_shared();
00267 }
00268 
00269 bool CurrentCommandsTool::Generator::populate()
00270 {
00271   if (isEnabled == false)
00272   {
00273     return false;
00274   }
00275 
00276   while (vector_of_scoreboard_vectors_it != vector_of_scoreboard_vectors_end)
00277   {
00278     while (scoreboard_vector_it != scoreboard_vector_end)
00279     {
00280       ScoreboardSlot *scoreboard_slot= *scoreboard_vector_it; 
00281       if (scoreboard_slot->isInUse())
00282       {
00283         UserCommands *user_commands= scoreboard_slot->getUserCommands();
00284         push(scoreboard_slot->getUser());
00285         push(scoreboard_slot->getIp());
00286 
00287         uint32_t number_commands= UserCommands::getUserCounts(); 
00288 
00289         for (uint32_t j= 0; j < number_commands; ++j)
00290         {
00291           push(user_commands->getUserCount(j));
00292         }
00293 
00294         ++scoreboard_vector_it;
00295         return true;
00296       }
00297       ++scoreboard_vector_it;
00298     }
00299     
00300     ++vector_of_scoreboard_vectors_it;
00301     current_lock->unlock_shared();
00302     ++current_bucket;
00303     if (vector_of_scoreboard_vectors_it != vector_of_scoreboard_vectors_end)
00304     {
00305       setVectorIteratorsAndLock(current_bucket); 
00306     } 
00307   }
00308 
00309   return false;
00310 }
00311 
00312 CumulativeCommandsTool::CumulativeCommandsTool(LoggingStats *logging_stats) :
00313   plugin::TableFunction("DATA_DICTIONARY", "CUMULATIVE_SQL_COMMANDS")
00314 {
00315   outer_logging_stats= logging_stats;
00316 
00317   add_field("USERNAME");
00318 
00319   uint32_t number_commands= UserCommands::getUserCounts();
00320 
00321   for (uint32_t j= 0; j < number_commands; ++j)
00322   {
00323     add_field(UserCommands::USER_COUNTS[j], TableFunction::NUMBER);
00324   }
00325 }
00326 
00327 CumulativeCommandsTool::Generator::Generator(Field **arg, LoggingStats *logging_stats) :
00328   plugin::TableFunction::Generator(arg)
00329 {
00330   inner_logging_stats= logging_stats;
00331   record_number= 0;
00332 
00333   if (inner_logging_stats->isEnabled())
00334   {
00335     last_valid_index= inner_logging_stats->getCumulativeStats()->getCumulativeStatsLastValidIndex();
00336   }
00337   else
00338   {
00339     last_valid_index= INVALID_INDEX; 
00340   }
00341 }
00342 
00343 bool CumulativeCommandsTool::Generator::populate()
00344 {
00345   if ((record_number > last_valid_index) || (last_valid_index == INVALID_INDEX))
00346   {
00347     return false;
00348   }
00349 
00350   while (record_number <= last_valid_index)
00351   {
00352     ScoreboardSlot *cumulative_scoreboard_slot= 
00353       inner_logging_stats->getCumulativeStats()->getCumulativeStatsByUserVector()->at(record_number);
00354 
00355     if (cumulative_scoreboard_slot->isInUse())
00356     {
00357       UserCommands *user_commands= cumulative_scoreboard_slot->getUserCommands(); 
00358       push(cumulative_scoreboard_slot->getUser());
00359 
00360       uint32_t number_commands= UserCommands::getUserCounts();
00361 
00362       for (uint32_t j= 0; j < number_commands; ++j)
00363       {
00364         push(user_commands->getUserCount(j));
00365       }
00366       ++record_number;
00367       return true;
00368     } 
00369     else 
00370     {
00371       ++record_number;
00372     }
00373   }
00374 
00375   return false;
00376 }
00377 
00378 CumulativeUserStatsTool::CumulativeUserStatsTool(LoggingStats *logging_stats) :
00379   plugin::TableFunction("DATA_DICTIONARY", "CUMULATIVE_USER_STATS")
00380 {
00381   outer_logging_stats= logging_stats;
00382 
00383   add_field("USERNAME");
00384   add_field("BYTES_RECEIVED");
00385   add_field("BYTES_SENT");
00386   add_field("DENIED_CONNECTIONS");
00387   add_field("LOST_CONNECTIONS");
00388   add_field("ACCESS_DENIED");
00389   add_field("CONNECTED_TIME_SEC");
00390   add_field("EXECUTION_TIME_NSEC");
00391   add_field("ROWS_FETCHED");
00392   add_field("ROWS_UPDATED");
00393   add_field("ROWS_DELETED");
00394   add_field("ROWS_INSERTED");
00395 }
00396 
00397 CumulativeUserStatsTool::Generator::Generator(Field **arg, LoggingStats *logging_stats) :
00398   plugin::TableFunction::Generator(arg)
00399 {
00400   inner_logging_stats= logging_stats;
00401   record_number= 0;
00402 
00403   if (inner_logging_stats->isEnabled())
00404   {
00405     last_valid_index= inner_logging_stats->getCumulativeStats()->getCumulativeStatsLastValidIndex();
00406   }
00407   else
00408   {
00409     last_valid_index= INVALID_INDEX;
00410   }
00411 }
00412 
00413 bool CumulativeUserStatsTool::Generator::populate()
00414 {
00415   if ((record_number > last_valid_index) || (last_valid_index == INVALID_INDEX))
00416   {
00417     return false;
00418   }
00419 
00420   while (record_number <= last_valid_index)
00421   {
00422     ScoreboardSlot *cumulative_scoreboard_slot=
00423       inner_logging_stats->getCumulativeStats()->getCumulativeStatsByUserVector()->at(record_number);
00424 
00425     if (cumulative_scoreboard_slot->isInUse())
00426     {
00427       StatusVars *status_vars= cumulative_scoreboard_slot->getStatusVars();
00428       push(cumulative_scoreboard_slot->getUser());
00429 
00430       push(status_vars->getStatusVarCounters()->bytes_received);
00431       push(status_vars->getStatusVarCounters()->bytes_sent);
00432       push(status_vars->getStatusVarCounters()->aborted_connects);
00433       push(status_vars->getStatusVarCounters()->aborted_threads);
00434       push(status_vars->getStatusVarCounters()->access_denied);
00435       push(status_vars->getStatusVarCounters()->connection_time);
00436       push(status_vars->getStatusVarCounters()->execution_time_nsec);
00437       push(status_vars->sent_row_count);
00438       push(status_vars->getStatusVarCounters()->updated_row_count);
00439       push(status_vars->getStatusVarCounters()->deleted_row_count);
00440       push(status_vars->getStatusVarCounters()->inserted_row_count);
00441 
00442       ++record_number;
00443       return true;
00444     }
00445     else
00446     {
00447       ++record_number;
00448     }
00449   }
00450 
00451   return false;
00452 }
00453 
00454 ScoreboardStatsTool::ScoreboardStatsTool(LoggingStats *logging_stats) :
00455   plugin::TableFunction("DATA_DICTIONARY", "SCOREBOARD_STATISTICS")
00456 {
00457   outer_logging_stats= logging_stats;
00458 
00459   add_field("SCOREBOARD_SIZE", TableFunction::NUMBER);
00460   add_field("NUMBER_OF_RANGE_LOCKS", TableFunction::NUMBER);
00461   add_field("MAX_USERS_LOGGED", TableFunction::NUMBER);
00462   add_field("MEMORY_USAGE_BYTES", TableFunction::NUMBER);
00463 }
00464 
00465 ScoreboardStatsTool::Generator::Generator(Field **arg, LoggingStats *logging_stats) :
00466   plugin::TableFunction::Generator(arg)
00467 {
00468   inner_logging_stats= logging_stats;
00469   is_last_record= false; 
00470 }
00471 
00472 bool ScoreboardStatsTool::Generator::populate()
00473 {
00474   if (is_last_record)
00475   {
00476     return false;
00477   }
00478 
00479   Scoreboard *scoreboard= inner_logging_stats->getCurrentScoreboard();
00480   CumulativeStats *cumulativeStats= inner_logging_stats->getCumulativeStats();
00481 
00482   push(static_cast<uint64_t>(scoreboard->getNumberPerBucket() * scoreboard->getNumberBuckets()));
00483   push(static_cast<uint64_t>(scoreboard->getNumberBuckets()));
00484   push(static_cast<uint64_t>(cumulativeStats->getCumulativeStatsByUserMax()));
00485   push(cumulativeStats->getCumulativeSizeBytes() + scoreboard->getScoreboardSizeBytes()); 
00486   
00487   is_last_record= true;
00488 
00489   return true;
00490 }