~ps10gel/ubuntu/xenial/trafficserver/6.2.0

« back to all changes in this revision

Viewing changes to proxy/mgmt2/stats/StatProcessor.cc

  • Committer: Bazaar Package Importer
  • Author(s): Arno Toell
  • Date: 2011-01-13 11:49:18 UTC
  • Revision ID: james.westby@ubuntu.com-20110113114918-vu422h8dknrgkj15
Tags: upstream-2.1.5-unstable
ImportĀ upstreamĀ versionĀ 2.1.5-unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** @file
 
2
 
 
3
  A brief file description
 
4
 
 
5
  @section license License
 
6
 
 
7
  Licensed to the Apache Software Foundation (ASF) under one
 
8
  or more contributor license agreements.  See the NOTICE file
 
9
  distributed with this work for additional information
 
10
  regarding copyright ownership.  The ASF licenses this file
 
11
  to you under the Apache License, Version 2.0 (the
 
12
  "License"); you may not use this file except in compliance
 
13
  with the License.  You may obtain a copy of the License at
 
14
 
 
15
      http://www.apache.org/licenses/LICENSE-2.0
 
16
 
 
17
  Unless required by applicable law or agreed to in writing, software
 
18
  distributed under the License is distributed on an "AS IS" BASIS,
 
19
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
20
  See the License for the specific language governing permissions and
 
21
  limitations under the License.
 
22
 */
 
23
 
 
24
/***************************************/
 
25
/****************************************************************************
 
26
 *
 
27
 *  StatProcessor.cc - Functions for computing node and cluster stat
 
28
 *                          aggregation
 
29
 *
 
30
 *
 
31
 ****************************************************************************/
 
32
 
 
33
#include "ink_config.h"
 
34
#include "StatProcessor.h"
 
35
#include "ink_unused.h"
 
36
 
 
37
#define STAT_CONFIG_FILE "stats.config.xml"
 
38
 
 
39
StatObjectList statObjectList;
 
40
StatXMLTag currentTag = INVALID_TAG;
 
41
StatObject *statObject = NULL;
 
42
char *exprContent = NULL;
 
43
static unsigned statCount = 0;  // global statistics object counter
 
44
bool nodeVar;
 
45
bool sumClusterVar;
 
46
 
 
47
 
 
48
void
 
49
startElement(void *userData, const char *name, const char **atts)
 
50
{
 
51
  NOWARN_UNUSED(userData);
 
52
  NOWARN_UNUSED(name);
 
53
  int i = 0;
 
54
 
 
55
  if (!strcmp(name, "ink:statistics"))
 
56
    currentTag = ROOT_TAG;
 
57
  else if (!strcmp(name, "statistics"))
 
58
    currentTag = STAT_TAG;
 
59
  else if (!strcmp(name, "destination"))
 
60
    currentTag = DST_TAG;
 
61
  else if (!strcmp(name, "expression"))
 
62
    currentTag = EXPR_TAG;
 
63
  else
 
64
    currentTag = INVALID_TAG;
 
65
 
 
66
  switch (currentTag) {
 
67
  case STAT_TAG:
 
68
    statObject = NEW(new StatObject(++statCount));
 
69
    Debug(MODULE_INIT, "\nStat #: ----------------------- %d -----------------------\n", statCount);
 
70
 
 
71
    for (i = 0; atts[i]; i += 2) {
 
72
      ink_debug_assert(atts[i + 1]);    // Attribute comes in pairs, hopefully.
 
73
 
 
74
      if (!strcmp(atts[i], "minimum")) {
 
75
        statObject->m_stats_min = (MgmtFloat) atof(atts[i + 1]);
 
76
      } else if (!strcmp(atts[i], "maximum")) {
 
77
        statObject->m_stats_max = (MgmtFloat) atof(atts[i + 1]);
 
78
      } else if (!strcmp(atts[i], "interval")) {
 
79
        statObject->m_update_interval = (ink_hrtime) atoi(atts[i + 1]);
 
80
      } else if (!strcmp(atts[i], "debug")) {
 
81
        statObject->m_debug = (atts[i + 1] && atts[i + 1][0] == '1');
 
82
      }
 
83
 
 
84
      Debug(MODULE_INIT, "\tDESTINTATION w/ attribute: %s -> %s\n", atts[i], atts[i + 1]);
 
85
    }
 
86
    break;
 
87
 
 
88
  case EXPR_TAG:
 
89
    exprContent = NEW(new char[BUFSIZ * 10]);   // Will free up at endElement
 
90
    memset(exprContent, 0, BUFSIZ * 10);
 
91
    break;
 
92
 
 
93
  case DST_TAG:
 
94
    nodeVar = true;
 
95
    sumClusterVar = true;       // Should only be used with cluster variable
 
96
 
 
97
    for (i = 0; atts[i]; i += 2) {
 
98
      ink_debug_assert(atts[i + 1]);    // Attribute comes in pairs, hopefully.
 
99
      if (!strcmp(atts[i], "scope")) {
 
100
        nodeVar = (!strcmp(atts[i + 1], "node") ? true : false);
 
101
      } else if (!strcmp(atts[i], "operation")) {
 
102
        sumClusterVar = (!strcmp(atts[i + 1], "sum") ? true : false);
 
103
      }
 
104
 
 
105
      Debug(MODULE_INIT, "\tDESTINTATION w/ attribute: %s -> %s\n", atts[i], atts[i + 1]);
 
106
    }
 
107
 
 
108
    break;
 
109
 
 
110
  case INVALID_TAG:
 
111
    Debug(MODULE_INIT, "==========================================>%s<=\n", name);
 
112
    break;
 
113
 
 
114
  default:
 
115
    break;
 
116
  }
 
117
}
 
118
 
 
119
 
 
120
void
 
121
endElement(void *userData, const char *name)
 
122
{
 
123
  NOWARN_UNUSED(userData);
 
124
  NOWARN_UNUSED(name);
 
125
  switch (currentTag) {
 
126
  case STAT_TAG:
 
127
    statObjectList.enqueue(statObject);
 
128
    currentTag = ROOT_TAG;
 
129
    break;
 
130
 
 
131
  case EXPR_TAG:
 
132
    statObject->assignExpr(exprContent);
 
133
    delete(exprContent);
 
134
    // fall through
 
135
 
 
136
  default:
 
137
    currentTag = STAT_TAG;
 
138
    break;
 
139
  }
 
140
}
 
141
 
 
142
 
 
143
void
 
144
charDataHandler(void *userData, const XML_Char * name, int len)
 
145
{
 
146
  NOWARN_UNUSED(userData);
 
147
  NOWARN_UNUSED(len);
 
148
  if (currentTag != EXPR_TAG && currentTag != DST_TAG) {
 
149
    return;
 
150
  }
 
151
 
 
152
  char content[BUFSIZ * 10];
 
153
  if (XML_extractContent(name, content, BUFSIZ * 10) == 0) {
 
154
    return;
 
155
  }
 
156
 
 
157
  if (currentTag == EXPR_TAG) {
 
158
 
 
159
    strncat(exprContent, content, ((BUFSIZ * 10) - strlen(exprContent) - 1));
 
160
 
 
161
  } else {
 
162
 
 
163
    statObject->assignDst(content, nodeVar, sumClusterVar);
 
164
 
 
165
  }
 
166
 
 
167
}
 
168
 
 
169
 
 
170
StatProcessor::StatProcessor():m_lmgmt(NULL), m_overviewGenerator(NULL)
 
171
{
 
172
  rereadConfig();
 
173
}
 
174
 
 
175
 
 
176
void
 
177
StatProcessor::rereadConfig()
 
178
{
 
179
  textBuffer *fileContent = NULL;
 
180
  Rollback *fileRB = NULL;
 
181
  char *fileBuffer = NULL;
 
182
  version_t fileVersion;
 
183
  int fileLen;
 
184
 
 
185
  statObjectList.clean();
 
186
  statCount = 0;                // reset statistics counter
 
187
 
 
188
  int ret = configFiles->getRollbackObj(STAT_CONFIG_FILE, &fileRB);
 
189
  if (!ret) {
 
190
    Debug(MODULE_INIT, " Can't get Rollback for file: %s\n", STAT_CONFIG_FILE);
 
191
  }
 
192
  fileVersion = fileRB->getCurrentVersion();
 
193
  fileRB->getVersion(fileVersion, &fileContent);
 
194
  fileBuffer = fileContent->bufPtr();
 
195
  fileLen = strlen(fileBuffer);
 
196
 
 
197
  /*
 
198
   * Start the XML Praser -- the package used is EXPAT
 
199
   */
 
200
  XML_Parser parser = XML_ParserCreate(NULL);
 
201
  XML_SetUserData(parser, NULL);
 
202
  XML_SetElementHandler(parser, startElement, endElement);
 
203
  XML_SetCharacterDataHandler(parser, charDataHandler);
 
204
 
 
205
  /*
 
206
   * Substiture every newline with a space to get around
 
207
   * the SetCharacterDataHandler problem.
 
208
   */
 
209
  char *newlinePtr;
 
210
  while ((newlinePtr = strchr(fileBuffer, '\n')) != NULL || (newlinePtr = strchr(fileBuffer, '\r')) != NULL) {
 
211
    *newlinePtr = ' ';
 
212
  }
 
213
 
 
214
  /*
 
215
   * Parse the input file according to XML standard.
 
216
   * Print error if we encounter any
 
217
   */
 
218
  int status = XML_Parse(parser, fileBuffer, fileLen, true);
 
219
  if (!status) {
 
220
    mgmt_log(stderr, "%s at line %d\n", XML_ErrorString(XML_GetErrorCode(parser)), XML_GetCurrentLineNumber(parser));
 
221
  }
 
222
 
 
223
  /*
 
224
   * Cleaning upt
 
225
   */
 
226
  XML_ParserFree(parser);
 
227
  delete fileContent;
 
228
 
 
229
  Debug(MODULE_INIT, "\n\n---------- END OF PARSING & INITIALIZING ---------\n\n");
 
230
}
 
231
 
 
232
 
 
233
StatProcessor::~StatProcessor()
 
234
{
 
235
 
 
236
  Debug(MODULE_INIT, "[StatProcessor] Destructing Statistics Processor\n");
 
237
 
 
238
}
 
239
 
 
240
 
 
241
void
 
242
setTest()
 
243
{
 
244
  char var_name[64];
 
245
 
 
246
  for (int i = 1; i <= 5; i++) {
 
247
    memset(var_name, 0, 64);
 
248
    snprintf(var_name, sizeof(var_name), "proxy.node.stats.test%d", i);
 
249
    if (i == 4) {
 
250
      MgmtFloat tmp;
 
251
      varFloatFromName("proxy.node.stats.test4", &tmp);
 
252
      varSetFloat(var_name, tmp + 1, true);
 
253
    } else {
 
254
      varSetFloat(var_name, i, true);
 
255
    }
 
256
  }
 
257
}
 
258
 
 
259
 
 
260
void
 
261
verifyTest()
 
262
{
 
263
  MgmtFloat tmp1, tmp2;
 
264
 
 
265
  // 1. simple copy
 
266
  varFloatFromName("proxy.node.stats.test1", &tmp1);
 
267
  varFloatFromName("proxy.node.stats.test2", &tmp2);
 
268
  if (tmp1 == tmp2) {
 
269
    Debug(MODULE_INIT, "PASS -- simple copy");
 
270
  } else {
 
271
    Debug(MODULE_INIT, "FAIL -- simple copy");
 
272
  }
 
273
 
 
274
  // 2. simple interval
 
275
  varFloatFromName("proxy.node.stats.test3", &tmp2);
 
276
  if (tmp2 >= 10) {
 
277
    Debug(MODULE_INIT, "PASS -- simple interval & constant");
 
278
  } else {
 
279
    Debug(MODULE_INIT, "FAIL -- simple interval & constant %f", tmp2);
 
280
  }
 
281
 
 
282
  // 3. delta
 
283
  varFloatFromName("proxy.node.stats.test4", &tmp2);
 
284
  if ((tmp2 > 150) && (tmp2 < 250)) {
 
285
    Debug(MODULE_INIT, "PASS -- delta");
 
286
  } else {
 
287
    Debug(MODULE_INIT, "FAIL -- delta %f", tmp2);
 
288
  }
 
289
}
 
290
 
 
291
 
 
292
/**
 
293
 * Updating the statistics NOW.
 
294
 **/
 
295
unsigned short
 
296
StatProcessor::processStat()
 
297
{
 
298
  unsigned short result = 0;
 
299
 
 
300
  Debug(MODULE_INIT, "[StatProcessor] Processing Statistics....\n");
 
301
 
 
302
//    setTest();
 
303
  statObjectList.Eval();
 
304
//    verifyTest();
 
305
 
 
306
  return (result);
 
307
}
 
308
 
 
309
 
 
310
/**
 
311
 * ExpressionEval
 
312
 * --------------
 
313
 *
 
314
 */
 
315
MgmtFloat
 
316
ExpressionEval(char *exprString)
 
317
{
 
318
  StatObject statObject;
 
319
 
 
320
  char content[BUFSIZ * 10];
 
321
  XML_extractContent(exprString, content, BUFSIZ * 10);
 
322
 
 
323
  statObject.assignExpr(content);
 
324
  return statObject.NodeStatEval(false);
 
325
}