Drizzled Public API Documentation

cumulative_stats.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 
00030 #include <config.h>
00031 #include <drizzled/statistics_variables.h>
00032 #include "cumulative_stats.h"
00033 
00034 using namespace std;
00035 using namespace drizzled;
00036 
00037 CumulativeStats::CumulativeStats(uint32_t in_cumulative_stats_by_user_max) 
00038     :
00039       cumulative_stats_by_user_max(in_cumulative_stats_by_user_max)  
00040 {
00041   cumulative_stats_by_user_vector= new vector<ScoreboardSlot *>(cumulative_stats_by_user_max);
00042 
00043   vector<ScoreboardSlot *>::iterator it= cumulative_stats_by_user_vector->begin();
00044   for (int32_t j=0; j < cumulative_stats_by_user_max; ++j)
00045   {
00046     ScoreboardSlot *scoreboard_slot= new ScoreboardSlot();
00047     it= cumulative_stats_by_user_vector->insert(it, scoreboard_slot);
00048   }
00049   cumulative_stats_by_user_vector->resize(cumulative_stats_by_user_max);
00050 
00051   last_valid_index= INVALID_INDEX;
00052   isOpenUserSlots= true;
00053   global_stats= new GlobalStats();
00054   global_status_vars= new StatusVars();
00055 
00056   /* calculate the approximate memory allocation for the cumulative statistics */
00057   size_t statusVarsSize= sizeof(StatusVars) + sizeof(system_status_var);
00058   size_t userCommandsSize= sizeof(UserCommands) + sizeof(uint64_t) * SQLCOM_END;
00059 
00060   cumulative_size_bytes= (statusVarsSize + userCommandsSize) * cumulative_stats_by_user_max; 
00061 }
00062 
00063 CumulativeStats::~CumulativeStats()
00064 {
00065   vector<ScoreboardSlot *>::iterator it= cumulative_stats_by_user_vector->begin();
00066   for (; it < cumulative_stats_by_user_vector->end(); ++it)
00067   {
00068     delete *it;
00069   }
00070   cumulative_stats_by_user_vector->clear();
00071   delete cumulative_stats_by_user_vector;
00072   delete global_stats;
00073   delete global_status_vars;
00074 }
00075 
00076 void CumulativeStats::logUserStats(ScoreboardSlot *scoreboard_slot, bool reserveSlot)
00077 {
00078   vector<ScoreboardSlot *>::iterator cumulative_it= cumulative_stats_by_user_vector->begin();
00079 
00080   /* Search if this is a pre-existing user */
00081 
00082   int32_t current_index= last_valid_index;
00083 
00084   if (cumulative_stats_by_user_max <= current_index)
00085   {
00086     current_index= cumulative_stats_by_user_max;
00087   }
00088 
00089   for (int32_t j=0; j <= current_index; ++j)
00090   {
00091     ScoreboardSlot *cumulative_scoreboard_slot= *cumulative_it;
00092     string user= cumulative_scoreboard_slot->getUser();
00093     if (user.compare(scoreboard_slot->getUser()) == 0)
00094     {
00095       reserveSlot= false;
00096       cumulative_scoreboard_slot->merge(scoreboard_slot);
00097       break;
00098     }
00099     ++cumulative_it;
00100   }
00101   
00102   if (reserveSlot)
00103   {
00104     /* the user was not found */
00105     /* its possible multiple simultaneous connections with the same user
00106        could result in duplicate entries here, not likely and it would
00107        be harmless except for duplicate users showing up in a query */
00108     
00109     if (hasOpenUserSlots())
00110     { 
00111       int32_t our_index= last_valid_index.add_and_fetch(1);
00112       if (our_index < cumulative_stats_by_user_max)
00113       {
00114         ScoreboardSlot *cumulative_scoreboard_slot=
00115           cumulative_stats_by_user_vector->at(our_index);
00116         cumulative_scoreboard_slot->setUser(scoreboard_slot->getUser());
00117         cumulative_scoreboard_slot->merge(scoreboard_slot);
00118         cumulative_scoreboard_slot->setInUse(true);
00119       } 
00120       else 
00121       {
00122         last_valid_index= cumulative_stats_by_user_max - 1; 
00123         isOpenUserSlots= false;
00124       }
00125     } 
00126   }
00127 }
00128 
00129 void CumulativeStats::logGlobalStats(ScoreboardSlot* scoreboard_slot)
00130 {
00131   global_stats->updateUserCommands(scoreboard_slot); 
00132 }
00133 
00134 void CumulativeStats::logGlobalStatusVars(ScoreboardSlot* scoreboard_slot)
00135 {
00136   global_status_vars->merge(scoreboard_slot->getStatusVars());
00137 }
00138 
00139 int32_t CumulativeStats::getCumulativeStatsLastValidIndex() const
00140 {
00141   return last_valid_index < cumulative_stats_by_user_max ? last_valid_index : cumulative_stats_by_user_max;
00142 }
00143 
00144 void CumulativeStats::sumCurrentScoreboard(Scoreboard *scoreboard, StatusVars *current_status_vars, UserCommands *current_user_commands)
00145 {
00146   /* the vector of vectors */
00147   vector<vector<ScoreboardSlot* >* > *vector_of_scoreboard_vectors= scoreboard->getVectorOfScoreboardVectors();
00148 
00149   /* iterate through each vector from above and sum each ScoreboardSlot */
00150 
00151   vector<vector<ScoreboardSlot* >* >::iterator v_of_scoreboard_v_begin_it= vector_of_scoreboard_vectors->begin(); 
00152   vector<vector<ScoreboardSlot* >* >::iterator v_of_scoreboard_v_end_it= vector_of_scoreboard_vectors->end(); 
00153 
00154   for (; v_of_scoreboard_v_begin_it != v_of_scoreboard_v_end_it; ++v_of_scoreboard_v_begin_it)
00155   {
00156     vector<ScoreboardSlot* > *scoreboard_vector= *v_of_scoreboard_v_begin_it;
00157     
00158     vector<ScoreboardSlot* >::iterator scoreboard_vector_it= scoreboard_vector->begin();
00159     vector<ScoreboardSlot* >::iterator scoreboard_vector_end= scoreboard_vector->end();
00160     for (; scoreboard_vector_it != scoreboard_vector_end; ++scoreboard_vector_it)
00161     {
00162       ScoreboardSlot *scoreboard_slot= *scoreboard_vector_it;
00163       if (scoreboard_slot->isInUse())
00164       {
00165         if (current_status_vars)
00166         {
00167           current_status_vars->merge(scoreboard_slot->getStatusVars());
00168         }
00169 
00170         if (current_user_commands)
00171         {
00172           current_user_commands->merge(scoreboard_slot->getUserCommands());
00173         }
00174       }
00175     }
00176   }
00177 }