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