Drizzled Public API Documentation

cumulative_stats.cc
1 /*
2  * Copyright (C) 2010 Joseph Daly <skinny.moey@gmail.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
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.
16  *
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.
28  */
29 
30 #include <config.h>
31 #include <drizzled/statistics_variables.h>
32 #include "cumulative_stats.h"
33 
34 using namespace std;
35 using namespace drizzled;
36 
37 CumulativeStats::CumulativeStats(uint32_t in_cumulative_stats_by_user_max)
38  :
39  cumulative_stats_by_user_max(in_cumulative_stats_by_user_max)
40 {
41  cumulative_stats_by_user_vector= new vector<ScoreboardSlot *>(cumulative_stats_by_user_max);
42 
43  vector<ScoreboardSlot *>::iterator it= cumulative_stats_by_user_vector->begin();
44  for (int32_t j=0; j < cumulative_stats_by_user_max; ++j)
45  {
46  ScoreboardSlot *scoreboard_slot= new ScoreboardSlot();
47  it= cumulative_stats_by_user_vector->insert(it, scoreboard_slot);
48  }
49  cumulative_stats_by_user_vector->resize(cumulative_stats_by_user_max);
50 
51  last_valid_index= INVALID_INDEX;
52  isOpenUserSlots= true;
53  global_stats= new GlobalStats();
54  global_status_vars= new StatusVars();
55 
56  /* calculate the approximate memory allocation for the cumulative statistics */
57  size_t statusVarsSize= sizeof(StatusVars) + sizeof(system_status_var);
58  size_t userCommandsSize= sizeof(UserCommands) + sizeof(uint64_t) * SQLCOM_END;
59 
60  cumulative_size_bytes= (statusVarsSize + userCommandsSize) * cumulative_stats_by_user_max;
61 }
62 
63 CumulativeStats::~CumulativeStats()
64 {
65  vector<ScoreboardSlot *>::iterator it= cumulative_stats_by_user_vector->begin();
66  for (; it < cumulative_stats_by_user_vector->end(); ++it)
67  {
68  delete *it;
69  }
70  cumulative_stats_by_user_vector->clear();
71  delete cumulative_stats_by_user_vector;
72  delete global_stats;
73  delete global_status_vars;
74 }
75 
76 void CumulativeStats::logUserStats(ScoreboardSlot *scoreboard_slot, bool reserveSlot)
77 {
78  vector<ScoreboardSlot *>::iterator cumulative_it= cumulative_stats_by_user_vector->begin();
79 
80  /* Search if this is a pre-existing user */
81 
82  int32_t current_index= last_valid_index;
83 
84  if (cumulative_stats_by_user_max <= current_index)
85  {
86  current_index= cumulative_stats_by_user_max;
87  }
88 
89  for (int32_t j=0; j <= current_index; ++j)
90  {
91  ScoreboardSlot *cumulative_scoreboard_slot= *cumulative_it;
92  string user= cumulative_scoreboard_slot->getUser();
93  if (user.compare(scoreboard_slot->getUser()) == 0)
94  {
95  reserveSlot= false;
96  cumulative_scoreboard_slot->merge(scoreboard_slot);
97  break;
98  }
99  ++cumulative_it;
100  }
101 
102  if (reserveSlot)
103  {
104  /* the user was not found */
105  /* its possible multiple simultaneous connections with the same user
106  could result in duplicate entries here, not likely and it would
107  be harmless except for duplicate users showing up in a query */
108 
109  if (hasOpenUserSlots())
110  {
111  int32_t our_index= last_valid_index.add_and_fetch(1);
112  if (our_index < cumulative_stats_by_user_max)
113  {
114  ScoreboardSlot *cumulative_scoreboard_slot=
115  cumulative_stats_by_user_vector->at(our_index);
116  cumulative_scoreboard_slot->setUser(scoreboard_slot->getUser());
117  cumulative_scoreboard_slot->merge(scoreboard_slot);
118  cumulative_scoreboard_slot->setInUse(true);
119  }
120  else
121  {
122  last_valid_index= cumulative_stats_by_user_max - 1;
123  isOpenUserSlots= false;
124  }
125  }
126  }
127 }
128 
129 void CumulativeStats::logGlobalStats(ScoreboardSlot* scoreboard_slot)
130 {
131  global_stats->updateUserCommands(scoreboard_slot);
132 }
133 
134 void CumulativeStats::logGlobalStatusVars(ScoreboardSlot* scoreboard_slot)
135 {
136  global_status_vars->merge(scoreboard_slot->getStatusVars());
137 }
138 
139 int32_t CumulativeStats::getCumulativeStatsLastValidIndex() const
140 {
141  return last_valid_index < cumulative_stats_by_user_max ? last_valid_index : cumulative_stats_by_user_max;
142 }
143 
144 void CumulativeStats::sumCurrentScoreboard(Scoreboard *scoreboard, StatusVars *current_status_vars, UserCommands *current_user_commands)
145 {
146  /* the vector of vectors */
147  vector<vector<ScoreboardSlot* >* > *vector_of_scoreboard_vectors= scoreboard->getVectorOfScoreboardVectors();
148 
149  /* iterate through each vector from above and sum each ScoreboardSlot */
150 
151  vector<vector<ScoreboardSlot* >* >::iterator v_of_scoreboard_v_begin_it= vector_of_scoreboard_vectors->begin();
152  vector<vector<ScoreboardSlot* >* >::iterator v_of_scoreboard_v_end_it= vector_of_scoreboard_vectors->end();
153 
154  for (; v_of_scoreboard_v_begin_it != v_of_scoreboard_v_end_it; ++v_of_scoreboard_v_begin_it)
155  {
156  vector<ScoreboardSlot* > *scoreboard_vector= *v_of_scoreboard_v_begin_it;
157 
158  vector<ScoreboardSlot* >::iterator scoreboard_vector_it= scoreboard_vector->begin();
159  vector<ScoreboardSlot* >::iterator scoreboard_vector_end= scoreboard_vector->end();
160  for (; scoreboard_vector_it != scoreboard_vector_end; ++scoreboard_vector_it)
161  {
162  ScoreboardSlot *scoreboard_slot= *scoreboard_vector_it;
163  if (scoreboard_slot->isInUse())
164  {
165  if (current_status_vars)
166  {
167  current_status_vars->merge(scoreboard_slot->getStatusVars());
168  }
169 
170  if (current_user_commands)
171  {
172  current_user_commands->merge(scoreboard_slot->getUserCommands());
173  }
174  }
175  }
176  }
177 }