~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
/*
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
35
CumulativeStats::CumulativeStats(uint32_t in_cumulative_stats_by_user_max) 
36
    :
37
      cumulative_stats_by_user_max(in_cumulative_stats_by_user_max)  
38
{
39
  cumulative_stats_by_user_vector= new vector<ScoreboardSlot *>(cumulative_stats_by_user_max);
40
41
  vector<ScoreboardSlot *>::iterator it= cumulative_stats_by_user_vector->begin();
1491.4.2 by Joe Daly
rework atomics on cumulative user tables
42
  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
43
  {
44
    ScoreboardSlot *scoreboard_slot= new ScoreboardSlot();
45
    it= cumulative_stats_by_user_vector->insert(it, scoreboard_slot);
46
  }
47
  cumulative_stats_by_user_vector->resize(cumulative_stats_by_user_max);
48
1491.4.2 by Joe Daly
rework atomics on cumulative user tables
49
  last_valid_index= INVALID_INDEX;
1491.4.4 by Joe Daly
rework UserCommands class to have a simplier interface and remove get/increment functions
50
  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
51
  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
52
  global_status_vars= new StatusVars();
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
}
54
55
CumulativeStats::~CumulativeStats()
56
{
57
  vector<ScoreboardSlot *>::iterator it= cumulative_stats_by_user_vector->begin();
58
  for (; it < cumulative_stats_by_user_vector->end(); ++it)
59
  {
60
    delete *it;
61
  }
62
  cumulative_stats_by_user_vector->clear();
63
  delete cumulative_stats_by_user_vector;
64
  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
65
  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
66
}
67
1625.2.1 by Joe Daly
initial user stats impl
68
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
69
{
70
  vector<ScoreboardSlot *>::iterator cumulative_it= cumulative_stats_by_user_vector->begin();
71
72
  /* Search if this is a pre-existing user */
73
1491.4.2 by Joe Daly
rework atomics on cumulative user tables
74
  int32_t current_index= last_valid_index;
75
76
  if (cumulative_stats_by_user_max <= current_index)
77
  {
78
    current_index= cumulative_stats_by_user_max;
79
  }
80
81
  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
82
  {
83
    ScoreboardSlot *cumulative_scoreboard_slot= *cumulative_it;
84
    string user= cumulative_scoreboard_slot->getUser();
85
    if (user.compare(scoreboard_slot->getUser()) == 0)
86
    {
1625.2.1 by Joe Daly
initial user stats impl
87
      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
88
      cumulative_scoreboard_slot->merge(scoreboard_slot);
89
      break;
90
    }
91
    ++cumulative_it;
92
  }
1625.2.1 by Joe Daly
initial user stats impl
93
  
94
  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
95
  {
96
    /* the user was not found */
97
    /* its possible multiple simultaneous connections with the same user
98
       could result in duplicate entries here, not likely and it would
99
       be harmless except for duplicate users showing up in a query */
100
    
101
    if (hasOpenUserSlots())
102
    { 
1491.4.2 by Joe Daly
rework atomics on cumulative user tables
103
      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
104
      if (our_index < cumulative_stats_by_user_max)
105
      {
106
        ScoreboardSlot *cumulative_scoreboard_slot=
107
          cumulative_stats_by_user_vector->at(our_index);
108
        cumulative_scoreboard_slot->setUser(scoreboard_slot->getUser());
109
        cumulative_scoreboard_slot->merge(scoreboard_slot);
1491.4.2 by Joe Daly
rework atomics on cumulative user tables
110
        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
111
      } 
112
      else 
113
      {
1491.4.2 by Joe Daly
rework atomics on cumulative user tables
114
        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
115
        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
116
      }
117
    } 
118
  }
119
}
120
121
void CumulativeStats::logGlobalStats(ScoreboardSlot* scoreboard_slot)
122
{
123
  global_stats->updateUserCommands(scoreboard_slot); 
124
}
1491.4.2 by Joe Daly
rework atomics on cumulative user tables
125
1561.3.4 by Joe Daly
global status changes, will need updates to combine session and global status to a realistic class structure
126
void CumulativeStats::logGlobalStatusVars(ScoreboardSlot* scoreboard_slot)
127
{
128
  global_status_vars->merge(scoreboard_slot->getStatusVars());
129
}
130
131
int32_t CumulativeStats::getCumulativeStatsLastValidIndex()
1491.4.2 by Joe Daly
rework atomics on cumulative user tables
132
{
133
  if (last_valid_index < cumulative_stats_by_user_max)
134
  {
135
    return last_valid_index;
136
  } 
137
  else 
138
  {
139
    return cumulative_stats_by_user_max;
140
  }
141
}
1561.3.4 by Joe Daly
global status changes, will need updates to combine session and global status to a realistic class structure
142
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
143
void CumulativeStats::sumCurrentScoreboard(Scoreboard *scoreboard,
144
                                           StatusVars *current_status_vars,
145
                                           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
146
{
147
  /* the vector of vectors */
148
  vector<vector<ScoreboardSlot* >* > *vector_of_scoreboard_vectors= 
149
    scoreboard->getVectorOfScoreboardVectors();
150
151
  /* iterate through each vector from above and sum each ScoreboardSlot */
152
153
  vector<vector<ScoreboardSlot* >* >::iterator v_of_scoreboard_v_begin_it= vector_of_scoreboard_vectors->begin(); 
154
155
  vector<vector<ScoreboardSlot* >* >::iterator v_of_scoreboard_v_end_it= vector_of_scoreboard_vectors->end(); 
156
157
  for (; v_of_scoreboard_v_begin_it != v_of_scoreboard_v_end_it; ++v_of_scoreboard_v_begin_it)
158
  {
159
    vector<ScoreboardSlot* > *scoreboard_vector= *v_of_scoreboard_v_begin_it;
160
    
161
    vector<ScoreboardSlot* >::iterator scoreboard_vector_it= scoreboard_vector->begin();
162
    vector<ScoreboardSlot* >::iterator scoreboard_vector_end= scoreboard_vector->end();
163
    for (; scoreboard_vector_it != scoreboard_vector_end; ++scoreboard_vector_it)
164
    {
165
      ScoreboardSlot *scoreboard_slot= *scoreboard_vector_it;
166
      if (scoreboard_slot->isInUse())
167
      {
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
168
        if (current_status_vars)
169
        {
170
          current_status_vars->merge(scoreboard_slot->getStatusVars());
171
        }
172
173
        if (current_user_commands)
174
        {
175
          current_user_commands->merge(scoreboard_slot->getUserCommands());
176
        }
1561.3.4 by Joe Daly
global status changes, will need updates to combine session and global status to a realistic class structure
177
      }
178
    }
179
  }
180
}