00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
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
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
00105
00106
00107
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
00147 vector<vector<ScoreboardSlot* >* > *vector_of_scoreboard_vectors= scoreboard->getVectorOfScoreboardVectors();
00148
00149
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 }