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