-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstatistics.cc
290 lines (238 loc) · 5.94 KB
/
statistics.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//
// statistics.cc
//
// This file holds the implementation for the StatisticsMgr class.
//
// The class is designed to dynamically track statistics for the client.
// You can add any statistic that you would like to track via a call to
// StatisticsMgr::Register.
// There is no need to setup in advance which statistics that you want to
// track. The call to Register is sufficient.
// This is essentially a (poor-man's) simplified version of gprof.
// Andre Bergholz, who was the TA for the 2000 offering has written
// some (or maybe all) of this code.
#include <iostream>
#include "statistics.h"
#include <cstring>
using namespace std;
//
// Here are Statistics Keys utilized by the PF layer of the Redbase
// project.
//
const char *PF_GETPAGE = "GETPAGE";
const char *PF_PAGEFOUND = "PAGEFOUND";
const char *PF_PAGENOTFOUND = "PAGENOTFOUND";
const char *PF_READPAGE = "READPAGE"; // IO
const char *PF_WRITEPAGE = "WRITEPAGE"; // IO
const char *PF_FLUSHPAGES = "FLUSHPAGES";
//
// Statistic class
//
// This class will track a single statistic
//
// Default Constructor utilized by the templates
//
Statistic::Statistic()
{
psKey = NULL;
iValue = 0;
}
//
// Constructor utilized by the StatisticMgr class
//
// We are assured by the StatisticMgr that psKey_ is not a NULL pointer.
//
Statistic::Statistic(const char *psKey_)
{
psKey = new char[strlen(psKey_) + 1];
strcpy (psKey, psKey_);
iValue = 0;
}
//
// Copy constructor
//
Statistic::Statistic(const Statistic &stat)
{
psKey = new char[strlen(stat.psKey)+1];
strcpy (psKey, stat.psKey);
iValue = stat.iValue;
}
//
// Equality constructor
//
Statistic& Statistic::operator=(const Statistic &stat)
{
if (this==&stat)
return *this;
delete [] psKey;
psKey = new char[strlen(stat.psKey)+1];
strcpy (psKey, stat.psKey);
iValue = stat.iValue;
return *this;
}
//
// Destructor
//
Statistic::~Statistic()
{
delete [] psKey;
}
Boolean Statistic::operator==(const char *psKey_) const
{
return (strcmp(psKey_, psKey)==0);
}
// --------------------------------------------------------------
//
// StatisticMgr class
//
// This class will track a dynamic list of statistics.
//
//
// Register
//
// Register a change to a statistic. The psKey is the char* name of
// the statistic to be tracked. This method will look for the statistic
// name withing its list of statistics and perform the operation over the
// stored value. The piValue is utilized for some of the operations.
//
// Note: if the statistic isn't found (as it will not be the very first
// time) then it will be initialized to 0 - the default value.
//
RC StatisticsMgr::Register (const char *psKey, const Stat_Operation op,
const int *const piValue)
{
int i, iCount;
Statistic *pStat = NULL;
if (psKey==NULL || (op != STAT_ADDONE && piValue == NULL))
return STAT_INVALID_ARGS;
iCount = llStats.GetLength();
for (i=0; i < iCount; i++) {
pStat = llStats[i];
if (*pStat == psKey)
break;
}
// Check to see if we found the Stat
if (i==iCount)
// We haven't found it so create a new statistic
// with the key psKey and initial value of 0.
pStat = new Statistic( psKey );
// Now perform the operation over the statistic
switch (op) {
case STAT_ADDONE:
pStat->iValue++;
break;
case STAT_ADDVALUE:
pStat->iValue += *piValue;
break;
case STAT_SETVALUE:
pStat->iValue = *piValue;
break;
case STAT_MULTVALUE:
pStat->iValue *= *piValue;
break;
case STAT_DIVVALUE:
pStat->iValue = (int) (pStat->iValue/(*piValue));
break;
case STAT_SUBVALUE:
pStat->iValue -= *piValue;
break;
};
// Finally, if the statistic wasn't in the original list then add it to
// the list.
// JASON:: Confirm that it makes a copy of the object in line 229 of
// linkedlist.h.
if (i==iCount) {
llStats.Append(*pStat);
delete pStat;
}
return 0;
}
//
// Print
//
// Print out the information pertaining to a specific statistic
RC StatisticsMgr::Print(const char *psKey)
{
if (psKey==NULL)
return STAT_INVALID_ARGS;
int *iValue = Get(psKey);
if (iValue)
cout << psKey << "::" << *iValue << "\n";
else
return STAT_UNKNOWN_KEY;
delete iValue;
return 0;
}
//
// Get
//
// The Get method will return a pointer to the integer value associated
// with a particular statistic. If it cannot find the statistic then it
// will return NULL. The caller must remember to delete the memory
// returned when done.
//
int *StatisticsMgr::Get(const char *psKey)
{
int i, iCount;
Statistic *pStat = NULL;
iCount = llStats.GetLength();
for (i=0; i < iCount; i++) {
pStat = llStats[i];
if (*pStat == psKey)
break;
}
// Check to see if we found the Stat
if (i==iCount)
return NULL;
return new int(pStat->iValue);
}
//
// Print
//
// Print out all the statistics tracked
//
void StatisticsMgr::Print()
{
int i, iCount;
Statistic *pStat = NULL;
iCount = llStats.GetLength();
for (i=0; i < iCount; i++) {
pStat = llStats[i];
cout << pStat->psKey << "::" << pStat->iValue << "\n";
}
}
//
// Reset
//
// Reset a specific statistic. The easiest way to do this is to remove it
// completely from the list
//
RC StatisticsMgr::Reset(const char *psKey)
{
int i, iCount;
Statistic *pStat = NULL;
if (psKey==NULL)
return STAT_INVALID_ARGS;
iCount = llStats.GetLength();
for (i=0; i < iCount; i++) {
pStat = llStats[i];
if (*pStat == psKey)
break;
}
// If we found the statistic then remove it from the list
if (i!=iCount)
llStats.Delete(i);
else
return STAT_UNKNOWN_KEY;
return 0;
}
//
// Reset
//
// Reset all of the statistics. The easiest way is to tell the linklist of
// elements to Erase itself.
//
void StatisticsMgr::Reset()
{
llStats.Erase();
}