2
* Copyright (C) 2010 Joseph Daly <skinny.moey@gmail.com>
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
8
* * Redistributions of source code must retain the above copyright notice,
9
* this list of conditions and the following disclaimer.
10
* * Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
13
* * Neither the name of Joseph Daly nor the names of its contributors
14
* may be used to endorse or promote products derived from this software
15
* without specific prior written permission.
17
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27
* THE POSSIBILITY OF SUCH DAMAGE.
31
#include "cumulative_stats.h"
34
using namespace drizzled;
36
CumulativeStats::CumulativeStats(uint32_t in_cumulative_stats_by_user_max)
38
cumulative_stats_by_user_max(in_cumulative_stats_by_user_max)
40
cumulative_stats_by_user_vector= new vector<ScoreboardSlot *>(cumulative_stats_by_user_max);
42
vector<ScoreboardSlot *>::iterator it= cumulative_stats_by_user_vector->begin();
43
for (int32_t j=0; j < cumulative_stats_by_user_max; ++j)
45
ScoreboardSlot *scoreboard_slot= new ScoreboardSlot();
46
it= cumulative_stats_by_user_vector->insert(it, scoreboard_slot);
48
cumulative_stats_by_user_vector->resize(cumulative_stats_by_user_max);
50
last_valid_index= INVALID_INDEX;
51
isOpenUserSlots= true;
52
global_stats= new GlobalStats();
53
global_status_vars= new StatusVars();
55
/* calculate the approximate memory allocation for the cumulative statistics */
56
size_t statusVarsSize= sizeof(StatusVars) + sizeof(system_status_var);
57
size_t userCommandsSize= sizeof(UserCommands) + sizeof(uint64_t) * SQLCOM_END;
59
cumulative_size_bytes= (statusVarsSize + userCommandsSize) * cumulative_stats_by_user_max;
62
CumulativeStats::~CumulativeStats()
64
vector<ScoreboardSlot *>::iterator it= cumulative_stats_by_user_vector->begin();
65
for (; it < cumulative_stats_by_user_vector->end(); ++it)
69
cumulative_stats_by_user_vector->clear();
70
delete cumulative_stats_by_user_vector;
72
delete global_status_vars;
75
void CumulativeStats::logUserStats(ScoreboardSlot *scoreboard_slot, bool reserveSlot)
77
vector<ScoreboardSlot *>::iterator cumulative_it= cumulative_stats_by_user_vector->begin();
79
/* Search if this is a pre-existing user */
81
int32_t current_index= last_valid_index;
83
if (cumulative_stats_by_user_max <= current_index)
85
current_index= cumulative_stats_by_user_max;
88
for (int32_t j=0; j <= current_index; ++j)
90
ScoreboardSlot *cumulative_scoreboard_slot= *cumulative_it;
91
string user= cumulative_scoreboard_slot->getUser();
92
if (user.compare(scoreboard_slot->getUser()) == 0)
95
cumulative_scoreboard_slot->merge(scoreboard_slot);
103
/* the user was not found */
104
/* its possible multiple simultaneous connections with the same user
105
could result in duplicate entries here, not likely and it would
106
be harmless except for duplicate users showing up in a query */
108
if (hasOpenUserSlots())
110
int32_t our_index= last_valid_index.add_and_fetch(1);
111
if (our_index < cumulative_stats_by_user_max)
113
ScoreboardSlot *cumulative_scoreboard_slot=
114
cumulative_stats_by_user_vector->at(our_index);
115
cumulative_scoreboard_slot->setUser(scoreboard_slot->getUser());
116
cumulative_scoreboard_slot->merge(scoreboard_slot);
117
cumulative_scoreboard_slot->setInUse(true);
121
last_valid_index= cumulative_stats_by_user_max - 1;
122
isOpenUserSlots= false;
128
void CumulativeStats::logGlobalStats(ScoreboardSlot* scoreboard_slot)
130
global_stats->updateUserCommands(scoreboard_slot);
133
void CumulativeStats::logGlobalStatusVars(ScoreboardSlot* scoreboard_slot)
135
global_status_vars->merge(scoreboard_slot->getStatusVars());
138
int32_t CumulativeStats::getCumulativeStatsLastValidIndex()
140
if (last_valid_index < cumulative_stats_by_user_max)
142
return last_valid_index;
146
return cumulative_stats_by_user_max;
150
void CumulativeStats::sumCurrentScoreboard(Scoreboard *scoreboard,
151
StatusVars *current_status_vars,
152
UserCommands *current_user_commands)
154
/* the vector of vectors */
155
vector<vector<ScoreboardSlot* >* > *vector_of_scoreboard_vectors=
156
scoreboard->getVectorOfScoreboardVectors();
158
/* iterate through each vector from above and sum each ScoreboardSlot */
160
vector<vector<ScoreboardSlot* >* >::iterator v_of_scoreboard_v_begin_it= vector_of_scoreboard_vectors->begin();
162
vector<vector<ScoreboardSlot* >* >::iterator v_of_scoreboard_v_end_it= vector_of_scoreboard_vectors->end();
164
for (; v_of_scoreboard_v_begin_it != v_of_scoreboard_v_end_it; ++v_of_scoreboard_v_begin_it)
166
vector<ScoreboardSlot* > *scoreboard_vector= *v_of_scoreboard_v_begin_it;
168
vector<ScoreboardSlot* >::iterator scoreboard_vector_it= scoreboard_vector->begin();
169
vector<ScoreboardSlot* >::iterator scoreboard_vector_end= scoreboard_vector->end();
170
for (; scoreboard_vector_it != scoreboard_vector_end; ++scoreboard_vector_it)
172
ScoreboardSlot *scoreboard_slot= *scoreboard_vector_it;
173
if (scoreboard_slot->isInUse())
175
if (current_status_vars)
177
current_status_vars->merge(scoreboard_slot->getStatusVars());
180
if (current_user_commands)
182
current_user_commands->merge(scoreboard_slot->getUserCommands());