~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/logging_stats/cumulative_stats.cc

  • Committer: Monty Taylor
  • Date: 2010-03-02 19:10:25 UTC
  • mto: (1317.1.8)
  • mto: This revision was merged to the branch mainline in revision 1322.
  • Revision ID: mordred@inaugust.com-20100302191025-zoxjz4xwkoa6160h
Prevent unauthorized users from changing schema.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 "cumulative_stats.h"
32
 
 
33
 
using namespace std;
34
 
using namespace drizzled;
35
 
 
36
 
CumulativeStats::CumulativeStats(uint32_t in_cumulative_stats_by_user_max) 
37
 
    :
38
 
      cumulative_stats_by_user_max(in_cumulative_stats_by_user_max)  
39
 
{
40
 
  cumulative_stats_by_user_vector= new vector<ScoreboardSlot *>(cumulative_stats_by_user_max);
41
 
 
42
 
  vector<ScoreboardSlot *>::iterator it= cumulative_stats_by_user_vector->begin();
43
 
  for (int32_t j=0; j < cumulative_stats_by_user_max; ++j)
44
 
  {
45
 
    ScoreboardSlot *scoreboard_slot= new ScoreboardSlot();
46
 
    it= cumulative_stats_by_user_vector->insert(it, scoreboard_slot);
47
 
  }
48
 
  cumulative_stats_by_user_vector->resize(cumulative_stats_by_user_max);
49
 
 
50
 
  last_valid_index= INVALID_INDEX;
51
 
  isOpenUserSlots= true;
52
 
  global_stats= new GlobalStats();
53
 
  global_status_vars= new StatusVars();
54
 
 
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;
58
 
 
59
 
  cumulative_size_bytes= (statusVarsSize + userCommandsSize) * cumulative_stats_by_user_max; 
60
 
}
61
 
 
62
 
CumulativeStats::~CumulativeStats()
63
 
{
64
 
  vector<ScoreboardSlot *>::iterator it= cumulative_stats_by_user_vector->begin();
65
 
  for (; it < cumulative_stats_by_user_vector->end(); ++it)
66
 
  {
67
 
    delete *it;
68
 
  }
69
 
  cumulative_stats_by_user_vector->clear();
70
 
  delete cumulative_stats_by_user_vector;
71
 
  delete global_stats;
72
 
  delete global_status_vars;
73
 
}
74
 
 
75
 
void CumulativeStats::logUserStats(ScoreboardSlot *scoreboard_slot, bool reserveSlot)
76
 
{
77
 
  vector<ScoreboardSlot *>::iterator cumulative_it= cumulative_stats_by_user_vector->begin();
78
 
 
79
 
  /* Search if this is a pre-existing user */
80
 
 
81
 
  int32_t current_index= last_valid_index;
82
 
 
83
 
  if (cumulative_stats_by_user_max <= current_index)
84
 
  {
85
 
    current_index= cumulative_stats_by_user_max;
86
 
  }
87
 
 
88
 
  for (int32_t j=0; j <= current_index; ++j)
89
 
  {
90
 
    ScoreboardSlot *cumulative_scoreboard_slot= *cumulative_it;
91
 
    string user= cumulative_scoreboard_slot->getUser();
92
 
    if (user.compare(scoreboard_slot->getUser()) == 0)
93
 
    {
94
 
      reserveSlot= false;
95
 
      cumulative_scoreboard_slot->merge(scoreboard_slot);
96
 
      break;
97
 
    }
98
 
    ++cumulative_it;
99
 
  }
100
 
  
101
 
  if (reserveSlot)
102
 
  {
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 */
107
 
    
108
 
    if (hasOpenUserSlots())
109
 
    { 
110
 
      int32_t our_index= last_valid_index.add_and_fetch(1);
111
 
      if (our_index < cumulative_stats_by_user_max)
112
 
      {
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);
118
 
      } 
119
 
      else 
120
 
      {
121
 
        last_valid_index= cumulative_stats_by_user_max - 1; 
122
 
        isOpenUserSlots= false;
123
 
      }
124
 
    } 
125
 
  }
126
 
}
127
 
 
128
 
void CumulativeStats::logGlobalStats(ScoreboardSlot* scoreboard_slot)
129
 
{
130
 
  global_stats->updateUserCommands(scoreboard_slot); 
131
 
}
132
 
 
133
 
void CumulativeStats::logGlobalStatusVars(ScoreboardSlot* scoreboard_slot)
134
 
{
135
 
  global_status_vars->merge(scoreboard_slot->getStatusVars());
136
 
}
137
 
 
138
 
int32_t CumulativeStats::getCumulativeStatsLastValidIndex()
139
 
{
140
 
  if (last_valid_index < cumulative_stats_by_user_max)
141
 
  {
142
 
    return last_valid_index;
143
 
  } 
144
 
  else 
145
 
  {
146
 
    return cumulative_stats_by_user_max;
147
 
  }
148
 
}
149
 
 
150
 
void CumulativeStats::sumCurrentScoreboard(Scoreboard *scoreboard,
151
 
                                           StatusVars *current_status_vars,
152
 
                                           UserCommands *current_user_commands)
153
 
{
154
 
  /* the vector of vectors */
155
 
  vector<vector<ScoreboardSlot* >* > *vector_of_scoreboard_vectors= 
156
 
    scoreboard->getVectorOfScoreboardVectors();
157
 
 
158
 
  /* iterate through each vector from above and sum each ScoreboardSlot */
159
 
 
160
 
  vector<vector<ScoreboardSlot* >* >::iterator v_of_scoreboard_v_begin_it= vector_of_scoreboard_vectors->begin(); 
161
 
 
162
 
  vector<vector<ScoreboardSlot* >* >::iterator v_of_scoreboard_v_end_it= vector_of_scoreboard_vectors->end(); 
163
 
 
164
 
  for (; v_of_scoreboard_v_begin_it != v_of_scoreboard_v_end_it; ++v_of_scoreboard_v_begin_it)
165
 
  {
166
 
    vector<ScoreboardSlot* > *scoreboard_vector= *v_of_scoreboard_v_begin_it;
167
 
    
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)
171
 
    {
172
 
      ScoreboardSlot *scoreboard_slot= *scoreboard_vector_it;
173
 
      if (scoreboard_slot->isInUse())
174
 
      {
175
 
        if (current_status_vars)
176
 
        {
177
 
          current_status_vars->merge(scoreboard_slot->getStatusVars());
178
 
        }
179
 
 
180
 
        if (current_user_commands)
181
 
        {
182
 
          current_user_commands->merge(scoreboard_slot->getUserCommands());
183
 
        }
184
 
      }
185
 
    }
186
 
  }
187
 
}