~drizzle-trunk/drizzle/development

1491.4.1 by Joe Daly
move any tracking logic out of logging_stats its almost just a entry point, rework logic on cumulative stats by user to use a atomic rather then a lock
1
/*
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
2
 * Copyright (C) 2010 Joseph Daly <skinny.moey@gmail.com>
1491.4.1 by Joe Daly
move any tracking logic out of logging_stats its almost just a entry point, rework logic on cumulative stats by user to use a atomic rather then a lock
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
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
30
#include <config.h>
2241.3.1 by Olaf van der Spek
Refactor Session::status_var
31
#include <drizzled/statistics_variables.h>
1491.4.1 by Joe Daly
move any tracking logic out of logging_stats its almost just a entry point, rework logic on cumulative stats by user to use a atomic rather then a lock
32
#include "cumulative_stats.h"
33
34
using namespace std;
1711.7.2 by Joseph Daly
add memory usage
35
using namespace drizzled;
1491.4.1 by Joe Daly
move any tracking logic out of logging_stats its almost just a entry point, rework logic on cumulative stats by user to use a atomic rather then a lock
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();
1491.4.2 by Joe Daly
rework atomics on cumulative user tables
44
  for (int32_t j=0; j < cumulative_stats_by_user_max; ++j)
1491.4.1 by Joe Daly
move any tracking logic out of logging_stats its almost just a entry point, rework logic on cumulative stats by user to use a atomic rather then a lock
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
1491.4.2 by Joe Daly
rework atomics on cumulative user tables
51
  last_valid_index= INVALID_INDEX;
1491.4.4 by Joe Daly
rework UserCommands class to have a simplier interface and remove get/increment functions
52
  isOpenUserSlots= true;
1491.4.1 by Joe Daly
move any tracking logic out of logging_stats its almost just a entry point, rework logic on cumulative stats by user to use a atomic rather then a lock
53
  global_stats= new GlobalStats();
1561.3.4 by Joe Daly
global status changes, will need updates to combine session and global status to a realistic class structure
54
  global_status_vars= new StatusVars();
1711.7.2 by Joseph Daly
add memory usage
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; 
1491.4.1 by Joe Daly
move any tracking logic out of logging_stats its almost just a entry point, rework logic on cumulative stats by user to use a atomic rather then a lock
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;
1561.3.4 by Joe Daly
global status changes, will need updates to combine session and global status to a realistic class structure
73
  delete global_status_vars;
1491.4.1 by Joe Daly
move any tracking logic out of logging_stats its almost just a entry point, rework logic on cumulative stats by user to use a atomic rather then a lock
74
}
75
1625.2.1 by Joe Daly
initial user stats impl
76
void CumulativeStats::logUserStats(ScoreboardSlot *scoreboard_slot, bool reserveSlot)
1491.4.1 by Joe Daly
move any tracking logic out of logging_stats its almost just a entry point, rework logic on cumulative stats by user to use a atomic rather then a lock
77
{
78
  vector<ScoreboardSlot *>::iterator cumulative_it= cumulative_stats_by_user_vector->begin();
79
80
  /* Search if this is a pre-existing user */
81
1491.4.2 by Joe Daly
rework atomics on cumulative user tables
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)
1491.4.1 by Joe Daly
move any tracking logic out of logging_stats its almost just a entry point, rework logic on cumulative stats by user to use a atomic rather then a lock
90
  {
91
    ScoreboardSlot *cumulative_scoreboard_slot= *cumulative_it;
92
    string user= cumulative_scoreboard_slot->getUser();
93
    if (user.compare(scoreboard_slot->getUser()) == 0)
94
    {
1625.2.1 by Joe Daly
initial user stats impl
95
      reserveSlot= false;
1491.4.1 by Joe Daly
move any tracking logic out of logging_stats its almost just a entry point, rework logic on cumulative stats by user to use a atomic rather then a lock
96
      cumulative_scoreboard_slot->merge(scoreboard_slot);
97
      break;
98
    }
99
    ++cumulative_it;
100
  }
1625.2.1 by Joe Daly
initial user stats impl
101
  
102
  if (reserveSlot)
1491.4.1 by Joe Daly
move any tracking logic out of logging_stats its almost just a entry point, rework logic on cumulative stats by user to use a atomic rather then a lock
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
    { 
1491.4.2 by Joe Daly
rework atomics on cumulative user tables
111
      int32_t our_index= last_valid_index.add_and_fetch(1);
1491.4.1 by Joe Daly
move any tracking logic out of logging_stats its almost just a entry point, rework logic on cumulative stats by user to use a atomic rather then a lock
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);
1491.4.2 by Joe Daly
rework atomics on cumulative user tables
118
        cumulative_scoreboard_slot->setInUse(true);
1491.4.1 by Joe Daly
move any tracking logic out of logging_stats its almost just a entry point, rework logic on cumulative stats by user to use a atomic rather then a lock
119
      } 
120
      else 
121
      {
1491.4.2 by Joe Daly
rework atomics on cumulative user tables
122
        last_valid_index= cumulative_stats_by_user_max - 1; 
1491.4.4 by Joe Daly
rework UserCommands class to have a simplier interface and remove get/increment functions
123
        isOpenUserSlots= false;
1491.4.1 by Joe Daly
move any tracking logic out of logging_stats its almost just a entry point, rework logic on cumulative stats by user to use a atomic rather then a lock
124
      }
125
    } 
126
  }
127
}
128
129
void CumulativeStats::logGlobalStats(ScoreboardSlot* scoreboard_slot)
130
{
131
  global_stats->updateUserCommands(scoreboard_slot); 
132
}
1491.4.2 by Joe Daly
rework atomics on cumulative user tables
133
1561.3.4 by Joe Daly
global status changes, will need updates to combine session and global status to a realistic class structure
134
void CumulativeStats::logGlobalStatusVars(ScoreboardSlot* scoreboard_slot)
135
{
136
  global_status_vars->merge(scoreboard_slot->getStatusVars());
137
}
138
2409.2.1 by Olaf van der Spek
cppcheck
139
int32_t CumulativeStats::getCumulativeStatsLastValidIndex() const
1491.4.2 by Joe Daly
rework atomics on cumulative user tables
140
{
2409.2.1 by Olaf van der Spek
cppcheck
141
  return last_valid_index < cumulative_stats_by_user_max ? last_valid_index : cumulative_stats_by_user_max;
1491.4.2 by Joe Daly
rework atomics on cumulative user tables
142
}
1561.3.4 by Joe Daly
global status changes, will need updates to combine session and global status to a realistic class structure
143
2409.2.1 by Olaf van der Spek
cppcheck
144
void CumulativeStats::sumCurrentScoreboard(Scoreboard *scoreboard, StatusVars *current_status_vars, UserCommands *current_user_commands)
1561.3.4 by Joe Daly
global status changes, will need updates to combine session and global status to a realistic class structure
145
{
146
  /* the vector of vectors */
2409.2.1 by Olaf van der Spek
cppcheck
147
  vector<vector<ScoreboardSlot* >* > *vector_of_scoreboard_vectors= scoreboard->getVectorOfScoreboardVectors();
1561.3.4 by Joe Daly
global status changes, will need updates to combine session and global status to a realistic class structure
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
      {
1616.2.1 by Joe Daly
update GLOBAL_STATEMENTS logic it was not including a sum of the current scoreboard only sessions that had terminated
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
        }
1561.3.4 by Joe Daly
global status changes, will need updates to combine session and global status to a realistic class structure
174
      }
175
    }
176
  }
177
}