~alinuxninja/nginx-edge/trunk

« back to all changes in this revision

Viewing changes to debian/modules/ngx_pagespeed/psol/include/pagespeed/kernel/util/statistics_logger.h

  • Committer: Vivian
  • Date: 2015-12-04 18:20:11 UTC
  • Revision ID: git-v1:a36f2bc32e884f7473b3a47040e5411306144d7d
* Do not extract psol.tar.gz

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright 2013 Google Inc. All Rights Reserved.
2
 
//
3
 
// Licensed under the Apache License, Version 2.0 (the "License");
4
 
// you may not use this file except in compliance with the License.
5
 
// You may obtain a copy of the License at
6
 
//
7
 
//      http://www.apache.org/licenses/LICENSE-2.0
8
 
//
9
 
// Unless required by applicable law or agreed to in writing, software
10
 
// distributed under the License is distributed on an "AS IS" BASIS,
11
 
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
 
// See the License for the specific language governing permissions and
13
 
// limitations under the License.
14
 
//
15
 
// Author: bvb@google.com (Ben VanBerkum)
16
 
// Author: sligocki@google.com (Shawn Ligocki)
17
 
 
18
 
#ifndef PAGESPEED_KERNEL_BASE_STATISTICS_LOGGER_H_
19
 
#define PAGESPEED_KERNEL_BASE_STATISTICS_LOGGER_H_
20
 
 
21
 
#include <cstddef>
22
 
#include <map>
23
 
#include <utility>
24
 
#include <vector>
25
 
 
26
 
#include "pagespeed/kernel/base/basictypes.h"
27
 
#include "pagespeed/kernel/base/file_system.h"
28
 
#include "pagespeed/kernel/base/string.h"
29
 
#include "pagespeed/kernel/base/string_util.h"
30
 
 
31
 
namespace net_instaweb {
32
 
 
33
 
class MessageHandler;
34
 
class MutexedScalar;
35
 
class Statistics;
36
 
class StatisticsLogfileReader;
37
 
class Timer;
38
 
class UpDownCounter;
39
 
class Variable;
40
 
class Writer;
41
 
 
42
 
class StatisticsLogger {
43
 
 public:
44
 
  // Does not take ownership of any objects passed in.
45
 
  StatisticsLogger(
46
 
      int64 update_interval_ms, int64 max_logfile_size_kb,
47
 
      const StringPiece& log_file, MutexedScalar* last_dump_timestamp,
48
 
      MessageHandler* message_handler, Statistics* stats,
49
 
      FileSystem* file_system, Timer* timer);
50
 
  ~StatisticsLogger();
51
 
 
52
 
  // Writes filtered variable data in JSON format to the given writer.
53
 
  // Variable data is a time series collected from with data points from
54
 
  // start_time to end_time. Granularity is the minimum time difference
55
 
  // between each successive data point.
56
 
  void DumpJSON(bool dump_for_graphs, const StringSet& var_titles,
57
 
                int64 start_time, int64 end_time, int64 granularity_ms,
58
 
                Writer* writer, MessageHandler* message_handler) const;
59
 
 
60
 
  // If it's been longer than kStatisticsDumpIntervalMs, update the
61
 
  // timestamp to now and dump the current state of the Statistics.
62
 
  void UpdateAndDumpIfRequired();
63
 
 
64
 
  // Trim file down if it gets above max_logfile_size_kb.
65
 
  void TrimLogfileIfNeeded();
66
 
 
67
 
  // Preload all the variables required for statistics logging.  This
68
 
  // must be called after statistics have been established, and
69
 
  // before any logging is done.
70
 
  //
71
 
  // It is OK to call this multiple times (e.g. before & after a fork).
72
 
  void Init();
73
 
 
74
 
 private:
75
 
  friend class StatisticsLoggerTest;
76
 
 
77
 
  typedef std::vector<GoogleString> VariableInfo;
78
 
  typedef std::map<GoogleString, VariableInfo> VarMap;
79
 
 
80
 
  // Note that exactly one of these will be non-null; this is really
81
 
  // a union, but I'm too lazy to make the enum tag, and there's no
82
 
  // space advantage to doing so when there are only two choices.
83
 
  typedef std::pair<Variable*, UpDownCounter*> VariableOrCounter;
84
 
  typedef std::map<StringPiece, VariableOrCounter> VariableMap;
85
 
 
86
 
  // Export statistics to a writer. Only export stats needed for console.
87
 
  // current_time_ms: The time at which the dump was triggered.
88
 
  void DumpConsoleVarsToWriter(int64 current_time_ms, Writer* writer);
89
 
  // Save the variables listed in var_titles to the map.
90
 
  void ParseDataFromReader(const StringSet& var_titles,
91
 
                           StatisticsLogfileReader* reader,
92
 
                           std::vector<int64>* list_of_timestamps,
93
 
                           VarMap* parsed_var_data) const;
94
 
  // Save the variables needed by graphs page to the map.
95
 
  void ParseDataForGraphs(StatisticsLogfileReader* reader,
96
 
                          std::vector<int64>* list_of_timestamps,
97
 
                          VarMap* parsed_var_data) const;
98
 
  // Parse a string into a map of variable name -> value.
99
 
  // Note: parsed_var_data StringPieces point into logfile_var_data and thus
100
 
  // have same lifetime as it.
101
 
  void ParseVarDataIntoMap(StringPiece logfile_var_data,
102
 
                           std::map<StringPiece, StringPiece>* parsed_var_data)
103
 
      const;
104
 
  void PrintVarDataAsJSON(const VarMap& parsed_var_data, Writer* writer,
105
 
                          MessageHandler* message_handler) const;
106
 
  void PrintTimestampListAsJSON(const std::vector<int64>& list_of_timestamps,
107
 
                                Writer* writer,
108
 
                                MessageHandler* message_handler) const;
109
 
  void PrintJSON(const std::vector<int64>& list_of_timestamps,
110
 
                 const VarMap& parsed_var_data,
111
 
                 Writer* writer, MessageHandler* message_handler) const;
112
 
  void AddVariable(StringPiece var_name);
113
 
 
114
 
  // Initializes all stats that will be needed for logging. Only call this in
115
 
  // tests to make sure getting those stats will work.
116
 
  void InitStatsForTest();
117
 
 
118
 
  // The last_dump_timestamp not only contains the time of the last dump,
119
 
  // it also controls locking so that multiple threads can't dump at once.
120
 
  MutexedScalar* last_dump_timestamp_;
121
 
  MessageHandler* message_handler_;
122
 
  Statistics* statistics_;  // Needed so we can dump the stats contained here.
123
 
  // file_system_ and timer_ are owned by someone who called the constructor
124
 
  // (usually Apache's ServerContext).
125
 
  FileSystem* file_system_;
126
 
  Timer* timer_;    // Used to retrieve timestamps
127
 
  const int64 update_interval_ms_;
128
 
  const int64 max_logfile_size_kb_;
129
 
  GoogleString logfile_name_;
130
 
  VariableMap variables_to_log_;
131
 
 
132
 
  DISALLOW_COPY_AND_ASSIGN(StatisticsLogger);
133
 
};
134
 
 
135
 
// Handles reading the logfile created by StatisticsLogger.
136
 
class StatisticsLogfileReader {
137
 
 public:
138
 
  StatisticsLogfileReader(FileSystem::InputFile* file, int64 start_time,
139
 
                          int64 end_time, int64 granularity_ms,
140
 
                          MessageHandler* message_handler);
141
 
  ~StatisticsLogfileReader();
142
 
 
143
 
  // Reads the next timestamp in the file into timestamp and the corresponding
144
 
  // chunk of data into data. Returns true if new data has been read.
145
 
  // TODO(sligocki): Use a StringPiece* here to avoid extra copies. We need
146
 
  // to guarantee that the data pointed to by the StringPiece will be valid
147
 
  // for the right lifetime first.
148
 
  bool ReadNextDataBlock(int64* timestamp, GoogleString* data);
149
 
  int64 end_time() { return end_time_; }
150
 
 
151
 
 private:
152
 
  // TODO(sligocki): Use StringPiece here instead of char*.
153
 
  size_t BufferFind(const char* search_for, size_t start_at);
154
 
  int FeedBuffer();
155
 
 
156
 
  FileSystem::InputFile* file_;
157
 
  int64 start_time_;
158
 
  int64 end_time_;
159
 
  int64 granularity_ms_;
160
 
  MessageHandler* message_handler_;
161
 
  // Logfile buffer.
162
 
  GoogleString buffer_;
163
 
 
164
 
  DISALLOW_COPY_AND_ASSIGN(StatisticsLogfileReader);
165
 
};
166
 
 
167
 
}  // namespace net_instaweb
168
 
 
169
 
#endif  // PAGESPEED_KERNEL_BASE_STATISTICS_LOGGER_H_