1
// Copyright 2013 Google Inc. All Rights Reserved.
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
7
// http://www.apache.org/licenses/LICENSE-2.0
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.
15
// Author: bvb@google.com (Ben VanBerkum)
16
// Author: sligocki@google.com (Shawn Ligocki)
18
#ifndef PAGESPEED_KERNEL_BASE_STATISTICS_LOGGER_H_
19
#define PAGESPEED_KERNEL_BASE_STATISTICS_LOGGER_H_
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"
31
namespace net_instaweb {
36
class StatisticsLogfileReader;
42
class StatisticsLogger {
44
// Does not take ownership of any objects passed in.
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);
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;
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();
64
// Trim file down if it gets above max_logfile_size_kb.
65
void TrimLogfileIfNeeded();
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.
71
// It is OK to call this multiple times (e.g. before & after a fork).
75
friend class StatisticsLoggerTest;
77
typedef std::vector<GoogleString> VariableInfo;
78
typedef std::map<GoogleString, VariableInfo> VarMap;
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;
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)
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,
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);
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();
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_;
132
DISALLOW_COPY_AND_ASSIGN(StatisticsLogger);
135
// Handles reading the logfile created by StatisticsLogger.
136
class StatisticsLogfileReader {
138
StatisticsLogfileReader(FileSystem::InputFile* file, int64 start_time,
139
int64 end_time, int64 granularity_ms,
140
MessageHandler* message_handler);
141
~StatisticsLogfileReader();
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_; }
152
// TODO(sligocki): Use StringPiece here instead of char*.
153
size_t BufferFind(const char* search_for, size_t start_at);
156
FileSystem::InputFile* file_;
159
int64 granularity_ms_;
160
MessageHandler* message_handler_;
162
GoogleString buffer_;
164
DISALLOW_COPY_AND_ASSIGN(StatisticsLogfileReader);
167
} // namespace net_instaweb
169
#endif // PAGESPEED_KERNEL_BASE_STATISTICS_LOGGER_H_