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 "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
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
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
00104
00105
00106
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
00155 vector<vector<ScoreboardSlot* >* > *vector_of_scoreboard_vectors=
00156 scoreboard->getVectorOfScoreboardVectors();
00157
00158
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 }