Ninja
metrics.h
Go to the documentation of this file.
1 // Copyright 2011 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 #ifndef NINJA_METRICS_H_
16 #define NINJA_METRICS_H_
17 
18 #include <string>
19 #include <vector>
20 
21 #include "util.h" // For int64_t.
22 
23 /// The Metrics module is used for the debug mode that dumps timing stats of
24 /// various actions. To use, see METRIC_RECORD below.
25 
26 /// A single metrics we're tracking, like "depfile load time".
27 struct Metric {
28  std::string name;
29  /// Number of times we've hit the code path.
30  int count;
31  /// Total time (in platform-dependent units) we've spent on the code path.
33 };
34 
35 /// A scoped object for recording a metric across the body of a function.
36 /// Used by the METRIC_RECORD macro.
37 struct ScopedMetric {
38  explicit ScopedMetric(Metric* metric);
39  ~ScopedMetric();
40 
41 private:
43  /// Timestamp when the measurement started.
44  /// Value is platform-dependent.
46 };
47 
48 /// The singleton that stores metrics and prints the report.
49 struct Metrics {
50  Metric* NewMetric(const std::string& name);
51 
52  /// Print a summary report to stdout.
53  void Report();
54 
55 private:
56  std::vector<Metric*> metrics_;
57 };
58 
59 /// Get the current time as relative to some epoch.
60 /// Epoch varies between platforms; only useful for measuring elapsed time.
62 
63 /// A simple stopwatch which returns the time
64 /// in seconds since Restart() was called.
65 struct Stopwatch {
66  public:
67  Stopwatch() : started_(0) {}
68 
69  /// Seconds since Restart() call.
70  double Elapsed() const;
71 
72  void Restart() { started_ = NowRaw(); }
73 
74  private:
76  // Return the current time using the native frequency of the high resolution
77  // timer.
78  uint64_t NowRaw() const;
79 };
80 
81 /// The primary interface to metrics. Use METRIC_RECORD("foobar") at the top
82 /// of a function to get timing stats recorded for each call of the function.
83 #define METRIC_RECORD(name) \
84  static Metric* metrics_h_metric = \
85  g_metrics ? g_metrics->NewMetric(name) : NULL; \
86  ScopedMetric metrics_h_scoped(metrics_h_metric);
87 
88 /// A variant of METRIC_RECORD that doesn't record anything if |condition|
89 /// is false.
90 #define METRIC_RECORD_IF(name, condition) \
91  static Metric* metrics_h_metric = \
92  g_metrics ? g_metrics->NewMetric(name) : NULL; \
93  ScopedMetric metrics_h_scoped((condition) ? metrics_h_metric : NULL);
94 
95 extern Metrics* g_metrics;
96 
97 #endif // NINJA_METRICS_H_
Metrics * g_metrics
Definition: metrics.cc:28
int64_t GetTimeMillis()
Get the current time as relative to some epoch.
Definition: metrics.cc:111
The Metrics module is used for the debug mode that dumps timing stats of various actions.
Definition: metrics.h:27
int count
Number of times we've hit the code path.
Definition: metrics.h:30
std::string name
Definition: metrics.h:28
int64_t sum
Total time (in platform-dependent units) we've spent on the code path.
Definition: metrics.h:32
The singleton that stores metrics and prints the report.
Definition: metrics.h:49
std::vector< Metric * > metrics_
Definition: metrics.h:56
void Report()
Print a summary report to stdout.
Definition: metrics.cc:82
Metric * NewMetric(const std::string &name)
Definition: metrics.cc:73
A scoped object for recording a metric across the body of a function.
Definition: metrics.h:37
int64_t start_
Timestamp when the measurement started.
Definition: metrics.h:45
ScopedMetric(Metric *metric)
Definition: metrics.cc:57
~ScopedMetric()
Definition: metrics.cc:63
Metric * metric_
Definition: metrics.h:42
A simple stopwatch which returns the time in seconds since Restart() was called.
Definition: metrics.h:65
uint64_t NowRaw() const
Definition: metrics.cc:107
uint64_t started_
Definition: metrics.h:75
Stopwatch()
Definition: metrics.h:67
double Elapsed() const
Seconds since Restart() call.
Definition: metrics.cc:102
void Restart()
Definition: metrics.h:72
unsigned long long uint64_t
Definition: win32port.h:29
signed long long int64_t
A 64-bit integer type.
Definition: win32port.h:28